1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
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.
|
|
|
8 |
//
|
|
|
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.
|
|
|
13 |
//
|
|
|
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/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Existing user selector.
|
|
|
19 |
*
|
|
|
20 |
* @package core_role
|
|
|
21 |
* @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
defined('MOODLE_INTERNAL') || die();
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* User selector subclass for the list of users who already have the role in
|
|
|
29 |
* question on the assign roles page.
|
|
|
30 |
*/
|
|
|
31 |
class core_role_existing_role_holders extends core_role_assign_user_selector_base {
|
|
|
32 |
|
|
|
33 |
public function find_users($search) {
|
|
|
34 |
global $DB;
|
|
|
35 |
|
|
|
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;
|
|
|
40 |
|
|
|
41 |
list($sort, $sortparams) = users_order_by_sql('u', $search, $this->accesscontext, $this->userfieldsmappings);
|
|
|
42 |
$params = array_merge($params, $sortparams);
|
|
|
43 |
|
|
|
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
|
|
|
49 |
$this->userfieldsjoin
|
|
|
50 |
WHERE $wherecondition
|
|
|
51 |
AND ctx.id $ctxcondition
|
|
|
52 |
AND ra.roleid = :roleid";
|
|
|
53 |
$order = " ORDER BY ctx.depth DESC, ra.component, $sort";
|
|
|
54 |
|
|
|
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);
|
|
|
59 |
}
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
$contextusers = $DB->get_records_sql($fields . $sql . $order, $params);
|
|
|
63 |
|
|
|
64 |
// No users at all.
|
|
|
65 |
if (empty($contextusers)) {
|
|
|
66 |
return array();
|
|
|
67 |
}
|
|
|
68 |
|
|
|
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;
|
|
|
74 |
$dummyuser->id = 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])) {
|
|
|
83 |
continue;
|
|
|
84 |
}
|
|
|
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));
|
|
|
90 |
} else {
|
|
|
91 |
$groupname = $this->parent_con_group_name($search, $currentcontextid);
|
|
|
92 |
}
|
|
|
93 |
$results[$groupname] = $currentgroup;
|
|
|
94 |
// Get ready for the next group.
|
|
|
95 |
$currentcontextid = $user->contextid;
|
|
|
96 |
$currentgroup = array();
|
|
|
97 |
}
|
|
|
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;
|
|
|
102 |
}
|
|
|
103 |
if ($user->component !== '') {
|
|
|
104 |
// Bad luck, you can tweak only manual role assignments.
|
|
|
105 |
$user->disabled = true;
|
|
|
106 |
}
|
|
|
107 |
unset($user->component);
|
|
|
108 |
$currentgroup[$user->id] = $user;
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
return $results;
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
protected function this_con_group_name($search, $numusers) {
|
|
|
115 |
if ($this->context->contextlevel == CONTEXT_SYSTEM) {
|
|
|
116 |
// Special case in the System context.
|
|
|
117 |
if ($search) {
|
|
|
118 |
return get_string('extusersmatching', 'core_role', $search);
|
|
|
119 |
} else {
|
|
|
120 |
return get_string('extusers', 'core_role');
|
|
|
121 |
}
|
|
|
122 |
}
|
|
|
123 |
$contexttype = context_helper::get_level_name($this->context->contextlevel);
|
|
|
124 |
if ($search) {
|
|
|
125 |
$a = new stdClass;
|
|
|
126 |
$a->search = $search;
|
|
|
127 |
$a->contexttype = $contexttype;
|
|
|
128 |
if ($numusers) {
|
|
|
129 |
return get_string('usersinthisxmatching', 'core_role', $a);
|
|
|
130 |
} else {
|
|
|
131 |
return get_string('noneinthisxmatching', 'core_role', $a);
|
|
|
132 |
}
|
|
|
133 |
} else {
|
|
|
134 |
if ($numusers) {
|
|
|
135 |
return get_string('usersinthisx', 'core_role', $contexttype);
|
|
|
136 |
} else {
|
|
|
137 |
return get_string('noneinthisx', 'core_role', $contexttype);
|
|
|
138 |
}
|
|
|
139 |
}
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
protected function parent_con_group_name($search, $contextid) {
|
|
|
143 |
$context = context::instance_by_id($contextid);
|
|
|
144 |
$contextname = $context->get_context_name(true, true);
|
|
|
145 |
if ($search) {
|
|
|
146 |
$a = new stdClass;
|
|
|
147 |
$a->contextname = $contextname;
|
|
|
148 |
$a->search = $search;
|
|
|
149 |
return get_string('usersfrommatching', 'core_role', $a);
|
|
|
150 |
} else {
|
|
|
151 |
return get_string('usersfrom', 'core_role', $contextname);
|
|
|
152 |
}
|
|
|
153 |
}
|
|
|
154 |
}
|