MDL-20830 workshop requires sesskey when deleting submission
[moodle.git] / user / profile / lib.php
blob0dc2700af6971e20527cd8b6912596538568b251
1 <?php //$Id$
3 /// Some constants
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
11 /**
12 * Base class for the cusomisable 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
18 var $fieldid;
19 var $userid;
21 var $field;
22 var $inputname;
23 var $data;
25 /**
26 * Constructor method.
27 * @param integer id of the profile from the user_info_field table
28 * @param integer id of the user for whom we are displaying data
30 function profile_field_base($fieldid=0, $userid=0) {
31 global $USER;
33 $this->set_fieldid($fieldid);
34 $this->set_userid($userid);
35 $this->load_data();
39 /***** The following methods must be overwritten by child classes *****/
41 /**
42 * Abstract method: Adds the profile field to the moodle form class
43 * @param form instance of the moodleform class
45 function edit_field_add(&$mform) {
46 error('This abstract method must be overriden');
50 /***** The following methods may be overwritten by child classes *****/
52 /**
53 * Display the data for this field
55 function display_data() {
56 $options->para = false;
57 return format_text($this->data, FORMAT_MOODLE, $options);
60 /**
61 * Print out the form field in the edit profile page
62 * @param object instance of the moodleform class
63 * $return boolean
65 function edit_field(&$mform) {
67 if ($this->field->visible != PROFILE_VISIBLE_NONE
68 or has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM))) {
70 $this->edit_field_add($mform);
71 $this->edit_field_set_default($mform);
72 $this->edit_field_set_required($mform);
73 return true;
75 return false;
78 /**
79 * Tweaks the edit form
80 * @param object instance of the moodleform class
81 * $return boolean
83 function edit_after_data(&$mform) {
85 if ($this->field->visible != PROFILE_VISIBLE_NONE
86 or has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM))) {
87 $this->edit_field_set_locked($mform);
88 return true;
90 return false;
93 /**
94 * Saves the data coming from form
95 * @param mixed data coming from the form
96 * @return mixed returns data id if success of db insert/update, false on fail, 0 if not permitted
98 function edit_save_data($usernew) {
100 if (!isset($usernew->{$this->inputname})) {
101 // field not present in form, probably locked and invisible - skip it
102 return;
105 $usernew->{$this->inputname} = $this->edit_save_data_preprocess($usernew->{$this->inputname});
107 $data = new object();
108 $data->userid = $usernew->id;
109 $data->fieldid = $this->field->id;
110 $data->data = $usernew->{$this->inputname};
112 if ($dataid = get_field('user_info_data', 'id', 'userid', $data->userid, 'fieldid', $data->fieldid)) {
113 $data->id = $dataid;
114 if (!update_record('user_info_data', $data)) {
115 error('Error updating custom profile field!');
117 } else {
118 insert_record('user_info_data', $data);
123 * Validate the form field from profile page
124 * @return string contains error message otherwise NULL
126 function edit_validate_field($usernew) {
127 $errors = array();
128 /// Check for uniqueness of data if required
129 if ($this->is_unique()) {
130 if ($userid = get_field('user_info_data', 'userid', 'fieldid', $this->field->id, 'data', $usernew->{$this->inputname})) {
131 if ($userid != $usernew->id) {
132 $errors["{$this->inputname}"] = get_string('valuealreadyused');
136 return $errors;
140 * Sets the default data for the field in the form object
141 * @param object instance of the moodleform class
143 function edit_field_set_default(&$mform) {
144 if (!empty($default)) {
145 $mform->setDefault($this->inputname, $this->field->defaultdata);
150 * Sets the required flag for the field in the form object
151 * @param object instance of the moodleform class
153 function edit_field_set_required(&$mform) {
154 if ($this->is_required() and !has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM))) {
155 $mform->addRule($this->inputname, get_string('required'), 'required', null, 'client');
160 * HardFreeze the field if locked.
161 * @param object instance of the moodleform class
163 function edit_field_set_locked(&$mform) {
164 if (!$mform->elementExists($this->inputname)) {
165 return;
167 if ($this->is_locked() and !has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM))) {
168 $mform->hardFreeze($this->inputname);
169 $mform->setConstant($this->inputname, $this->data);
174 * Hook for child classess to process the data before it gets saved in database
175 * @param mixed
176 * @return mixed
178 function edit_save_data_preprocess($data) {
179 return $data;
183 * Loads a user object with data for this field ready for the edit profile
184 * form
185 * @param object a user object
187 function edit_load_user_data(&$user) {
188 if ($this->data !== NULL) {
189 $user->{$this->inputname} = $this->data;
194 * Check if the field data should be loaded into the user object
195 * By default it is, but for field types where the data may be potentially
196 * large, the child class should override this and return false
197 * @return boolean
199 function is_user_object_data() {
200 return true;
204 /***** The following methods generally should not be overwritten by child classes *****/
207 * Accessor method: set the userid for this instance
208 * @param integer id from the user table
210 function set_userid($userid) {
211 $this->userid = $userid;
215 * Accessor method: set the fieldid for this instance
216 * @param integer id from the user_info_field table
218 function set_fieldid($fieldid) {
219 $this->fieldid = $fieldid;
223 * Accessor method: Load the field record and user data associated with the
224 * object's fieldid and userid
226 function load_data() {
227 /// Load the field object
228 if (($this->fieldid == 0) or (!($field = get_record('user_info_field', 'id', $this->fieldid)))) {
229 $this->field = NULL;
230 $this->inputname = '';
231 } else {
232 $this->field = $field;
233 $this->inputname = 'profile_field_'.$field->shortname;
236 if (!empty($this->field)) {
237 if ($datafield = get_field('user_info_data', 'data', 'userid', $this->userid, 'fieldid', $this->fieldid)) {
238 $this->data = $datafield;
239 } else {
240 $this->data = $this->field->defaultdata;
242 } else {
243 $this->data = NULL;
248 * Check if the field data is visible to the current user
249 * @return boolean
251 function is_visible() {
252 global $USER;
254 switch ($this->field->visible) {
255 case PROFILE_VISIBLE_ALL:
256 return true;
257 case PROFILE_VISIBLE_PRIVATE:
258 if ($this->userid == $USER->id) {
259 return true;
260 } else {
261 return has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM));
263 default:
264 return has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM));
269 * Check if the field data is considered empty
270 * return boolean
272 function is_empty() {
273 return ( ($this->data != '0') and empty($this->data));
277 * Check if the field is required on the edit profile page
278 * @return boolean
280 function is_required() {
281 return (boolean)$this->field->required;
285 * Check if the field is locked on the edit profile page
286 * @return boolean
288 function is_locked() {
289 return (boolean)$this->field->locked;
293 * Check if the field data should be unique
294 * @return boolean
296 function is_unique() {
297 return (boolean)$this->field->forceunique;
301 * Check if the field should appear on the signup page
302 * @return boolean
304 function is_signup_field() {
305 return (boolean)$this->field->signup;
309 } /// End of class definition
312 /***** General purpose functions for customisable user profiles *****/
314 function profile_load_data(&$user) {
315 global $CFG;
317 if ($fields = get_records_select('user_info_field')) {
318 foreach ($fields as $field) {
319 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
320 $newfield = 'profile_field_'.$field->datatype;
321 $formfield = new $newfield($field->id, $user->id);
322 $formfield->edit_load_user_data($user);
328 * Print out the customisable categories and fields for a users profile
329 * @param object instance of the moodleform class
331 function profile_definition(&$mform) {
332 global $CFG;
334 // if user is "admin" fields are displayed regardless
335 $update = has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM));
337 if ($categories = get_records_select('user_info_category', '', 'sortorder ASC')) {
338 foreach ($categories as $category) {
339 if ($fields = get_records_select('user_info_field', "categoryid=$category->id", 'sortorder ASC')) {
341 // check first if *any* fields will be displayed
342 $display = false;
343 foreach ($fields as $field) {
344 if ($field->visible != PROFILE_VISIBLE_NONE) {
345 $display = true;
349 // display the header and the fields
350 if ($display or $update) {
351 $mform->addElement('header', 'category_'.$category->id, format_string($category->name));
352 foreach ($fields as $field) {
353 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
354 $newfield = 'profile_field_'.$field->datatype;
355 $formfield = new $newfield($field->id);
356 $formfield->edit_field($mform);
364 function profile_definition_after_data(&$mform, $userid) {
365 global $CFG;
367 $userid = ($userid < 0) ? 0 : (int)$userid;
369 if ($fields = get_records('user_info_field')) {
370 foreach ($fields as $field) {
371 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
372 $newfield = 'profile_field_'.$field->datatype;
373 $formfield = new $newfield($field->id, $userid);
374 $formfield->edit_after_data($mform);
379 function profile_validation($usernew, $files) {
380 global $CFG;
382 $err = array();
383 if ($fields = get_records('user_info_field')) {
384 foreach ($fields as $field) {
385 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
386 $newfield = 'profile_field_'.$field->datatype;
387 $formfield = new $newfield($field->id, $usernew->id);
388 $err += $formfield->edit_validate_field($usernew, $files);
391 return $err;
394 function profile_save_data($usernew) {
395 global $CFG;
397 if ($fields = get_records_select('user_info_field')) {
398 foreach ($fields as $field) {
399 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
400 $newfield = 'profile_field_'.$field->datatype;
401 $formfield = new $newfield($field->id, $usernew->id);
402 $formfield->edit_save_data($usernew);
407 function profile_display_fields($userid) {
408 global $CFG, $USER;
410 if ($categories = get_records_select('user_info_category', '', 'sortorder ASC')) {
411 foreach ($categories as $category) {
412 if ($fields = get_records_select('user_info_field', "categoryid=$category->id", 'sortorder ASC')) {
413 foreach ($fields as $field) {
414 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
415 $newfield = 'profile_field_'.$field->datatype;
416 $formfield = new $newfield($field->id, $userid);
417 if ($formfield->is_visible() and !$formfield->is_empty()) {
418 print_row(s($formfield->field->name.':'), $formfield->display_data());
427 * Adds code snippet to a moodle form object for custom profile fields that
428 * should appear on the signup page
429 * @param object moodle form object
431 function profile_signup_fields(&$mform) {
432 global $CFG;
434 //only retrieve required custom fields (with category information)
435 //results are sort by categories, then by fields
436 $sql = "SELECT uf.id as fieldid, ic.id as categoryid, ic.name as categoryname, uf.datatype
437 FROM ".$CFG->prefix."user_info_field uf
438 JOIN ".$CFG->prefix."user_info_category ic
439 ON uf.categoryid = ic.id AND uf.signup = 1 AND uf.visible<>0
440 ORDER BY ic.sortorder ASC, uf.sortorder ASC";
442 if ( $fields = get_records_sql($sql)) {
443 foreach ($fields as $field) {
444 //check if we change the categories
445 if (!isset($currentcat) || $currentcat != $field->categoryid) {
446 $currentcat = $field->categoryid;
447 $mform->addElement('header', 'category_'.$field->categoryid, format_string($field->categoryname));
449 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
450 $newfield = 'profile_field_'.$field->datatype;
451 $formfield = new $newfield($field->fieldid);
452 $formfield->edit_field($mform);
458 * Returns an object with the custom profile fields set for the given user
459 * @param integer userid
460 * @return object
462 function profile_user_record($userid) {
463 global $CFG;
465 $user = new object();
467 if ($fields = get_records_select('user_info_field')) {
468 foreach ($fields as $field) {
469 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
470 $newfield = 'profile_field_'.$field->datatype;
471 $formfield = new $newfield($field->id, $userid);
472 if ($formfield->is_user_object_data()) $user->{$field->shortname} = $formfield->data;
476 return $user;