SQL report of Cohorts on Courses

Re: SQL report of Cohorts on Courses

by Pavel Lomov -
Number of replies: 0

Try this query - it is not so optimal because it represent cohort, which members have role 'student' in some courses.

SELECT DISTINCT
h.name AS "Cohort",
c.id AS "ID course",
c.fullname AS "Course name"
FROM prefix_cohort AS h
JOIN prefix_cohort_members AS hm ON h.id = hm.cohortid
JOIN prefix_user AS u ON hm.userid = u.id
JOIN prefix_user_enrolments AS ue ON ue.userid = u.id
JOIN prefix_enrol AS e ON e.id = ue.enrolid
JOIN prefix_role_assignments AS ra ON ra.userid = u.id
JOIN prefix_context AS ct ON ct.id = ra.contextid AND ct.contextlevel = 50
JOIN prefix_course AS c ON c.id = ct.instanceid AND e.courseid = c.id
JOIN prefix_role AS r ON r.id = ra.roleid AND r.shortname = 'student'
WHERE 
UPPER(h.idnumber) LIKE UPPER(:idcohort) 
OR
UPPER(h.name) LIKE UPPER(:cohortname) 
AND e.status = 0 AND u.suspended = 0 AND u.deleted = 0
AND (ue.timeend = 0 OR ue.timeend > NOW()) AND ue.status = 0
ORDER BY h.name

PS: K.O - Query sholbe used in Ad-hoc database queries plugin

Average of ratings: Useful (2)