MDL-21676 user: minor gravatar fixes
[moodle.git] / user / editadvanced_form.php
1 <?php
3 if (!defined('MOODLE_INTERNAL')) {
4     die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
5 }
7 require_once($CFG->dirroot.'/lib/formslib.php');
9 class user_editadvanced_form extends moodleform {
11     // Define the form
12     function definition() {
13         global $USER, $CFG, $COURSE;
15         $mform =& $this->_form;
17         if (is_array($this->_customdata) && array_key_exists('editoroptions', $this->_customdata)) {
18             $editoroptions = $this->_customdata['editoroptions'];
19         } else {
20             $editoroptions = null;
21         }
23         //Accessibility: "Required" is bad legend text.
24         $strgeneral  = get_string('general');
25         $strrequired = get_string('required');
27         /// Add some extra hidden fields
28         $mform->addElement('hidden', 'id');
29         $mform->setType('id', PARAM_INT);
30         $mform->addElement('hidden', 'course', $COURSE->id);
31         $mform->setType('course', PARAM_INT);
33         /// Print the required moodle fields first
34         $mform->addElement('header', 'moodle', $strgeneral);
36         $mform->addElement('text', 'username', get_string('username'), 'size="20"');
37         $mform->addRule('username', $strrequired, 'required', null, 'client');
38         $mform->setType('username', PARAM_RAW);
40         $auths = get_plugin_list('auth');
41         $auth_options = array();
42         foreach ($auths as $auth => $unused) {
43             $auth_options[$auth] = get_string('pluginname', "auth_{$auth}");
44         }
45         $mform->addElement('select', 'auth', get_string('chooseauthmethod','auth'), $auth_options);
46         $mform->addHelpButton('auth', 'chooseauthmethod', 'auth');
48         if (!empty($CFG->passwordpolicy)){
49             $mform->addElement('static', 'passwordpolicyinfo', '', print_password_policy());
50         }
51         $mform->addElement('passwordunmask', 'newpassword', get_string('newpassword'), 'size="20"');
52         $mform->addHelpButton('newpassword', 'newpassword');
53         $mform->setType('newpassword', PARAM_RAW);
55         $mform->addElement('advcheckbox', 'preference_auth_forcepasswordchange', get_string('forcepasswordchange'));
56         $mform->addHelpButton('preference_auth_forcepasswordchange', 'forcepasswordchange');
57         /// shared fields
58         useredit_shared_definition($mform, $editoroptions);
60         /// Next the customisable profile fields
61         profile_definition($mform);
63         $this->add_action_buttons(false, get_string('updatemyprofile'));
64     }
66     function definition_after_data() {
67         global $USER, $CFG, $DB, $OUTPUT;
69         $mform =& $this->_form;
70         if ($userid = $mform->getElementValue('id')) {
71             $user = $DB->get_record('user', array('id'=>$userid));
72         } else {
73             $user = false;
74         }
76         // if language does not exist, use site default lang
77         if ($langsel = $mform->getElementValue('lang')) {
78             $lang = reset($langsel);
79             // check lang exists
80             if (!get_string_manager()->translation_exists($lang, false)) {
81                 $lang_el =& $mform->getElement('lang');
82                 $lang_el->setValue($CFG->lang);
83             }
84         }
86         // user can not change own auth method
87         if ($userid == $USER->id) {
88             $mform->hardFreeze('auth');
89             $mform->hardFreeze('preference_auth_forcepasswordchange');
90         }
92         // admin must choose some password and supply correct email
93         if (!empty($USER->newadminuser)) {
94             $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
95         }
97         // require password for new users
98         if ($userid == -1) {
99             $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
100         }
102         // print picture
103         if (!empty($CFG->gdversion) and empty($USER->newadminuser)) {
104             if ($user) {
105                 $context = get_context_instance(CONTEXT_USER, $user->id, MUST_EXIST);
106                 $fs = get_file_storage();
107                 $hasuploadedpicture = ($fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.png') || $fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.jpg'));
108                 if (!empty($user->picture) && $hasuploadedpicture) {
109                     $imagevalue = $OUTPUT->user_picture($user, array('courseid' => SITEID, 'size'=>64));
110                 } else {
111                     $imagevalue = get_string('none');
112                 }
113             } else {
114                 $imagevalue = get_string('none');
115             }
116             $imageelement = $mform->getElement('currentpicture');
117             $imageelement->setValue($imagevalue);
119             if ($user && $mform->elementExists('deletepicture') && !$hasuploadedpicture) {
120                 $mform->removeElement('deletepicture');
121             }
122         }
124         /// Next the customisable profile fields
125         profile_definition_after_data($mform, $userid);
126     }
128     function validation($usernew, $files) {
129         global $CFG, $DB;
131         $usernew = (object)$usernew;
132         $usernew->username = trim($usernew->username);
134         $user = $DB->get_record('user', array('id'=>$usernew->id));
135         $err = array();
137         if (!empty($usernew->newpassword)) {
138             $errmsg = '';//prevent eclipse warning
139             if (!check_password_policy($usernew->newpassword, $errmsg)) {
140                 $err['newpassword'] = $errmsg;
141             }
142         }
144         if (empty($usernew->username)) {
145             //might be only whitespace
146             $err['username'] = get_string('required');
147         } else if (!$user or $user->username !== $usernew->username) {
148             //check new username does not exist
149             if ($DB->record_exists('user', array('username'=>$usernew->username, 'mnethostid'=>$CFG->mnet_localhost_id))) {
150                 $err['username'] = get_string('usernameexists');
151             }
152             //check allowed characters
153             if ($usernew->username !== moodle_strtolower($usernew->username)) {
154                 $err['username'] = get_string('usernamelowercase');
155             } else {
156                 if ($usernew->username !== clean_param($usernew->username, PARAM_USERNAME)) {
157                     $err['username'] = get_string('invalidusername');
158                 }
159             }
160         }
162         if (!$user or $user->email !== $usernew->email) {
163             if (!validate_email($usernew->email)) {
164                 $err['email'] = get_string('invalidemail');
165             } else if ($DB->record_exists('user', array('email'=>$usernew->email, 'mnethostid'=>$CFG->mnet_localhost_id))) {
166                 $err['email'] = get_string('emailexists');
167             }
168         }
170         /// Next the customisable profile fields
171         $err += profile_validation($usernew, $files);
173         if (count($err) == 0){
174             return true;
175         } else {
176             return $err;
177         }
178     }