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');
34 $userid = optional_param('id', $USER->id
, PARAM_INT
); // user id
35 $course = optional_param('course', SITEID
, PARAM_INT
); // course id (defaults to Site)
36 $cancelemailchange = optional_param('cancelemailchange', false, PARAM_INT
); // course id (defaults to Site)
38 $url = new moodle_url('/user/edit.php', array('course'=>$course));
39 if ($userid !== $USER->id
) {
40 $url->param('id', $userid);
44 if (!$course = $DB->get_record('course', array('id'=>$course))) {
45 print_error('invalidcourseid');
48 if ($course->id
!= SITEID
) {
49 require_login($course);
50 } else if (!isloggedin()) {
51 if (empty($SESSION->wantsurl
)) {
52 $SESSION->wantsurl
= $CFG->httpswwwroot
.'/user/edit.php';
54 redirect(get_login_url());
56 $PAGE->set_course($course);
61 print_error('guestnoeditprofile');
64 // The user profile we are editing
65 if (!$user = $DB->get_record('user', array('id'=>$userid))) {
66 print_error('invaliduserid');
69 // Guest can not be edited
70 if (isguestuser($user)) {
71 print_error('guestnoeditprofile');
74 // User interests separated by commas
75 if (!empty($CFG->usetags
)) {
76 require_once($CFG->dirroot
.'/tag/lib.php');
77 $user->interests
= tag_get_tags_array('user', $user->id
);
80 // remote users cannot be edited
81 if (is_mnet_remote_user($user)) {
82 if (user_not_fully_set_up($user)) {
83 $hostwwwroot = $DB->get_field('mnet_host', 'wwwroot', array('id'=>$user->mnethostid
));
84 print_error('usernotfullysetup', 'mnet', '', $hostwwwroot);
86 redirect($CFG->wwwroot
. "/user/view.php?course={$course->id}");
89 if ($course->id
== SITEID
) {
90 $coursecontext = get_context_instance(CONTEXT_SYSTEM
); // SYSTEM context
92 $coursecontext = get_context_instance(CONTEXT_COURSE
, $course->id
); // Course context
94 $systemcontext = get_context_instance(CONTEXT_SYSTEM
);
95 $personalcontext = get_context_instance(CONTEXT_USER
, $user->id
);
97 // check access control
98 if ($user->id
== $USER->id
) {
99 //editing own profile - require_login() MUST NOT be used here, it would result in infinite loop!
100 if (!has_capability('moodle/user:editownprofile', $systemcontext)) {
101 print_error('cannotedityourprofile');
105 // teachers, parents, etc.
106 require_capability('moodle/user:editprofile', $personalcontext);
107 // no editing of guest user account
108 if (isguestuser($user->id
)) {
109 print_error('guestnoeditprofileother');
111 // no editing of primary admin!
112 if (is_primary_admin($user->id
)) {
113 print_error('adminprimarynoedit');
117 if ($user->deleted
) {
118 echo $OUTPUT->header();
119 echo $OUTPUT->heading(get_string('userdeleted'));
120 echo $OUTPUT->footer();
124 // Process email change cancellation
125 if ($cancelemailchange) {
126 cancel_email_update($user->id
);
129 //load user preferences
130 useredit_load_preferences($user);
132 //Load custom profile fields data
133 profile_load_data($user);
136 // Prepare the editor and create form
137 $editoroptions = array('maxfiles'=>EDITOR_UNLIMITED_FILES
, 'maxbytes'=>$CFG->maxbytes
, 'trusttext'=>false, 'forcehttps'=>false);
138 $user = file_prepare_standard_editor($user, 'description', $editoroptions, $personalcontext, 'user', 'profile', 0);
139 $userform = new user_edit_form(null, array('editoroptions'=>$editoroptions));
140 if (empty($user->country
)) {
141 // MDL-16308 - we must unset the value here so $CFG->country can be used as default one
142 unset($user->country
);
144 $userform->set_data($user);
146 $email_changed = false;
148 if ($usernew = $userform->get_data()) {
150 add_to_log($course->id
, 'user', 'update', "view.php?id=$user->id&course=$course->id", '');
152 $email_changed_html = '';
154 if ($CFG->emailchangeconfirmation
) {
155 // Handle change of email carefully for non-trusted users
156 if (isset($usernew->email
) and $user->email
!= $usernew->email
&& !has_capability('moodle/user:update', $systemcontext)) {
158 $a->newemail
= $usernew->preference_newemail
= $usernew->email
;
159 $usernew->preference_newemailkey
= random_string(20);
160 $usernew->preference_newemailattemptsleft
= 3;
161 $a->oldemail
= $usernew->email
= $user->email
;
163 $email_changed_html = $OUTPUT->box(get_string('auth_changingemailaddress', 'auth_email', $a), 'generalbox', 'notice');
164 $email_changed_html .= $OUTPUT->continue_button("$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id");
165 $email_changed = true;
169 $authplugin = get_auth_plugin($user->auth
);
171 $usernew->timemodified
= time();
173 // description editor element may not exist!
174 if (isset($usernew->description_editor
)) {
175 $usernew = file_postupdate_standard_editor($usernew, 'description', $editoroptions, $personalcontext, 'user', 'profile', 0);
178 $DB->update_record('user', $usernew);
180 // pass a true $userold here
181 if (! $authplugin->user_update($user, $usernew)) {
182 // auth update failed, rollback for moodle
183 $DB->update_record('user', $user);
184 print_error('cannotupdateprofile');
188 useredit_update_user_preference($usernew);
191 if (!empty($CFG->usetags
)) {
192 useredit_update_interests($usernew, $usernew->interests
);
195 //update user picture
196 if (!empty($CFG->gdversion
) and empty($CFG->disableuserimages
)) {
197 useredit_update_picture($usernew, $userform);
200 // update mail bounces
201 useredit_update_bounces($user, $usernew);
203 /// update forum track preference
204 useredit_update_trackforums($user, $usernew);
206 // save custom profile fields data
207 profile_save_data($usernew);
209 // If email was changed, send confirmation email now
210 if ($email_changed && $CFG->emailchangeconfirmation
) {
211 $temp_user = fullclone($user);
212 $temp_user->email
= $usernew->preference_newemail
;
213 $temp_user->emailstop
= NULL;
216 $a->url
= $CFG->wwwroot
. '/user/emailupdate.php?key=' . $usernew->preference_newemailkey
. '&id=' . $user->id
;
217 $a->site
= $SITE->fullname
;
218 $a->fullname
= fullname($user, true);
220 $emailupdatemessage = get_string('auth_emailupdatemessage', 'auth_email', $a);
221 $emailupdatetitle = get_string('auth_emailupdatetitle', 'auth_email', $a);
223 if (!$mail_results = email_to_user($temp_user, get_admin(), $emailupdatetitle, $emailupdatemessage)) {
224 die("could not send email!");
229 $usernew = $DB->get_record('user', array('id'=>$user->id
));
230 events_trigger('user_updated', $usernew);
232 if ($USER->id
== $user->id
) {
233 // Override old $USER session variable if needed
234 foreach ((array)$usernew as $variable => $value) {
235 $USER->$variable = $value;
237 // preload custom fields
238 profile_load_custom_fields($USER);
241 if (is_siteadmin() and empty($SITE->shortname
)) {
242 // fresh cli install - we need to finish site settings
243 redirect(new moodle_url('/admin/index.php'));
246 if (!$email_changed ||
!$CFG->emailchangeconfirmation
) {
247 redirect("$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id");
252 /// Display page header
253 $streditmyprofile = get_string('editmyprofile');
254 $strparticipants = get_string('participants');
255 $userfullname = fullname($user, true);
257 $PAGE->set_title("$course->shortname: $streditmyprofile");
258 $PAGE->set_heading($course->fullname
);
260 echo $OUTPUT->header();
262 if ($email_changed) {
263 echo $email_changed_html;
265 /// Finally display THE form
266 $userform->display();
269 /// and proper footer
270 echo $OUTPUT->footer();