MDL-22015 fixed more string_manager encapsulation issues - use edit form
[moodle.git] / user / editadvanced_form.php
1 <?php
3 require_once($CFG->dirroot.'/lib/formslib.php');
5 class user_editadvanced_form extends moodleform {
7     // Define the form
8     function definition() {
9         global $USER, $CFG, $COURSE;
11         $mform =& $this->_form;
13         if (is_array($this->_customdata) && array_key_exists('editoroptions', $this->_customdata)) {
14             $editoroptions = $this->_customdata['editoroptions'];
15         } else {
16             $editoroptions = null;
17         }
19         //Accessibility: "Required" is bad legend text.
20         $strgeneral  = get_string('general');
21         $strrequired = get_string('required');
23         /// Add some extra hidden fields
24         $mform->addElement('hidden', 'id');
25         $mform->setType('id', PARAM_INT);
26         $mform->addElement('hidden', 'course', $COURSE->id);
27         $mform->setType('course', PARAM_INT);
29         /// Print the required moodle fields first
30         $mform->addElement('header', 'moodle', $strgeneral);
32         $mform->addElement('text', 'username', get_string('username'), 'size="20"');
33         $mform->addRule('username', $strrequired, 'required', null, 'client');
34         $mform->setType('username', PARAM_RAW);
36         $auths = get_plugin_list('auth');
37         $auth_options = array();
38         foreach ($auths as $auth => $unused) {
39             $auth_options[$auth] = get_string('pluginname', "auth_{$auth}");
40         }
41         $mform->addElement('select', 'auth', get_string('chooseauthmethod','auth'), $auth_options);
42         $mform->setHelpButton('auth', array('authchange', get_string('chooseauthmethod','auth')));
43         $mform->setAdvanced('auth');
45         if (!empty($CFG->passwordpolicy)){
46             $mform->addElement('static', 'passwordpolicyinfo', '', print_password_policy());
47         }
48         $mform->addElement('passwordunmask', 'newpassword', get_string('newpassword'), 'size="20"');
49         $mform->setHelpButton('newpassword',array('newpassword', get_string('leavetokeep')));
50         $mform->setType('newpassword', PARAM_RAW);
52         $mform->addElement('advcheckbox', 'preference_auth_forcepasswordchange', get_string('forcepasswordchange'));
53         $mform->setHelpButton('preference_auth_forcepasswordchange',array('forcepasswordchange', get_string('forcepasswordchange')));
54         /// shared fields
55         useredit_shared_definition($mform, $editoroptions);
57         /// Next the customisable profile fields
58         profile_definition($mform);
60         $this->add_action_buttons(false, get_string('updatemyprofile'));
61     }
63     function definition_after_data() {
64         global $USER, $CFG, $DB, $OUTPUT;
66         $mform =& $this->_form;
67         if ($userid = $mform->getElementValue('id')) {
68             $user = $DB->get_record('user', array('id'=>$userid));
69         } else {
70             $user = false;
71         }
73         // if language does not exist, use site default lang
74         if ($langsel = $mform->getElementValue('lang')) {
75             $lang = reset($langsel);
76             // check lang exists
77             if (get_string_manager()->translation_exists($lang, false)) {
78                 $lang_el =& $mform->getElement('lang');
79                 $lang_el->setValue($CFG->lang);
80             }
81         }
83         // user can not change own auth method
84         if ($userid == $USER->id) {
85             $mform->hardFreeze('auth');
86             $mform->hardFreeze('preference_auth_forcepasswordchange');
87         }
89         // admin must choose some password and supply correct email
90         if (!empty($USER->newadminuser)) {
91             $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
92         }
94         // require password for new users
95         if ($userid == -1) {
96             $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
97         }
99         // print picture
100         if (!empty($CFG->gdversion)) {
101             $image_el =& $mform->getElement('currentpicture');
102             if ($user and $user->picture) {
103                 $image_el->setValue($OUTPUT->user_picture($user, array('courseid'=>SITEID)));
104             } else {
105                 $image_el->setValue(get_string('none'));
106             }
107         }
109         /// Next the customisable profile fields
110         profile_definition_after_data($mform, $userid);
111     }
113     function validation($usernew, $files) { 
114         global $CFG, $DB;
116         $usernew = (object)$usernew;
117         $usernew->username = trim($usernew->username);
119         $user = $DB->get_record('user', array('id'=>$usernew->id));
120         $err = array();
122         if (!empty($usernew->newpassword)) {
123             $errmsg = '';//prevent eclipse warning
124             if (!check_password_policy($usernew->newpassword, $errmsg)) {
125                 $err['newpassword'] = $errmsg;
126             }
127         }
129         if (empty($usernew->username)) {
130             //might be only whitespace
131             $err['username'] = get_string('required');
132         } else if (!$user or $user->username !== $usernew->username) {
133             //check new username does not exist
134             if ($DB->record_exists('user', array('username'=>$usernew->username, 'mnethostid'=>$CFG->mnet_localhost_id))) {
135                 $err['username'] = get_string('usernameexists');
136             }
137             //check allowed characters
138             if ($usernew->username !== moodle_strtolower($usernew->username)) {
139                 $err['username'] = get_string('usernamelowercase');
140             } else {                
141                 $string = clean_param($usernew->username, PARAM_USERNAME);
142                 if ($usernew->username !== $string) {
143                     $err['username'] = get_string('invalidusername');
144                 }
145             }
146         }
148         if (!$user or $user->email !== $usernew->email) {
149             if (!validate_email($usernew->email)) {
150                 $err['email'] = get_string('invalidemail');
151             } else if ($DB->record_exists('user', array('email'=>$usernew->email, 'mnethostid'=>$CFG->mnet_localhost_id))) {
152                 $err['email'] = get_string('emailexists');
153             }
154         }
156         /// Next the customisable profile fields
157         $err += profile_validation($usernew, $files);
159         if (count($err) == 0){
160             return true;
161         } else {
162             return $err;
163         }
164     }
166     function get_um() {
167         return $this->_upload_manager;
168     }