MDL-32787 user: rule required for custom profile fields applies to all users editing...
[moodle.git] / user / profile / lib.php
CommitLineData
8bdc9cac 1<?php
6b64d3b3 2
3/// Some constants
4
bb6d3d34 5define ('PROFILE_VISIBLE_ALL', '2'); // only visible for users with moodle/user:update capability
6define ('PROFILE_VISIBLE_PRIVATE', '1'); // either we are viewing our own profile or we have moodle/user:update capability
7define ('PROFILE_VISIBLE_NONE', '0'); // only visible for moodle/user:update capability
6b64d3b3 8
334415e9 9
10
d052a813 11/**
a1248ca4 12 * Base class for the customisable profile fields.
d052a813 13 */
6b64d3b3 14class profile_field_base {
15
334415e9 16 /// These 2 variables are really what we're interested in.
17 /// Everything else can be extracted from them
18 var $fieldid;
19 var $userid;
aa6c1ced 20
bb6d3d34 21 var $field;
22 var $inputname;
334415e9 23 var $data;
8bdc9cac 24 var $dataformat;
6b64d3b3 25
b1c70023 26 /**
27 * Constructor method.
28 * @param integer id of the profile from the user_info_field table
29 * @param integer id of the user for whom we are displaying data
30 */
334415e9 31 function profile_field_base($fieldid=0, $userid=0) {
32 global $USER;
b1c70023 33
334415e9 34 $this->set_fieldid($fieldid);
35 $this->set_userid($userid);
36 $this->load_data();
b1c70023 37 }
38
334415e9 39
40/***** The following methods must be overwritten by child classes *****/
41
6b64d3b3 42 /**
334415e9 43 * Abstract method: Adds the profile field to the moodle form class
44 * @param form instance of the moodleform class
6b64d3b3 45 */
c990e4bf 46 function edit_field_add($mform) {
d3248238 47 print_error('mustbeoveride', 'debug', '', 'edit_field_add');
6b64d3b3 48 }
49
aa6c1ced 50
334415e9 51/***** The following methods may be overwritten by child classes *****/
52
53 /**
54 * Display the data for this field
55 */
56 function display_data() {
bf718f50 57 $options = new stdClass();
3212d2f2 58 $options->para = false;
59 return format_text($this->data, FORMAT_MOODLE, $options);
334415e9 60 }
aa6c1ced 61
6b64d3b3 62 /**
334415e9 63 * Print out the form field in the edit profile page
b1c70023 64 * @param object instance of the moodleform class
6b64d3b3 65 * $return boolean
66 */
c990e4bf 67 function edit_field($mform) {
6b64d3b3 68
bb6d3d34 69 if ($this->field->visible != PROFILE_VISIBLE_NONE
ef35441d 70 or has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM))) {
6b64d3b3 71
334415e9 72 $this->edit_field_add($mform);
73 $this->edit_field_set_default($mform);
74 $this->edit_field_set_required($mform);
63466095 75 return true;
76 }
77 return false;
78 }
79
80 /**
81 * Tweaks the edit form
82 * @param object instance of the moodleform class
83 * $return boolean
84 */
c990e4bf 85 function edit_after_data($mform) {
63466095 86
87 if ($this->field->visible != PROFILE_VISIBLE_NONE
88 or has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM))) {
334415e9 89 $this->edit_field_set_locked($mform);
17a99a13 90 return true;
9ac33264 91 }
17a99a13 92 return false;
b1c70023 93 }
6b64d3b3 94
b1c70023 95 /**
bb6d3d34 96 * Saves the data coming from form
97 * @param mixed data coming from the form
98 * @return mixed returns data id if success of db insert/update, false on fail, 0 if not permitted
b1c70023 99 */
334415e9 100 function edit_save_data($usernew) {
5d910388 101 global $DB;
6b64d3b3 102
bb6d3d34 103 if (!isset($usernew->{$this->inputname})) {
1bf65e58 104 // field not present in form, probably locked and invisible - skip it
bb6d3d34 105 return;
106 }
6b64d3b3 107
bf718f50 108 $data = new stdClass();
aa6c1ced 109
8bdc9cac
SH
110 $usernew->{$this->inputname} = $this->edit_save_data_preprocess($usernew->{$this->inputname}, $data);
111
bb6d3d34 112 $data->userid = $usernew->id;
113 $data->fieldid = $this->field->id;
114 $data->data = $usernew->{$this->inputname};
9ac33264 115
5d910388 116 if ($dataid = $DB->get_field('user_info_data', 'id', array('userid'=>$data->userid, 'fieldid'=>$data->fieldid))) {
bb6d3d34 117 $data->id = $dataid;
bf8e93d7 118 $DB->update_record('user_info_data', $data);
bb6d3d34 119 } else {
5d910388 120 $DB->insert_record('user_info_data', $data);
bb6d3d34 121 }
6b64d3b3 122 }
123
9ac33264 124 /**
bb6d3d34 125 * Validate the form field from profile page
126 * @return string contains error message otherwise NULL
127 **/
334415e9 128 function edit_validate_field($usernew) {
6a08c830 129 global $DB;
130
35a261eb 131 $errors = array();
132 /// Check for uniqueness of data if required
133 if ($this->is_unique()) {
af0a06cb
PS
134 $value = (is_array($usernew->{$this->inputname}) and isset($usernew->{$this->inputname}['text'])) ? $usernew->{$this->inputname}['text'] : $usernew->{$this->inputname};
135 $data = $DB->get_records_sql('
136 SELECT id, userid
25f44c72
TH
137 FROM {user_info_data}
138 WHERE fieldid = ?
ffcb8379 139 AND ' . $DB->sql_compare_text('data', 255) . ' = ' . $DB->sql_compare_text('?', 255),
af0a06cb
PS
140 array($this->field->id, $value));
141 if ($data) {
142 $existing = false;
143 foreach ($data as $v) {
144 if ($v->userid == $usernew->id) {
145 $existing = true;
146 break;
147 }
148 }
149 if (!$existing) {
150 $errors[$this->inputname] = get_string('valuealreadyused');
151 }
35a261eb 152 }
153 }
154 return $errors;
9ac33264 155 }
156
9ac33264 157 /**
bb6d3d34 158 * Sets the default data for the field in the form object
159 * @param object instance of the moodleform class
9ac33264 160 */
c990e4bf 161 function edit_field_set_default($mform) {
bb6d3d34 162 if (!empty($default)) {
a5d3b072 163 $mform->setDefault($this->inputname, $this->field->defaultdata);
bb6d3d34 164 }
6b64d3b3 165 }
166
167 /**
bb6d3d34 168 * Sets the required flag for the field in the form object
169 * @param object instance of the moodleform class
6b64d3b3 170 */
c990e4bf 171 function edit_field_set_required($mform) {
28f23328
RT
172 global $USER;
173 if ($this->is_required() && ($this->userid == $USER->id)) {
a5d3b072 174 $mform->addRule($this->inputname, get_string('required'), 'required', null, 'client');
bb6d3d34 175 }
6b64d3b3 176 }
177
178 /**
bb6d3d34 179 * HardFreeze the field if locked.
180 * @param object instance of the moodleform class
6b64d3b3 181 */
c990e4bf 182 function edit_field_set_locked($mform) {
63466095 183 if (!$mform->elementExists($this->inputname)) {
184 return;
185 }
ef35441d 186 if ($this->is_locked() and !has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM))) {
a5d3b072 187 $mform->hardFreeze($this->inputname);
cc444336 188 $mform->setConstant($this->inputname, $this->data);
6b64d3b3 189 }
6b64d3b3 190 }
191
192 /**
193 * Hook for child classess to process the data before it gets saved in database
c990e4bf
PS
194 * @param mixed $data
195 * @param stdClass $datarecord The object that will be used to save the record
6b64d3b3 196 * @return mixed
197 */
c990e4bf 198 function edit_save_data_preprocess($data, $datarecord) {
6b64d3b3 199 return $data;
200 }
6b64d3b3 201
334415e9 202 /**
203 * Loads a user object with data for this field ready for the edit profile
204 * form
205 * @param object a user object
206 */
c990e4bf 207 function edit_load_user_data($user) {
334415e9 208 if ($this->data !== NULL) {
209 $user->{$this->inputname} = $this->data;
210 }
211 }
212
91cddbc4 213 /**
214 * Check if the field data should be loaded into the user object
215 * By default it is, but for field types where the data may be potentially
216 * large, the child class should override this and return false
217 * @return boolean
218 */
219 function is_user_object_data() {
220 return true;
221 }
222
334415e9 223
224/***** The following methods generally should not be overwritten by child classes *****/
aa6c1ced 225
334415e9 226 /**
227 * Accessor method: set the userid for this instance
228 * @param integer id from the user table
229 */
230 function set_userid($userid) {
231 $this->userid = $userid;
232 }
233
234 /**
235 * Accessor method: set the fieldid for this instance
236 * @param integer id from the user_info_field table
237 */
238 function set_fieldid($fieldid) {
239 $this->fieldid = $fieldid;
240 }
241
242 /**
243 * Accessor method: Load the field record and user data associated with the
244 * object's fieldid and userid
245 */
246 function load_data() {
5d910388 247 global $DB;
248
334415e9 249 /// Load the field object
5d910388 250 if (($this->fieldid == 0) or (!($field = $DB->get_record('user_info_field', array('id'=>$this->fieldid))))) {
334415e9 251 $this->field = NULL;
252 $this->inputname = '';
253 } else {
254 $this->field = $field;
255 $this->inputname = 'profile_field_'.$field->shortname;
256 }
257
258 if (!empty($this->field)) {
8bdc9cac
SH
259 if ($data = $DB->get_record('user_info_data', array('userid'=>$this->userid, 'fieldid'=>$this->fieldid), 'data, dataformat')) {
260 $this->data = $data->data;
261 $this->dataformat = $data->dataformat;
334415e9 262 } else {
263 $this->data = $this->field->defaultdata;
8bdc9cac 264 $this->dataformat = FORMAT_HTML;
334415e9 265 }
266 } else {
267 $this->data = NULL;
268 }
269 }
270
271 /**
272 * Check if the field data is visible to the current user
273 * @return boolean
274 */
275 function is_visible() {
276 global $USER;
277
278 switch ($this->field->visible) {
279 case PROFILE_VISIBLE_ALL:
280 return true;
281 case PROFILE_VISIBLE_PRIVATE:
76727901 282 if ($this->userid == $USER->id) {
283 return true;
284 } else {
6d153e67 285 return has_capability('moodle/user:viewalldetails',
286 get_context_instance(CONTEXT_USER, $this->userid));
76727901 287 }
334415e9 288 default:
6d153e67 289 return has_capability('moodle/user:viewalldetails',
290 get_context_instance(CONTEXT_USER, $this->userid));
334415e9 291 }
292 }
293
4a7030e0 294 /**
295 * Check if the field data is considered empty
296 * return boolean
297 */
298 function is_empty() {
299 return ( ($this->data != '0') and empty($this->data));
300 }
301
334415e9 302 /**
303 * Check if the field is required on the edit profile page
304 * @return boolean
305 */
306 function is_required() {
307 return (boolean)$this->field->required;
308 }
309
310 /**
311 * Check if the field is locked on the edit profile page
312 * @return boolean
313 */
314 function is_locked() {
315 return (boolean)$this->field->locked;
316 }
317
62ebb000 318 /**
319 * Check if the field data should be unique
320 * @return boolean
321 */
322 function is_unique() {
501ca377 323 return (boolean)$this->field->forceunique;
62ebb000 324 }
325
326 /**
327 * Check if the field should appear on the signup page
328 * @return boolean
329 */
330 function is_signup_field() {
331 return (boolean)$this->field->signup;
332 }
aa6c1ced 333
62ebb000 334
6b64d3b3 335} /// End of class definition
336
9ac33264 337
338/***** General purpose functions for customisable user profiles *****/
339
c990e4bf 340function profile_load_data($user) {
5d910388 341 global $CFG, $DB;
bb6d3d34 342
5d910388 343 if ($fields = $DB->get_records('user_info_field')) {
bb6d3d34 344 foreach ($fields as $field) {
345 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
346 $newfield = 'profile_field_'.$field->datatype;
334415e9 347 $formfield = new $newfield($field->id, $user->id);
348 $formfield->edit_load_user_data($user);
bb6d3d34 349 }
350 }
351}
352
7240a6ea 353/**
354 * Print out the customisable categories and fields for a users profile
355 * @param object instance of the moodleform class
28f23328 356 * @param int $userid id of user whose profile is being edited.
7240a6ea 357 */
28f23328 358function profile_definition($mform, $userid = 0) {
5d910388 359 global $CFG, $DB;
7240a6ea 360
85576776 361 // if user is "admin" fields are displayed regardless
362 $update = has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM));
363
5d910388 364 if ($categories = $DB->get_records('user_info_category', null, 'sortorder ASC')) {
7240a6ea 365 foreach ($categories as $category) {
5d910388 366 if ($fields = $DB->get_records('user_info_field', array('categoryid'=>$category->id), 'sortorder ASC')) {
aa6c1ced 367
85576776 368 // check first if *any* fields will be displayed
369 $display = false;
7240a6ea 370 foreach ($fields as $field) {
85576776 371 if ($field->visible != PROFILE_VISIBLE_NONE) {
372 $display = true;
17a99a13 373 }
374 }
85576776 375
376 // display the header and the fields
377 if ($display or $update) {
17a99a13 378 $mform->addElement('header', 'category_'.$category->id, format_string($category->name));
85576776 379 foreach ($fields as $field) {
380 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
381 $newfield = 'profile_field_'.$field->datatype;
28f23328 382 $formfield = new $newfield($field->id, $userid);
85576776 383 $formfield->edit_field($mform);
384 }
7240a6ea 385 }
bb6d3d34 386 }
9ac33264 387 }
388 }
9ac33264 389}
390
c990e4bf 391function profile_definition_after_data($mform, $userid) {
5d910388 392 global $CFG, $DB;
63466095 393
394 $userid = ($userid < 0) ? 0 : (int)$userid;
395
5d910388 396 if ($fields = $DB->get_records('user_info_field')) {
bb6d3d34 397 foreach ($fields as $field) {
398 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
399 $newfield = 'profile_field_'.$field->datatype;
63466095 400 $formfield = new $newfield($field->id, $userid);
401 $formfield->edit_after_data($mform);
d052a813 402 }
63466095 403 }
d052a813 404}
405
a78890d5 406function profile_validation($usernew, $files) {
5d910388 407 global $CFG, $DB;
d052a813 408
bb6d3d34 409 $err = array();
5d910388 410 if ($fields = $DB->get_records('user_info_field')) {
bb6d3d34 411 foreach ($fields as $field) {
412 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
413 $newfield = 'profile_field_'.$field->datatype;
334415e9 414 $formfield = new $newfield($field->id, $usernew->id);
a78890d5 415 $err += $formfield->edit_validate_field($usernew, $files);
d052a813 416 }
417 }
bb6d3d34 418 return $err;
d052a813 419}
420
bb6d3d34 421function profile_save_data($usernew) {
5d910388 422 global $CFG, $DB;
d052a813 423
5849cfe2 424 if ($fields = $DB->get_records('user_info_field')) {
bb6d3d34 425 foreach ($fields as $field) {
426 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
427 $newfield = 'profile_field_'.$field->datatype;
334415e9 428 $formfield = new $newfield($field->id, $usernew->id);
429 $formfield->edit_save_data($usernew);
bb6d3d34 430 }
9ac33264 431 }
9ac33264 432}
433
334415e9 434function profile_display_fields($userid) {
5d910388 435 global $CFG, $USER, $DB;
d052a813 436
5d910388 437 if ($categories = $DB->get_records('user_info_category', null, 'sortorder ASC')) {
334415e9 438 foreach ($categories as $category) {
5d910388 439 if ($fields = $DB->get_records('user_info_field', array('categoryid'=>$category->id), 'sortorder ASC')) {
334415e9 440 foreach ($fields as $field) {
441 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
442 $newfield = 'profile_field_'.$field->datatype;
443 $formfield = new $newfield($field->id, $userid);
4a7030e0 444 if ($formfield->is_visible() and !$formfield->is_empty()) {
dc45a43a 445 print_row(format_string($formfield->field->name.':'), $formfield->display_data());
334415e9 446 }
447 }
448 }
449 }
450 }
451}
d052a813 452
831d450e 453/**
454 * Adds code snippet to a moodle form object for custom profile fields that
455 * should appear on the signup page
456 * @param object moodle form object
457 */
c990e4bf 458function profile_signup_fields($mform) {
5d910388 459 global $CFG, $DB;
416afdbb 460
461 //only retrieve required custom fields (with category information)
462 //results are sort by categories, then by fields
463 $sql = "SELECT uf.id as fieldid, ic.id as categoryid, ic.name as categoryname, uf.datatype
351d5250 464 FROM {user_info_field} uf
465 JOIN {user_info_category} ic
aa6c1ced 466 ON uf.categoryid = ic.id AND uf.signup = 1 AND uf.visible<>0
416afdbb 467 ORDER BY ic.sortorder ASC, uf.sortorder ASC";
468
469 if ( $fields = $DB->get_records_sql($sql)) {
831d450e 470 foreach ($fields as $field) {
416afdbb 471 //check if we change the categories
472 if (!isset($currentcat) || $currentcat != $field->categoryid) {
473 $currentcat = $field->categoryid;
474 $mform->addElement('header', 'category_'.$field->categoryid, format_string($field->categoryname));
475 }
831d450e 476 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
477 $newfield = 'profile_field_'.$field->datatype;
416afdbb 478 $formfield = new $newfield($field->fieldid);
831d450e 479 $formfield->edit_field($mform);
480 }
481 }
482}
d052a813 483
66643c0a 484/**
485 * Returns an object with the custom profile fields set for the given user
486 * @param integer userid
487 * @return object
488 */
489function profile_user_record($userid) {
5d910388 490 global $CFG, $DB;
66643c0a 491
bf718f50 492 $usercustomfields = new stdClass();
66643c0a 493
5d910388 494 if ($fields = $DB->get_records('user_info_field')) {
66643c0a 495 foreach ($fields as $field) {
496 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
497 $newfield = 'profile_field_'.$field->datatype;
498 $formfield = new $newfield($field->id, $userid);
a1248ca4
PS
499 if ($formfield->is_user_object_data()) {
500 $usercustomfields->{$field->shortname} = $formfield->data;
501 }
66643c0a 502 }
503 }
504
fb79269b 505 return $usercustomfields;
66643c0a 506}
507
a1248ca4
PS
508/**
509 * Load custom profile fields into user object
510 *
511 * Please note originally in 1.9 we were using the custom field names directly,
512 * but it was causing unexpected collisions when adding new fields to user table,
513 * so instead we now use 'profile_' prefix.
514 *
515 * @param object $user user object
516 * @return void $user object is modified
517 */
c990e4bf 518function profile_load_custom_fields($user) {
a1248ca4
PS
519 $user->profile = (array)profile_user_record($user->id);
520}
66643c0a 521
aa6c1ced 522