MDL-30975 Docs Documenting report/livelogs
[moodle.git] / user / edit.php
blob6fa160819e9e34a53dd662fbc996512d45f3b3c8
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 $userform = new user_edit_form(null, array('editoroptions'=>$editoroptions));
157 if (empty($user->country)) {
158 // MDL-16308 - we must unset the value here so $CFG->country can be used as default one
159 unset($user->country);
161 $userform->set_data($user);
163 $email_changed = false;
165 if ($usernew = $userform->get_data()) {
167 add_to_log($course->id, 'user', 'update', "view.php?id=$user->id&course=$course->id", '');
169 $email_changed_html = '';
171 if ($CFG->emailchangeconfirmation) {
172 // Handle change of email carefully for non-trusted users
173 if (isset($usernew->email) and $user->email != $usernew->email && !has_capability('moodle/user:update', $systemcontext)) {
174 $a = new stdClass();
175 $a->newemail = $usernew->preference_newemail = $usernew->email;
176 $usernew->preference_newemailkey = random_string(20);
177 $usernew->preference_newemailattemptsleft = 3;
178 $a->oldemail = $usernew->email = $user->email;
180 $email_changed_html = $OUTPUT->box(get_string('auth_changingemailaddress', 'auth', $a), 'generalbox', 'notice');
181 $email_changed_html .= $OUTPUT->continue_button("$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id");
182 $email_changed = true;
186 $authplugin = get_auth_plugin($user->auth);
188 $usernew->timemodified = time();
190 // description editor element may not exist!
191 if (isset($usernew->description_editor)) {
192 $usernew = file_postupdate_standard_editor($usernew, 'description', $editoroptions, $personalcontext, 'user', 'profile', 0);
195 $DB->update_record('user', $usernew);
197 // pass a true $userold here
198 if (! $authplugin->user_update($user, $usernew)) {
199 // auth update failed, rollback for moodle
200 $DB->update_record('user', $user);
201 print_error('cannotupdateprofile');
204 //update preferences
205 useredit_update_user_preference($usernew);
207 //update interests
208 if (!empty($CFG->usetags)) {
209 useredit_update_interests($usernew, $usernew->interests);
212 //update user picture
213 if (!empty($CFG->gdversion) and empty($CFG->disableuserimages)) {
214 useredit_update_picture($usernew, $userform);
217 // update mail bounces
218 useredit_update_bounces($user, $usernew);
220 /// update forum track preference
221 useredit_update_trackforums($user, $usernew);
223 // save custom profile fields data
224 profile_save_data($usernew);
226 // If email was changed, send confirmation email now
227 if ($email_changed && $CFG->emailchangeconfirmation) {
228 $temp_user = fullclone($user);
229 $temp_user->email = $usernew->preference_newemail;
231 $a = new stdClass();
232 $a->url = $CFG->wwwroot . '/user/emailupdate.php?key=' . $usernew->preference_newemailkey . '&id=' . $user->id;
233 $a->site = format_string($SITE->fullname, true, array('context' => get_context_instance(CONTEXT_COURSE, SITEID)));
234 $a->fullname = fullname($user, true);
236 $emailupdatemessage = get_string('emailupdatemessage', 'auth', $a);
237 $emailupdatetitle = get_string('emailupdatetitle', 'auth', $a);
239 //email confirmation directly rather than using messaging so they will definitely get an email
240 if (!$mail_results = email_to_user($temp_user, get_admin(), $emailupdatetitle, $emailupdatemessage)) {
241 die("could not send email!");
245 // reload from db
246 $usernew = $DB->get_record('user', array('id'=>$user->id));
247 events_trigger('user_updated', $usernew);
249 if ($USER->id == $user->id) {
250 // Override old $USER session variable if needed
251 foreach ((array)$usernew as $variable => $value) {
252 $USER->$variable = $value;
254 // preload custom fields
255 profile_load_custom_fields($USER);
258 if (is_siteadmin() and empty($SITE->shortname)) {
259 // fresh cli install - we need to finish site settings
260 redirect(new moodle_url('/admin/index.php'));
263 if (!$email_changed || !$CFG->emailchangeconfirmation) {
264 redirect("$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id");
268 // make sure we really are on the https page when https login required
269 $PAGE->verify_https_required();
272 /// Display page header
273 $streditmyprofile = get_string('editmyprofile');
274 $strparticipants = get_string('participants');
275 $userfullname = fullname($user, true);
277 $PAGE->set_title("$course->shortname: $streditmyprofile");
278 $PAGE->set_heading($course->fullname);
280 echo $OUTPUT->header();
282 if ($email_changed) {
283 echo $email_changed_html;
284 } else {
285 /// Finally display THE form
286 $userform->display();
289 /// and proper footer
290 echo $OUTPUT->footer();