Merge branch 'MDL-37181_m24' of https://github.com/markn86/moodle into MOODLE_24_STABLE
[moodle.git] / user / editadvanced_form.php
blobe376b5441e1961b0a0fc7a0424850743b83185cb
1 <?php
3 if (!defined('MOODLE_INTERNAL')) {
4 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
7 require_once($CFG->dirroot.'/lib/formslib.php');
9 class user_editadvanced_form extends moodleform {
11 // Define the form
12 function definition() {
13 global $USER, $CFG, $COURSE;
15 $mform =& $this->_form;
16 $editoroptions = null;
17 $filemanageroptions = null;
18 $userid = $USER->id;
20 if (is_array($this->_customdata)) {
21 if (array_key_exists('editoroptions', $this->_customdata)) {
22 $editoroptions = $this->_customdata['editoroptions'];
24 if (array_key_exists('filemanageroptions', $this->_customdata)) {
25 $filemanageroptions = $this->_customdata['filemanageroptions'];
27 if (array_key_exists('userid', $this->_customdata)) {
28 $userid = $this->_customdata['userid'];
32 //Accessibility: "Required" is bad legend text.
33 $strgeneral = get_string('general');
34 $strrequired = get_string('required');
36 /// Add some extra hidden fields
37 $mform->addElement('hidden', 'id');
38 $mform->setType('id', PARAM_INT);
39 $mform->addElement('hidden', 'course', $COURSE->id);
40 $mform->setType('course', PARAM_INT);
42 /// Print the required moodle fields first
43 $mform->addElement('header', 'moodle', $strgeneral);
45 $mform->addElement('text', 'username', get_string('username'), 'size="20"');
46 $mform->addRule('username', $strrequired, 'required', null, 'client');
47 $mform->setType('username', PARAM_RAW);
49 $auths = get_plugin_list('auth');
50 $auth_options = array();
51 foreach ($auths as $auth => $unused) {
52 $auth_options[$auth] = get_string('pluginname', "auth_{$auth}");
54 $mform->addElement('select', 'auth', get_string('chooseauthmethod','auth'), $auth_options);
55 $mform->addHelpButton('auth', 'chooseauthmethod', 'auth');
57 $mform->addElement('advcheckbox', 'suspended', get_string('suspended','auth'));
58 $mform->addHelpButton('suspended', 'suspended', 'auth');
60 if (!empty($CFG->passwordpolicy)){
61 $mform->addElement('static', 'passwordpolicyinfo', '', print_password_policy());
63 $mform->addElement('passwordunmask', 'newpassword', get_string('newpassword'), 'size="20"');
64 $mform->addHelpButton('newpassword', 'newpassword');
65 $mform->setType('newpassword', PARAM_RAW);
67 $mform->addElement('advcheckbox', 'preference_auth_forcepasswordchange', get_string('forcepasswordchange'));
68 $mform->addHelpButton('preference_auth_forcepasswordchange', 'forcepasswordchange');
69 /// shared fields
70 useredit_shared_definition($mform, $editoroptions, $filemanageroptions);
72 /// Next the customisable profile fields
73 profile_definition($mform, $userid);
75 if ($userid == -1) {
76 $btnstring = get_string('createuser');
77 } else {
78 $btnstring = get_string('updatemyprofile');
81 $this->add_action_buttons(false, $btnstring);
84 function definition_after_data() {
85 global $USER, $CFG, $DB, $OUTPUT;
87 $mform =& $this->_form;
88 if ($userid = $mform->getElementValue('id')) {
89 $user = $DB->get_record('user', array('id'=>$userid));
90 } else {
91 $user = false;
94 // if language does not exist, use site default lang
95 if ($langsel = $mform->getElementValue('lang')) {
96 $lang = reset($langsel);
97 // check lang exists
98 if (!get_string_manager()->translation_exists($lang, false)) {
99 $lang_el =& $mform->getElement('lang');
100 $lang_el->setValue($CFG->lang);
104 // user can not change own auth method
105 if ($userid == $USER->id) {
106 $mform->hardFreeze('auth');
107 $mform->hardFreeze('preference_auth_forcepasswordchange');
110 // admin must choose some password and supply correct email
111 if (!empty($USER->newadminuser)) {
112 $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
113 if ($mform->elementExists('suspended')) {
114 $mform->removeElement('suspended');
118 // require password for new users
119 if ($userid == -1) {
120 $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
123 if ($user and is_mnet_remote_user($user)) {
124 // only local accounts can be suspended
125 if ($mform->elementExists('suspended')) {
126 $mform->removeElement('suspended');
129 if ($user and ($user->id == $USER->id or is_siteadmin($user))) {
130 // prevent self and admin mess ups
131 if ($mform->elementExists('suspended')) {
132 $mform->hardFreeze('suspended');
136 // print picture
137 if (!empty($CFG->gdversion) and empty($USER->newadminuser)) {
138 if ($user) {
139 $context = context_user::instance($user->id, MUST_EXIST);
140 $fs = get_file_storage();
141 $hasuploadedpicture = ($fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.png') || $fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.jpg'));
142 if (!empty($user->picture) && $hasuploadedpicture) {
143 $imagevalue = $OUTPUT->user_picture($user, array('courseid' => SITEID, 'size'=>64));
144 } else {
145 $imagevalue = get_string('none');
147 } else {
148 $imagevalue = get_string('none');
150 $imageelement = $mform->getElement('currentpicture');
151 $imageelement->setValue($imagevalue);
153 if ($user && $mform->elementExists('deletepicture') && !$hasuploadedpicture) {
154 $mform->removeElement('deletepicture');
158 /// Next the customisable profile fields
159 profile_definition_after_data($mform, $userid);
162 function validation($usernew, $files) {
163 global $CFG, $DB;
165 $usernew = (object)$usernew;
166 $usernew->username = trim($usernew->username);
168 $user = $DB->get_record('user', array('id'=>$usernew->id));
169 $err = array();
171 if (!empty($usernew->newpassword)) {
172 $errmsg = '';//prevent eclipse warning
173 if (!check_password_policy($usernew->newpassword, $errmsg)) {
174 $err['newpassword'] = $errmsg;
178 if (empty($usernew->username)) {
179 //might be only whitespace
180 $err['username'] = get_string('required');
181 } else if (!$user or $user->username !== $usernew->username) {
182 //check new username does not exist
183 if ($DB->record_exists('user', array('username'=>$usernew->username, 'mnethostid'=>$CFG->mnet_localhost_id))) {
184 $err['username'] = get_string('usernameexists');
186 //check allowed characters
187 if ($usernew->username !== textlib::strtolower($usernew->username)) {
188 $err['username'] = get_string('usernamelowercase');
189 } else {
190 if ($usernew->username !== clean_param($usernew->username, PARAM_USERNAME)) {
191 $err['username'] = get_string('invalidusername');
196 if (!$user or $user->email !== $usernew->email) {
197 if (!validate_email($usernew->email)) {
198 $err['email'] = get_string('invalidemail');
199 } else if ($DB->record_exists('user', array('email'=>$usernew->email, 'mnethostid'=>$CFG->mnet_localhost_id))) {
200 $err['email'] = get_string('emailexists');
204 /// Next the customisable profile fields
205 $err += profile_validation($usernew, $files);
207 if (count($err) == 0){
208 return true;
209 } else {
210 return $err;