How to get list of all users with a specific custom role in a course

Re: How to get list of all users with a specific custom role in a course

by Moumita Adak -
Number of replies: 3
Hi,
Please go to Your course > Participants > Filter by your custom role by clicking the "Search keyword or select filter" drop-down > Then it will display the list of all users who are assigned this custom role in this course.

Please make sure you have created the custom role from Site administration > Users > Permissions > Define roles > by clicking the "Add a new role" button.

Regards,
Moumita
In reply to Moumita Adak

Re: How to get list of all users with a specific custom role in a course

by Ratish Saini -
Thanks for replying but I was looking for sql query to be used in a custom plugin.
In reply to Ratish Saini

Re: How to get list of all users with a specific custom role in a course

by Nilesh Pathade -
May be that will help you :

SELECT u.id, c.id, cc.id
FROM mdl_user u
INNER JOIN mdl_role_assignments ra ON ra.userid = u.id
INNER JOIN mdl_context ct ON ct.id = ra.contextid
INNER JOIN mdl_course c ON c.id = ct.instanceid
INNER JOIN mdl_role r ON r.id = ra.roleid
INNER JOIN mdl_course_categories cc ON cc.id = c.category
WHERE r.id =
Please check this all database table which you will get all relation.

Happy Day smile
In reply to Ratish Saini

Re: How to get list of all users with a specific custom role in a course

by Moumita Adak -
Hi Ratish,
You could use
$DB->get_records_sql("SELECT u.* FROM {user} u, {role_assignments} ra, {role} r, {context} c WHERE ra.userid = u.id AND r.id = ra.roleid AND c.id = ra.contextid AND u.deleted = ? AND u.suspended = ? AND c.instanceid = ? AND r.shortname = ?", array(0, 0, courseid, "Short name of custom role"));

Thanks & Regards,
Moumita