Merge branch 'MDL-71708' of https://github.com/lostrogit/moodle
[moodle.git] / user / editadvanced.php
blob3bc3e14f505c92c5f106c71939adc23a3a54c431
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');
32 require_once($CFG->dirroot.'/webservice/lib.php');
34 $id = optional_param('id', $USER->id, PARAM_INT); // User id; -1 if creating new user.
35 $course = optional_param('course', SITEID, PARAM_INT); // Course id (defaults to Site).
36 $returnto = optional_param('returnto', null, PARAM_ALPHA); // Code determining where to return to after save.
38 $PAGE->set_url('/user/editadvanced.php', array('course' => $course, 'id' => $id));
40 $course = $DB->get_record('course', array('id' => $course), '*', MUST_EXIST);
42 if (!empty($USER->newadminuser)) {
43 // Ignore double clicks, we must finish all operations before cancelling request.
44 ignore_user_abort(true);
46 $PAGE->set_course($SITE);
47 $PAGE->set_pagelayout('maintenance');
48 } else {
49 if ($course->id == SITEID) {
50 require_login();
51 $PAGE->set_context(context_system::instance());
52 } else {
53 require_login($course);
55 $PAGE->set_pagelayout('admin');
56 $PAGE->add_body_class('limitedwidth');
59 if ($course->id == SITEID) {
60 $coursecontext = context_system::instance(); // SYSTEM context.
61 } else {
62 $coursecontext = context_course::instance($course->id); // Course context.
64 $systemcontext = context_system::instance();
66 if ($id == -1) {
67 // Creating new user.
68 $user = new stdClass();
69 $user->id = -1;
70 $user->auth = 'manual';
71 $user->confirmed = 1;
72 $user->deleted = 0;
73 $user->timezone = '99';
74 require_capability('moodle/user:create', $systemcontext);
75 admin_externalpage_setup('addnewuser', '', array('id' => -1));
76 $PAGE->set_primary_active_tab('siteadminnode');
77 $PAGE->navbar->add(get_string('addnewuser', 'moodle'), $PAGE->url);
78 } else {
79 // Editing existing user.
80 require_capability('moodle/user:update', $systemcontext);
81 $user = $DB->get_record('user', array('id' => $id), '*', MUST_EXIST);
82 $PAGE->set_context(context_user::instance($user->id));
83 $PAGE->navbar->includesettingsbase = true;
84 if ($user->id != $USER->id) {
85 $PAGE->navigation->extend_for_user($user);
86 } else {
87 if ($node = $PAGE->navigation->find('myprofile', navigation_node::TYPE_ROOTNODE)) {
88 $node->force_open();
93 // Remote users cannot be edited.
94 if ($user->id != -1 and is_mnet_remote_user($user)) {
95 redirect($CFG->wwwroot . "/user/view.php?id=$id&course={$course->id}");
98 if ($user->id != $USER->id and is_siteadmin($user) and !is_siteadmin($USER)) { // Only admins may edit other admins.
99 throw new \moodle_exception('useradmineditadmin');
102 if (isguestuser($user->id)) { // The real guest user can not be edited.
103 throw new \moodle_exception('guestnoeditprofileother');
106 if ($user->deleted) {
107 echo $OUTPUT->header();
108 echo $OUTPUT->heading(get_string('userdeleted'));
109 echo $OUTPUT->footer();
110 die;
113 // Load user preferences.
114 useredit_load_preferences($user);
116 // Load custom profile fields data.
117 profile_load_data($user);
119 // User interests.
120 $user->interests = core_tag_tag::get_item_tags_array('core', 'user', $id);
122 if ($user->id !== -1) {
123 $usercontext = context_user::instance($user->id);
124 $editoroptions = array(
125 'maxfiles' => EDITOR_UNLIMITED_FILES,
126 'maxbytes' => $CFG->maxbytes,
127 'trusttext' => false,
128 'forcehttps' => false,
129 'context' => $usercontext
132 $user = file_prepare_standard_editor($user, 'description', $editoroptions, $usercontext, 'user', 'profile', 0);
133 } else {
134 $usercontext = null;
135 // This is a new user, we don't want to add files here.
136 $editoroptions = array(
137 'maxfiles' => 0,
138 'maxbytes' => 0,
139 'trusttext' => false,
140 'forcehttps' => false,
141 'context' => $coursecontext
145 // Prepare filemanager draft area.
146 $draftitemid = 0;
147 $filemanagercontext = $editoroptions['context'];
148 $filemanageroptions = array('maxbytes' => $CFG->maxbytes,
149 'subdirs' => 0,
150 'maxfiles' => 1,
151 'accepted_types' => 'optimised_image');
152 file_prepare_draft_area($draftitemid, $filemanagercontext->id, 'user', 'newicon', 0, $filemanageroptions);
153 $user->imagefile = $draftitemid;
154 // Create form.
155 $userform = new user_editadvanced_form(new moodle_url($PAGE->url, array('returnto' => $returnto)), array(
156 'editoroptions' => $editoroptions,
157 'filemanageroptions' => $filemanageroptions,
158 'user' => $user));
161 // Deciding where to send the user back in most cases.
162 if ($returnto === 'profile') {
163 if ($course->id != SITEID) {
164 $returnurl = new moodle_url('/user/view.php', array('id' => $user->id, 'course' => $course->id));
165 } else {
166 $returnurl = new moodle_url('/user/profile.php', array('id' => $user->id));
168 } else {
169 $returnurl = new moodle_url('/user/preferences.php', array('userid' => $user->id));
172 if ($userform->is_cancelled()) {
173 redirect($returnurl);
174 } else if ($usernew = $userform->get_data()) {
175 $usercreated = false;
177 if (empty($usernew->auth)) {
178 // User editing self.
179 $authplugin = get_auth_plugin($user->auth);
180 unset($usernew->auth); // Can not change/remove.
181 } else {
182 $authplugin = get_auth_plugin($usernew->auth);
185 $usernew->timemodified = time();
186 $createpassword = false;
188 if ($usernew->id == -1) {
189 unset($usernew->id);
190 $createpassword = !empty($usernew->createpassword);
191 unset($usernew->createpassword);
192 $usernew = file_postupdate_standard_editor($usernew, 'description', $editoroptions, null, 'user', 'profile', null);
193 $usernew->mnethostid = $CFG->mnet_localhost_id; // Always local user.
194 $usernew->confirmed = 1;
195 $usernew->timecreated = time();
196 if ($authplugin->is_internal()) {
197 if ($createpassword or empty($usernew->newpassword)) {
198 $usernew->password = '';
199 } else {
200 $usernew->password = hash_internal_user_password($usernew->newpassword);
202 } else {
203 $usernew->password = AUTH_PASSWORD_NOT_CACHED;
205 $usernew->id = user_create_user($usernew, false, false);
207 if (!$authplugin->is_internal() and $authplugin->can_change_password() and !empty($usernew->newpassword)) {
208 if (!$authplugin->user_update_password($usernew, $usernew->newpassword)) {
209 // Do not stop here, we need to finish user creation.
210 debugging(get_string('cannotupdatepasswordonextauth', 'error', $usernew->auth), DEBUG_NONE);
213 $usercreated = true;
214 } else {
215 $usernew = file_postupdate_standard_editor($usernew, 'description', $editoroptions, $usercontext, 'user', 'profile', 0);
216 // Pass a true old $user here.
217 if (!$authplugin->user_update($user, $usernew)) {
218 // Auth update failed.
219 throw new \moodle_exception('cannotupdateuseronexauth', '', '', $user->auth);
221 user_update_user($usernew, false, false);
223 // Set new password if specified.
224 if (!empty($usernew->newpassword)) {
225 if ($authplugin->can_change_password()) {
226 if (!$authplugin->user_update_password($usernew, $usernew->newpassword)) {
227 throw new \moodle_exception('cannotupdatepasswordonextauth', '', '', $usernew->auth);
229 unset_user_preference('create_password', $usernew); // Prevent cron from generating the password.
231 if (!empty($CFG->passwordchangelogout)) {
232 // We can use SID of other user safely here because they are unique,
233 // the problem here is we do not want to logout admin here when changing own password.
234 \core\session\manager::kill_user_sessions($usernew->id, session_id());
236 if (!empty($usernew->signoutofotherservices)) {
237 webservice::delete_user_ws_tokens($usernew->id);
242 // Force logout if user just suspended.
243 if (isset($usernew->suspended) and $usernew->suspended and !$user->suspended) {
244 \core\session\manager::kill_user_sessions($user->id);
248 $usercontext = context_user::instance($usernew->id);
250 // Update preferences.
251 useredit_update_user_preference($usernew);
253 // Update tags.
254 if (empty($USER->newadminuser) && isset($usernew->interests)) {
255 useredit_update_interests($usernew, $usernew->interests);
258 // Update user picture.
259 if (empty($USER->newadminuser)) {
260 core_user::update_picture($usernew, $filemanageroptions);
263 // Update mail bounces.
264 useredit_update_bounces($user, $usernew);
266 // Update forum track preference.
267 useredit_update_trackforums($user, $usernew);
269 // Save custom profile fields data.
270 profile_save_data($usernew);
272 // Reload from db.
273 $usernew = $DB->get_record('user', array('id' => $usernew->id));
275 if ($createpassword) {
276 setnew_password_and_mail($usernew);
277 unset_user_preference('create_password', $usernew);
278 set_user_preference('auth_forcepasswordchange', 1, $usernew);
281 // Trigger update/create event, after all fields are stored.
282 if ($usercreated) {
283 \core\event\user_created::create_from_userid($usernew->id)->trigger();
284 } else {
285 \core\event\user_updated::create_from_userid($usernew->id)->trigger();
288 if ($user->id == $USER->id) {
289 // Override old $USER session variable.
290 foreach ((array)$usernew as $variable => $value) {
291 if ($variable === 'description' or $variable === 'password') {
292 // These are not set for security nad perf reasons.
293 continue;
295 $USER->$variable = $value;
297 // Preload custom fields.
298 profile_load_custom_fields($USER);
300 if (!empty($USER->newadminuser)) {
301 unset($USER->newadminuser);
302 // Apply defaults again - some of them might depend on admin user info, backup, roles, etc.
303 admin_apply_default_settings(null, false);
304 // Admin account is fully configured - set flag here in case the redirect does not work.
305 unset_config('adminsetuppending');
306 // Redirect to admin/ to continue with installation.
307 redirect("$CFG->wwwroot/$CFG->admin/");
308 } else if (empty($SITE->fullname)) {
309 // Somebody double clicked when editing admin user during install.
310 redirect("$CFG->wwwroot/$CFG->admin/");
311 } else {
312 redirect($returnurl, get_string('changessaved'), null, \core\output\notification::NOTIFY_SUCCESS);
314 } else {
315 \core\session\manager::gc(); // Remove stale sessions.
316 redirect("$CFG->wwwroot/$CFG->admin/user.php", get_string('changessaved'), null, \core\output\notification::NOTIFY_SUCCESS);
318 // Never reached..
322 // Display page header.
323 if ($user->id == -1 or ($user->id != $USER->id)) {
324 if ($user->id == -1) {
325 echo $OUTPUT->header();
326 } else {
327 $streditmyprofile = get_string('editmyprofile');
328 $userfullname = fullname($user, true);
329 $PAGE->set_heading($userfullname);
330 $PAGE->set_title("$course->shortname: $streditmyprofile - $userfullname");
331 echo $OUTPUT->header();
332 echo $OUTPUT->heading($userfullname);
334 } else if (!empty($USER->newadminuser)) {
335 $strinstallation = get_string('installation', 'install');
336 $strprimaryadminsetup = get_string('primaryadminsetup');
338 $PAGE->navbar->add($strprimaryadminsetup);
339 $PAGE->set_title($strinstallation);
340 $PAGE->set_heading($strinstallation);
341 $PAGE->set_cacheable(false);
343 echo $OUTPUT->header();
344 echo $OUTPUT->box(get_string('configintroadmin', 'admin'), 'generalbox boxwidthnormal boxaligncenter');
345 echo '<br />';
346 } else {
347 $streditmyprofile = get_string('editmyprofile');
348 $strparticipants = get_string('participants');
349 $strnewuser = get_string('newuser');
350 $userfullname = fullname($user, true);
352 $PAGE->set_title("$course->shortname: $streditmyprofile");
353 $PAGE->set_heading($userfullname);
355 echo $OUTPUT->header();
356 echo $OUTPUT->heading($streditmyprofile);
359 // Finally display THE form.
360 $userform->display();
362 // And proper footer.
363 echo $OUTPUT->footer();