5 define ('PROFILE_VISIBLE_ALL', '2'); // only visible for users with moodle/user:update capability
6 define ('PROFILE_VISIBLE_PRIVATE', '1'); // either we are viewing our own profile or we have moodle/user:update capability
7 define ('PROFILE_VISIBLE_NONE', '0'); // only visible for moodle/user:update capability
12 * Base class for the customisable profile fields.
14 class profile_field_base {
16 /// These 2 variables are really what we're interested in.
17 /// Everything else can be extracted from them
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
31 function profile_field_base($fieldid=0, $userid=0) {
34 $this->set_fieldid($fieldid);
35 $this->set_userid($userid);
40 /***** The following methods must be overwritten by child classes *****/
43 * Abstract method: Adds the profile field to the moodle form class
44 * @param form instance of the moodleform class
46 function edit_field_add(&$mform) {
47 print_error('mustbeoveride', 'debug', '', 'edit_field_add');
51 /***** The following methods may be overwritten by child classes *****/
54 * Display the data for this field
56 function display_data() {
57 $options = new stdClass();
58 $options->para = false;
59 return format_text($this->data, FORMAT_MOODLE, $options);
63 * Print out the form field in the edit profile page
64 * @param object instance of the moodleform class
67 function edit_field(&$mform) {
69 if ($this->field->visible != PROFILE_VISIBLE_NONE
70 or has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM))) {
72 $this->edit_field_add($mform);
73 $this->edit_field_set_default($mform);
74 $this->edit_field_set_required($mform);
81 * Tweaks the edit form
82 * @param object instance of the moodleform class
85 function edit_after_data(&$mform) {
87 if ($this->field->visible != PROFILE_VISIBLE_NONE
88 or has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM))) {
89 $this->edit_field_set_locked($mform);
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
100 function edit_save_data($usernew) {
103 if (!isset($usernew->{$this->inputname})) {
104 // field not present in form, probably locked and invisible - skip it
108 $data = new stdClass();
110 $usernew->{$this->inputname} = $this->edit_save_data_preprocess($usernew->{$this->inputname}, $data);
112 $data->userid = $usernew->id;
113 $data->fieldid = $this->field->id;
114 $data->data = $usernew->{$this->inputname};
116 if ($dataid = $DB->get_field('user_info_data', 'id', array('userid'=>$data->userid, 'fieldid'=>$data->fieldid))) {
118 $DB->update_record('user_info_data', $data);
120 $DB->insert_record('user_info_data', $data);
125 * Validate the form field from profile page
126 * @return string contains error message otherwise NULL
128 function edit_validate_field($usernew) {
132 /// Check for uniqueness of data if required
133 if ($this->is_unique()) {
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('
137 FROM {user_info_data}
139 AND ' . $DB->sql_compare_text('data', 255) . ' = ' . $DB->sql_compare_text('?', 255),
140 array($this->field->id, $value));
143 foreach ($data as $v) {
144 if ($v->userid == $usernew->id) {
150 $errors[$this->inputname] = get_string('valuealreadyused');
158 * Sets the default data for the field in the form object
159 * @param object instance of the moodleform class
161 function edit_field_set_default(&$mform) {
162 if (!empty($default)) {
163 $mform->setDefault($this->inputname, $this->field->defaultdata);
168 * Sets the required flag for the field in the form object
169 * @param object instance of the moodleform class
171 function edit_field_set_required(&$mform) {
172 if ($this->is_required() and !has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM))) {
173 $mform->addRule($this->inputname, get_string('required'), 'required', null, 'client');
178 * HardFreeze the field if locked.
179 * @param object instance of the moodleform class
181 function edit_field_set_locked(&$mform) {
182 if (!$mform->elementExists($this->inputname)) {
185 if ($this->is_locked() and !has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM))) {
186 $mform->hardFreeze($this->inputname);
187 $mform->setConstant($this->inputname, $this->data);
192 * Hook for child classess to process the data before it gets saved in database
194 * @param stdClass The object that will be used to save the record
197 function edit_save_data_preprocess($data, &$datarecord) {
202 * Loads a user object with data for this field ready for the edit profile
204 * @param object a user object
206 function edit_load_user_data(&$user) {
207 if ($this->data !== NULL) {
208 $user->{$this->inputname} = $this->data;
213 * Check if the field data should be loaded into the user object
214 * By default it is, but for field types where the data may be potentially
215 * large, the child class should override this and return false
218 function is_user_object_data() {
223 /***** The following methods generally should not be overwritten by child classes *****/
226 * Accessor method: set the userid for this instance
227 * @param integer id from the user table
229 function set_userid($userid) {
230 $this->userid = $userid;
234 * Accessor method: set the fieldid for this instance
235 * @param integer id from the user_info_field table
237 function set_fieldid($fieldid) {
238 $this->fieldid = $fieldid;
242 * Accessor method: Load the field record and user data associated with the
243 * object's fieldid and userid
245 function load_data() {
248 /// Load the field object
249 if (($this->fieldid == 0) or (!($field = $DB->get_record('user_info_field', array('id'=>$this->fieldid))))) {
251 $this->inputname = '';
253 $this->field = $field;
254 $this->inputname = 'profile_field_'.$field->shortname;
257 if (!empty($this->field)) {
258 if ($data = $DB->get_record('user_info_data', array('userid'=>$this->userid, 'fieldid'=>$this->fieldid), 'data, dataformat')) {
259 $this->data = $data->data;
260 $this->dataformat = $data->dataformat;
262 $this->data = $this->field->defaultdata;
263 $this->dataformat = FORMAT_HTML;
271 * Check if the field data is visible to the current user
274 function is_visible() {
277 switch ($this->field->visible) {
278 case PROFILE_VISIBLE_ALL:
280 case PROFILE_VISIBLE_PRIVATE:
281 if ($this->userid == $USER->id) {
284 return has_capability('moodle/user:viewalldetails',
285 get_context_instance(CONTEXT_USER, $this->userid));
288 return has_capability('moodle/user:viewalldetails',
289 get_context_instance(CONTEXT_USER, $this->userid));
294 * Check if the field data is considered empty
297 function is_empty() {
298 return ( ($this->data != '0') and empty($this->data));
302 * Check if the field is required on the edit profile page
305 function is_required() {
306 return (boolean)$this->field->required;
310 * Check if the field is locked on the edit profile page
313 function is_locked() {
314 return (boolean)$this->field->locked;
318 * Check if the field data should be unique
321 function is_unique() {
322 return (boolean)$this->field->forceunique;
326 * Check if the field should appear on the signup page
329 function is_signup_field() {
330 return (boolean)$this->field->signup;
334 } /// End of class definition
337 /***** General purpose functions for customisable user profiles *****/
339 function profile_load_data(&$user) {
342 if ($fields = $DB->get_records('user_info_field')) {
343 foreach ($fields as $field) {
344 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
345 $newfield = 'profile_field_'.$field->datatype;
346 $formfield = new $newfield($field->id, $user->id);
347 $formfield->edit_load_user_data($user);
353 * Print out the customisable categories and fields for a users profile
354 * @param object instance of the moodleform class
356 function profile_definition(&$mform) {
359 // if user is "admin" fields are displayed regardless
360 $update = has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM));
362 if ($categories = $DB->get_records('user_info_category', null, 'sortorder ASC')) {
363 foreach ($categories as $category) {
364 if ($fields = $DB->get_records('user_info_field', array('categoryid'=>$category->id), 'sortorder ASC')) {
366 // check first if *any* fields will be displayed
368 foreach ($fields as $field) {
369 if ($field->visible != PROFILE_VISIBLE_NONE) {
374 // display the header and the fields
375 if ($display or $update) {
376 $mform->addElement('header', 'category_'.$category->id, format_string($category->name));
377 foreach ($fields as $field) {
378 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
379 $newfield = 'profile_field_'.$field->datatype;
380 $formfield = new $newfield($field->id);
381 $formfield->edit_field($mform);
389 function profile_definition_after_data(&$mform, $userid) {
392 $userid = ($userid < 0) ? 0 : (int)$userid;
394 if ($fields = $DB->get_records('user_info_field')) {
395 foreach ($fields as $field) {
396 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
397 $newfield = 'profile_field_'.$field->datatype;
398 $formfield = new $newfield($field->id, $userid);
399 $formfield->edit_after_data($mform);
404 function profile_validation($usernew, $files) {
408 if ($fields = $DB->get_records('user_info_field')) {
409 foreach ($fields as $field) {
410 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
411 $newfield = 'profile_field_'.$field->datatype;
412 $formfield = new $newfield($field->id, $usernew->id);
413 $err += $formfield->edit_validate_field($usernew, $files);
419 function profile_save_data($usernew) {
422 if ($fields = $DB->get_records('user_info_field')) {
423 foreach ($fields as $field) {
424 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
425 $newfield = 'profile_field_'.$field->datatype;
426 $formfield = new $newfield($field->id, $usernew->id);
427 $formfield->edit_save_data($usernew);
432 function profile_display_fields($userid) {
433 global $CFG, $USER, $DB;
435 if ($categories = $DB->get_records('user_info_category', null, 'sortorder ASC')) {
436 foreach ($categories as $category) {
437 if ($fields = $DB->get_records('user_info_field', array('categoryid'=>$category->id), 'sortorder ASC')) {
438 foreach ($fields as $field) {
439 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
440 $newfield = 'profile_field_'.$field->datatype;
441 $formfield = new $newfield($field->id, $userid);
442 if ($formfield->is_visible() and !$formfield->is_empty()) {
443 print_row(format_string($formfield->field->name.':'), $formfield->display_data());
452 * Adds code snippet to a moodle form object for custom profile fields that
453 * should appear on the signup page
454 * @param object moodle form object
456 function profile_signup_fields(&$mform) {
459 //only retrieve required custom fields (with category information)
460 //results are sort by categories, then by fields
461 $sql = "SELECT uf.id as fieldid, ic.id as categoryid, ic.name as categoryname, uf.datatype
462 FROM {user_info_field} uf
463 JOIN {user_info_category} ic
464 ON uf.categoryid = ic.id AND uf.signup = 1 AND uf.visible<>0
465 ORDER BY ic.sortorder ASC, uf.sortorder ASC";
467 if ( $fields = $DB->get_records_sql($sql)) {
468 foreach ($fields as $field) {
469 //check if we change the categories
470 if (!isset($currentcat) || $currentcat != $field->categoryid) {
471 $currentcat = $field->categoryid;
472 $mform->addElement('header', 'category_'.$field->categoryid, format_string($field->categoryname));
474 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
475 $newfield = 'profile_field_'.$field->datatype;
476 $formfield = new $newfield($field->fieldid);
477 $formfield->edit_field($mform);
483 * Returns an object with the custom profile fields set for the given user
484 * @param integer userid
487 function profile_user_record($userid) {
490 $usercustomfields = new stdClass();
492 if ($fields = $DB->get_records('user_info_field')) {
493 foreach ($fields as $field) {
494 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
495 $newfield = 'profile_field_'.$field->datatype;
496 $formfield = new $newfield($field->id, $userid);
497 if ($formfield->is_user_object_data()) {
498 $usercustomfields->{$field->shortname} = $formfield->data;
503 return $usercustomfields;
507 * Load custom profile fields into user object
509 * Please note originally in 1.9 we were using the custom field names directly,
510 * but it was causing unexpected collisions when adding new fields to user table,
511 * so instead we now use 'profile_' prefix.
513 * @param object $user user object
514 * @return void $user object is modified
516 function profile_load_custom_fields(&$user) {
517 $user->profile = (array)profile_user_record($user->id);