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 * Allows you to edit 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
25 require_once('../config.php');
26 require_once($CFG->libdir
.'/gdlib.php');
27 require_once($CFG->dirroot
.'/user/edit_form.php');
28 require_once($CFG->dirroot
.'/user/editlib.php');
29 require_once($CFG->dirroot
.'/user/profile/lib.php');
30 require_once($CFG->dirroot
.'/user/lib.php');
32 // HTTPS is required in this page when $CFG->loginhttps enabled.
33 $PAGE->https_required();
35 $userid = optional_param('id', $USER->id
, PARAM_INT
); // User id.
36 $course = optional_param('course', SITEID
, PARAM_INT
); // Course id (defaults to Site).
37 $returnto = optional_param('returnto', null, PARAM_ALPHA
); // Code determining where to return to after save.
38 $cancelemailchange = optional_param('cancelemailchange', 0, PARAM_INT
); // Course id (defaults to Site).
40 $PAGE->set_url('/user/edit.php', array('course' => $course, 'id' => $userid));
42 if (!$course = $DB->get_record('course', array('id' => $course))) {
43 print_error('invalidcourseid');
46 if ($course->id
!= SITEID
) {
47 require_login($course);
48 } else if (!isloggedin()) {
49 if (empty($SESSION->wantsurl
)) {
50 $SESSION->wantsurl
= $CFG->httpswwwroot
.'/user/edit.php';
52 redirect(get_login_url());
54 $PAGE->set_context(context_system
::instance());
57 // Guest can not edit.
59 print_error('guestnoeditprofile');
62 // The user profile we are editing.
63 if (!$user = $DB->get_record('user', array('id' => $userid))) {
64 print_error('invaliduserid');
67 // Guest can not be edited.
68 if (isguestuser($user)) {
69 print_error('guestnoeditprofile');
72 // User interests separated by commas.
73 $user->interests
= core_tag_tag
::get_item_tags_array('core', 'user', $user->id
);
75 // Remote users cannot be edited. Note we have to perform the strict user_not_fully_set_up() check.
76 // Otherwise the remote user could end up in endless loop between user/view.php and here.
77 // Required custom fields are not supported in MNet environment anyway.
78 if (is_mnet_remote_user($user)) {
79 if (user_not_fully_set_up($user, true)) {
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 // Load the appropriate auth plugin.
87 $userauth = get_auth_plugin($user->auth
);
89 if (!$userauth->can_edit_profile()) {
90 print_error('noprofileedit', 'auth');
93 if ($editurl = $userauth->edit_profile_url()) {
94 // This internal script not used.
98 if ($course->id
== SITEID
) {
99 $coursecontext = context_system
::instance(); // SYSTEM context.
101 $coursecontext = context_course
::instance($course->id
); // Course context.
103 $systemcontext = context_system
::instance();
104 $personalcontext = context_user
::instance($user->id
);
106 // Check access control.
107 if ($user->id
== $USER->id
) {
108 // Editing own profile - require_login() MUST NOT be used here, it would result in infinite loop!
109 if (!has_capability('moodle/user:editownprofile', $systemcontext)) {
110 print_error('cannotedityourprofile');
114 // Teachers, parents, etc.
115 require_capability('moodle/user:editprofile', $personalcontext);
116 // No editing of guest user account.
117 if (isguestuser($user->id
)) {
118 print_error('guestnoeditprofileother');
120 // No editing of primary admin!
121 if (is_siteadmin($user) and !is_siteadmin($USER)) { // Only admins may edit other admins.
122 print_error('useradmineditadmin');
126 if ($user->deleted
) {
127 echo $OUTPUT->header();
128 echo $OUTPUT->heading(get_string('userdeleted'));
129 echo $OUTPUT->footer();
133 $PAGE->set_pagelayout('admin');
134 $PAGE->set_context($personalcontext);
135 if ($USER->id
!= $user->id
) {
136 $PAGE->navigation
->extend_for_user($user);
138 if ($node = $PAGE->navigation
->find('myprofile', navigation_node
::TYPE_ROOTNODE
)) {
143 // Process email change cancellation.
144 if ($cancelemailchange) {
145 cancel_email_update($user->id
);
148 // Load user preferences.
149 useredit_load_preferences($user);
151 // Load custom profile fields data.
152 profile_load_data($user);
155 // Prepare the editor and create form.
156 $editoroptions = array(
157 'maxfiles' => EDITOR_UNLIMITED_FILES
,
158 'maxbytes' => $CFG->maxbytes
,
159 'trusttext' => false,
160 'forcehttps' => false,
161 'context' => $personalcontext
164 $user = file_prepare_standard_editor($user, 'description', $editoroptions, $personalcontext, 'user', 'profile', 0);
165 // Prepare filemanager draft area.
167 $filemanagercontext = $editoroptions['context'];
168 $filemanageroptions = array('maxbytes' => $CFG->maxbytes
,
171 'accepted_types' => 'web_image');
172 file_prepare_draft_area($draftitemid, $filemanagercontext->id
, 'user', 'newicon', 0, $filemanageroptions);
173 $user->imagefile
= $draftitemid;
175 $userform = new user_edit_form(new moodle_url($PAGE->url
, array('returnto' => $returnto)), array(
176 'editoroptions' => $editoroptions,
177 'filemanageroptions' => $filemanageroptions,
180 $emailchanged = false;
182 if ($usernew = $userform->get_data()) {
184 // Deciding where to send the user back in most cases.
185 if ($returnto === 'profile') {
186 if ($course->id
!= SITEID
) {
187 $returnurl = new moodle_url('/user/view.php', array('id' => $user->id
, 'course' => $course->id
));
189 $returnurl = new moodle_url('/user/profile.php', array('id' => $user->id
));
192 $returnurl = new moodle_url('/user/preferences.php', array('userid' => $user->id
));
195 $emailchangedhtml = '';
197 if ($CFG->emailchangeconfirmation
) {
198 // Users with 'moodle/user:update' can change their email address immediately.
199 // Other users require a confirmation email.
200 if (isset($usernew->email
) and $user->email
!= $usernew->email
&& !has_capability('moodle/user:update', $systemcontext)) {
202 $emailchangedkey = random_string(20);
203 set_user_preference('newemail', $usernew->email
, $user->id
);
204 set_user_preference('newemailkey', $emailchangedkey, $user->id
);
205 set_user_preference('newemailattemptsleft', 3, $user->id
);
207 $a->newemail
= $emailchanged = $usernew->email
;
208 $a->oldemail
= $usernew->email
= $user->email
;
210 $emailchangedhtml = $OUTPUT->box(get_string('auth_changingemailaddress', 'auth', $a), 'generalbox', 'notice');
211 $emailchangedhtml .= $OUTPUT->continue_button($returnurl);
215 $authplugin = get_auth_plugin($user->auth
);
217 $usernew->timemodified
= time();
219 // Description editor element may not exist!
220 if (isset($usernew->description_editor
) && isset($usernew->description_editor
['format'])) {
221 $usernew = file_postupdate_standard_editor($usernew, 'description', $editoroptions, $personalcontext, 'user', 'profile', 0);
224 // Pass a true old $user here.
225 if (!$authplugin->user_update($user, $usernew)) {
226 // Auth update failed.
227 print_error('cannotupdateprofile');
230 // Update user with new profile data.
231 user_update_user($usernew, false, false);
233 // Update preferences.
234 useredit_update_user_preference($usernew);
237 if (isset($usernew->interests
)) {
238 useredit_update_interests($usernew, $usernew->interests
);
241 // Update user picture.
242 if (empty($CFG->disableuserimages
)) {
243 core_user
::update_picture($usernew, $filemanageroptions);
246 // Update mail bounces.
247 useredit_update_bounces($user, $usernew);
249 // Update forum track preference.
250 useredit_update_trackforums($user, $usernew);
252 // Save custom profile fields data.
253 profile_save_data($usernew);
256 \core\event\user_updated
::create_from_userid($user->id
)->trigger();
258 // If email was changed and confirmation is required, send confirmation email now to the new address.
259 if ($emailchanged !== false && $CFG->emailchangeconfirmation
) {
260 $tempuser = $DB->get_record('user', array('id' => $user->id
), '*', MUST_EXIST
);
261 $tempuser->email
= $emailchanged;
263 $supportuser = core_user
::get_support_user();
266 $a->url
= $CFG->wwwroot
. '/user/emailupdate.php?key=' . $emailchangedkey . '&id=' . $user->id
;
267 $a->site
= format_string($SITE->fullname
, true, array('context' => context_course
::instance(SITEID
)));
268 $a->fullname
= fullname($tempuser, true);
269 $a->supportemail
= $supportuser->email
;
271 $emailupdatemessage = get_string('emailupdatemessage', 'auth', $a);
272 $emailupdatetitle = get_string('emailupdatetitle', 'auth', $a);
274 // Email confirmation directly rather than using messaging so they will definitely get an email.
275 $noreplyuser = core_user
::get_noreply_user();
276 if (!$mailresults = email_to_user($tempuser, $noreplyuser, $emailupdatetitle, $emailupdatemessage)) {
277 die("could not send email!");
281 // Reload from db, we need new full name on this page if we do not redirect.
282 $user = $DB->get_record('user', array('id' => $user->id
), '*', MUST_EXIST
);
284 if ($USER->id
== $user->id
) {
285 // Override old $USER session variable if needed.
286 foreach ((array)$user as $variable => $value) {
287 if ($variable === 'description' or $variable === 'password') {
288 // These are not set for security nad perf reasons.
291 $USER->$variable = $value;
293 // Preload custom fields.
294 profile_load_custom_fields($USER);
297 if (is_siteadmin() and empty($SITE->shortname
)) {
298 // Fresh cli install - we need to finish site settings.
299 redirect(new moodle_url('/admin/index.php'));
302 if (!$emailchanged ||
!$CFG->emailchangeconfirmation
) {
303 redirect($returnurl);
307 // Make sure we really are on the https page when https login required.
308 $PAGE->verify_https_required();
311 // Display page header.
312 $streditmyprofile = get_string('editmyprofile');
313 $strparticipants = get_string('participants');
314 $userfullname = fullname($user, true);
316 $PAGE->set_title("$course->shortname: $streditmyprofile");
317 $PAGE->set_heading($userfullname);
319 echo $OUTPUT->header();
320 echo $OUTPUT->heading($userfullname);
323 echo $emailchangedhtml;
325 // Finally display THE form.
326 $userform->display();
329 // And proper footer.
330 echo $OUTPUT->footer();