Commit | Line | Data |
---|---|---|
ce221eb5 | 1 | <?php |
ce221eb5 | 2 | // This file is part of Moodle - http://moodle.org/ |
3 | // | |
4 | // Moodle is free software: you can redistribute it and/or modify | |
5 | // it under the terms of the GNU General Public License as published by | |
6 | // the Free Software Foundation, either version 3 of the License, or | |
7 | // (at your option) any later version. | |
8 | // | |
9 | // Moodle is distributed in the hope that it will be useful, | |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | // GNU General Public License for more details. | |
13 | // | |
14 | // You should have received a copy of the GNU General Public License | |
15 | // along with Moodle. If not, see <http://www.gnu.org/licenses/>. | |
16 | ||
17 | /** | |
18 | * Allows you to edit a users profile | |
19 | * | |
20 | * @copyright 1999 Martin Dougiamas http://dougiamas.com | |
21 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
a2ed6e69 | 22 | * @package core_user |
ce221eb5 | 23 | */ |
24 | ||
25 | require_once('../config.php'); | |
26 | require_once($CFG->libdir.'/gdlib.php'); | |
27 | require_once($CFG->dirroot.'/user/edit_form.php'); | |
28 | require_once($CFG->dirroot.'/user/editlib.php'); | |
29 | require_once($CFG->dirroot.'/user/profile/lib.php'); | |
bb78e249 | 30 | require_once($CFG->dirroot.'/user/lib.php'); |
ce221eb5 | 31 | |
a2ed6e69 | 32 | // HTTPS is required in this page when $CFG->loginhttps enabled. |
17c70aa0 | 33 | $PAGE->https_required(); |
ce221eb5 | 34 | |
a2ed6e69 SH |
35 | $userid = optional_param('id', $USER->id, PARAM_INT); // User id. |
36 | $course = optional_param('course', SITEID, PARAM_INT); // Course id (defaults to Site). | |
69a35871 | 37 | $returnto = optional_param('returnto', null, PARAM_ALPHA); // Code determining where to return to after save. |
a2ed6e69 | 38 | $cancelemailchange = optional_param('cancelemailchange', 0, PARAM_INT); // Course id (defaults to Site). |
ce221eb5 | 39 | |
a2ed6e69 | 40 | $PAGE->set_url('/user/edit.php', array('course' => $course, 'id' => $userid)); |
ce221eb5 | 41 | |
a2ed6e69 | 42 | if (!$course = $DB->get_record('course', array('id' => $course))) { |
ce221eb5 | 43 | print_error('invalidcourseid'); |
44 | } | |
45 | ||
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'; | |
56f52742 | 51 | } |
ce221eb5 | 52 | redirect(get_login_url()); |
53 | } else { | |
0601e0ee | 54 | $PAGE->set_context(context_system::instance()); |
ce221eb5 | 55 | } |
56 | ||
a2ed6e69 | 57 | // Guest can not edit. |
ce221eb5 | 58 | if (isguestuser()) { |
59 | print_error('guestnoeditprofile'); | |
60 | } | |
61 | ||
a2ed6e69 SH |
62 | // The user profile we are editing. |
63 | if (!$user = $DB->get_record('user', array('id' => $userid))) { | |
ce221eb5 | 64 | print_error('invaliduserid'); |
65 | } | |
66 | ||
a2ed6e69 | 67 | // Guest can not be edited. |
ce221eb5 | 68 | if (isguestuser($user)) { |
69 | print_error('guestnoeditprofile'); | |
70 | } | |
71 | ||
a2ed6e69 | 72 | // User interests separated by commas. |
c4e868d5 | 73 | $user->interests = core_tag_tag::get_item_tags_array('core', 'user', $user->id); |
ce221eb5 | 74 | |
8df850ad DM |
75 | // Remote users cannot be edited. Note we have to perform the strict user_not_fully_set_up() check. |
76 | // Otherwise the remote user could end up in endless loop between user/view.php and here. | |
77 | // Required custom fields are not supported in MNet environment anyway. | |
ce221eb5 | 78 | if (is_mnet_remote_user($user)) { |
8df850ad | 79 | if (user_not_fully_set_up($user, true)) { |
a2ed6e69 | 80 | $hostwwwroot = $DB->get_field('mnet_host', 'wwwroot', array('id' => $user->mnethostid)); |
5f08e124 PL |
81 | print_error('usernotfullysetup', 'mnet', '', $hostwwwroot); |
82 | } | |
ce221eb5 | 83 | redirect($CFG->wwwroot . "/user/view.php?course={$course->id}"); |
84 | } | |
85 | ||
a2ed6e69 | 86 | // Load the appropriate auth plugin. |
99f9f85f PS |
87 | $userauth = get_auth_plugin($user->auth); |
88 | ||
89 | if (!$userauth->can_edit_profile()) { | |
90 | print_error('noprofileedit', 'auth'); | |
91 | } | |
92 | ||
93 | if ($editurl = $userauth->edit_profile_url()) { | |
a2ed6e69 | 94 | // This internal script not used. |
99f9f85f PS |
95 | redirect($editurl); |
96 | } | |
97 | ||
ce221eb5 | 98 | if ($course->id == SITEID) { |
a2ed6e69 | 99 | $coursecontext = context_system::instance(); // SYSTEM context. |
ce221eb5 | 100 | } else { |
a2ed6e69 | 101 | $coursecontext = context_course::instance($course->id); // Course context. |
ce221eb5 | 102 | } |
43731030 FM |
103 | $systemcontext = context_system::instance(); |
104 | $personalcontext = context_user::instance($user->id); | |
ce221eb5 | 105 | |
a2ed6e69 | 106 | // Check access control. |
ce221eb5 | 107 | if ($user->id == $USER->id) { |
a2ed6e69 | 108 | // Editing own profile - require_login() MUST NOT be used here, it would result in infinite loop! |
ce221eb5 | 109 | if (!has_capability('moodle/user:editownprofile', $systemcontext)) { |
110 | print_error('cannotedityourprofile'); | |
bb9a123a | 111 | } |
1cb3da36 | 112 | |
ce221eb5 | 113 | } else { |
a2ed6e69 | 114 | // Teachers, parents, etc. |
ce221eb5 | 115 | require_capability('moodle/user:editprofile', $personalcontext); |
a2ed6e69 | 116 | // No editing of guest user account. |
ce221eb5 | 117 | if (isguestuser($user->id)) { |
118 | print_error('guestnoeditprofileother'); | |
b0c90e6e | 119 | } |
a2ed6e69 SH |
120 | // No editing of primary admin! |
121 | if (is_siteadmin($user) and !is_siteadmin($USER)) { // Only admins may edit other admins. | |
4f622c38 | 122 | print_error('useradmineditadmin'); |
f5fc83e8 | 123 | } |
ce221eb5 | 124 | } |
f5fc83e8 | 125 | |
ce221eb5 | 126 | if ($user->deleted) { |
127 | echo $OUTPUT->header(); | |
128 | echo $OUTPUT->heading(get_string('userdeleted')); | |
129 | echo $OUTPUT->footer(); | |
130 | die; | |
131 | } | |
05c38e2b | 132 | |
9dbf62d2 MG |
133 | $PAGE->set_pagelayout('admin'); |
134 | $PAGE->set_context($personalcontext); | |
135 | if ($USER->id != $user->id) { | |
136 | $PAGE->navigation->extend_for_user($user); | |
137 | } else { | |
138 | if ($node = $PAGE->navigation->find('myprofile', navigation_node::TYPE_ROOTNODE)) { | |
139 | $node->force_open(); | |
140 | } | |
141 | } | |
142 | ||
a2ed6e69 | 143 | // Process email change cancellation. |
ce221eb5 | 144 | if ($cancelemailchange) { |
145 | cancel_email_update($user->id); | |
146 | } | |
14a6b7e1 | 147 | |
a2ed6e69 | 148 | // Load user preferences. |
ce221eb5 | 149 | useredit_load_preferences($user); |
f9903ed0 | 150 | |
a2ed6e69 | 151 | // Load custom profile fields data. |
ce221eb5 | 152 | profile_load_data($user); |
0be6f678 | 153 | |
2d836c0b | 154 | |
a2ed6e69 | 155 | // Prepare the editor and create form. |
e9de1cf4 RT |
156 | $editoroptions = array( |
157 | 'maxfiles' => EDITOR_UNLIMITED_FILES, | |
158 | 'maxbytes' => $CFG->maxbytes, | |
159 | 'trusttext' => false, | |
160 | 'forcehttps' => false, | |
161 | 'context' => $personalcontext | |
162 | ); | |
163 | ||
64f93798 | 164 | $user = file_prepare_standard_editor($user, 'description', $editoroptions, $personalcontext, 'user', 'profile', 0); |
4e782b32 RT |
165 | // Prepare filemanager draft area. |
166 | $draftitemid = 0; | |
167 | $filemanagercontext = $editoroptions['context']; | |
168 | $filemanageroptions = array('maxbytes' => $CFG->maxbytes, | |
169 | 'subdirs' => 0, | |
170 | 'maxfiles' => 1, | |
171 | 'accepted_types' => 'web_image'); | |
172 | file_prepare_draft_area($draftitemid, $filemanagercontext->id, 'user', 'newicon', 0, $filemanageroptions); | |
173 | $user->imagefile = $draftitemid; | |
a2ed6e69 | 174 | // Create form. |
69a35871 | 175 | $userform = new user_edit_form(new moodle_url($PAGE->url, array('returnto' => $returnto)), array( |
4e782b32 | 176 | 'editoroptions' => $editoroptions, |
fc3aa0fb | 177 | 'filemanageroptions' => $filemanageroptions, |
d6e7a63d | 178 | 'user' => $user)); |
d6ace123 | 179 | |
a2ed6e69 | 180 | $emailchanged = false; |
1e1c51a3 | 181 | |
ce221eb5 | 182 | if ($usernew = $userform->get_data()) { |
f9903ed0 | 183 | |
69a35871 FM |
184 | // Deciding where to send the user back in most cases. |
185 | if ($returnto === 'profile') { | |
186 | if ($course->id != SITEID) { | |
187 | $returnurl = new moodle_url('/user/view.php', array('id' => $user->id, 'course' => $course->id)); | |
188 | } else { | |
189 | $returnurl = new moodle_url('/user/profile.php', array('id' => $user->id)); | |
190 | } | |
191 | } else { | |
192 | $returnurl = new moodle_url('/user/preferences.php', array('userid' => $user->id)); | |
193 | } | |
194 | ||
a2ed6e69 | 195 | $emailchangedhtml = ''; |
d6ace123 | 196 | |
ce221eb5 | 197 | if ($CFG->emailchangeconfirmation) { |
a2ed6e69 SH |
198 | // Users with 'moodle/user:update' can change their email address immediately. |
199 | // Other users require a confirmation email. | |
ce221eb5 | 200 | if (isset($usernew->email) and $user->email != $usernew->email && !has_capability('moodle/user:update', $systemcontext)) { |
201 | $a = new stdClass(); | |
202 | $a->newemail = $usernew->preference_newemail = $usernew->email; | |
203 | $usernew->preference_newemailkey = random_string(20); | |
204 | $usernew->preference_newemailattemptsleft = 3; | |
205 | $a->oldemail = $usernew->email = $user->email; | |
206 | ||
a2ed6e69 | 207 | $emailchangedhtml = $OUTPUT->box(get_string('auth_changingemailaddress', 'auth', $a), 'generalbox', 'notice'); |
69a35871 | 208 | $emailchangedhtml .= $OUTPUT->continue_button($returnurl); |
a2ed6e69 | 209 | $emailchanged = true; |
d6ace123 | 210 | } |
ce221eb5 | 211 | } |
d6ace123 | 212 | |
ce221eb5 | 213 | $authplugin = get_auth_plugin($user->auth); |
a3447e10 | 214 | |
ce221eb5 | 215 | $usernew->timemodified = time(); |
42086054 | 216 | |
a2ed6e69 | 217 | // Description editor element may not exist! |
3d944c8b | 218 | if (isset($usernew->description_editor) && isset($usernew->description_editor['format'])) { |
64f93798 | 219 | $usernew = file_postupdate_standard_editor($usernew, 'description', $editoroptions, $personalcontext, 'user', 'profile', 0); |
42086054 | 220 | } |
8f0cd6ef | 221 | |
bb78e249 RT |
222 | // Pass a true old $user here. |
223 | if (!$authplugin->user_update($user, $usernew)) { | |
224 | // Auth update failed. | |
ce221eb5 | 225 | print_error('cannotupdateprofile'); |
226 | } | |
6bc1e5d5 | 227 | |
bb78e249 | 228 | // Update user with new profile data. |
9363073b | 229 | user_update_user($usernew, false, false); |
bb78e249 | 230 | |
a2ed6e69 | 231 | // Update preferences. |
ce221eb5 | 232 | useredit_update_user_preference($usernew); |
0be6f678 | 233 | |
a2ed6e69 | 234 | // Update interests. |
c4e868d5 | 235 | if (isset($usernew->interests)) { |
ce221eb5 | 236 | useredit_update_interests($usernew, $usernew->interests); |
237 | } | |
0be6f678 | 238 | |
a2ed6e69 | 239 | // Update user picture. |
689096bc | 240 | if (empty($CFG->disableuserimages)) { |
5407c5b0 | 241 | core_user::update_picture($usernew, $filemanageroptions); |
ce221eb5 | 242 | } |
d1c8eb14 | 243 | |
a2ed6e69 | 244 | // Update mail bounces. |
ce221eb5 | 245 | useredit_update_bounces($user, $usernew); |
7cbb4c96 | 246 | |
a2ed6e69 | 247 | // Update forum track preference. |
ce221eb5 | 248 | useredit_update_trackforums($user, $usernew); |
f9903ed0 | 249 | |
a2ed6e69 | 250 | // Save custom profile fields data. |
ce221eb5 | 251 | profile_save_data($usernew); |
f9a0ea69 | 252 | |
9363073b RT |
253 | // Trigger event. |
254 | \core\event\user_updated::create_from_userid($user->id)->trigger(); | |
255 | ||
e1132146 | 256 | // If email was changed and confirmation is required, send confirmation email now to the new address. |
a2ed6e69 SH |
257 | if ($emailchanged && $CFG->emailchangeconfirmation) { |
258 | $tempuser = $DB->get_record('user', array('id' => $user->id), '*', MUST_EXIST); | |
259 | $tempuser->email = $usernew->preference_newemail; | |
d6ace123 | 260 | |
ce221eb5 | 261 | $a = new stdClass(); |
262 | $a->url = $CFG->wwwroot . '/user/emailupdate.php?key=' . $usernew->preference_newemailkey . '&id=' . $user->id; | |
43731030 | 263 | $a->site = format_string($SITE->fullname, true, array('context' => context_course::instance(SITEID))); |
a2ed6e69 | 264 | $a->fullname = fullname($tempuser, true); |
d6ace123 | 265 | |
c6a074f8 PS |
266 | $emailupdatemessage = get_string('emailupdatemessage', 'auth', $a); |
267 | $emailupdatetitle = get_string('emailupdatetitle', 'auth', $a); | |
d6ace123 | 268 | |
a2ed6e69 | 269 | // Email confirmation directly rather than using messaging so they will definitely get an email. |
2b503e40 | 270 | $supportuser = core_user::get_support_user(); |
a2ed6e69 | 271 | if (!$mailresults = email_to_user($tempuser, $supportuser, $emailupdatetitle, $emailupdatemessage)) { |
ce221eb5 | 272 | die("could not send email!"); |
d6ace123 | 273 | } |
ce221eb5 | 274 | } |
d6ace123 | 275 | |
cf361a95 | 276 | // Reload from db, we need new full name on this page if we do not redirect. |
a2ed6e69 | 277 | $user = $DB->get_record('user', array('id' => $user->id), '*', MUST_EXIST); |
d6ace123 | 278 | |
ce221eb5 | 279 | if ($USER->id == $user->id) { |
a2ed6e69 | 280 | // Override old $USER session variable if needed. |
cf361a95 PS |
281 | foreach ((array)$user as $variable => $value) { |
282 | if ($variable === 'description' or $variable === 'password') { | |
283 | // These are not set for security nad perf reasons. | |
284 | continue; | |
285 | } | |
ce221eb5 | 286 | $USER->$variable = $value; |
d6ace123 | 287 | } |
a2ed6e69 | 288 | // Preload custom fields. |
a1248ca4 | 289 | profile_load_custom_fields($USER); |
1f33691c | 290 | } |
291 | ||
79efabd4 | 292 | if (is_siteadmin() and empty($SITE->shortname)) { |
a2ed6e69 | 293 | // Fresh cli install - we need to finish site settings. |
79efabd4 PS |
294 | redirect(new moodle_url('/admin/index.php')); |
295 | } | |
296 | ||
a2ed6e69 | 297 | if (!$emailchanged || !$CFG->emailchangeconfirmation) { |
69a35871 | 298 | redirect($returnurl); |
bb9a123a | 299 | } |
ce221eb5 | 300 | } |
0be6f678 | 301 | |
a2ed6e69 | 302 | // Make sure we really are on the https page when https login required. |
17c70aa0 PS |
303 | $PAGE->verify_https_required(); |
304 | ||
0236ec73 | 305 | |
a2ed6e69 | 306 | // Display page header. |
ce221eb5 | 307 | $streditmyprofile = get_string('editmyprofile'); |
308 | $strparticipants = get_string('participants'); | |
309 | $userfullname = fullname($user, true); | |
310 | ||
ce221eb5 | 311 | $PAGE->set_title("$course->shortname: $streditmyprofile"); |
880c5073 | 312 | $PAGE->set_heading($userfullname); |
ce221eb5 | 313 | |
314 | echo $OUTPUT->header(); | |
83f31bcf | 315 | echo $OUTPUT->heading($userfullname); |
ce221eb5 | 316 | |
a2ed6e69 SH |
317 | if ($emailchanged) { |
318 | echo $emailchangedhtml; | |
ce221eb5 | 319 | } else { |
a2ed6e69 | 320 | // Finally display THE form. |
ce221eb5 | 321 | $userform->display(); |
322 | } | |
a3f1f815 | 323 | |
a2ed6e69 | 324 | // And proper footer. |
ce221eb5 | 325 | echo $OUTPUT->footer(); |
1f33691c | 326 |