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