3 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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
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());
53 $PAGE->set_context(get_system_context());
54 $PAGE->set_pagelayout('standard');
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
99 if ($course->id
== SITEID
) {
100 $coursecontext = get_context_instance(CONTEXT_SYSTEM
); // SYSTEM context
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');
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();
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, 'userid' => $user->id
));
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 // Users with 'moodle/user:update' can change their email address immediately
173 // Other users require a confirmation email
174 if (isset($usernew->email
) and $user->email
!= $usernew->email
&& !has_capability('moodle/user:update', $systemcontext)) {
176 $a->newemail
= $usernew->preference_newemail
= $usernew->email
;
177 $usernew->preference_newemailkey
= random_string(20);
178 $usernew->preference_newemailattemptsleft
= 3;
179 $a->oldemail
= $usernew->email
= $user->email
;
181 $email_changed_html = $OUTPUT->box(get_string('auth_changingemailaddress', 'auth', $a), 'generalbox', 'notice');
182 $email_changed_html .= $OUTPUT->continue_button("$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id");
183 $email_changed = true;
187 $authplugin = get_auth_plugin($user->auth
);
189 $usernew->timemodified
= time();
191 // description editor element may not exist!
192 if (isset($usernew->description_editor
)) {
193 $usernew = file_postupdate_standard_editor($usernew, 'description', $editoroptions, $personalcontext, 'user', 'profile', 0);
196 $DB->update_record('user', $usernew);
198 // pass a true $userold here
199 if (! $authplugin->user_update($user, $usernew)) {
200 // auth update failed, rollback for moodle
201 $DB->update_record('user', $user);
202 print_error('cannotupdateprofile');
206 useredit_update_user_preference($usernew);
209 if (!empty($CFG->usetags
)) {
210 useredit_update_interests($usernew, $usernew->interests
);
213 //update user picture
214 if (!empty($CFG->gdversion
) and empty($CFG->disableuserimages
)) {
215 useredit_update_picture($usernew, $userform);
218 // update mail bounces
219 useredit_update_bounces($user, $usernew);
221 /// update forum track preference
222 useredit_update_trackforums($user, $usernew);
224 // save custom profile fields data
225 profile_save_data($usernew);
227 // If email was changed and confirmation is required, send confirmation email now
228 if ($email_changed && $CFG->emailchangeconfirmation
) {
229 $temp_user = fullclone($user);
230 $temp_user->email
= $usernew->preference_newemail
;
233 $a->url
= $CFG->wwwroot
. '/user/emailupdate.php?key=' . $usernew->preference_newemailkey
. '&id=' . $user->id
;
234 $a->site
= format_string($SITE->fullname
, true, array('context' => get_context_instance(CONTEXT_COURSE
, SITEID
)));
235 $a->fullname
= fullname($user, true);
237 $emailupdatemessage = get_string('emailupdatemessage', 'auth', $a);
238 $emailupdatetitle = get_string('emailupdatetitle', 'auth', $a);
240 //email confirmation directly rather than using messaging so they will definitely get an email
241 $supportuser = generate_email_supportuser();
242 if (!$mail_results = email_to_user($temp_user, $supportuser, $emailupdatetitle, $emailupdatemessage)) {
243 die("could not send email!");
248 $usernew = $DB->get_record('user', array('id'=>$user->id
));
249 events_trigger('user_updated', $usernew);
251 if ($USER->id
== $user->id
) {
252 // Override old $USER session variable if needed
253 foreach ((array)$usernew as $variable => $value) {
254 $USER->$variable = $value;
256 // preload custom fields
257 profile_load_custom_fields($USER);
260 if (is_siteadmin() and empty($SITE->shortname
)) {
261 // fresh cli install - we need to finish site settings
262 redirect(new moodle_url('/admin/index.php'));
265 if (!$email_changed ||
!$CFG->emailchangeconfirmation
) {
266 redirect("$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id");
270 // make sure we really are on the https page when https login required
271 $PAGE->verify_https_required();
274 /// Display page header
275 $streditmyprofile = get_string('editmyprofile');
276 $strparticipants = get_string('participants');
277 $userfullname = fullname($user, true);
279 $PAGE->set_title("$course->shortname: $streditmyprofile");
280 $PAGE->set_heading($course->fullname
);
282 echo $OUTPUT->header();
284 if ($email_changed) {
285 echo $email_changed_html;
287 /// Finally display THE form
288 $userform->display();
291 /// and proper footer
292 echo $OUTPUT->footer();