MDL-49634 navigation: My grades now relevant to teachers.
[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
a2ed6e69
SH
36/**
37 * Loads the given users preferences into the given user object.
38 *
39 * @param stdClass $user The user object, modified by reference.
40 * @param bool $reload
41 */
05c38e2b 42function useredit_load_preferences(&$user, $reload=true) {
43 global $USER;
14a6b7e1 44
05c38e2b 45 if (!empty($user->id)) {
46 if ($reload and $USER->id == $user->id) {
a2ed6e69 47 // Reload preferences in case it was changed in other session.
05c38e2b 48 unset($USER->preference);
49 }
aa6c1ced 50
05c38e2b 51 if ($preferences = get_user_preferences(null, null, $user->id)) {
a2ed6e69 52 foreach ($preferences as $name => $value) {
05c38e2b 53 $user->{'preference_'.$name} = $value;
54 }
14a6b7e1 55 }
56 }
57}
58
a2ed6e69
SH
59/**
60 * Updates the user preferences for teh given user.
61 *
62 * @param stdClass|array $usernew
63 */
14a6b7e1 64function useredit_update_user_preference($usernew) {
65 $ua = (array)$usernew;
a2ed6e69 66 foreach ($ua as $key => $value) {
14a6b7e1 67 if (strpos($key, 'preference_') === 0) {
68 $name = substr($key, strlen('preference_'));
69 set_user_preference($name, $value, $usernew->id);
70 }
71 }
72}
73
4125bdc1 74/**
a2ed6e69 75 * Updates the provided users profile picture based upon the expected fields returned from the edit or edit_advanced forms.
4125bdc1
SH
76 *
77 * @global moodle_database $DB
78 * @param stdClass $usernew An object that contains some information about the user being updated
79 * @param moodleform $userform The form that was submitted to edit the form
a2ed6e69 80 * @param array $filemanageroptions
4125bdc1
SH
81 * @return bool True if the user was updated, false if it stayed the same.
82 */
4e782b32 83function useredit_update_picture(stdClass $usernew, moodleform $userform, $filemanageroptions = array()) {
a5d424df 84 global $CFG, $DB;
e88dd876 85 require_once("$CFG->libdir/gdlib.php");
9d85247d 86
43731030 87 $context = context_user::instance($usernew->id, MUST_EXIST);
a2ed6e69 88 $user = $DB->get_record('user', array('id' => $usernew->id), 'id, picture', MUST_EXIST);
4d254790
PS
89
90 $newpicture = $user->picture;
4e782b32
RT
91 // Get file_storage to process files.
92 $fs = get_file_storage();
4125bdc1 93 if (!empty($usernew->deletepicture)) {
a2ed6e69
SH
94 // The user has chosen to delete the selected users picture.
95 $fs->delete_area_files($context->id, 'user', 'icon'); // Drop all images in area.
4d254790
PS
96 $newpicture = 0;
97
4e782b32
RT
98 } else {
99 // Save newly uploaded file, this will avoid context mismatch for newly created users.
100 file_save_draft_area_files($usernew->imagefile, $context->id, 'user', 'newicon', 0, $filemanageroptions);
101 if (($iconfiles = $fs->get_area_files($context->id, 'user', 'newicon')) && count($iconfiles) == 2) {
a2ed6e69 102 // Get file which was uploaded in draft area.
4e782b32
RT
103 foreach ($iconfiles as $file) {
104 if (!$file->is_directory()) {
105 break;
106 }
107 }
a2ed6e69 108 // Copy file to temporary location and the send it for processing icon.
4e782b32 109 if ($iconfile = $file->copy_content_to_temp()) {
a2ed6e69 110 // There is a new image that has been uploaded.
4e782b32 111 // Process the new image and set the user to make use of it.
a2ed6e69 112 // NOTE: Uploaded images always take over Gravatar.
4e782b32 113 $newpicture = (int)process_new_icon($context, 'user', 'icon', 0, $iconfile);
a2ed6e69 114 // Delete temporary file.
4e782b32
RT
115 @unlink($iconfile);
116 // Remove uploaded file.
117 $fs->delete_area_files($context->id, 'user', 'newicon');
118 } else {
119 // Something went wrong while creating temp file.
120 // Remove uploaded file.
121 $fs->delete_area_files($context->id, 'user', 'newicon');
122 return false;
123 }
124 }
14a6b7e1 125 }
4125bdc1 126
4d254790
PS
127 if ($newpicture != $user->picture) {
128 $DB->set_field('user', 'picture', $newpicture, array('id' => $user->id));
4125bdc1 129 return true;
4d254790
PS
130 } else {
131 return false;
4125bdc1 132 }
14a6b7e1 133}
134
a2ed6e69
SH
135/**
136 * Updates the user email bounce + send counts when the user is edited.
137 *
138 * @param stdClass $user The current user object.
139 * @param stdClass $usernew The updated user object.
140 */
14a6b7e1 141function useredit_update_bounces($user, $usernew) {
142 if (!isset($usernew->email)) {
a2ed6e69 143 // Locked field.
14a6b7e1 144 return;
d8734783 145 }
a9457b54 146 if (!isset($user->email) || $user->email !== $usernew->email) {
a2ed6e69
SH
147 set_bounce_count($usernew, true);
148 set_send_count($usernew, true);
14a6b7e1 149 }
150}
151
a2ed6e69
SH
152/**
153 * Updates the forums a user is tracking when the user is edited.
154 *
155 * @param stdClass $user The original user object.
156 * @param stdClass $usernew The updated user object.
157 */
14a6b7e1 158function useredit_update_trackforums($user, $usernew) {
159 global $CFG;
160 if (!isset($usernew->trackforums)) {
a2ed6e69 161 // Locked field.
14a6b7e1 162 return;
163 }
a9457b54 164 if ((!isset($user->trackforums) || ($usernew->trackforums != $user->trackforums)) and !$usernew->trackforums) {
14a6b7e1 165 require_once($CFG->dirroot.'/mod/forum/lib.php');
166 forum_tp_delete_read_records($usernew->id);
167 }
168}
169
a2ed6e69
SH
170/**
171 * Updates a users interests.
172 *
173 * @param stdClass $user
174 * @param array $interests
175 */
c060fc6a 176function useredit_update_interests($user, $interests) {
cc033d48 177 tag_set('user', $user->id, $interests, 'core', context_user::instance($user->id)->id);
1e1c51a3 178}
179
a2ed6e69
SH
180/**
181 * Powerful function that is used by edit and editadvanced to add common form elements/rules/etc.
182 *
183 * @param moodleform $mform
d6e7a63d
PS
184 * @param array $editoroptions
185 * @param array $filemanageroptions
186 * @param stdClass $user
a2ed6e69 187 */
d6e7a63d 188function useredit_shared_definition(&$mform, $editoroptions, $filemanageroptions, $user) {
d6ace123 189 global $CFG, $USER, $DB;
190
d6e7a63d
PS
191 if ($user->id > 0) {
192 useredit_load_preferences($user, false);
193 }
14a6b7e1 194
195 $strrequired = get_string('required');
d8734783 196
1cc3a9ae
AG
197 // Add the necessary names.
198 foreach (useredit_get_required_name_fields() as $fullname) {
a327f25e 199 $mform->addElement('text', $fullname, get_string($fullname), 'maxlength="100" size="30"');
1cc3a9ae 200 $mform->addRule($fullname, $strrequired, 'required', null, 'client');
a327f25e
AG
201 $mform->setType($fullname, PARAM_NOTAGS);
202 }
d8734783 203
1cc3a9ae
AG
204 $enabledusernamefields = useredit_get_enabled_name_fields();
205 // Add the enabled additional name fields.
206 foreach ($enabledusernamefields as $addname) {
a327f25e
AG
207 $mform->addElement('text', $addname, get_string($addname), 'maxlength="100" size="30"');
208 $mform->setType($addname, PARAM_NOTAGS);
209 }
d8734783 210
a2ed6e69 211 // Do not show email field if change confirmation is pending.
d6e7a63d 212 if ($user->id > 0 and !empty($CFG->emailchangeconfirmation) and !empty($user->preference_newemail)) {
c6a074f8 213 $notice = get_string('emailchangepending', 'auth', $user);
d6ace123 214 $notice .= '<br /><a href="edit.php?cancelemailchange=1&amp;id='.$user->id.'">'
c6a074f8 215 . get_string('emailchangecancel', 'auth') . '</a>';
d6ace123 216 $mform->addElement('static', 'emailpending', get_string('email'), $notice);
217 } else {
218 $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="30"');
219 $mform->addRule('email', $strrequired, 'required', null, 'client');
947ab40b 220 $mform->setType('email', PARAM_RAW_TRIMMED);
d6ace123 221 }
d8734783 222
c5c0d2ff 223 $mform->addElement('text', 'city', get_string('city'), 'maxlength="120" size="21"');
071e68f9 224 $mform->setType('city', PARAM_TEXT);
fa7f750c
PS
225 if (!empty($CFG->defaultcity)) {
226 $mform->setDefault('city', $CFG->defaultcity);
227 }
d8734783 228
0aa759b0 229 $choices = get_string_manager()->get_list_of_countries();
a2ed6e69 230 $choices = array('' => get_string('selectacountry') . '...') + $choices;
d8734783 231 $mform->addElement('select', 'country', get_string('selectacountry'), $choices);
d8734783 232 if (!empty($CFG->country)) {
233 $mform->setDefault('country', $CFG->country);
234 }
235
d6e7a63d
PS
236 if (isset($CFG->forcetimezone) and $CFG->forcetimezone != 99) {
237 $choices = core_date::get_list_of_timezones($CFG->forcetimezone);
d8734783 238 $mform->addElement('static', 'forcedtimezone', get_string('timezone'), $choices[$CFG->forcetimezone]);
d6e7a63d
PS
239 $mform->addElement('hidden', 'timezone');
240 $mform->setType('timezone', PARAM_TIMEZONE);
d8734783 241 } else {
d6e7a63d 242 $choices = core_date::get_list_of_timezones($user->timezone, true);
d8734783 243 $mform->addElement('select', 'timezone', get_string('timezone'), $choices);
d8734783 244 }
245
2f00e1b2 246 // Multi-Calendar Support - see MDL-18375.
df5d27d8
MN
247 $calendartypes = \core_calendar\type_factory::get_list_of_calendar_types();
248 // We do not want to show this option unless there is more than one calendar type to display.
249 if (count($calendartypes) > 1) {
250 $mform->addElement('select', 'calendartype', get_string('preferredcalendar', 'calendar'), $calendartypes);
8bf0f207 251 $mform->setDefault('calendartype', $CFG->calendartype);
df5d27d8 252 }
6dd59aab 253
d8734783 254 if (!empty($CFG->allowuserthemes)) {
ad6226fb 255 $choices = array();
d8734783 256 $choices[''] = get_string('default');
36798745 257 $themes = get_list_of_themes();
a2ed6e69 258 foreach ($themes as $key => $theme) {
36798745 259 if (empty($theme->hidefromselector)) {
d609d962 260 $choices[$key] = get_string('pluginname', 'theme_'.$theme->name);
36798745
SH
261 }
262 }
c3ed4a5a 263 $mform->addElement('select', 'theme', get_string('preferredtheme'), $choices);
d8734783 264 }
ad6226fb 265
8bdc9cac
SH
266 $mform->addElement('editor', 'description_editor', get_string('userdescription'), null, $editoroptions);
267 $mform->setType('description_editor', PARAM_CLEANHTML);
27d1a27c 268 $mform->addHelpButton('description_editor', 'userdescription');
ad6226fb 269
7a0bf1bd
MG
270 $mform->addElement('header', 'moodle_userpreferences', get_string('preferences'));
271 useredit_shared_definition_preferences($user, $mform, $editoroptions, $filemanageroptions);
272
689096bc 273 if (empty($USER->newadminuser)) {
757e89d2 274 $mform->addElement('header', 'moodle_picture', get_string('pictureofuser'));
ad6226fb 275
4125bdc1 276 if (!empty($CFG->enablegravatar)) {
ed9e0cb6 277 $mform->addElement('html', html_writer::tag('p', get_string('gravatarenabled')));
4125bdc1
SH
278 }
279
d8734783 280 $mform->addElement('static', 'currentpicture', get_string('currentpicture'));
ad6226fb 281
d8734783 282 $mform->addElement('checkbox', 'deletepicture', get_string('delete'));
edfd6a5e 283 $mform->setDefault('deletepicture', 0);
ad6226fb 284
4e782b32 285 $mform->addElement('filemanager', 'imagefile', get_string('newpicture'), '', $filemanageroptions);
27d1a27c 286 $mform->addHelpButton('imagefile', 'newpicture');
ad6226fb 287
d8734783 288 $mform->addElement('text', 'imagealt', get_string('imagealt'), 'maxlength="100" size="30"');
071e68f9 289 $mform->setType('imagealt', PARAM_TEXT);
ad6226fb 290
d8734783 291 }
ad6226fb 292
1cc3a9ae
AG
293 // Display user name fields that are not currenlty enabled here if there are any.
294 $disabledusernamefields = useredit_get_disabled_name_fields($enabledusernamefields);
b63f542d 295 if (count($disabledusernamefields) > 0) {
a327f25e 296 $mform->addElement('header', 'moodle_additional_names', get_string('additionalnames'));
1cc3a9ae
AG
297 foreach ($disabledusernamefields as $allname) {
298 $mform->addElement('text', $allname, get_string($allname), 'maxlength="100" size="30"');
299 $mform->setType($allname, PARAM_NOTAGS);
a327f25e 300 }
a327f25e
AG
301 }
302
de2d81fa 303 if (!empty($CFG->usetags) and empty($USER->newadminuser)) {
1e1c51a3 304 $mform->addElement('header', 'moodle_interests', get_string('interests'));
c060fc6a 305 $mform->addElement('tags', 'interests', get_string('interestslist'), array('display' => 'noofficial'));
27d1a27c 306 $mform->addHelpButton('interests', 'interestslist');
1e1c51a3 307 }
d6ace123 308
a2ed6e69 309 // Moodle optional fields.
c3ed4a5a 310 $mform->addElement('header', 'moodle_optional', get_string('optional', 'form'));
ad6226fb 311
d8734783 312 $mform->addElement('text', 'url', get_string('webpage'), 'maxlength="255" size="50"');
313 $mform->setType('url', PARAM_URL);
ad6226fb 314
d8734783 315 $mform->addElement('text', 'icq', get_string('icqnumber'), 'maxlength="15" size="25"');
93de0ac2 316 $mform->setType('icq', PARAM_NOTAGS);
ad6226fb 317
d8734783 318 $mform->addElement('text', 'skype', get_string('skypeid'), 'maxlength="50" size="25"');
93de0ac2 319 $mform->setType('skype', PARAM_NOTAGS);
ad6226fb 320
d8734783 321 $mform->addElement('text', 'aim', get_string('aimid'), 'maxlength="50" size="25"');
93de0ac2 322 $mform->setType('aim', PARAM_NOTAGS);
ad6226fb 323
d8734783 324 $mform->addElement('text', 'yahoo', get_string('yahooid'), 'maxlength="50" size="25"');
93de0ac2 325 $mform->setType('yahoo', PARAM_NOTAGS);
ad6226fb 326
d8734783 327 $mform->addElement('text', 'msn', get_string('msnid'), 'maxlength="50" size="25"');
93de0ac2 328 $mform->setType('msn', PARAM_NOTAGS);
ad6226fb 329
8b9cfac4 330 $mform->addElement('text', 'idnumber', get_string('idnumber'), 'maxlength="255" size="25"');
93de0ac2 331 $mform->setType('idnumber', PARAM_NOTAGS);
ad6226fb 332
a8fd33b0 333 $mform->addElement('text', 'institution', get_string('institution'), 'maxlength="255" size="25"');
071e68f9 334 $mform->setType('institution', PARAM_TEXT);
ad6226fb 335
a8fd33b0 336 $mform->addElement('text', 'department', get_string('department'), 'maxlength="255" size="25"');
071e68f9 337 $mform->setType('department', PARAM_TEXT);
ad6226fb 338
d8734783 339 $mform->addElement('text', 'phone1', get_string('phone'), 'maxlength="20" size="25"');
93de0ac2 340 $mform->setType('phone1', PARAM_NOTAGS);
ad6226fb 341
55ac3d6f 342 $mform->addElement('text', 'phone2', get_string('phone2'), 'maxlength="20" size="25"');
93de0ac2 343 $mform->setType('phone2', PARAM_NOTAGS);
ad6226fb 344
a8fd33b0 345 $mform->addElement('text', 'address', get_string('address'), 'maxlength="255" size="25"');
071e68f9 346 $mform->setType('address', PARAM_TEXT);
14a6b7e1 347}
ad6226fb 348
7a0bf1bd
MG
349/**
350 * Adds user preferences elements to user edit form.
351 *
352 * @param stdClass $user
353 * @param moodleform $mform
354 * @param array|null $editoroptions
355 * @param array|null $filemanageroptions
356 */
357function useredit_shared_definition_preferences($user, &$mform, $editoroptions = null, $filemanageroptions = null) {
358 global $CFG;
359
360 $choices = array();
361 $choices['0'] = get_string('emaildisplayno');
362 $choices['1'] = get_string('emaildisplayyes');
363 $choices['2'] = get_string('emaildisplaycourse');
364 $mform->addElement('select', 'maildisplay', get_string('emaildisplay'), $choices);
747dac69 365 $mform->setDefault('maildisplay', $CFG->defaultpreference_maildisplay);
7a0bf1bd
MG
366
367 $choices = array();
368 $choices['0'] = get_string('textformat');
369 $choices['1'] = get_string('htmlformat');
370 $mform->addElement('select', 'mailformat', get_string('emailformat'), $choices);
747dac69 371 $mform->setDefault('mailformat', $CFG->defaultpreference_mailformat);
7a0bf1bd
MG
372
373 if (!empty($CFG->allowusermailcharset)) {
374 $choices = array();
375 $charsets = get_list_of_charsets();
376 if (!empty($CFG->sitemailcharset)) {
377 $choices['0'] = get_string('site').' ('.$CFG->sitemailcharset.')';
378 } else {
379 $choices['0'] = get_string('site').' (UTF-8)';
380 }
381 $choices = array_merge($choices, $charsets);
382 $mform->addElement('select', 'preference_mailcharset', get_string('emailcharset'), $choices);
383 }
384
385 $choices = array();
386 $choices['0'] = get_string('emaildigestoff');
387 $choices['1'] = get_string('emaildigestcomplete');
388 $choices['2'] = get_string('emaildigestsubjects');
389 $mform->addElement('select', 'maildigest', get_string('emaildigest'), $choices);
747dac69 390 $mform->setDefault('maildigest', $CFG->defaultpreference_maildigest);
7a0bf1bd
MG
391 $mform->addHelpButton('maildigest', 'emaildigest');
392
393 $choices = array();
394 $choices['1'] = get_string('autosubscribeyes');
395 $choices['0'] = get_string('autosubscribeno');
396 $mform->addElement('select', 'autosubscribe', get_string('autosubscribe'), $choices);
747dac69 397 $mform->setDefault('autosubscribe', $CFG->defaultpreference_autosubscribe);
7a0bf1bd
MG
398
399 if (!empty($CFG->forum_trackreadposts)) {
400 $choices = array();
401 $choices['0'] = get_string('trackforumsno');
402 $choices['1'] = get_string('trackforumsyes');
403 $mform->addElement('select', 'trackforums', get_string('trackforums'), $choices);
747dac69 404 $mform->setDefault('trackforums', $CFG->defaultpreference_trackforums);
7a0bf1bd
MG
405 }
406
407 $editors = editors_get_enabled();
408 if (count($editors) > 1) {
409 $choices = array('' => get_string('defaulteditor'));
410 $firsteditor = '';
411 foreach (array_keys($editors) as $editor) {
412 if (!$firsteditor) {
413 $firsteditor = $editor;
414 }
415 $choices[$editor] = get_string('pluginname', 'editor_' . $editor);
416 }
417 $mform->addElement('select', 'preference_htmleditor', get_string('textediting'), $choices);
418 $mform->setDefault('preference_htmleditor', '');
419 } else {
420 // Empty string means use the first chosen text editor.
421 $mform->addElement('hidden', 'preference_htmleditor');
422 $mform->setDefault('preference_htmleditor', '');
423 $mform->setType('preference_htmleditor', PARAM_PLUGIN);
424 }
425
426 $mform->addElement('select', 'lang', get_string('preferredlanguage'), get_string_manager()->get_list_of_translations());
427 $mform->setDefault('lang', $CFG->lang);
428
429}
430
1cc3a9ae
AG
431/**
432 * Return required user name fields for forms.
433 *
434 * @return array required user name fields in order according to settings.
435 */
436function useredit_get_required_name_fields() {
437 global $CFG;
438
439 // Get the name display format.
440 $nameformat = $CFG->fullnamedisplay;
441
442 // Names that are required fields on user forms.
443 $necessarynames = array('firstname', 'lastname');
444 $languageformat = get_string('fullnamedisplay');
445
446 // Check that the language string and the $nameformat contain the necessary names.
447 foreach ($necessarynames as $necessaryname) {
448 $pattern = "/$necessaryname\b/";
449 if (!preg_match($pattern, $languageformat)) {
450 // If the language string has been altered then fall back on the below order.
451 $languageformat = 'firstname lastname';
452 }
453 if (!preg_match($pattern, $nameformat)) {
454 // If the nameformat doesn't contain the necessary name fields then use the languageformat.
455 $nameformat = $languageformat;
456 }
457 }
458
459 // Order all of the name fields in the postion they are written in the fullnamedisplay setting.
460 $necessarynames = order_in_string($necessarynames, $nameformat);
461 return $necessarynames;
462}
463
464/**
465 * Gets enabled (from fullnameformate setting) user name fields in appropriate order.
466 *
467 * @return array Enabled user name fields.
468 */
469function useredit_get_enabled_name_fields() {
470 global $CFG;
471
472 // Get all of the other name fields which are not ranked as necessary.
473 $additionalusernamefields = array_diff(get_all_user_name_fields(), array('firstname', 'lastname'));
474 // Find out which additional name fields are actually being used from the fullnamedisplay setting.
475 $enabledadditionalusernames = array();
476 foreach ($additionalusernamefields as $enabledname) {
477 if (strpos($CFG->fullnamedisplay, $enabledname) !== false) {
478 $enabledadditionalusernames[] = $enabledname;
479 }
480 }
481
482 // Order all of the name fields in the postion they are written in the fullnamedisplay setting.
483 $enabledadditionalusernames = order_in_string($enabledadditionalusernames, $CFG->fullnamedisplay);
484 return $enabledadditionalusernames;
485}
486
487/**
488 * Gets user name fields not enabled from the setting fullnamedisplay.
489 *
490 * @param array $enabledadditionalusernames Current enabled additional user name fields.
491 * @return array Disabled user name fields.
492 */
493function useredit_get_disabled_name_fields($enabledadditionalusernames = null) {
494 // If we don't have enabled additional user name information then go and fetch it (try to avoid).
495 if (!isset($enabledadditionalusernames)) {
496 $enabledadditionalusernames = useredit_get_enabled_name_fields();
497 }
498
499 // These are the additional fields that are not currently enabled.
500 $nonusednamefields = array_diff(get_all_user_name_fields(),
501 array_merge(array('firstname', 'lastname'), $enabledadditionalusernames));
502 return $nonusednamefields;
503}