Need a MySQL report of Enrollments in all courses in Moodle site

Re: Need a MySQL report of Enrollments in all courses in Moodle site

by tony chesney -
Number of replies: 0

I think this sql will give you the teacher(s), followed by the students, for each moodle course in your database

SELECT c.fullname as coursename, r.name as rolename, u.lastname as usersname FROM `mdl2_course` c
JOIN mdl2_context con
ON con.instanceid = c.id
JOIN mdl2_role_assignments ra
ON con.id = ra.contextid
JOIN mdl2_role r
ON ra.roleid = r.id
JOIN mdl2_user u
ON u.id = ra.userid
WHERE contextlevel = 50
AND (r.id = 3 OR r.id = 5)
ORDER BY c.fullname, rolename DESC

Role id 3 is teacher, role id 5 is student.  The context levels are in lib/accesslib.php  from your root directory (if you have access to the files).  contextlevel 50 is the value given to the constant CONTEXT_COURSE in this file.