Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
/**
18
 * Profile field API library file.
19
 *
20
 * @package core_user
21
 * @copyright  2007 onwards Shane Elliot {@link http://pukunui.com}
22
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
/**
26
 * Visible to anyone who has the moodle/site:viewuseridentity permission.
27
 * Editable by the profile owner if they have the moodle/user:editownprofile capability
28
 * or any user with the moodle/user:update capability.
29
 */
30
define('PROFILE_VISIBLE_TEACHERS', '3');
31
 
32
/**
33
 * Visible to anyone who can view the user.
34
 * Editable by the profile owner if they have the moodle/user:editownprofile capability
35
 * or any user with the moodle/user:update capability.
36
 */
37
define('PROFILE_VISIBLE_ALL', '2');
38
/**
39
 * Visible to the profile owner or anyone with the moodle/user:viewalldetails capability.
40
 * Editable by the profile owner if they have the moodle/user:editownprofile capability
41
 * or any user with moodle/user:viewalldetails and moodle/user:update capabilities.
42
 */
43
define('PROFILE_VISIBLE_PRIVATE', '1');
44
/**
45
 * Only visible to users with the moodle/user:viewalldetails capability.
46
 * Only editable by users with the moodle/user:viewalldetails and moodle/user:update capabilities.
47
 */
48
define('PROFILE_VISIBLE_NONE', '0');
49
 
50
/**
51
 * Base class for the customisable profile fields.
52
 *
53
 * @package core_user
54
 * @copyright  2007 onwards Shane Elliot {@link http://pukunui.com}
55
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
56
 */
57
class profile_field_base {
58
 
59
    // These 2 variables are really what we're interested in.
60
    // Everything else can be extracted from them.
61
 
62
    /** @var int */
63
    public $fieldid;
64
 
65
    /** @var int */
66
    public $userid;
67
 
68
    /** @var stdClass */
69
    public $field;
70
 
71
    /** @var string */
72
    public $inputname;
73
 
74
    /** @var mixed */
75
    public $data;
76
 
77
    /** @var string */
78
    public $dataformat;
79
 
80
    /** @var string name of the user profile category */
81
    protected $categoryname;
82
 
83
    /**
84
     * Constructor method.
85
     * @param int $fieldid id of the profile from the user_info_field table
86
     * @param int $userid id of the user for whom we are displaying data
87
     * @param stdClass $fielddata optional data for the field object plus additional fields 'hasuserdata', 'data' and 'dataformat'
88
     *    with user data. (If $fielddata->hasuserdata is empty, user data is not available and we should use default data).
89
     *    If this parameter is passed, constructor will not call load_data() at all.
90
     */
91
    public function __construct($fieldid=0, $userid=0, $fielddata=null) {
92
        global $CFG;
93
 
94
        if ($CFG->debugdeveloper) {
95
            // In Moodle 3.4 the new argument $fielddata was added to the constructor. Make sure that
96
            // plugin constructor properly passes this argument.
97
            $backtrace = debug_backtrace();
98
            if (isset($backtrace[1]['class']) && $backtrace[1]['function'] === '__construct' &&
99
                    in_array(self::class, class_parents($backtrace[1]['class']))) {
100
                // If this constructor is called from the constructor of the plugin make sure that the third argument was passed through.
101
                if (count($backtrace[1]['args']) >= 3 && count($backtrace[0]['args']) < 3) {
102
                    debugging($backtrace[1]['class'].'::__construct() must support $fielddata as the third argument ' .
103
                        'and pass it to the parent constructor', DEBUG_DEVELOPER);
104
                }
105
            }
106
        }
107
 
108
        $this->set_fieldid($fieldid);
109
        $this->set_userid($userid);
110
        if ($fielddata) {
111
            $this->set_field($fielddata);
112
            if ($userid > 0 && !empty($fielddata->hasuserdata)) {
113
                $this->set_user_data($fielddata->data, $fielddata->dataformat);
114
            }
115
        } else {
116
            $this->load_data();
117
        }
118
    }
119
 
120
    /**
121
     * Old syntax of class constructor. Deprecated in PHP7.
122
     *
123
     * @deprecated since Moodle 3.1
124
     */
125
    public function profile_field_base($fieldid=0, $userid=0) {
126
        debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
127
        self::__construct($fieldid, $userid);
128
    }
129
 
130
    /**
131
     * Abstract method: Adds the profile field to the moodle form class
132
     * @abstract The following methods must be overwritten by child classes
133
     * @param MoodleQuickForm $mform instance of the moodleform class
134
     */
135
    public function edit_field_add($mform) {
136
        throw new \moodle_exception('mustbeoveride', 'debug', '', 'edit_field_add');
137
    }
138
 
139
    /**
140
     * Display the data for this field
141
     * @return string
142
     */
143
    public function display_data() {
144
        $options = new stdClass();
145
        $options->para = false;
146
        return format_text($this->data, FORMAT_MOODLE, $options);
147
    }
148
 
149
    /**
150
     * Display the name of the profile field.
151
     *
1441 ariadna 152
     * @param bool $escape
1 efrain 153
     * @return string
154
     */
1441 ariadna 155
    public function display_name(bool $escape = true): string {
156
        return format_string($this->field->name, true, [
157
            'context' => context_system::instance(),
158
            'escape' => $escape,
1 efrain 159
        ]);
160
    }
161
 
162
    /**
163
     * Print out the form field in the edit profile page
164
     * @param MoodleQuickForm $mform instance of the moodleform class
165
     * @return bool
166
     */
167
    public function edit_field($mform) {
168
        if (!$this->is_editable()) {
169
            return false;
170
        }
171
 
172
        $this->edit_field_add($mform);
173
        $this->edit_field_set_default($mform);
174
        $this->edit_field_set_required($mform);
175
        return true;
176
    }
177
 
178
    /**
179
     * Tweaks the edit form
180
     * @param MoodleQuickForm $mform instance of the moodleform class
181
     * @return bool
182
     */
183
    public function edit_after_data($mform) {
184
        if (!$this->is_editable()) {
185
            return false;
186
        }
187
 
188
        $this->edit_field_set_locked($mform);
189
        return true;
190
    }
191
 
192
    /**
193
     * Saves the data coming from form
194
     * @param stdClass $usernew data coming from the form
195
     */
196
    public function edit_save_data($usernew) {
197
        global $DB;
198
 
199
        if (!isset($usernew->{$this->inputname})) {
200
            // Field not present in form, probably locked and invisible - skip it.
201
            return;
202
        }
203
 
204
        $data = new stdClass();
205
 
206
        $usernew->{$this->inputname} = $this->edit_save_data_preprocess($usernew->{$this->inputname}, $data);
207
        if (!isset($usernew->{$this->inputname})) {
208
            // Field cannot be set to null, set the default value.
209
            $usernew->{$this->inputname} = $this->field->defaultdata;
210
        }
211
 
212
        $data->userid  = $usernew->id;
213
        $data->fieldid = $this->field->id;
214
        $data->data    = $usernew->{$this->inputname};
215
 
216
        if ($dataid = $DB->get_field('user_info_data', 'id', array('userid' => $data->userid, 'fieldid' => $data->fieldid))) {
217
            $data->id = $dataid;
218
            $DB->update_record('user_info_data', $data);
219
        } else {
220
            $DB->insert_record('user_info_data', $data);
221
        }
222
    }
223
 
224
    /**
225
     * Validate the form field from profile page
226
     *
227
     * @param stdClass $usernew
228
     * @return  array  error messages for the form validation
229
     */
230
    public function edit_validate_field($usernew) {
231
        global $DB;
232
 
233
        $errors = array();
234
        // Get input value.
235
        if (isset($usernew->{$this->inputname})) {
236
            if (is_array($usernew->{$this->inputname}) && isset($usernew->{$this->inputname}['text'])) {
237
                $value = $usernew->{$this->inputname}['text'];
238
            } else {
239
                $value = $usernew->{$this->inputname};
240
            }
241
        } else {
242
            $value = '';
243
        }
244
 
245
        // Check for uniqueness of data if required.
246
        if ($this->is_unique() && (($value !== '') || $this->is_required())) {
247
            $data = $DB->get_records_sql('
248
                    SELECT id, userid
249
                      FROM {user_info_data}
250
                     WHERE fieldid = ?
251
                       AND ' . $DB->sql_compare_text('data', 255) . ' = ' . $DB->sql_compare_text('?', 255),
252
                    array($this->field->id, $value));
253
            if ($data) {
254
                $existing = false;
255
                foreach ($data as $v) {
256
                    if ($v->userid == $usernew->id) {
257
                        $existing = true;
258
                        break;
259
                    }
260
                }
261
                if (!$existing) {
262
                    $errors[$this->inputname] = get_string('valuealreadyused');
263
                }
264
            }
265
        }
266
        return $errors;
267
    }
268
 
269
    /**
270
     * Sets the default data for the field in the form object
271
     * @param MoodleQuickForm $mform instance of the moodleform class
272
     */
273
    public function edit_field_set_default($mform) {
274
        if (isset($this->field->defaultdata)) {
275
            $mform->setDefault($this->inputname, $this->field->defaultdata);
276
        }
277
    }
278
 
279
    /**
280
     * Sets the required flag for the field in the form object
281
     *
282
     * @param MoodleQuickForm $mform instance of the moodleform class
283
     */
284
    public function edit_field_set_required($mform) {
285
        global $USER;
286
        if ($this->is_required() && ($this->userid == $USER->id || isguestuser())) {
287
            $mform->addRule($this->inputname, get_string('required'), 'required', null, 'client');
288
        }
289
    }
290
 
291
    /**
292
     * HardFreeze the field if locked.
293
     * @param MoodleQuickForm $mform instance of the moodleform class
294
     */
295
    public function edit_field_set_locked($mform) {
296
        if (!$mform->elementExists($this->inputname)) {
297
            return;
298
        }
299
        if ($this->is_locked() and !has_capability('moodle/user:update', context_system::instance())) {
300
            $mform->hardFreeze($this->inputname);
301
            $mform->setConstant($this->inputname, $this->data);
302
        }
303
    }
304
 
305
    /**
306
     * Hook for child classess to process the data before it gets saved in database
307
     * @param stdClass $data
308
     * @param stdClass $datarecord The object that will be used to save the record
309
     * @return  mixed
310
     */
311
    public function edit_save_data_preprocess($data, $datarecord) {
312
        return $data;
313
    }
314
 
315
    /**
316
     * Loads a user object with data for this field ready for the edit profile
317
     * form
318
     * @param stdClass $user a user object
319
     */
320
    public function edit_load_user_data($user) {
321
        if ($this->data !== null) {
322
            $user->{$this->inputname} = $this->data;
323
        }
324
    }
325
 
326
    /**
327
     * Check if the field data should be loaded into the user object
328
     * By default it is, but for field types where the data may be potentially
329
     * large, the child class should override this and return false
330
     * @return bool
331
     */
332
    public function is_user_object_data() {
333
        return true;
334
    }
335
 
336
    /**
337
     * Accessor method: set the userid for this instance
338
     * @internal This method should not generally be overwritten by child classes.
339
     * @param integer $userid id from the user table
340
     */
341
    public function set_userid($userid) {
342
        $this->userid = $userid;
343
    }
344
 
345
    /**
346
     * Accessor method: set the fieldid for this instance
347
     * @internal This method should not generally be overwritten by child classes.
348
     * @param integer $fieldid id from the user_info_field table
349
     */
350
    public function set_fieldid($fieldid) {
351
        $this->fieldid = $fieldid;
352
    }
353
 
354
    /**
355
     * Sets the field object and default data and format into $this->data and $this->dataformat
356
     *
357
     * This method should be called before {@link self::set_user_data}
358
     *
359
     * @param stdClass $field
360
     * @throws coding_exception
361
     */
362
    public function set_field($field) {
363
        global $CFG;
364
        if ($CFG->debugdeveloper) {
365
            $properties = ['id', 'shortname', 'name', 'datatype', 'description', 'descriptionformat', 'categoryid', 'sortorder',
366
                'required', 'locked', 'visible', 'forceunique', 'signup', 'defaultdata', 'defaultdataformat', 'param1', 'param2',
367
                'param3', 'param4', 'param5'];
368
            foreach ($properties as $property) {
369
                if (!property_exists($field, $property)) {
370
                    debugging('The \'' . $property . '\' property must be set.', DEBUG_DEVELOPER);
371
                }
372
            }
373
        }
374
        if ($this->fieldid && $this->fieldid != $field->id) {
375
            throw new coding_exception('Can not set field object after a different field id was set');
376
        }
377
        $this->fieldid = $field->id;
378
        $this->field = $field;
379
        $this->inputname = 'profile_field_' . $this->field->shortname;
380
        $this->data = $this->field->defaultdata;
381
        $this->dataformat = FORMAT_HTML;
382
    }
383
 
384
    /**
385
     * Sets user id and user data for the field
386
     *
387
     * @param mixed $data
388
     * @param int $dataformat
389
     */
390
    public function set_user_data($data, $dataformat) {
391
        $this->data = $data;
392
        $this->dataformat = $dataformat;
393
    }
394
 
395
    /**
396
     * Set the name for the profile category where this field is
397
     *
398
     * @param string $categoryname
399
     */
400
    public function set_category_name($categoryname) {
401
        $this->categoryname = $categoryname;
402
    }
403
 
404
    /**
405
     * Return field short name
406
     *
407
     * @return string
408
     */
409
    public function get_shortname(): string {
410
        return $this->field->shortname;
411
    }
412
 
413
    /**
414
     * Returns the name of the profile category where this field is
415
     *
416
     * @return string
417
     */
418
    public function get_category_name() {
419
        global $DB;
420
        if ($this->categoryname === null) {
421
            $this->categoryname = $DB->get_field('user_info_category', 'name', ['id' => $this->field->categoryid]);
422
        }
423
        return $this->categoryname;
424
    }
425
 
426
    /**
427
     * Accessor method: Load the field record and user data associated with the
428
     * object's fieldid and userid
429
     *
430
     * @internal This method should not generally be overwritten by child classes.
431
     */
432
    public function load_data() {
433
        global $DB;
434
 
435
        // Load the field object.
436
        if (($this->fieldid == 0) or (!($field = $DB->get_record('user_info_field', array('id' => $this->fieldid))))) {
437
            $this->field = null;
438
            $this->inputname = '';
439
        } else {
440
            $this->set_field($field);
441
        }
442
 
443
        if (!empty($this->field) && $this->userid > 0) {
444
            $params = array('userid' => $this->userid, 'fieldid' => $this->fieldid);
445
            if ($data = $DB->get_record('user_info_data', $params, 'data, dataformat')) {
446
                $this->set_user_data($data->data, $data->dataformat);
447
            }
448
        } else {
449
            $this->data = null;
450
        }
451
    }
452
 
453
    /**
454
     * Check if the field data is visible to the current user
455
     * @internal This method should not generally be overwritten by child classes.
456
     *
457
     * @param context|null $context
458
     * @return bool
459
     */
460
    public function is_visible(?context $context = null): bool {
461
        global $USER, $COURSE;
462
 
463
        if ($context === null) {
464
            $context = ($this->userid > 0) ? context_user::instance($this->userid) : context_system::instance();
465
        }
466
 
467
        switch ($this->field->visible) {
468
            case PROFILE_VISIBLE_TEACHERS:
469
                if ($this->is_signup_field() && (empty($this->userid) || isguestuser($this->userid))) {
470
                    return true;
471
                } else if ($this->userid == $USER->id) {
472
                    return true;
473
                } else if ($this->userid > 0) {
474
                    return has_capability('moodle/user:viewalldetails', $context);
1441 ariadna 475
                } else if ($context instanceof context_course) {
476
                    return has_capability('moodle/site:viewuseridentity', $context);
1 efrain 477
                } else {
1441 ariadna 478
                    // Fall back to the global course object.
1 efrain 479
                    $coursecontext = context_course::instance($COURSE->id);
480
                    return has_capability('moodle/site:viewuseridentity', $coursecontext);
481
                }
482
            case PROFILE_VISIBLE_ALL:
483
                return true;
484
            case PROFILE_VISIBLE_PRIVATE:
485
                if ($this->is_signup_field() && (empty($this->userid) || isguestuser($this->userid))) {
486
                    return true;
487
                } else if ($this->userid == $USER->id) {
488
                    return true;
489
                } else {
490
                    return has_capability('moodle/user:viewalldetails', $context);
491
                }
492
            default:
493
                // PROFILE_VISIBLE_NONE, so let's check capabilities at system level.
494
                if ($this->userid > 0) {
495
                    $context = context_system::instance();
496
                }
497
                return has_capability('moodle/user:viewalldetails', $context);
498
        }
499
    }
500
 
501
    /**
502
     * Check if the field data is editable for the current user
503
     * This method should not generally be overwritten by child classes.
504
     * @return bool
505
     */
506
    public function is_editable() {
507
        global $USER;
508
 
509
        if (!$this->is_visible()) {
510
            return false;
511
        }
512
 
513
        if ($this->is_signup_field() && (empty($this->userid) || isguestuser($this->userid))) {
514
            // Allow editing the field on the signup page.
515
            return true;
516
        }
517
 
518
        $systemcontext = context_system::instance();
519
 
520
        if ($this->userid == $USER->id && has_capability('moodle/user:editownprofile', $systemcontext)) {
521
            return true;
522
        }
523
 
524
        if (has_capability('moodle/user:update', $systemcontext)) {
525
            return true;
526
        }
527
 
528
        // Checking for mentors have capability to edit user's profile.
529
        if ($this->userid > 0) {
530
            $usercontext = context_user::instance($this->userid);
531
            if ($this->userid != $USER->id && has_capability('moodle/user:editprofile', $usercontext, $USER->id)) {
532
                return true;
533
            }
534
        }
535
 
536
        return false;
537
    }
538
 
539
    /**
540
     * Check if the field data is considered empty
541
     * @internal This method should not generally be overwritten by child classes.
542
     * @return boolean
543
     */
544
    public function is_empty() {
545
        return ( ($this->data != '0') and empty($this->data));
546
    }
547
 
548
    /**
549
     * Check if the field is required on the edit profile page
550
     * @internal This method should not generally be overwritten by child classes.
551
     * @return bool
552
     */
553
    public function is_required() {
554
        return (boolean)$this->field->required;
555
    }
556
 
557
    /**
558
     * Check if the field is locked on the edit profile page
559
     * @internal This method should not generally be overwritten by child classes.
560
     * @return bool
561
     */
562
    public function is_locked() {
563
        return (boolean)$this->field->locked;
564
    }
565
 
566
    /**
567
     * Check if the field data should be unique
568
     * @internal This method should not generally be overwritten by child classes.
569
     * @return bool
570
     */
571
    public function is_unique() {
572
        return (boolean)$this->field->forceunique;
573
    }
574
 
575
    /**
576
     * Check if the field should appear on the signup page
577
     * @internal This method should not generally be overwritten by child classes.
578
     * @return bool
579
     */
580
    public function is_signup_field() {
581
        return (boolean)$this->field->signup;
582
    }
583
 
584
    /**
585
     * Return the field settings suitable to be exported via an external function.
586
     * By default it return all the field settings.
587
     *
588
     * @return array all the settings
589
     * @since Moodle 3.2
590
     */
591
    public function get_field_config_for_external() {
592
        return (array) $this->field;
593
    }
594
 
595
    /**
596
     * Return the field type and null properties.
597
     * This will be used for validating the data submitted by a user.
598
     *
599
     * @return array the param type and null property
600
     * @since Moodle 3.2
601
     */
602
    public function get_field_properties() {
603
        return array(PARAM_RAW, NULL_NOT_ALLOWED);
604
    }
605
 
606
    /**
607
     * Whether to display the field and content to the user
608
     *
609
     * @param context|null $context
610
     * @return bool
611
     */
612
    public function show_field_content(?context $context = null): bool {
613
        return $this->is_visible($context) && !$this->is_empty();
614
    }
615
 
616
    /**
617
     * Check if the field should convert the raw data into user-friendly data when exporting
618
     *
619
     * @return bool
620
     */
621
    public function is_transform_supported(): bool {
622
        return false;
623
    }
624
}
625
 
626
/**
627
 * Return profile field instance for given type
628
 *
629
 * @param string $type
630
 * @param int $fieldid
631
 * @param int $userid
632
 * @param stdClass|null $fielddata
633
 * @return profile_field_base
634
 */
635
function profile_get_user_field(string $type, int $fieldid = 0, int $userid = 0, ?stdClass $fielddata = null): profile_field_base {
636
    global $CFG;
637
 
638
    require_once("{$CFG->dirroot}/user/profile/field/{$type}/field.class.php");
639
 
640
    // Return instance of profile field type.
641
    $profilefieldtype = "profile_field_{$type}";
642
    return new $profilefieldtype($fieldid, $userid, $fielddata);
643
}
644
 
645
/**
646
 * Returns an array of all custom field records with any defined data (or empty data), for the specified user id.
647
 * @param int $userid
648
 * @return profile_field_base[]
649
 */
650
function profile_get_user_fields_with_data(int $userid): array {
651
    global $DB;
652
 
653
    // Join any user info data present with each user info field for the user object.
654
    $sql = 'SELECT uif.*, uic.name AS categoryname ';
655
    if ($userid > 0) {
656
        $sql .= ', uind.id AS hasuserdata, uind.data, uind.dataformat ';
657
    }
658
    $sql .= 'FROM {user_info_field} uif ';
659
    $sql .= 'LEFT JOIN {user_info_category} uic ON uif.categoryid = uic.id ';
660
    if ($userid > 0) {
661
        $sql .= 'LEFT JOIN {user_info_data} uind ON uif.id = uind.fieldid AND uind.userid = :userid ';
662
    }
663
    $sql .= 'ORDER BY uic.sortorder ASC, uif.sortorder ASC ';
664
    $fields = $DB->get_records_sql($sql, ['userid' => $userid]);
665
    $data = [];
666
    foreach ($fields as $field) {
667
        $field->hasuserdata = !empty($field->hasuserdata);
668
        $fieldobject = profile_get_user_field($field->datatype, $field->id, $userid, $field);
669
        $fieldobject->set_category_name($field->categoryname);
670
        unset($field->categoryname);
671
        $data[] = $fieldobject;
672
    }
673
    return $data;
674
}
675
 
676
/**
677
 * Returns an array of all custom field records with any defined data (or empty data), for the specified user id, by category.
678
 * @param int $userid
679
 * @return profile_field_base[][]
680
 */
681
function profile_get_user_fields_with_data_by_category(int $userid): array {
682
    $fields = profile_get_user_fields_with_data($userid);
683
    $data = [];
684
    foreach ($fields as $field) {
685
        $data[$field->field->categoryid][] = $field;
686
    }
687
    return $data;
688
}
689
 
690
/**
691
 * Loads user profile field data into the user object.
692
 * @param stdClass $user
693
 */
694
function profile_load_data(stdClass $user): void {
695
    $fields = profile_get_user_fields_with_data($user->id);
696
    foreach ($fields as $formfield) {
697
        $formfield->edit_load_user_data($user);
698
    }
699
}
700
 
701
/**
702
 * Print out the customisable categories and fields for a users profile
703
 *
704
 * @param MoodleQuickForm $mform instance of the moodleform class
705
 * @param int $userid id of user whose profile is being edited or 0 for the new user
706
 */
707
function profile_definition(MoodleQuickForm $mform, int $userid = 0): void {
708
    $categories = profile_get_user_fields_with_data_by_category($userid);
709
    foreach ($categories as $categoryid => $fields) {
710
        // Check first if *any* fields will be displayed.
711
        $fieldstodisplay = [];
712
 
713
        foreach ($fields as $formfield) {
714
            if ($formfield->is_editable()) {
715
                $fieldstodisplay[] = $formfield;
716
            }
717
        }
718
 
719
        if (empty($fieldstodisplay)) {
720
            continue;
721
        }
722
 
723
        // Display the header and the fields.
724
        $mform->addElement('header', 'category_'.$categoryid, format_string($fields[0]->get_category_name()));
725
        foreach ($fieldstodisplay as $formfield) {
726
            $formfield->edit_field($mform);
727
        }
728
    }
729
}
730
 
731
/**
732
 * Adds profile fields to user edit forms.
733
 * @param MoodleQuickForm $mform
734
 * @param int $userid
735
 */
736
function profile_definition_after_data(MoodleQuickForm $mform, int $userid): void {
737
    $userid = ($userid < 0) ? 0 : (int)$userid;
738
 
739
    $fields = profile_get_user_fields_with_data($userid);
740
    foreach ($fields as $formfield) {
741
        $formfield->edit_after_data($mform);
742
    }
743
}
744
 
745
/**
746
 * Validates profile data.
747
 * @param stdClass $usernew
748
 * @param array $files
749
 * @return array array of errors, same as in {@see moodleform::validation()}
750
 */
751
function profile_validation(stdClass $usernew, array $files): array {
752
    $err = array();
753
    $fields = profile_get_user_fields_with_data($usernew->id);
754
    foreach ($fields as $formfield) {
755
        $err += $formfield->edit_validate_field($usernew, $files);
756
    }
757
    return $err;
758
}
759
 
760
/**
761
 * Saves profile data for a user.
762
 * @param stdClass $usernew
763
 */
764
function profile_save_data(stdClass $usernew): void {
765
    global $CFG;
766
 
767
    $fields = profile_get_user_fields_with_data($usernew->id);
768
    foreach ($fields as $formfield) {
769
        $formfield->edit_save_data($usernew);
770
    }
771
}
772
 
773
/**
774
 * Retrieves a list of profile fields that must be displayed in the sign-up form.
775
 *
776
 * @return array list of profile fields info
777
 * @since Moodle 3.2
778
 */
779
function profile_get_signup_fields(): array {
780
    $profilefields = array();
781
    $fieldobjects = profile_get_user_fields_with_data(0);
782
    foreach ($fieldobjects as $fieldobject) {
783
        $field = (object)$fieldobject->get_field_config_for_external();
784
        if ($fieldobject->get_category_name() !== null && $fieldobject->is_signup_field() && $field->visible <> 0) {
785
            $profilefields[] = (object) array(
786
                'categoryid' => $field->categoryid,
787
                'categoryname' => $fieldobject->get_category_name(),
788
                'fieldid' => $field->id,
789
                'datatype' => $field->datatype,
790
                'object' => $fieldobject
791
            );
792
        }
793
    }
794
    return $profilefields;
795
}
796
 
797
/**
798
 * Adds code snippet to a moodle form object for custom profile fields that
799
 * should appear on the signup page
800
 * @param MoodleQuickForm $mform moodle form object
801
 */
802
function profile_signup_fields(MoodleQuickForm $mform): void {
803
 
804
    if ($fields = profile_get_signup_fields()) {
805
        foreach ($fields as $field) {
806
            // Check if we change the categories.
807
            if (!isset($currentcat) || $currentcat != $field->categoryid) {
808
                 $currentcat = $field->categoryid;
809
                 $mform->addElement('header', 'category_'.$field->categoryid, format_string($field->categoryname));
810
            };
811
            $field->object->edit_field($mform);
812
        }
813
    }
814
}
815
 
816
/**
817
 * Returns an object with the custom profile fields set for the given user
818
 * @param int $userid
819
 * @param bool $onlyinuserobject True if you only want the ones in $USER.
820
 * @return stdClass object where properties names are shortnames of custom profile fields
821
 */
822
function profile_user_record(int $userid, bool $onlyinuserobject = true): stdClass {
823
    $usercustomfields = new stdClass();
824
 
825
    $fields = profile_get_user_fields_with_data($userid);
826
    foreach ($fields as $formfield) {
827
        if (!$onlyinuserobject || $formfield->is_user_object_data()) {
828
            $usercustomfields->{$formfield->field->shortname} = $formfield->data;
829
        }
830
    }
831
 
832
    return $usercustomfields;
833
}
834
 
835
/**
836
 * Obtains a list of all available custom profile fields, indexed by id.
837
 *
838
 * Some profile fields are not included in the user object data (see
839
 * profile_user_record function above). Optionally, you can obtain only those
840
 * fields that are included in the user object.
841
 *
842
 * To be clear, this function returns the available fields, and does not
843
 * return the field values for a particular user.
844
 *
845
 * @param bool $onlyinuserobject True if you only want the ones in $USER
846
 * @return array Array of field objects from database (indexed by id)
847
 * @since Moodle 2.7.1
848
 */
849
function profile_get_custom_fields(bool $onlyinuserobject = false): array {
850
    $fieldobjects = profile_get_user_fields_with_data(0);
851
    $fields = [];
852
    foreach ($fieldobjects as $fieldobject) {
853
        if (!$onlyinuserobject || $fieldobject->is_user_object_data()) {
854
            $fields[$fieldobject->fieldid] = (object)$fieldobject->get_field_config_for_external();
855
        }
856
    }
857
    ksort($fields);
858
    return $fields;
859
}
860
 
861
/**
862
 * Load custom profile fields into user object
863
 *
864
 * @param stdClass $user user object
865
 */
866
function profile_load_custom_fields($user) {
867
    $user->profile = (array)profile_user_record($user->id);
868
}
869
 
870
/**
871
 * Save custom profile fields for a user.
872
 *
873
 * @param int $userid The user id
874
 * @param array $profilefields The fields to save
875
 */
876
function profile_save_custom_fields($userid, $profilefields) {
877
    global $DB;
878
 
879
    $fields = profile_get_user_fields_with_data(0);
880
    if ($fields) {
881
        foreach ($fields as $fieldobject) {
882
            $field = (object)$fieldobject->get_field_config_for_external();
883
            if (isset($profilefields[$field->shortname])) {
884
                $conditions = array('fieldid' => $field->id, 'userid' => $userid);
885
                $id = $DB->get_field('user_info_data', 'id', $conditions);
886
                $data = $profilefields[$field->shortname];
887
                if ($id) {
888
                    $DB->set_field('user_info_data', 'data', $data, array('id' => $id));
889
                } else {
890
                    $record = array('fieldid' => $field->id, 'userid' => $userid, 'data' => $data);
891
                    $DB->insert_record('user_info_data', $record);
892
                }
893
            }
894
        }
895
    }
896
}
897
 
898
/**
899
 * Gets basic data about custom profile fields. This is minimal data that is cached within the
900
 * current request for all fields so that it can be used quickly.
901
 *
902
 * @param string $shortname Shortname of custom profile field
903
 * @param bool $casesensitive Whether to perform case-sensitive matching of shortname. Note current limitations of custom profile
904
 *  fields allow the same shortname to exist differing only by it's case
905
 * @return stdClass|null Object with properties id, shortname, name, visible, datatype, categoryid, etc
906
 */
907
function profile_get_custom_field_data_by_shortname(string $shortname, bool $casesensitive = true): ?stdClass {
908
    $cache = \cache::make_from_params(cache_store::MODE_REQUEST, 'core_profile', 'customfields',
909
            [], ['simplekeys' => true, 'simpledata' => true]);
910
    $data = $cache->get($shortname);
911
    if ($data === false) {
912
        // If we don't have data, we get and cache it for all fields to avoid multiple DB requests.
913
        $fields = profile_get_custom_fields();
914
        $data = null;
915
        foreach ($fields as $field) {
916
            $cache->set($field->shortname, $field);
917
 
918
            // Perform comparison according to case sensitivity parameter.
919
            $shortnamematch = $casesensitive
920
                ? strcmp($field->shortname, $shortname) === 0
921
                : strcasecmp($field->shortname, $shortname) === 0;
922
 
923
            if ($shortnamematch) {
924
                $data = $field;
925
            }
926
        }
927
    }
928
 
929
    return $data;
930
}
931
 
932
/**
933
 * Trigger a user profile viewed event.
934
 *
935
 * @param stdClass  $user user  object
936
 * @param stdClass  $context  context object (course or user)
937
 * @param stdClass  $course course  object
938
 * @since Moodle 2.9
939
 */
940
function profile_view($user, $context, $course = null) {
941
 
942
    $eventdata = array(
943
        'objectid' => $user->id,
944
        'relateduserid' => $user->id,
945
        'context' => $context
946
    );
947
 
948
    if (!empty($course)) {
949
        $eventdata['courseid'] = $course->id;
950
        $eventdata['other'] = array(
951
            'courseid' => $course->id,
952
            'courseshortname' => $course->shortname,
953
            'coursefullname' => $course->fullname
954
        );
955
    }
956
 
957
    $event = \core\event\user_profile_viewed::create($eventdata);
958
    $event->add_record_snapshot('user', $user);
959
    $event->trigger();
960
}
961
 
962
/**
963
 * Does the user have all required custom fields set?
964
 *
965
 * Internal, to be exclusively used by {@link user_not_fully_set_up()} only.
966
 *
967
 * Note that if users have no way to fill a required field via editing their
968
 * profiles (e.g. the field is not visible or it is locked), we still return true.
969
 * So this is actually checking if we should redirect the user to edit their
970
 * profile, rather than whether there is a value in the database.
971
 *
972
 * @param int $userid
973
 * @return bool
974
 */
975
function profile_has_required_custom_fields_set($userid) {
976
    $profilefields = profile_get_user_fields_with_data($userid);
977
    foreach ($profilefields as $profilefield) {
978
        if ($profilefield->is_required() && !$profilefield->is_locked() &&
979
            $profilefield->is_empty() && $profilefield->get_field_config_for_external()['visible']) {
980
            return false;
981
        }
982
    }
983
 
984
    return true;
985
}
986
 
987
/**
988
 * Return the list of valid custom profile user fields.
989
 *
990
 * @return array array of profile field names
991
 */
992
function get_profile_field_names(): array {
993
    $profilefields = profile_get_user_fields_with_data(0);
994
    $profilefieldnames = [];
995
    foreach ($profilefields as $field) {
996
        $profilefieldnames[] = $field->inputname;
997
    }
998
    return $profilefieldnames;
999
}
1000
 
1001
/**
1002
 * Return the list of profile fields
1003
 * in a format they can be used for choices in a group select menu.
1004
 *
1005
 * @return array array of category name with its profile fields
1006
 */
1007
function get_profile_field_list(): array {
1008
    $customfields = profile_get_user_fields_with_data_by_category(0);
1009
    $data = [];
1010
    foreach ($customfields as $category) {
1011
        foreach ($category as $field) {
1012
            $categoryname = $field->get_category_name();
1013
            if (!isset($data[$categoryname])) {
1014
                $data[$categoryname] = [];
1015
            }
1441 ariadna 1016
            $data[$categoryname][$field->inputname] = $field->display_name();
1 efrain 1017
        }
1018
    }
1019
    return $data;
1020
}