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.
76 if (is_mnet_remote_user($user)) {
77 if (user_not_fully_set_up($user)) {
78 $hostwwwroot = $DB->get_field('mnet_host', 'wwwroot', array('id' => $user->mnethostid
));
79 print_error('usernotfullysetup', 'mnet', '', $hostwwwroot);
81 redirect($CFG->wwwroot
. "/user/view.php?course={$course->id}");
84 // Load the appropriate auth plugin.
85 $userauth = get_auth_plugin($user->auth
);
87 if (!$userauth->can_edit_profile()) {
88 print_error('noprofileedit', 'auth');
91 if ($editurl = $userauth->edit_profile_url()) {
92 // This internal script not used.
96 if ($course->id
== SITEID
) {
97 $coursecontext = context_system
::instance(); // SYSTEM context.
99 $coursecontext = context_course
::instance($course->id
); // Course context.
101 $systemcontext = context_system
::instance();
102 $personalcontext = context_user
::instance($user->id
);
104 // Check access control.
105 if ($user->id
== $USER->id
) {
106 // Editing own profile - require_login() MUST NOT be used here, it would result in infinite loop!
107 if (!has_capability('moodle/user:editownprofile', $systemcontext)) {
108 print_error('cannotedityourprofile');
112 // Teachers, parents, etc.
113 require_capability('moodle/user:editprofile', $personalcontext);
114 // No editing of guest user account.
115 if (isguestuser($user->id
)) {
116 print_error('guestnoeditprofileother');
118 // No editing of primary admin!
119 if (is_siteadmin($user) and !is_siteadmin($USER)) { // Only admins may edit other admins.
120 print_error('useradmineditadmin');
124 if ($user->deleted
) {
125 echo $OUTPUT->header();
126 echo $OUTPUT->heading(get_string('userdeleted'));
127 echo $OUTPUT->footer();
131 $PAGE->set_pagelayout('admin');
132 $PAGE->set_context($personalcontext);
133 if ($USER->id
!= $user->id
) {
134 $PAGE->navigation
->extend_for_user($user);
136 if ($node = $PAGE->navigation
->find('myprofile', navigation_node
::TYPE_ROOTNODE
)) {
141 // Process email change cancellation.
142 if ($cancelemailchange) {
143 cancel_email_update($user->id
);
146 // Load user preferences.
147 useredit_load_preferences($user);
149 // Load custom profile fields data.
150 profile_load_data($user);
153 // Prepare the editor and create form.
154 $editoroptions = array(
155 'maxfiles' => EDITOR_UNLIMITED_FILES
,
156 'maxbytes' => $CFG->maxbytes
,
157 'trusttext' => false,
158 'forcehttps' => false,
159 'context' => $personalcontext
162 $user = file_prepare_standard_editor($user, 'description', $editoroptions, $personalcontext, 'user', 'profile', 0);
163 // Prepare filemanager draft area.
165 $filemanagercontext = $editoroptions['context'];
166 $filemanageroptions = array('maxbytes' => $CFG->maxbytes
,
169 'accepted_types' => 'web_image');
170 file_prepare_draft_area($draftitemid, $filemanagercontext->id
, 'user', 'newicon', 0, $filemanageroptions);
171 $user->imagefile
= $draftitemid;
173 $userform = new user_edit_form(new moodle_url($PAGE->url
, array('returnto' => $returnto)), array(
174 'editoroptions' => $editoroptions,
175 'filemanageroptions' => $filemanageroptions,
178 $emailchanged = false;
180 if ($usernew = $userform->get_data()) {
182 // Deciding where to send the user back in most cases.
183 if ($returnto === 'profile') {
184 if ($course->id
!= SITEID
) {
185 $returnurl = new moodle_url('/user/view.php', array('id' => $user->id
, 'course' => $course->id
));
187 $returnurl = new moodle_url('/user/profile.php', array('id' => $user->id
));
190 $returnurl = new moodle_url('/user/preferences.php', array('userid' => $user->id
));
193 $emailchangedhtml = '';
195 if ($CFG->emailchangeconfirmation
) {
196 // Users with 'moodle/user:update' can change their email address immediately.
197 // Other users require a confirmation email.
198 if (isset($usernew->email
) and $user->email
!= $usernew->email
&& !has_capability('moodle/user:update', $systemcontext)) {
200 $a->newemail
= $usernew->preference_newemail
= $usernew->email
;
201 $usernew->preference_newemailkey
= random_string(20);
202 $usernew->preference_newemailattemptsleft
= 3;
203 $a->oldemail
= $usernew->email
= $user->email
;
205 $emailchangedhtml = $OUTPUT->box(get_string('auth_changingemailaddress', 'auth', $a), 'generalbox', 'notice');
206 $emailchangedhtml .= $OUTPUT->continue_button($returnurl);
207 $emailchanged = true;
211 $authplugin = get_auth_plugin($user->auth
);
213 $usernew->timemodified
= time();
215 // Description editor element may not exist!
216 if (isset($usernew->description_editor
) && isset($usernew->description_editor
['format'])) {
217 $usernew = file_postupdate_standard_editor($usernew, 'description', $editoroptions, $personalcontext, 'user', 'profile', 0);
220 // Pass a true old $user here.
221 if (!$authplugin->user_update($user, $usernew)) {
222 // Auth update failed.
223 print_error('cannotupdateprofile');
226 // Update user with new profile data.
227 user_update_user($usernew, false, false);
229 // Update preferences.
230 useredit_update_user_preference($usernew);
233 if (isset($usernew->interests
)) {
234 useredit_update_interests($usernew, $usernew->interests
);
237 // Update user picture.
238 if (empty($CFG->disableuserimages
)) {
239 useredit_update_picture($usernew, $userform, $filemanageroptions);
242 // Update mail bounces.
243 useredit_update_bounces($user, $usernew);
245 // Update forum track preference.
246 useredit_update_trackforums($user, $usernew);
248 // Save custom profile fields data.
249 profile_save_data($usernew);
252 \core\event\user_updated
::create_from_userid($user->id
)->trigger();
254 // If email was changed and confirmation is required, send confirmation email now to the new address.
255 if ($emailchanged && $CFG->emailchangeconfirmation
) {
256 $tempuser = $DB->get_record('user', array('id' => $user->id
), '*', MUST_EXIST
);
257 $tempuser->email
= $usernew->preference_newemail
;
260 $a->url
= $CFG->wwwroot
. '/user/emailupdate.php?key=' . $usernew->preference_newemailkey
. '&id=' . $user->id
;
261 $a->site
= format_string($SITE->fullname
, true, array('context' => context_course
::instance(SITEID
)));
262 $a->fullname
= fullname($tempuser, true);
264 $emailupdatemessage = get_string('emailupdatemessage', 'auth', $a);
265 $emailupdatetitle = get_string('emailupdatetitle', 'auth', $a);
267 // Email confirmation directly rather than using messaging so they will definitely get an email.
268 $supportuser = core_user
::get_support_user();
269 if (!$mailresults = email_to_user($tempuser, $supportuser, $emailupdatetitle, $emailupdatemessage)) {
270 die("could not send email!");
274 // Reload from db, we need new full name on this page if we do not redirect.
275 $user = $DB->get_record('user', array('id' => $user->id
), '*', MUST_EXIST
);
277 if ($USER->id
== $user->id
) {
278 // Override old $USER session variable if needed.
279 foreach ((array)$user as $variable => $value) {
280 if ($variable === 'description' or $variable === 'password') {
281 // These are not set for security nad perf reasons.
284 $USER->$variable = $value;
286 // Preload custom fields.
287 profile_load_custom_fields($USER);
290 if (is_siteadmin() and empty($SITE->shortname
)) {
291 // Fresh cli install - we need to finish site settings.
292 redirect(new moodle_url('/admin/index.php'));
295 if (!$emailchanged ||
!$CFG->emailchangeconfirmation
) {
296 redirect($returnurl);
300 // Make sure we really are on the https page when https login required.
301 $PAGE->verify_https_required();
304 // Display page header.
305 $streditmyprofile = get_string('editmyprofile');
306 $strparticipants = get_string('participants');
307 $userfullname = fullname($user, true);
309 $PAGE->set_title("$course->shortname: $streditmyprofile");
310 $PAGE->set_heading($userfullname);
312 echo $OUTPUT->header();
313 echo $OUTPUT->heading($userfullname);
316 echo $emailchangedhtml;
318 // Finally display THE form.
319 $userform->display();
322 // And proper footer.
323 echo $OUTPUT->footer();