2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * This file contains function used when editing a users profile and preferences.
20 * @copyright 1999 Martin Dougiamas http://dougiamas.com
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 * Cancels the requirement for a user to update their email address.
30 function cancel_email_update($userid) {
31 unset_user_preference('newemail', $userid);
32 unset_user_preference('newemailkey', $userid);
33 unset_user_preference('newemailattemptsleft', $userid);
37 * Performs the common access checks and page setup for all
38 * user preference pages.
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.
44 function useredit_setup_preference_page($userid, $courseid) {
45 global $PAGE, $SESSION, $DB, $CFG, $OUTPUT, $USER;
47 // Guest can not edit.
49 print_error('guestnoeditprofile');
52 if (!$course = $DB->get_record('course', array('id' => $courseid))) {
53 print_error('invalidcourseid');
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';
62 redirect(get_login_url());
64 $PAGE->set_context(context_system
::instance());
67 // The user profile we are editing.
68 if (!$user = $DB->get_record('user', array('id' => $userid))) {
69 print_error('invaliduserid');
72 // Guest can not be edited.
73 if (isguestuser($user)) {
74 print_error('guestnoeditprofile');
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);
83 redirect($CFG->wwwroot
. "/user/view.php?course={$course->id}");
86 $systemcontext = context_system
::instance();
87 $personalcontext = context_user
::instance($user->id
);
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');
97 // Teachers, parents, etc.
98 require_capability('moodle/user:editprofile', $personalcontext);
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');
106 if ($user->deleted
) {
107 echo $OUTPUT->header();
108 echo $OUTPUT->heading(get_string('userdeleted'));
109 echo $OUTPUT->footer();
113 $PAGE->set_pagelayout('admin');
114 $PAGE->set_context($personalcontext);
115 if ($USER->id
!= $user->id
) {
116 $PAGE->navigation
->extend_for_user($user);
118 if ($node = $PAGE->navigation
->find('myprofile', navigation_node
::TYPE_ROOTNODE
)) {
123 return array($user, $course);
127 * Loads the given users preferences into the given user object.
129 * @param stdClass $user The user object, modified by reference.
130 * @param bool $reload
132 function useredit_load_preferences(&$user, $reload=true) {
135 if (!empty($user->id
)) {
136 if ($reload and $USER->id
== $user->id
) {
137 // Reload preferences in case it was changed in other session.
138 unset($USER->preference
);
141 if ($preferences = get_user_preferences(null, null, $user->id
)) {
142 foreach ($preferences as $name => $value) {
143 $user->{'preference_'.$name} = $value;
150 * Updates the user preferences for teh given user.
152 * @param stdClass|array $usernew
154 function useredit_update_user_preference($usernew) {
155 $ua = (array)$usernew;
156 foreach ($ua as $key => $value) {
157 if (strpos($key, 'preference_') === 0) {
158 $name = substr($key, strlen('preference_'));
159 set_user_preference($name, $value, $usernew->id
);
165 * Updates the provided users profile picture based upon the expected fields returned from the edit or edit_advanced forms.
167 * @global moodle_database $DB
168 * @param stdClass $usernew An object that contains some information about the user being updated
169 * @param moodleform $userform The form that was submitted to edit the form
170 * @param array $filemanageroptions
171 * @return bool True if the user was updated, false if it stayed the same.
173 function useredit_update_picture(stdClass
$usernew, moodleform
$userform, $filemanageroptions = array()) {
175 require_once("$CFG->libdir/gdlib.php");
177 $context = context_user
::instance($usernew->id
, MUST_EXIST
);
178 $user = $DB->get_record('user', array('id' => $usernew->id
), 'id, picture', MUST_EXIST
);
180 $newpicture = $user->picture
;
181 // Get file_storage to process files.
182 $fs = get_file_storage();
183 if (!empty($usernew->deletepicture
)) {
184 // The user has chosen to delete the selected users picture.
185 $fs->delete_area_files($context->id
, 'user', 'icon'); // Drop all images in area.
189 // Save newly uploaded file, this will avoid context mismatch for newly created users.
190 file_save_draft_area_files($usernew->imagefile
, $context->id
, 'user', 'newicon', 0, $filemanageroptions);
191 if (($iconfiles = $fs->get_area_files($context->id
, 'user', 'newicon')) && count($iconfiles) == 2) {
192 // Get file which was uploaded in draft area.
193 foreach ($iconfiles as $file) {
194 if (!$file->is_directory()) {
198 // Copy file to temporary location and the send it for processing icon.
199 if ($iconfile = $file->copy_content_to_temp()) {
200 // There is a new image that has been uploaded.
201 // Process the new image and set the user to make use of it.
202 // NOTE: Uploaded images always take over Gravatar.
203 $newpicture = (int)process_new_icon($context, 'user', 'icon', 0, $iconfile);
204 // Delete temporary file.
206 // Remove uploaded file.
207 $fs->delete_area_files($context->id
, 'user', 'newicon');
209 // Something went wrong while creating temp file.
210 // Remove uploaded file.
211 $fs->delete_area_files($context->id
, 'user', 'newicon');
217 if ($newpicture != $user->picture
) {
218 $DB->set_field('user', 'picture', $newpicture, array('id' => $user->id
));
226 * Updates the user email bounce + send counts when the user is edited.
228 * @param stdClass $user The current user object.
229 * @param stdClass $usernew The updated user object.
231 function useredit_update_bounces($user, $usernew) {
232 if (!isset($usernew->email
)) {
236 if (!isset($user->email
) ||
$user->email
!== $usernew->email
) {
237 set_bounce_count($usernew, true);
238 set_send_count($usernew, true);
243 * Updates the forums a user is tracking when the user is edited.
245 * @param stdClass $user The original user object.
246 * @param stdClass $usernew The updated user object.
248 function useredit_update_trackforums($user, $usernew) {
250 if (!isset($usernew->trackforums
)) {
254 if ((!isset($user->trackforums
) ||
($usernew->trackforums
!= $user->trackforums
)) and !$usernew->trackforums
) {
255 require_once($CFG->dirroot
.'/mod/forum/lib.php');
256 forum_tp_delete_read_records($usernew->id
);
261 * Updates a users interests.
263 * @param stdClass $user
264 * @param array $interests
266 function useredit_update_interests($user, $interests) {
267 tag_set('user', $user->id
, $interests, 'core', context_user
::instance($user->id
)->id
);
271 * Powerful function that is used by edit and editadvanced to add common form elements/rules/etc.
273 * @param moodleform $mform
274 * @param array $editoroptions
275 * @param array $filemanageroptions
276 * @param stdClass $user
278 function useredit_shared_definition(&$mform, $editoroptions, $filemanageroptions, $user) {
279 global $CFG, $USER, $DB;
282 useredit_load_preferences($user, false);
285 $strrequired = get_string('required');
287 // Add the necessary names.
288 foreach (useredit_get_required_name_fields() as $fullname) {
289 $mform->addElement('text', $fullname, get_string($fullname), 'maxlength="100" size="30"');
290 $mform->addRule($fullname, $strrequired, 'required', null, 'client');
291 $mform->setType($fullname, PARAM_NOTAGS
);
294 $enabledusernamefields = useredit_get_enabled_name_fields();
295 // Add the enabled additional name fields.
296 foreach ($enabledusernamefields as $addname) {
297 $mform->addElement('text', $addname, get_string($addname), 'maxlength="100" size="30"');
298 $mform->setType($addname, PARAM_NOTAGS
);
301 // Do not show email field if change confirmation is pending.
302 if ($user->id
> 0 and !empty($CFG->emailchangeconfirmation
) and !empty($user->preference_newemail
)) {
303 $notice = get_string('emailchangepending', 'auth', $user);
304 $notice .= '<br /><a href="edit.php?cancelemailchange=1&id='.$user->id
.'">'
305 . get_string('emailchangecancel', 'auth') . '</a>';
306 $mform->addElement('static', 'emailpending', get_string('email'), $notice);
308 $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="30"');
309 $mform->addRule('email', $strrequired, 'required', null, 'client');
310 $mform->setType('email', PARAM_RAW_TRIMMED
);
314 $choices['0'] = get_string('emaildisplayno');
315 $choices['1'] = get_string('emaildisplayyes');
316 $choices['2'] = get_string('emaildisplaycourse');
317 $mform->addElement('select', 'maildisplay', get_string('emaildisplay'), $choices);
318 $mform->setDefault('maildisplay', $CFG->defaultpreference_maildisplay
);
320 $mform->addElement('text', 'city', get_string('city'), 'maxlength="120" size="21"');
321 $mform->setType('city', PARAM_TEXT
);
322 if (!empty($CFG->defaultcity
)) {
323 $mform->setDefault('city', $CFG->defaultcity
);
326 $choices = get_string_manager()->get_list_of_countries();
327 $choices = array('' => get_string('selectacountry') . '...') +
$choices;
328 $mform->addElement('select', 'country', get_string('selectacountry'), $choices);
329 if (!empty($CFG->country
)) {
330 $mform->setDefault('country', $CFG->country
);
333 if (isset($CFG->forcetimezone
) and $CFG->forcetimezone
!= 99) {
334 $choices = core_date
::get_list_of_timezones($CFG->forcetimezone
);
335 $mform->addElement('static', 'forcedtimezone', get_string('timezone'), $choices[$CFG->forcetimezone
]);
336 $mform->addElement('hidden', 'timezone');
337 $mform->setType('timezone', PARAM_TIMEZONE
);
339 $choices = core_date
::get_list_of_timezones($user->timezone
, true);
340 $mform->addElement('select', 'timezone', get_string('timezone'), $choices);
343 // Multi-Calendar Support - see MDL-18375.
344 $calendartypes = \core_calendar\type_factory
::get_list_of_calendar_types();
345 // We do not want to show this option unless there is more than one calendar type to display.
346 if (count($calendartypes) > 1) {
347 $mform->addElement('select', 'calendartype', get_string('preferredcalendar', 'calendar'), $calendartypes);
348 $mform->setDefault('calendartype', $CFG->calendartype
);
351 if (!empty($CFG->allowuserthemes
)) {
353 $choices[''] = get_string('default');
354 $themes = get_list_of_themes();
355 foreach ($themes as $key => $theme) {
356 if (empty($theme->hidefromselector
)) {
357 $choices[$key] = get_string('pluginname', 'theme_'.$theme->name
);
360 $mform->addElement('select', 'theme', get_string('preferredtheme'), $choices);
363 $mform->addElement('editor', 'description_editor', get_string('userdescription'), null, $editoroptions);
364 $mform->setType('description_editor', PARAM_CLEANHTML
);
365 $mform->addHelpButton('description_editor', 'userdescription');
367 if (empty($USER->newadminuser
)) {
368 $mform->addElement('header', 'moodle_picture', get_string('pictureofuser'));
370 if (!empty($CFG->enablegravatar
)) {
371 $mform->addElement('html', html_writer
::tag('p', get_string('gravatarenabled')));
374 $mform->addElement('static', 'currentpicture', get_string('currentpicture'));
376 $mform->addElement('checkbox', 'deletepicture', get_string('delete'));
377 $mform->setDefault('deletepicture', 0);
379 $mform->addElement('filemanager', 'imagefile', get_string('newpicture'), '', $filemanageroptions);
380 $mform->addHelpButton('imagefile', 'newpicture');
382 $mform->addElement('text', 'imagealt', get_string('imagealt'), 'maxlength="100" size="30"');
383 $mform->setType('imagealt', PARAM_TEXT
);
387 // Display user name fields that are not currenlty enabled here if there are any.
388 $disabledusernamefields = useredit_get_disabled_name_fields($enabledusernamefields);
389 if (count($disabledusernamefields) > 0) {
390 $mform->addElement('header', 'moodle_additional_names', get_string('additionalnames'));
391 foreach ($disabledusernamefields as $allname) {
392 $mform->addElement('text', $allname, get_string($allname), 'maxlength="100" size="30"');
393 $mform->setType($allname, PARAM_NOTAGS
);
397 if (!empty($CFG->usetags
) and empty($USER->newadminuser
)) {
398 $mform->addElement('header', 'moodle_interests', get_string('interests'));
399 $mform->addElement('tags', 'interests', get_string('interestslist'), array('display' => 'noofficial'));
400 $mform->addHelpButton('interests', 'interestslist');
403 // Moodle optional fields.
404 $mform->addElement('header', 'moodle_optional', get_string('optional', 'form'));
406 $mform->addElement('text', 'url', get_string('webpage'), 'maxlength="255" size="50"');
407 $mform->setType('url', PARAM_URL
);
409 $mform->addElement('text', 'icq', get_string('icqnumber'), 'maxlength="15" size="25"');
410 $mform->setType('icq', PARAM_NOTAGS
);
412 $mform->addElement('text', 'skype', get_string('skypeid'), 'maxlength="50" size="25"');
413 $mform->setType('skype', PARAM_NOTAGS
);
415 $mform->addElement('text', 'aim', get_string('aimid'), 'maxlength="50" size="25"');
416 $mform->setType('aim', PARAM_NOTAGS
);
418 $mform->addElement('text', 'yahoo', get_string('yahooid'), 'maxlength="50" size="25"');
419 $mform->setType('yahoo', PARAM_NOTAGS
);
421 $mform->addElement('text', 'msn', get_string('msnid'), 'maxlength="50" size="25"');
422 $mform->setType('msn', PARAM_NOTAGS
);
424 $mform->addElement('text', 'idnumber', get_string('idnumber'), 'maxlength="255" size="25"');
425 $mform->setType('idnumber', PARAM_NOTAGS
);
427 $mform->addElement('text', 'institution', get_string('institution'), 'maxlength="255" size="25"');
428 $mform->setType('institution', PARAM_TEXT
);
430 $mform->addElement('text', 'department', get_string('department'), 'maxlength="255" size="25"');
431 $mform->setType('department', PARAM_TEXT
);
433 $mform->addElement('text', 'phone1', get_string('phone'), 'maxlength="20" size="25"');
434 $mform->setType('phone1', PARAM_NOTAGS
);
436 $mform->addElement('text', 'phone2', get_string('phone2'), 'maxlength="20" size="25"');
437 $mform->setType('phone2', PARAM_NOTAGS
);
439 $mform->addElement('text', 'address', get_string('address'), 'maxlength="255" size="25"');
440 $mform->setType('address', PARAM_TEXT
);
444 * Return required user name fields for forms.
446 * @return array required user name fields in order according to settings.
448 function useredit_get_required_name_fields() {
451 // Get the name display format.
452 $nameformat = $CFG->fullnamedisplay
;
454 // Names that are required fields on user forms.
455 $necessarynames = array('firstname', 'lastname');
456 $languageformat = get_string('fullnamedisplay');
458 // Check that the language string and the $nameformat contain the necessary names.
459 foreach ($necessarynames as $necessaryname) {
460 $pattern = "/$necessaryname\b/";
461 if (!preg_match($pattern, $languageformat)) {
462 // If the language string has been altered then fall back on the below order.
463 $languageformat = 'firstname lastname';
465 if (!preg_match($pattern, $nameformat)) {
466 // If the nameformat doesn't contain the necessary name fields then use the languageformat.
467 $nameformat = $languageformat;
471 // Order all of the name fields in the postion they are written in the fullnamedisplay setting.
472 $necessarynames = order_in_string($necessarynames, $nameformat);
473 return $necessarynames;
477 * Gets enabled (from fullnameformate setting) user name fields in appropriate order.
479 * @return array Enabled user name fields.
481 function useredit_get_enabled_name_fields() {
484 // Get all of the other name fields which are not ranked as necessary.
485 $additionalusernamefields = array_diff(get_all_user_name_fields(), array('firstname', 'lastname'));
486 // Find out which additional name fields are actually being used from the fullnamedisplay setting.
487 $enabledadditionalusernames = array();
488 foreach ($additionalusernamefields as $enabledname) {
489 if (strpos($CFG->fullnamedisplay
, $enabledname) !== false) {
490 $enabledadditionalusernames[] = $enabledname;
494 // Order all of the name fields in the postion they are written in the fullnamedisplay setting.
495 $enabledadditionalusernames = order_in_string($enabledadditionalusernames, $CFG->fullnamedisplay
);
496 return $enabledadditionalusernames;
500 * Gets user name fields not enabled from the setting fullnamedisplay.
502 * @param array $enabledadditionalusernames Current enabled additional user name fields.
503 * @return array Disabled user name fields.
505 function useredit_get_disabled_name_fields($enabledadditionalusernames = null) {
506 // If we don't have enabled additional user name information then go and fetch it (try to avoid).
507 if (!isset($enabledadditionalusernames)) {
508 $enabledadditionalusernames = useredit_get_enabled_name_fields();
511 // These are the additional fields that are not currently enabled.
512 $nonusednamefields = array_diff(get_all_user_name_fields(),
513 array_merge(array('firstname', 'lastname'), $enabledadditionalusernames));
514 return $nonusednamefields;