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');
31 require_once($CFG->dirroot
.'/user/lib.php');
33 //HTTPS is required in this page when $CFG->loginhttps enabled
34 $PAGE->https_required();
36 $userid = optional_param('id', $USER->id
, PARAM_INT
); // user id
37 $course = optional_param('course', SITEID
, PARAM_INT
); // course id (defaults to Site)
38 $cancelemailchange = optional_param('cancelemailchange', 0, PARAM_INT
); // course id (defaults to Site)
40 $PAGE->set_url('/user/edit.php', array('course'=>$course, 'id'=>$userid));
42 if (!$course = $DB->get_record('course', array('id'=>$course))) {
43 print_error('invalidcourseid');
46 if ($course->id
!= SITEID
) {
47 require_login($course);
48 } else if (!isloggedin()) {
49 if (empty($SESSION->wantsurl
)) {
50 $SESSION->wantsurl
= $CFG->httpswwwroot
.'/user/edit.php';
52 redirect(get_login_url());
54 $PAGE->set_context(context_system
::instance());
55 $PAGE->set_pagelayout('standard');
60 print_error('guestnoeditprofile');
63 // The user profile we are editing
64 if (!$user = $DB->get_record('user', array('id'=>$userid))) {
65 print_error('invaliduserid');
68 // Guest can not be edited
69 if (isguestuser($user)) {
70 print_error('guestnoeditprofile');
73 // User interests separated by commas
74 if (!empty($CFG->usetags
)) {
75 require_once($CFG->dirroot
.'/tag/lib.php');
76 $user->interests
= tag_get_tags_array('user', $user->id
);
79 // remote users cannot be edited
80 if (is_mnet_remote_user($user)) {
81 if (user_not_fully_set_up($user)) {
82 $hostwwwroot = $DB->get_field('mnet_host', 'wwwroot', array('id'=>$user->mnethostid
));
83 print_error('usernotfullysetup', 'mnet', '', $hostwwwroot);
85 redirect($CFG->wwwroot
. "/user/view.php?course={$course->id}");
88 // load the appropriate auth plugin
89 $userauth = get_auth_plugin($user->auth
);
91 if (!$userauth->can_edit_profile()) {
92 print_error('noprofileedit', 'auth');
95 if ($editurl = $userauth->edit_profile_url()) {
96 // this internal script not used
100 if ($course->id
== SITEID
) {
101 $coursecontext = context_system
::instance(); // SYSTEM context
103 $coursecontext = context_course
::instance($course->id
); // Course context
105 $systemcontext = context_system
::instance();
106 $personalcontext = context_user
::instance($user->id
);
108 // check access control
109 if ($user->id
== $USER->id
) {
110 //editing own profile - require_login() MUST NOT be used here, it would result in infinite loop!
111 if (!has_capability('moodle/user:editownprofile', $systemcontext)) {
112 print_error('cannotedityourprofile');
116 // teachers, parents, etc.
117 require_capability('moodle/user:editprofile', $personalcontext);
118 // no editing of guest user account
119 if (isguestuser($user->id
)) {
120 print_error('guestnoeditprofileother');
122 // no editing of primary admin!
123 if (is_siteadmin($user) and !is_siteadmin($USER)) { // Only admins may edit other admins
124 print_error('useradmineditadmin');
128 if ($user->deleted
) {
129 echo $OUTPUT->header();
130 echo $OUTPUT->heading(get_string('userdeleted'));
131 echo $OUTPUT->footer();
135 // Process email change cancellation
136 if ($cancelemailchange) {
137 cancel_email_update($user->id
);
140 //load user preferences
141 useredit_load_preferences($user);
143 //Load custom profile fields data
144 profile_load_data($user);
147 // Prepare the editor and create form
148 $editoroptions = array(
149 'maxfiles' => EDITOR_UNLIMITED_FILES
,
150 'maxbytes' => $CFG->maxbytes
,
151 'trusttext' => false,
152 'forcehttps' => false,
153 'context' => $personalcontext
156 $user = file_prepare_standard_editor($user, 'description', $editoroptions, $personalcontext, 'user', 'profile', 0);
157 // Prepare filemanager draft area.
159 $filemanagercontext = $editoroptions['context'];
160 $filemanageroptions = array('maxbytes' => $CFG->maxbytes
,
163 'accepted_types' => 'web_image');
164 file_prepare_draft_area($draftitemid, $filemanagercontext->id
, 'user', 'newicon', 0, $filemanageroptions);
165 $user->imagefile
= $draftitemid;
167 $userform = new user_edit_form(null, array(
168 'editoroptions' => $editoroptions,
169 'filemanageroptions' => $filemanageroptions,
170 'userid' => $user->id
));
171 if (empty($user->country
)) {
172 // MDL-16308 - we must unset the value here so $CFG->country can be used as default one
173 unset($user->country
);
175 $userform->set_data($user);
177 $email_changed = false;
179 if ($usernew = $userform->get_data()) {
181 $email_changed_html = '';
183 if ($CFG->emailchangeconfirmation
) {
184 // Users with 'moodle/user:update' can change their email address immediately
185 // Other users require a confirmation email
186 if (isset($usernew->email
) and $user->email
!= $usernew->email
&& !has_capability('moodle/user:update', $systemcontext)) {
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
;
193 $email_changed_html = $OUTPUT->box(get_string('auth_changingemailaddress', 'auth', $a), 'generalbox', 'notice');
194 $email_changed_html .= $OUTPUT->continue_button("$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id");
195 $email_changed = true;
199 $authplugin = get_auth_plugin($user->auth
);
201 $usernew->timemodified
= time();
203 // description editor element may not exist!
204 if (isset($usernew->description_editor
)) {
205 $usernew = file_postupdate_standard_editor($usernew, 'description', $editoroptions, $personalcontext, 'user', 'profile', 0);
208 // Pass a true old $user here.
209 if (!$authplugin->user_update($user, $usernew)) {
210 // Auth update failed.
211 print_error('cannotupdateprofile');
214 // Update user with new profile data.
215 user_update_user($usernew, false);
218 useredit_update_user_preference($usernew);
221 if (!empty($CFG->usetags
)) {
222 useredit_update_interests($usernew, $usernew->interests
);
225 //update user picture
226 if (empty($CFG->disableuserimages
)) {
227 useredit_update_picture($usernew, $userform, $filemanageroptions);
230 // update mail bounces
231 useredit_update_bounces($user, $usernew);
233 /// update forum track preference
234 useredit_update_trackforums($user, $usernew);
236 // save custom profile fields data
237 profile_save_data($usernew);
239 // If email was changed and confirmation is required, send confirmation email now
240 if ($email_changed && $CFG->emailchangeconfirmation
) {
241 $temp_user = fullclone($user);
242 $temp_user->email
= $usernew->preference_newemail
;
245 $a->url
= $CFG->wwwroot
. '/user/emailupdate.php?key=' . $usernew->preference_newemailkey
. '&id=' . $user->id
;
246 $a->site
= format_string($SITE->fullname
, true, array('context' => context_course
::instance(SITEID
)));
247 $a->fullname
= fullname($user, true);
249 $emailupdatemessage = get_string('emailupdatemessage', 'auth', $a);
250 $emailupdatetitle = get_string('emailupdatetitle', 'auth', $a);
252 //email confirmation directly rather than using messaging so they will definitely get an email
253 $supportuser = core_user
::get_support_user();
254 if (!$mail_results = email_to_user($temp_user, $supportuser, $emailupdatetitle, $emailupdatemessage)) {
255 die("could not send email!");
260 $usernew = $DB->get_record('user', array('id'=>$user->id
));
262 if ($USER->id
== $user->id
) {
263 // Override old $USER session variable if needed
264 foreach ((array)$usernew as $variable => $value) {
265 $USER->$variable = $value;
267 // preload custom fields
268 profile_load_custom_fields($USER);
271 if (is_siteadmin() and empty($SITE->shortname
)) {
272 // fresh cli install - we need to finish site settings
273 redirect(new moodle_url('/admin/index.php'));
276 if (!$email_changed ||
!$CFG->emailchangeconfirmation
) {
277 redirect("$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id");
281 // make sure we really are on the https page when https login required
282 $PAGE->verify_https_required();
285 /// Display page header
286 $streditmyprofile = get_string('editmyprofile');
287 $strparticipants = get_string('participants');
288 $userfullname = fullname($user, true);
290 $PAGE->set_title("$course->shortname: $streditmyprofile");
291 $PAGE->set_heading($course->fullname
);
293 echo $OUTPUT->header();
294 echo $OUTPUT->heading($userfullname);
296 if ($email_changed) {
297 echo $email_changed_html;
299 /// Finally display THE form
300 $userform->display();
303 /// and proper footer
304 echo $OUTPUT->footer();