}
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.
$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';
$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';