From 71d5abb422b62c3b720ffa30d49919eaeefe2f56 Mon Sep 17 00:00:00 2001 From: Jake Dallimore Date: Thu, 14 Sep 2017 14:10:48 +0800 Subject: [PATCH] MDL-37810 roles: get_user_roles_in_course returns all relevant roles Fixed to return both those roles in the profileroles site policy and any roles which the current user can assign in the respective context, meaning a user can see a link to any role they can assign. --- lib/accesslib.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) 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);