NOBUG: Bump version
[moodle.git] / user / edit.php
blobc15475a1e0a2cea3e2824a180e5fd2acef0f7177
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 $PAGE->set_pagelayout('admin');
107 $PAGE->set_context($personalcontext);
108 if ($USER->id != $user->id) {
109 $PAGE->navigation->extend_for_user($user);
110 } else {
111 if ($node = $PAGE->navigation->find('myprofile', navigation_node::TYPE_ROOTNODE)) {
112 $node->force_open();
116 // Check access control.
117 if ($user->id == $USER->id) {
118 // Editing own profile - require_login() MUST NOT be used here, it would result in infinite loop!
119 if (!has_capability('moodle/user:editownprofile', $systemcontext)) {
120 print_error('cannotedityourprofile');
123 } else {
124 // Teachers, parents, etc.
125 require_capability('moodle/user:editprofile', $personalcontext);
126 // No editing of guest user account.
127 if (isguestuser($user->id)) {
128 print_error('guestnoeditprofileother');
130 // No editing of primary admin!
131 if (is_siteadmin($user) and !is_siteadmin($USER)) { // Only admins may edit other admins.
132 print_error('useradmineditadmin');
136 if ($user->deleted) {
137 echo $OUTPUT->header();
138 echo $OUTPUT->heading(get_string('userdeleted'));
139 echo $OUTPUT->footer();
140 die;
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);
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 // If email was changed and confirmation is required, send confirmation email now to the new address.
248 if ($emailchanged && $CFG->emailchangeconfirmation) {
249 $tempuser = $DB->get_record('user', array('id' => $user->id), '*', MUST_EXIST);
250 $tempuser->email = $usernew->preference_newemail;
252 $a = new stdClass();
253 $a->url = $CFG->wwwroot . '/user/emailupdate.php?key=' . $usernew->preference_newemailkey . '&id=' . $user->id;
254 $a->site = format_string($SITE->fullname, true, array('context' => context_course::instance(SITEID)));
255 $a->fullname = fullname($tempuser, true);
257 $emailupdatemessage = get_string('emailupdatemessage', 'auth', $a);
258 $emailupdatetitle = get_string('emailupdatetitle', 'auth', $a);
260 // Email confirmation directly rather than using messaging so they will definitely get an email.
261 $supportuser = core_user::get_support_user();
262 if (!$mailresults = email_to_user($tempuser, $supportuser, $emailupdatetitle, $emailupdatemessage)) {
263 die("could not send email!");
267 // Reload from db, we need new full name on this page if we do not redirect.
268 $user = $DB->get_record('user', array('id' => $user->id), '*', MUST_EXIST);
270 if ($USER->id == $user->id) {
271 // Override old $USER session variable if needed.
272 foreach ((array)$user as $variable => $value) {
273 if ($variable === 'description' or $variable === 'password') {
274 // These are not set for security nad perf reasons.
275 continue;
277 $USER->$variable = $value;
279 // Preload custom fields.
280 profile_load_custom_fields($USER);
283 if (is_siteadmin() and empty($SITE->shortname)) {
284 // Fresh cli install - we need to finish site settings.
285 redirect(new moodle_url('/admin/index.php'));
288 if (!$emailchanged || !$CFG->emailchangeconfirmation) {
289 redirect("$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id");
293 // Make sure we really are on the https page when https login required.
294 $PAGE->verify_https_required();
297 // Display page header.
298 $streditmyprofile = get_string('editmyprofile');
299 $strparticipants = get_string('participants');
300 $userfullname = fullname($user, true);
302 $PAGE->set_title("$course->shortname: $streditmyprofile");
303 $PAGE->set_heading($course->fullname);
305 echo $OUTPUT->header();
306 echo $OUTPUT->heading($userfullname);
308 if ($emailchanged) {
309 echo $emailchangedhtml;
310 } else {
311 // Finally display THE form.
312 $userform->display();
315 // And proper footer.
316 echo $OUTPUT->footer();