MDL-14460, fix get_context_instance, merged from MOODLE_19_STABLE, see tracker.
[moodle.git] / user / profile / lib.php
CommitLineData
6b64d3b3 1<?php //$Id$
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/**
12 * Base class for the cusomisable profile fields.
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;
20
bb6d3d34 21 var $field;
22 var $inputname;
334415e9 23 var $data;
6b64d3b3 24
b1c70023 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
29 */
334415e9 30 function profile_field_base($fieldid=0, $userid=0) {
31 global $USER;
b1c70023 32
334415e9 33 $this->set_fieldid($fieldid);
34 $this->set_userid($userid);
35 $this->load_data();
b1c70023 36 }
37
334415e9 38
39/***** The following methods must be overwritten by child classes *****/
40
6b64d3b3 41 /**
334415e9 42 * Abstract method: Adds the profile field to the moodle form class
43 * @param form instance of the moodleform class
6b64d3b3 44 */
334415e9 45 function edit_field_add(&$mform) {
5a2a5331 46 print_error('This abstract method must be overriden');
6b64d3b3 47 }
48
334415e9 49
50/***** The following methods may be overwritten by child classes *****/
51
52 /**
53 * Display the data for this field
54 */
55 function display_data() {
3212d2f2 56 $options->para = false;
57 return format_text($this->data, FORMAT_MOODLE, $options);
334415e9 58 }
59
6b64d3b3 60 /**
334415e9 61 * Print out the form field in the edit profile page
b1c70023 62 * @param object instance of the moodleform class
6b64d3b3 63 * $return boolean
64 */
334415e9 65 function edit_field(&$mform) {
6b64d3b3 66
bb6d3d34 67 if ($this->field->visible != PROFILE_VISIBLE_NONE
ef35441d 68 or has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM))) {
6b64d3b3 69
334415e9 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);
9ac33264 74 }
b1c70023 75 }
6b64d3b3 76
b1c70023 77 /**
bb6d3d34 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
b1c70023 81 */
334415e9 82 function edit_save_data($usernew) {
6b64d3b3 83
bb6d3d34 84 if (!isset($usernew->{$this->inputname})) {
1bf65e58 85 // field not present in form, probably locked and invisible - skip it
bb6d3d34 86 return;
87 }
334415e9 88
89 $usernew->{$this->inputname} = $this->edit_save_data_preprocess($usernew->{$this->inputname});
6b64d3b3 90
bb6d3d34 91 $data = new object();
92 $data->userid = $usernew->id;
93 $data->fieldid = $this->field->id;
94 $data->data = $usernew->{$this->inputname};
9ac33264 95
bb6d3d34 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)) {
5a2a5331 99 print_error('Error updating custom profile field!');
bb6d3d34 100 }
101 } else {
102 insert_record('user_info_data', $data);
103 }
6b64d3b3 104 }
105
9ac33264 106 /**
bb6d3d34 107 * Validate the form field from profile page
108 * @return string contains error message otherwise NULL
109 **/
334415e9 110 function edit_validate_field($usernew) {
bb6d3d34 111 //no errors by default
112 return array();
9ac33264 113 }
114
9ac33264 115 /**
bb6d3d34 116 * Sets the default data for the field in the form object
117 * @param object instance of the moodleform class
9ac33264 118 */
334415e9 119 function edit_field_set_default(&$mform) {
bb6d3d34 120 if (!empty($default)) {
a5d3b072 121 $mform->setDefault($this->inputname, $this->field->defaultdata);
bb6d3d34 122 }
6b64d3b3 123 }
124
125 /**
bb6d3d34 126 * Sets the required flag for the field in the form object
127 * @param object instance of the moodleform class
6b64d3b3 128 */
334415e9 129 function edit_field_set_required(&$mform) {
ef35441d 130 if ($this->is_required() and !has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM))) {
a5d3b072 131 $mform->addRule($this->inputname, get_string('required'), 'required', null, 'client');
bb6d3d34 132 }
6b64d3b3 133 }
134
135 /**
bb6d3d34 136 * HardFreeze the field if locked.
137 * @param object instance of the moodleform class
6b64d3b3 138 */
334415e9 139 function edit_field_set_locked(&$mform) {
ef35441d 140 if ($this->is_locked() and !has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM))) {
a5d3b072 141 $mform->hardFreeze($this->inputname);
6b64d3b3 142 }
6b64d3b3 143 }
144
145 /**
146 * Hook for child classess to process the data before it gets saved in database
147 * @param mixed
148 * @return mixed
149 */
334415e9 150 function edit_save_data_preprocess($data) {
6b64d3b3 151 return $data;
152 }
6b64d3b3 153
334415e9 154 /**
155 * Loads a user object with data for this field ready for the edit profile
156 * form
157 * @param object a user object
158 */
159 function edit_load_user_data(&$user) {
160 if ($this->data !== NULL) {
161 $user->{$this->inputname} = $this->data;
162 }
163 }
164
91cddbc4 165 /**
166 * Check if the field data should be loaded into the user object
167 * By default it is, but for field types where the data may be potentially
168 * large, the child class should override this and return false
169 * @return boolean
170 */
171 function is_user_object_data() {
172 return true;
173 }
174
334415e9 175
176/***** The following methods generally should not be overwritten by child classes *****/
177
178 /**
179 * Accessor method: set the userid for this instance
180 * @param integer id from the user table
181 */
182 function set_userid($userid) {
183 $this->userid = $userid;
184 }
185
186 /**
187 * Accessor method: set the fieldid for this instance
188 * @param integer id from the user_info_field table
189 */
190 function set_fieldid($fieldid) {
191 $this->fieldid = $fieldid;
192 }
193
194 /**
195 * Accessor method: Load the field record and user data associated with the
196 * object's fieldid and userid
197 */
198 function load_data() {
199 /// Load the field object
200 if (($this->fieldid == 0) or (!($field = get_record('user_info_field', 'id', $this->fieldid)))) {
201 $this->field = NULL;
202 $this->inputname = '';
203 } else {
204 $this->field = $field;
205 $this->inputname = 'profile_field_'.$field->shortname;
206 }
207
208 if (!empty($this->field)) {
209 if ($datafield = get_field('user_info_data', 'data', 'userid', $this->userid, 'fieldid', $this->fieldid)) {
210 $this->data = $datafield;
211 } else {
212 $this->data = $this->field->defaultdata;
213 }
214 } else {
215 $this->data = NULL;
216 }
217 }
218
219 /**
220 * Check if the field data is visible to the current user
221 * @return boolean
222 */
223 function is_visible() {
224 global $USER;
225
226 switch ($this->field->visible) {
227 case PROFILE_VISIBLE_ALL:
228 return true;
229 case PROFILE_VISIBLE_PRIVATE:
230 return ($this->userid == $USER->id);
231 default:
ef35441d 232 return has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM));
334415e9 233 }
234 }
235
4a7030e0 236 /**
237 * Check if the field data is considered empty
238 * return boolean
239 */
240 function is_empty() {
241 return ( ($this->data != '0') and empty($this->data));
242 }
243
334415e9 244 /**
245 * Check if the field is required on the edit profile page
246 * @return boolean
247 */
248 function is_required() {
249 return (boolean)$this->field->required;
250 }
251
252 /**
253 * Check if the field is locked on the edit profile page
254 * @return boolean
255 */
256 function is_locked() {
257 return (boolean)$this->field->locked;
258 }
259
62ebb000 260 /**
261 * Check if the field data should be unique
262 * @return boolean
263 */
264 function is_unique() {
265 return (boolean)$tihs->field->forceunique;
266 }
267
268 /**
269 * Check if the field should appear on the signup page
270 * @return boolean
271 */
272 function is_signup_field() {
273 return (boolean)$this->field->signup;
274 }
275
276
6b64d3b3 277} /// End of class definition
278
9ac33264 279
280/***** General purpose functions for customisable user profiles *****/
281
bb6d3d34 282function profile_load_data(&$user) {
283 global $CFG;
284
285 if ($fields = get_records_select('user_info_field')) {
286 foreach ($fields as $field) {
287 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
288 $newfield = 'profile_field_'.$field->datatype;
334415e9 289 $formfield = new $newfield($field->id, $user->id);
290 $formfield->edit_load_user_data($user);
bb6d3d34 291 }
292 }
293}
294
7240a6ea 295/**
296 * Print out the customisable categories and fields for a users profile
297 * @param object instance of the moodleform class
7240a6ea 298 */
a5d3b072 299function profile_definition(&$mform) {
bb6d3d34 300 global $CFG;
7240a6ea 301
302 if ($categories = get_records_select('user_info_category', '', 'sortorder ASC')) {
303 foreach ($categories as $category) {
304 if ($fields = get_records_select('user_info_field', "categoryid=$category->id", 'sortorder ASC')) {
a5d3b072 305 $mform->addElement('header', 'category_'.$category->id, format_string($category->name));
7240a6ea 306 foreach ($fields as $field) {
7240a6ea 307 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
308 $newfield = 'profile_field_'.$field->datatype;
bb6d3d34 309 $formfield = new $newfield($field->id);
334415e9 310 $formfield->edit_field($mform);
7240a6ea 311
312 }
bb6d3d34 313 }
9ac33264 314 }
315 }
9ac33264 316}
317
bb6d3d34 318function profile_definition_after_data(&$mform) {
319 global $CFG;
320/*
321 if ($fields = get_records('user_info_field')) {
322 foreach ($fields as $field) {
323 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
324 $newfield = 'profile_field_'.$field->datatype;
325 $formfield = new $newfield($field->id);
326//TODO add: method into field class
d052a813 327
d052a813 328 }
bb6d3d34 329 }*/
d052a813 330}
331
a78890d5 332function profile_validation($usernew, $files) {
bb6d3d34 333 global $CFG;
d052a813 334
bb6d3d34 335 $err = array();
336 if ($fields = get_records('user_info_field')) {
337 foreach ($fields as $field) {
338 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
339 $newfield = 'profile_field_'.$field->datatype;
334415e9 340 $formfield = new $newfield($field->id, $usernew->id);
a78890d5 341 $err += $formfield->edit_validate_field($usernew, $files);
d052a813 342 }
343 }
bb6d3d34 344 return $err;
d052a813 345}
346
bb6d3d34 347function profile_save_data($usernew) {
348 global $CFG;
d052a813 349
bb6d3d34 350 if ($fields = get_records_select('user_info_field')) {
351 foreach ($fields as $field) {
352 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
353 $newfield = 'profile_field_'.$field->datatype;
334415e9 354 $formfield = new $newfield($field->id, $usernew->id);
355 $formfield->edit_save_data($usernew);
bb6d3d34 356 }
9ac33264 357 }
9ac33264 358}
359
334415e9 360function profile_display_fields($userid) {
361 global $CFG, $USER;
d052a813 362
334415e9 363 if ($categories = get_records_select('user_info_category', '', 'sortorder ASC')) {
364 foreach ($categories as $category) {
365 if ($fields = get_records_select('user_info_field', "categoryid=$category->id", 'sortorder ASC')) {
366 foreach ($fields as $field) {
367 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
368 $newfield = 'profile_field_'.$field->datatype;
369 $formfield = new $newfield($field->id, $userid);
4a7030e0 370 if ($formfield->is_visible() and !$formfield->is_empty()) {
334415e9 371 print_row(s($formfield->field->name.':'), $formfield->display_data());
372 }
373 }
374 }
375 }
376 }
377}
d052a813 378
831d450e 379/**
380 * Adds code snippet to a moodle form object for custom profile fields that
381 * should appear on the signup page
382 * @param object moodle form object
383 */
384function profile_signup_fields(&$mform) {
385 global $CFG;
386
bbebbc0c 387 if ($fields = get_records('user_info_field', 'signup', 1, 'sortorder ASC')) {
831d450e 388 foreach ($fields as $field) {
389 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
390 $newfield = 'profile_field_'.$field->datatype;
391 $formfield = new $newfield($field->id);
392 $formfield->edit_field($mform);
393 }
394 }
395}
d052a813 396
66643c0a 397/**
398 * Returns an object with the custom profile fields set for the given user
399 * @param integer userid
400 * @return object
401 */
402function profile_user_record($userid) {
403 global $CFG;
404
405 $user = new object();
406
407 if ($fields = get_records_select('user_info_field')) {
408 foreach ($fields as $field) {
409 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
410 $newfield = 'profile_field_'.$field->datatype;
411 $formfield = new $newfield($field->id, $userid);
91cddbc4 412 if ($formfield->is_user_object_data()) $user->{$field->shortname} = $formfield->data;
66643c0a 413 }
414 }
415
416 return $user;
417}
418
419
6b64d3b3 420?>