forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
664 lines (586 loc) · 21.9 KB
/
lib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Profile field API library file.
*
* @package core_user
* @copyright 2007 onwards Shane Elliot {@link http://pukunui.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define ('PROFILE_VISIBLE_ALL', '2'); // Only visible for users with moodle/user:update capability.
define ('PROFILE_VISIBLE_PRIVATE', '1'); // Either we are viewing our own profile or we have moodle/user:update capability.
define ('PROFILE_VISIBLE_NONE', '0'); // Only visible for moodle/user:update capability.
/**
* Base class for the customisable profile fields.
*
* @package core_user
* @copyright 2007 onwards Shane Elliot {@link http://pukunui.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class profile_field_base {
// These 2 variables are really what we're interested in.
// Everything else can be extracted from them.
/** @var int */
public $fieldid;
/** @var int */
public $userid;
/** @var stdClass */
public $field;
/** @var string */
public $inputname;
/** @var mixed */
public $data;
/** @var string */
public $dataformat;
/**
* Constructor method.
* @param int $fieldid id of the profile from the user_info_field table
* @param int $userid id of the user for whom we are displaying data
*/
public function __construct($fieldid=0, $userid=0) {
global $USER;
$this->set_fieldid($fieldid);
$this->set_userid($userid);
$this->load_data();
}
/**
* Old syntax of class constructor. Deprecated in PHP7.
*
* @deprecated since Moodle 3.1
*/
public function profile_field_base($fieldid=0, $userid=0) {
debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
self::__construct($fieldid, $userid);
}
/**
* Abstract method: Adds the profile field to the moodle form class
* @abstract The following methods must be overwritten by child classes
* @param moodleform $mform instance of the moodleform class
*/
public function edit_field_add($mform) {
print_error('mustbeoveride', 'debug', '', 'edit_field_add');
}
/**
* Display the data for this field
* @return string
*/
public function display_data() {
$options = new stdClass();
$options->para = false;
return format_text($this->data, FORMAT_MOODLE, $options);
}
/**
* Print out the form field in the edit profile page
* @param moodleform $mform instance of the moodleform class
* @return bool
*/
public function edit_field($mform) {
if ($this->field->visible != PROFILE_VISIBLE_NONE
or has_capability('moodle/user:update', context_system::instance())) {
$this->edit_field_add($mform);
$this->edit_field_set_default($mform);
$this->edit_field_set_required($mform);
return true;
}
return false;
}
/**
* Tweaks the edit form
* @param moodleform $mform instance of the moodleform class
* @return bool
*/
public function edit_after_data($mform) {
if ($this->field->visible != PROFILE_VISIBLE_NONE
or has_capability('moodle/user:update', context_system::instance())) {
$this->edit_field_set_locked($mform);
return true;
}
return false;
}
/**
* Saves the data coming from form
* @param stdClass $usernew data coming from the form
* @return mixed returns data id if success of db insert/update, false on fail, 0 if not permitted
*/
public function edit_save_data($usernew) {
global $DB;
if (!isset($usernew->{$this->inputname})) {
// Field not present in form, probably locked and invisible - skip it.
return;
}
$data = new stdClass();
$usernew->{$this->inputname} = $this->edit_save_data_preprocess($usernew->{$this->inputname}, $data);
$data->userid = $usernew->id;
$data->fieldid = $this->field->id;
$data->data = $usernew->{$this->inputname};
if ($dataid = $DB->get_field('user_info_data', 'id', array('userid' => $data->userid, 'fieldid' => $data->fieldid))) {
$data->id = $dataid;
$DB->update_record('user_info_data', $data);
} else {
$DB->insert_record('user_info_data', $data);
}
}
/**
* Validate the form field from profile page
*
* @param stdClass $usernew
* @return string contains error message otherwise null
*/
public function edit_validate_field($usernew) {
global $DB;
$errors = array();
// Get input value.
if (isset($usernew->{$this->inputname})) {
if (is_array($usernew->{$this->inputname}) && isset($usernew->{$this->inputname}['text'])) {
$value = $usernew->{$this->inputname}['text'];
} else {
$value = $usernew->{$this->inputname};
}
} else {
$value = '';
}
// Check for uniqueness of data if required.
if ($this->is_unique() && (($value !== '') || $this->is_required())) {
$data = $DB->get_records_sql('
SELECT id, userid
FROM {user_info_data}
WHERE fieldid = ?
AND ' . $DB->sql_compare_text('data', 255) . ' = ' . $DB->sql_compare_text('?', 255),
array($this->field->id, $value));
if ($data) {
$existing = false;
foreach ($data as $v) {
if ($v->userid == $usernew->id) {
$existing = true;
break;
}
}
if (!$existing) {
$errors[$this->inputname] = get_string('valuealreadyused');
}
}
}
return $errors;
}
/**
* Sets the default data for the field in the form object
* @param moodleform $mform instance of the moodleform class
*/
public function edit_field_set_default($mform) {
if (!empty($default)) {
$mform->setDefault($this->inputname, $this->field->defaultdata);
}
}
/**
* Sets the required flag for the field in the form object
*
* @param moodleform $mform instance of the moodleform class
*/
public function edit_field_set_required($mform) {
global $USER;
if ($this->is_required() && ($this->userid == $USER->id || isguestuser())) {
$mform->addRule($this->inputname, get_string('required'), 'required', null, 'client');
}
}
/**
* HardFreeze the field if locked.
* @param moodleform $mform instance of the moodleform class
*/
public function edit_field_set_locked($mform) {
if (!$mform->elementExists($this->inputname)) {
return;
}
if ($this->is_locked() and !has_capability('moodle/user:update', context_system::instance())) {
$mform->hardFreeze($this->inputname);
$mform->setConstant($this->inputname, $this->data);
}
}
/**
* Hook for child classess to process the data before it gets saved in database
* @param stdClass $data
* @param stdClass $datarecord The object that will be used to save the record
* @return mixed
*/
public function edit_save_data_preprocess($data, $datarecord) {
return $data;
}
/**
* Loads a user object with data for this field ready for the edit profile
* form
* @param stdClass $user a user object
*/
public function edit_load_user_data($user) {
if ($this->data !== null) {
$user->{$this->inputname} = $this->data;
}
}
/**
* Check if the field data should be loaded into the user object
* By default it is, but for field types where the data may be potentially
* large, the child class should override this and return false
* @return bool
*/
public function is_user_object_data() {
return true;
}
/**
* Accessor method: set the userid for this instance
* @internal This method should not generally be overwritten by child classes.
* @param integer $userid id from the user table
*/
public function set_userid($userid) {
$this->userid = $userid;
}
/**
* Accessor method: set the fieldid for this instance
* @internal This method should not generally be overwritten by child classes.
* @param integer $fieldid id from the user_info_field table
*/
public function set_fieldid($fieldid) {
$this->fieldid = $fieldid;
}
/**
* Accessor method: Load the field record and user data associated with the
* object's fieldid and userid
* @internal This method should not generally be overwritten by child classes.
*/
public function load_data() {
global $DB;
// Load the field object.
if (($this->fieldid == 0) or (!($field = $DB->get_record('user_info_field', array('id' => $this->fieldid))))) {
$this->field = null;
$this->inputname = '';
} else {
$this->field = $field;
$this->inputname = 'profile_field_'.$field->shortname;
}
if (!empty($this->field)) {
$params = array('userid' => $this->userid, 'fieldid' => $this->fieldid);
if ($data = $DB->get_record('user_info_data', $params, 'data, dataformat')) {
$this->data = $data->data;
$this->dataformat = $data->dataformat;
} else {
$this->data = $this->field->defaultdata;
$this->dataformat = FORMAT_HTML;
}
} else {
$this->data = null;
}
}
/**
* Check if the field data is visible to the current user
* @internal This method should not generally be overwritten by child classes.
* @return bool
*/
public function is_visible() {
global $USER;
switch ($this->field->visible) {
case PROFILE_VISIBLE_ALL:
return true;
case PROFILE_VISIBLE_PRIVATE:
if ($this->userid == $USER->id) {
return true;
} else {
return has_capability('moodle/user:viewalldetails',
context_user::instance($this->userid));
}
default:
return has_capability('moodle/user:viewalldetails',
context_user::instance($this->userid));
}
}
/**
* Check if the field data is considered empty
* @internal This method should not generally be overwritten by child classes.
* @return boolean
*/
public function is_empty() {
return ( ($this->data != '0') and empty($this->data));
}
/**
* Check if the field is required on the edit profile page
* @internal This method should not generally be overwritten by child classes.
* @return bool
*/
public function is_required() {
return (boolean)$this->field->required;
}
/**
* Check if the field is locked on the edit profile page
* @internal This method should not generally be overwritten by child classes.
* @return bool
*/
public function is_locked() {
return (boolean)$this->field->locked;
}
/**
* Check if the field data should be unique
* @internal This method should not generally be overwritten by child classes.
* @return bool
*/
public function is_unique() {
return (boolean)$this->field->forceunique;
}
/**
* Check if the field should appear on the signup page
* @internal This method should not generally be overwritten by child classes.
* @return bool
*/
public function is_signup_field() {
return (boolean)$this->field->signup;
}
}
/**
* Loads user profile field data into the user object.
* @param stdClass $user
*/
function profile_load_data($user) {
global $CFG, $DB;
if ($fields = $DB->get_records('user_info_field')) {
foreach ($fields as $field) {
require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
$newfield = 'profile_field_'.$field->datatype;
$formfield = new $newfield($field->id, $user->id);
$formfield->edit_load_user_data($user);
}
}
}
/**
* Print out the customisable categories and fields for a users profile
*
* @param moodleform $mform instance of the moodleform class
* @param int $userid id of user whose profile is being edited.
*/
function profile_definition($mform, $userid = 0) {
global $CFG, $DB;
// If user is "admin" fields are displayed regardless.
$update = has_capability('moodle/user:update', context_system::instance());
if ($categories = $DB->get_records('user_info_category', null, 'sortorder ASC')) {
foreach ($categories as $category) {
if ($fields = $DB->get_records('user_info_field', array('categoryid' => $category->id), 'sortorder ASC')) {
// Check first if *any* fields will be displayed.
$display = false;
foreach ($fields as $field) {
if ($field->visible != PROFILE_VISIBLE_NONE) {
$display = true;
}
}
// Display the header and the fields.
if ($display or $update) {
$mform->addElement('header', 'category_'.$category->id, format_string($category->name));
foreach ($fields as $field) {
require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
$newfield = 'profile_field_'.$field->datatype;
$formfield = new $newfield($field->id, $userid);
$formfield->edit_field($mform);
}
}
}
}
}
}
/**
* Adds profile fields to user edit forms.
* @param moodleform $mform
* @param int $userid
*/
function profile_definition_after_data($mform, $userid) {
global $CFG, $DB;
$userid = ($userid < 0) ? 0 : (int)$userid;
if ($fields = $DB->get_records('user_info_field')) {
foreach ($fields as $field) {
require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
$newfield = 'profile_field_'.$field->datatype;
$formfield = new $newfield($field->id, $userid);
$formfield->edit_after_data($mform);
}
}
}
/**
* Validates profile data.
* @param stdClass $usernew
* @param array $files
* @return array
*/
function profile_validation($usernew, $files) {
global $CFG, $DB;
$err = array();
if ($fields = $DB->get_records('user_info_field')) {
foreach ($fields as $field) {
require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
$newfield = 'profile_field_'.$field->datatype;
$formfield = new $newfield($field->id, $usernew->id);
$err += $formfield->edit_validate_field($usernew, $files);
}
}
return $err;
}
/**
* Saves profile data for a user.
* @param stdClass $usernew
*/
function profile_save_data($usernew) {
global $CFG, $DB;
if ($fields = $DB->get_records('user_info_field')) {
foreach ($fields as $field) {
require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
$newfield = 'profile_field_'.$field->datatype;
$formfield = new $newfield($field->id, $usernew->id);
$formfield->edit_save_data($usernew);
}
}
}
/**
* Display profile fields.
* @param int $userid
*/
function profile_display_fields($userid) {
global $CFG, $USER, $DB;
if ($categories = $DB->get_records('user_info_category', null, 'sortorder ASC')) {
foreach ($categories as $category) {
if ($fields = $DB->get_records('user_info_field', array('categoryid' => $category->id), 'sortorder ASC')) {
foreach ($fields as $field) {
require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
$newfield = 'profile_field_'.$field->datatype;
$formfield = new $newfield($field->id, $userid);
if ($formfield->is_visible() and !$formfield->is_empty()) {
echo html_writer::tag('dt', format_string($formfield->field->name));
echo html_writer::tag('dd', $formfield->display_data());
}
}
}
}
}
}
/**
* Adds code snippet to a moodle form object for custom profile fields that
* should appear on the signup page
* @param moodleform $mform moodle form object
*/
function profile_signup_fields($mform) {
global $CFG, $DB;
// Only retrieve required custom fields (with category information)
// results are sort by categories, then by fields.
$sql = "SELECT uf.id as fieldid, ic.id as categoryid, ic.name as categoryname, uf.datatype
FROM {user_info_field} uf
JOIN {user_info_category} ic
ON uf.categoryid = ic.id AND uf.signup = 1 AND uf.visible<>0
ORDER BY ic.sortorder ASC, uf.sortorder ASC";
if ( $fields = $DB->get_records_sql($sql)) {
foreach ($fields as $field) {
// Check if we change the categories.
if (!isset($currentcat) || $currentcat != $field->categoryid) {
$currentcat = $field->categoryid;
$mform->addElement('header', 'category_'.$field->categoryid, format_string($field->categoryname));
}
require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
$newfield = 'profile_field_'.$field->datatype;
$formfield = new $newfield($field->fieldid);
$formfield->edit_field($mform);
}
}
}
/**
* Returns an object with the custom profile fields set for the given user
* @param integer $userid
* @param bool $onlyinuserobject True if you only want the ones in $USER.
* @return stdClass
*/
function profile_user_record($userid, $onlyinuserobject = true) {
global $CFG, $DB;
$usercustomfields = new stdClass();
if ($fields = $DB->get_records('user_info_field')) {
foreach ($fields as $field) {
require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
$newfield = 'profile_field_'.$field->datatype;
$formfield = new $newfield($field->id, $userid);
if (!$onlyinuserobject || $formfield->is_user_object_data()) {
$usercustomfields->{$field->shortname} = $formfield->data;
}
}
}
return $usercustomfields;
}
/**
* Obtains a list of all available custom profile fields, indexed by id.
*
* Some profile fields are not included in the user object data (see
* profile_user_record function above). Optionally, you can obtain only those
* fields that are included in the user object.
*
* To be clear, this function returns the available fields, and does not
* return the field values for a particular user.
*
* @param bool $onlyinuserobject True if you only want the ones in $USER
* @return array Array of field objects from database (indexed by id)
* @since Moodle 2.7.1
*/
function profile_get_custom_fields($onlyinuserobject = false) {
global $DB, $CFG;
// Get all the fields.
$fields = $DB->get_records('user_info_field', null, 'id ASC');
// If only doing the user object ones, unset the rest.
if ($onlyinuserobject) {
foreach ($fields as $id => $field) {
require_once($CFG->dirroot . '/user/profile/field/' .
$field->datatype . '/field.class.php');
$newfield = 'profile_field_' . $field->datatype;
$formfield = new $newfield();
if (!$formfield->is_user_object_data()) {
unset($fields[$id]);
}
}
}
return $fields;
}
/**
* Load custom profile fields into user object
*
* Please note originally in 1.9 we were using the custom field names directly,
* but it was causing unexpected collisions when adding new fields to user table,
* so instead we now use 'profile_' prefix.
*
* @param stdClass $user user object
*/
function profile_load_custom_fields($user) {
$user->profile = (array)profile_user_record($user->id);
}
/**
* Trigger a user profile viewed event.
*
* @param stdClass $user user object
* @param stdClass $context context object (course or user)
* @param stdClass $course course object
* @since Moodle 2.9
*/
function profile_view($user, $context, $course = null) {
$eventdata = array(
'objectid' => $user->id,
'relateduserid' => $user->id,
'context' => $context
);
if (!empty($course)) {
$eventdata['courseid'] = $course->id;
$eventdata['other'] = array(
'courseid' => $course->id,
'courseshortname' => $course->shortname,
'coursefullname' => $course->fullname
);
}
$event = \core\event\user_profile_viewed::create($eventdata);
$event->add_record_snapshot('user', $user);
$event->trigger();
}