weekly release 2.6dev
[moodle.git] / user / editadvanced_form.php
CommitLineData
aa6c1ced 1<?php
ad6226fb 2
bfebaf64
MD
3if (!defined('MOODLE_INTERNAL')) {
4 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
5}
6
ad6226fb 7require_once($CFG->dirroot.'/lib/formslib.php');
ad6226fb 8
9class user_editadvanced_form extends moodleform {
10
11 // Define the form
bb6d3d34 12 function definition() {
d8734783 13 global $USER, $CFG, $COURSE;
ad6226fb 14
15 $mform =& $this->_form;
fc3aa0fb
RT
16 $editoroptions = null;
17 $filemanageroptions = null;
18 $userid = $USER->id;
19
20 if (is_array($this->_customdata)) {
21 if (array_key_exists('editoroptions', $this->_customdata)) {
22 $editoroptions = $this->_customdata['editoroptions'];
23 }
24 if (array_key_exists('filemanageroptions', $this->_customdata)) {
25 $filemanageroptions = $this->_customdata['filemanageroptions'];
26 }
27 if (array_key_exists('userid', $this->_customdata)) {
28 $userid = $this->_customdata['userid'];
29 }
4e782b32 30 }
8bdc9cac 31
c8149f0f 32 //Accessibility: "Required" is bad legend text.
33 $strgeneral = get_string('general');
ad6226fb 34 $strrequired = get_string('required');
35
36 /// Add some extra hidden fields
37 $mform->addElement('hidden', 'id');
d18e0fe6 38 $mform->setType('id', PARAM_INT);
d8734783 39 $mform->addElement('hidden', 'course', $COURSE->id);
d18e0fe6 40 $mform->setType('course', PARAM_INT);
ad6226fb 41
42 /// Print the required moodle fields first
c8149f0f 43 $mform->addElement('header', 'moodle', $strgeneral);
ad6226fb 44
45 $mform->addElement('text', 'username', get_string('username'), 'size="20"');
46 $mform->addRule('username', $strrequired, 'required', null, 'client');
47 $mform->setType('username', PARAM_RAW);
48
17da2e6f 49 $auths = get_plugin_list('auth');
c5772ede
PS
50 $enabled = get_string('pluginenabled', 'core_plugin');
51 $disabled = get_string('plugindisabled', 'core_plugin');
52 $auth_options = array($enabled=>array(), $disabled=>array());
17da2e6f 53 foreach ($auths as $auth => $unused) {
c5772ede
PS
54 if (is_enabled_auth($auth)) {
55 $auth_options[$enabled][$auth] = get_string('pluginname', "auth_{$auth}");
56 } else {
57 $auth_options[$disabled][$auth] = get_string('pluginname', "auth_{$auth}");
58 }
ad6226fb 59 }
c5772ede 60 $mform->addElement('selectgroups', 'auth', get_string('chooseauthmethod','auth'), $auth_options);
f728d49b 61 $mform->addHelpButton('auth', 'chooseauthmethod', 'auth');
ad6226fb 62
4ad72c28
PS
63 $mform->addElement('advcheckbox', 'suspended', get_string('suspended','auth'));
64 $mform->addHelpButton('suspended', 'suspended', 'auth');
65
3bf11ca4
DM
66 if (!empty($CFG->passwordpolicy)){
67 $mform->addElement('static', 'passwordpolicyinfo', '', print_password_policy());
851481dc 68 }
54bb33eb 69 $mform->addElement('passwordunmask', 'newpassword', get_string('newpassword'), 'size="20"');
27d1a27c 70 $mform->addHelpButton('newpassword', 'newpassword');
ad6226fb 71 $mform->setType('newpassword', PARAM_RAW);
ad6226fb 72
fa1d88eb 73 $mform->addElement('advcheckbox', 'preference_auth_forcepasswordchange', get_string('forcepasswordchange'));
27d1a27c 74 $mform->addHelpButton('preference_auth_forcepasswordchange', 'forcepasswordchange');
14a6b7e1 75 /// shared fields
4e782b32 76 useredit_shared_definition($mform, $editoroptions, $filemanageroptions);
ad6226fb 77
bb6d3d34 78 /// Next the customisable profile fields
fc3aa0fb 79 profile_definition($mform, $userid);
bb6d3d34 80
607fdfc3
AA
81 if ($userid == -1) {
82 $btnstring = get_string('createuser');
83 } else {
84 $btnstring = get_string('updatemyprofile');
85 }
86
87 $this->add_action_buttons(false, $btnstring);
ad6226fb 88 }
89
90 function definition_after_data() {
f2f085ee 91 global $USER, $CFG, $DB, $OUTPUT;
ad6226fb 92
93 $mform =& $this->_form;
9122137f 94 if ($userid = $mform->getElementValue('id')) {
5d910388 95 $user = $DB->get_record('user', array('id'=>$userid));
9122137f 96 } else {
97 $user = false;
98 }
ad6226fb 99
1545d7f0 100 // if language does not exist, use site default lang
101 if ($langsel = $mform->getElementValue('lang')) {
102 $lang = reset($langsel);
eb309bed 103 // check lang exists
96de38c3 104 if (!get_string_manager()->translation_exists($lang, false)) {
1545d7f0 105 $lang_el =& $mform->getElement('lang');
106 $lang_el->setValue($CFG->lang);
107 }
108 }
109
e8e0bb2d 110 // user can not change own auth method
111 if ($userid == $USER->id) {
112 $mform->hardFreeze('auth');
113 $mform->hardFreeze('preference_auth_forcepasswordchange');
ad6226fb 114 }
115
116 // admin must choose some password and supply correct email
117 if (!empty($USER->newadminuser)) {
118 $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
4ad72c28
PS
119 if ($mform->elementExists('suspended')) {
120 $mform->removeElement('suspended');
121 }
ad6226fb 122 }
123
e8e0bb2d 124 // require password for new users
125 if ($userid == -1) {
126 $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
127 }
128
4ad72c28
PS
129 if ($user and is_mnet_remote_user($user)) {
130 // only local accounts can be suspended
131 if ($mform->elementExists('suspended')) {
132 $mform->removeElement('suspended');
133 }
134 }
135 if ($user and ($user->id == $USER->id or is_siteadmin($user))) {
136 // prevent self and admin mess ups
137 if ($mform->elementExists('suspended')) {
138 $mform->hardFreeze('suspended');
139 }
140 }
141
e8e0bb2d 142 // print picture
689096bc 143 if (empty($USER->newadminuser)) {
4125bdc1 144 if ($user) {
43731030 145 $context = context_user::instance($user->id, MUST_EXIST);
4125bdc1 146 $fs = get_file_storage();
ed9e0cb6 147 $hasuploadedpicture = ($fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.png') || $fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.jpg'));
4125bdc1
SH
148 if (!empty($user->picture) && $hasuploadedpicture) {
149 $imagevalue = $OUTPUT->user_picture($user, array('courseid' => SITEID, 'size'=>64));
150 } else {
151 $imagevalue = get_string('none');
152 }
e8e0bb2d 153 } else {
4125bdc1
SH
154 $imagevalue = get_string('none');
155 }
156 $imageelement = $mform->getElement('currentpicture');
157 $imageelement->setValue($imagevalue);
158
159 if ($user && $mform->elementExists('deletepicture') && !$hasuploadedpicture) {
160 $mform->removeElement('deletepicture');
e8e0bb2d 161 }
162 }
bb6d3d34 163
164 /// Next the customisable profile fields
63466095 165 profile_definition_after_data($mform, $userid);
e8e0bb2d 166 }
167
6b8ad965 168 function validation($usernew, $files) {
5d910388 169 global $CFG, $DB;
e8e0bb2d 170
171 $usernew = (object)$usernew;
f728d49b
PS
172 $usernew->username = trim($usernew->username);
173
174 $user = $DB->get_record('user', array('id'=>$usernew->id));
04f639a0 175 $err = array();
176
83022298 177 if (!empty($usernew->newpassword)) {
178 $errmsg = '';//prevent eclipse warning
179 if (!check_password_policy($usernew->newpassword, $errmsg)) {
180 $err['newpassword'] = $errmsg;
181 }
182 }
183
04f639a0 184 if (empty($usernew->username)) {
185 //might be only whitespace
186 $err['username'] = get_string('required');
187 } else if (!$user or $user->username !== $usernew->username) {
e8e0bb2d 188 //check new username does not exist
5d910388 189 if ($DB->record_exists('user', array('username'=>$usernew->username, 'mnethostid'=>$CFG->mnet_localhost_id))) {
e8e0bb2d 190 $err['username'] = get_string('usernameexists');
191 }
f728d49b 192 //check allowed characters
6f3451e5 193 if ($usernew->username !== textlib::strtolower($usernew->username)) {
e8e0bb2d 194 $err['username'] = get_string('usernamelowercase');
6b8ad965
PS
195 } else {
196 if ($usernew->username !== clean_param($usernew->username, PARAM_USERNAME)) {
f4065447 197 $err['username'] = get_string('invalidusername');
e8e0bb2d 198 }
ad6226fb 199 }
200 }
e8e0bb2d 201
202 if (!$user or $user->email !== $usernew->email) {
203 if (!validate_email($usernew->email)) {
204 $err['email'] = get_string('invalidemail');
5d910388 205 } else if ($DB->record_exists('user', array('email'=>$usernew->email, 'mnethostid'=>$CFG->mnet_localhost_id))) {
e8e0bb2d 206 $err['email'] = get_string('emailexists');
207 }
208 }
209
bb6d3d34 210 /// Next the customisable profile fields
c8969c22 211 $err += profile_validation($usernew, $files);
bb6d3d34 212
e8e0bb2d 213 if (count($err) == 0){
214 return true;
215 } else {
216 return $err;
217 }
ad6226fb 218 }
ad6226fb 219}
220
aa6c1ced 221