65
65
function edit_field(&$mform) {
67
67
if ($this->field->visible != PROFILE_VISIBLE_NONE
68
or has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
68
or has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM))) {
70
70
$this->edit_field_add($mform);
71
71
$this->edit_field_set_default($mform);
72
72
$this->edit_field_set_required($mform);
73
73
$this->edit_field_set_locked($mform);
108
110
* @return string contains error message otherwise NULL
110
112
function edit_validate_field($usernew) {
111
//no errors by default
114
/// Check for uniqueness of data if required
115
if ($this->is_unique()) {
116
if ($userid = get_field('user_info_data', 'userid', 'fieldid', $this->field->id, 'data', $usernew->{$this->inputname})) {
117
if ($userid != $usernew->id) {
118
$errors["{$this->inputname}"] = get_string('valuealreadyused');
127
137
* @param object instance of the moodleform class
129
139
function edit_field_set_required(&$mform) {
130
if ($this->is_required() and !has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
140
if ($this->is_required() and !has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM))) {
131
141
$mform->addRule($this->inputname, get_string('required'), 'required', null, 'client');
137
147
* @param object instance of the moodleform class
139
149
function edit_field_set_locked(&$mform) {
140
if ($this->is_locked() and !has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
150
if ($this->is_locked() and !has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM))) {
141
151
$mform->hardFreeze($this->inputname);
152
$mform->setConstant($this->inputname, $this->data);
177
* Check if the field data should be loaded into the user object
178
* By default it is, but for field types where the data may be potentially
179
* large, the child class should override this and return false
182
function is_user_object_data() {
166
187
/***** The following methods generally should not be overwritten by child classes *****/
217
238
case PROFILE_VISIBLE_ALL:
219
240
case PROFILE_VISIBLE_PRIVATE:
220
return ($this->userid == $USER->id);
241
if ($this->userid == $USER->id) {
244
return has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM));
222
return has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM, SITEID));
247
return has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM));
247
272
return (boolean)$this->field->locked;
276
* Check if the field data should be unique
279
function is_unique() {
280
return (boolean)$this->field->forceunique;
284
* Check if the field should appear on the signup page
287
function is_signup_field() {
288
return (boolean)$this->field->signup;
250
292
} /// End of class definition
272
314
function profile_definition(&$mform) {
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);
317
// if user is "admin" fields are displayed regardless
318
$update = has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM));
320
if ($categories = get_records_select('user_info_category', '', 'sortorder ASC')) {
321
foreach ($categories as $category) {
322
if ($fields = get_records_select('user_info_field', "categoryid=$category->id", 'sortorder ASC')) {
324
// check first if *any* fields will be displayed
326
foreach ($fields as $field) {
327
if ($field->visible != PROFILE_VISIBLE_NONE) {
332
// display the header and the fields
333
if ($display or $update) {
334
$mform->addElement('header', 'category_'.$category->id, format_string($category->name));
335
foreach ($fields as $field) {
336
require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
337
$newfield = 'profile_field_'.$field->datatype;
338
$formfield = new $newfield($field->id);
339
$formfield->edit_field($mform);
291
347
function profile_definition_after_data(&$mform) {
311
367
require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
312
368
$newfield = 'profile_field_'.$field->datatype;
313
369
$formfield = new $newfield($field->id, $usernew->id);
314
$err += $formfield->edit_validate_field($usernew);
370
$err += $formfield->edit_validate_field($usernew, $files);
409
* Adds code snippet to a moodle form object for custom profile fields that
410
* should appear on the signup page
411
* @param object moodle form object
413
function profile_signup_fields(&$mform) {
416
//only retrieve required custom fields (with category information)
417
//results are sort by categories, then by fields
418
$sql = "SELECT uf.id as fieldid, ic.id as categoryid, ic.name as categoryname, uf.datatype
419
FROM ".$CFG->prefix."user_info_field uf
420
JOIN ".$CFG->prefix."user_info_category ic
421
ON uf.categoryid = ic.id AND uf.signup = 1 AND uf.visible<>0
422
ORDER BY ic.sortorder ASC, uf.sortorder ASC";
424
if ( $fields = get_records_sql($sql)) {
425
foreach ($fields as $field) {
426
//check if we change the categories
427
if (!isset($currentcat) || $currentcat != $field->categoryid) {
428
$currentcat = $field->categoryid;
429
$mform->addElement('header', 'category_'.$field->categoryid, format_string($field->categoryname));
431
require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
432
$newfield = 'profile_field_'.$field->datatype;
433
$formfield = new $newfield($field->fieldid);
434
$formfield->edit_field($mform);
440
* Returns an object with the custom profile fields set for the given user
441
* @param integer userid
444
function profile_user_record($userid) {
447
$user = new object();
449
if ($fields = get_records_select('user_info_field')) {
450
foreach ($fields as $field) {
451
require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
452
$newfield = 'profile_field_'.$field->datatype;
453
$formfield = new $newfield($field->id, $userid);
454
if ($formfield->is_user_object_data()) $user->{$field->shortname} = $formfield->data;