b82e7396a6ee2d67fe96527dc6c03128060e273d
[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 (!file_exists($CFG->dataroot.'/lang/'.$lang) and
78               !file_exists($CFG->dirroot .'/lang/'.$lang)) {
79                 $lang_el =& $mform->getElement('lang');
80                 $lang_el->setValue($CFG->lang);
81             }
82         }
84         // user can not change own auth method
85         if ($userid == $USER->id) {
86             $mform->hardFreeze('auth');
87             $mform->hardFreeze('preference_auth_forcepasswordchange');
88         }
90         // admin must choose some password and supply correct email
91         if (!empty($USER->newadminuser)) {
92             $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
93         }
95         // require password for new users
96         if ($userid == -1) {
97             $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
98         }
100         // print picture
101         if (!empty($CFG->gdversion)) {
102             $image_el =& $mform->getElement('currentpicture');
103             if ($user and $user->picture) {
104                 $image_el->setValue($OUTPUT->user_picture($user, array('courseid'=>SITEID)));
105             } else {
106                 $image_el->setValue(get_string('none'));
107             }
108         }
110         /// Next the customisable profile fields
111         profile_definition_after_data($mform, $userid);
112     }
114     function validation($usernew, $files) { 
115         global $CFG, $DB;
117         $usernew = (object)$usernew;
118         $usernew->username = trim($usernew->username);
120         $user = $DB->get_record('user', array('id'=>$usernew->id));
121         $err = array();
123         if (!empty($usernew->newpassword)) {
124             $errmsg = '';//prevent eclipse warning
125             if (!check_password_policy($usernew->newpassword, $errmsg)) {
126                 $err['newpassword'] = $errmsg;
127             }
128         }
130         if (empty($usernew->username)) {
131             //might be only whitespace
132             $err['username'] = get_string('required');
133         } else if (!$user or $user->username !== $usernew->username) {
134             //check new username does not exist
135             if ($DB->record_exists('user', array('username'=>$usernew->username, 'mnethostid'=>$CFG->mnet_localhost_id))) {
136                 $err['username'] = get_string('usernameexists');
137             }
138             //check allowed characters
139             if ($usernew->username !== moodle_strtolower($usernew->username)) {
140                 $err['username'] = get_string('usernamelowercase');
141             } else {                
142                 $string = clean_param($usernew->username, PARAM_USERNAME);
143                 if ($usernew->username !== $string) {
144                     $err['username'] = get_string('invalidusername');
145                 }
146             }
147         }
149         if (!$user or $user->email !== $usernew->email) {
150             if (!validate_email($usernew->email)) {
151                 $err['email'] = get_string('invalidemail');
152             } else if ($DB->record_exists('user', array('email'=>$usernew->email, 'mnethostid'=>$CFG->mnet_localhost_id))) {
153                 $err['email'] = get_string('emailexists');
154             }
155         }
157         /// Next the customisable profile fields
158         $err += profile_validation($usernew, $files);
160         if (count($err) == 0){
161             return true;
162         } else {
163             return $err;
164         }
165     }
167     function get_um() {
168         return $this->_upload_manager;
169     }