From 3e09bb690b691fffc6246a478f132dded416612d Mon Sep 17 00:00:00 2001 From: Eloy Lafuente Date: Mon, 19 Jul 2010 17:51:17 +0000 Subject: [PATCH] MDL-23400 accesslib - get_assignable_roles() now accepts $user param --- lib/accesslib.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/accesslib.php b/lib/accesslib.php index b3dcd389c0b..3ebbf2e5fc0 100755 --- a/lib/accesslib.php +++ b/lib/accesslib.php @@ -4304,14 +4304,22 @@ function allow_switch($fromroleid, $targetroleid) { * @param int $rolenamedisplay the type of role name to display. One of the * ROLENAME_X constants. Default ROLENAME_ALIAS. * @param bool $withusercounts if true, count the number of users with each role. + * @param integer|object $user A user id or object. By default (NULL) checks the permissions of the current user. * @return array if $withusercounts is false, then an array $roleid => $rolename. * if $withusercounts is true, returns a list of three arrays, * $rolenames, $rolecounts, and $nameswithcounts. */ -function get_assignable_roles($context, $rolenamedisplay = ROLENAME_ALIAS, $withusercounts = false) { +function get_assignable_roles($context, $rolenamedisplay = ROLENAME_ALIAS, $withusercounts = false, $user = null) { global $USER, $DB; - if (!has_capability('moodle/role:assign', $context)) { + // make sure there is a real user specified + if ($user === null) { + $userid = !empty($USER->id) ? $USER->id : 0; + } else { + $userid = !empty($user->id) ? $user->id : $user; + } + + if (!has_capability('moodle/role:assign', $context, $userid)) { if ($withusercounts) { return array(array(), array(), array()); } else { @@ -4336,7 +4344,7 @@ function get_assignable_roles($context, $rolenamedisplay = ROLENAME_ALIAS, $with $params['conid'] = $context->id; } - if (is_siteadmin()) { + if (is_siteadmin($userid)) { // show all roles allowed in this context to admins $assignrestriction = ""; } else { @@ -4345,7 +4353,7 @@ function get_assignable_roles($context, $rolenamedisplay = ROLENAME_ALIAS, $with JOIN {role_assignments} ra ON ra.roleid = raa.roleid WHERE ra.userid = :userid AND ra.contextid IN ($contexts) ) ar ON ar.id = r.id"; - $params['userid'] = $USER->id; + $params['userid'] = $userid; } $params['contextlevel'] = $context->contextlevel; $sql = "SELECT r.id, r.name $extrafields -- 2.43.0