MDL-32787 user: rule required for custom profile fields applies to all users editing...
[moodle.git] / user / edit_form.php
CommitLineData
8bdc9cac 1<?php
c9c74c47 2
bfebaf64
MD
3if (!defined('MOODLE_INTERNAL')) {
4 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
5}
6
d8734783 7require_once($CFG->dirroot.'/lib/formslib.php');
c9c74c47 8
9class user_edit_form extends moodleform {
10
11 // Define the form
12 function definition () {
28f23328 13 global $CFG, $COURSE, $USER;
c9c74c47 14
d8734783 15 $mform =& $this->_form;
28f23328
RT
16 $editoroptions = null;
17 $filemanageroptions = null;
18 $userid = $USER->id;
19
20 if (is_array($this->_customdata)) {
21 if (array_key_exists('editoroptions', $this->_customdata)) {
22 $editoroptions = $this->_customdata['editoroptions'];
23 }
24 if (array_key_exists('filemanageroptions', $this->_customdata)) {
25 $filemanageroptions = $this->_customdata['filemanageroptions'];
26 }
27 if (array_key_exists('userid', $this->_customdata)) {
28 $userid = $this->_customdata['userid'];
29 }
4e782b32 30 }
c8149f0f 31 //Accessibility: "Required" is bad legend text.
32 $strgeneral = get_string('general');
c9c74c47 33 $strrequired = get_string('required');
34
c9c74c47 35 /// Add some extra hidden fields
3c19571f 36 $mform->addElement('hidden', 'id');
d18e0fe6 37 $mform->setType('id', PARAM_INT);
d8734783 38 $mform->addElement('hidden', 'course', $COURSE->id);
d18e0fe6 39 $mform->setType('course', PARAM_INT);
acc9c3e0 40
c9c74c47 41 /// Print the required moodle fields first
c8149f0f 42 $mform->addElement('header', 'moodle', $strgeneral);
c9c74c47 43
14a6b7e1 44 /// shared fields
4e782b32 45 useredit_shared_definition($mform, $editoroptions, $filemanageroptions);
c9c74c47 46
d8734783 47 /// extra settigs
d8734783 48 if (!empty($CFG->gdversion) and !empty($CFG->disableuserimages)) {
49 $mform->removeElement('deletepicture');
50 $mform->removeElement('imagefile');
51 $mform->removeElement('imagealt');
c9c74c47 52 }
acc9c3e0 53
bb6d3d34 54 /// Next the customisable profile fields
28f23328 55 profile_definition($mform, $userid);
bb6d3d34 56
d8734783 57 $this->add_action_buttons(false, get_string('updatemyprofile'));
58 }
c9c74c47 59
d8734783 60 function definition_after_data() {
f2f085ee 61 global $CFG, $DB, $OUTPUT;
c9c74c47 62
d8734783 63 $mform =& $this->_form;
64 $userid = $mform->getElementValue('id');
acc9c3e0 65
1545d7f0 66 // if language does not exist, use site default lang
67 if ($langsel = $mform->getElementValue('lang')) {
68 $lang = reset($langsel);
eb309bed 69 // check lang exists
96de38c3 70 if (!get_string_manager()->translation_exists($lang, false)) {
1545d7f0 71 $lang_el =& $mform->getElement('lang');
72 $lang_el->setValue($CFG->lang);
73 }
74 }
75
35f0660a 76
5d910388 77 if ($user = $DB->get_record('user', array('id'=>$userid))) {
c9c74c47 78
35f0660a 79 // remove description
6eaf2e37 80 if (empty($user->description) && !empty($CFG->profilesforenrolledusersonly) && !$DB->record_exists('role_assignments', array('userid'=>$userid))) {
8bdc9cac 81 $mform->removeElement('description_editor');
35f0660a 82 }
83
3eff8146 84 // print picture
85 if (!empty($CFG->gdversion)) {
4125bdc1
SH
86 $context = get_context_instance(CONTEXT_USER, $user->id, MUST_EXIST);
87 $fs = get_file_storage();
ed9e0cb6 88 $hasuploadedpicture = ($fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.png') || $fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.jpg'));
4125bdc1
SH
89 if (!empty($user->picture) && $hasuploadedpicture) {
90 $imagevalue = $OUTPUT->user_picture($user, array('courseid' => SITEID, 'size'=>64));
3eff8146 91 } else {
4125bdc1
SH
92 $imagevalue = get_string('none');
93 }
94 $imageelement = $mform->getElement('currentpicture');
95 $imageelement->setValue($imagevalue);
96
97 if ($mform->elementExists('deletepicture') && !$hasuploadedpicture) {
98 $mform->removeElement('deletepicture');
3eff8146 99 }
c9c74c47 100 }
acc9c3e0 101
d8734783 102 /// disable fields that are locked by auth plugins
103 $fields = get_user_fieldnames();
d8734783 104 $authplugin = get_auth_plugin($user->auth);
105 foreach ($fields as $field) {
106 if (!$mform->elementExists($field)) {
107 continue;
108 }
109 $configvariable = 'field_lock_' . $field;
110 if (isset($authplugin->config->{$configvariable})) {
111 if ($authplugin->config->{$configvariable} === 'locked') {
cc444336 112 $mform->hardFreeze($field);
113 $mform->setConstant($field, $user->$field);
d8734783 114 } else if ($authplugin->config->{$configvariable} === 'unlockedifempty' and $user->$field != '') {
cc444336 115 $mform->hardFreeze($field);
116 $mform->setConstant($field, $user->$field);
d8734783 117 }
118 }
68e9d368 119 }
f2f085ee 120
63466095 121 /// Next the customisable profile fields
122 profile_definition_after_data($mform, $user->id);
c9c74c47 123
63466095 124 } else {
125 profile_definition_after_data($mform, 0);
126 }
c9c74c47 127 }
128
a78890d5 129 function validation($usernew, $files) {
5d910388 130 global $CFG, $DB;
c9c74c47 131
a78890d5 132 $errors = parent::validation($usernew, $files);
133
acc9c3e0 134 $usernew = (object)$usernew;
5d910388 135 $user = $DB->get_record('user', array('id'=>$usernew->id));
acc9c3e0 136
d8734783 137 // validate email
d0b2acde 138 if (!isset($usernew->email)) {
139 // mail not confirmed yet
140 } else if (!validate_email($usernew->email)) {
a78890d5 141 $errors['email'] = get_string('invalidemail');
5d910388 142 } else if (($usernew->email !== $user->email) and $DB->record_exists('user', array('email'=>$usernew->email, 'mnethostid'=>$CFG->mnet_localhost_id))) {
a78890d5 143 $errors['email'] = get_string('emailexists');
c9c74c47 144 }
14a6b7e1 145
d0b2acde 146 if (isset($usernew->email) and $usernew->email === $user->email and over_bounce_threshold($user)) {
a78890d5 147 $errors['email'] = get_string('toomanybounces');
c9c74c47 148 }
149
d0b2acde 150 if (isset($usernew->email) and !empty($CFG->verifychangedemail) and !isset($errors['email']) and !has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM))) {
bff34a67 151 $errorstr = email_is_not_allowed($usernew->email);
152 if ($errorstr !== false) {
153 $errors['email'] = $errorstr;
154 }
155 }
156
bb6d3d34 157 /// Next the customisable profile fields
a78890d5 158 $errors += profile_validation($usernew, $files);
bb6d3d34 159
a78890d5 160 return $errors;
c9c74c47 161 }
162}
163
aa6c1ced 164