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 |
|
1441 |
ariadna |
17 |
namespace core;
|
1 |
efrain |
18 |
|
1441 |
ariadna |
19 |
use core\context\user as context_user;
|
|
|
20 |
use core\context\course as context_course;
|
|
|
21 |
use core\context\system as context_system;
|
|
|
22 |
use core_user\fields;
|
|
|
23 |
use core\exception\invalid_parameter_exception;
|
|
|
24 |
use core\exception\moodle_exception;
|
|
|
25 |
use core\exception\coding_exception;
|
|
|
26 |
use core\output\theme_config;
|
|
|
27 |
use core\output\user_picture;
|
|
|
28 |
use core_date;
|
|
|
29 |
use dml_exception;
|
|
|
30 |
use stdClass;
|
1 |
efrain |
31 |
|
|
|
32 |
/**
|
|
|
33 |
* User class to access user details.
|
|
|
34 |
*
|
1441 |
ariadna |
35 |
* @todo MDL-82650 Move api's from user/lib.php and deprecate old ones.
|
1 |
efrain |
36 |
* @package core
|
|
|
37 |
* @copyright 2013 Rajesh Taneja <rajesh@moodle.com>
|
|
|
38 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
39 |
*/
|
1441 |
ariadna |
40 |
class user {
|
1 |
efrain |
41 |
/**
|
|
|
42 |
* No reply user id.
|
|
|
43 |
*/
|
|
|
44 |
const NOREPLY_USER = -10;
|
|
|
45 |
|
|
|
46 |
/**
|
|
|
47 |
* Support user id.
|
|
|
48 |
*/
|
|
|
49 |
const SUPPORT_USER = -20;
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* Hide email address from everyone.
|
|
|
53 |
*/
|
|
|
54 |
const MAILDISPLAY_HIDE = 0;
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
* Display email address to everyone.
|
|
|
58 |
*/
|
|
|
59 |
const MAILDISPLAY_EVERYONE = 1;
|
|
|
60 |
|
|
|
61 |
/**
|
|
|
62 |
* Display email address to course members only.
|
|
|
63 |
*/
|
|
|
64 |
const MAILDISPLAY_COURSE_MEMBERS_ONLY = 2;
|
|
|
65 |
|
|
|
66 |
/**
|
|
|
67 |
* List of fields that can be synched/locked during authentication.
|
|
|
68 |
*/
|
|
|
69 |
const AUTHSYNCFIELDS = [
|
|
|
70 |
'firstname',
|
|
|
71 |
'lastname',
|
|
|
72 |
'email',
|
|
|
73 |
'city',
|
|
|
74 |
'country',
|
|
|
75 |
'lang',
|
|
|
76 |
'description',
|
|
|
77 |
'idnumber',
|
|
|
78 |
'institution',
|
|
|
79 |
'department',
|
|
|
80 |
'phone1',
|
|
|
81 |
'phone2',
|
|
|
82 |
'address',
|
|
|
83 |
'firstnamephonetic',
|
|
|
84 |
'lastnamephonetic',
|
|
|
85 |
'middlename',
|
1441 |
ariadna |
86 |
'alternatename',
|
1 |
efrain |
87 |
];
|
|
|
88 |
|
|
|
89 |
/** @var int Indicates that user profile view should be prevented */
|
|
|
90 |
const VIEWPROFILE_PREVENT = -1;
|
|
|
91 |
/** @var int Indicates that user profile view should not be prevented */
|
|
|
92 |
const VIEWPROFILE_DO_NOT_PREVENT = 0;
|
|
|
93 |
/** @var int Indicates that user profile view should be allowed even if Moodle would prevent it */
|
|
|
94 |
const VIEWPROFILE_FORCE_ALLOW = 1;
|
|
|
95 |
|
|
|
96 |
/** @var stdClass keep record of noreply user */
|
|
|
97 |
public static $noreplyuser = false;
|
|
|
98 |
|
|
|
99 |
/** @var stdClass keep record of support user */
|
|
|
100 |
public static $supportuser = false;
|
|
|
101 |
|
|
|
102 |
/** @var array store user fields properties cache. */
|
|
|
103 |
protected static $propertiescache = null;
|
|
|
104 |
|
|
|
105 |
/** @var array store user preferences cache. */
|
|
|
106 |
protected static $preferencescache = null;
|
|
|
107 |
|
|
|
108 |
/**
|
|
|
109 |
* Return user object from db or create noreply or support user,
|
1441 |
ariadna |
110 |
* if userid matches \core\user::NOREPLY_USER or \core\user::SUPPORT_USER
|
1 |
efrain |
111 |
* respectively. If userid is not found, then return false.
|
|
|
112 |
*
|
|
|
113 |
* @param int $userid user id
|
|
|
114 |
* @param string $fields A comma separated list of user fields to be returned, support and noreply user
|
|
|
115 |
* will not be filtered by this.
|
|
|
116 |
* @param int $strictness IGNORE_MISSING means compatible mode, false returned if user not found, debug message if more found;
|
|
|
117 |
* IGNORE_MULTIPLE means return first user, ignore multiple user records found(not recommended);
|
|
|
118 |
* MUST_EXIST means throw an exception if no user record or multiple records found.
|
|
|
119 |
* @return stdClass|bool user record if found, else false.
|
|
|
120 |
* @throws dml_exception if user record not found and respective $strictness is set.
|
|
|
121 |
*/
|
|
|
122 |
public static function get_user($userid, $fields = '*', $strictness = IGNORE_MISSING) {
|
|
|
123 |
global $DB;
|
|
|
124 |
|
|
|
125 |
// If noreply user then create fake record and return.
|
|
|
126 |
switch ($userid) {
|
|
|
127 |
case self::NOREPLY_USER:
|
|
|
128 |
return self::get_noreply_user();
|
|
|
129 |
break;
|
|
|
130 |
case self::SUPPORT_USER:
|
|
|
131 |
return self::get_support_user();
|
|
|
132 |
break;
|
|
|
133 |
default:
|
1441 |
ariadna |
134 |
return $DB->get_record('user', ['id' => $userid], $fields, $strictness);
|
1 |
efrain |
135 |
}
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
/**
|
|
|
139 |
* Return user object from db based on their email.
|
|
|
140 |
*
|
|
|
141 |
* @param string $email The email of the user searched.
|
|
|
142 |
* @param string $fields A comma separated list of user fields to be returned, support and noreply user.
|
|
|
143 |
* @param int $mnethostid The id of the remote host.
|
|
|
144 |
* @param int $strictness IGNORE_MISSING means compatible mode, false returned if user not found, debug message if more found;
|
|
|
145 |
* IGNORE_MULTIPLE means return first user, ignore multiple user records found(not recommended);
|
|
|
146 |
* MUST_EXIST means throw an exception if no user record or multiple records found.
|
|
|
147 |
* @return stdClass|bool user record if found, else false.
|
|
|
148 |
* @throws dml_exception if user record not found and respective $strictness is set.
|
|
|
149 |
*/
|
|
|
150 |
public static function get_user_by_email($email, $fields = '*', $mnethostid = null, $strictness = IGNORE_MISSING) {
|
|
|
151 |
global $DB, $CFG;
|
|
|
152 |
|
|
|
153 |
// Because we use the username as the search criteria, we must also restrict our search based on mnet host.
|
|
|
154 |
if (empty($mnethostid)) {
|
|
|
155 |
// If empty, we restrict to local users.
|
|
|
156 |
$mnethostid = $CFG->mnet_localhost_id;
|
|
|
157 |
}
|
|
|
158 |
|
1441 |
ariadna |
159 |
return $DB->get_record('user', ['email' => $email, 'mnethostid' => $mnethostid], $fields, $strictness);
|
1 |
efrain |
160 |
}
|
|
|
161 |
|
|
|
162 |
/**
|
|
|
163 |
* Return user object from db based on their username.
|
|
|
164 |
*
|
|
|
165 |
* @param string $username The username of the user searched.
|
|
|
166 |
* @param string $fields A comma separated list of user fields to be returned, support and noreply user.
|
|
|
167 |
* @param int $mnethostid The id of the remote host.
|
|
|
168 |
* @param int $strictness IGNORE_MISSING means compatible mode, false returned if user not found, debug message if more found;
|
|
|
169 |
* IGNORE_MULTIPLE means return first user, ignore multiple user records found(not recommended);
|
|
|
170 |
* MUST_EXIST means throw an exception if no user record or multiple records found.
|
|
|
171 |
* @return stdClass|bool user record if found, else false.
|
|
|
172 |
* @throws dml_exception if user record not found and respective $strictness is set.
|
|
|
173 |
*/
|
|
|
174 |
public static function get_user_by_username($username, $fields = '*', $mnethostid = null, $strictness = IGNORE_MISSING) {
|
|
|
175 |
global $DB, $CFG;
|
|
|
176 |
|
|
|
177 |
// Because we use the username as the search criteria, we must also restrict our search based on mnet host.
|
|
|
178 |
if (empty($mnethostid)) {
|
|
|
179 |
// If empty, we restrict to local users.
|
|
|
180 |
$mnethostid = $CFG->mnet_localhost_id;
|
|
|
181 |
}
|
|
|
182 |
|
1441 |
ariadna |
183 |
return $DB->get_record('user', ['username' => $username, 'mnethostid' => $mnethostid], $fields, $strictness);
|
1 |
efrain |
184 |
}
|
|
|
185 |
|
|
|
186 |
/**
|
1441 |
ariadna |
187 |
* Return User object based on their idnumber.
|
|
|
188 |
*
|
|
|
189 |
* @param string $idnumber The idnumber of the user searched.
|
|
|
190 |
* @param string $fields A comma separated list of user fields to be returned, support and noreply user.
|
|
|
191 |
* @param null|int $mnethostid The id of the remote host.
|
|
|
192 |
* @param int $strictness IGNORE_MISSING means compatible mode, false returned if user not found, debug message if more found;
|
|
|
193 |
* IGNORE_MULTIPLE means return first user, ignore multiple user records found(not recommended);
|
|
|
194 |
* MUST_EXIST means throw an exception if no user record or multiple records found.
|
|
|
195 |
* @return stdClass|bool user record if found, else false.
|
|
|
196 |
*/
|
|
|
197 |
public static function get_user_by_idnumber(
|
|
|
198 |
string $idnumber,
|
|
|
199 |
string $fields = '*',
|
|
|
200 |
?int $mnethostid = null,
|
|
|
201 |
int $strictness = IGNORE_MISSING,
|
|
|
202 |
): stdClass|bool {
|
|
|
203 |
global $DB, $CFG;
|
|
|
204 |
|
|
|
205 |
// Because we use the username as the search criteria, we must also restrict our search based on mnet host.
|
|
|
206 |
if (empty($mnethostid)) {
|
|
|
207 |
// If empty, we restrict to local users.
|
|
|
208 |
$mnethostid = $CFG->mnet_localhost_id;
|
|
|
209 |
}
|
|
|
210 |
|
|
|
211 |
return $DB->get_record('user', [
|
|
|
212 |
'idnumber' => $idnumber,
|
|
|
213 |
'mnethostid' => $mnethostid,
|
|
|
214 |
], $fields, $strictness);
|
|
|
215 |
}
|
|
|
216 |
|
|
|
217 |
/**
|
1 |
efrain |
218 |
* Searches for users by name, possibly within a specified context, with current user's access.
|
|
|
219 |
*
|
|
|
220 |
* Deciding which users to search is complicated because it relies on user permissions;
|
|
|
221 |
* ideally, we shouldn't show names if you aren't allowed to see their profile. The permissions
|
|
|
222 |
* for seeing profile are really complicated.
|
|
|
223 |
*
|
|
|
224 |
* Even if search is restricted to a course, it's possible that other people might have
|
|
|
225 |
* been able to contribute within the course (e.g. they were enrolled before and not now;
|
|
|
226 |
* or people with system-level roles) so if the user has permission we do want to include
|
|
|
227 |
* everyone. However, if there are multiple results then we prioritise the ones who are
|
|
|
228 |
* enrolled in the course.
|
|
|
229 |
*
|
|
|
230 |
* If you have moodle/user:viewdetails at system level, you can search everyone.
|
|
|
231 |
* Otherwise we check which courses you *do* have that permission and search everyone who is
|
|
|
232 |
* enrolled on those courses.
|
|
|
233 |
*
|
|
|
234 |
* Normally you can only search the user's name. If you have the moodle/site:viewuseridentity
|
|
|
235 |
* capability then we also let you search the fields which are listed as identity fields in
|
|
|
236 |
* the 'showuseridentity' config option. For example, this might include the user's ID number
|
|
|
237 |
* or email.
|
|
|
238 |
*
|
|
|
239 |
* The $max parameter controls the maximum number of users returned. If users are restricted
|
|
|
240 |
* from view for some reason, multiple runs of the main query might be made; the $querylimit
|
|
|
241 |
* parameter allows this to be restricted. Both parameters can be zero to remove limits.
|
|
|
242 |
*
|
|
|
243 |
* The returned user objects include id, username, all fields required for user pictures, and
|
|
|
244 |
* user identity fields.
|
|
|
245 |
*
|
|
|
246 |
* @param string $query Search query text
|
1441 |
ariadna |
247 |
* @param context_course|null $coursecontext Course context or null if system-wide
|
1 |
efrain |
248 |
* @param int $max Max number of users to return, default 30 (zero = no limit)
|
|
|
249 |
* @param int $querylimit Max number of database queries, default 5 (zero = no limit)
|
|
|
250 |
* @return array Array of user objects with limited fields
|
|
|
251 |
*/
|
1441 |
ariadna |
252 |
public static function search(
|
|
|
253 |
$query,
|
|
|
254 |
?context_course $coursecontext = null,
|
|
|
255 |
$max = 30,
|
|
|
256 |
$querylimit = 5
|
|
|
257 |
) {
|
1 |
efrain |
258 |
global $CFG, $DB;
|
|
|
259 |
require_once($CFG->dirroot . '/user/lib.php');
|
|
|
260 |
|
|
|
261 |
// Allow limits to be turned off.
|
|
|
262 |
if (!$max) {
|
|
|
263 |
$max = PHP_INT_MAX;
|
|
|
264 |
}
|
|
|
265 |
if (!$querylimit) {
|
|
|
266 |
$querylimit = PHP_INT_MAX;
|
|
|
267 |
}
|
|
|
268 |
|
|
|
269 |
// Check permission to view profiles at each context.
|
1441 |
ariadna |
270 |
$systemcontext = context_system::instance();
|
1 |
efrain |
271 |
$viewsystem = has_capability('moodle/user:viewdetails', $systemcontext);
|
|
|
272 |
if ($viewsystem) {
|
|
|
273 |
$userquery = 'SELECT id FROM {user}';
|
|
|
274 |
$userparams = [];
|
|
|
275 |
}
|
|
|
276 |
if (!$viewsystem) {
|
1441 |
ariadna |
277 |
[$userquery, $userparams] = self::get_enrolled_sql_on_courses_with_capability(
|
|
|
278 |
'moodle/user:viewdetails'
|
|
|
279 |
);
|
1 |
efrain |
280 |
if (!$userquery) {
|
|
|
281 |
// No permissions anywhere, return nothing.
|
|
|
282 |
return [];
|
|
|
283 |
}
|
|
|
284 |
}
|
|
|
285 |
|
|
|
286 |
// Start building the WHERE clause based on name.
|
1441 |
ariadna |
287 |
[$where, $whereparams] = users_search_sql($query, 'u');
|
1 |
efrain |
288 |
|
|
|
289 |
// We allow users to search with extra identity fields (as well as name) but only if they
|
|
|
290 |
// have the permission to display those identity fields.
|
|
|
291 |
$extrasql = '';
|
|
|
292 |
$extraparams = [];
|
|
|
293 |
|
|
|
294 |
// TODO Does not support custom user profile fields (MDL-70456).
|
|
|
295 |
$userfieldsapi = \core_user\fields::for_identity(null, false)->with_userpic()->with_name()
|
|
|
296 |
->including('username', 'deleted');
|
|
|
297 |
$selectfields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
|
|
|
298 |
$extra = $userfieldsapi->get_required_fields([\core_user\fields::PURPOSE_IDENTITY]);
|
|
|
299 |
|
|
|
300 |
$index = 1;
|
|
|
301 |
foreach ($extra as $fieldname) {
|
|
|
302 |
if ($extrasql) {
|
|
|
303 |
$extrasql .= ' OR ';
|
|
|
304 |
}
|
|
|
305 |
$extrasql .= $DB->sql_like('u.' . $fieldname, ':extra' . $index, false);
|
|
|
306 |
$extraparams['extra' . $index] = $query . '%';
|
|
|
307 |
$index++;
|
|
|
308 |
}
|
|
|
309 |
|
|
|
310 |
$usingshowidentity = false;
|
|
|
311 |
// Only do this code if there actually are some identity fields being searched.
|
|
|
312 |
if ($extrasql) {
|
|
|
313 |
$identitysystem = has_capability('moodle/site:viewuseridentity', $systemcontext);
|
|
|
314 |
if ($identitysystem) {
|
|
|
315 |
// They have permission everywhere so just add the extra query to the normal query.
|
|
|
316 |
$where .= ' OR ' . $extrasql;
|
|
|
317 |
$whereparams = array_merge($whereparams, $extraparams);
|
|
|
318 |
} else {
|
|
|
319 |
// Get all courses where user can view full user identity.
|
1441 |
ariadna |
320 |
[$sql, $params] = self::get_enrolled_sql_on_courses_with_capability(
|
|
|
321 |
'moodle/site:viewuseridentity'
|
|
|
322 |
);
|
1 |
efrain |
323 |
if ($sql) {
|
|
|
324 |
// Join that with the user query to get an extra field indicating if we can.
|
|
|
325 |
$userquery = "
|
|
|
326 |
SELECT innerusers.id, COUNT(identityusers.id) AS showidentity
|
|
|
327 |
FROM ($userquery) innerusers
|
|
|
328 |
LEFT JOIN ($sql) identityusers ON identityusers.id = innerusers.id
|
|
|
329 |
GROUP BY innerusers.id";
|
|
|
330 |
$userparams = array_merge($userparams, $params);
|
|
|
331 |
$usingshowidentity = true;
|
|
|
332 |
|
|
|
333 |
// Query on the extra fields only in those places.
|
|
|
334 |
$where .= ' OR (users.showidentity > 0 AND (' . $extrasql . '))';
|
|
|
335 |
$whereparams = array_merge($whereparams, $extraparams);
|
|
|
336 |
}
|
|
|
337 |
}
|
|
|
338 |
}
|
|
|
339 |
|
|
|
340 |
// Default order is just name order. But if searching within a course then we show users
|
|
|
341 |
// within the course first.
|
1441 |
ariadna |
342 |
[$order, $orderparams] = users_order_by_sql('u', $query, $systemcontext);
|
1 |
efrain |
343 |
if ($coursecontext) {
|
1441 |
ariadna |
344 |
[$sql, $params] = get_enrolled_sql($coursecontext);
|
1 |
efrain |
345 |
$mainfield = 'innerusers2.id';
|
|
|
346 |
if ($usingshowidentity) {
|
|
|
347 |
$mainfield .= ', innerusers2.showidentity';
|
|
|
348 |
}
|
|
|
349 |
$userquery = "
|
|
|
350 |
SELECT $mainfield, COUNT(courseusers.id) AS incourse
|
|
|
351 |
FROM ($userquery) innerusers2
|
|
|
352 |
LEFT JOIN ($sql) courseusers ON courseusers.id = innerusers2.id
|
|
|
353 |
GROUP BY $mainfield";
|
|
|
354 |
$userparams = array_merge($userparams, $params);
|
|
|
355 |
|
|
|
356 |
$order = 'incourse DESC, ' . $order;
|
|
|
357 |
}
|
|
|
358 |
|
|
|
359 |
// Get result (first 30 rows only) from database. Take a couple spare in case we have to
|
|
|
360 |
// drop some.
|
|
|
361 |
$result = [];
|
|
|
362 |
$got = 0;
|
|
|
363 |
$pos = 0;
|
|
|
364 |
$readcount = $max + 2;
|
|
|
365 |
for ($i = 0; $i < $querylimit; $i++) {
|
1441 |
ariadna |
366 |
$rawresult = $DB->get_records_sql(
|
|
|
367 |
"
|
1 |
efrain |
368 |
SELECT $selectfields
|
|
|
369 |
FROM ($userquery) users
|
|
|
370 |
JOIN {user} u ON u.id = users.id
|
|
|
371 |
WHERE $where
|
1441 |
ariadna |
372 |
ORDER BY $order",
|
|
|
373 |
array_merge($userparams, $whereparams, $orderparams),
|
|
|
374 |
$pos,
|
|
|
375 |
$readcount
|
|
|
376 |
);
|
1 |
efrain |
377 |
foreach ($rawresult as $user) {
|
|
|
378 |
// Skip guest.
|
|
|
379 |
if ($user->username === 'guest') {
|
|
|
380 |
continue;
|
|
|
381 |
}
|
|
|
382 |
// Check user can really view profile (there are per-user cases where this could
|
|
|
383 |
// be different for some reason, this is the same check used by the profile view pages
|
|
|
384 |
// to double-check that it is OK).
|
|
|
385 |
if (!user_can_view_profile($user)) {
|
|
|
386 |
continue;
|
|
|
387 |
}
|
|
|
388 |
$result[] = $user;
|
|
|
389 |
$got++;
|
|
|
390 |
if ($got >= $max) {
|
|
|
391 |
break;
|
|
|
392 |
}
|
|
|
393 |
}
|
|
|
394 |
|
|
|
395 |
if ($got >= $max) {
|
|
|
396 |
// All necessary results obtained.
|
|
|
397 |
break;
|
|
|
398 |
}
|
|
|
399 |
if (count($rawresult) < $readcount) {
|
|
|
400 |
// No more results from database.
|
|
|
401 |
break;
|
|
|
402 |
}
|
|
|
403 |
$pos += $readcount;
|
|
|
404 |
}
|
|
|
405 |
|
|
|
406 |
return $result;
|
|
|
407 |
}
|
|
|
408 |
|
|
|
409 |
/**
|
|
|
410 |
* Gets an SQL query that lists all enrolled user ids on any course where the current
|
|
|
411 |
* user has the specified capability. Helper function used for searching users.
|
|
|
412 |
*
|
|
|
413 |
* @param string $capability Required capability
|
|
|
414 |
* @return array Array containing SQL and params, or two nulls if there are no courses
|
|
|
415 |
*/
|
|
|
416 |
protected static function get_enrolled_sql_on_courses_with_capability($capability) {
|
|
|
417 |
// Get all courses where user have the capability.
|
1441 |
ariadna |
418 |
$courses = get_user_capability_course(
|
|
|
419 |
$capability,
|
|
|
420 |
null,
|
|
|
421 |
true,
|
|
|
422 |
implode(',', array_values(context_helper::get_preload_record_columns('ctx')))
|
|
|
423 |
);
|
1 |
efrain |
424 |
if (!$courses) {
|
|
|
425 |
return [null, null];
|
|
|
426 |
}
|
|
|
427 |
|
|
|
428 |
// Loop around all courses getting the SQL for enrolled users. Note: This query could
|
|
|
429 |
// probably be more efficient (without the union) if get_enrolled_sql had a way to
|
|
|
430 |
// pass an array of courseids, but it doesn't.
|
|
|
431 |
$unionsql = '';
|
|
|
432 |
$unionparams = [];
|
|
|
433 |
foreach ($courses as $course) {
|
|
|
434 |
// Get SQL to list user ids enrolled in this course.
|
1441 |
ariadna |
435 |
context_helper::preload_from_record($course);
|
|
|
436 |
[$sql, $params] = get_enrolled_sql(context_course::instance($course->id));
|
1 |
efrain |
437 |
|
|
|
438 |
// Combine to a big union query.
|
|
|
439 |
if ($unionsql) {
|
|
|
440 |
$unionsql .= ' UNION ';
|
|
|
441 |
}
|
|
|
442 |
$unionsql .= $sql;
|
|
|
443 |
$unionparams = array_merge($unionparams, $params);
|
|
|
444 |
}
|
|
|
445 |
|
|
|
446 |
return [$unionsql, $unionparams];
|
|
|
447 |
}
|
|
|
448 |
|
|
|
449 |
/**
|
|
|
450 |
* Helper function to return dummy noreply user record.
|
|
|
451 |
*
|
|
|
452 |
* @return stdClass
|
|
|
453 |
*/
|
|
|
454 |
protected static function get_dummy_user_record() {
|
|
|
455 |
global $CFG;
|
|
|
456 |
|
|
|
457 |
$dummyuser = new stdClass();
|
|
|
458 |
$dummyuser->id = self::NOREPLY_USER;
|
|
|
459 |
$dummyuser->email = $CFG->noreplyaddress;
|
|
|
460 |
$dummyuser->firstname = get_string('noreplyname');
|
|
|
461 |
$dummyuser->username = 'noreply';
|
|
|
462 |
$dummyuser->lastname = '';
|
|
|
463 |
$dummyuser->confirmed = 1;
|
|
|
464 |
$dummyuser->suspended = 0;
|
|
|
465 |
$dummyuser->deleted = 0;
|
|
|
466 |
$dummyuser->picture = 0;
|
|
|
467 |
$dummyuser->auth = 'manual';
|
|
|
468 |
$dummyuser->firstnamephonetic = '';
|
|
|
469 |
$dummyuser->lastnamephonetic = '';
|
|
|
470 |
$dummyuser->middlename = '';
|
|
|
471 |
$dummyuser->alternatename = '';
|
|
|
472 |
$dummyuser->imagealt = '';
|
|
|
473 |
return $dummyuser;
|
|
|
474 |
}
|
|
|
475 |
|
|
|
476 |
/**
|
|
|
477 |
* Return noreply user record, this is currently used in messaging
|
|
|
478 |
* system only for sending messages from noreply email.
|
|
|
479 |
* It will return record of $CFG->noreplyuserid if set else return dummy
|
|
|
480 |
* user object with hard-coded $user->emailstop = 1 so noreply can be sent to user.
|
|
|
481 |
*
|
|
|
482 |
* @return stdClass user record.
|
|
|
483 |
*/
|
|
|
484 |
public static function get_noreply_user() {
|
|
|
485 |
global $CFG;
|
|
|
486 |
|
|
|
487 |
if (!empty(self::$noreplyuser)) {
|
|
|
488 |
return self::$noreplyuser;
|
|
|
489 |
}
|
|
|
490 |
|
|
|
491 |
// If noreply user is set then use it, else create one.
|
|
|
492 |
if (!empty($CFG->noreplyuserid)) {
|
|
|
493 |
self::$noreplyuser = self::get_user($CFG->noreplyuserid);
|
|
|
494 |
self::$noreplyuser->emailstop = 1; // Force msg stop for this user.
|
|
|
495 |
return self::$noreplyuser;
|
|
|
496 |
} else {
|
|
|
497 |
// Do not cache the dummy user record to avoid language internationalization issues.
|
|
|
498 |
$noreplyuser = self::get_dummy_user_record();
|
|
|
499 |
$noreplyuser->maildisplay = '1'; // Show to all.
|
|
|
500 |
$noreplyuser->emailstop = 1;
|
|
|
501 |
return $noreplyuser;
|
|
|
502 |
}
|
|
|
503 |
}
|
|
|
504 |
|
|
|
505 |
/**
|
|
|
506 |
* Return support user record, this is currently used in messaging
|
|
|
507 |
* system only for sending messages to support email.
|
|
|
508 |
* $CFG->supportuserid is set then returns user record
|
|
|
509 |
* $CFG->supportemail is set then return dummy record with $CFG->supportemail
|
|
|
510 |
* else return admin user record with hard-coded $user->emailstop = 0, so user
|
|
|
511 |
* gets support message.
|
|
|
512 |
*
|
|
|
513 |
* @return stdClass user record.
|
|
|
514 |
*/
|
|
|
515 |
public static function get_support_user() {
|
|
|
516 |
global $CFG;
|
|
|
517 |
|
|
|
518 |
if (!empty(self::$supportuser)) {
|
|
|
519 |
return self::$supportuser;
|
|
|
520 |
}
|
|
|
521 |
|
|
|
522 |
// If custom support user is set then use it, else if supportemail is set then use it, else use noreply.
|
|
|
523 |
if (!empty($CFG->supportuserid)) {
|
|
|
524 |
self::$supportuser = self::get_user($CFG->supportuserid, '*', MUST_EXIST);
|
|
|
525 |
} else if (empty(self::$supportuser) && !empty($CFG->supportemail)) {
|
|
|
526 |
// Try sending it to support email if support user is not set.
|
|
|
527 |
$supportuser = self::get_dummy_user_record();
|
|
|
528 |
$supportuser->id = self::SUPPORT_USER;
|
|
|
529 |
$supportuser->email = $CFG->supportemail;
|
|
|
530 |
if ($CFG->supportname) {
|
|
|
531 |
$supportuser->firstname = $CFG->supportname;
|
|
|
532 |
}
|
|
|
533 |
$supportuser->username = 'support';
|
|
|
534 |
$supportuser->maildisplay = '1'; // Show to all.
|
|
|
535 |
// Unset emailstop to make sure support message is sent.
|
|
|
536 |
$supportuser->emailstop = 0;
|
|
|
537 |
return $supportuser;
|
|
|
538 |
}
|
|
|
539 |
|
|
|
540 |
// Send support msg to admin user if nothing is set above.
|
|
|
541 |
if (empty(self::$supportuser)) {
|
|
|
542 |
self::$supportuser = get_admin();
|
|
|
543 |
}
|
|
|
544 |
|
|
|
545 |
// Unset emailstop to make sure support message is sent.
|
|
|
546 |
self::$supportuser->emailstop = 0;
|
|
|
547 |
return self::$supportuser;
|
|
|
548 |
}
|
|
|
549 |
|
|
|
550 |
/**
|
|
|
551 |
* Reset self::$noreplyuser and self::$supportuser.
|
|
|
552 |
* This is only used by phpunit, and there is no other use case for this function.
|
|
|
553 |
* Please don't use it outside phpunit.
|
|
|
554 |
*/
|
|
|
555 |
public static function reset_internal_users() {
|
|
|
556 |
if (PHPUNIT_TEST) {
|
|
|
557 |
self::$noreplyuser = false;
|
|
|
558 |
self::$supportuser = false;
|
|
|
559 |
} else {
|
|
|
560 |
debugging('reset_internal_users() should not be used outside phpunit.', DEBUG_DEVELOPER);
|
|
|
561 |
}
|
|
|
562 |
}
|
|
|
563 |
|
|
|
564 |
/**
|
|
|
565 |
* Return true if user id is greater than 0 and alternatively check db.
|
|
|
566 |
*
|
|
|
567 |
* @param int $userid user id.
|
|
|
568 |
* @param bool $checkdb if true userid will be checked in db. By default it's false, and
|
|
|
569 |
* userid is compared with 0 for performance.
|
|
|
570 |
* @return bool true is real user else false.
|
|
|
571 |
*/
|
|
|
572 |
public static function is_real_user($userid, $checkdb = false) {
|
|
|
573 |
global $DB;
|
|
|
574 |
|
|
|
575 |
if ($userid <= 0) {
|
|
|
576 |
return false;
|
|
|
577 |
}
|
|
|
578 |
if ($checkdb) {
|
1441 |
ariadna |
579 |
return $DB->record_exists('user', ['id' => $userid]);
|
1 |
efrain |
580 |
} else {
|
|
|
581 |
return true;
|
|
|
582 |
}
|
|
|
583 |
}
|
|
|
584 |
|
|
|
585 |
/**
|
|
|
586 |
* Determine whether the given user ID is that of the current user. Useful for components implementing permission callbacks
|
|
|
587 |
* for preferences consumed by {@see fill_preferences_cache}
|
|
|
588 |
*
|
|
|
589 |
* @param stdClass $user
|
|
|
590 |
* @return bool
|
|
|
591 |
*/
|
|
|
592 |
public static function is_current_user(stdClass $user): bool {
|
|
|
593 |
global $USER;
|
|
|
594 |
return $user->id == $USER->id;
|
|
|
595 |
}
|
|
|
596 |
|
|
|
597 |
/**
|
|
|
598 |
* Check if the given user is an active user in the site.
|
|
|
599 |
*
|
|
|
600 |
* @param stdClass $user user object
|
|
|
601 |
* @param boolean $checksuspended whether to check if the user has the account suspended
|
|
|
602 |
* @param boolean $checknologin whether to check if the user uses the nologin auth method
|
|
|
603 |
* @throws moodle_exception
|
|
|
604 |
* @since Moodle 3.0
|
|
|
605 |
*/
|
|
|
606 |
public static function require_active_user($user, $checksuspended = false, $checknologin = false) {
|
|
|
607 |
|
|
|
608 |
if (!self::is_real_user($user->id)) {
|
|
|
609 |
throw new moodle_exception('invaliduser', 'error');
|
|
|
610 |
}
|
|
|
611 |
|
|
|
612 |
if ($user->deleted) {
|
|
|
613 |
throw new moodle_exception('userdeleted');
|
|
|
614 |
}
|
|
|
615 |
|
|
|
616 |
if (empty($user->confirmed)) {
|
|
|
617 |
throw new moodle_exception('usernotconfirmed', 'moodle', '', $user->username);
|
|
|
618 |
}
|
|
|
619 |
|
|
|
620 |
if (isguestuser($user)) {
|
|
|
621 |
throw new moodle_exception('guestsarenotallowed', 'error');
|
|
|
622 |
}
|
|
|
623 |
|
1441 |
ariadna |
624 |
if ($checksuspended && $user->suspended) {
|
1 |
efrain |
625 |
throw new moodle_exception('suspended', 'auth');
|
|
|
626 |
}
|
|
|
627 |
|
1441 |
ariadna |
628 |
if ($checknologin && $user->auth == 'nologin') {
|
1 |
efrain |
629 |
throw new moodle_exception('suspended', 'auth');
|
|
|
630 |
}
|
|
|
631 |
}
|
|
|
632 |
|
|
|
633 |
/**
|
|
|
634 |
* Updates the provided users profile picture based upon the expected fields returned from the edit or edit_advanced forms.
|
|
|
635 |
*
|
|
|
636 |
* @param stdClass $usernew An object that contains some information about the user being updated
|
|
|
637 |
* @param array $filemanageroptions
|
|
|
638 |
* @return bool True if the user was updated, false if it stayed the same.
|
|
|
639 |
*/
|
1441 |
ariadna |
640 |
public static function update_picture(stdClass $usernew, $filemanageroptions = []) {
|
1 |
efrain |
641 |
global $CFG, $DB;
|
|
|
642 |
require_once("$CFG->libdir/gdlib.php");
|
|
|
643 |
|
|
|
644 |
$context = context_user::instance($usernew->id, MUST_EXIST);
|
1441 |
ariadna |
645 |
$user = self::get_user($usernew->id, 'id, picture', MUST_EXIST);
|
1 |
efrain |
646 |
|
|
|
647 |
$newpicture = $user->picture;
|
|
|
648 |
// Get file_storage to process files.
|
|
|
649 |
$fs = get_file_storage();
|
|
|
650 |
if (!empty($usernew->deletepicture)) {
|
|
|
651 |
// The user has chosen to delete the selected users picture.
|
|
|
652 |
$fs->delete_area_files($context->id, 'user', 'icon'); // Drop all images in area.
|
|
|
653 |
$newpicture = 0;
|
|
|
654 |
}
|
|
|
655 |
|
|
|
656 |
// Save newly uploaded file, this will avoid context mismatch for newly created users.
|
|
|
657 |
if (!isset($usernew->imagefile)) {
|
|
|
658 |
$usernew->imagefile = 0;
|
|
|
659 |
}
|
|
|
660 |
file_save_draft_area_files($usernew->imagefile, $context->id, 'user', 'newicon', 0, $filemanageroptions);
|
|
|
661 |
if (($iconfiles = $fs->get_area_files($context->id, 'user', 'newicon')) && count($iconfiles) == 2) {
|
|
|
662 |
// Get file which was uploaded in draft area.
|
|
|
663 |
foreach ($iconfiles as $file) {
|
|
|
664 |
if (!$file->is_directory()) {
|
|
|
665 |
break;
|
|
|
666 |
}
|
|
|
667 |
}
|
|
|
668 |
// Copy file to temporary location and the send it for processing icon.
|
|
|
669 |
if ($iconfile = $file->copy_content_to_temp()) {
|
|
|
670 |
// There is a new image that has been uploaded.
|
|
|
671 |
// Process the new image and set the user to make use of it.
|
|
|
672 |
// NOTE: Uploaded images always take over Gravatar.
|
|
|
673 |
$newpicture = (int)process_new_icon($context, 'user', 'icon', 0, $iconfile);
|
|
|
674 |
// Delete temporary file.
|
|
|
675 |
@unlink($iconfile);
|
|
|
676 |
// Remove uploaded file.
|
|
|
677 |
$fs->delete_area_files($context->id, 'user', 'newicon');
|
|
|
678 |
} else {
|
|
|
679 |
// Something went wrong while creating temp file.
|
|
|
680 |
// Remove uploaded file.
|
|
|
681 |
$fs->delete_area_files($context->id, 'user', 'newicon');
|
|
|
682 |
return false;
|
|
|
683 |
}
|
|
|
684 |
}
|
|
|
685 |
|
|
|
686 |
if ($newpicture != $user->picture) {
|
1441 |
ariadna |
687 |
$DB->set_field('user', 'picture', $newpicture, ['id' => $user->id]);
|
1 |
efrain |
688 |
return true;
|
|
|
689 |
} else {
|
|
|
690 |
return false;
|
|
|
691 |
}
|
|
|
692 |
}
|
|
|
693 |
|
|
|
694 |
|
|
|
695 |
|
|
|
696 |
/**
|
|
|
697 |
* Definition of user profile fields and the expected parameter type for data validation.
|
|
|
698 |
*
|
|
|
699 |
* array(
|
|
|
700 |
* 'property_name' => array( // The user property to be checked. Should match the field on the user table.
|
|
|
701 |
* 'null' => NULL_ALLOWED, // Defaults to NULL_NOT_ALLOWED. Takes NULL_NOT_ALLOWED or NULL_ALLOWED.
|
|
|
702 |
* 'type' => PARAM_TYPE, // Expected parameter type of the user field.
|
|
|
703 |
* 'choices' => array(1, 2..) // An array of accepted values of the user field.
|
|
|
704 |
* 'default' => $CFG->setting // An default value for the field.
|
|
|
705 |
* )
|
|
|
706 |
* )
|
|
|
707 |
*
|
|
|
708 |
* The fields choices and default are optional.
|
|
|
709 |
*
|
|
|
710 |
* @return void
|
|
|
711 |
*/
|
|
|
712 |
protected static function fill_properties_cache() {
|
|
|
713 |
global $CFG, $SESSION;
|
|
|
714 |
if (self::$propertiescache !== null) {
|
|
|
715 |
return;
|
|
|
716 |
}
|
|
|
717 |
|
|
|
718 |
// Array of user fields properties and expected parameters.
|
|
|
719 |
// Every new field on the user table should be added here otherwise it won't be validated.
|
1441 |
ariadna |
720 |
$fields = [];
|
|
|
721 |
$fields['id'] = ['type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED];
|
|
|
722 |
$fields['auth'] = ['type' => PARAM_AUTH, 'null' => NULL_NOT_ALLOWED];
|
|
|
723 |
$fields['confirmed'] = ['type' => PARAM_BOOL, 'null' => NULL_NOT_ALLOWED];
|
|
|
724 |
$fields['policyagreed'] = ['type' => PARAM_BOOL, 'null' => NULL_NOT_ALLOWED];
|
|
|
725 |
$fields['deleted'] = ['type' => PARAM_BOOL, 'null' => NULL_NOT_ALLOWED];
|
|
|
726 |
$fields['suspended'] = ['type' => PARAM_BOOL, 'null' => NULL_NOT_ALLOWED];
|
|
|
727 |
$fields['mnethostid'] = ['type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED];
|
|
|
728 |
$fields['username'] = ['type' => PARAM_USERNAME, 'null' => NULL_NOT_ALLOWED];
|
|
|
729 |
$fields['password'] = ['type' => PARAM_RAW, 'null' => NULL_NOT_ALLOWED];
|
|
|
730 |
$fields['idnumber'] = ['type' => PARAM_RAW, 'null' => NULL_NOT_ALLOWED];
|
|
|
731 |
$fields['firstname'] = ['type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED];
|
|
|
732 |
$fields['lastname'] = ['type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED];
|
|
|
733 |
$fields['surname'] = ['type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED];
|
|
|
734 |
$fields['email'] = ['type' => PARAM_RAW_TRIMMED, 'null' => NULL_NOT_ALLOWED];
|
|
|
735 |
$fields['emailstop'] = ['type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED, 'default' => 0];
|
|
|
736 |
$fields['phone1'] = ['type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED];
|
|
|
737 |
$fields['phone2'] = ['type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED];
|
|
|
738 |
$fields['institution'] = ['type' => PARAM_TEXT, 'null' => NULL_NOT_ALLOWED];
|
|
|
739 |
$fields['department'] = ['type' => PARAM_TEXT, 'null' => NULL_NOT_ALLOWED];
|
|
|
740 |
$fields['address'] = ['type' => PARAM_TEXT, 'null' => NULL_NOT_ALLOWED];
|
|
|
741 |
$fields['city'] = ['type' => PARAM_TEXT, 'null' => NULL_NOT_ALLOWED, 'default' => $CFG->defaultcity];
|
|
|
742 |
$fields['country'] = [
|
|
|
743 |
'type' => PARAM_ALPHA,
|
|
|
744 |
'null' => NULL_NOT_ALLOWED,
|
|
|
745 |
'default' => $CFG->country,
|
|
|
746 |
'choices' => array_merge(
|
|
|
747 |
['' => ''],
|
|
|
748 |
get_string_manager()->get_list_of_countries(true, true)
|
|
|
749 |
),
|
|
|
750 |
];
|
|
|
751 |
$fields['lang'] = [
|
|
|
752 |
'type' => PARAM_LANG,
|
|
|
753 |
'null' => NULL_NOT_ALLOWED,
|
|
|
754 |
'default' => (!empty($CFG->autolangusercreation) && !empty($SESSION->lang)) ? $SESSION->lang : $CFG->lang,
|
|
|
755 |
'choices' => array_merge(
|
|
|
756 |
['' => ''],
|
|
|
757 |
get_string_manager()->get_list_of_translations(false)
|
|
|
758 |
),
|
|
|
759 |
];
|
|
|
760 |
$fields['calendartype'] = [
|
|
|
761 |
'type' => PARAM_PLUGIN,
|
|
|
762 |
'null' => NULL_NOT_ALLOWED,
|
|
|
763 |
'default' => $CFG->calendartype,
|
|
|
764 |
'choices' => array_merge(
|
|
|
765 |
['' => ''],
|
|
|
766 |
\core_calendar\type_factory::get_list_of_calendar_types()
|
|
|
767 |
),
|
|
|
768 |
];
|
|
|
769 |
$fields['theme'] = [
|
|
|
770 |
'type' => PARAM_THEME,
|
|
|
771 |
'null' => NULL_NOT_ALLOWED,
|
|
|
772 |
'default' => theme_config::DEFAULT_THEME,
|
|
|
773 |
'choices' => array_merge(
|
|
|
774 |
['' => ''],
|
|
|
775 |
get_list_of_themes()
|
|
|
776 |
),
|
|
|
777 |
];
|
|
|
778 |
$fields['timezone'] = [
|
|
|
779 |
// Must not use choices here: timezones can come and go.
|
|
|
780 |
'type' => PARAM_TIMEZONE,
|
|
|
781 |
'null' => NULL_NOT_ALLOWED,
|
|
|
782 |
'default' => core_date::get_server_timezone(),
|
|
|
783 |
];
|
|
|
784 |
$fields['firstaccess'] = ['type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED];
|
|
|
785 |
$fields['lastaccess'] = ['type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED];
|
|
|
786 |
$fields['lastlogin'] = ['type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED];
|
|
|
787 |
$fields['currentlogin'] = ['type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED];
|
|
|
788 |
$fields['lastip'] = ['type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED];
|
|
|
789 |
$fields['secret'] = ['type' => PARAM_ALPHANUM, 'null' => NULL_NOT_ALLOWED];
|
|
|
790 |
$fields['picture'] = ['type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED];
|
|
|
791 |
$fields['description'] = ['type' => PARAM_RAW, 'null' => NULL_ALLOWED];
|
|
|
792 |
$fields['descriptionformat'] = ['type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED];
|
|
|
793 |
$fields['mailformat'] = [
|
|
|
794 |
'type' => PARAM_INT,
|
|
|
795 |
'null' => NULL_NOT_ALLOWED,
|
|
|
796 |
'default' => $CFG->defaultpreference_mailformat,
|
|
|
797 |
];
|
|
|
798 |
$fields['maildigest'] = [
|
|
|
799 |
'type' => PARAM_INT,
|
|
|
800 |
'null' => NULL_NOT_ALLOWED,
|
|
|
801 |
'default' => $CFG->defaultpreference_maildigest,
|
|
|
802 |
];
|
|
|
803 |
$fields['maildisplay'] = [
|
|
|
804 |
'type' => PARAM_INT,
|
|
|
805 |
'null' => NULL_NOT_ALLOWED,
|
|
|
806 |
'default' => $CFG->defaultpreference_maildisplay,
|
|
|
807 |
];
|
|
|
808 |
$fields['autosubscribe'] = [
|
|
|
809 |
'type' => PARAM_INT,
|
|
|
810 |
'null' => NULL_NOT_ALLOWED,
|
|
|
811 |
'default' => $CFG->defaultpreference_autosubscribe,
|
|
|
812 |
];
|
|
|
813 |
$fields['trackforums'] = [
|
|
|
814 |
'type' => PARAM_INT,
|
|
|
815 |
'null' => NULL_NOT_ALLOWED,
|
|
|
816 |
'default' => $CFG->defaultpreference_trackforums,
|
|
|
817 |
];
|
|
|
818 |
$fields['timecreated'] = ['type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED];
|
|
|
819 |
$fields['timemodified'] = ['type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED];
|
|
|
820 |
$fields['trustbitmask'] = ['type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED];
|
|
|
821 |
$fields['imagealt'] = ['type' => PARAM_TEXT, 'null' => NULL_ALLOWED];
|
|
|
822 |
$fields['lastnamephonetic'] = ['type' => PARAM_NOTAGS, 'null' => NULL_ALLOWED];
|
|
|
823 |
$fields['firstnamephonetic'] = ['type' => PARAM_NOTAGS, 'null' => NULL_ALLOWED];
|
|
|
824 |
$fields['middlename'] = ['type' => PARAM_NOTAGS, 'null' => NULL_ALLOWED];
|
|
|
825 |
$fields['alternatename'] = ['type' => PARAM_NOTAGS, 'null' => NULL_ALLOWED];
|
1 |
efrain |
826 |
|
|
|
827 |
self::$propertiescache = $fields;
|
|
|
828 |
}
|
|
|
829 |
|
|
|
830 |
/**
|
|
|
831 |
* Get properties of a user field.
|
|
|
832 |
*
|
|
|
833 |
* @param string $property property name to be retrieved.
|
|
|
834 |
* @throws coding_exception if the requested property name is invalid.
|
|
|
835 |
* @return array the property definition.
|
|
|
836 |
*/
|
|
|
837 |
public static function get_property_definition($property) {
|
|
|
838 |
|
|
|
839 |
self::fill_properties_cache();
|
|
|
840 |
|
|
|
841 |
if (!array_key_exists($property, self::$propertiescache)) {
|
|
|
842 |
throw new coding_exception('Invalid property requested.');
|
|
|
843 |
}
|
|
|
844 |
|
|
|
845 |
return self::$propertiescache[$property];
|
|
|
846 |
}
|
|
|
847 |
|
|
|
848 |
/**
|
|
|
849 |
* Validate user data.
|
|
|
850 |
*
|
|
|
851 |
* This method just validates each user field and return an array of errors. It doesn't clean the data,
|
|
|
852 |
* the methods clean() and clean_field() should be used for this purpose.
|
|
|
853 |
*
|
|
|
854 |
* @param stdClass|array $data user data object or array to be validated.
|
|
|
855 |
* @return array|true $errors array of errors found on the user object, true if the validation passed.
|
|
|
856 |
*/
|
|
|
857 |
public static function validate($data) {
|
|
|
858 |
// Get all user profile fields definition.
|
|
|
859 |
self::fill_properties_cache();
|
|
|
860 |
|
|
|
861 |
foreach ($data as $property => $value) {
|
|
|
862 |
try {
|
|
|
863 |
if (isset(self::$propertiescache[$property])) {
|
|
|
864 |
validate_param($value, self::$propertiescache[$property]['type'], self::$propertiescache[$property]['null']);
|
|
|
865 |
}
|
|
|
866 |
// Check that the value is part of a list of allowed values.
|
1441 |
ariadna |
867 |
if (
|
|
|
868 |
!empty(self::$propertiescache[$property]['choices']) &&
|
|
|
869 |
!isset(self::$propertiescache[$property]['choices'][$value])
|
|
|
870 |
) {
|
1 |
efrain |
871 |
throw new invalid_parameter_exception($value);
|
|
|
872 |
}
|
|
|
873 |
} catch (invalid_parameter_exception $e) {
|
|
|
874 |
$errors[$property] = $e->getMessage();
|
|
|
875 |
}
|
|
|
876 |
}
|
|
|
877 |
|
|
|
878 |
return empty($errors) ? true : $errors;
|
|
|
879 |
}
|
|
|
880 |
|
|
|
881 |
/**
|
|
|
882 |
* Clean the properties cache.
|
|
|
883 |
*
|
|
|
884 |
* During unit tests we need to be able to reset all caches so that each new test starts in a known state.
|
|
|
885 |
* Intended for use only for testing, phpunit calls this before every test.
|
|
|
886 |
*/
|
|
|
887 |
public static function reset_caches() {
|
|
|
888 |
self::$propertiescache = null;
|
1441 |
ariadna |
889 |
self::$preferencescache = null;
|
1 |
efrain |
890 |
}
|
|
|
891 |
|
|
|
892 |
/**
|
|
|
893 |
* Clean the user data.
|
|
|
894 |
*
|
|
|
895 |
* @param stdClass|array $user the user data to be validated against properties definition.
|
|
|
896 |
* @return stdClass $user the cleaned user data.
|
|
|
897 |
*/
|
|
|
898 |
public static function clean_data($user) {
|
|
|
899 |
if (empty($user)) {
|
|
|
900 |
return $user;
|
|
|
901 |
}
|
|
|
902 |
|
|
|
903 |
foreach ($user as $field => $value) {
|
|
|
904 |
// Get the property parameter type and do the cleaning.
|
|
|
905 |
try {
|
1441 |
ariadna |
906 |
$user->$field = self::clean_field($value, $field);
|
1 |
efrain |
907 |
} catch (coding_exception $e) {
|
|
|
908 |
debugging("The property '$field' could not be cleaned.", DEBUG_DEVELOPER);
|
|
|
909 |
}
|
|
|
910 |
}
|
|
|
911 |
|
|
|
912 |
return $user;
|
|
|
913 |
}
|
|
|
914 |
|
|
|
915 |
/**
|
|
|
916 |
* Clean a specific user field.
|
|
|
917 |
*
|
|
|
918 |
* @param string $data the user field data to be cleaned.
|
|
|
919 |
* @param string $field the user field name on the property definition cache.
|
|
|
920 |
* @return string the cleaned user data.
|
|
|
921 |
*/
|
|
|
922 |
public static function clean_field($data, $field) {
|
|
|
923 |
if (empty($data) || empty($field)) {
|
|
|
924 |
return $data;
|
|
|
925 |
}
|
|
|
926 |
|
|
|
927 |
try {
|
1441 |
ariadna |
928 |
$type = self::get_property_type($field);
|
1 |
efrain |
929 |
|
|
|
930 |
if (isset(self::$propertiescache[$field]['choices'])) {
|
|
|
931 |
if (!array_key_exists($data, self::$propertiescache[$field]['choices'])) {
|
|
|
932 |
if (isset(self::$propertiescache[$field]['default'])) {
|
|
|
933 |
$data = self::$propertiescache[$field]['default'];
|
|
|
934 |
} else {
|
|
|
935 |
$data = '';
|
|
|
936 |
}
|
|
|
937 |
} else {
|
|
|
938 |
return $data;
|
|
|
939 |
}
|
|
|
940 |
} else {
|
|
|
941 |
$data = clean_param($data, $type);
|
|
|
942 |
}
|
|
|
943 |
} catch (coding_exception $e) {
|
|
|
944 |
debugging("The property '$field' could not be cleaned.", DEBUG_DEVELOPER);
|
|
|
945 |
}
|
|
|
946 |
|
|
|
947 |
return $data;
|
|
|
948 |
}
|
|
|
949 |
|
|
|
950 |
/**
|
|
|
951 |
* Get the parameter type of the property.
|
|
|
952 |
*
|
|
|
953 |
* @param string $property property name to be retrieved.
|
|
|
954 |
* @throws coding_exception if the requested property name is invalid.
|
|
|
955 |
* @return int the property parameter type.
|
|
|
956 |
*/
|
|
|
957 |
public static function get_property_type($property) {
|
|
|
958 |
|
|
|
959 |
self::fill_properties_cache();
|
|
|
960 |
|
|
|
961 |
if (!array_key_exists($property, self::$propertiescache)) {
|
|
|
962 |
throw new coding_exception('Invalid property requested: ' . $property);
|
|
|
963 |
}
|
|
|
964 |
|
|
|
965 |
return self::$propertiescache[$property]['type'];
|
|
|
966 |
}
|
|
|
967 |
|
|
|
968 |
/**
|
|
|
969 |
* Discover if the property is NULL_ALLOWED or NULL_NOT_ALLOWED.
|
|
|
970 |
*
|
|
|
971 |
* @param string $property property name to be retrieved.
|
|
|
972 |
* @throws coding_exception if the requested property name is invalid.
|
|
|
973 |
* @return bool true if the property is NULL_ALLOWED, false otherwise.
|
|
|
974 |
*/
|
|
|
975 |
public static function get_property_null($property) {
|
|
|
976 |
|
|
|
977 |
self::fill_properties_cache();
|
|
|
978 |
|
|
|
979 |
if (!array_key_exists($property, self::$propertiescache)) {
|
|
|
980 |
throw new coding_exception('Invalid property requested: ' . $property);
|
|
|
981 |
}
|
|
|
982 |
|
|
|
983 |
return self::$propertiescache[$property]['null'];
|
|
|
984 |
}
|
|
|
985 |
|
|
|
986 |
/**
|
|
|
987 |
* Get the choices of the property.
|
|
|
988 |
*
|
|
|
989 |
* This is a helper method to validate a value against a list of acceptable choices.
|
|
|
990 |
* For instance: country, language, themes and etc.
|
|
|
991 |
*
|
|
|
992 |
* @param string $property property name to be retrieved.
|
|
|
993 |
* @throws coding_exception if the requested property name is invalid or if it does not has a list of choices.
|
|
|
994 |
* @return array the property parameter type.
|
|
|
995 |
*/
|
|
|
996 |
public static function get_property_choices($property) {
|
|
|
997 |
|
|
|
998 |
self::fill_properties_cache();
|
|
|
999 |
|
1441 |
ariadna |
1000 |
if (
|
|
|
1001 |
!array_key_exists($property, self::$propertiescache) &&
|
|
|
1002 |
!array_key_exists('choices', self::$propertiescache[$property])
|
|
|
1003 |
) {
|
1 |
efrain |
1004 |
throw new coding_exception('Invalid property requested, or the property does not has a list of choices.');
|
|
|
1005 |
}
|
|
|
1006 |
|
|
|
1007 |
return self::$propertiescache[$property]['choices'];
|
|
|
1008 |
}
|
|
|
1009 |
|
|
|
1010 |
/**
|
|
|
1011 |
* Get the property default.
|
|
|
1012 |
*
|
|
|
1013 |
* This method gets the default value of a field (if exists).
|
|
|
1014 |
*
|
|
|
1015 |
* @param string $property property name to be retrieved.
|
|
|
1016 |
* @throws coding_exception if the requested property name is invalid or if it does not has a default value.
|
|
|
1017 |
* @return string the property default value.
|
|
|
1018 |
*/
|
|
|
1019 |
public static function get_property_default($property) {
|
|
|
1020 |
|
|
|
1021 |
self::fill_properties_cache();
|
|
|
1022 |
|
|
|
1023 |
if (!array_key_exists($property, self::$propertiescache) || !isset(self::$propertiescache[$property]['default'])) {
|
|
|
1024 |
throw new coding_exception('Invalid property requested, or the property does not has a default value.');
|
|
|
1025 |
}
|
|
|
1026 |
|
|
|
1027 |
return self::$propertiescache[$property]['default'];
|
|
|
1028 |
}
|
|
|
1029 |
|
|
|
1030 |
/**
|
|
|
1031 |
* Definition of updateable user preferences and rules for data and access validation.
|
|
|
1032 |
*
|
|
|
1033 |
* array(
|
|
|
1034 |
* 'preferencename' => array( // Either exact preference name or a regular expression.
|
|
|
1035 |
* 'null' => NULL_ALLOWED, // Defaults to NULL_NOT_ALLOWED. Takes NULL_NOT_ALLOWED or NULL_ALLOWED.
|
|
|
1036 |
* 'type' => PARAM_TYPE, // Expected parameter type of the user field - mandatory
|
|
|
1037 |
* 'choices' => array(1, 2..) // An array of accepted values of the user field - optional
|
|
|
1038 |
* 'default' => $CFG->setting // An default value for the field - optional
|
|
|
1039 |
* 'isregex' => false/true // Whether the name of the preference is a regular expression (default false).
|
1441 |
ariadna |
1040 |
* 'permissioncallback' => callable // Function accepting arguments ($user, $preferencename) that checks if current
|
|
|
1041 |
* // user
|
1 |
efrain |
1042 |
* // is allowed to modify this preference for given user.
|
1441 |
ariadna |
1043 |
* // If not specified \core\user::default_preference_permission_check() will be assumed.
|
|
|
1044 |
* 'cleancallback' => callable // Custom callback for cleaning value if something more difficult than just type/choices
|
|
|
1045 |
* // is needed accepts arguments ($value, $preferencename)
|
1 |
efrain |
1046 |
* )
|
|
|
1047 |
* )
|
|
|
1048 |
*
|
|
|
1049 |
* @return void
|
|
|
1050 |
*/
|
|
|
1051 |
protected static function fill_preferences_cache() {
|
|
|
1052 |
global $CFG;
|
|
|
1053 |
|
|
|
1054 |
if (self::$preferencescache !== null) {
|
|
|
1055 |
return;
|
|
|
1056 |
}
|
|
|
1057 |
|
|
|
1058 |
// Array of user preferences and expected types/values.
|
|
|
1059 |
// Every preference that can be updated directly by user should be added here.
|
1441 |
ariadna |
1060 |
$preferences = [];
|
|
|
1061 |
$preferences['auth_forcepasswordchange'] = [
|
|
|
1062 |
'type' => PARAM_INT,
|
|
|
1063 |
'null' => NULL_NOT_ALLOWED,
|
|
|
1064 |
'choices' => [0, 1],
|
|
|
1065 |
'permissioncallback' => function ($user, $preferencename) {
|
1 |
efrain |
1066 |
global $USER;
|
|
|
1067 |
$systemcontext = context_system::instance();
|
|
|
1068 |
return ($USER->id != $user->id && (has_capability('moodle/user:update', $systemcontext) ||
|
|
|
1069 |
($user->timecreated > time() - 10 && has_capability('moodle/user:create', $systemcontext))));
|
1441 |
ariadna |
1070 |
},
|
|
|
1071 |
];
|
|
|
1072 |
$preferences['forum_markasreadonnotification'] = [
|
|
|
1073 |
'type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED, 'default' => 1,
|
|
|
1074 |
'choices' => [0, 1],
|
|
|
1075 |
];
|
|
|
1076 |
$preferences['htmleditor'] = [
|
|
|
1077 |
'type' => PARAM_NOTAGS, 'null' => NULL_ALLOWED,
|
|
|
1078 |
'cleancallback' => function ($value, $preferencename) {
|
|
|
1079 |
if (empty($value) || !array_key_exists($value, component::get_plugin_list('editor'))) {
|
1 |
efrain |
1080 |
return null;
|
|
|
1081 |
}
|
|
|
1082 |
return $value;
|
1441 |
ariadna |
1083 |
},
|
|
|
1084 |
];
|
|
|
1085 |
$preferences['badgeprivacysetting'] = [
|
|
|
1086 |
'type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED, 'default' => 1,
|
|
|
1087 |
'choices' => [0, 1], 'permissioncallback' => function ($user, $preferencename) {
|
1 |
efrain |
1088 |
global $CFG;
|
|
|
1089 |
return !empty($CFG->enablebadges) && self::is_current_user($user);
|
1441 |
ariadna |
1090 |
},
|
|
|
1091 |
];
|
|
|
1092 |
$preferences['blogpagesize'] = [
|
|
|
1093 |
'type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED, 'default' => 10,
|
|
|
1094 |
'permissioncallback' => function ($user, $preferencename) {
|
1 |
efrain |
1095 |
return self::is_current_user($user) && has_capability('moodle/blog:view', context_system::instance());
|
1441 |
ariadna |
1096 |
},
|
|
|
1097 |
];
|
1 |
efrain |
1098 |
$preferences['filemanager_recentviewmode'] = [
|
|
|
1099 |
'type' => PARAM_INT,
|
|
|
1100 |
'null' => NULL_NOT_ALLOWED,
|
|
|
1101 |
'default' => 1,
|
|
|
1102 |
'choices' => [1, 2, 3],
|
|
|
1103 |
'permissioncallback' => [static::class, 'is_current_user'],
|
|
|
1104 |
];
|
|
|
1105 |
$preferences['filepicker_recentrepository'] = [
|
|
|
1106 |
'type' => PARAM_INT,
|
|
|
1107 |
'null' => NULL_NOT_ALLOWED,
|
|
|
1108 |
'permissioncallback' => [static::class, 'is_current_user'],
|
|
|
1109 |
];
|
|
|
1110 |
$preferences['filepicker_recentlicense'] = [
|
|
|
1111 |
'type' => PARAM_SAFEDIR,
|
|
|
1112 |
'null' => NULL_NOT_ALLOWED,
|
|
|
1113 |
'permissioncallback' => [static::class, 'is_current_user'],
|
|
|
1114 |
];
|
|
|
1115 |
$preferences['filepicker_recentviewmode'] = [
|
|
|
1116 |
'type' => PARAM_INT,
|
|
|
1117 |
'null' => NULL_NOT_ALLOWED,
|
|
|
1118 |
'default' => 1,
|
|
|
1119 |
'choices' => [1, 2, 3],
|
|
|
1120 |
'permissioncallback' => [static::class, 'is_current_user'],
|
|
|
1121 |
];
|
|
|
1122 |
$preferences['userselector_optionscollapsed'] = [
|
|
|
1123 |
'type' => PARAM_BOOL,
|
|
|
1124 |
'null' => NULL_NOT_ALLOWED,
|
|
|
1125 |
'default' => true,
|
|
|
1126 |
'permissioncallback' => [static::class, 'is_current_user'],
|
|
|
1127 |
];
|
|
|
1128 |
$preferences['userselector_autoselectunique'] = [
|
|
|
1129 |
'type' => PARAM_BOOL,
|
|
|
1130 |
'null' => NULL_NOT_ALLOWED,
|
|
|
1131 |
'default' => false,
|
|
|
1132 |
'permissioncallback' => [static::class, 'is_current_user'],
|
|
|
1133 |
];
|
|
|
1134 |
$preferences['userselector_preserveselected'] = [
|
|
|
1135 |
'type' => PARAM_BOOL,
|
|
|
1136 |
'null' => NULL_NOT_ALLOWED,
|
|
|
1137 |
'default' => false,
|
|
|
1138 |
'permissioncallback' => [static::class, 'is_current_user'],
|
|
|
1139 |
];
|
|
|
1140 |
$preferences['userselector_searchtype'] = [
|
|
|
1141 |
'type' => PARAM_INT,
|
|
|
1142 |
'null' => NULL_NOT_ALLOWED,
|
|
|
1143 |
'default' => USER_SEARCH_STARTS_WITH,
|
|
|
1144 |
'permissioncallback' => [static::class, 'is_current_user'],
|
|
|
1145 |
];
|
|
|
1146 |
$preferences['question_bank_advanced_search'] = [
|
|
|
1147 |
'type' => PARAM_BOOL,
|
|
|
1148 |
'null' => NULL_NOT_ALLOWED,
|
|
|
1149 |
'default' => false,
|
|
|
1150 |
'permissioncallback' => [static::class, 'is_current_user'],
|
|
|
1151 |
];
|
|
|
1152 |
|
|
|
1153 |
$choices = [HOMEPAGE_SITE];
|
|
|
1154 |
if (!empty($CFG->enabledashboard)) {
|
|
|
1155 |
$choices[] = HOMEPAGE_MY;
|
|
|
1156 |
}
|
|
|
1157 |
$choices[] = HOMEPAGE_MYCOURSES;
|
1441 |
ariadna |
1158 |
|
|
|
1159 |
// Allow hook callbacks to extend options.
|
|
|
1160 |
$hook = new \core_user\hook\extend_default_homepage(true);
|
|
|
1161 |
\core\di::get(\core\hook\manager::class)->dispatch($hook);
|
|
|
1162 |
$choices = array_merge($choices, array_keys($hook->get_options()));
|
|
|
1163 |
|
1 |
efrain |
1164 |
$preferences['user_home_page_preference'] = [
|
|
|
1165 |
'null' => NULL_ALLOWED,
|
|
|
1166 |
'default' => get_default_home_page(),
|
|
|
1167 |
'choices' => $choices,
|
|
|
1168 |
'permissioncallback' => function ($user, $preferencename) {
|
|
|
1169 |
global $CFG;
|
|
|
1170 |
return self::is_current_user($user) &&
|
|
|
1171 |
(!empty($CFG->defaulthomepage) && ($CFG->defaulthomepage == HOMEPAGE_USER));
|
1441 |
ariadna |
1172 |
},
|
1 |
efrain |
1173 |
];
|
|
|
1174 |
|
|
|
1175 |
// Core components that may want to define their preferences.
|
|
|
1176 |
// List of core components implementing callback is hardcoded here for performance reasons.
|
|
|
1177 |
// TODO MDL-58184 cache list of core components implementing a function.
|
|
|
1178 |
$corecomponents = ['core_message', 'core_calendar', 'core_contentbank'];
|
|
|
1179 |
foreach ($corecomponents as $component) {
|
|
|
1180 |
if (($pluginpreferences = component_callback($component, 'user_preferences')) && is_array($pluginpreferences)) {
|
|
|
1181 |
$preferences += $pluginpreferences;
|
|
|
1182 |
}
|
|
|
1183 |
}
|
|
|
1184 |
|
|
|
1185 |
// Plugins that may define their preferences.
|
|
|
1186 |
if ($pluginsfunction = get_plugins_with_function('user_preferences')) {
|
|
|
1187 |
foreach ($pluginsfunction as $plugintype => $plugins) {
|
|
|
1188 |
foreach ($plugins as $function) {
|
|
|
1189 |
if (($pluginpreferences = call_user_func($function)) && is_array($pluginpreferences)) {
|
|
|
1190 |
$preferences += $pluginpreferences;
|
|
|
1191 |
}
|
|
|
1192 |
}
|
|
|
1193 |
}
|
|
|
1194 |
}
|
|
|
1195 |
|
|
|
1196 |
self::$preferencescache = $preferences;
|
|
|
1197 |
}
|
|
|
1198 |
|
|
|
1199 |
/**
|
|
|
1200 |
* Retrieves the preference definition
|
|
|
1201 |
*
|
|
|
1202 |
* @param string $preferencename
|
|
|
1203 |
* @return array
|
|
|
1204 |
*/
|
1441 |
ariadna |
1205 |
public static function get_preference_definition($preferencename) {
|
1 |
efrain |
1206 |
self::fill_preferences_cache();
|
|
|
1207 |
|
|
|
1208 |
foreach (self::$preferencescache as $key => $preference) {
|
|
|
1209 |
if (empty($preference['isregex'])) {
|
|
|
1210 |
if ($key === $preferencename) {
|
|
|
1211 |
return $preference;
|
|
|
1212 |
}
|
|
|
1213 |
} else {
|
|
|
1214 |
if (preg_match($key, $preferencename)) {
|
|
|
1215 |
return $preference;
|
|
|
1216 |
}
|
|
|
1217 |
}
|
|
|
1218 |
}
|
|
|
1219 |
|
|
|
1220 |
throw new coding_exception('Invalid preference requested.');
|
|
|
1221 |
}
|
|
|
1222 |
|
|
|
1223 |
/**
|
|
|
1224 |
* Default callback used for checking if current user is allowed to change permission of user $user
|
|
|
1225 |
*
|
|
|
1226 |
* @param stdClass $user
|
|
|
1227 |
* @param string $preferencename
|
|
|
1228 |
* @return bool
|
|
|
1229 |
*/
|
|
|
1230 |
protected static function default_preference_permission_check($user, $preferencename) {
|
|
|
1231 |
global $USER;
|
|
|
1232 |
if (is_mnet_remote_user($user)) {
|
|
|
1233 |
// Can't edit MNET user.
|
|
|
1234 |
return false;
|
|
|
1235 |
}
|
|
|
1236 |
|
|
|
1237 |
if (self::is_current_user($user)) {
|
|
|
1238 |
// Editing own profile.
|
|
|
1239 |
$systemcontext = context_system::instance();
|
|
|
1240 |
return has_capability('moodle/user:editownprofile', $systemcontext);
|
1441 |
ariadna |
1241 |
} else {
|
1 |
efrain |
1242 |
// Teachers, parents, etc.
|
|
|
1243 |
$personalcontext = context_user::instance($user->id);
|
|
|
1244 |
if (!has_capability('moodle/user:editprofile', $personalcontext)) {
|
|
|
1245 |
return false;
|
|
|
1246 |
}
|
1441 |
ariadna |
1247 |
if (is_siteadmin($user->id) && !is_siteadmin($USER)) {
|
1 |
efrain |
1248 |
// Only admins may edit other admins.
|
|
|
1249 |
return false;
|
|
|
1250 |
}
|
|
|
1251 |
return true;
|
|
|
1252 |
}
|
|
|
1253 |
}
|
|
|
1254 |
|
|
|
1255 |
/**
|
|
|
1256 |
* Can current user edit preference of this/another user
|
|
|
1257 |
*
|
|
|
1258 |
* @param string $preferencename
|
|
|
1259 |
* @param stdClass $user
|
|
|
1260 |
* @return bool
|
|
|
1261 |
*/
|
|
|
1262 |
public static function can_edit_preference($preferencename, $user) {
|
|
|
1263 |
if (!isloggedin() || isguestuser()) {
|
|
|
1264 |
// Guests can not edit anything.
|
|
|
1265 |
return false;
|
|
|
1266 |
}
|
|
|
1267 |
|
|
|
1268 |
try {
|
|
|
1269 |
$definition = self::get_preference_definition($preferencename);
|
|
|
1270 |
} catch (coding_exception $e) {
|
|
|
1271 |
return false;
|
|
|
1272 |
}
|
|
|
1273 |
|
|
|
1274 |
if ($user->deleted || !context_user::instance($user->id, IGNORE_MISSING)) {
|
|
|
1275 |
// User is deleted.
|
|
|
1276 |
return false;
|
|
|
1277 |
}
|
|
|
1278 |
|
|
|
1279 |
if (isset($definition['permissioncallback'])) {
|
|
|
1280 |
$callback = $definition['permissioncallback'];
|
|
|
1281 |
if (is_callable($callback)) {
|
|
|
1282 |
return call_user_func_array($callback, [$user, $preferencename]);
|
|
|
1283 |
} else {
|
|
|
1284 |
throw new coding_exception('Permission callback for preference ' . s($preferencename) . ' is not callable');
|
|
|
1285 |
return false;
|
|
|
1286 |
}
|
|
|
1287 |
} else {
|
|
|
1288 |
return self::default_preference_permission_check($user, $preferencename);
|
|
|
1289 |
}
|
|
|
1290 |
}
|
|
|
1291 |
|
|
|
1292 |
/**
|
|
|
1293 |
* Clean value of a user preference
|
|
|
1294 |
*
|
|
|
1295 |
* @param string $value the user preference value to be cleaned.
|
|
|
1296 |
* @param string $preferencename the user preference name
|
|
|
1297 |
* @return string the cleaned preference value
|
|
|
1298 |
*/
|
|
|
1299 |
public static function clean_preference($value, $preferencename) {
|
|
|
1300 |
|
|
|
1301 |
$definition = self::get_preference_definition($preferencename);
|
|
|
1302 |
|
|
|
1303 |
if (isset($definition['type']) && $value !== null) {
|
|
|
1304 |
$value = clean_param($value, $definition['type']);
|
|
|
1305 |
}
|
|
|
1306 |
|
|
|
1307 |
if (isset($definition['cleancallback'])) {
|
|
|
1308 |
$callback = $definition['cleancallback'];
|
|
|
1309 |
if (is_callable($callback)) {
|
|
|
1310 |
return $callback($value, $preferencename);
|
|
|
1311 |
} else {
|
|
|
1312 |
throw new coding_exception('Clean callback for preference ' . s($preferencename) . ' is not callable');
|
|
|
1313 |
}
|
|
|
1314 |
} else if ($value === null && (!isset($definition['null']) || $definition['null'] == NULL_ALLOWED)) {
|
|
|
1315 |
return null;
|
|
|
1316 |
} else if (isset($definition['choices'])) {
|
|
|
1317 |
if (!in_array($value, $definition['choices'])) {
|
|
|
1318 |
if (isset($definition['default'])) {
|
|
|
1319 |
return $definition['default'];
|
|
|
1320 |
} else {
|
|
|
1321 |
$first = reset($definition['choices']);
|
|
|
1322 |
return $first;
|
|
|
1323 |
}
|
|
|
1324 |
} else {
|
|
|
1325 |
return $value;
|
|
|
1326 |
}
|
|
|
1327 |
} else {
|
|
|
1328 |
if ($value === null) {
|
|
|
1329 |
return isset($definition['default']) ? $definition['default'] : '';
|
|
|
1330 |
}
|
|
|
1331 |
return $value;
|
|
|
1332 |
}
|
|
|
1333 |
}
|
|
|
1334 |
|
|
|
1335 |
/**
|
|
|
1336 |
* Is the user expected to perform an action to start using Moodle properly?
|
|
|
1337 |
*
|
|
|
1338 |
* This covers cases such as filling the profile, changing password or agreeing to the site policy.
|
|
|
1339 |
*
|
|
|
1340 |
* @param stdClass $user User object, defaults to the current user.
|
|
|
1341 |
* @return bool
|
|
|
1342 |
*/
|
1441 |
ariadna |
1343 |
public static function awaiting_action(?stdClass $user = null): bool {
|
1 |
efrain |
1344 |
global $USER;
|
|
|
1345 |
|
|
|
1346 |
if ($user === null) {
|
|
|
1347 |
$user = $USER;
|
|
|
1348 |
}
|
|
|
1349 |
|
|
|
1350 |
if (user_not_fully_set_up($user)) {
|
|
|
1351 |
// Awaiting the user to fill all fields in the profile.
|
|
|
1352 |
return true;
|
|
|
1353 |
}
|
|
|
1354 |
|
|
|
1355 |
if (get_user_preferences('auth_forcepasswordchange', false, $user)) {
|
|
|
1356 |
// Awaiting the user to change their password.
|
|
|
1357 |
return true;
|
|
|
1358 |
}
|
|
|
1359 |
|
|
|
1360 |
if (empty($user->policyagreed) && !is_siteadmin($user)) {
|
|
|
1361 |
$manager = new \core_privacy\local\sitepolicy\manager();
|
|
|
1362 |
|
|
|
1363 |
if ($manager->is_defined(isguestuser($user))) {
|
|
|
1364 |
return true;
|
|
|
1365 |
}
|
|
|
1366 |
}
|
|
|
1367 |
|
|
|
1368 |
return false;
|
|
|
1369 |
}
|
|
|
1370 |
|
|
|
1371 |
/**
|
|
|
1372 |
* Get welcome message.
|
|
|
1373 |
*
|
|
|
1374 |
* @return lang_string welcome message
|
|
|
1375 |
*/
|
|
|
1376 |
public static function welcome_message(): ?lang_string {
|
|
|
1377 |
global $USER;
|
|
|
1378 |
|
|
|
1379 |
$isloggedinas = \core\session\manager::is_loggedinas();
|
|
|
1380 |
if (!isloggedin() || isguestuser() || $isloggedinas) {
|
|
|
1381 |
return null;
|
|
|
1382 |
}
|
|
|
1383 |
if (empty($USER->core_welcome_message)) {
|
|
|
1384 |
$USER->core_welcome_message = true;
|
|
|
1385 |
$messagekey = 'welcomeback';
|
|
|
1386 |
if (empty(get_user_preferences('core_user_welcome', null))) {
|
|
|
1387 |
$messagekey = 'welcometosite';
|
|
|
1388 |
set_user_preference('core_user_welcome', time());
|
|
|
1389 |
}
|
|
|
1390 |
|
|
|
1391 |
$namefields = [
|
|
|
1392 |
'fullname' => fullname($USER),
|
|
|
1393 |
'alternativefullname' => fullname($USER, true),
|
|
|
1394 |
];
|
|
|
1395 |
|
|
|
1396 |
foreach (\core_user\fields::get_name_fields() as $namefield) {
|
|
|
1397 |
$namefields[$namefield] = $USER->{$namefield};
|
|
|
1398 |
}
|
|
|
1399 |
|
|
|
1400 |
return new lang_string($messagekey, 'core', $namefields);
|
|
|
1401 |
};
|
|
|
1402 |
return null;
|
|
|
1403 |
}
|
|
|
1404 |
|
|
|
1405 |
/**
|
|
|
1406 |
* Return full name depending on context.
|
|
|
1407 |
* This function should be used for displaying purposes only as the details may not be the same as it is on database.
|
|
|
1408 |
*
|
|
|
1409 |
* @param stdClass $user the person to get details of.
|
|
|
1410 |
* @param context|null $context The context will be used to determine the visibility of the user's full name.
|
|
|
1411 |
* @param array $options can include: override - if true, will not use forced firstname/lastname settings
|
|
|
1412 |
* @return string Full name of the user
|
|
|
1413 |
*/
|
1441 |
ariadna |
1414 |
public static function get_fullname(stdClass $user, ?context $context = null, array $options = []): string {
|
1 |
efrain |
1415 |
global $CFG, $SESSION;
|
|
|
1416 |
|
|
|
1417 |
// Clone the user so that it does not mess up the original object.
|
|
|
1418 |
$user = clone($user);
|
|
|
1419 |
|
|
|
1420 |
// Override options.
|
|
|
1421 |
$override = $options["override"] ?? false;
|
|
|
1422 |
|
|
|
1423 |
if (!isset($user->firstname) && !isset($user->lastname)) {
|
|
|
1424 |
return '';
|
|
|
1425 |
}
|
|
|
1426 |
|
|
|
1427 |
// Get all of the name fields.
|
|
|
1428 |
$allnames = \core_user\fields::get_name_fields();
|
|
|
1429 |
if ($CFG->debugdeveloper) {
|
|
|
1430 |
$missingfields = [];
|
|
|
1431 |
foreach ($allnames as $allname) {
|
|
|
1432 |
if (!property_exists($user, $allname)) {
|
|
|
1433 |
$missingfields[] = $allname;
|
|
|
1434 |
}
|
|
|
1435 |
}
|
|
|
1436 |
if (!empty($missingfields)) {
|
|
|
1437 |
debugging('The following name fields are missing from the user object: ' . implode(', ', $missingfields));
|
|
|
1438 |
}
|
|
|
1439 |
}
|
|
|
1440 |
|
|
|
1441 |
if (!$override) {
|
|
|
1442 |
if (!empty($CFG->forcefirstname)) {
|
|
|
1443 |
$user->firstname = $CFG->forcefirstname;
|
|
|
1444 |
}
|
|
|
1445 |
if (!empty($CFG->forcelastname)) {
|
|
|
1446 |
$user->lastname = $CFG->forcelastname;
|
|
|
1447 |
}
|
|
|
1448 |
}
|
|
|
1449 |
|
|
|
1450 |
if (!empty($SESSION->fullnamedisplay)) {
|
|
|
1451 |
$CFG->fullnamedisplay = $SESSION->fullnamedisplay;
|
|
|
1452 |
}
|
|
|
1453 |
|
|
|
1454 |
$template = null;
|
|
|
1455 |
// If the fullnamedisplay setting is available, set the template to that.
|
|
|
1456 |
if (isset($CFG->fullnamedisplay)) {
|
|
|
1457 |
$template = $CFG->fullnamedisplay;
|
|
|
1458 |
}
|
|
|
1459 |
// If the template is empty, or set to language, return the language string.
|
|
|
1460 |
if ((empty($template) || $template == 'language') && !$override) {
|
|
|
1461 |
return get_string('fullnamedisplay', null, $user);
|
|
|
1462 |
}
|
|
|
1463 |
|
|
|
1464 |
// Check to see if we are displaying according to the alternative full name format.
|
|
|
1465 |
if ($override) {
|
|
|
1466 |
if (empty($CFG->alternativefullnameformat) || $CFG->alternativefullnameformat == 'language') {
|
|
|
1467 |
// Default to show just the user names according to the fullnamedisplay string.
|
|
|
1468 |
return get_string('fullnamedisplay', null, $user);
|
|
|
1469 |
} else {
|
|
|
1470 |
// If the override is true, then change the template to use the complete name.
|
|
|
1471 |
$template = $CFG->alternativefullnameformat;
|
|
|
1472 |
}
|
|
|
1473 |
}
|
|
|
1474 |
|
1441 |
ariadna |
1475 |
$requirednames = [];
|
1 |
efrain |
1476 |
// With each name, see if it is in the display name template, and add it to the required names array if it is.
|
|
|
1477 |
foreach ($allnames as $allname) {
|
|
|
1478 |
if (strpos($template, $allname) !== false) {
|
|
|
1479 |
$requirednames[] = $allname;
|
|
|
1480 |
}
|
|
|
1481 |
}
|
|
|
1482 |
|
|
|
1483 |
$displayname = $template;
|
|
|
1484 |
// Switch in the actual data into the template.
|
|
|
1485 |
foreach ($requirednames as $altname) {
|
|
|
1486 |
if (isset($user->$altname)) {
|
|
|
1487 |
// Using empty() on the below if statement causes breakages.
|
|
|
1488 |
if ((string)$user->$altname == '') {
|
|
|
1489 |
$displayname = str_replace($altname, 'EMPTY', $displayname);
|
|
|
1490 |
} else {
|
|
|
1491 |
$displayname = str_replace($altname, $user->$altname, $displayname);
|
|
|
1492 |
}
|
|
|
1493 |
} else {
|
|
|
1494 |
$displayname = str_replace($altname, 'EMPTY', $displayname);
|
|
|
1495 |
}
|
|
|
1496 |
}
|
|
|
1497 |
// Tidy up any misc. characters (Not perfect, but gets most characters).
|
|
|
1498 |
// Don't remove the "u" at the end of the first expression unless you want garbled characters when combining hiragana or
|
|
|
1499 |
// katakana and parenthesis.
|
1441 |
ariadna |
1500 |
$patterns = [];
|
1 |
efrain |
1501 |
// This regular expression replacement is to fix problems such as 'James () Kirk' Where 'Tiberius' (middlename) has not been
|
|
|
1502 |
// filled in by a user.
|
|
|
1503 |
// The special characters are Japanese brackets that are common enough to make allowances for them (not covered by :punct:).
|
|
|
1504 |
$patterns[] = '/[[:punct:]「」]*EMPTY[[:punct:]「」]*/u';
|
|
|
1505 |
// This regular expression is to remove any double spaces in the display name.
|
|
|
1506 |
$patterns[] = '/\s{2,}/u';
|
|
|
1507 |
foreach ($patterns as $pattern) {
|
|
|
1508 |
$displayname = preg_replace($pattern, ' ', $displayname);
|
|
|
1509 |
}
|
|
|
1510 |
|
|
|
1511 |
// Trimming $displayname will help the next check to ensure that we don't have a display name with spaces.
|
|
|
1512 |
$displayname = trim($displayname);
|
|
|
1513 |
if (empty($displayname)) {
|
|
|
1514 |
// Going with just the first name if no alternate fields are filled out. May be changed later depending on what
|
|
|
1515 |
// people in general feel is a good setting to fall back on.
|
|
|
1516 |
$displayname = $user->firstname;
|
|
|
1517 |
}
|
|
|
1518 |
return $displayname;
|
|
|
1519 |
}
|
|
|
1520 |
|
|
|
1521 |
/**
|
1441 |
ariadna |
1522 |
* Return fullname of a dummy user comprised of configured name fields only
|
|
|
1523 |
*
|
|
|
1524 |
* @param context|null $context
|
|
|
1525 |
* @param array $options
|
|
|
1526 |
* @return string
|
|
|
1527 |
*/
|
|
|
1528 |
public static function get_dummy_fullname(?context $context = null, array $options = []): string {
|
|
|
1529 |
|
|
|
1530 |
// Create a dummy user object containing all name fields.
|
|
|
1531 |
$namefields = \core_user\fields::get_name_fields();
|
|
|
1532 |
$user = (object) array_combine($namefields, $namefields);
|
|
|
1533 |
|
|
|
1534 |
return static::get_fullname($user, $context, $options);
|
|
|
1535 |
}
|
|
|
1536 |
|
|
|
1537 |
/**
|
1 |
efrain |
1538 |
* Return profile url depending on context.
|
|
|
1539 |
*
|
|
|
1540 |
* @param stdClass $user the person to get details of.
|
|
|
1541 |
* @param context|null $context The context will be used to determine the visibility of the user's profile url.
|
1441 |
ariadna |
1542 |
* @return url Profile url of the user
|
1 |
efrain |
1543 |
*/
|
1441 |
ariadna |
1544 |
public static function get_profile_url(stdClass $user, ?context $context = null): url {
|
1 |
efrain |
1545 |
if (empty($user->id)) {
|
|
|
1546 |
throw new coding_exception('User id is required when displaying profile url.');
|
|
|
1547 |
}
|
|
|
1548 |
|
|
|
1549 |
// Params to be passed to the user view page.
|
|
|
1550 |
$params = ['id' => $user->id];
|
|
|
1551 |
|
|
|
1552 |
// Get courseid from context if provided.
|
11 |
efrain |
1553 |
if ($context && $coursecontext = $context->get_course_context(false)) {
|
|
|
1554 |
$params['course'] = $coursecontext->instanceid;
|
1 |
efrain |
1555 |
}
|
|
|
1556 |
|
|
|
1557 |
// If courseid is not set or is set to site id, then return profile page, otherwise return view page.
|
11 |
efrain |
1558 |
if (!isset($params['course']) || $params['course'] == SITEID) {
|
1441 |
ariadna |
1559 |
return new url('/user/profile.php', $params);
|
1 |
efrain |
1560 |
} else {
|
1441 |
ariadna |
1561 |
return new url('/user/view.php', $params);
|
1 |
efrain |
1562 |
}
|
|
|
1563 |
}
|
|
|
1564 |
|
|
|
1565 |
/**
|
|
|
1566 |
* Return user picture depending on context.
|
|
|
1567 |
* This function should be used for displaying purposes only as the details may not be the same as it is on database.
|
|
|
1568 |
*
|
|
|
1569 |
* @param stdClass $user the person to get details of.
|
|
|
1570 |
* @param context|null $context The context will be used to determine the visibility of the user's picture.
|
|
|
1571 |
* @param array $options public properties of {@see user_picture} to be overridden
|
|
|
1572 |
* - courseid = $this->page->course->id (course id of user profile in link)
|
|
|
1573 |
* - size = 35 (size of image)
|
|
|
1574 |
* - link = true (make image clickable - the link leads to user profile)
|
|
|
1575 |
* - popup = false (open in popup)
|
|
|
1576 |
* - alttext = true (add image alt attribute)
|
|
|
1577 |
* - class = image class attribute (default 'userpicture')
|
|
|
1578 |
* - visibletoscreenreaders = true (whether to be visible to screen readers)
|
|
|
1579 |
* - includefullname = false (whether to include the user's full name together with the user picture)
|
|
|
1580 |
* - includetoken = false (whether to use a token for authentication. True for current user, int value for other user id)
|
|
|
1581 |
* @return user_picture User picture object
|
|
|
1582 |
*/
|
1441 |
ariadna |
1583 |
public static function get_profile_picture(stdClass $user, ?context $context = null, array $options = []): user_picture {
|
1 |
efrain |
1584 |
// Create a new user picture object.
|
|
|
1585 |
$userpicture = new user_picture($user);
|
|
|
1586 |
|
|
|
1587 |
// Override the user picture object with the options provided.
|
|
|
1588 |
foreach ($options as $key => $value) {
|
|
|
1589 |
if (property_exists($userpicture, $key)) {
|
|
|
1590 |
$userpicture->$key = $value;
|
|
|
1591 |
}
|
|
|
1592 |
}
|
|
|
1593 |
|
|
|
1594 |
// Return the user picture.
|
|
|
1595 |
return $userpicture;
|
|
|
1596 |
}
|
|
|
1597 |
|
|
|
1598 |
/**
|
|
|
1599 |
* Get initials for users
|
|
|
1600 |
*
|
|
|
1601 |
* @param stdClass $user
|
|
|
1602 |
* @return string
|
|
|
1603 |
*/
|
|
|
1604 |
public static function get_initials(stdClass $user): string {
|
|
|
1605 |
// Get the available name fields.
|
|
|
1606 |
$namefields = \core_user\fields::get_name_fields();
|
1441 |
ariadna |
1607 |
|
1 |
efrain |
1608 |
// Determine the name format by using fullname() and passing the dummy user.
|
1441 |
ariadna |
1609 |
$nameformat = static::get_dummy_fullname();
|
|
|
1610 |
|
1 |
efrain |
1611 |
// Fetch all the available username fields.
|
|
|
1612 |
$availablefields = order_in_string($namefields, $nameformat);
|
|
|
1613 |
// We only want the first and last name fields.
|
|
|
1614 |
if (!empty($availablefields) && count($availablefields) >= 2) {
|
|
|
1615 |
$availablefields = [reset($availablefields), end($availablefields)];
|
|
|
1616 |
}
|
|
|
1617 |
$initials = '';
|
|
|
1618 |
foreach ($availablefields as $userfieldname) {
|
|
|
1619 |
if (!empty($user->$userfieldname)) {
|
|
|
1620 |
$initials .= mb_substr($user->$userfieldname, 0, 1);
|
|
|
1621 |
}
|
|
|
1622 |
}
|
|
|
1623 |
return $initials;
|
|
|
1624 |
}
|
|
|
1625 |
|
1441 |
ariadna |
1626 |
/**
|
|
|
1627 |
* Prepare SQL where clause and associated parameters for any user searching being performed.
|
|
|
1628 |
* This mostly came from core_user\table\participants_search with some slight modifications four our use case.
|
|
|
1629 |
*
|
|
|
1630 |
* @param context $context Context we are in.
|
|
|
1631 |
* @param string $usersearch Array of field mappings (fieldname => SQL code for the value)
|
|
|
1632 |
* @return array SQL query data in the format ['where' => '', 'params' => []].
|
|
|
1633 |
*/
|
|
|
1634 |
public static function get_users_search_sql(context $context, string $usersearch = ''): array {
|
|
|
1635 |
global $DB, $USER;
|
|
|
1636 |
|
|
|
1637 |
$userfields = fields::for_identity($context, false)->with_userpic();
|
|
|
1638 |
['mappings' => $mappings] = (array)$userfields->get_sql('u', true);
|
|
|
1639 |
$userfields = $userfields->get_required_fields();
|
|
|
1640 |
|
|
|
1641 |
$canviewfullnames = has_capability('moodle/site:viewfullnames', $context);
|
|
|
1642 |
|
|
|
1643 |
$params = [];
|
|
|
1644 |
$searchkey1 = 'search01';
|
|
|
1645 |
$searchkey2 = 'search02';
|
|
|
1646 |
$searchkey3 = 'search03';
|
|
|
1647 |
|
|
|
1648 |
$conditions = [];
|
|
|
1649 |
|
|
|
1650 |
// Search by fullname.
|
|
|
1651 |
[$fullname, $fullnameparams] = fields::get_sql_fullname('u', $canviewfullnames);
|
|
|
1652 |
$conditions[] = $DB->sql_like($fullname, ':' . $searchkey1, false, false);
|
|
|
1653 |
$params = array_merge($params, $fullnameparams);
|
|
|
1654 |
|
|
|
1655 |
// Search by email.
|
|
|
1656 |
$email = $DB->sql_like('email', ':' . $searchkey2, false, false);
|
|
|
1657 |
|
|
|
1658 |
if (!in_array('email', $userfields)) {
|
|
|
1659 |
$maildisplay = 'maildisplay0';
|
|
|
1660 |
$userid1 = 'userid01';
|
|
|
1661 |
// Prevent users who hide their email address from being found by others
|
|
|
1662 |
// who aren't allowed to see hidden email addresses.
|
|
|
1663 |
$email = "(". $email ." AND (" .
|
|
|
1664 |
"u.maildisplay <> :$maildisplay " .
|
|
|
1665 |
"OR u.id = :$userid1". // Users can always find themselves.
|
|
|
1666 |
"))";
|
|
|
1667 |
$params[$maildisplay] = self::MAILDISPLAY_HIDE;
|
|
|
1668 |
$params[$userid1] = $USER->id;
|
|
|
1669 |
}
|
|
|
1670 |
|
|
|
1671 |
$conditions[] = $email;
|
|
|
1672 |
|
|
|
1673 |
// Search by idnumber.
|
|
|
1674 |
$idnumber = $DB->sql_like('idnumber', ':' . $searchkey3, false, false);
|
|
|
1675 |
|
|
|
1676 |
if (!in_array('idnumber', $userfields)) {
|
|
|
1677 |
$userid2 = 'userid02';
|
|
|
1678 |
// Users who aren't allowed to see idnumbers should at most find themselves
|
|
|
1679 |
// when searching for an idnumber.
|
|
|
1680 |
$idnumber = "(". $idnumber . " AND u.id = :$userid2)";
|
|
|
1681 |
$params[$userid2] = $USER->id;
|
|
|
1682 |
}
|
|
|
1683 |
|
|
|
1684 |
$conditions[] = $idnumber;
|
|
|
1685 |
|
|
|
1686 |
// Search all user identify fields.
|
|
|
1687 |
$extrasearchfields = fields::get_identity_fields(null, false);
|
|
|
1688 |
foreach ($extrasearchfields as $fieldindex => $extrasearchfield) {
|
|
|
1689 |
if (in_array($extrasearchfield, ['email', 'idnumber', 'country'])) {
|
|
|
1690 |
// Already covered above.
|
|
|
1691 |
continue;
|
|
|
1692 |
}
|
|
|
1693 |
// The param must be short (max 32 characters) so don't include field name.
|
|
|
1694 |
$param = $searchkey3 . '_ident' . $fieldindex;
|
|
|
1695 |
$fieldsql = $mappings[$extrasearchfield];
|
|
|
1696 |
$condition = $DB->sql_like($fieldsql, ':' . $param, false, false);
|
|
|
1697 |
$params[$param] = "%$usersearch%";
|
|
|
1698 |
|
|
|
1699 |
if (!in_array($extrasearchfield, $userfields)) {
|
|
|
1700 |
// User cannot see this field, but allow match if their own account.
|
|
|
1701 |
$userid3 = 'userid03_ident' . $fieldindex;
|
|
|
1702 |
$condition = "(". $condition . " AND u.id = :$userid3)";
|
|
|
1703 |
$params[$userid3] = $USER->id;
|
|
|
1704 |
}
|
|
|
1705 |
$conditions[] = $condition;
|
|
|
1706 |
}
|
|
|
1707 |
|
|
|
1708 |
$where = "(". implode(" OR ", $conditions) .") ";
|
|
|
1709 |
$params[$searchkey1] = "%$usersearch%";
|
|
|
1710 |
$params[$searchkey2] = "%$usersearch%";
|
|
|
1711 |
$params[$searchkey3] = "%$usersearch%";
|
|
|
1712 |
|
|
|
1713 |
return [
|
|
|
1714 |
'where' => $where,
|
|
|
1715 |
'params' => $params,
|
|
|
1716 |
];
|
|
|
1717 |
}
|
|
|
1718 |
|
|
|
1719 |
/**
|
|
|
1720 |
* Generates an array of name placeholders for a given user.
|
|
|
1721 |
*
|
|
|
1722 |
* @param stdClass $user The user object containing name fields.
|
|
|
1723 |
* @return array An associative array of name placeholders.
|
|
|
1724 |
*/
|
|
|
1725 |
public static function get_name_placeholders(stdClass $user): array {
|
|
|
1726 |
$namefields = [
|
|
|
1727 |
'fullname' => fullname($user),
|
|
|
1728 |
'alternativefullname' => fullname($user, true),
|
|
|
1729 |
];
|
|
|
1730 |
foreach (fields::get_name_fields() as $namefield) {
|
|
|
1731 |
$namefields[$namefield] = $user->{$namefield};
|
|
|
1732 |
}
|
|
|
1733 |
return $namefields;
|
|
|
1734 |
}
|
1 |
efrain |
1735 |
}
|
1441 |
ariadna |
1736 |
|
|
|
1737 |
// Alias this class to the old name.
|
|
|
1738 |
// This file will be autoloaded by the legacyclasses autoload system.
|
|
|
1739 |
// In future all uses of this class will be corrected and the legacy references will be removed.
|
|
|
1740 |
class_alias(user::class, \core_user::class);
|