Eliminate support for old-style question editing forms.
[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));
13 $strrequired = get_string('required');
14
15 /// Add some extra hidden fields
16 $mform->addElement('hidden', 'id');
d8734783 17 $mform->addElement('hidden', 'course', $COURSE->id);
ad6226fb 18
19 /// Print the required moodle fields first
20 $mform->addElement('header', 'moodle', $strrequired);
21
22 $mform->addElement('text', 'username', get_string('username'), 'size="20"');
23 $mform->addRule('username', $strrequired, 'required', null, 'client');
24 $mform->setType('username', PARAM_RAW);
25
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");
30 }
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');
34
e96167d2 35 $mform->addElement('passwordreveal', 'newpassword', get_string('newpassword'), 'size="20"');
ad6226fb 36 $mform->setType('newpassword', PARAM_RAW);
37 //TODO: add missing help - empty means no change
38
39 $mform->addElement('checkbox', 'preference_auth_forcepasswordchange', get_string('forcepasswordchange'));
40 //TODO: add missing help - user will be forced to change password
41
14a6b7e1 42 /// shared fields
43 useredit_shared_definition($mform);
ad6226fb 44
bb6d3d34 45 /// Next the customisable profile fields
46 profile_definition($mform);
47
ad6226fb 48 $this->add_action_buttons(false, get_string('updatemyprofile'));
49 }
50
51 function definition_after_data() {
52 global $USER, $CFG;
53
54 $mform =& $this->_form;
e8e0bb2d 55 $userid = $mform->getElementValue('id');
56 $user = get_record('user', 'id', $userid);
ad6226fb 57
1545d7f0 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);
65 }
66 }
67
e8e0bb2d 68 // user can not change own auth method
69 if ($userid == $USER->id) {
70 $mform->hardFreeze('auth');
71 $mform->hardFreeze('preference_auth_forcepasswordchange');
ad6226fb 72 }
73
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');
77
b4c46a8b 78 $email_el =& $mform->getElement('email');
e8e0bb2d 79 if ($email_el->getValue() == 'root@localhost') {
80 $email_el->setValue('');
ad6226fb 81 }
82 }
83
e8e0bb2d 84 // require password for new users
85 if ($userid == -1) {
86 $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
87 }
88
89 // print picture
ad6226fb 90 if (!empty($CFG->gdversion)) {
b4c46a8b 91 $image_el =& $mform->getElement('currentpicture');
e8e0bb2d 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'));
96 }
97 }
bb6d3d34 98
99 /// Next the customisable profile fields
100 profile_definition_after_data($mform);
e8e0bb2d 101 }
102
bb6d3d34 103 function validation($usernew) {
e8e0bb2d 104 global $CFG;
105
106 $usernew = (object)$usernew;
04f639a0 107 $usernew->username = trim($usernew->username);
e8e0bb2d 108
04f639a0 109 $user = get_record('user', 'id', $usernew->id);
110 $err = array();
111
112 if (empty($usernew->username)) {
113 //might be only whitespace
114 $err['username'] = get_string('required');
115 } else if (!$user or $user->username !== $usernew->username) {
e8e0bb2d 116 //check new username does not exist
117 if (record_exists('user', 'username', $usernew->username, 'mnethostid', $CFG->mnet_localhost_id)) {
118 $err['username'] = get_string('usernameexists');
119 }
120 //check allowed characters
121 if ($usernew->username !== moodle_strtolower($usernew->username)) {
122 $err['username'] = get_string('usernamelowercase');
ad6226fb 123 } else {
e8e0bb2d 124 if (empty($CFG->extendedusernamechars)) {
125 $string = eregi_replace("[^(-\.[:alnum:])]", '', $usernew->username);
126 if ($usernew->username !== $string) {
127 $err['username'] = get_string('alphanumerical');
128 }
129 }
ad6226fb 130 }
131 }
e8e0bb2d 132
133 if (!$user or $user->email !== $usernew->email) {
134 if (!validate_email($usernew->email)) {
135 $err['email'] = get_string('invalidemail');
136 } else if (record_exists('user', 'email', $usernew->email, 'mnethostid', $CFG->mnet_localhost_id)) {
137 $err['email'] = get_string('emailexists');
138 }
139 }
140
bb6d3d34 141 /// Next the customisable profile fields
142 $err += profile_validation($usernew);
143
e8e0bb2d 144 if (count($err) == 0){
145 return true;
146 } else {
147 return $err;
148 }
ad6226fb 149 }
150
151 function get_um() {
152 return $this->_upload_manager;
153 }
154}
155
156?>