MDL-9318 Improving the maxbytes setting.
[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
e8e0bb2d 58 // user can not change own auth method
59 if ($userid == $USER->id) {
60 $mform->hardFreeze('auth');
61 $mform->hardFreeze('preference_auth_forcepasswordchange');
ad6226fb 62 }
63
64 // admin must choose some password and supply correct email
65 if (!empty($USER->newadminuser)) {
66 $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
67
b4c46a8b 68 $email_el =& $mform->getElement('email');
e8e0bb2d 69 if ($email_el->getValue() == 'root@localhost') {
70 $email_el->setValue('');
ad6226fb 71 }
72 }
73
e8e0bb2d 74 // require password for new users
75 if ($userid == -1) {
76 $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
77 }
78
79 // print picture
ad6226fb 80 if (!empty($CFG->gdversion)) {
b4c46a8b 81 $image_el =& $mform->getElement('currentpicture');
e8e0bb2d 82 if ($user and $user->picture) {
83 $image_el->setValue(print_user_picture($user->id, SITEID, $user->picture, 64, true, false, '', true));
84 } else {
85 $image_el->setValue(get_string('none'));
86 }
87 }
bb6d3d34 88
89 /// Next the customisable profile fields
90 profile_definition_after_data($mform);
e8e0bb2d 91 }
92
bb6d3d34 93 function validation($usernew) {
e8e0bb2d 94 global $CFG;
95
96 $usernew = (object)$usernew;
04f639a0 97 $usernew->username = trim($usernew->username);
e8e0bb2d 98
04f639a0 99 $user = get_record('user', 'id', $usernew->id);
100 $err = array();
101
102 if (empty($usernew->username)) {
103 //might be only whitespace
104 $err['username'] = get_string('required');
105 } else if (!$user or $user->username !== $usernew->username) {
e8e0bb2d 106 //check new username does not exist
107 if (record_exists('user', 'username', $usernew->username, 'mnethostid', $CFG->mnet_localhost_id)) {
108 $err['username'] = get_string('usernameexists');
109 }
110 //check allowed characters
111 if ($usernew->username !== moodle_strtolower($usernew->username)) {
112 $err['username'] = get_string('usernamelowercase');
ad6226fb 113 } else {
e8e0bb2d 114 if (empty($CFG->extendedusernamechars)) {
115 $string = eregi_replace("[^(-\.[:alnum:])]", '', $usernew->username);
116 if ($usernew->username !== $string) {
117 $err['username'] = get_string('alphanumerical');
118 }
119 }
ad6226fb 120 }
121 }
e8e0bb2d 122
123 if (!$user or $user->email !== $usernew->email) {
124 if (!validate_email($usernew->email)) {
125 $err['email'] = get_string('invalidemail');
126 } else if (record_exists('user', 'email', $usernew->email, 'mnethostid', $CFG->mnet_localhost_id)) {
127 $err['email'] = get_string('emailexists');
128 }
129 }
130
bb6d3d34 131 /// Next the customisable profile fields
132 $err += profile_validation($usernew);
133
e8e0bb2d 134 if (count($err) == 0){
135 return true;
136 } else {
137 return $err;
138 }
ad6226fb 139 }
140
141 function get_um() {
142 return $this->_upload_manager;
143 }
144}
145
146?>