MDL-10724 Added the help buttons to the item edit form
[moodle-pu.git] / user / profile / lib.php
blobbb0062776a5aa0396870e4727909d1f27bae6859
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, SITEID))) {
70 $this->edit_field_add($mform);
71 $this->edit_field_set_default($mform);
72 $this->edit_field_set_required($mform);
73 $this->edit_field_set_locked($mform);
77 /**
78 * Saves the data coming from form
79 * @param mixed data coming from the form
80 * @return mixed returns data id if success of db insert/update, false on fail, 0 if not permitted
82 function edit_save_data($usernew) {
84 if (!isset($usernew->{$this->inputname})) {
85 // field not present in form, probably locked and invisible - skip it
86 return;
89 $usernew->{$this->inputname} = $this->edit_save_data_preprocess($usernew->{$this->inputname});
91 $data = new object();
92 $data->userid = $usernew->id;
93 $data->fieldid = $this->field->id;
94 $data->data = $usernew->{$this->inputname};
96 if ($dataid = get_field('user_info_data', 'id', 'userid', $data->userid, 'fieldid', $data->fieldid)) {
97 $data->id = $dataid;
98 if (!update_record('user_info_data', $data)) {
99 error('Error updating custom profile field!');
101 } else {
102 insert_record('user_info_data', $data);
107 * Validate the form field from profile page
108 * @return string contains error message otherwise NULL
110 function edit_validate_field($usernew) {
111 //no errors by default
112 return array();
116 * Sets the default data for the field in the form object
117 * @param object instance of the moodleform class
119 function edit_field_set_default(&$mform) {
120 if (!empty($default)) {
121 $mform->setDefault($this->inputname, $this->field->defaultdata);
126 * Sets the required flag for the field in the form object
127 * @param object instance of the moodleform class
129 function edit_field_set_required(&$mform) {
130 if ($this->is_required() and !has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
131 $mform->addRule($this->inputname, get_string('required'), 'required', null, 'client');
136 * HardFreeze the field if locked.
137 * @param object instance of the moodleform class
139 function edit_field_set_locked(&$mform) {
140 if ($this->is_locked() and !has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
141 $mform->hardFreeze($this->inputname);
146 * Hook for child classess to process the data before it gets saved in database
147 * @param mixed
148 * @return mixed
150 function edit_save_data_preprocess($data) {
151 return $data;
155 * Loads a user object with data for this field ready for the edit profile
156 * form
157 * @param object a user object
159 function edit_load_user_data(&$user) {
160 if ($this->data !== NULL) {
161 $user->{$this->inputname} = $this->data;
166 /***** The following methods generally should not be overwritten by child classes *****/
169 * Accessor method: set the userid for this instance
170 * @param integer id from the user table
172 function set_userid($userid) {
173 $this->userid = $userid;
177 * Accessor method: set the fieldid for this instance
178 * @param integer id from the user_info_field table
180 function set_fieldid($fieldid) {
181 $this->fieldid = $fieldid;
185 * Accessor method: Load the field record and user data associated with the
186 * object's fieldid and userid
188 function load_data() {
189 /// Load the field object
190 if (($this->fieldid == 0) or (!($field = get_record('user_info_field', 'id', $this->fieldid)))) {
191 $this->field = NULL;
192 $this->inputname = '';
193 } else {
194 $this->field = $field;
195 $this->inputname = 'profile_field_'.$field->shortname;
198 if (!empty($this->field)) {
199 if ($datafield = get_field('user_info_data', 'data', 'userid', $this->userid, 'fieldid', $this->fieldid)) {
200 $this->data = $datafield;
201 } else {
202 $this->data = $this->field->defaultdata;
204 } else {
205 $this->data = NULL;
210 * Check if the field data is visible to the current user
211 * @return boolean
213 function is_visible() {
214 global $USER;
216 switch ($this->field->visible) {
217 case PROFILE_VISIBLE_ALL:
218 return true;
219 case PROFILE_VISIBLE_PRIVATE:
220 return ($this->userid == $USER->id);
221 default:
222 return has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM, SITEID));
227 * Check if the field data is considered empty
228 * return boolean
230 function is_empty() {
231 return ( ($this->data != '0') and empty($this->data));
235 * Check if the field is required on the edit profile page
236 * @return boolean
238 function is_required() {
239 return (boolean)$this->field->required;
243 * Check if the field is locked on the edit profile page
244 * @return boolean
246 function is_locked() {
247 return (boolean)$this->field->locked;
250 } /// End of class definition
253 /***** General purpose functions for customisable user profiles *****/
255 function profile_load_data(&$user) {
256 global $CFG;
258 if ($fields = get_records_select('user_info_field')) {
259 foreach ($fields as $field) {
260 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
261 $newfield = 'profile_field_'.$field->datatype;
262 $formfield = new $newfield($field->id, $user->id);
263 $formfield->edit_load_user_data($user);
269 * Print out the customisable categories and fields for a users profile
270 * @param object instance of the moodleform class
272 function profile_definition(&$mform) {
273 global $CFG;
275 if ($categories = get_records_select('user_info_category', '', 'sortorder ASC')) {
276 foreach ($categories as $category) {
277 if ($fields = get_records_select('user_info_field', "categoryid=$category->id", 'sortorder ASC')) {
278 $mform->addElement('header', 'category_'.$category->id, format_string($category->name));
279 foreach ($fields as $field) {
280 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
281 $newfield = 'profile_field_'.$field->datatype;
282 $formfield = new $newfield($field->id);
283 $formfield->edit_field($mform);
291 function profile_definition_after_data(&$mform) {
292 global $CFG;
294 if ($fields = get_records('user_info_field')) {
295 foreach ($fields as $field) {
296 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
297 $newfield = 'profile_field_'.$field->datatype;
298 $formfield = new $newfield($field->id);
299 //TODO add: method into field class
305 function profile_validation($usernew) {
306 global $CFG;
308 $err = array();
309 if ($fields = get_records('user_info_field')) {
310 foreach ($fields as $field) {
311 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
312 $newfield = 'profile_field_'.$field->datatype;
313 $formfield = new $newfield($field->id, $usernew->id);
314 $err += $formfield->edit_validate_field($usernew);
317 return $err;
320 function profile_save_data($usernew) {
321 global $CFG;
323 if ($fields = get_records_select('user_info_field')) {
324 foreach ($fields as $field) {
325 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
326 $newfield = 'profile_field_'.$field->datatype;
327 $formfield = new $newfield($field->id, $usernew->id);
328 $formfield->edit_save_data($usernew);
333 function profile_display_fields($userid) {
334 global $CFG, $USER;
336 if ($categories = get_records_select('user_info_category', '', 'sortorder ASC')) {
337 foreach ($categories as $category) {
338 if ($fields = get_records_select('user_info_field', "categoryid=$category->id", 'sortorder ASC')) {
339 foreach ($fields as $field) {
340 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
341 $newfield = 'profile_field_'.$field->datatype;
342 $formfield = new $newfield($field->id, $userid);
343 if ($formfield->is_visible() and !$formfield->is_empty()) {
344 print_row(s($formfield->field->name.':'), $formfield->display_data());