MDL-64570 accessibility: purpose login fields
[moodle.git] / user / editlib.php
CommitLineData
8bdc9cac 1<?php
a2ed6e69
SH
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.
13//
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/>.
ad6226fb 16
a2ed6e69
SH
17/**
18 * This file contains function used when editing a users profile and preferences.
19 *
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
23 */
24
25/**
26 * Cancels the requirement for a user to update their email address.
27 *
28 * @param int $userid
29 */
05c38e2b 30function cancel_email_update($userid) {
31 unset_user_preference('newemail', $userid);
32 unset_user_preference('newemailkey', $userid);
33 unset_user_preference('newemailattemptsleft', $userid);
34}
35
479fa47d
DW
36/**
37 * Performs the common access checks and page setup for all
38 * user preference pages.
39 *
40 * @param int $userid The user id to edit taken from the page params.
41 * @param int $courseid The optional course id if we came from a course context.
42 * @return array containing the user and course records.
43 */
44function useredit_setup_preference_page($userid, $courseid) {
45 global $PAGE, $SESSION, $DB, $CFG, $OUTPUT, $USER;
46
47 // Guest can not edit.
48 if (isguestuser()) {
49 print_error('guestnoeditprofile');
50 }
51
52 if (!$course = $DB->get_record('course', array('id' => $courseid))) {
53 print_error('invalidcourseid');
54 }
55
56 if ($course->id != SITEID) {
57 require_login($course);
58 } else if (!isloggedin()) {
59 if (empty($SESSION->wantsurl)) {
672f4836 60 $SESSION->wantsurl = $CFG->wwwroot.'/user/preferences.php';
479fa47d
DW
61 }
62 redirect(get_login_url());
63 } else {
64 $PAGE->set_context(context_system::instance());
65 }
66
67 // The user profile we are editing.
68 if (!$user = $DB->get_record('user', array('id' => $userid))) {
69 print_error('invaliduserid');
70 }
71
72 // Guest can not be edited.
73 if (isguestuser($user)) {
74 print_error('guestnoeditprofile');
75 }
76
77 // Remote users cannot be edited.
78 if (is_mnet_remote_user($user)) {
8df850ad 79 if (user_not_fully_set_up($user, false)) {
479fa47d
DW
80 $hostwwwroot = $DB->get_field('mnet_host', 'wwwroot', array('id' => $user->mnethostid));
81 print_error('usernotfullysetup', 'mnet', '', $hostwwwroot);
82 }
83 redirect($CFG->wwwroot . "/user/view.php?course={$course->id}");
84 }
85
86 $systemcontext = context_system::instance();
87 $personalcontext = context_user::instance($user->id);
88
89 // Check access control.
90 if ($user->id == $USER->id) {
91 // Editing own profile - require_login() MUST NOT be used here, it would result in infinite loop!
92 if (!has_capability('moodle/user:editownprofile', $systemcontext)) {
93 print_error('cannotedityourprofile');
94 }
95
96 } else {
97 // Teachers, parents, etc.
98 require_capability('moodle/user:editprofile', $personalcontext);
45367bdf 99
479fa47d
DW
100 // No editing of primary admin!
101 if (is_siteadmin($user) and !is_siteadmin($USER)) { // Only admins may edit other admins.
102 print_error('useradmineditadmin');
103 }
104 }
105
106 if ($user->deleted) {
107 echo $OUTPUT->header();
108 echo $OUTPUT->heading(get_string('userdeleted'));
109 echo $OUTPUT->footer();
110 die;
111 }
112
113 $PAGE->set_pagelayout('admin');
114 $PAGE->set_context($personalcontext);
115 if ($USER->id != $user->id) {
116 $PAGE->navigation->extend_for_user($user);
117 } else {
118 if ($node = $PAGE->navigation->find('myprofile', navigation_node::TYPE_ROOTNODE)) {
119 $node->force_open();
120 }
121 }
122
123 return array($user, $course);
124}
125
a2ed6e69
SH
126/**
127 * Loads the given users preferences into the given user object.
128 *
129 * @param stdClass $user The user object, modified by reference.
130 * @param bool $reload
131 */
05c38e2b 132function useredit_load_preferences(&$user, $reload=true) {
133 global $USER;
14a6b7e1 134
05c38e2b 135 if (!empty($user->id)) {
136 if ($reload and $USER->id == $user->id) {
a2ed6e69 137 // Reload preferences in case it was changed in other session.
05c38e2b 138 unset($USER->preference);
139 }
aa6c1ced 140
05c38e2b 141 if ($preferences = get_user_preferences(null, null, $user->id)) {
a2ed6e69 142 foreach ($preferences as $name => $value) {
05c38e2b 143 $user->{'preference_'.$name} = $value;
144 }
14a6b7e1 145 }
146 }
147}
148
a2ed6e69 149/**
6e65554e 150 * Updates the user preferences for the given user
a2ed6e69 151 *
6e65554e
MG
152 * Only preference that can be updated directly will be updated here. This method is called from various WS
153 * updating users and should be used when updating user details. Plugins may whitelist preferences that can
154 * be updated by defining 'user_preferences' callback, {@see core_user::fill_preferences_cache()}
155 *
156 * Some parts of code may use user preference table to store internal data, in these cases it is acceptable
157 * to call set_user_preference()
158 *
159 * @param stdClass|array $usernew object or array that has user preferences as attributes with keys starting with preference_
a2ed6e69 160 */
14a6b7e1 161function useredit_update_user_preference($usernew) {
6e65554e 162 global $USER;
14a6b7e1 163 $ua = (array)$usernew;
6e65554e
MG
164 if (is_object($usernew) && isset($usernew->id) && isset($usernew->deleted) && isset($usernew->confirmed)) {
165 // This is already a full user object, maybe not completely full but these fields are enough.
166 $user = $usernew;
167 } else if (empty($ua['id']) || $ua['id'] == $USER->id) {
168 // We are updating current user.
169 $user = $USER;
170 } else {
171 // Retrieve user object.
172 $user = core_user::get_user($ua['id'], '*', MUST_EXIST);
173 }
174
a2ed6e69 175 foreach ($ua as $key => $value) {
14a6b7e1 176 if (strpos($key, 'preference_') === 0) {
177 $name = substr($key, strlen('preference_'));
6e65554e
MG
178 if (core_user::can_edit_preference($name, $user)) {
179 $value = core_user::clean_preference($value, $name);
180 set_user_preference($name, $value, $user->id);
181 }
14a6b7e1 182 }
183 }
184}
185
4125bdc1 186/**
f6094cd9 187 * @deprecated since Moodle 3.2
5407c5b0 188 * @see core_user::update_picture()
4125bdc1 189 */
f6094cd9
MG
190function useredit_update_picture() {
191 throw new coding_exception('useredit_update_picture() can not be used anymore. Please use ' .
192 'core_user::update_picture() instead.');
14a6b7e1 193}
194
a2ed6e69
SH
195/**
196 * Updates the user email bounce + send counts when the user is edited.
197 *
198 * @param stdClass $user The current user object.
199 * @param stdClass $usernew The updated user object.
200 */
14a6b7e1 201function useredit_update_bounces($user, $usernew) {
202 if (!isset($usernew->email)) {
a2ed6e69 203 // Locked field.
14a6b7e1 204 return;
d8734783 205 }
a9457b54 206 if (!isset($user->email) || $user->email !== $usernew->email) {
a2ed6e69
SH
207 set_bounce_count($usernew, true);
208 set_send_count($usernew, true);
14a6b7e1 209 }
210}
211
a2ed6e69
SH
212/**
213 * Updates the forums a user is tracking when the user is edited.
214 *
215 * @param stdClass $user The original user object.
216 * @param stdClass $usernew The updated user object.
217 */
14a6b7e1 218function useredit_update_trackforums($user, $usernew) {
219 global $CFG;
220 if (!isset($usernew->trackforums)) {
a2ed6e69 221 // Locked field.
14a6b7e1 222 return;
223 }
a9457b54 224 if ((!isset($user->trackforums) || ($usernew->trackforums != $user->trackforums)) and !$usernew->trackforums) {
14a6b7e1 225 require_once($CFG->dirroot.'/mod/forum/lib.php');
226 forum_tp_delete_read_records($usernew->id);
227 }
228}
229
a2ed6e69
SH
230/**
231 * Updates a users interests.
232 *
233 * @param stdClass $user
234 * @param array $interests
235 */
c060fc6a 236function useredit_update_interests($user, $interests) {
c4e868d5
MG
237 core_tag_tag::set_item_tags('core', 'user', $user->id,
238 context_user::instance($user->id), $interests);
1e1c51a3 239}
240
a2ed6e69
SH
241/**
242 * Powerful function that is used by edit and editadvanced to add common form elements/rules/etc.
243 *
244 * @param moodleform $mform
d6e7a63d
PS
245 * @param array $editoroptions
246 * @param array $filemanageroptions
247 * @param stdClass $user
a2ed6e69 248 */
d6e7a63d 249function useredit_shared_definition(&$mform, $editoroptions, $filemanageroptions, $user) {
d6ace123 250 global $CFG, $USER, $DB;
251
d6e7a63d
PS
252 if ($user->id > 0) {
253 useredit_load_preferences($user, false);
254 }
14a6b7e1 255
256 $strrequired = get_string('required');
e9c27d1e 257 $stringman = get_string_manager();
d8734783 258
1cc3a9ae
AG
259 // Add the necessary names.
260 foreach (useredit_get_required_name_fields() as $fullname) {
a327f25e 261 $mform->addElement('text', $fullname, get_string($fullname), 'maxlength="100" size="30"');
e9c27d1e
DM
262 if ($stringman->string_exists('missing'.$fullname, 'core')) {
263 $strmissingfield = get_string('missing'.$fullname, 'core');
264 } else {
265 $strmissingfield = $strrequired;
266 }
267 $mform->addRule($fullname, $strmissingfield, 'required', null, 'client');
a327f25e
AG
268 $mform->setType($fullname, PARAM_NOTAGS);
269 }
d8734783 270
1cc3a9ae
AG
271 $enabledusernamefields = useredit_get_enabled_name_fields();
272 // Add the enabled additional name fields.
273 foreach ($enabledusernamefields as $addname) {
a327f25e
AG
274 $mform->addElement('text', $addname, get_string($addname), 'maxlength="100" size="30"');
275 $mform->setType($addname, PARAM_NOTAGS);
276 }
d8734783 277
a2ed6e69 278 // Do not show email field if change confirmation is pending.
d6e7a63d 279 if ($user->id > 0 and !empty($CFG->emailchangeconfirmation) and !empty($user->preference_newemail)) {
c6a074f8 280 $notice = get_string('emailchangepending', 'auth', $user);
d6ace123 281 $notice .= '<br /><a href="edit.php?cancelemailchange=1&amp;id='.$user->id.'">'
c6a074f8 282 . get_string('emailchangecancel', 'auth') . '</a>';
d6ace123 283 $mform->addElement('static', 'emailpending', get_string('email'), $notice);
284 } else {
285 $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="30"');
286 $mform->addRule('email', $strrequired, 'required', null, 'client');
947ab40b 287 $mform->setType('email', PARAM_RAW_TRIMMED);
d6ace123 288 }
d8734783 289
479fa47d
DW
290 $choices = array();
291 $choices['0'] = get_string('emaildisplayno');
292 $choices['1'] = get_string('emaildisplayyes');
293 $choices['2'] = get_string('emaildisplaycourse');
294 $mform->addElement('select', 'maildisplay', get_string('emaildisplay'), $choices);
ac9768fc 295 $mform->setDefault('maildisplay', core_user::get_property_default('maildisplay'));
d2bba97d 296 $mform->addHelpButton('maildisplay', 'emaildisplay');
479fa47d 297
c5c0d2ff 298 $mform->addElement('text', 'city', get_string('city'), 'maxlength="120" size="21"');
071e68f9 299 $mform->setType('city', PARAM_TEXT);
fa7f750c
PS
300 if (!empty($CFG->defaultcity)) {
301 $mform->setDefault('city', $CFG->defaultcity);
302 }
d8734783 303
0aa759b0 304 $choices = get_string_manager()->get_list_of_countries();
a2ed6e69 305 $choices = array('' => get_string('selectacountry') . '...') + $choices;
d8734783 306 $mform->addElement('select', 'country', get_string('selectacountry'), $choices);
d8734783 307 if (!empty($CFG->country)) {
ac9768fc 308 $mform->setDefault('country', core_user::get_property_default('country'));
d8734783 309 }
310
d6e7a63d
PS
311 if (isset($CFG->forcetimezone) and $CFG->forcetimezone != 99) {
312 $choices = core_date::get_list_of_timezones($CFG->forcetimezone);
d8734783 313 $mform->addElement('static', 'forcedtimezone', get_string('timezone'), $choices[$CFG->forcetimezone]);
d6e7a63d 314 $mform->addElement('hidden', 'timezone');
ac9768fc 315 $mform->setType('timezone', core_user::get_property_type('timezone'));
d8734783 316 } else {
d6e7a63d 317 $choices = core_date::get_list_of_timezones($user->timezone, true);
d8734783 318 $mform->addElement('select', 'timezone', get_string('timezone'), $choices);
d8734783 319 }
320
b6e594b1
SA
321 if ($user->id < 0) {
322 $mform->addElement('select', 'lang', get_string('preferredlanguage'), get_string_manager()->get_list_of_translations());
323 $lang = empty($user->lang) ? $CFG->lang : $user->lang;
324 $mform->setDefault('lang', $lang);
325 }
326
d8734783 327 if (!empty($CFG->allowuserthemes)) {
ad6226fb 328 $choices = array();
d8734783 329 $choices[''] = get_string('default');
36798745 330 $themes = get_list_of_themes();
a2ed6e69 331 foreach ($themes as $key => $theme) {
36798745 332 if (empty($theme->hidefromselector)) {
d609d962 333 $choices[$key] = get_string('pluginname', 'theme_'.$theme->name);
36798745
SH
334 }
335 }
c3ed4a5a 336 $mform->addElement('select', 'theme', get_string('preferredtheme'), $choices);
d8734783 337 }
ad6226fb 338
8bdc9cac 339 $mform->addElement('editor', 'description_editor', get_string('userdescription'), null, $editoroptions);
067d0efe 340 $mform->setType('description_editor', PARAM_RAW);
27d1a27c 341 $mform->addHelpButton('description_editor', 'userdescription');
ad6226fb 342
689096bc 343 if (empty($USER->newadminuser)) {
757e89d2 344 $mform->addElement('header', 'moodle_picture', get_string('pictureofuser'));
80ef91ef 345 $mform->setExpanded('moodle_picture', true);
ad6226fb 346
4125bdc1 347 if (!empty($CFG->enablegravatar)) {
ed9e0cb6 348 $mform->addElement('html', html_writer::tag('p', get_string('gravatarenabled')));
4125bdc1
SH
349 }
350
d8734783 351 $mform->addElement('static', 'currentpicture', get_string('currentpicture'));
ad6226fb 352
febd7a62 353 $mform->addElement('checkbox', 'deletepicture', get_string('deletepicture'));
edfd6a5e 354 $mform->setDefault('deletepicture', 0);
ad6226fb 355
4e782b32 356 $mform->addElement('filemanager', 'imagefile', get_string('newpicture'), '', $filemanageroptions);
27d1a27c 357 $mform->addHelpButton('imagefile', 'newpicture');
ad6226fb 358
d8734783 359 $mform->addElement('text', 'imagealt', get_string('imagealt'), 'maxlength="100" size="30"');
071e68f9 360 $mform->setType('imagealt', PARAM_TEXT);
ad6226fb 361
d8734783 362 }
ad6226fb 363
1cc3a9ae
AG
364 // Display user name fields that are not currenlty enabled here if there are any.
365 $disabledusernamefields = useredit_get_disabled_name_fields($enabledusernamefields);
b63f542d 366 if (count($disabledusernamefields) > 0) {
a327f25e 367 $mform->addElement('header', 'moodle_additional_names', get_string('additionalnames'));
1cc3a9ae
AG
368 foreach ($disabledusernamefields as $allname) {
369 $mform->addElement('text', $allname, get_string($allname), 'maxlength="100" size="30"');
370 $mform->setType($allname, PARAM_NOTAGS);
a327f25e 371 }
a327f25e
AG
372 }
373
c4e868d5 374 if (core_tag_tag::is_enabled('core', 'user') and empty($USER->newadminuser)) {
1e1c51a3 375 $mform->addElement('header', 'moodle_interests', get_string('interests'));
c4e868d5 376 $mform->addElement('tags', 'interests', get_string('interestslist'),
4be9c7ad 377 array('itemtype' => 'user', 'component' => 'core'));
27d1a27c 378 $mform->addHelpButton('interests', 'interestslist');
1e1c51a3 379 }
d6ace123 380
a2ed6e69 381 // Moodle optional fields.
c3ed4a5a 382 $mform->addElement('header', 'moodle_optional', get_string('optional', 'form'));
ad6226fb 383
d8734783 384 $mform->addElement('text', 'url', get_string('webpage'), 'maxlength="255" size="50"');
ac9768fc 385 $mform->setType('url', core_user::get_property_type('url'));
ad6226fb 386
d8734783 387 $mform->addElement('text', 'icq', get_string('icqnumber'), 'maxlength="15" size="25"');
ac9768fc 388 $mform->setType('icq', core_user::get_property_type('icq'));
525ef9c8 389 $mform->setForceLtr('icq');
ad6226fb 390
d8734783 391 $mform->addElement('text', 'skype', get_string('skypeid'), 'maxlength="50" size="25"');
ac9768fc 392 $mform->setType('skype', core_user::get_property_type('skype'));
525ef9c8 393 $mform->setForceLtr('skype');
ad6226fb 394
d8734783 395 $mform->addElement('text', 'aim', get_string('aimid'), 'maxlength="50" size="25"');
ac9768fc 396 $mform->setType('aim', core_user::get_property_type('aim'));
525ef9c8 397 $mform->setForceLtr('aim');
ad6226fb 398
d8734783 399 $mform->addElement('text', 'yahoo', get_string('yahooid'), 'maxlength="50" size="25"');
ac9768fc 400 $mform->setType('yahoo', core_user::get_property_type('yahoo'));
525ef9c8 401 $mform->setForceLtr('yahoo');
ad6226fb 402
d8734783 403 $mform->addElement('text', 'msn', get_string('msnid'), 'maxlength="50" size="25"');
ac9768fc 404 $mform->setType('msn', core_user::get_property_type('msn'));
525ef9c8 405 $mform->setForceLtr('msn');
ad6226fb 406
8b9cfac4 407 $mform->addElement('text', 'idnumber', get_string('idnumber'), 'maxlength="255" size="25"');
ac9768fc 408 $mform->setType('idnumber', core_user::get_property_type('idnumber'));
ad6226fb 409
a8fd33b0 410 $mform->addElement('text', 'institution', get_string('institution'), 'maxlength="255" size="25"');
ac9768fc 411 $mform->setType('institution', core_user::get_property_type('institution'));
ad6226fb 412
a8fd33b0 413 $mform->addElement('text', 'department', get_string('department'), 'maxlength="255" size="25"');
ac9768fc 414 $mform->setType('department', core_user::get_property_type('department'));
ad6226fb 415
70fb46c8 416 $mform->addElement('text', 'phone1', get_string('phone1'), 'maxlength="20" size="25"');
ac9768fc 417 $mform->setType('phone1', core_user::get_property_type('phone1'));
525ef9c8 418 $mform->setForceLtr('phone1');
ad6226fb 419
55ac3d6f 420 $mform->addElement('text', 'phone2', get_string('phone2'), 'maxlength="20" size="25"');
ac9768fc 421 $mform->setType('phone2', core_user::get_property_type('phone2'));
525ef9c8 422 $mform->setForceLtr('phone2');
ad6226fb 423
a8fd33b0 424 $mform->addElement('text', 'address', get_string('address'), 'maxlength="255" size="25"');
ac9768fc 425 $mform->setType('address', core_user::get_property_type('address'));
14a6b7e1 426}
ad6226fb 427
1cc3a9ae
AG
428/**
429 * Return required user name fields for forms.
430 *
431 * @return array required user name fields in order according to settings.
432 */
433function useredit_get_required_name_fields() {
434 global $CFG;
435
436 // Get the name display format.
437 $nameformat = $CFG->fullnamedisplay;
438
439 // Names that are required fields on user forms.
440 $necessarynames = array('firstname', 'lastname');
441 $languageformat = get_string('fullnamedisplay');
442
443 // Check that the language string and the $nameformat contain the necessary names.
444 foreach ($necessarynames as $necessaryname) {
445 $pattern = "/$necessaryname\b/";
446 if (!preg_match($pattern, $languageformat)) {
447 // If the language string has been altered then fall back on the below order.
448 $languageformat = 'firstname lastname';
449 }
450 if (!preg_match($pattern, $nameformat)) {
451 // If the nameformat doesn't contain the necessary name fields then use the languageformat.
452 $nameformat = $languageformat;
453 }
454 }
455
456 // Order all of the name fields in the postion they are written in the fullnamedisplay setting.
457 $necessarynames = order_in_string($necessarynames, $nameformat);
458 return $necessarynames;
459}
460
461/**
462 * Gets enabled (from fullnameformate setting) user name fields in appropriate order.
463 *
464 * @return array Enabled user name fields.
465 */
466function useredit_get_enabled_name_fields() {
467 global $CFG;
468
469 // Get all of the other name fields which are not ranked as necessary.
470 $additionalusernamefields = array_diff(get_all_user_name_fields(), array('firstname', 'lastname'));
471 // Find out which additional name fields are actually being used from the fullnamedisplay setting.
472 $enabledadditionalusernames = array();
473 foreach ($additionalusernamefields as $enabledname) {
474 if (strpos($CFG->fullnamedisplay, $enabledname) !== false) {
475 $enabledadditionalusernames[] = $enabledname;
476 }
477 }
478
479 // Order all of the name fields in the postion they are written in the fullnamedisplay setting.
480 $enabledadditionalusernames = order_in_string($enabledadditionalusernames, $CFG->fullnamedisplay);
481 return $enabledadditionalusernames;
482}
483
484/**
485 * Gets user name fields not enabled from the setting fullnamedisplay.
486 *
487 * @param array $enabledadditionalusernames Current enabled additional user name fields.
488 * @return array Disabled user name fields.
489 */
490function useredit_get_disabled_name_fields($enabledadditionalusernames = null) {
491 // If we don't have enabled additional user name information then go and fetch it (try to avoid).
492 if (!isset($enabledadditionalusernames)) {
493 $enabledadditionalusernames = useredit_get_enabled_name_fields();
494 }
495
496 // These are the additional fields that are not currently enabled.
497 $nonusednamefields = array_diff(get_all_user_name_fields(),
498 array_merge(array('firstname', 'lastname'), $enabledadditionalusernames));
499 return $nonusednamefields;
500}