Merge branch 'MDL-25863_automated_backups_wip' of git://github.com/stronk7/moodle
[moodle.git] / user / edit.php
blob379fa4214b6dc4dd66b8f6f16f65da953fb40d4b
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * Allows you to edit a users profile
21 * @copyright 1999 Martin Dougiamas http://dougiamas.com
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 * @package user
26 require_once('../config.php');
27 require_once($CFG->libdir.'/gdlib.php');
28 require_once($CFG->dirroot.'/user/edit_form.php');
29 require_once($CFG->dirroot.'/user/editlib.php');
30 require_once($CFG->dirroot.'/user/profile/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, 'cancelemailchange'=>$cancelemailchange));
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_course($course);
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 = get_context_instance(CONTEXT_SYSTEM); // SYSTEM context
100 } else {
101 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); // Course context
103 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
104 $personalcontext = get_context_instance(CONTEXT_USER, $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 // Process email change cancellation
134 if ($cancelemailchange) {
135 cancel_email_update($user->id);
138 //load user preferences
139 useredit_load_preferences($user);
141 //Load custom profile fields data
142 profile_load_data($user);
145 // Prepare the editor and create form
146 $editoroptions = array('maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>$CFG->maxbytes, 'trusttext'=>false, 'forcehttps'=>false);
147 $user = file_prepare_standard_editor($user, 'description', $editoroptions, $personalcontext, 'user', 'profile', 0);
148 $userform = new user_edit_form(null, array('editoroptions'=>$editoroptions));
149 if (empty($user->country)) {
150 // MDL-16308 - we must unset the value here so $CFG->country can be used as default one
151 unset($user->country);
153 $userform->set_data($user);
155 $email_changed = false;
157 if ($usernew = $userform->get_data()) {
159 add_to_log($course->id, 'user', 'update', "view.php?id=$user->id&course=$course->id", '');
161 $email_changed_html = '';
163 if ($CFG->emailchangeconfirmation) {
164 // Handle change of email carefully for non-trusted users
165 if (isset($usernew->email) and $user->email != $usernew->email && !has_capability('moodle/user:update', $systemcontext)) {
166 $a = new stdClass();
167 $a->newemail = $usernew->preference_newemail = $usernew->email;
168 $usernew->preference_newemailkey = random_string(20);
169 $usernew->preference_newemailattemptsleft = 3;
170 $a->oldemail = $usernew->email = $user->email;
172 $email_changed_html = $OUTPUT->box(get_string('auth_changingemailaddress', 'auth_email', $a), 'generalbox', 'notice');
173 $email_changed_html .= $OUTPUT->continue_button("$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id");
174 $email_changed = true;
178 $authplugin = get_auth_plugin($user->auth);
180 $usernew->timemodified = time();
182 // description editor element may not exist!
183 if (isset($usernew->description_editor)) {
184 $usernew = file_postupdate_standard_editor($usernew, 'description', $editoroptions, $personalcontext, 'user', 'profile', 0);
187 $DB->update_record('user', $usernew);
189 // pass a true $userold here
190 if (! $authplugin->user_update($user, $usernew)) {
191 // auth update failed, rollback for moodle
192 $DB->update_record('user', $user);
193 print_error('cannotupdateprofile');
196 //update preferences
197 useredit_update_user_preference($usernew);
199 //update interests
200 if (!empty($CFG->usetags)) {
201 useredit_update_interests($usernew, $usernew->interests);
204 //update user picture
205 if (!empty($CFG->gdversion) and empty($CFG->disableuserimages)) {
206 useredit_update_picture($usernew, $userform);
209 // update mail bounces
210 useredit_update_bounces($user, $usernew);
212 /// update forum track preference
213 useredit_update_trackforums($user, $usernew);
215 // save custom profile fields data
216 profile_save_data($usernew);
218 // If email was changed, send confirmation email now
219 if ($email_changed && $CFG->emailchangeconfirmation) {
220 $temp_user = fullclone($user);
221 $temp_user->email = $usernew->preference_newemail;
223 $a = new stdClass();
224 $a->url = $CFG->wwwroot . '/user/emailupdate.php?key=' . $usernew->preference_newemailkey . '&id=' . $user->id;
225 $a->site = $SITE->fullname;
226 $a->fullname = fullname($user, true);
228 $emailupdatemessage = get_string('auth_emailupdatemessage', 'auth_email', $a);
229 $emailupdatetitle = get_string('auth_emailupdatetitle', 'auth_email', $a);
231 //email confirmation directly rather than using messaging so they will definitely get an email
232 if (!$mail_results = email_to_user($temp_user, get_admin(), $emailupdatetitle, $emailupdatemessage)) {
233 die("could not send email!");
237 // reload from db
238 $usernew = $DB->get_record('user', array('id'=>$user->id));
239 events_trigger('user_updated', $usernew);
241 if ($USER->id == $user->id) {
242 // Override old $USER session variable if needed
243 foreach ((array)$usernew as $variable => $value) {
244 $USER->$variable = $value;
246 // preload custom fields
247 profile_load_custom_fields($USER);
250 if (is_siteadmin() and empty($SITE->shortname)) {
251 // fresh cli install - we need to finish site settings
252 redirect(new moodle_url('/admin/index.php'));
255 if (!$email_changed || !$CFG->emailchangeconfirmation) {
256 redirect("$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id");
260 // make sure we really are on the https page when https login required
261 $PAGE->verify_https_required();
264 /// Display page header
265 $streditmyprofile = get_string('editmyprofile');
266 $strparticipants = get_string('participants');
267 $userfullname = fullname($user, true);
269 $PAGE->set_title("$course->shortname: $streditmyprofile");
270 $PAGE->set_heading($course->fullname);
272 echo $OUTPUT->header();
274 if ($email_changed) {
275 echo $email_changed_html;
276 } else {
277 /// Finally display THE form
278 $userform->display();
281 /// and proper footer
282 echo $OUTPUT->footer();