Get users with there roles for certain course

Get users with there roles for certain course

by Raphael Goldman -
Number of replies: 3

Hi,


I need to build a query that display all the users and their roles for cerain course.

I tried this query: 

But it take in account only roleid = 5.

SELECT

user2.firstname AS Firstname,

user2.lastname AS Lastname,

user2.email AS Email,

user2.city AS City,

course.fullname AS Course

,(SELECT shortname FROM mdl_role WHERE id=en.roleid) AS ROLE

,(SELECT name FROM mdl_role WHERE id=en.roleid) AS RoleName

 

FROM mdl_course AS course 

JOIN mdl_enrol AS en ON en.courseid = course.id

JOIN mdl_user_enrolments AS ue ON ue.enrolid = en.id

JOIN mdl_user AS user2 ON ue.userid = user2.id


I need all roles, I tried playing also with table role_assigments but that gave me only special roles (like manager).


How can I get all the roles?


Thanks

Average of ratings: -
In reply to Raphael Goldman

Re: Get users with there roles for certain course

by Emma Richardson -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers

Here is one I use to get student enrollments.  You are missing the context table in your query.  You should be able to take mine, adjust to what you need (add a column for role) and remove the specific role reference at the end.  Mine is specific to a single category but you can remove that line.

SELECT DISTINCT c.shortname, usr.firstname, usr.lastname, usr.city, groups.name

FROM mdl_course AS c

INNER JOIN mdl_context AS cx ON c.id = cx.instanceid

AND cx.contextlevel = '50'

AND c.category = '4'

INNER JOIN mdl_role_assignments AS ra ON cx.id = ra.contextid

INNER JOIN mdl_role AS r ON ra.roleid = r.id

INNER JOIN mdl_user AS usr ON ra.userid = usr.id

INNER JOIN mdl_groups AS groups ON c.id = groups.courseid

INNER JOIN mdl_groups_members AS members ON groups.id = members.groupid

WHERE members.userid = usr.id

AND r.name = "Student"

ORDER BY groups.name

Average of ratings: Useful (1)
In reply to Emma Richardson

תשובה ל: Re: Get users with there roles for certain course

by Raphael Goldman -

Thank you very much I changed it a little and it's working for my needs.

In reply to Raphael Goldman

Re: תשובה ל: Re: Get users with there roles for certain course

by chahnez keskes -

Hello,

Can you Raphael copy your query please ?

Thank you