MDL-25476 messages: switched some notification emails to come from the support contac...
[moodle.git] / user / edit.php
CommitLineData
ce221eb5 1<?php
2
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.
14//
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/>.
17
18/**
19 * Allows you to edit a users profile
20 *
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
24 */
25
26require_once('../config.php');
27require_once($CFG->libdir.'/gdlib.php');
28require_once($CFG->dirroot.'/user/edit_form.php');
29require_once($CFG->dirroot.'/user/editlib.php');
30require_once($CFG->dirroot.'/user/profile/lib.php');
31
17c70aa0
PS
32//HTTPS is required in this page when $CFG->loginhttps enabled
33$PAGE->https_required();
ce221eb5 34
35$userid = optional_param('id', $USER->id, PARAM_INT); // user id
36$course = optional_param('course', SITEID, PARAM_INT); // course id (defaults to Site)
17c70aa0 37$cancelemailchange = optional_param('cancelemailchange', 0, PARAM_INT); // course id (defaults to Site)
ce221eb5 38
01ec550b 39$PAGE->set_url('/user/edit.php', array('course'=>$course, 'id'=>$userid));
ce221eb5 40
41if (!$course = $DB->get_record('course', array('id'=>$course))) {
42 print_error('invalidcourseid');
43}
44
45if ($course->id != SITEID) {
46 require_login($course);
47} else if (!isloggedin()) {
48 if (empty($SESSION->wantsurl)) {
49 $SESSION->wantsurl = $CFG->httpswwwroot.'/user/edit.php';
56f52742 50 }
ce221eb5 51 redirect(get_login_url());
52} else {
01ec550b
SH
53 $PAGE->set_context(get_system_context());
54 $PAGE->set_pagelayout('standard');
ce221eb5 55}
56
57// Guest can not edit
58if (isguestuser()) {
59 print_error('guestnoeditprofile');
60}
61
62// The user profile we are editing
63if (!$user = $DB->get_record('user', array('id'=>$userid))) {
64 print_error('invaliduserid');
65}
66
67// Guest can not be edited
68if (isguestuser($user)) {
69 print_error('guestnoeditprofile');
70}
71
72// User interests separated by commas
73if (!empty($CFG->usetags)) {
74 require_once($CFG->dirroot.'/tag/lib.php');
75 $user->interests = tag_get_tags_array('user', $user->id);
76}
77
78// remote users cannot be edited
79if (is_mnet_remote_user($user)) {
5f08e124
PL
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);
83 }
ce221eb5 84 redirect($CFG->wwwroot . "/user/view.php?course={$course->id}");
85}
86
99f9f85f
PS
87// load the appropriate auth plugin
88$userauth = get_auth_plugin($user->auth);
89
90if (!$userauth->can_edit_profile()) {
91 print_error('noprofileedit', 'auth');
92}
93
94if ($editurl = $userauth->edit_profile_url()) {
95 // this internal script not used
96 redirect($editurl);
97}
98
ce221eb5 99if ($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
103}
104$systemcontext = get_context_instance(CONTEXT_SYSTEM);
105$personalcontext = get_context_instance(CONTEXT_USER, $user->id);
106
107// check access control
108if ($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');
bb9a123a 112 }
1cb3da36 113
ce221eb5 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');
b0c90e6e 120 }
ce221eb5 121 // no editing of primary admin!
4f622c38
PS
122 if (is_siteadmin($user) and !is_siteadmin($USER)) { // Only admins may edit other admins
123 print_error('useradmineditadmin');
f5fc83e8 124 }
ce221eb5 125}
f5fc83e8 126
ce221eb5 127if ($user->deleted) {
128 echo $OUTPUT->header();
129 echo $OUTPUT->heading(get_string('userdeleted'));
130 echo $OUTPUT->footer();
131 die;
132}
05c38e2b 133
ce221eb5 134// Process email change cancellation
135if ($cancelemailchange) {
136 cancel_email_update($user->id);
137}
14a6b7e1 138
ce221eb5 139//load user preferences
140useredit_load_preferences($user);
f9903ed0 141
ce221eb5 142//Load custom profile fields data
143profile_load_data($user);
0be6f678 144
2d836c0b 145
8bdc9cac 146// Prepare the editor and create form
e9de1cf4
RT
147$editoroptions = array(
148 'maxfiles' => EDITOR_UNLIMITED_FILES,
149 'maxbytes' => $CFG->maxbytes,
150 'trusttext' => false,
151 'forcehttps' => false,
152 'context' => $personalcontext
153);
154
64f93798 155$user = file_prepare_standard_editor($user, 'description', $editoroptions, $personalcontext, 'user', 'profile', 0);
4e782b32
RT
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');
163file_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));
ce221eb5 169if (empty($user->country)) {
170 // MDL-16308 - we must unset the value here so $CFG->country can be used as default one
171 unset($user->country);
172}
173$userform->set_data($user);
d6ace123 174
ce221eb5 175$email_changed = false;
1e1c51a3 176
ce221eb5 177if ($usernew = $userform->get_data()) {
f9903ed0 178
ce221eb5 179 add_to_log($course->id, 'user', 'update', "view.php?id=$user->id&course=$course->id", '');
d6ace123 180
ce221eb5 181 $email_changed_html = '';
d6ace123 182
ce221eb5 183 if ($CFG->emailchangeconfirmation) {
6737eda4
AD
184 // Users with 'moodle/user:update' can change their email address immediately
185 // Other users require a confirmation email
ce221eb5 186 if (isset($usernew->email) and $user->email != $usernew->email && !has_capability('moodle/user:update', $systemcontext)) {
187 $a = new stdClass();
188 $a->newemail = $usernew->preference_newemail = $usernew->email;
189 $usernew->preference_newemailkey = random_string(20);
190 $usernew->preference_newemailattemptsleft = 3;
191 $a->oldemail = $usernew->email = $user->email;
192
c6a074f8 193 $email_changed_html = $OUTPUT->box(get_string('auth_changingemailaddress', 'auth', $a), 'generalbox', 'notice');
ce221eb5 194 $email_changed_html .= $OUTPUT->continue_button("$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id");
195 $email_changed = true;
d6ace123 196 }
ce221eb5 197 }
d6ace123 198
ce221eb5 199 $authplugin = get_auth_plugin($user->auth);
a3447e10 200
ce221eb5 201 $usernew->timemodified = time();
42086054
SH
202
203 // description editor element may not exist!
204 if (isset($usernew->description_editor)) {
64f93798 205 $usernew = file_postupdate_standard_editor($usernew, 'description', $editoroptions, $personalcontext, 'user', 'profile', 0);
42086054 206 }
8f0cd6ef 207
ce221eb5 208 $DB->update_record('user', $usernew);
2d836c0b 209
ce221eb5 210 // pass a true $userold here
64521d31 211 if (! $authplugin->user_update($user, $usernew)) {
ce221eb5 212 // auth update failed, rollback for moodle
213 $DB->update_record('user', $user);
214 print_error('cannotupdateprofile');
215 }
6bc1e5d5 216
ce221eb5 217 //update preferences
218 useredit_update_user_preference($usernew);
0be6f678 219
ce221eb5 220 //update interests
221 if (!empty($CFG->usetags)) {
222 useredit_update_interests($usernew, $usernew->interests);
223 }
0be6f678 224
ce221eb5 225 //update user picture
226 if (!empty($CFG->gdversion) and empty($CFG->disableuserimages)) {
4e782b32 227 useredit_update_picture($usernew, $userform, $filemanageroptions);
ce221eb5 228 }
d1c8eb14 229
ce221eb5 230 // update mail bounces
231 useredit_update_bounces($user, $usernew);
7cbb4c96 232
ce221eb5 233 /// update forum track preference
234 useredit_update_trackforums($user, $usernew);
f9903ed0 235
ce221eb5 236 // save custom profile fields data
237 profile_save_data($usernew);
f9a0ea69 238
6737eda4 239 // If email was changed and confirmation is required, send confirmation email now
ce221eb5 240 if ($email_changed && $CFG->emailchangeconfirmation) {
241 $temp_user = fullclone($user);
242 $temp_user->email = $usernew->preference_newemail;
d6ace123 243
ce221eb5 244 $a = new stdClass();
245 $a->url = $CFG->wwwroot . '/user/emailupdate.php?key=' . $usernew->preference_newemailkey . '&id=' . $user->id;
91d284c1 246 $a->site = format_string($SITE->fullname, true, array('context' => get_context_instance(CONTEXT_COURSE, SITEID)));
ce221eb5 247 $a->fullname = fullname($user, true);
d6ace123 248
c6a074f8
PS
249 $emailupdatemessage = get_string('emailupdatemessage', 'auth', $a);
250 $emailupdatetitle = get_string('emailupdatetitle', 'auth', $a);
d6ace123 251
d8f14128 252 //email confirmation directly rather than using messaging so they will definitely get an email
6737eda4
AD
253 $supportuser = generate_email_supportuser();
254 if (!$mail_results = email_to_user($temp_user, $supportuser, $emailupdatetitle, $emailupdatemessage)) {
ce221eb5 255 die("could not send email!");
d6ace123 256 }
ce221eb5 257 }
d6ace123 258
ce221eb5 259 // reload from db
260 $usernew = $DB->get_record('user', array('id'=>$user->id));
261 events_trigger('user_updated', $usernew);
d6ace123 262
ce221eb5 263 if ($USER->id == $user->id) {
264 // Override old $USER session variable if needed
265 foreach ((array)$usernew as $variable => $value) {
266 $USER->$variable = $value;
d6ace123 267 }
a1248ca4
PS
268 // preload custom fields
269 profile_load_custom_fields($USER);
1f33691c 270 }
271
79efabd4
PS
272 if (is_siteadmin() and empty($SITE->shortname)) {
273 // fresh cli install - we need to finish site settings
274 redirect(new moodle_url('/admin/index.php'));
275 }
276
ce221eb5 277 if (!$email_changed || !$CFG->emailchangeconfirmation) {
278 redirect("$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id");
bb9a123a 279 }
ce221eb5 280}
0be6f678 281
17c70aa0
PS
282// make sure we really are on the https page when https login required
283$PAGE->verify_https_required();
284
0236ec73 285
ce221eb5 286/// Display page header
287$streditmyprofile = get_string('editmyprofile');
288$strparticipants = get_string('participants');
289$userfullname = fullname($user, true);
290
ce221eb5 291$PAGE->set_title("$course->shortname: $streditmyprofile");
292$PAGE->set_heading($course->fullname);
293
294echo $OUTPUT->header();
295
ce221eb5 296if ($email_changed) {
297 echo $email_changed_html;
298} else {
299/// Finally display THE form
300 $userform->display();
301}
a3f1f815 302
d8734783 303/// and proper footer
ce221eb5 304echo $OUTPUT->footer();
1f33691c 305