Help with adding a cohort filter

Help with adding a cohort filter

by Dan Bell -
Number of replies: 1

Hi,

A little new to configurable reports, and Im no sql expert. Thats only two strikes.

Ive been creating many reports since I recently installed the plugin and using some of the many sql examples from moodle docs page. Now I want to add a cohort filter to many of my reports. I found documentation on how to do that, although Im still working on that...

Anyways I also found this previous post here that appears to have the answer, but not the actual sql that solved the problem, and I cant figure out  how to incorporate the logic and where clause with this sql. In other words, the sql in that post worked for me (see below), but when talking about working in the logic for cohorts and adding a where clause is not working for me. Can anyone suggest the edit (and/or any additional steps needed) for this sql to add a cohort filter?

SELECT DISTINCT u.lastname as "Last Name", u.firstname as "First Name", concat('<a target="_new" href="%%WWWROOT%%/course/view.php?id=',c.id,'">',c.fullname,'</a>') AS Course,

case 

  when ccom.timecompleted IS NULL then concat('<a target="_new" href="%%WWWROOT%%/report/completion/index.php?course=',c.id,'">', 'Not Complete','</a>')

  when ccom.timecompleted IS NOT NULL then 'Completed'

end as "Completion Status"

FROM {user} AS u 

  JOIN {course_completions} AS ccom ON u.id = ccom.userid

  JOIN {course} AS c ON c.id = ccom.course

ORDER BY u.lastname, u.firstname,c.fullname


Average of ratings: -
In reply to Dan Bell

Re: Help with adding a cohort filter

by Gregor McNish -
Picture of Particularly helpful Moodlers
I'm not sure about the configurable reports part of it -- cohorts isn't one of the filters specfied in the documentation, but if you're able to enter the cohort name or id, the sql to select the above, but filtered on cohorts would be
SELECT DISTINCT u.lastname as "Last Name", u.firstname as "First Name", concat('',c.fullname,'') AS Course,
case
when ccom.timecompleted IS NULL then concat('', 'Not Complete','')
when ccom.timecompleted IS NOT NULL then 'Completed'
end as "Completion Status"

FROM {user} AS u
JOIN {course_completions} AS ccom ON u.id = ccom.userid
JOIN {course} AS c ON c.id = ccom.course
join {cohort_members} cm on cm.userid=u.id
join {cohort} co on co.id=cm.cohortid
where co.idnumber= (here is where you put the input variable)
 ORDER BY u.lastname, u.firstname,c.fullname

if you want to filter on cohort name then it would be where co.name= ...
Note that in this case cohort membership is treated independently of the course; if you're using cohort enrolments, then you may want to factor that in, so for a cohort you only see the list of people and courses relevant to that cohort., rather than all courses for people in a particular cohort.
Average of ratings: Useful (1)