MDL-14556 reset coursepage state after switchrole merged from MOODLE_19_STABLE
[moodle.git] / user / editadvanced_form.php
CommitLineData
ad6226fb 1<?php //$Id$
2
3require_once($CFG->dirroot.'/lib/formslib.php');
ad6226fb 4
5class user_editadvanced_form extends moodleform {
6
7 // Define the form
bb6d3d34 8 function definition() {
d8734783 9 global $USER, $CFG, $COURSE;
ad6226fb 10
11 $mform =& $this->_form;
ad6226fb 12 $this->set_upload_manager(new upload_manager('imagefile', false, false, null, false, 0, true, true, false));
c8149f0f 13 //Accessibility: "Required" is bad legend text.
14 $strgeneral = get_string('general');
ad6226fb 15 $strrequired = get_string('required');
16
17 /// Add some extra hidden fields
18 $mform->addElement('hidden', 'id');
d8734783 19 $mform->addElement('hidden', 'course', $COURSE->id);
ad6226fb 20
21 /// Print the required moodle fields first
c8149f0f 22 $mform->addElement('header', 'moodle', $strgeneral);
ad6226fb 23
24 $mform->addElement('text', 'username', get_string('username'), 'size="20"');
25 $mform->addRule('username', $strrequired, 'required', null, 'client');
26 $mform->setType('username', PARAM_RAW);
27
28 $modules = get_list_of_plugins('auth');
29 $auth_options = array();
30 foreach ($modules as $module) {
31 $auth_options[$module] = get_string("auth_$module"."title", "auth");
32 }
33 $mform->addElement('select', 'auth', get_string('chooseauthmethod','auth'), $auth_options);
34 $mform->setHelpButton('auth', array('authchange', get_string('chooseauthmethod','auth')));
35 $mform->setAdvanced('auth');
36
54bb33eb 37 $mform->addElement('passwordunmask', 'newpassword', get_string('newpassword'), 'size="20"');
1c54b2bb 38 $mform->setHelpButton('newpassword', array(false, get_string('leavetokeep'),
39 false, true, false, get_string('leavetokeep')));
ad6226fb 40 $mform->setType('newpassword', PARAM_RAW);
ad6226fb 41
fa1d88eb 42 $mform->addElement('advcheckbox', 'preference_auth_forcepasswordchange', get_string('forcepasswordchange'));
1c54b2bb 43 $mform->setHelpButton('preference_auth_forcepasswordchange', array(false, get_string('forcepasswordchangehelp'),
44 false, true, false, get_string('forcepasswordchangehelp')));
ad6226fb 45
14a6b7e1 46 /// shared fields
47 useredit_shared_definition($mform);
ad6226fb 48
bb6d3d34 49 /// Next the customisable profile fields
50 profile_definition($mform);
51
ad6226fb 52 $this->add_action_buttons(false, get_string('updatemyprofile'));
53 }
54
55 function definition_after_data() {
56 global $USER, $CFG;
57
58 $mform =& $this->_form;
e8e0bb2d 59 $userid = $mform->getElementValue('id');
60 $user = get_record('user', 'id', $userid);
ad6226fb 61
1545d7f0 62 // if language does not exist, use site default lang
63 if ($langsel = $mform->getElementValue('lang')) {
64 $lang = reset($langsel);
65 if (!file_exists($CFG->dataroot.'/lang/'.$lang) and
66 !file_exists($CFG->dirroot .'/lang/'.$lang)) {
67 $lang_el =& $mform->getElement('lang');
68 $lang_el->setValue($CFG->lang);
69 }
70 }
71
e8e0bb2d 72 // user can not change own auth method
73 if ($userid == $USER->id) {
74 $mform->hardFreeze('auth');
75 $mform->hardFreeze('preference_auth_forcepasswordchange');
ad6226fb 76 }
77
78 // admin must choose some password and supply correct email
79 if (!empty($USER->newadminuser)) {
80 $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
81
b4c46a8b 82 $email_el =& $mform->getElement('email');
e8e0bb2d 83 if ($email_el->getValue() == 'root@localhost') {
84 $email_el->setValue('');
ad6226fb 85 }
86 }
87
e8e0bb2d 88 // require password for new users
89 if ($userid == -1) {
90 $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
91 }
92
93 // print picture
ad6226fb 94 if (!empty($CFG->gdversion)) {
b4c46a8b 95 $image_el =& $mform->getElement('currentpicture');
e8e0bb2d 96 if ($user and $user->picture) {
65bcf17b 97 $image_el->setValue(print_user_picture($user, SITEID, $user->picture, 64, true, false, '', true));
e8e0bb2d 98 } else {
99 $image_el->setValue(get_string('none'));
100 }
101 }
bb6d3d34 102
103 /// Next the customisable profile fields
104 profile_definition_after_data($mform);
e8e0bb2d 105 }
106
c8969c22 107 function validation($usernew, $files) {
e8e0bb2d 108 global $CFG;
109
110 $usernew = (object)$usernew;
04f639a0 111 $usernew->username = trim($usernew->username);
e8e0bb2d 112
04f639a0 113 $user = get_record('user', 'id', $usernew->id);
114 $err = array();
115
83022298 116 if (!empty($usernew->newpassword)) {
117 $errmsg = '';//prevent eclipse warning
118 if (!check_password_policy($usernew->newpassword, $errmsg)) {
119 $err['newpassword'] = $errmsg;
120 }
121 }
122
04f639a0 123 if (empty($usernew->username)) {
124 //might be only whitespace
125 $err['username'] = get_string('required');
126 } else if (!$user or $user->username !== $usernew->username) {
e8e0bb2d 127 //check new username does not exist
128 if (record_exists('user', 'username', $usernew->username, 'mnethostid', $CFG->mnet_localhost_id)) {
129 $err['username'] = get_string('usernameexists');
130 }
131 //check allowed characters
132 if ($usernew->username !== moodle_strtolower($usernew->username)) {
133 $err['username'] = get_string('usernamelowercase');
ad6226fb 134 } else {
e8e0bb2d 135 if (empty($CFG->extendedusernamechars)) {
136 $string = eregi_replace("[^(-\.[:alnum:])]", '', $usernew->username);
137 if ($usernew->username !== $string) {
138 $err['username'] = get_string('alphanumerical');
139 }
140 }
ad6226fb 141 }
142 }
e8e0bb2d 143
144 if (!$user or $user->email !== $usernew->email) {
145 if (!validate_email($usernew->email)) {
146 $err['email'] = get_string('invalidemail');
147 } else if (record_exists('user', 'email', $usernew->email, 'mnethostid', $CFG->mnet_localhost_id)) {
148 $err['email'] = get_string('emailexists');
149 }
150 }
151
bb6d3d34 152 /// Next the customisable profile fields
c8969c22 153 $err += profile_validation($usernew, $files);
bb6d3d34 154
e8e0bb2d 155 if (count($err) == 0){
156 return true;
157 } else {
158 return $err;
159 }
ad6226fb 160 }
161
162 function get_um() {
163 return $this->_upload_manager;
164 }
165}
166
167?>