MDL-22015 fixed more string_manager encapsulation issues - use edit form
[moodle.git] / user / edit_form.php
1 <?php
3 require_once($CFG->dirroot.'/lib/formslib.php');
5 class user_edit_form extends moodleform {
7     // Define the form
8     function definition () {
9         global $CFG, $COURSE;
11         $mform =& $this->_form;
12         if (is_array($this->_customdata) && array_key_exists('editoroptions', $this->_customdata)) {
13             $editoroptions = $this->_customdata['editoroptions'];
14         } else {
15             $editoroptions = null;
16         }
17         //Accessibility: "Required" is bad legend text.
18         $strgeneral  = get_string('general');
19         $strrequired = get_string('required');
21         /// Add some extra hidden fields
22         $mform->addElement('hidden', 'id');
23         $mform->setType('id', PARAM_INT);
24         $mform->addElement('hidden', 'course', $COURSE->id);
25         $mform->setType('course', PARAM_INT);
27         /// Print the required moodle fields first
28         $mform->addElement('header', 'moodle', $strgeneral);
30         /// shared fields
31         useredit_shared_definition($mform, $editoroptions);
33         /// extra settigs
34         $mform->addRule('description_editor', $strrequired, 'required', null, 'client');
35         if (!empty($CFG->gdversion) and !empty($CFG->disableuserimages)) {
36             $mform->removeElement('deletepicture');
37             $mform->removeElement('imagefile');
38             $mform->removeElement('imagealt');
39         }
41         /// Next the customisable profile fields
42         profile_definition($mform);
44         $this->add_action_buttons(false, get_string('updatemyprofile'));
45     }
47     function definition_after_data() {
48         global $CFG, $DB, $OUTPUT;
50         $mform =& $this->_form;
51         $userid = $mform->getElementValue('id');
53         // if language does not exist, use site default lang
54         if ($langsel = $mform->getElementValue('lang')) {
55             $lang = reset($langsel);
56             // check lang exists
57             if (get_string_manager()->translation_exists($lang, false)) {
58                 $lang_el =& $mform->getElement('lang');
59                 $lang_el->setValue($CFG->lang);
60             }
61         }
64         if ($user = $DB->get_record('user', array('id'=>$userid))) {
66             // remove description
67             if (empty($user->description) && !empty($CFG->profilesforenrolledusersonly) && !$DB->record_exists('role_assignments', array('userid'=>$userid))) {
68                 $mform->removeElement('description_editor');
69             }
71             // print picture
72             if (!empty($CFG->gdversion)) {
73                 $image_el =& $mform->getElement('currentpicture');
74                 if ($user and $user->picture) {
75                     $image_el->setValue($OUTPUT->user_picture($user, array('courseid'=>SITEID, 'size'=>64)));
76                 } else {
77                     $image_el->setValue(get_string('none'));
78                 }
79             }
81             /// disable fields that are locked by auth plugins
82             $fields = get_user_fieldnames();
83             $authplugin = get_auth_plugin($user->auth);
84             foreach ($fields as $field) {
85                 if (!$mform->elementExists($field)) {
86                     continue;
87                 }
88                 $configvariable = 'field_lock_' . $field;
89                 if (isset($authplugin->config->{$configvariable})) {
90                     if ($authplugin->config->{$configvariable} === 'locked') {
91                         $mform->hardFreeze($field);
92                         $mform->setConstant($field, $user->$field);
93                     } else if ($authplugin->config->{$configvariable} === 'unlockedifempty' and $user->$field != '') {
94                         $mform->hardFreeze($field);
95                         $mform->setConstant($field, $user->$field);
96                     }
97                 }
98             }
100             /// Next the customisable profile fields
101             profile_definition_after_data($mform, $user->id);
103         } else {
104             profile_definition_after_data($mform, 0);
105         }
106     }
108     function validation($usernew, $files) {
109         global $CFG, $DB;
111         $errors = parent::validation($usernew, $files);
113         $usernew = (object)$usernew;
114         $user    = $DB->get_record('user', array('id'=>$usernew->id));
116         // validate email
117         if (!isset($usernew->email)) {
118             // mail not confirmed yet
119         } else if (!validate_email($usernew->email)) {
120             $errors['email'] = get_string('invalidemail');
121         } else if (($usernew->email !== $user->email) and $DB->record_exists('user', array('email'=>$usernew->email, 'mnethostid'=>$CFG->mnet_localhost_id))) {
122             $errors['email'] = get_string('emailexists');
123         }
125         if (isset($usernew->email) and $usernew->email === $user->email and over_bounce_threshold($user)) {
126             $errors['email'] = get_string('toomanybounces');
127         }
129         if (isset($usernew->email) and !empty($CFG->verifychangedemail) and !isset($errors['email']) and !has_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM))) {
130             $errorstr = email_is_not_allowed($usernew->email);
131             if ($errorstr !== false) {
132                 $errors['email'] = $errorstr;
133             }
134         }
136         /// Next the customisable profile fields
137         $errors += profile_validation($usernew, $files);
139         return $errors;
140     }
142     function get_um() {
143         return $this->_upload_manager;
144     }