Merge branch 'MDL-52544-30' of git://github.com/damyon/moodle into MOODLE_30_STABLE
[moodle.git] / user / edit.php
blob55e93b92a1949962517323ed303e2aefa89aee7b
1 <?php
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.
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/>.
17 /**
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
22 * @package core_user
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());
53 } else {
54 $PAGE->set_context(context_system::instance());
57 // Guest can not edit.
58 if (isguestuser()) {
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 if (!empty($CFG->usetags)) {
74 require_once($CFG->dirroot.'/tag/lib.php');
75 $user->interests = tag_get_tags_array('user', $user->id);
78 // Remote users cannot be edited.
79 if (is_mnet_remote_user($user)) {
80 if (user_not_fully_set_up($user)) {
81 $hostwwwroot = $DB->get_field('mnet_host', 'wwwroot', array('id' => $user->mnethostid));
82 print_error('usernotfullysetup', 'mnet', '', $hostwwwroot);
84 redirect($CFG->wwwroot . "/user/view.php?course={$course->id}");
87 // Load the appropriate auth plugin.
88 $userauth = get_auth_plugin($user->auth);
90 if (!$userauth->can_edit_profile()) {
91 print_error('noprofileedit', 'auth');
94 if ($editurl = $userauth->edit_profile_url()) {
95 // This internal script not used.
96 redirect($editurl);
99 if ($course->id == SITEID) {
100 $coursecontext = context_system::instance(); // SYSTEM context.
101 } else {
102 $coursecontext = context_course::instance($course->id); // Course context.
104 $systemcontext = context_system::instance();
105 $personalcontext = context_user::instance($user->id);
107 // Check access control.
108 if ($user->id == $USER->id) {
109 // Editing own profile - require_login() MUST NOT be used here, it would result in infinite loop!
110 if (!has_capability('moodle/user:editownprofile', $systemcontext)) {
111 print_error('cannotedityourprofile');
114 } else {
115 // Teachers, parents, etc.
116 require_capability('moodle/user:editprofile', $personalcontext);
117 // No editing of guest user account.
118 if (isguestuser($user->id)) {
119 print_error('guestnoeditprofileother');
121 // No editing of primary admin!
122 if (is_siteadmin($user) and !is_siteadmin($USER)) { // Only admins may edit other admins.
123 print_error('useradmineditadmin');
127 if ($user->deleted) {
128 echo $OUTPUT->header();
129 echo $OUTPUT->heading(get_string('userdeleted'));
130 echo $OUTPUT->footer();
131 die;
134 $PAGE->set_pagelayout('admin');
135 $PAGE->set_context($personalcontext);
136 if ($USER->id != $user->id) {
137 $PAGE->navigation->extend_for_user($user);
138 } else {
139 if ($node = $PAGE->navigation->find('myprofile', navigation_node::TYPE_ROOTNODE)) {
140 $node->force_open();
144 // Process email change cancellation.
145 if ($cancelemailchange) {
146 cancel_email_update($user->id);
149 // Load user preferences.
150 useredit_load_preferences($user);
152 // Load custom profile fields data.
153 profile_load_data($user);
156 // Prepare the editor and create form.
157 $editoroptions = array(
158 'maxfiles' => EDITOR_UNLIMITED_FILES,
159 'maxbytes' => $CFG->maxbytes,
160 'trusttext' => false,
161 'forcehttps' => false,
162 'context' => $personalcontext
165 $user = file_prepare_standard_editor($user, 'description', $editoroptions, $personalcontext, 'user', 'profile', 0);
166 // Prepare filemanager draft area.
167 $draftitemid = 0;
168 $filemanagercontext = $editoroptions['context'];
169 $filemanageroptions = array('maxbytes' => $CFG->maxbytes,
170 'subdirs' => 0,
171 'maxfiles' => 1,
172 'accepted_types' => 'web_image');
173 file_prepare_draft_area($draftitemid, $filemanagercontext->id, 'user', 'newicon', 0, $filemanageroptions);
174 $user->imagefile = $draftitemid;
175 // Create form.
176 $userform = new user_edit_form(new moodle_url($PAGE->url, array('returnto' => $returnto)), array(
177 'editoroptions' => $editoroptions,
178 'filemanageroptions' => $filemanageroptions,
179 'user' => $user));
181 $emailchanged = false;
183 if ($usernew = $userform->get_data()) {
185 // Deciding where to send the user back in most cases.
186 if ($returnto === 'profile') {
187 if ($course->id != SITEID) {
188 $returnurl = new moodle_url('/user/view.php', array('id' => $user->id, 'course' => $course->id));
189 } else {
190 $returnurl = new moodle_url('/user/profile.php', array('id' => $user->id));
192 } else {
193 $returnurl = new moodle_url('/user/preferences.php', array('userid' => $user->id));
196 $emailchangedhtml = '';
198 if ($CFG->emailchangeconfirmation) {
199 // Users with 'moodle/user:update' can change their email address immediately.
200 // Other users require a confirmation email.
201 if (isset($usernew->email) and $user->email != $usernew->email && !has_capability('moodle/user:update', $systemcontext)) {
202 $a = new stdClass();
203 $a->newemail = $usernew->preference_newemail = $usernew->email;
204 $usernew->preference_newemailkey = random_string(20);
205 $usernew->preference_newemailattemptsleft = 3;
206 $a->oldemail = $usernew->email = $user->email;
208 $emailchangedhtml = $OUTPUT->box(get_string('auth_changingemailaddress', 'auth', $a), 'generalbox', 'notice');
209 $emailchangedhtml .= $OUTPUT->continue_button($returnurl);
210 $emailchanged = true;
214 $authplugin = get_auth_plugin($user->auth);
216 $usernew->timemodified = time();
218 // Description editor element may not exist!
219 if (isset($usernew->description_editor) && isset($usernew->description_editor['format'])) {
220 $usernew = file_postupdate_standard_editor($usernew, 'description', $editoroptions, $personalcontext, 'user', 'profile', 0);
223 // Pass a true old $user here.
224 if (!$authplugin->user_update($user, $usernew)) {
225 // Auth update failed.
226 print_error('cannotupdateprofile');
229 // Update user with new profile data.
230 user_update_user($usernew, false, false);
232 // Update preferences.
233 useredit_update_user_preference($usernew);
235 // Update interests.
236 if (!empty($CFG->usetags)) {
237 useredit_update_interests($usernew, $usernew->interests);
240 // Update user picture.
241 if (empty($CFG->disableuserimages)) {
242 useredit_update_picture($usernew, $userform, $filemanageroptions);
245 // Update mail bounces.
246 useredit_update_bounces($user, $usernew);
248 // Update forum track preference.
249 useredit_update_trackforums($user, $usernew);
251 // Save custom profile fields data.
252 profile_save_data($usernew);
254 // Trigger event.
255 \core\event\user_updated::create_from_userid($user->id)->trigger();
257 // If email was changed and confirmation is required, send confirmation email now to the new address.
258 if ($emailchanged && $CFG->emailchangeconfirmation) {
259 $tempuser = $DB->get_record('user', array('id' => $user->id), '*', MUST_EXIST);
260 $tempuser->email = $usernew->preference_newemail;
262 $a = new stdClass();
263 $a->url = $CFG->wwwroot . '/user/emailupdate.php?key=' . $usernew->preference_newemailkey . '&id=' . $user->id;
264 $a->site = format_string($SITE->fullname, true, array('context' => context_course::instance(SITEID)));
265 $a->fullname = fullname($tempuser, true);
267 $emailupdatemessage = get_string('emailupdatemessage', 'auth', $a);
268 $emailupdatetitle = get_string('emailupdatetitle', 'auth', $a);
270 // Email confirmation directly rather than using messaging so they will definitely get an email.
271 $supportuser = core_user::get_support_user();
272 if (!$mailresults = email_to_user($tempuser, $supportuser, $emailupdatetitle, $emailupdatemessage)) {
273 die("could not send email!");
277 // Reload from db, we need new full name on this page if we do not redirect.
278 $user = $DB->get_record('user', array('id' => $user->id), '*', MUST_EXIST);
280 if ($USER->id == $user->id) {
281 // Override old $USER session variable if needed.
282 foreach ((array)$user as $variable => $value) {
283 if ($variable === 'description' or $variable === 'password') {
284 // These are not set for security nad perf reasons.
285 continue;
287 $USER->$variable = $value;
289 // Preload custom fields.
290 profile_load_custom_fields($USER);
293 if (is_siteadmin() and empty($SITE->shortname)) {
294 // Fresh cli install - we need to finish site settings.
295 redirect(new moodle_url('/admin/index.php'));
298 if (!$emailchanged || !$CFG->emailchangeconfirmation) {
299 redirect($returnurl);
303 // Make sure we really are on the https page when https login required.
304 $PAGE->verify_https_required();
307 // Display page header.
308 $streditmyprofile = get_string('editmyprofile');
309 $strparticipants = get_string('participants');
310 $userfullname = fullname($user, true);
312 $PAGE->set_title("$course->shortname: $streditmyprofile");
313 $PAGE->set_heading($userfullname);
315 echo $OUTPUT->header();
316 echo $OUTPUT->heading($userfullname);
318 if ($emailchanged) {
319 echo $emailchangedhtml;
320 } else {
321 // Finally display THE form.
322 $userform->display();
325 // And proper footer.
326 echo $OUTPUT->footer();