Merge branch 'w23_MDL-39465_m23_environment' of git://github.com/skodak/moodle into...
[moodle.git] / user / edit.php
blob0ca75096f073c90dd41371c607a9db1423b72e1b
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));
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(get_system_context());
54 $PAGE->set_pagelayout('standard');
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 = get_context_instance(CONTEXT_SYSTEM); // SYSTEM context
101 } else {
102 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); // Course context
104 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
105 $personalcontext = get_context_instance(CONTEXT_USER, $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 // Process email change cancellation
135 if ($cancelemailchange) {
136 cancel_email_update($user->id);
139 //load user preferences
140 useredit_load_preferences($user);
142 //Load custom profile fields data
143 profile_load_data($user);
146 // Prepare the editor and create form
147 $editoroptions = array(
148 'maxfiles' => EDITOR_UNLIMITED_FILES,
149 'maxbytes' => $CFG->maxbytes,
150 'trusttext' => false,
151 'forcehttps' => false,
152 'context' => $personalcontext
155 $user = file_prepare_standard_editor($user, 'description', $editoroptions, $personalcontext, 'user', 'profile', 0);
156 // Prepare filemanager draft area.
157 $draftitemid = 0;
158 $filemanagercontext = $editoroptions['context'];
159 $filemanageroptions = array('maxbytes' => $CFG->maxbytes,
160 'subdirs' => 0,
161 'maxfiles' => 1,
162 'accepted_types' => 'web_image');
163 file_prepare_draft_area($draftitemid, $filemanagercontext->id, 'user', 'newicon', 0, $filemanageroptions);
164 $user->imagefile = $draftitemid;
165 //create form
166 $userform = new user_edit_form(null, array(
167 'editoroptions' => $editoroptions,
168 'filemanageroptions' => $filemanageroptions,
169 'userid' => $user->id));
170 if (empty($user->country)) {
171 // MDL-16308 - we must unset the value here so $CFG->country can be used as default one
172 unset($user->country);
174 $userform->set_data($user);
176 $email_changed = false;
178 if ($usernew = $userform->get_data()) {
180 add_to_log($course->id, 'user', 'update', "view.php?id=$user->id&course=$course->id", '');
182 $email_changed_html = '';
184 if ($CFG->emailchangeconfirmation) {
185 // Users with 'moodle/user:update' can change their email address immediately
186 // Other users require a confirmation email
187 if (isset($usernew->email) and $user->email != $usernew->email && !has_capability('moodle/user:update', $systemcontext)) {
188 $a = new stdClass();
189 $a->newemail = $usernew->preference_newemail = $usernew->email;
190 $usernew->preference_newemailkey = random_string(20);
191 $usernew->preference_newemailattemptsleft = 3;
192 $a->oldemail = $usernew->email = $user->email;
194 $email_changed_html = $OUTPUT->box(get_string('auth_changingemailaddress', 'auth', $a), 'generalbox', 'notice');
195 $email_changed_html .= $OUTPUT->continue_button("$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id");
196 $email_changed = true;
200 $authplugin = get_auth_plugin($user->auth);
202 $usernew->timemodified = time();
204 // description editor element may not exist!
205 if (isset($usernew->description_editor)) {
206 $usernew = file_postupdate_standard_editor($usernew, 'description', $editoroptions, $personalcontext, 'user', 'profile', 0);
209 $DB->update_record('user', $usernew);
211 // pass a true $userold here
212 if (! $authplugin->user_update($user, $usernew)) {
213 // auth update failed, rollback for moodle
214 $DB->update_record('user', $user);
215 print_error('cannotupdateprofile');
218 //update preferences
219 useredit_update_user_preference($usernew);
221 //update interests
222 if (!empty($CFG->usetags)) {
223 useredit_update_interests($usernew, $usernew->interests);
226 //update user picture
227 if (!empty($CFG->gdversion) and empty($CFG->disableuserimages)) {
228 useredit_update_picture($usernew, $userform, $filemanageroptions);
231 // update mail bounces
232 useredit_update_bounces($user, $usernew);
234 /// update forum track preference
235 useredit_update_trackforums($user, $usernew);
237 // save custom profile fields data
238 profile_save_data($usernew);
240 // If email was changed and confirmation is required, send confirmation email now
241 if ($email_changed && $CFG->emailchangeconfirmation) {
242 $temp_user = fullclone($user);
243 $temp_user->email = $usernew->preference_newemail;
245 $a = new stdClass();
246 $a->url = $CFG->wwwroot . '/user/emailupdate.php?key=' . $usernew->preference_newemailkey . '&id=' . $user->id;
247 $a->site = format_string($SITE->fullname, true, array('context' => get_context_instance(CONTEXT_COURSE, SITEID)));
248 $a->fullname = fullname($user, true);
250 $emailupdatemessage = get_string('emailupdatemessage', 'auth', $a);
251 $emailupdatetitle = get_string('emailupdatetitle', 'auth', $a);
253 //email confirmation directly rather than using messaging so they will definitely get an email
254 $supportuser = generate_email_supportuser();
255 if (!$mail_results = email_to_user($temp_user, $supportuser, $emailupdatetitle, $emailupdatemessage)) {
256 die("could not send email!");
260 // reload from db
261 $usernew = $DB->get_record('user', array('id'=>$user->id));
262 events_trigger('user_updated', $usernew);
264 if ($USER->id == $user->id) {
265 // Override old $USER session variable if needed
266 foreach ((array)$usernew as $variable => $value) {
267 $USER->$variable = $value;
269 // preload custom fields
270 profile_load_custom_fields($USER);
273 if (is_siteadmin() and empty($SITE->shortname)) {
274 // fresh cli install - we need to finish site settings
275 redirect(new moodle_url('/admin/index.php'));
278 if (!$email_changed || !$CFG->emailchangeconfirmation) {
279 redirect("$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id");
283 // make sure we really are on the https page when https login required
284 $PAGE->verify_https_required();
287 /// Display page header
288 $streditmyprofile = get_string('editmyprofile');
289 $strparticipants = get_string('participants');
290 $userfullname = fullname($user, true);
292 $PAGE->set_title("$course->shortname: $streditmyprofile");
293 $PAGE->set_heading($course->fullname);
295 echo $OUTPUT->header();
296 echo $OUTPUT->heading($userfullname);
298 if ($email_changed) {
299 echo $email_changed_html;
300 } else {
301 /// Finally display THE form
302 $userform->display();
305 /// and proper footer
306 echo $OUTPUT->footer();