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.
This commit is contained in:
Jake Dallimore
2017-09-18 09:00:31 +08:00
parent a8f69dbed0
commit 71d5abb422
+9 -6
View File
@@ -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);