MDL-10680:
[moodle-linuxchix.git] / user / editadvanced_form.php
blob3cd636d83b4509f025374851e6411f2927ea715d
1 <?php //$Id$
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;
12 $this->set_upload_manager(new upload_manager('imagefile', false, false, null, false, 0, true, true, false));
13 $strrequired = get_string('required');
15 /// Add some extra hidden fields
16 $mform->addElement('hidden', 'id');
17 $mform->addElement('hidden', 'course', $COURSE->id);
19 /// Print the required moodle fields first
20 $mform->addElement('header', 'moodle', $strrequired);
22 $mform->addElement('text', 'username', get_string('username'), 'size="20"');
23 $mform->addRule('username', $strrequired, 'required', null, 'client');
24 $mform->setType('username', PARAM_RAW);
26 $modules = get_list_of_plugins('auth');
27 $auth_options = array();
28 foreach ($modules as $module) {
29 $auth_options[$module] = get_string("auth_$module"."title", "auth");
31 $mform->addElement('select', 'auth', get_string('chooseauthmethod','auth'), $auth_options);
32 $mform->setHelpButton('auth', array('authchange', get_string('chooseauthmethod','auth')));
33 $mform->setAdvanced('auth');
35 $mform->addElement('passwordunmask', 'newpassword', get_string('newpassword'), 'size="20"');
36 $mform->setType('newpassword', PARAM_RAW);
37 //TODO: add missing help - empty means no change
39 $mform->addElement('checkbox', 'preference_auth_forcepasswordchange', get_string('forcepasswordchange'));
40 //TODO: add missing help - user will be forced to change password
42 /// shared fields
43 useredit_shared_definition($mform);
45 /// Next the customisable profile fields
46 profile_definition($mform);
48 $this->add_action_buttons(false, get_string('updatemyprofile'));
51 function definition_after_data() {
52 global $USER, $CFG;
54 $mform =& $this->_form;
55 $userid = $mform->getElementValue('id');
56 $user = get_record('user', 'id', $userid);
58 // if language does not exist, use site default lang
59 if ($langsel = $mform->getElementValue('lang')) {
60 $lang = reset($langsel);
61 if (!file_exists($CFG->dataroot.'/lang/'.$lang) and
62 !file_exists($CFG->dirroot .'/lang/'.$lang)) {
63 $lang_el =& $mform->getElement('lang');
64 $lang_el->setValue($CFG->lang);
68 // user can not change own auth method
69 if ($userid == $USER->id) {
70 $mform->hardFreeze('auth');
71 $mform->hardFreeze('preference_auth_forcepasswordchange');
74 // admin must choose some password and supply correct email
75 if (!empty($USER->newadminuser)) {
76 $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
78 $email_el =& $mform->getElement('email');
79 if ($email_el->getValue() == 'root@localhost') {
80 $email_el->setValue('');
84 // require password for new users
85 if ($userid == -1) {
86 $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
89 // print picture
90 if (!empty($CFG->gdversion)) {
91 $image_el =& $mform->getElement('currentpicture');
92 if ($user and $user->picture) {
93 $image_el->setValue(print_user_picture($user->id, SITEID, $user->picture, 64, true, false, '', true));
94 } else {
95 $image_el->setValue(get_string('none'));
99 /// Next the customisable profile fields
100 profile_definition_after_data($mform);
103 function validation($usernew) {
104 global $CFG;
106 $usernew = (object)$usernew;
107 $usernew->username = trim($usernew->username);
109 $user = get_record('user', 'id', $usernew->id);
110 $err = array();
112 if (!empty($usernew->newpassword)) {
113 $errmsg = '';//prevent eclipse warning
114 if (!check_password_policy($usernew->newpassword, $errmsg)) {
115 $err['newpassword'] = $errmsg;
119 if (empty($usernew->username)) {
120 //might be only whitespace
121 $err['username'] = get_string('required');
122 } else if (!$user or $user->username !== $usernew->username) {
123 //check new username does not exist
124 if (record_exists('user', 'username', $usernew->username, 'mnethostid', $CFG->mnet_localhost_id)) {
125 $err['username'] = get_string('usernameexists');
127 //check allowed characters
128 if ($usernew->username !== moodle_strtolower($usernew->username)) {
129 $err['username'] = get_string('usernamelowercase');
130 } else {
131 if (empty($CFG->extendedusernamechars)) {
132 $string = eregi_replace("[^(-\.[:alnum:])]", '', $usernew->username);
133 if ($usernew->username !== $string) {
134 $err['username'] = get_string('alphanumerical');
140 if (!$user or $user->email !== $usernew->email) {
141 if (!validate_email($usernew->email)) {
142 $err['email'] = get_string('invalidemail');
143 } else if (record_exists('user', 'email', $usernew->email, 'mnethostid', $CFG->mnet_localhost_id)) {
144 $err['email'] = get_string('emailexists');
148 /// Next the customisable profile fields
149 $err += profile_validation($usernew);
151 if (count($err) == 0){
152 return true;
153 } else {
154 return $err;
158 function get_um() {
159 return $this->_upload_manager;