From: tjhunt Date: Fri, 21 Nov 2008 05:46:45 +0000 (+0000) Subject: role assign: MDL-17323 Show users with the role in a parent context as disabled opito... X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=698ae7eb24301d5842565bd4f482412540811400;p=moodle.git role assign: MDL-17323 Show users with the role in a parent context as disabled opitons in the existing users box --- diff --git a/admin/roles/lib.php b/admin/roles/lib.php index 9f8088d5ca..f65ee7efb2 100644 --- a/admin/roles/lib.php +++ b/admin/roles/lib.php @@ -1114,22 +1114,95 @@ class existing_role_holders extends role_assign_user_selector_base { } public function find_users($search) { + global $DB; + list($wherecondition, $params) = $this->search_sql($search, 'u'); - $contextusers = get_role_users($this->roleid, $this->context, false, - $this->required_fields_sql('u') . ', ra.hidden', 'u.lastname, u.firstname', - true, '', '', '', $wherecondition, $params); + list($ctxcondition, $ctxparams) = $DB->get_in_or_equal(get_parent_contexts($this->context, true)); + $params = array_merge($params, $ctxparams); + $params[] = $this->roleid; + $sql = "SELECT ra.id as raid," . $this->required_fields_sql('u') . ",ra.hidden,ra.contextid + FROM {role_assignments} ra + JOIN {user} u ON u.id = ra.userid + JOIN {context} ctx ON ra.contextid = ctx.id + WHERE + $wherecondition AND + ctx.id $ctxcondition AND + ra.roleid = ? + ORDER BY ctx.depth DESC, u.lastname, u.firstname"; + $contextusers = $DB->get_records_sql($sql, $params); + + // No users at all. if (empty($contextusers)) { return array(); } + // We have users. Out put them in groups by context depth. + // To help the loop below, tack a dummy user on the end of the results + // array, to trigger output of the last group. + $dummyuser = new stdClass; + $dummyuser->contextid = 0; + $dummyuser->id = 0; + $contextusers[] = $dummyuser; + $results = array(); // The results array we are building up. + $doneusers = array(); // Ensures we only list each user at most once. + $currentcontextid = $this->context->id; + $currentgroup = array(); + foreach ($contextusers as $user) { + if (isset($doneusers[$user->id])) { + continue; + } + $doneusers[$user->id] = 1; + if ($user->contextid != $currentcontextid) { + // We have got to the end of the previous group. Add it to the results array. + if ($currentcontextid == $this->context->id) { + $groupname = $this->this_con_group_name($search, count($currentgroup)); + } else { + $groupname = $this->parent_con_group_name($search, $currentcontextid); + } + $results[$groupname] = $currentgroup; + // Get ready for the next group. + $currentcontextid = $user->contextid; + $currentgroup = array(); + } + // Add this user to the group we are building up. + unset($user->contextid); + if ($currentcontextid != $this->context->id) { + $user->disabled = true; + } + $currentgroup[$user->id] = $user; + } + + return $results; + } + + protected function this_con_group_name($search, $numusers) { if ($search) { - $groupname = get_string('extusersmatching', 'role', $search); + if ($numusers) { + return get_string('usersinthiscontextmatching', 'role', $search); + } else { + return get_string('noneinthiscontextmatching', 'role', $search); + } } else { - $groupname = get_string('extusers', 'role'); + if ($numusers) { + return get_string('usersinthiscontext', 'role'); + } else { + return get_string('noneinthiscontext', 'role'); + } } + } - return array($groupname => $contextusers); + protected function parent_con_group_name($search, $contextid) { + $context = get_context_instance_by_id($contextid); + $contextname = print_context_name($context, true, true); + if ($search) { + $a = new stdClass; + $a->contextname = $contextname; + $a->search = $search; + return get_string('usersfrommatching', 'role', $a); + } else { + return get_string('usersfrom', 'role', $contextname); + } } // Override to add (hidden) to hidden role assignments. diff --git a/lang/en_utf8/role.php b/lang/en_utf8/role.php index 18a28ece88..f88d9ababa 100644 --- a/lang/en_utf8/role.php +++ b/lang/en_utf8/role.php @@ -134,6 +134,8 @@ $string['morethan'] = 'More than $a'; $string['multipleroles'] = 'Multiple roles'; $string['my:manageblocks'] = 'Manage myMoodle page blocks'; $string['nocapabilitiesincontext'] = 'No capabilities available in this context'; +$string['noneinthiscontext'] = 'None in this context'; +$string['noneinthiscontextmatching'] = 'No users matching \'$a\' in this context'; $string['notabletoassignroleshere'] = 'You are not able to assign any roles here'; $string['notabletooverrideroleshere'] = 'You are not able to override the permissions on any roles here'; $string['notset'] = 'Not set'; @@ -226,6 +228,10 @@ $string['user:viewdetails'] = 'View user profiles'; $string['user:viewhiddendetails'] = 'View hidden details of users'; $string['user:viewuseractivitiesreport'] = 'See user activity reports'; $string['userhashiddenassignments'] = 'This user has one or more hidden role assignments in this course'; +$string['usersfrom'] = 'Users from $a'; +$string['usersfrommatching'] = 'Users from $a->contextname matching \'$a->search\''; +$string['usersinthiscontext'] = 'Users in this context'; +$string['usersinthiscontextmatching'] = 'Users in this context matching \'$a\''; $string['userswiththisrole'] = 'Users with role'; $string['userswithrole'] = 'All users with a role'; $string['useshowadvancedtochange'] = 'Use \'Show advanced\' to change'; diff --git a/lib/accesslib.php b/lib/accesslib.php index 451cc1f2c1..b2d9a2119e 100755 --- a/lib/accesslib.php +++ b/lib/accesslib.php @@ -3640,7 +3640,7 @@ function role_context_capabilities($roleid, $context, $cap='') { * @param object $context * @return array() */ -function get_parent_contexts($context) { +function get_parent_contexts($context, $includeself = false) { if ($context->path == '') { return array(); @@ -3648,7 +3648,9 @@ function get_parent_contexts($context) { $parentcontexts = substr($context->path, 1); // kill leading slash $parentcontexts = explode('/', $parentcontexts); - array_pop($parentcontexts); // and remove its own id + if (!$includeself) { + array_pop($parentcontexts); // and remove its own id + } return array_reverse($parentcontexts); }