Bumping version for 1.8.10 release (note new convention for numbering)
[moodle.git] / user / editadvanced_form.php
blob2eb223ffa4bcc9c454ee9b27733a52b3fe704107
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 //Accessibility: "Required" is bad legend text.
14 $strgeneral = get_string('general');
15 $strrequired = get_string('required');
17 /// Add some extra hidden fields
18 $mform->addElement('hidden', 'id');
19 $mform->addElement('hidden', 'course', $COURSE->id);
21 /// Print the required moodle fields first
22 $mform->addElement('header', 'moodle', $strgeneral);
24 $mform->addElement('text', 'username', get_string('username'), 'size="20"');
25 $mform->addRule('username', $strrequired, 'required', null, 'client');
26 $mform->setType('username', PARAM_RAW);
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");
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');
37 $mform->addElement('text', 'newpassword', get_string('newpassword'), 'size="20"');
38 $mform->setType('newpassword', PARAM_RAW);
39 //TODO: add missing help - empty means no change
41 $mform->addElement('advcheckbox', 'preference_auth_forcepasswordchange', get_string('forcepasswordchange'));
42 //TODO: add missing help - user will be forced to change password
44 /// shared fields
45 useredit_shared_definition($mform);
47 /// Next the customisable profile fields
48 profile_definition($mform);
50 $this->add_action_buttons(false, get_string('updatemyprofile'));
53 function definition_after_data() {
54 global $USER, $CFG;
56 $mform =& $this->_form;
57 if ($userid = $mform->getElementValue('id')) {
58 $user = get_record('user', 'id', $userid);
59 } else {
60 $user = false;
63 // if language does not exist, use site default lang
64 if ($langsel = $mform->getElementValue('lang')) {
65 $lang = reset($langsel);
66 if (!file_exists($CFG->dataroot.'/lang/'.$lang) and
67 !file_exists($CFG->dirroot .'/lang/'.$lang)) {
68 $lang_el =& $mform->getElement('lang');
69 $lang_el->setValue($CFG->lang);
73 // user can not change own auth method
74 if ($userid == $USER->id) {
75 $mform->hardFreeze('auth');
76 $mform->hardFreeze('preference_auth_forcepasswordchange');
79 // admin must choose some password and supply correct email
80 if (!empty($USER->newadminuser)) {
81 $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
83 $email_el =& $mform->getElement('email');
84 if ($email_el->getValue() == 'root@localhost') {
85 $email_el->setValue('');
89 // require password for new users
90 if ($userid == -1) {
91 $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
94 // print picture
95 if (!empty($CFG->gdversion)) {
96 $image_el =& $mform->getElement('currentpicture');
97 if ($user and $user->picture) {
98 $image_el->setValue(print_user_picture($user->id, SITEID, $user->picture, 64, true, false, '', true));
99 } else {
100 $image_el->setValue(get_string('none'));
104 /// Next the customisable profile fields
105 profile_definition_after_data($mform, $userid);
108 function validation($usernew) {
109 global $CFG;
111 $usernew = (object)$usernew;
112 $usernew->username = trim($usernew->username);
114 $user = get_record('user', 'id', $usernew->id);
115 $err = array();
117 if (empty($usernew->username)) {
118 //might be only whitespace
119 $err['username'] = get_string('required');
120 } else if (!$user or $user->username !== $usernew->username) {
121 //check new username does not exist
122 if (record_exists('user', 'username', $usernew->username, 'mnethostid', $CFG->mnet_localhost_id)) {
123 $err['username'] = get_string('usernameexists');
125 //check allowed characters
126 if ($usernew->username !== moodle_strtolower($usernew->username)) {
127 $err['username'] = get_string('usernamelowercase');
128 } else {
129 if (empty($CFG->extendedusernamechars)) {
130 $string = eregi_replace("[^(-\.[:alnum:])]", '', $usernew->username);
131 if ($usernew->username !== $string) {
132 $err['username'] = get_string('alphanumerical');
138 if (!$user or $user->email !== stripslashes($usernew->email)) {
139 if (!validate_email($usernew->email)) {
140 $err['email'] = get_string('invalidemail');
141 } else if (record_exists('user', 'email', $usernew->email, 'mnethostid', $CFG->mnet_localhost_id)) {
142 $err['email'] = get_string('emailexists');
146 /// Next the customisable profile fields
147 $err += profile_validation($usernew);
149 if (count($err) == 0){
150 return true;
151 } else {
152 return $err;
156 function get_um() {
157 return $this->_upload_manager;