Merge branch 'MDL-52078-30' of git://github.com/cameron1729/moodle into MOODLE_30_STABLE
[moodle.git] / user / editadvanced_form.php
blob2dad585bfc1619a7dbb4c4439e851a36e2a32caa
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Form for editing a users profile
20 * @copyright 1999 Martin Dougiamas http://dougiamas.com
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22 * @package core_user
25 if (!defined('MOODLE_INTERNAL')) {
26 die('Direct access to this script is forbidden.'); // It must be included from a Moodle page.
29 require_once($CFG->dirroot.'/lib/formslib.php');
31 /**
32 * Class user_editadvanced_form.
34 * @copyright 1999 Martin Dougiamas http://dougiamas.com
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class user_editadvanced_form extends moodleform {
39 /**
40 * Define the form.
42 public function definition() {
43 global $USER, $CFG, $COURSE;
45 $mform = $this->_form;
46 $editoroptions = null;
47 $filemanageroptions = null;
49 if (!is_array($this->_customdata)) {
50 throw new coding_exception('invalid custom data for user_edit_form');
52 $editoroptions = $this->_customdata['editoroptions'];
53 $filemanageroptions = $this->_customdata['filemanageroptions'];
54 $user = $this->_customdata['user'];
55 $userid = $user->id;
57 // Accessibility: "Required" is bad legend text.
58 $strgeneral = get_string('general');
59 $strrequired = get_string('required');
61 // Add some extra hidden fields.
62 $mform->addElement('hidden', 'id');
63 $mform->setType('id', PARAM_INT);
64 $mform->addElement('hidden', 'course', $COURSE->id);
65 $mform->setType('course', PARAM_INT);
67 // Print the required moodle fields first.
68 $mform->addElement('header', 'moodle', $strgeneral);
70 $mform->addElement('text', 'username', get_string('username'), 'size="20"');
71 $mform->addRule('username', $strrequired, 'required', null, 'client');
72 $mform->setType('username', PARAM_RAW);
74 $auths = core_component::get_plugin_list('auth');
75 $enabled = get_string('pluginenabled', 'core_plugin');
76 $disabled = get_string('plugindisabled', 'core_plugin');
77 $authoptions = array($enabled => array(), $disabled => array());
78 $cannotchangepass = array();
79 foreach ($auths as $auth => $unused) {
80 $authinst = get_auth_plugin($auth);
81 $passwordurl = $authinst->change_password_url();
82 if (!($authinst->can_change_password() && empty($passwordurl))) {
83 if ($userid < 1 and $authinst->is_internal()) {
84 // This is unlikely but we can not create account without password
85 // when plugin uses passwords, we need to set it initially at least.
86 } else {
87 $cannotchangepass[] = $auth;
90 if (is_enabled_auth($auth)) {
91 $authoptions[$enabled][$auth] = get_string('pluginname', "auth_{$auth}");
92 } else {
93 $authoptions[$disabled][$auth] = get_string('pluginname', "auth_{$auth}");
96 $mform->addElement('selectgroups', 'auth', get_string('chooseauthmethod', 'auth'), $authoptions);
97 $mform->addHelpButton('auth', 'chooseauthmethod', 'auth');
99 $mform->addElement('advcheckbox', 'suspended', get_string('suspended', 'auth'));
100 $mform->addHelpButton('suspended', 'suspended', 'auth');
102 $mform->addElement('checkbox', 'createpassword', get_string('createpassword', 'auth'));
103 $mform->disabledIf('createpassword', 'auth', 'in', $cannotchangepass);
105 if (!empty($CFG->passwordpolicy)) {
106 $mform->addElement('static', 'passwordpolicyinfo', '', print_password_policy());
108 $mform->addElement('passwordunmask', 'newpassword', get_string('newpassword'), 'size="20"');
109 $mform->addHelpButton('newpassword', 'newpassword');
110 $mform->setType('newpassword', PARAM_RAW);
111 $mform->disabledIf('newpassword', 'createpassword', 'checked');
113 $mform->disabledIf('newpassword', 'auth', 'in', $cannotchangepass);
115 $mform->addElement('advcheckbox', 'preference_auth_forcepasswordchange', get_string('forcepasswordchange'));
116 $mform->addHelpButton('preference_auth_forcepasswordchange', 'forcepasswordchange');
117 $mform->disabledIf('preference_auth_forcepasswordchange', 'createpassword', 'checked');
119 // Shared fields.
120 useredit_shared_definition($mform, $editoroptions, $filemanageroptions, $user);
122 // Next the customisable profile fields.
123 profile_definition($mform, $userid);
125 if ($userid == -1) {
126 $btnstring = get_string('createuser');
127 } else {
128 $btnstring = get_string('updatemyprofile');
131 $this->add_action_buttons(false, $btnstring);
133 $this->set_data($user);
137 * Extend the form definition after data has been parsed.
139 public function definition_after_data() {
140 global $USER, $CFG, $DB, $OUTPUT;
142 $mform = $this->_form;
143 if ($userid = $mform->getElementValue('id')) {
144 $user = $DB->get_record('user', array('id' => $userid));
145 } else {
146 $user = false;
149 // User can not change own auth method.
150 if ($userid == $USER->id) {
151 $mform->hardFreeze('auth');
152 $mform->hardFreeze('preference_auth_forcepasswordchange');
155 // Admin must choose some password and supply correct email.
156 if (!empty($USER->newadminuser)) {
157 $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
158 if ($mform->elementExists('suspended')) {
159 $mform->removeElement('suspended');
163 // Require password for new users.
164 if ($userid > 0) {
165 if ($mform->elementExists('createpassword')) {
166 $mform->removeElement('createpassword');
170 if ($user and is_mnet_remote_user($user)) {
171 // Only local accounts can be suspended.
172 if ($mform->elementExists('suspended')) {
173 $mform->removeElement('suspended');
176 if ($user and ($user->id == $USER->id or is_siteadmin($user))) {
177 // Prevent self and admin mess ups.
178 if ($mform->elementExists('suspended')) {
179 $mform->hardFreeze('suspended');
183 // Print picture.
184 if (empty($USER->newadminuser)) {
185 if ($user) {
186 $context = context_user::instance($user->id, MUST_EXIST);
187 $fs = get_file_storage();
188 $hasuploadedpicture = ($fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.png') || $fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.jpg'));
189 if (!empty($user->picture) && $hasuploadedpicture) {
190 $imagevalue = $OUTPUT->user_picture($user, array('courseid' => SITEID, 'size' => 64));
191 } else {
192 $imagevalue = get_string('none');
194 } else {
195 $imagevalue = get_string('none');
197 $imageelement = $mform->getElement('currentpicture');
198 $imageelement->setValue($imagevalue);
200 if ($user && $mform->elementExists('deletepicture') && !$hasuploadedpicture) {
201 $mform->removeElement('deletepicture');
205 // Next the customisable profile fields.
206 profile_definition_after_data($mform, $userid);
210 * Validate the form data.
211 * @param array $usernew
212 * @param array $files
213 * @return array|bool
215 public function validation($usernew, $files) {
216 global $CFG, $DB;
218 $usernew = (object)$usernew;
219 $usernew->username = trim($usernew->username);
221 $user = $DB->get_record('user', array('id' => $usernew->id));
222 $err = array();
224 if (!$user and !empty($usernew->createpassword)) {
225 if ($usernew->suspended) {
226 // Show some error because we can not mail suspended users.
227 $err['suspended'] = get_string('error');
229 } else {
230 if (!empty($usernew->newpassword)) {
231 $errmsg = ''; // Prevent eclipse warning.
232 if (!check_password_policy($usernew->newpassword, $errmsg)) {
233 $err['newpassword'] = $errmsg;
235 } else if (!$user) {
236 $auth = get_auth_plugin($usernew->auth);
237 if ($auth->is_internal()) {
238 // Internal accounts require password!
239 $err['newpassword'] = get_string('required');
244 if (empty($usernew->username)) {
245 // Might be only whitespace.
246 $err['username'] = get_string('required');
247 } else if (!$user or $user->username !== $usernew->username) {
248 // Check new username does not exist.
249 if ($DB->record_exists('user', array('username' => $usernew->username, 'mnethostid' => $CFG->mnet_localhost_id))) {
250 $err['username'] = get_string('usernameexists');
252 // Check allowed characters.
253 if ($usernew->username !== core_text::strtolower($usernew->username)) {
254 $err['username'] = get_string('usernamelowercase');
255 } else {
256 if ($usernew->username !== clean_param($usernew->username, PARAM_USERNAME)) {
257 $err['username'] = get_string('invalidusername');
262 if (!$user or (isset($usernew->email) && $user->email !== $usernew->email)) {
263 if (!validate_email($usernew->email)) {
264 $err['email'] = get_string('invalidemail');
265 } else if (empty($CFG->allowaccountssameemail)
266 and $DB->record_exists('user', array('email' => $usernew->email, 'mnethostid' => $CFG->mnet_localhost_id))) {
267 $err['email'] = get_string('emailexists');
271 // Next the customisable profile fields.
272 $err += profile_validation($usernew, $files);
274 if (count($err) == 0) {
275 return true;
276 } else {
277 return $err;