2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Existing user selector.
21 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') ||
die();
28 * User selector subclass for the list of users who already have the role in
29 * question on the assign roles page.
31 class core_role_existing_role_holders
extends core_role_assign_user_selector_base
{
33 public function find_users($search) {
36 list($wherecondition, $params) = $this->search_sql($search, 'u');
37 list($ctxcondition, $ctxparams) = $DB->get_in_or_equal($this->context
->get_parent_context_ids(true), SQL_PARAMS_NAMED
, 'ctx');
38 $params = array_merge($params, $ctxparams, $this->userfieldsparams
);
39 $params['roleid'] = $this->roleid
;
41 list($sort, $sortparams) = users_order_by_sql('u', $search, $this->accesscontext
, $this->userfieldsmappings
);
42 $params = array_merge($params, $sortparams);
44 $fields = "SELECT ra.id AS raid, u.id, " . $this->userfieldsselects
. ", ra.contextid, ra.component ";
45 $countfields = "SELECT COUNT(1) ";
46 $sql = "FROM {role_assignments} ra
47 JOIN {user} u ON u.id = ra.userid
48 JOIN {context} ctx ON ra.contextid = ctx.id
51 AND ctx.id $ctxcondition
52 AND ra.roleid = :roleid";
53 $order = " ORDER BY ctx.depth DESC, ra.component, $sort";
55 if (!$this->is_validating()) {
56 $existinguserscount = $DB->count_records_sql($countfields . $sql, $params);
57 if ($existinguserscount > $this->maxusersperpage
) {
58 return $this->too_many_results($search, $existinguserscount);
62 $contextusers = $DB->get_records_sql($fields . $sql . $order, $params);
65 if (empty($contextusers)) {
69 // We have users. Out put them in groups by context depth.
70 // To help the loop below, tack a dummy user on the end of the results
71 // array, to trigger output of the last group.
72 $dummyuser = new stdClass
;
73 $dummyuser->contextid
= 0;
75 $dummyuser->component
= '';
76 $contextusers[] = $dummyuser;
77 $results = array(); // The results array we are building up.
78 $doneusers = array(); // Ensures we only list each user at most once.
79 $currentcontextid = $this->context
->id
;
80 $currentgroup = array();
81 foreach ($contextusers as $user) {
82 if (isset($doneusers[$user->id
])) {
85 $doneusers[$user->id
] = 1;
86 if ($user->contextid
!= $currentcontextid) {
87 // We have got to the end of the previous group. Add it to the results array.
88 if ($currentcontextid == $this->context
->id
) {
89 $groupname = $this->this_con_group_name($search, count($currentgroup));
91 $groupname = $this->parent_con_group_name($search, $currentcontextid);
93 $results[$groupname] = $currentgroup;
94 // Get ready for the next group.
95 $currentcontextid = $user->contextid
;
96 $currentgroup = array();
98 // Add this user to the group we are building up.
99 unset($user->contextid
);
100 if ($currentcontextid != $this->context
->id
) {
101 $user->disabled
= true;
103 if ($user->component
!== '') {
104 // Bad luck, you can tweak only manual role assignments.
105 $user->disabled
= true;
107 unset($user->component
);
108 $currentgroup[$user->id
] = $user;
114 protected function this_con_group_name($search, $numusers) {
115 if ($this->context
->contextlevel
== CONTEXT_SYSTEM
) {
116 // Special case in the System context.
118 return get_string('extusersmatching', 'core_role', $search);
120 return get_string('extusers', 'core_role');
123 $contexttype = context_helper
::get_level_name($this->context
->contextlevel
);
126 $a->search
= $search;
127 $a->contexttype
= $contexttype;
129 return get_string('usersinthisxmatching', 'core_role', $a);
131 return get_string('noneinthisxmatching', 'core_role', $a);
135 return get_string('usersinthisx', 'core_role', $contexttype);
137 return get_string('noneinthisx', 'core_role', $contexttype);
142 protected function parent_con_group_name($search, $contextid) {
143 $context = context
::instance_by_id($contextid);
144 $contextname = $context->get_context_name(true, true);
147 $a->contextname
= $contextname;
148 $a->search
= $search;
149 return get_string('usersfrommatching', 'core_role', $a);
151 return get_string('usersfrom', 'core_role', $contextname);