Revert "Merge branch 'MDL-45594_m27' of git://github.com/markn86/moodle into MOODLE_2...
[moodle.git] / user / edit.php
blobc7f182bbf45028709bb75d5b6c18fa9948fa334f
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 $cancelemailchange = optional_param('cancelemailchange', 0, PARAM_INT); // Course id (defaults to Site).
39 $PAGE->set_url('/user/edit.php', array('course' => $course, 'id' => $userid));
41 if (!$course = $DB->get_record('course', array('id' => $course))) {
42 print_error('invalidcourseid');
45 if ($course->id != SITEID) {
46 require_login($course);
47 } else if (!isloggedin()) {
48 if (empty($SESSION->wantsurl)) {
49 $SESSION->wantsurl = $CFG->httpswwwroot.'/user/edit.php';
51 redirect(get_login_url());
52 } else {
53 $PAGE->set_context(context_system::instance());
56 // Guest can not edit.
57 if (isguestuser()) {
58 print_error('guestnoeditprofile');
61 // The user profile we are editing.
62 if (!$user = $DB->get_record('user', array('id' => $userid))) {
63 print_error('invaliduserid');
66 // Guest can not be edited.
67 if (isguestuser($user)) {
68 print_error('guestnoeditprofile');
71 // User interests separated by commas.
72 if (!empty($CFG->usetags)) {
73 require_once($CFG->dirroot.'/tag/lib.php');
74 $user->interests = tag_get_tags_array('user', $user->id);
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 // 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.
95 redirect($editurl);
98 if ($course->id == SITEID) {
99 $coursecontext = context_system::instance(); // SYSTEM context.
100 } else {
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');
113 } else {
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();
130 die;
133 $PAGE->set_pagelayout('admin');
134 $PAGE->set_context($personalcontext);
135 if ($USER->id != $user->id) {
136 $PAGE->navigation->extend_for_user($user);
137 } else {
138 if ($node = $PAGE->navigation->find('myprofile', navigation_node::TYPE_ROOTNODE)) {
139 $node->force_open();
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.
166 $draftitemid = 0;
167 $filemanagercontext = $editoroptions['context'];
168 $filemanageroptions = array('maxbytes' => $CFG->maxbytes,
169 'subdirs' => 0,
170 'maxfiles' => 1,
171 'accepted_types' => 'web_image');
172 file_prepare_draft_area($draftitemid, $filemanagercontext->id, 'user', 'newicon', 0, $filemanageroptions);
173 $user->imagefile = $draftitemid;
174 // Create form.
175 $userform = new user_edit_form(null, array(
176 'editoroptions' => $editoroptions,
177 'filemanageroptions' => $filemanageroptions,
178 'userid' => $user->id));
179 if (empty($user->country)) {
180 // MDL-16308 - we must unset the value here so $CFG->country can be used as default one.
181 unset($user->country);
183 $userform->set_data($user);
185 $emailchanged = false;
187 if ($usernew = $userform->get_data()) {
189 $emailchangedhtml = '';
191 if ($CFG->emailchangeconfirmation) {
192 // Users with 'moodle/user:update' can change their email address immediately.
193 // Other users require a confirmation email.
194 if (isset($usernew->email) and $user->email != $usernew->email && !has_capability('moodle/user:update', $systemcontext)) {
195 $a = new stdClass();
196 $a->newemail = $usernew->preference_newemail = $usernew->email;
197 $usernew->preference_newemailkey = random_string(20);
198 $usernew->preference_newemailattemptsleft = 3;
199 $a->oldemail = $usernew->email = $user->email;
201 $emailchangedhtml = $OUTPUT->box(get_string('auth_changingemailaddress', 'auth', $a), 'generalbox', 'notice');
202 $emailchangedhtml .= $OUTPUT->continue_button("$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id");
203 $emailchanged = true;
207 $authplugin = get_auth_plugin($user->auth);
209 $usernew->timemodified = time();
211 // Description editor element may not exist!
212 if (isset($usernew->description_editor)) {
213 $usernew = file_postupdate_standard_editor($usernew, 'description', $editoroptions, $personalcontext, 'user', 'profile', 0);
216 // Pass a true old $user here.
217 if (!$authplugin->user_update($user, $usernew)) {
218 // Auth update failed.
219 print_error('cannotupdateprofile');
222 // Update user with new profile data.
223 user_update_user($usernew, false, false);
225 // Update preferences.
226 useredit_update_user_preference($usernew);
228 // Update interests.
229 if (!empty($CFG->usetags)) {
230 useredit_update_interests($usernew, $usernew->interests);
233 // Update user picture.
234 if (empty($CFG->disableuserimages)) {
235 useredit_update_picture($usernew, $userform, $filemanageroptions);
238 // Update mail bounces.
239 useredit_update_bounces($user, $usernew);
241 // Update forum track preference.
242 useredit_update_trackforums($user, $usernew);
244 // Save custom profile fields data.
245 profile_save_data($usernew);
247 // Trigger event.
248 \core\event\user_updated::create_from_userid($user->id)->trigger();
250 // If email was changed and confirmation is required, send confirmation email now to the new address.
251 if ($emailchanged && $CFG->emailchangeconfirmation) {
252 $tempuser = $DB->get_record('user', array('id' => $user->id), '*', MUST_EXIST);
253 $tempuser->email = $usernew->preference_newemail;
255 $a = new stdClass();
256 $a->url = $CFG->wwwroot . '/user/emailupdate.php?key=' . $usernew->preference_newemailkey . '&id=' . $user->id;
257 $a->site = format_string($SITE->fullname, true, array('context' => context_course::instance(SITEID)));
258 $a->fullname = fullname($tempuser, true);
260 $emailupdatemessage = get_string('emailupdatemessage', 'auth', $a);
261 $emailupdatetitle = get_string('emailupdatetitle', 'auth', $a);
263 // Email confirmation directly rather than using messaging so they will definitely get an email.
264 $supportuser = core_user::get_support_user();
265 if (!$mailresults = email_to_user($tempuser, $supportuser, $emailupdatetitle, $emailupdatemessage)) {
266 die("could not send email!");
270 // Reload from db, we need new full name on this page if we do not redirect.
271 $user = $DB->get_record('user', array('id' => $user->id), '*', MUST_EXIST);
273 if ($USER->id == $user->id) {
274 // Override old $USER session variable if needed.
275 foreach ((array)$user as $variable => $value) {
276 if ($variable === 'description' or $variable === 'password') {
277 // These are not set for security nad perf reasons.
278 continue;
280 $USER->$variable = $value;
282 // Preload custom fields.
283 profile_load_custom_fields($USER);
286 if (is_siteadmin() and empty($SITE->shortname)) {
287 // Fresh cli install - we need to finish site settings.
288 redirect(new moodle_url('/admin/index.php'));
291 if (!$emailchanged || !$CFG->emailchangeconfirmation) {
292 redirect("$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id");
296 // Make sure we really are on the https page when https login required.
297 $PAGE->verify_https_required();
300 // Display page header.
301 $streditmyprofile = get_string('editmyprofile');
302 $strparticipants = get_string('participants');
303 $userfullname = fullname($user, true);
305 $PAGE->set_title("$course->shortname: $streditmyprofile");
306 $PAGE->set_heading($course->fullname);
308 echo $OUTPUT->header();
309 echo $OUTPUT->heading($userfullname);
311 if ($emailchanged) {
312 echo $emailchangedhtml;
313 } else {
314 // Finally display THE form.
315 $userform->display();
318 // And proper footer.
319 echo $OUTPUT->footer();