Merge branch 'MDL-52597-29' of git://github.com/andrewnicols/moodle into MOODLE_29_STABLE
[moodle.git] / user / editadvanced.php
blobaa74c8d710c0d480a0bdaa4aab16bf44c1f6357e
1 <?php
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.
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/>.
17 /**
18 * Allows you to edit a users profile
20 * @copyright 1999 Martin Dougiamas http://dougiamas.com
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22 * @package core_user
25 require_once('../config.php');
26 require_once($CFG->libdir.'/gdlib.php');
27 require_once($CFG->libdir.'/adminlib.php');
28 require_once($CFG->dirroot.'/user/editadvanced_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 $id = optional_param('id', $USER->id, PARAM_INT); // User id; -1 if creating new user.
37 $course = optional_param('course', SITEID, PARAM_INT); // Course id (defaults to Site).
38 $returnto = optional_param('returnto', null, PARAM_ALPHA); // Code determining where to return to after save.
40 $PAGE->set_url('/user/editadvanced.php', array('course' => $course, 'id' => $id));
42 $course = $DB->get_record('course', array('id' => $course), '*', MUST_EXIST);
44 if (!empty($USER->newadminuser)) {
45 // Ignore double clicks, we must finish all operations before cancelling request.
46 ignore_user_abort(true);
48 $PAGE->set_course($SITE);
49 $PAGE->set_pagelayout('maintenance');
50 } else {
51 if ($course->id == SITEID) {
52 require_login();
53 $PAGE->set_context(context_system::instance());
54 } else {
55 require_login($course);
57 $PAGE->set_pagelayout('admin');
60 if ($course->id == SITEID) {
61 $coursecontext = context_system::instance(); // SYSTEM context.
62 } else {
63 $coursecontext = context_course::instance($course->id); // Course context.
65 $systemcontext = context_system::instance();
67 if ($id == -1) {
68 // Creating new user.
69 $user = new stdClass();
70 $user->id = -1;
71 $user->auth = 'manual';
72 $user->confirmed = 1;
73 $user->deleted = 0;
74 $user->timezone = '99';
75 require_capability('moodle/user:create', $systemcontext);
76 admin_externalpage_setup('addnewuser', '', array('id' => -1));
77 } else {
78 // Editing existing user.
79 require_capability('moodle/user:update', $systemcontext);
80 $user = $DB->get_record('user', array('id' => $id), '*', MUST_EXIST);
81 $PAGE->set_context(context_user::instance($user->id));
82 $PAGE->navbar->includesettingsbase = true;
83 if ($user->id != $USER->id) {
84 $PAGE->navigation->extend_for_user($user);
85 } else {
86 if ($node = $PAGE->navigation->find('myprofile', navigation_node::TYPE_ROOTNODE)) {
87 $node->force_open();
92 // Remote users cannot be edited.
93 if ($user->id != -1 and is_mnet_remote_user($user)) {
94 redirect($CFG->wwwroot . "/user/view.php?id=$id&course={$course->id}");
97 if ($user->id != $USER->id and is_siteadmin($user) and !is_siteadmin($USER)) { // Only admins may edit other admins.
98 print_error('useradmineditadmin');
101 if (isguestuser($user->id)) { // The real guest user can not be edited.
102 print_error('guestnoeditprofileother');
105 if ($user->deleted) {
106 echo $OUTPUT->header();
107 echo $OUTPUT->heading(get_string('userdeleted'));
108 echo $OUTPUT->footer();
109 die;
112 // Load user preferences.
113 useredit_load_preferences($user);
115 // Load custom profile fields data.
116 profile_load_data($user);
118 // User interests.
119 if (!empty($CFG->usetags)) {
120 require_once($CFG->dirroot.'/tag/lib.php');
121 $user->interests = tag_get_tags_array('user', $id);
124 if ($user->id !== -1) {
125 $usercontext = context_user::instance($user->id);
126 $editoroptions = array(
127 'maxfiles' => EDITOR_UNLIMITED_FILES,
128 'maxbytes' => $CFG->maxbytes,
129 'trusttext' => false,
130 'forcehttps' => false,
131 'context' => $usercontext
134 $user = file_prepare_standard_editor($user, 'description', $editoroptions, $usercontext, 'user', 'profile', 0);
135 } else {
136 $usercontext = null;
137 // This is a new user, we don't want to add files here.
138 $editoroptions = array(
139 'maxfiles' => 0,
140 'maxbytes' => 0,
141 'trusttext' => false,
142 'forcehttps' => false,
143 'context' => $coursecontext
147 // Prepare filemanager draft area.
148 $draftitemid = 0;
149 $filemanagercontext = $editoroptions['context'];
150 $filemanageroptions = array('maxbytes' => $CFG->maxbytes,
151 'subdirs' => 0,
152 'maxfiles' => 1,
153 'accepted_types' => 'web_image');
154 file_prepare_draft_area($draftitemid, $filemanagercontext->id, 'user', 'newicon', 0, $filemanageroptions);
155 $user->imagefile = $draftitemid;
156 // Create form.
157 $userform = new user_editadvanced_form(new moodle_url($PAGE->url, array('returnto' => $returnto)), array(
158 'editoroptions' => $editoroptions,
159 'filemanageroptions' => $filemanageroptions,
160 'user' => $user));
162 if ($usernew = $userform->get_data()) {
163 $usercreated = false;
165 if (empty($usernew->auth)) {
166 // User editing self.
167 $authplugin = get_auth_plugin($user->auth);
168 unset($usernew->auth); // Can not change/remove.
169 } else {
170 $authplugin = get_auth_plugin($usernew->auth);
173 $usernew->timemodified = time();
174 $createpassword = false;
176 if ($usernew->id == -1) {
177 unset($usernew->id);
178 $createpassword = !empty($usernew->createpassword);
179 unset($usernew->createpassword);
180 $usernew = file_postupdate_standard_editor($usernew, 'description', $editoroptions, null, 'user', 'profile', null);
181 $usernew->mnethostid = $CFG->mnet_localhost_id; // Always local user.
182 $usernew->confirmed = 1;
183 $usernew->timecreated = time();
184 if ($authplugin->is_internal()) {
185 if ($createpassword or empty($usernew->newpassword)) {
186 $usernew->password = '';
187 } else {
188 $usernew->password = hash_internal_user_password($usernew->newpassword);
190 } else {
191 $usernew->password = AUTH_PASSWORD_NOT_CACHED;
193 $usernew->id = user_create_user($usernew, false, false);
195 if (!$authplugin->is_internal() and $authplugin->can_change_password() and !empty($usernew->newpassword)) {
196 if (!$authplugin->user_update_password($usernew, $usernew->newpassword)) {
197 // Do not stop here, we need to finish user creation.
198 debugging(get_string('cannotupdatepasswordonextauth', '', '', $usernew->auth), DEBUG_NONE);
201 $usercreated = true;
202 } else {
203 $usernew = file_postupdate_standard_editor($usernew, 'description', $editoroptions, $usercontext, 'user', 'profile', 0);
204 // Pass a true old $user here.
205 if (!$authplugin->user_update($user, $usernew)) {
206 // Auth update failed.
207 print_error('cannotupdateuseronexauth', '', '', $user->auth);
209 user_update_user($usernew, false, false);
211 // Set new password if specified.
212 if (!empty($usernew->newpassword)) {
213 if ($authplugin->can_change_password()) {
214 if (!$authplugin->user_update_password($usernew, $usernew->newpassword)) {
215 print_error('cannotupdatepasswordonextauth', '', '', $usernew->auth);
217 unset_user_preference('create_password', $usernew); // Prevent cron from generating the password.
219 if (!empty($CFG->passwordchangelogout)) {
220 // We can use SID of other user safely here because they are unique,
221 // the problem here is we do not want to logout admin here when changing own password.
222 \core\session\manager::kill_user_sessions($usernew->id, session_id());
227 // Force logout if user just suspended.
228 if (isset($usernew->suspended) and $usernew->suspended and !$user->suspended) {
229 \core\session\manager::kill_user_sessions($user->id);
233 $usercontext = context_user::instance($usernew->id);
235 // Update preferences.
236 useredit_update_user_preference($usernew);
238 // Update tags.
239 if (!empty($CFG->usetags) and empty($USER->newadminuser)) {
240 useredit_update_interests($usernew, $usernew->interests);
243 // Update user picture.
244 if (empty($USER->newadminuser)) {
245 useredit_update_picture($usernew, $userform, $filemanageroptions);
248 // Update mail bounces.
249 useredit_update_bounces($user, $usernew);
251 // Update forum track preference.
252 useredit_update_trackforums($user, $usernew);
254 // Save custom profile fields data.
255 profile_save_data($usernew);
257 // Reload from db.
258 $usernew = $DB->get_record('user', array('id' => $usernew->id));
260 if ($createpassword) {
261 setnew_password_and_mail($usernew);
262 unset_user_preference('create_password', $usernew);
263 set_user_preference('auth_forcepasswordchange', 1, $usernew);
266 // Trigger update/create event, after all fields are stored.
267 if ($usercreated) {
268 \core\event\user_created::create_from_userid($usernew->id)->trigger();
269 } else {
270 \core\event\user_updated::create_from_userid($usernew->id)->trigger();
273 if ($user->id == $USER->id) {
274 // Override old $USER session variable.
275 foreach ((array)$usernew as $variable => $value) {
276 if ($variable === 'description' or $variable === 'password') {
277 // These are not set for security nad perf reasons.
278 continue;
280 $USER->$variable = $value;
282 // Preload custom fields.
283 profile_load_custom_fields($USER);
285 if (!empty($USER->newadminuser)) {
286 unset($USER->newadminuser);
287 // Apply defaults again - some of them might depend on admin user info, backup, roles, etc.
288 admin_apply_default_settings(null, false);
289 // Admin account is fully configured - set flag here in case the redirect does not work.
290 unset_config('adminsetuppending');
291 // Redirect to admin/ to continue with installation.
292 redirect("$CFG->wwwroot/$CFG->admin/");
293 } else if (empty($SITE->fullname)) {
294 // Somebody double clicked when editing admin user during install.
295 redirect("$CFG->wwwroot/$CFG->admin/");
296 } else {
297 if ($returnto === 'profile') {
298 if ($course->id != SITEID) {
299 $returnurl = new moodle_url('/user/view.php', array('id' => $user->id, 'course' => $course->id));
300 } else {
301 $returnurl = new moodle_url('/user/profile.php', array('id' => $user->id));
303 } else {
304 $returnurl = new moodle_url('/user/preferences.php', array('userid' => $user->id));
306 redirect($returnurl);
308 } else {
309 \core\session\manager::gc(); // Remove stale sessions.
310 redirect("$CFG->wwwroot/$CFG->admin/user.php");
312 // Never reached..
315 // Make sure we really are on the https page when https login required.
316 $PAGE->verify_https_required();
319 // Display page header.
320 if ($user->id == -1 or ($user->id != $USER->id)) {
321 if ($user->id == -1) {
322 echo $OUTPUT->header();
323 } else {
324 $streditmyprofile = get_string('editmyprofile');
325 $userfullname = fullname($user, true);
326 $PAGE->set_heading($userfullname);
327 $PAGE->set_title("$course->shortname: $streditmyprofile - $userfullname");
328 echo $OUTPUT->header();
329 echo $OUTPUT->heading($userfullname);
331 } else if (!empty($USER->newadminuser)) {
332 $strinstallation = get_string('installation', 'install');
333 $strprimaryadminsetup = get_string('primaryadminsetup');
335 $PAGE->navbar->add($strprimaryadminsetup);
336 $PAGE->set_title($strinstallation);
337 $PAGE->set_heading($strinstallation);
338 $PAGE->set_cacheable(false);
340 echo $OUTPUT->header();
341 echo $OUTPUT->box(get_string('configintroadmin', 'admin'), 'generalbox boxwidthnormal boxaligncenter');
342 echo '<br />';
343 } else {
344 $streditmyprofile = get_string('editmyprofile');
345 $strparticipants = get_string('participants');
346 $strnewuser = get_string('newuser');
347 $userfullname = fullname($user, true);
349 $PAGE->set_title("$course->shortname: $streditmyprofile");
350 $PAGE->set_heading($userfullname);
352 echo $OUTPUT->header();
353 echo $OUTPUT->heading($streditmyprofile);
356 // Finally display THE form.
357 $userform->display();
359 // And proper footer.
360 echo $OUTPUT->footer();