MDL-54734 user: Add tests to demonstrate multi-user issues
[moodle.git] / lib / classes / user.php
CommitLineData
3bcf6b3c
RT
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * User class
19 *
20 * @package core
21 * @copyright 2013 Rajesh Taneja <rajesh@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27/**
28 * User class to access user details.
29 *
30 * @todo move api's from user/lib.php and depreciate old ones.
31 * @package core
32 * @copyright 2013 Rajesh Taneja <rajesh@moodle.com>
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 */
35class core_user {
36 /**
37 * No reply user id.
38 */
39 const NOREPLY_USER = -10;
40
41 /**
6c3ad77e 42 * Support user id.
3bcf6b3c
RT
43 */
44 const SUPPORT_USER = -20;
45
46 /** @var stdClass keep record of noreply user */
47 public static $noreplyuser = false;
48
49 /** @var stdClass keep record of support user */
50 public static $supportuser = false;
51
dccf9ca3
SL
52 /** @var array store user fields properties cache. */
53 protected static $propertiescache = null;
54
3bcf6b3c
RT
55 /**
56 * Return user object from db or create noreply or support user,
57 * if userid matches corse_user::NOREPLY_USER or corse_user::SUPPORT_USER
58 * respectively. If userid is not found, then return false.
59 *
60 * @param int $userid user id
61 * @param string $fields A comma separated list of user fields to be returned, support and noreply user
62 * will not be filtered by this.
63 * @param int $strictness IGNORE_MISSING means compatible mode, false returned if user not found, debug message if more found;
64 * IGNORE_MULTIPLE means return first user, ignore multiple user records found(not recommended);
65 * MUST_EXIST means throw an exception if no user record or multiple records found.
66 * @return stdClass|bool user record if found, else false.
67 * @throws dml_exception if user record not found and respective $strictness is set.
68 */
69 public static function get_user($userid, $fields = '*', $strictness = IGNORE_MISSING) {
70 global $DB;
71
72 // If noreply user then create fake record and return.
73 switch ($userid) {
74 case self::NOREPLY_USER:
d1b30757 75 return self::get_noreply_user();
3bcf6b3c
RT
76 break;
77 case self::SUPPORT_USER:
d1b30757 78 return self::get_support_user();
3bcf6b3c
RT
79 break;
80 default:
81 return $DB->get_record('user', array('id' => $userid), $fields, $strictness);
82 }
83 }
84
2d35b7d3
GPL
85
86 /**
87 * Return user object from db based on their username.
88 *
89 * @param string $username The username of the user searched.
90 * @param string $fields A comma separated list of user fields to be returned, support and noreply user.
91 * @param int $mnethostid The id of the remote host.
92 * @param int $strictness IGNORE_MISSING means compatible mode, false returned if user not found, debug message if more found;
93 * IGNORE_MULTIPLE means return first user, ignore multiple user records found(not recommended);
94 * MUST_EXIST means throw an exception if no user record or multiple records found.
95 * @return stdClass|bool user record if found, else false.
96 * @throws dml_exception if user record not found and respective $strictness is set.
97 */
98 public static function get_user_by_username($username, $fields = '*', $mnethostid = null, $strictness = IGNORE_MISSING) {
99 global $DB, $CFG;
100
101 // Because we use the username as the search criteria, we must also restrict our search based on mnet host.
102 if (empty($mnethostid)) {
103 // If empty, we restrict to local users.
104 $mnethostid = $CFG->mnet_localhost_id;
105 }
106
107 return $DB->get_record('user', array('username' => $username, 'mnethostid' => $mnethostid), $fields, $strictness);
108 }
109
3bcf6b3c
RT
110 /**
111 * Helper function to return dummy noreply user record.
112 *
113 * @return stdClass
114 */
115 protected static function get_dummy_user_record() {
116 global $CFG;
117
118 $dummyuser = new stdClass();
119 $dummyuser->id = self::NOREPLY_USER;
120 $dummyuser->email = $CFG->noreplyaddress;
121 $dummyuser->firstname = get_string('noreplyname');
122 $dummyuser->username = 'noreply';
123 $dummyuser->lastname = '';
124 $dummyuser->confirmed = 1;
125 $dummyuser->suspended = 0;
126 $dummyuser->deleted = 0;
127 $dummyuser->picture = 0;
128 $dummyuser->auth = 'manual';
129 $dummyuser->firstnamephonetic = '';
130 $dummyuser->lastnamephonetic = '';
131 $dummyuser->middlename = '';
132 $dummyuser->alternatename = '';
133 $dummyuser->imagealt = '';
134 return $dummyuser;
135 }
136
137 /**
138 * Return noreply user record, this is currently used in messaging
139 * system only for sending messages from noreply email.
140 * It will return record of $CFG->noreplyuserid if set else return dummy
141 * user object with hard-coded $user->emailstop = 1 so noreply can be sent to user.
142 *
143 * @return stdClass user record.
144 */
145 public static function get_noreply_user() {
146 global $CFG;
147
148 if (!empty(self::$noreplyuser)) {
149 return self::$noreplyuser;
150 }
151
152 // If noreply user is set then use it, else create one.
153 if (!empty($CFG->noreplyuserid)) {
154 self::$noreplyuser = self::get_user($CFG->noreplyuserid);
155 }
156
157 if (empty(self::$noreplyuser)) {
158 self::$noreplyuser = self::get_dummy_user_record();
9a90e7c5 159 self::$noreplyuser->maildisplay = '1'; // Show to all.
3bcf6b3c
RT
160 }
161 self::$noreplyuser->emailstop = 1; // Force msg stop for this user.
162 return self::$noreplyuser;
163 }
164
165 /**
166 * Return support user record, this is currently used in messaging
167 * system only for sending messages to support email.
168 * $CFG->supportuserid is set then returns user record
169 * $CFG->supportemail is set then return dummy record with $CFG->supportemail
170 * else return admin user record with hard-coded $user->emailstop = 0, so user
171 * gets support message.
172 *
173 * @return stdClass user record.
174 */
175 public static function get_support_user() {
176 global $CFG;
177
178 if (!empty(self::$supportuser)) {
179 return self::$supportuser;
180 }
181
182 // If custom support user is set then use it, else if supportemail is set then use it, else use noreply.
183 if (!empty($CFG->supportuserid)) {
184 self::$supportuser = self::get_user($CFG->supportuserid, '*', MUST_EXIST);
185 }
186
187 // Try sending it to support email if support user is not set.
188 if (empty(self::$supportuser) && !empty($CFG->supportemail)) {
189 self::$supportuser = self::get_dummy_user_record();
190 self::$supportuser->id = self::SUPPORT_USER;
191 self::$supportuser->email = $CFG->supportemail;
6c3ad77e
PS
192 if ($CFG->supportname) {
193 self::$supportuser->firstname = $CFG->supportname;
194 }
3bcf6b3c 195 self::$supportuser->username = 'support';
6c3ad77e 196 self::$supportuser->maildisplay = '1'; // Show to all.
3bcf6b3c
RT
197 }
198
199 // Send support msg to admin user if nothing is set above.
200 if (empty(self::$supportuser)) {
201 self::$supportuser = get_admin();
202 }
203
204 // Unset emailstop to make sure support message is sent.
205 self::$supportuser->emailstop = 0;
206 return self::$supportuser;
207 }
208
209 /**
210 * Reset self::$noreplyuser and self::$supportuser.
211 * This is only used by phpunit, and there is no other use case for this function.
212 * Please don't use it outside phpunit.
213 */
214 public static function reset_internal_users() {
215 if (PHPUNIT_TEST) {
216 self::$noreplyuser = false;
217 self::$supportuser = false;
218 } else {
219 debugging('reset_internal_users() should not be used outside phpunit.', DEBUG_DEVELOPER);
220 }
221 }
222
223 /**
224 * Return true is user id is greater than self::NOREPLY_USER and
6c3ad77e 225 * alternatively check db.
3bcf6b3c
RT
226 *
227 * @param int $userid user id.
228 * @param bool $checkdb if true userid will be checked in db. By default it's false, and
229 * userid is compared with NOREPLY_USER for performance.
230 * @return bool true is real user else false.
231 */
232 public static function is_real_user($userid, $checkdb = false) {
6c3ad77e
PS
233 global $DB;
234
3bcf6b3c
RT
235 if ($userid < 0) {
236 return false;
237 }
238 if ($checkdb) {
239 return $DB->record_exists('user', array('id' => $userid));
240 } else {
241 return true;
242 }
243 }
3961ebfb
JL
244
245 /**
246 * Check if the given user is an active user in the site.
247 *
248 * @param stdClass $user user object
249 * @param boolean $checksuspended whether to check if the user has the account suspended
250 * @param boolean $checknologin whether to check if the user uses the nologin auth method
251 * @throws moodle_exception
252 * @since Moodle 3.0
253 */
254 public static function require_active_user($user, $checksuspended = false, $checknologin = false) {
255
256 if (!self::is_real_user($user->id)) {
257 throw new moodle_exception('invaliduser', 'error');
258 }
259
260 if ($user->deleted) {
261 throw new moodle_exception('userdeleted');
262 }
263
264 if (empty($user->confirmed)) {
265 throw new moodle_exception('usernotconfirmed', 'moodle', '', $user->username);
266 }
267
268 if (isguestuser($user)) {
269 throw new moodle_exception('guestsarenotallowed', 'error');
270 }
271
272 if ($checksuspended and $user->suspended) {
273 throw new moodle_exception('suspended', 'auth');
274 }
275
276 if ($checknologin and $user->auth == 'nologin') {
277 throw new moodle_exception('suspended', 'auth');
278 }
279 }
dccf9ca3 280
5407c5b0
RS
281 /**
282 * Updates the provided users profile picture based upon the expected fields returned from the edit or edit_advanced forms.
283 *
284 * @param stdClass $usernew An object that contains some information about the user being updated
285 * @param array $filemanageroptions
286 * @return bool True if the user was updated, false if it stayed the same.
287 */
288 public static function update_picture(stdClass $usernew, $filemanageroptions = array()) {
289 global $CFG, $DB;
290 require_once("$CFG->libdir/gdlib.php");
291
292 $context = context_user::instance($usernew->id, MUST_EXIST);
293 $user = core_user::get_user($usernew->id, 'id, picture', MUST_EXIST);
294
295 $newpicture = $user->picture;
296 // Get file_storage to process files.
297 $fs = get_file_storage();
298 if (!empty($usernew->deletepicture)) {
299 // The user has chosen to delete the selected users picture.
300 $fs->delete_area_files($context->id, 'user', 'icon'); // Drop all images in area.
301 $newpicture = 0;
302
303 } else {
304 // Save newly uploaded file, this will avoid context mismatch for newly created users.
305 file_save_draft_area_files($usernew->imagefile, $context->id, 'user', 'newicon', 0, $filemanageroptions);
306 if (($iconfiles = $fs->get_area_files($context->id, 'user', 'newicon')) && count($iconfiles) == 2) {
307 // Get file which was uploaded in draft area.
308 foreach ($iconfiles as $file) {
309 if (!$file->is_directory()) {
310 break;
311 }
312 }
313 // Copy file to temporary location and the send it for processing icon.
314 if ($iconfile = $file->copy_content_to_temp()) {
315 // There is a new image that has been uploaded.
316 // Process the new image and set the user to make use of it.
317 // NOTE: Uploaded images always take over Gravatar.
318 $newpicture = (int)process_new_icon($context, 'user', 'icon', 0, $iconfile);
319 // Delete temporary file.
320 @unlink($iconfile);
321 // Remove uploaded file.
322 $fs->delete_area_files($context->id, 'user', 'newicon');
323 } else {
324 // Something went wrong while creating temp file.
325 // Remove uploaded file.
326 $fs->delete_area_files($context->id, 'user', 'newicon');
327 return false;
328 }
329 }
330 }
331
332 if ($newpicture != $user->picture) {
333 $DB->set_field('user', 'picture', $newpicture, array('id' => $user->id));
334 return true;
335 } else {
336 return false;
337 }
338 }
339
340
341
dccf9ca3
SL
342 /**
343 * Definition of user profile fields and the expected parameter type for data validation.
344 *
4ce09314
SL
345 * array(
346 * 'property_name' => array( // The user property to be checked. Should match the field on the user table.
347 * 'null' => NULL_ALLOWED, // Defaults to NULL_NOT_ALLOWED. Takes NULL_NOT_ALLOWED or NULL_ALLOWED.
348 * 'type' => PARAM_TYPE, // Expected parameter type of the user field.
349 * 'choices' => array(1, 2..) // An array of accepted values of the user field.
350 * 'default' => $CFG->setting // An default value for the field.
351 * )
352 * )
353 *
354 * The fields choices and default are optional.
355 *
dccf9ca3
SL
356 * @return void
357 */
358 protected static function fill_properties_cache() {
4ce09314 359 global $CFG;
dccf9ca3
SL
360 if (self::$propertiescache !== null) {
361 return;
362 }
363
364 // Array of user fields properties and expected parameters.
365 // Every new field on the user table should be added here otherwise it won't be validated.
366 $fields = array();
4ce09314
SL
367 $fields['id'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED);
368 $fields['auth'] = array('type' => PARAM_AUTH, 'null' => NULL_NOT_ALLOWED);
369 $fields['confirmed'] = array('type' => PARAM_BOOL, 'null' => NULL_NOT_ALLOWED);
370 $fields['policyagreed'] = array('type' => PARAM_BOOL, 'null' => NULL_NOT_ALLOWED);
371 $fields['deleted'] = array('type' => PARAM_BOOL, 'null' => NULL_NOT_ALLOWED);
372 $fields['suspended'] = array('type' => PARAM_BOOL, 'null' => NULL_NOT_ALLOWED);
373 $fields['mnethostid'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED);
374 $fields['username'] = array('type' => PARAM_USERNAME, 'null' => NULL_NOT_ALLOWED);
375 $fields['password'] = array('type' => PARAM_RAW, 'null' => NULL_NOT_ALLOWED);
376 $fields['idnumber'] = array('type' => PARAM_RAW, 'null' => NULL_NOT_ALLOWED);
377 $fields['firstname'] = array('type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED);
378 $fields['lastname'] = array('type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED);
379 $fields['surname'] = array('type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED);
380 $fields['email'] = array('type' => PARAM_RAW_TRIMMED, 'null' => NULL_NOT_ALLOWED);
381 $fields['emailstop'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED);
382 $fields['icq'] = array('type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED);
383 $fields['skype'] = array('type' => PARAM_NOTAGS, 'null' => NULL_ALLOWED);
384 $fields['aim'] = array('type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED);
385 $fields['yahoo'] = array('type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED);
386 $fields['msn'] = array('type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED);
387 $fields['phone1'] = array('type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED);
388 $fields['phone2'] = array('type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED);
389 $fields['institution'] = array('type' => PARAM_TEXT, 'null' => NULL_NOT_ALLOWED);
390 $fields['department'] = array('type' => PARAM_TEXT, 'null' => NULL_NOT_ALLOWED);
391 $fields['address'] = array('type' => PARAM_TEXT, 'null' => NULL_NOT_ALLOWED);
392 $fields['city'] = array('type' => PARAM_TEXT, 'null' => NULL_NOT_ALLOWED, 'default' => $CFG->defaultcity);
393 $fields['country'] = array('type' => PARAM_ALPHA, 'null' => NULL_NOT_ALLOWED, 'default' => $CFG->country,
394 'choices' => array_merge(array('' => ''), get_string_manager()->get_list_of_countries(true, true)));
395 $fields['lang'] = array('type' => PARAM_LANG, 'null' => NULL_NOT_ALLOWED, 'default' => $CFG->lang,
aa788b3c 396 'choices' => array_merge(array('' => ''), get_string_manager()->get_list_of_translations(false)));
4ce09314
SL
397 $fields['calendartype'] = array('type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED, 'default' => $CFG->calendartype,
398 'choices' => array_merge(array('' => ''), \core_calendar\type_factory::get_list_of_calendar_types()));
399 $fields['theme'] = array('type' => PARAM_THEME, 'null' => NULL_NOT_ALLOWED,
400 'default' => theme_config::DEFAULT_THEME, 'choices' => array_merge(array('' => ''), get_list_of_themes()));
16825c4e
FM
401 $fields['timezone'] = array('type' => PARAM_TIMEZONE, 'null' => NULL_NOT_ALLOWED,
402 'default' => core_date::get_server_timezone()); // Must not use choices here: timezones can come and go.
4ce09314
SL
403 $fields['firstaccess'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED);
404 $fields['lastaccess'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED);
405 $fields['lastlogin'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED);
406 $fields['currentlogin'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED);
407 $fields['lastip'] = array('type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED);
408 $fields['secret'] = array('type' => PARAM_RAW, 'null' => NULL_NOT_ALLOWED);
409 $fields['picture'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED);
410 $fields['url'] = array('type' => PARAM_URL, 'null' => NULL_NOT_ALLOWED);
411 $fields['description'] = array('type' => PARAM_RAW, 'null' => NULL_ALLOWED);
412 $fields['descriptionformat'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED);
413 $fields['mailformat'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED,
414 'default' => $CFG->defaultpreference_mailformat);
415 $fields['maildigest'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED,
416 'default' => $CFG->defaultpreference_maildigest);
417 $fields['maildisplay'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED,
418 'default' => $CFG->defaultpreference_maildisplay);
419 $fields['autosubscribe'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED,
420 'default' => $CFG->defaultpreference_autosubscribe);
421 $fields['trackforums'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED,
422 'default' => $CFG->defaultpreference_trackforums);
423 $fields['timecreated'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED);
424 $fields['timemodified'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED);
425 $fields['trustbitmask'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED);
426 $fields['imagealt'] = array('type' => PARAM_TEXT, 'null' => NULL_ALLOWED);
427 $fields['lastnamephonetic'] = array('type' => PARAM_NOTAGS, 'null' => NULL_ALLOWED);
428 $fields['firstnamephonetic'] = array('type' => PARAM_NOTAGS, 'null' => NULL_ALLOWED);
429 $fields['middlename'] = array('type' => PARAM_NOTAGS, 'null' => NULL_ALLOWED);
430 $fields['alternatename'] = array('type' => PARAM_NOTAGS, 'null' => NULL_ALLOWED);
dccf9ca3
SL
431
432 self::$propertiescache = $fields;
433 }
434
435 /**
436 * Get properties of a user field.
437 *
438 * @param string $property property name to be retrieved.
439 * @throws coding_exception if the requested property name is invalid.
440 * @return array the property definition.
441 */
442 public static function get_property_definition($property) {
443
444 self::fill_properties_cache();
445
446 if (!array_key_exists($property, self::$propertiescache)) {
447 throw new coding_exception('Invalid property requested.');
448 }
449
450 return self::$propertiescache[$property];
451 }
452
4ce09314
SL
453 /**
454 * Validate user data.
455 *
456 * This method just validates each user field and return an array of errors. It doesn't clean the data,
457 * the methods clean() and clean_field() should be used for this purpose.
458 *
459 * @param stdClass|array $data user data object or array to be validated.
460 * @return array|true $errors array of errors found on the user object, true if the validation passed.
461 */
462 public static function validate($data) {
463 // Get all user profile fields definition.
464 self::fill_properties_cache();
465
466 foreach ($data as $property => $value) {
467 try {
468 if (isset(self::$propertiescache[$property])) {
469 validate_param($value, self::$propertiescache[$property]['type'], self::$propertiescache[$property]['null']);
470 }
471 // Check that the value is part of a list of allowed values.
472 if (!empty(self::$propertiescache[$property]['choices']) &&
aa788b3c 473 !isset(self::$propertiescache[$property]['choices'][$value])) {
4ce09314
SL
474 throw new invalid_parameter_exception($value);
475 }
476 } catch (invalid_parameter_exception $e) {
477 $errors[$property] = $e->getMessage();
478 }
479 }
480
481 return empty($errors) ? true : $errors;
482 }
483
dccf9ca3
SL
484 /**
485 * Clean the properties cache.
486 *
487 * During unit tests we need to be able to reset all caches so that each new test starts in a known state.
488 * Intended for use only for testing, phpunit calls this before every test.
489 */
490 public static function reset_caches() {
491 self::$propertiescache = null;
492 }
4ce09314
SL
493
494 /**
495 * Clean the user data.
496 *
497 * @param stdClass|array $user the user data to be validated against properties definition.
498 * @return stdClass $user the cleaned user data.
499 */
500 public static function clean_data($user) {
501 if (empty($user)) {
502 return $user;
503 }
504
505 foreach ($user as $field => $value) {
506 // Get the property parameter type and do the cleaning.
507 try {
aa788b3c 508 $user->$field = core_user::clean_field($value, $field);
4ce09314
SL
509 } catch (coding_exception $e) {
510 debugging("The property '$field' could not be cleaned.", DEBUG_DEVELOPER);
511 }
512 }
513
514 return $user;
515 }
516
517 /**
518 * Clean a specific user field.
519 *
520 * @param string $data the user field data to be cleaned.
521 * @param string $field the user field name on the property definition cache.
522 * @return string the cleaned user data.
523 */
524 public static function clean_field($data, $field) {
525 if (empty($data) || empty($field)) {
526 return $data;
527 }
528
529 try {
530 $type = core_user::get_property_type($field);
531
532 if (isset(self::$propertiescache[$field]['choices'])) {
533 if (!array_key_exists($data, self::$propertiescache[$field]['choices'])) {
534 if (isset(self::$propertiescache[$field]['default'])) {
535 $data = self::$propertiescache[$field]['default'];
536 } else {
537 $data = '';
538 }
aa788b3c
SL
539 } else {
540 return $data;
4ce09314
SL
541 }
542 } else {
543 $data = clean_param($data, $type);
544 }
545 } catch (coding_exception $e) {
546 debugging("The property '$field' could not be cleaned.", DEBUG_DEVELOPER);
547 }
548
549 return $data;
550 }
551
552 /**
553 * Get the parameter type of the property.
554 *
555 * @param string $property property name to be retrieved.
556 * @throws coding_exception if the requested property name is invalid.
557 * @return int the property parameter type.
558 */
559 public static function get_property_type($property) {
560
561 self::fill_properties_cache();
562
563 if (!array_key_exists($property, self::$propertiescache)) {
564 throw new coding_exception('Invalid property requested: ' . $property);
565 }
566
567 return self::$propertiescache[$property]['type'];
568 }
569
570 /**
571 * Discover if the property is NULL_ALLOWED or NULL_NOT_ALLOWED.
572 *
573 * @param string $property property name to be retrieved.
574 * @throws coding_exception if the requested property name is invalid.
575 * @return bool true if the property is NULL_ALLOWED, false otherwise.
576 */
577 public static function get_property_null($property) {
578
579 self::fill_properties_cache();
580
581 if (!array_key_exists($property, self::$propertiescache)) {
582 throw new coding_exception('Invalid property requested: ' . $property);
583 }
584
585 return self::$propertiescache[$property]['null'];
586 }
587
588 /**
589 * Get the choices of the property.
590 *
591 * This is a helper method to validate a value against a list of acceptable choices.
16825c4e 592 * For instance: country, language, themes and etc.
4ce09314
SL
593 *
594 * @param string $property property name to be retrieved.
595 * @throws coding_exception if the requested property name is invalid or if it does not has a list of choices.
596 * @return array the property parameter type.
597 */
598 public static function get_property_choices($property) {
599
600 self::fill_properties_cache();
601
602 if (!array_key_exists($property, self::$propertiescache) && !array_key_exists('choices',
603 self::$propertiescache[$property])) {
604
605 throw new coding_exception('Invalid property requested, or the property does not has a list of choices.');
606 }
607
608 return self::$propertiescache[$property]['choices'];
609 }
610
611 /**
612 * Get the property default.
613 *
614 * This method gets the default value of a field (if exists).
615 *
616 * @param string $property property name to be retrieved.
617 * @throws coding_exception if the requested property name is invalid or if it does not has a default value.
618 * @return string the property default value.
619 */
620 public static function get_property_default($property) {
621
622 self::fill_properties_cache();
623
624 if (!array_key_exists($property, self::$propertiescache) || !isset(self::$propertiescache[$property]['default'])) {
625 throw new coding_exception('Invalid property requested, or the property does not has a default value.');
626 }
627
628 return self::$propertiescache[$property]['default'];
629 }
3bcf6b3c 630}