diff --git a/lib/accesslib.php b/lib/accesslib.php index 62b1395a6f9..0ca34fbe07a 100644 --- a/lib/accesslib.php +++ b/lib/accesslib.php @@ -2820,18 +2820,21 @@ function get_roles_used_in_context(context $context) { */ function get_user_roles_in_course($userid, $courseid) { global $CFG, $DB; - - if (empty($CFG->profileroles)) { - return ''; - } - if ($courseid == SITEID) { $context = context_system::instance(); } else { $context = context_course::instance($courseid); } + // If the current user can assign roles, then they can also see those assignable roles on the profile and participants page, + // provided the roles are assigned to at least 1 user in the context. + $policyroles = empty($CFG->profileroles) ? [] : array_map('trim', explode(',', $CFG->profileroles)); + $assignableroles = array_keys(get_assignable_roles($context)); + $rolesinscope = array_values(array_unique(array_merge($policyroles, $assignableroles))); + if (empty($rolesinscope)) { + return ''; + } - list($rallowed, $params) = $DB->get_in_or_equal(explode(',', $CFG->profileroles), SQL_PARAMS_NAMED, 'a'); + list($rallowed, $params) = $DB->get_in_or_equal($rolesinscope, SQL_PARAMS_NAMED, 'a'); list($contextlist, $cparams) = $DB->get_in_or_equal($context->get_parent_context_ids(true), SQL_PARAMS_NAMED, 'p'); $params = array_merge($params, $cparams);