Proyectos de Subversion Moodle

Rev

Rev 11 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 11 Rev 1441
Línea 12... Línea 12...
12
// GNU General Public License for more details.
12
// GNU General Public License for more details.
13
//
13
//
14
// You should have received a copy of the GNU General Public License
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/>.
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
Línea 16... Línea -...
16
 
-
 
17
/**
-
 
18
 * User class
-
 
19
 *
16
 
20
 * @package    core
-
 
21
 * @copyright  2013 Rajesh Taneja <rajesh@moodle.com>
-
 
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
-
 
Línea -... Línea 17...
-
 
17
namespace core;
-
 
18
 
-
 
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
 */
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;
Línea 24... Línea 29...
24
 
29
use dml_exception;
25
defined('MOODLE_INTERNAL') || die();
30
use stdClass;
26
 
31
 
27
/**
32
/**
28
 * User class to access user details.
33
 * User class to access user details.
29
 *
34
 *
30
 * @todo       move api's from user/lib.php and deprecate old ones.
35
 * @todo       MDL-82650 Move api's from user/lib.php and deprecate old ones.
31
 * @package    core
36
 * @package    core
32
 * @copyright  2013 Rajesh Taneja <rajesh@moodle.com>
37
 * @copyright  2013 Rajesh Taneja <rajesh@moodle.com>
33
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34
 */
39
 */
35
class core_user {
40
class user {
36
    /**
41
    /**
Línea 76... Línea 81...
76
        'phone2',
81
        'phone2',
77
        'address',
82
        'address',
78
        'firstnamephonetic',
83
        'firstnamephonetic',
79
        'lastnamephonetic',
84
        'lastnamephonetic',
80
        'middlename',
85
        'middlename',
81
        'alternatename'
86
        'alternatename',
82
    ];
87
    ];
Línea 83... Línea 88...
83
 
88
 
84
    /** @var int Indicates that user profile view should be prevented */
89
    /** @var int Indicates that user profile view should be prevented */
85
    const VIEWPROFILE_PREVENT = -1;
90
    const VIEWPROFILE_PREVENT = -1;
Línea 100... Línea 105...
100
    /** @var array store user preferences cache. */
105
    /** @var array store user preferences cache. */
101
    protected static $preferencescache = null;
106
    protected static $preferencescache = null;
Línea 102... Línea 107...
102
 
107
 
103
    /**
108
    /**
104
     * Return user object from db or create noreply or support user,
109
     * Return user object from db or create noreply or support user,
105
     * if userid matches corse_user::NOREPLY_USER or corse_user::SUPPORT_USER
110
     * if userid matches \core\user::NOREPLY_USER or \core\user::SUPPORT_USER
106
     * respectively. If userid is not found, then return false.
111
     * respectively. If userid is not found, then return false.
107
     *
112
     *
108
     * @param int $userid user id
113
     * @param int $userid user id
109
     * @param string $fields A comma separated list of user fields to be returned, support and noreply user
114
     * @param string $fields A comma separated list of user fields to be returned, support and noreply user
Línea 124... Línea 129...
124
                break;
129
                break;
125
            case self::SUPPORT_USER:
130
            case self::SUPPORT_USER:
126
                return self::get_support_user();
131
                return self::get_support_user();
127
                break;
132
                break;
128
            default:
133
            default:
129
                return $DB->get_record('user', array('id' => $userid), $fields, $strictness);
134
                return $DB->get_record('user', ['id' => $userid], $fields, $strictness);
130
        }
135
        }
131
    }
136
    }
Línea 132... Línea 137...
132
 
137
 
133
    /**
138
    /**
Línea 149... Línea 154...
149
        if (empty($mnethostid)) {
154
        if (empty($mnethostid)) {
150
            // If empty, we restrict to local users.
155
            // If empty, we restrict to local users.
151
            $mnethostid = $CFG->mnet_localhost_id;
156
            $mnethostid = $CFG->mnet_localhost_id;
152
        }
157
        }
Línea 153... Línea 158...
153
 
158
 
154
        return $DB->get_record('user', array('email' => $email, 'mnethostid' => $mnethostid), $fields, $strictness);
159
        return $DB->get_record('user', ['email' => $email, 'mnethostid' => $mnethostid], $fields, $strictness);
Línea 155... Línea 160...
155
    }
160
    }
156
 
161
 
157
    /**
162
    /**
Línea 173... Línea 178...
173
        if (empty($mnethostid)) {
178
        if (empty($mnethostid)) {
174
            // If empty, we restrict to local users.
179
            // If empty, we restrict to local users.
175
            $mnethostid = $CFG->mnet_localhost_id;
180
            $mnethostid = $CFG->mnet_localhost_id;
176
        }
181
        }
Línea 177... Línea 182...
177
 
182
 
-
 
183
        return $DB->get_record('user', ['username' => $username, 'mnethostid' => $mnethostid], $fields, $strictness);
-
 
184
    }
-
 
185
 
-
 
186
    /**
-
 
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,
178
        return $DB->get_record('user', array('username' => $username, 'mnethostid' => $mnethostid), $fields, $strictness);
214
        ], $fields, $strictness);
Línea 179... Línea 215...
179
    }
215
    }
180
 
216
 
181
    /**
217
    /**
Línea 206... Línea 242...
206
     *
242
     *
207
     * The returned user objects include id, username, all fields required for user pictures, and
243
     * The returned user objects include id, username, all fields required for user pictures, and
208
     * user identity fields.
244
     * user identity fields.
209
     *
245
     *
210
     * @param string $query Search query text
246
     * @param string $query Search query text
211
     * @param \context_course|null $coursecontext Course context or null if system-wide
247
     * @param context_course|null $coursecontext Course context or null if system-wide
212
     * @param int $max Max number of users to return, default 30 (zero = no limit)
248
     * @param int $max Max number of users to return, default 30 (zero = no limit)
213
     * @param int $querylimit Max number of database queries, default 5 (zero = no limit)
249
     * @param int $querylimit Max number of database queries, default 5 (zero = no limit)
214
     * @return array Array of user objects with limited fields
250
     * @return array Array of user objects with limited fields
215
     */
251
     */
-
 
252
    public static function search(
-
 
253
        $query,
216
    public static function search($query, \context_course $coursecontext = null,
254
        ?context_course $coursecontext = null,
-
 
255
        $max = 30,
217
            $max = 30, $querylimit = 5) {
256
        $querylimit = 5
-
 
257
    ) {
218
        global $CFG, $DB;
258
        global $CFG, $DB;
219
        require_once($CFG->dirroot . '/user/lib.php');
259
        require_once($CFG->dirroot . '/user/lib.php');
Línea 220... Línea 260...
220
 
260
 
221
        // Allow limits to be turned off.
261
        // Allow limits to be turned off.
Línea 225... Línea 265...
225
        if (!$querylimit) {
265
        if (!$querylimit) {
226
            $querylimit = PHP_INT_MAX;
266
            $querylimit = PHP_INT_MAX;
227
        }
267
        }
Línea 228... Línea 268...
228
 
268
 
229
        // Check permission to view profiles at each context.
269
        // Check permission to view profiles at each context.
230
        $systemcontext = \context_system::instance();
270
        $systemcontext = context_system::instance();
231
        $viewsystem = has_capability('moodle/user:viewdetails', $systemcontext);
271
        $viewsystem = has_capability('moodle/user:viewdetails', $systemcontext);
232
        if ($viewsystem) {
272
        if ($viewsystem) {
233
            $userquery = 'SELECT id FROM {user}';
273
            $userquery = 'SELECT id FROM {user}';
234
            $userparams = [];
274
            $userparams = [];
235
        }
275
        }
236
        if (!$viewsystem) {
276
        if (!$viewsystem) {
237
            list($userquery, $userparams) = self::get_enrolled_sql_on_courses_with_capability(
277
            [$userquery, $userparams] = self::get_enrolled_sql_on_courses_with_capability(
-
 
278
                'moodle/user:viewdetails'
238
                    'moodle/user:viewdetails');
279
            );
239
            if (!$userquery) {
280
            if (!$userquery) {
240
                // No permissions anywhere, return nothing.
281
                // No permissions anywhere, return nothing.
241
                return [];
282
                return [];
242
            }
283
            }
Línea 243... Línea 284...
243
        }
284
        }
244
 
285
 
Línea 245... Línea 286...
245
        // Start building the WHERE clause based on name.
286
        // Start building the WHERE clause based on name.
246
        list ($where, $whereparams) = users_search_sql($query, 'u');
287
         [$where, $whereparams] = users_search_sql($query, 'u');
247
 
288
 
248
        // We allow users to search with extra identity fields (as well as name) but only if they
289
        // We allow users to search with extra identity fields (as well as name) but only if they
Línea 274... Línea 315...
274
                // They have permission everywhere so just add the extra query to the normal query.
315
                // They have permission everywhere so just add the extra query to the normal query.
275
                $where .= ' OR ' . $extrasql;
316
                $where .= ' OR ' . $extrasql;
276
                $whereparams = array_merge($whereparams, $extraparams);
317
                $whereparams = array_merge($whereparams, $extraparams);
277
            } else {
318
            } else {
278
                // Get all courses where user can view full user identity.
319
                // Get all courses where user can view full user identity.
279
                list($sql, $params) = self::get_enrolled_sql_on_courses_with_capability(
320
                [$sql, $params] = self::get_enrolled_sql_on_courses_with_capability(
280
                    'moodle/site:viewuseridentity');
321
                    'moodle/site:viewuseridentity'
-
 
322
                );
281
                if ($sql) {
323
                if ($sql) {
282
                    // Join that with the user query to get an extra field indicating if we can.
324
                    // Join that with the user query to get an extra field indicating if we can.
283
                    $userquery = "
325
                    $userquery = "
284
                        SELECT innerusers.id, COUNT(identityusers.id) AS showidentity
326
                        SELECT innerusers.id, COUNT(identityusers.id) AS showidentity
285
                          FROM ($userquery) innerusers
327
                          FROM ($userquery) innerusers
Línea 295... Línea 337...
295
            }
337
            }
296
        }
338
        }
Línea 297... Línea 339...
297
 
339
 
298
        // Default order is just name order. But if searching within a course then we show users
340
        // Default order is just name order. But if searching within a course then we show users
299
        // within the course first.
341
        // within the course first.
300
        list ($order, $orderparams) = users_order_by_sql('u', $query, $systemcontext);
342
         [$order, $orderparams] = users_order_by_sql('u', $query, $systemcontext);
301
        if ($coursecontext) {
343
        if ($coursecontext) {
302
            list ($sql, $params) = get_enrolled_sql($coursecontext);
344
             [$sql, $params] = get_enrolled_sql($coursecontext);
303
            $mainfield = 'innerusers2.id';
345
            $mainfield = 'innerusers2.id';
304
            if ($usingshowidentity) {
346
            if ($usingshowidentity) {
305
                $mainfield .= ', innerusers2.showidentity';
347
                $mainfield .= ', innerusers2.showidentity';
306
            }
348
            }
Línea 319... Línea 361...
319
        $result = [];
361
        $result = [];
320
        $got = 0;
362
        $got = 0;
321
        $pos = 0;
363
        $pos = 0;
322
        $readcount = $max + 2;
364
        $readcount = $max + 2;
323
        for ($i = 0; $i < $querylimit; $i++) {
365
        for ($i = 0; $i < $querylimit; $i++) {
324
            $rawresult = $DB->get_records_sql("
366
            $rawresult = $DB->get_records_sql(
-
 
367
                "
325
                    SELECT $selectfields
368
                    SELECT $selectfields
326
                      FROM ($userquery) users
369
                      FROM ($userquery) users
327
                      JOIN {user} u ON u.id = users.id
370
                      JOIN {user} u ON u.id = users.id
328
                     WHERE $where
371
                     WHERE $where
-
 
372
                  ORDER BY $order",
329
                  ORDER BY $order", array_merge($userparams, $whereparams, $orderparams),
373
                array_merge($userparams, $whereparams, $orderparams),
-
 
374
                $pos,
330
                    $pos, $readcount);
375
                $readcount
-
 
376
            );
331
            foreach ($rawresult as $user) {
377
            foreach ($rawresult as $user) {
332
                // Skip guest.
378
                // Skip guest.
333
                if ($user->username === 'guest') {
379
                if ($user->username === 'guest') {
334
                    continue;
380
                    continue;
335
                }
381
                }
Línea 367... Línea 413...
367
     * @param string $capability Required capability
413
     * @param string $capability Required capability
368
     * @return array Array containing SQL and params, or two nulls if there are no courses
414
     * @return array Array containing SQL and params, or two nulls if there are no courses
369
     */
415
     */
370
    protected static function get_enrolled_sql_on_courses_with_capability($capability) {
416
    protected static function get_enrolled_sql_on_courses_with_capability($capability) {
371
        // Get all courses where user have the capability.
417
        // Get all courses where user have the capability.
372
        $courses = get_user_capability_course($capability, null, true,
418
        $courses = get_user_capability_course(
-
 
419
            $capability,
-
 
420
            null,
-
 
421
            true,
373
                implode(',', array_values(context_helper::get_preload_record_columns('ctx'))));
422
            implode(',', array_values(context_helper::get_preload_record_columns('ctx')))
-
 
423
        );
374
        if (!$courses) {
424
        if (!$courses) {
375
            return [null, null];
425
            return [null, null];
376
        }
426
        }
Línea 377... Línea 427...
377
 
427
 
Línea 380... Línea 430...
380
        // pass an array of courseids, but it doesn't.
430
        // pass an array of courseids, but it doesn't.
381
        $unionsql = '';
431
        $unionsql = '';
382
        $unionparams = [];
432
        $unionparams = [];
383
        foreach ($courses as $course) {
433
        foreach ($courses as $course) {
384
            // Get SQL to list user ids enrolled in this course.
434
            // Get SQL to list user ids enrolled in this course.
385
            \context_helper::preload_from_record($course);
435
            context_helper::preload_from_record($course);
386
            list ($sql, $params) = get_enrolled_sql(\context_course::instance($course->id));
436
             [$sql, $params] = get_enrolled_sql(context_course::instance($course->id));
Línea 387... Línea 437...
387
 
437
 
388
            // Combine to a big union query.
438
            // Combine to a big union query.
389
            if ($unionsql) {
439
            if ($unionsql) {
390
                $unionsql .= ' UNION ';
440
                $unionsql .= ' UNION ';
Línea 524... Línea 574...
524
 
574
 
525
        if ($userid <= 0) {
575
        if ($userid <= 0) {
526
            return false;
576
            return false;
527
        }
577
        }
528
        if ($checkdb) {
578
        if ($checkdb) {
529
            return $DB->record_exists('user', array('id' => $userid));
579
            return $DB->record_exists('user', ['id' => $userid]);
530
        } else {
580
        } else {
531
            return true;
581
            return true;
532
        }
582
        }
Línea 569... Línea 619...
569
 
619
 
570
        if (isguestuser($user)) {
620
        if (isguestuser($user)) {
571
            throw new moodle_exception('guestsarenotallowed', 'error');
621
            throw new moodle_exception('guestsarenotallowed', 'error');
Línea 572... Línea 622...
572
        }
622
        }
573
 
623
 
574
        if ($checksuspended and $user->suspended) {
624
        if ($checksuspended && $user->suspended) {
Línea 575... Línea 625...
575
            throw new moodle_exception('suspended', 'auth');
625
            throw new moodle_exception('suspended', 'auth');
576
        }
626
        }
577
 
627
 
578
        if ($checknologin and $user->auth == 'nologin') {
628
        if ($checknologin && $user->auth == 'nologin') {
Línea 579... Línea 629...
579
            throw new moodle_exception('suspended', 'auth');
629
            throw new moodle_exception('suspended', 'auth');
Línea 585... Línea 635...
585
     *
635
     *
586
     * @param stdClass $usernew An object that contains some information about the user being updated
636
     * @param stdClass $usernew An object that contains some information about the user being updated
587
     * @param array $filemanageroptions
637
     * @param array $filemanageroptions
588
     * @return bool True if the user was updated, false if it stayed the same.
638
     * @return bool True if the user was updated, false if it stayed the same.
589
     */
639
     */
590
    public static function update_picture(stdClass $usernew, $filemanageroptions = array()) {
640
    public static function update_picture(stdClass $usernew, $filemanageroptions = []) {
591
        global $CFG, $DB;
641
        global $CFG, $DB;
592
        require_once("$CFG->libdir/gdlib.php");
642
        require_once("$CFG->libdir/gdlib.php");
Línea 593... Línea 643...
593
 
643
 
594
        $context = context_user::instance($usernew->id, MUST_EXIST);
644
        $context = context_user::instance($usernew->id, MUST_EXIST);
Línea 595... Línea 645...
595
        $user = core_user::get_user($usernew->id, 'id, picture', MUST_EXIST);
645
        $user = self::get_user($usernew->id, 'id, picture', MUST_EXIST);
596
 
646
 
597
        $newpicture = $user->picture;
647
        $newpicture = $user->picture;
598
        // Get file_storage to process files.
648
        // Get file_storage to process files.
Línea 632... Línea 682...
632
                return false;
682
                return false;
633
            }
683
            }
634
        }
684
        }
Línea 635... Línea 685...
635
 
685
 
636
        if ($newpicture != $user->picture) {
686
        if ($newpicture != $user->picture) {
637
            $DB->set_field('user', 'picture', $newpicture, array('id' => $user->id));
687
            $DB->set_field('user', 'picture', $newpicture, ['id' => $user->id]);
638
            return true;
688
            return true;
639
        } else {
689
        } else {
640
            return false;
690
            return false;
641
        }
691
        }
Línea 665... Línea 715...
665
            return;
715
            return;
666
        }
716
        }
Línea 667... Línea 717...
667
 
717
 
668
        // Array of user fields properties and expected parameters.
718
        // Array of user fields properties and expected parameters.
669
        // Every new field on the user table should be added here otherwise it won't be validated.
719
        // Every new field on the user table should be added here otherwise it won't be validated.
670
        $fields = array();
720
        $fields = [];
671
        $fields['id'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED);
721
        $fields['id'] = ['type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED];
672
        $fields['auth'] = array('type' => PARAM_AUTH, 'null' => NULL_NOT_ALLOWED);
722
        $fields['auth'] = ['type' => PARAM_AUTH, 'null' => NULL_NOT_ALLOWED];
673
        $fields['confirmed'] = array('type' => PARAM_BOOL, 'null' => NULL_NOT_ALLOWED);
723
        $fields['confirmed'] = ['type' => PARAM_BOOL, 'null' => NULL_NOT_ALLOWED];
674
        $fields['policyagreed'] = array('type' => PARAM_BOOL, 'null' => NULL_NOT_ALLOWED);
724
        $fields['policyagreed'] = ['type' => PARAM_BOOL, 'null' => NULL_NOT_ALLOWED];
675
        $fields['deleted'] = array('type' => PARAM_BOOL, 'null' => NULL_NOT_ALLOWED);
725
        $fields['deleted'] = ['type' => PARAM_BOOL, 'null' => NULL_NOT_ALLOWED];
676
        $fields['suspended'] = array('type' => PARAM_BOOL, 'null' => NULL_NOT_ALLOWED);
726
        $fields['suspended'] = ['type' => PARAM_BOOL, 'null' => NULL_NOT_ALLOWED];
677
        $fields['mnethostid'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED);
727
        $fields['mnethostid'] = ['type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED];
678
        $fields['username'] = array('type' => PARAM_USERNAME, 'null' => NULL_NOT_ALLOWED);
728
        $fields['username'] = ['type' => PARAM_USERNAME, 'null' => NULL_NOT_ALLOWED];
679
        $fields['password'] = array('type' => PARAM_RAW, 'null' => NULL_NOT_ALLOWED);
729
        $fields['password'] = ['type' => PARAM_RAW, 'null' => NULL_NOT_ALLOWED];
680
        $fields['idnumber'] = array('type' => PARAM_RAW, 'null' => NULL_NOT_ALLOWED);
730
        $fields['idnumber'] = ['type' => PARAM_RAW, 'null' => NULL_NOT_ALLOWED];
681
        $fields['firstname'] = array('type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED);
731
        $fields['firstname'] = ['type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED];
682
        $fields['lastname'] = array('type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED);
732
        $fields['lastname'] = ['type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED];
683
        $fields['surname'] = array('type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED);
733
        $fields['surname'] = ['type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED];
684
        $fields['email'] = array('type' => PARAM_RAW_TRIMMED, 'null' => NULL_NOT_ALLOWED);
734
        $fields['email'] = ['type' => PARAM_RAW_TRIMMED, 'null' => NULL_NOT_ALLOWED];
685
        $fields['emailstop'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED, 'default' => 0);
735
        $fields['emailstop'] = ['type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED, 'default' => 0];
686
        $fields['phone1'] = array('type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED);
736
        $fields['phone1'] = ['type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED];
687
        $fields['phone2'] = array('type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED);
737
        $fields['phone2'] = ['type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED];
688
        $fields['institution'] = array('type' => PARAM_TEXT, 'null' => NULL_NOT_ALLOWED);
738
        $fields['institution'] = ['type' => PARAM_TEXT, 'null' => NULL_NOT_ALLOWED];
689
        $fields['department'] = array('type' => PARAM_TEXT, 'null' => NULL_NOT_ALLOWED);
739
        $fields['department'] = ['type' => PARAM_TEXT, 'null' => NULL_NOT_ALLOWED];
690
        $fields['address'] = array('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,
691
        $fields['city'] = array('type' => PARAM_TEXT, 'null' => NULL_NOT_ALLOWED, 'default' => $CFG->defaultcity);
744
            'null' => NULL_NOT_ALLOWED,
-
 
745
            'default' => $CFG->country,
-
 
746
            'choices' => array_merge(
692
        $fields['country'] = array('type' => PARAM_ALPHA, 'null' => NULL_NOT_ALLOWED, 'default' => $CFG->country,
747
                ['' => ''],
-
 
748
                get_string_manager()->get_list_of_countries(true, true)
-
 
749
            ),
-
 
750
        ];
-
 
751
        $fields['lang'] = [
693
                'choices' => array_merge(array('' => ''), get_string_manager()->get_list_of_countries(true, true)));
752
            'type' => PARAM_LANG,
694
        $fields['lang'] = array('type' => PARAM_LANG, 'null' => NULL_NOT_ALLOWED,
753
            'null' => NULL_NOT_ALLOWED,
-
 
754
            'default' => (!empty($CFG->autolangusercreation) && !empty($SESSION->lang)) ? $SESSION->lang : $CFG->lang,
-
 
755
            'choices' => array_merge(
695
                'default' => (!empty($CFG->autolangusercreation) && !empty($SESSION->lang)) ? $SESSION->lang : $CFG->lang,
756
                ['' => ''],
-
 
757
                get_string_manager()->get_list_of_translations(false)
-
 
758
            ),
-
 
759
        ];
-
 
760
        $fields['calendartype'] = [
-
 
761
            'type' => PARAM_PLUGIN,
696
                'choices' => array_merge(array('' => ''), get_string_manager()->get_list_of_translations(false)));
762
            'null' => NULL_NOT_ALLOWED,
-
 
763
            'default' => $CFG->calendartype,
-
 
764
            'choices' => array_merge(
697
        $fields['calendartype'] = array('type' => PARAM_PLUGIN, 'null' => NULL_NOT_ALLOWED, 'default' => $CFG->calendartype,
765
                ['' => ''],
-
 
766
                \core_calendar\type_factory::get_list_of_calendar_types()
-
 
767
            ),
-
 
768
        ];
-
 
769
        $fields['theme'] = [
698
                'choices' => array_merge(array('' => ''), \core_calendar\type_factory::get_list_of_calendar_types()));
770
            'type' => PARAM_THEME,
699
        $fields['theme'] = array('type' => PARAM_THEME, 'null' => NULL_NOT_ALLOWED,
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.
700
                'default' => theme_config::DEFAULT_THEME, 'choices' => array_merge(array('' => ''), get_list_of_themes()));
780
            'type' => PARAM_TIMEZONE,
701
        $fields['timezone'] = array('type' => PARAM_TIMEZONE, 'null' => NULL_NOT_ALLOWED,
781
            'null' => NULL_NOT_ALLOWED,
-
 
782
            'default' => core_date::get_server_timezone(),
702
                'default' => core_date::get_server_timezone()); // Must not use choices here: timezones can come and go.
783
        ];
703
        $fields['firstaccess'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED);
784
        $fields['firstaccess'] = ['type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED];
704
        $fields['lastaccess'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED);
785
        $fields['lastaccess'] = ['type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED];
705
        $fields['lastlogin'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED);
786
        $fields['lastlogin'] = ['type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED];
706
        $fields['currentlogin'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED);
787
        $fields['currentlogin'] = ['type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED];
707
        $fields['lastip'] = array('type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED);
788
        $fields['lastip'] = ['type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED];
708
        $fields['secret'] = array('type' => PARAM_ALPHANUM, 'null' => NULL_NOT_ALLOWED);
789
        $fields['secret'] = ['type' => PARAM_ALPHANUM, 'null' => NULL_NOT_ALLOWED];
709
        $fields['picture'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED);
790
        $fields['picture'] = ['type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED];
710
        $fields['description'] = array('type' => PARAM_RAW, 'null' => NULL_ALLOWED);
791
        $fields['description'] = ['type' => PARAM_RAW, 'null' => NULL_ALLOWED];
-
 
792
        $fields['descriptionformat'] = ['type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED];
-
 
793
        $fields['mailformat'] = [
711
        $fields['descriptionformat'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED);
794
            'type' => PARAM_INT,
712
        $fields['mailformat'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED,
795
            'null' => NULL_NOT_ALLOWED,
-
 
796
            'default' => $CFG->defaultpreference_mailformat,
-
 
797
        ];
-
 
798
        $fields['maildigest'] = [
713
                'default' => $CFG->defaultpreference_mailformat);
799
            'type' => PARAM_INT,
714
        $fields['maildigest'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED,
800
            'null' => NULL_NOT_ALLOWED,
-
 
801
            'default' => $CFG->defaultpreference_maildigest,
-
 
802
        ];
-
 
803
        $fields['maildisplay'] = [
715
                'default' => $CFG->defaultpreference_maildigest);
804
            'type' => PARAM_INT,
716
        $fields['maildisplay'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED,
805
            'null' => NULL_NOT_ALLOWED,
-
 
806
            'default' => $CFG->defaultpreference_maildisplay,
-
 
807
        ];
-
 
808
        $fields['autosubscribe'] = [
717
                'default' => $CFG->defaultpreference_maildisplay);
809
            'type' => PARAM_INT,
718
        $fields['autosubscribe'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED,
810
            'null' => NULL_NOT_ALLOWED,
-
 
811
            'default' => $CFG->defaultpreference_autosubscribe,
-
 
812
        ];
-
 
813
        $fields['trackforums'] = [
719
                'default' => $CFG->defaultpreference_autosubscribe);
814
            'type' => PARAM_INT,
720
        $fields['trackforums'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED,
815
            'null' => NULL_NOT_ALLOWED,
-
 
816
            'default' => $CFG->defaultpreference_trackforums,
721
                'default' => $CFG->defaultpreference_trackforums);
817
        ];
722
        $fields['timecreated'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED);
818
        $fields['timecreated'] = ['type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED];
723
        $fields['timemodified'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED);
819
        $fields['timemodified'] = ['type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED];
724
        $fields['trustbitmask'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED);
820
        $fields['trustbitmask'] = ['type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED];
725
        $fields['imagealt'] = array('type' => PARAM_TEXT, 'null' => NULL_ALLOWED);
821
        $fields['imagealt'] = ['type' => PARAM_TEXT, 'null' => NULL_ALLOWED];
726
        $fields['lastnamephonetic'] = array('type' => PARAM_NOTAGS, 'null' => NULL_ALLOWED);
822
        $fields['lastnamephonetic'] = ['type' => PARAM_NOTAGS, 'null' => NULL_ALLOWED];
727
        $fields['firstnamephonetic'] = array('type' => PARAM_NOTAGS, 'null' => NULL_ALLOWED);
823
        $fields['firstnamephonetic'] = ['type' => PARAM_NOTAGS, 'null' => NULL_ALLOWED];
728
        $fields['middlename'] = array('type' => PARAM_NOTAGS, 'null' => NULL_ALLOWED);
824
        $fields['middlename'] = ['type' => PARAM_NOTAGS, 'null' => NULL_ALLOWED];
Línea 729... Línea 825...
729
        $fields['alternatename'] = array('type' => PARAM_NOTAGS, 'null' => NULL_ALLOWED);
825
        $fields['alternatename'] = ['type' => PARAM_NOTAGS, 'null' => NULL_ALLOWED];
730
 
826
 
Línea 731... Línea 827...
731
        self::$propertiescache = $fields;
827
        self::$propertiescache = $fields;
Línea 766... Línea 862...
766
            try {
862
            try {
767
                if (isset(self::$propertiescache[$property])) {
863
                if (isset(self::$propertiescache[$property])) {
768
                    validate_param($value, self::$propertiescache[$property]['type'], self::$propertiescache[$property]['null']);
864
                    validate_param($value, self::$propertiescache[$property]['type'], self::$propertiescache[$property]['null']);
769
                }
865
                }
770
                // Check that the value is part of a list of allowed values.
866
                // Check that the value is part of a list of allowed values.
-
 
867
                if (
771
                if (!empty(self::$propertiescache[$property]['choices']) &&
868
                    !empty(self::$propertiescache[$property]['choices']) &&
772
                        !isset(self::$propertiescache[$property]['choices'][$value])) {
869
                    !isset(self::$propertiescache[$property]['choices'][$value])
-
 
870
                ) {
773
                    throw new invalid_parameter_exception($value);
871
                    throw new invalid_parameter_exception($value);
774
                }
872
                }
775
            } catch (invalid_parameter_exception $e) {
873
            } catch (invalid_parameter_exception $e) {
776
                $errors[$property] = $e->getMessage();
874
                $errors[$property] = $e->getMessage();
777
            }
875
            }
Línea 786... Línea 884...
786
     * During unit tests we need to be able to reset all caches so that each new test starts in a known state.
884
     * During unit tests we need to be able to reset all caches so that each new test starts in a known state.
787
     * Intended for use only for testing, phpunit calls this before every test.
885
     * Intended for use only for testing, phpunit calls this before every test.
788
     */
886
     */
789
    public static function reset_caches() {
887
    public static function reset_caches() {
790
        self::$propertiescache = null;
888
        self::$propertiescache = null;
-
 
889
        self::$preferencescache = null;
791
    }
890
    }
Línea 792... Línea 891...
792
 
891
 
793
    /**
892
    /**
794
     * Clean the user data.
893
     * Clean the user data.
Línea 802... Línea 901...
802
        }
901
        }
Línea 803... Línea 902...
803
 
902
 
804
        foreach ($user as $field => $value) {
903
        foreach ($user as $field => $value) {
805
            // Get the property parameter type and do the cleaning.
904
            // Get the property parameter type and do the cleaning.
806
            try {
905
            try {
807
                $user->$field = core_user::clean_field($value, $field);
906
                $user->$field = self::clean_field($value, $field);
808
            } catch (coding_exception $e) {
907
            } catch (coding_exception $e) {
809
                debugging("The property '$field' could not be cleaned.", DEBUG_DEVELOPER);
908
                debugging("The property '$field' could not be cleaned.", DEBUG_DEVELOPER);
810
            }
909
            }
Línea 824... Línea 923...
824
        if (empty($data) || empty($field)) {
923
        if (empty($data) || empty($field)) {
825
            return $data;
924
            return $data;
826
        }
925
        }
Línea 827... Línea 926...
827
 
926
 
828
        try {
927
        try {
Línea 829... Línea 928...
829
            $type = core_user::get_property_type($field);
928
            $type = self::get_property_type($field);
830
 
929
 
831
            if (isset(self::$propertiescache[$field]['choices'])) {
930
            if (isset(self::$propertiescache[$field]['choices'])) {
832
                if (!array_key_exists($data, self::$propertiescache[$field]['choices'])) {
931
                if (!array_key_exists($data, self::$propertiescache[$field]['choices'])) {
Línea 896... Línea 995...
896
     */
995
     */
897
    public static function get_property_choices($property) {
996
    public static function get_property_choices($property) {
Línea 898... Línea 997...
898
 
997
 
Línea -... Línea 998...
-
 
998
        self::fill_properties_cache();
899
        self::fill_properties_cache();
999
 
900
 
1000
        if (
901
        if (!array_key_exists($property, self::$propertiescache) && !array_key_exists('choices',
1001
            !array_key_exists($property, self::$propertiescache) &&
902
                self::$propertiescache[$property])) {
1002
            !array_key_exists('choices', self::$propertiescache[$property])
903
 
1003
        ) {
Línea 904... Línea 1004...
904
            throw new coding_exception('Invalid property requested, or the property does not has a list of choices.');
1004
            throw new coding_exception('Invalid property requested, or the property does not has a list of choices.');
905
        }
1005
        }
Línea 935... Línea 1035...
935
     *          'null' => NULL_ALLOWED,    // Defaults to NULL_NOT_ALLOWED. Takes NULL_NOT_ALLOWED or NULL_ALLOWED.
1035
     *          'null' => NULL_ALLOWED,    // Defaults to NULL_NOT_ALLOWED. Takes NULL_NOT_ALLOWED or NULL_ALLOWED.
936
     *          'type' => PARAM_TYPE,      // Expected parameter type of the user field - mandatory
1036
     *          'type' => PARAM_TYPE,      // Expected parameter type of the user field - mandatory
937
     *          'choices' => array(1, 2..) // An array of accepted values of the user field - optional
1037
     *          'choices' => array(1, 2..) // An array of accepted values of the user field - optional
938
     *          'default' => $CFG->setting // An default value for the field - optional
1038
     *          'default' => $CFG->setting // An default value for the field - optional
939
     *          'isregex' => false/true    // Whether the name of the preference is a regular expression (default false).
1039
     *          'isregex' => false/true    // Whether the name of the preference is a regular expression (default false).
940
     *          'permissioncallback' => callable // Function accepting arguments ($user, $preferencename) that checks if current user
1040
     *          'permissioncallback' => callable // Function accepting arguments ($user, $preferencename) that checks if current
-
 
1041
     *                                     // user
941
     *                                     // is allowed to modify this preference for given user.
1042
     *                                     // is allowed to modify this preference for given user.
942
     *                                     // If not specified core_user::default_preference_permission_check() will be assumed.
1043
     *                                     // If not specified \core\user::default_preference_permission_check() will be assumed.
943
     *          'cleancallback' => callable // Custom callback for cleaning value if something more difficult than just type/choices is needed
1044
     *          'cleancallback' => callable // Custom callback for cleaning value if something more difficult than just type/choices
944
     *                                     // accepts arguments ($value, $preferencename)
1045
     *                                      // is needed accepts arguments ($value, $preferencename)
945
     *     )
1046
     *     )
946
     * )
1047
     * )
947
     *
1048
     *
948
     * @return void
1049
     * @return void
949
     */
1050
     */
Línea 954... Línea 1055...
954
            return;
1055
            return;
955
        }
1056
        }
Línea 956... Línea 1057...
956
 
1057
 
957
        // Array of user preferences and expected types/values.
1058
        // Array of user preferences and expected types/values.
958
        // Every preference that can be updated directly by user should be added here.
1059
        // Every preference that can be updated directly by user should be added here.
959
        $preferences = array();
1060
        $preferences = [];
-
 
1061
        $preferences['auth_forcepasswordchange'] = [
-
 
1062
            'type' => PARAM_INT,
-
 
1063
            'null' => NULL_NOT_ALLOWED,
960
        $preferences['auth_forcepasswordchange'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED, 'choices' => array(0, 1),
1064
            'choices' => [0, 1],
961
            'permissioncallback' => function($user, $preferencename) {
1065
            'permissioncallback' => function ($user, $preferencename) {
962
                global $USER;
1066
                global $USER;
963
                $systemcontext = context_system::instance();
1067
                $systemcontext = context_system::instance();
964
                return ($USER->id != $user->id && (has_capability('moodle/user:update', $systemcontext) ||
1068
                return ($USER->id != $user->id && (has_capability('moodle/user:update', $systemcontext) ||
965
                        ($user->timecreated > time() - 10 && has_capability('moodle/user:create', $systemcontext))));
1069
                        ($user->timecreated > time() - 10 && has_capability('moodle/user:create', $systemcontext))));
-
 
1070
            },
-
 
1071
        ];
966
            });
1072
        $preferences['forum_markasreadonnotification'] = [
967
        $preferences['forum_markasreadonnotification'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED, 'default' => 1,
1073
            'type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED, 'default' => 1,
-
 
1074
            'choices' => [0, 1],
-
 
1075
        ];
968
            'choices' => array(0, 1));
1076
        $preferences['htmleditor'] = [
969
        $preferences['htmleditor'] = array('type' => PARAM_NOTAGS, 'null' => NULL_ALLOWED,
1077
            'type' => PARAM_NOTAGS, 'null' => NULL_ALLOWED,
970
            'cleancallback' => function($value, $preferencename) {
1078
            'cleancallback' => function ($value, $preferencename) {
971
                if (empty($value) || !array_key_exists($value, core_component::get_plugin_list('editor'))) {
1079
                if (empty($value) || !array_key_exists($value, component::get_plugin_list('editor'))) {
972
                    return null;
1080
                    return null;
973
                }
1081
                }
974
                return $value;
1082
                return $value;
-
 
1083
            },
-
 
1084
        ];
975
            });
1085
        $preferences['badgeprivacysetting'] = [
976
        $preferences['badgeprivacysetting'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED, 'default' => 1,
1086
            'type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED, 'default' => 1,
977
            'choices' => array(0, 1), 'permissioncallback' => function($user, $preferencename) {
1087
            'choices' => [0, 1], 'permissioncallback' => function ($user, $preferencename) {
978
                global $CFG;
1088
                global $CFG;
979
                return !empty($CFG->enablebadges) && self::is_current_user($user);
1089
                return !empty($CFG->enablebadges) && self::is_current_user($user);
-
 
1090
            },
-
 
1091
        ];
980
            });
1092
        $preferences['blogpagesize'] = [
981
        $preferences['blogpagesize'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED, 'default' => 10,
1093
            'type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED, 'default' => 10,
982
            'permissioncallback' => function($user, $preferencename) {
1094
            'permissioncallback' => function ($user, $preferencename) {
983
                return self::is_current_user($user) && has_capability('moodle/blog:view', context_system::instance());
1095
                return self::is_current_user($user) && has_capability('moodle/blog:view', context_system::instance());
-
 
1096
            },
984
            });
1097
        ];
985
        $preferences['filemanager_recentviewmode'] = [
1098
        $preferences['filemanager_recentviewmode'] = [
986
            'type' => PARAM_INT,
1099
            'type' => PARAM_INT,
987
            'null' => NULL_NOT_ALLOWED,
1100
            'null' => NULL_NOT_ALLOWED,
988
            'default' => 1,
1101
            'default' => 1,
Línea 1040... Línea 1153...
1040
        $choices = [HOMEPAGE_SITE];
1153
        $choices = [HOMEPAGE_SITE];
1041
        if (!empty($CFG->enabledashboard)) {
1154
        if (!empty($CFG->enabledashboard)) {
1042
            $choices[] = HOMEPAGE_MY;
1155
            $choices[] = HOMEPAGE_MY;
1043
        }
1156
        }
1044
        $choices[] = HOMEPAGE_MYCOURSES;
1157
        $choices[] = HOMEPAGE_MYCOURSES;
-
 
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
 
1045
        $preferences['user_home_page_preference'] = [
1164
        $preferences['user_home_page_preference'] = [
1046
            'type' => PARAM_INT,
-
 
1047
            'null' => NULL_ALLOWED,
1165
            'null' => NULL_ALLOWED,
1048
            'default' => get_default_home_page(),
1166
            'default' => get_default_home_page(),
1049
            'choices' => $choices,
1167
            'choices' => $choices,
1050
            'permissioncallback' => function ($user, $preferencename) {
1168
            'permissioncallback' => function ($user, $preferencename) {
1051
                global $CFG;
1169
                global $CFG;
1052
                return self::is_current_user($user) &&
1170
                return self::is_current_user($user) &&
1053
                    (!empty($CFG->defaulthomepage) && ($CFG->defaulthomepage == HOMEPAGE_USER));
1171
                    (!empty($CFG->defaulthomepage) && ($CFG->defaulthomepage == HOMEPAGE_USER));
1054
            }
1172
            },
1055
        ];
1173
        ];
Línea 1056... Línea 1174...
1056
 
1174
 
1057
        // Core components that may want to define their preferences.
1175
        // Core components that may want to define their preferences.
1058
        // List of core components implementing callback is hardcoded here for performance reasons.
1176
        // List of core components implementing callback is hardcoded here for performance reasons.
Línea 1082... Línea 1200...
1082
     * Retrieves the preference definition
1200
     * Retrieves the preference definition
1083
     *
1201
     *
1084
     * @param string $preferencename
1202
     * @param string $preferencename
1085
     * @return array
1203
     * @return array
1086
     */
1204
     */
1087
    protected static function get_preference_definition($preferencename) {
1205
    public static function get_preference_definition($preferencename) {
1088
        self::fill_preferences_cache();
1206
        self::fill_preferences_cache();
Línea 1089... Línea 1207...
1089
 
1207
 
1090
        foreach (self::$preferencescache as $key => $preference) {
1208
        foreach (self::$preferencescache as $key => $preference) {
1091
            if (empty($preference['isregex'])) {
1209
            if (empty($preference['isregex'])) {
Línea 1118... Línea 1236...
1118
 
1236
 
1119
        if (self::is_current_user($user)) {
1237
        if (self::is_current_user($user)) {
1120
            // Editing own profile.
1238
            // Editing own profile.
1121
            $systemcontext = context_system::instance();
1239
            $systemcontext = context_system::instance();
1122
            return has_capability('moodle/user:editownprofile', $systemcontext);
1240
            return has_capability('moodle/user:editownprofile', $systemcontext);
1123
        } else  {
1241
        } else {
1124
            // Teachers, parents, etc.
1242
            // Teachers, parents, etc.
1125
            $personalcontext = context_user::instance($user->id);
1243
            $personalcontext = context_user::instance($user->id);
1126
            if (!has_capability('moodle/user:editprofile', $personalcontext)) {
1244
            if (!has_capability('moodle/user:editprofile', $personalcontext)) {
1127
                return false;
1245
                return false;
1128
            }
1246
            }
1129
            if (is_siteadmin($user->id) and !is_siteadmin($USER)) {
1247
            if (is_siteadmin($user->id) && !is_siteadmin($USER)) {
1130
                // Only admins may edit other admins.
1248
                // Only admins may edit other admins.
1131
                return false;
1249
                return false;
1132
            }
1250
            }
1133
            return true;
1251
            return true;
Línea 1220... Línea 1338...
1220
     * This covers cases such as filling the profile, changing password or agreeing to the site policy.
1338
     * This covers cases such as filling the profile, changing password or agreeing to the site policy.
1221
     *
1339
     *
1222
     * @param stdClass $user User object, defaults to the current user.
1340
     * @param stdClass $user User object, defaults to the current user.
1223
     * @return bool
1341
     * @return bool
1224
     */
1342
     */
1225
    public static function awaiting_action(stdClass $user = null): bool {
1343
    public static function awaiting_action(?stdClass $user = null): bool {
1226
        global $USER;
1344
        global $USER;
Línea 1227... Línea 1345...
1227
 
1345
 
1228
        if ($user === null) {
1346
        if ($user === null) {
1229
            $user = $USER;
1347
            $user = $USER;
Línea 1291... Línea 1409...
1291
     * @param stdClass $user the person to get details of.
1409
     * @param stdClass $user the person to get details of.
1292
     * @param context|null $context The context will be used to determine the visibility of the user's full name.
1410
     * @param context|null $context The context will be used to determine the visibility of the user's full name.
1293
     * @param array $options can include: override - if true, will not use forced firstname/lastname settings
1411
     * @param array $options can include: override - if true, will not use forced firstname/lastname settings
1294
     * @return string Full name of the user
1412
     * @return string Full name of the user
1295
     */
1413
     */
1296
    public static function get_fullname(stdClass $user, context $context = null, array $options = []): string {
1414
    public static function get_fullname(stdClass $user, ?context $context = null, array $options = []): string {
1297
        global $CFG, $SESSION;
1415
        global $CFG, $SESSION;
Línea 1298... Línea 1416...
1298
 
1416
 
1299
        // Clone the user so that it does not mess up the original object.
1417
        // Clone the user so that it does not mess up the original object.
Línea 1352... Línea 1470...
1352
                // If the override is true, then change the template to use the complete name.
1470
                // If the override is true, then change the template to use the complete name.
1353
                $template = $CFG->alternativefullnameformat;
1471
                $template = $CFG->alternativefullnameformat;
1354
            }
1472
            }
1355
        }
1473
        }
Línea 1356... Línea 1474...
1356
 
1474
 
1357
        $requirednames = array();
1475
        $requirednames = [];
1358
        // With each name, see if it is in the display name template, and add it to the required names array if it is.
1476
        // With each name, see if it is in the display name template, and add it to the required names array if it is.
1359
        foreach ($allnames as $allname) {
1477
        foreach ($allnames as $allname) {
1360
            if (strpos($template, $allname) !== false) {
1478
            if (strpos($template, $allname) !== false) {
1361
                $requirednames[] = $allname;
1479
                $requirednames[] = $allname;
Línea 1377... Línea 1495...
1377
            }
1495
            }
1378
        }
1496
        }
1379
        // Tidy up any misc. characters (Not perfect, but gets most characters).
1497
        // Tidy up any misc. characters (Not perfect, but gets most characters).
1380
        // Don't remove the "u" at the end of the first expression unless you want garbled characters when combining hiragana or
1498
        // Don't remove the "u" at the end of the first expression unless you want garbled characters when combining hiragana or
1381
        // katakana and parenthesis.
1499
        // katakana and parenthesis.
1382
        $patterns = array();
1500
        $patterns = [];
1383
        // This regular expression replacement is to fix problems such as 'James () Kirk' Where 'Tiberius' (middlename) has not been
1501
        // This regular expression replacement is to fix problems such as 'James () Kirk' Where 'Tiberius' (middlename) has not been
1384
        // filled in by a user.
1502
        // filled in by a user.
1385
        // The special characters are Japanese brackets that are common enough to make allowances for them (not covered by :punct:).
1503
        // The special characters are Japanese brackets that are common enough to make allowances for them (not covered by :punct:).
1386
        $patterns[] = '/[[:punct:]「」]*EMPTY[[:punct:]「」]*/u';
1504
        $patterns[] = '/[[:punct:]「」]*EMPTY[[:punct:]「」]*/u';
1387
        // This regular expression is to remove any double spaces in the display name.
1505
        // This regular expression is to remove any double spaces in the display name.
Línea 1399... Línea 1517...
1399
        }
1517
        }
1400
        return $displayname;
1518
        return $displayname;
1401
    }
1519
    }
Línea 1402... Línea 1520...
1402
 
1520
 
-
 
1521
    /**
-
 
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
 
1403
    /**
1537
    /**
1404
     * Return profile url depending on context.
1538
     * Return profile url depending on context.
1405
     *
1539
     *
1406
     * @param stdClass $user the person to get details of.
1540
     * @param stdClass $user the person to get details of.
1407
     * @param context|null $context The context will be used to determine the visibility of the user's profile url.
1541
     * @param context|null $context The context will be used to determine the visibility of the user's profile url.
1408
     * @return moodle_url Profile url of the user
1542
     * @return url Profile url of the user
1409
     */
1543
     */
1410
    public static function get_profile_url(stdClass $user, context $context = null): moodle_url {
1544
    public static function get_profile_url(stdClass $user, ?context $context = null): url {
1411
        if (empty($user->id)) {
1545
        if (empty($user->id)) {
1412
            throw new coding_exception('User id is required when displaying profile url.');
1546
            throw new coding_exception('User id is required when displaying profile url.');
Línea 1413... Línea 1547...
1413
        }
1547
        }
Línea 1420... Línea 1554...
1420
            $params['course'] = $coursecontext->instanceid;
1554
            $params['course'] = $coursecontext->instanceid;
1421
        }
1555
        }
Línea 1422... Línea 1556...
1422
 
1556
 
1423
        // If courseid is not set or is set to site id, then return profile page, otherwise return view page.
1557
        // If courseid is not set or is set to site id, then return profile page, otherwise return view page.
1424
        if (!isset($params['course']) || $params['course'] == SITEID) {
1558
        if (!isset($params['course']) || $params['course'] == SITEID) {
1425
            return new moodle_url('/user/profile.php', $params);
1559
            return new url('/user/profile.php', $params);
1426
        } else {
1560
        } else {
1427
            return new moodle_url('/user/view.php', $params);
1561
            return new url('/user/view.php', $params);
1428
        }
1562
        }
Línea 1429... Línea 1563...
1429
    }
1563
    }
1430
 
1564
 
Línea 1444... Línea 1578...
1444
     *     - visibletoscreenreaders = true (whether to be visible to screen readers)
1578
     *     - visibletoscreenreaders = true (whether to be visible to screen readers)
1445
     *     - includefullname = false (whether to include the user's full name together with the user picture)
1579
     *     - includefullname = false (whether to include the user's full name together with the user picture)
1446
     *     - includetoken = false (whether to use a token for authentication. True for current user, int value for other user id)
1580
     *     - includetoken = false (whether to use a token for authentication. True for current user, int value for other user id)
1447
     * @return user_picture User picture object
1581
     * @return user_picture User picture object
1448
     */
1582
     */
1449
    public static function get_profile_picture(stdClass $user, context $context = null, array $options = []): user_picture {
1583
    public static function get_profile_picture(stdClass $user, ?context $context = null, array $options = []): user_picture {
1450
        // Create a new user picture object.
1584
        // Create a new user picture object.
1451
        $userpicture = new user_picture($user);
1585
        $userpicture = new user_picture($user);
Línea 1452... Línea 1586...
1452
 
1586
 
1453
        // Override the user picture object with the options provided.
1587
        // Override the user picture object with the options provided.
Línea 1468... Línea 1602...
1468
     * @return string
1602
     * @return string
1469
     */
1603
     */
1470
    public static function get_initials(stdClass $user): string {
1604
    public static function get_initials(stdClass $user): string {
1471
        // Get the available name fields.
1605
        // Get the available name fields.
1472
        $namefields = \core_user\fields::get_name_fields();
1606
        $namefields = \core_user\fields::get_name_fields();
1473
        // Build a dummy user to determine the name format.
-
 
1474
        $dummyuser = array_combine($namefields, $namefields);
-
 
-
 
1607
 
1475
        // Determine the name format by using fullname() and passing the dummy user.
1608
        // Determine the name format by using fullname() and passing the dummy user.
1476
        $nameformat = fullname((object) $dummyuser);
1609
        $nameformat = static::get_dummy_fullname();
-
 
1610
 
1477
        // Fetch all the available username fields.
1611
        // Fetch all the available username fields.
1478
        $availablefields = order_in_string($namefields, $nameformat);
1612
        $availablefields = order_in_string($namefields, $nameformat);
1479
        // We only want the first and last name fields.
1613
        // We only want the first and last name fields.
1480
        if (!empty($availablefields) && count($availablefields) >= 2) {
1614
        if (!empty($availablefields) && count($availablefields) >= 2) {
1481
            $availablefields = [reset($availablefields), end($availablefields)];
1615
            $availablefields = [reset($availablefields), end($availablefields)];
Línea 1487... Línea 1621...
1487
            }
1621
            }
1488
        }
1622
        }
1489
        return $initials;
1623
        return $initials;
1490
    }
1624
    }
Línea -... Línea 1625...
-
 
1625
 
-
 
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;
1491
 
1734
    }
-
 
1735
}
-
 
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.