Merge branch 'MDL-48688-28' of git://github.com/ankitagarwal/moodle into MOODLE_28_STABLE
[moodle.git] / user / editadvanced.php
blob3843bcf4648a46cc9a8d285414eaa5eaa24b80ae
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).
39 $PAGE->set_url('/user/editadvanced.php', array('course' => $course, 'id' => $id));
41 $course = $DB->get_record('course', array('id' => $course), '*', MUST_EXIST);
43 if (!empty($USER->newadminuser)) {
44 // Ignore double clicks, we must finish all operations before cancelling request.
45 ignore_user_abort(true);
47 $PAGE->set_course($SITE);
48 $PAGE->set_pagelayout('maintenance');
49 } else {
50 if ($course->id == SITEID) {
51 require_login();
52 $PAGE->set_context(context_system::instance());
53 } else {
54 require_login($course);
56 $PAGE->set_pagelayout('admin');
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 require_capability('moodle/user:create', $systemcontext);
74 admin_externalpage_setup('addnewuser', '', array('id' => -1));
75 } else {
76 // Editing existing user.
77 require_capability('moodle/user:update', $systemcontext);
78 $user = $DB->get_record('user', array('id' => $id), '*', MUST_EXIST);
79 $PAGE->set_context(context_user::instance($user->id));
80 if ($user->id != $USER->id) {
81 $PAGE->navigation->extend_for_user($user);
82 } else {
83 if ($node = $PAGE->navigation->find('myprofile', navigation_node::TYPE_ROOTNODE)) {
84 $node->force_open();
89 // Remote users cannot be edited.
90 if ($user->id != -1 and is_mnet_remote_user($user)) {
91 redirect($CFG->wwwroot . "/user/view.php?id=$id&course={$course->id}");
94 if ($user->id != $USER->id and is_siteadmin($user) and !is_siteadmin($USER)) { // Only admins may edit other admins.
95 print_error('useradmineditadmin');
98 if (isguestuser($user->id)) { // The real guest user can not be edited.
99 print_error('guestnoeditprofileother');
102 if ($user->deleted) {
103 echo $OUTPUT->header();
104 echo $OUTPUT->heading(get_string('userdeleted'));
105 echo $OUTPUT->footer();
106 die;
109 // Load user preferences.
110 useredit_load_preferences($user);
112 // Load custom profile fields data.
113 profile_load_data($user);
115 // User interests.
116 if (!empty($CFG->usetags)) {
117 require_once($CFG->dirroot.'/tag/lib.php');
118 $user->interests = tag_get_tags_array('user', $id);
121 if ($user->id !== -1) {
122 $usercontext = context_user::instance($user->id);
123 $editoroptions = array(
124 'maxfiles' => EDITOR_UNLIMITED_FILES,
125 'maxbytes' => $CFG->maxbytes,
126 'trusttext' => false,
127 'forcehttps' => false,
128 'context' => $usercontext
131 $user = file_prepare_standard_editor($user, 'description', $editoroptions, $usercontext, 'user', 'profile', 0);
132 } else {
133 $usercontext = null;
134 // This is a new user, we don't want to add files here.
135 $editoroptions = array(
136 'maxfiles' => 0,
137 'maxbytes' => 0,
138 'trusttext' => false,
139 'forcehttps' => false,
140 'context' => $coursecontext
144 // Prepare filemanager draft area.
145 $draftitemid = 0;
146 $filemanagercontext = $editoroptions['context'];
147 $filemanageroptions = array('maxbytes' => $CFG->maxbytes,
148 'subdirs' => 0,
149 'maxfiles' => 1,
150 'accepted_types' => 'web_image');
151 file_prepare_draft_area($draftitemid, $filemanagercontext->id, 'user', 'newicon', 0, $filemanageroptions);
152 $user->imagefile = $draftitemid;
153 // Create form.
154 $userform = new user_editadvanced_form(null, array(
155 'editoroptions' => $editoroptions,
156 'filemanageroptions' => $filemanageroptions,
157 'userid' => $user->id));
158 $userform->set_data($user);
160 if ($usernew = $userform->get_data()) {
161 $usercreated = false;
163 if (empty($usernew->auth)) {
164 // User editing self.
165 $authplugin = get_auth_plugin($user->auth);
166 unset($usernew->auth); // Can not change/remove.
167 } else {
168 $authplugin = get_auth_plugin($usernew->auth);
171 $usernew->timemodified = time();
172 $createpassword = false;
174 if ($usernew->id == -1) {
175 unset($usernew->id);
176 $createpassword = !empty($usernew->createpassword);
177 unset($usernew->createpassword);
178 $usernew = file_postupdate_standard_editor($usernew, 'description', $editoroptions, null, 'user', 'profile', null);
179 $usernew->mnethostid = $CFG->mnet_localhost_id; // Always local user.
180 $usernew->confirmed = 1;
181 $usernew->timecreated = time();
182 if ($authplugin->is_internal()) {
183 if ($createpassword or empty($usernew->newpassword)) {
184 $usernew->password = '';
185 } else {
186 $usernew->password = hash_internal_user_password($usernew->newpassword);
188 } else {
189 $usernew->password = AUTH_PASSWORD_NOT_CACHED;
191 $usernew->id = user_create_user($usernew, false, false);
193 if (!$authplugin->is_internal() and $authplugin->can_change_password() and !empty($usernew->newpassword)) {
194 if (!$authplugin->user_update_password($usernew, $usernew->newpassword)) {
195 // Do not stop here, we need to finish user creation.
196 debugging(get_string('cannotupdatepasswordonextauth', '', '', $usernew->auth), DEBUG_NONE);
199 $usercreated = true;
200 } else {
201 $usernew = file_postupdate_standard_editor($usernew, 'description', $editoroptions, $usercontext, 'user', 'profile', 0);
202 // Pass a true old $user here.
203 if (!$authplugin->user_update($user, $usernew)) {
204 // Auth update failed.
205 print_error('cannotupdateuseronexauth', '', '', $user->auth);
207 user_update_user($usernew, false, false);
209 // Set new password if specified.
210 if (!empty($usernew->newpassword)) {
211 if ($authplugin->can_change_password()) {
212 if (!$authplugin->user_update_password($usernew, $usernew->newpassword)) {
213 print_error('cannotupdatepasswordonextauth', '', '', $usernew->auth);
215 unset_user_preference('create_password', $usernew); // Prevent cron from generating the password.
219 // Force logout if user just suspended.
220 if (isset($usernew->suspended) and $usernew->suspended and !$user->suspended) {
221 \core\session\manager::kill_user_sessions($user->id);
225 $usercontext = context_user::instance($usernew->id);
227 // Update preferences.
228 useredit_update_user_preference($usernew);
230 // Update tags.
231 if (!empty($CFG->usetags) and empty($USER->newadminuser)) {
232 useredit_update_interests($usernew, $usernew->interests);
235 // Update user picture.
236 if (empty($USER->newadminuser)) {
237 useredit_update_picture($usernew, $userform, $filemanageroptions);
240 // Update mail bounces.
241 useredit_update_bounces($user, $usernew);
243 // Update forum track preference.
244 useredit_update_trackforums($user, $usernew);
246 // Save custom profile fields data.
247 profile_save_data($usernew);
249 // Reload from db.
250 $usernew = $DB->get_record('user', array('id' => $usernew->id));
252 if ($createpassword) {
253 setnew_password_and_mail($usernew);
254 unset_user_preference('create_password', $usernew);
255 set_user_preference('auth_forcepasswordchange', 1, $usernew);
258 // Trigger update/create event, after all fields are stored.
259 if ($usercreated) {
260 \core\event\user_created::create_from_userid($usernew->id)->trigger();
261 } else {
262 \core\event\user_updated::create_from_userid($usernew->id)->trigger();
265 if ($user->id == $USER->id) {
266 // Override old $USER session variable.
267 foreach ((array)$usernew as $variable => $value) {
268 if ($variable === 'description' or $variable === 'password') {
269 // These are not set for security nad perf reasons.
270 continue;
272 $USER->$variable = $value;
274 // Preload custom fields.
275 profile_load_custom_fields($USER);
277 if (!empty($USER->newadminuser)) {
278 unset($USER->newadminuser);
279 // Apply defaults again - some of them might depend on admin user info, backup, roles, etc.
280 admin_apply_default_settings(null, false);
281 // Admin account is fully configured - set flag here in case the redirect does not work.
282 unset_config('adminsetuppending');
283 // Redirect to admin/ to continue with installation.
284 redirect("$CFG->wwwroot/$CFG->admin/");
285 } else if (empty($SITE->fullname)) {
286 // Somebody double clicked when editing admin user during install.
287 redirect("$CFG->wwwroot/$CFG->admin/");
288 } else {
289 redirect("$CFG->wwwroot/user/view.php?id=$USER->id&course=$course->id");
291 } else {
292 \core\session\manager::gc(); // Remove stale sessions.
293 redirect("$CFG->wwwroot/$CFG->admin/user.php");
295 // Never reached..
298 // Make sure we really are on the https page when https login required.
299 $PAGE->verify_https_required();
302 // Display page header.
303 if ($user->id == -1 or ($user->id != $USER->id)) {
304 if ($user->id == -1) {
305 echo $OUTPUT->header();
306 } else {
307 $PAGE->set_heading($SITE->fullname);
308 echo $OUTPUT->header();
309 $userfullname = fullname($user, true);
310 echo $OUTPUT->heading($userfullname);
312 } else if (!empty($USER->newadminuser)) {
313 $strinstallation = get_string('installation', 'install');
314 $strprimaryadminsetup = get_string('primaryadminsetup');
316 $PAGE->navbar->add($strprimaryadminsetup);
317 $PAGE->set_title($strinstallation);
318 $PAGE->set_heading($strinstallation);
319 $PAGE->set_cacheable(false);
321 echo $OUTPUT->header();
322 echo $OUTPUT->box(get_string('configintroadmin', 'admin'), 'generalbox boxwidthnormal boxaligncenter');
323 echo '<br />';
324 } else {
325 $streditmyprofile = get_string('editmyprofile');
326 $strparticipants = get_string('participants');
327 $strnewuser = get_string('newuser');
328 $userfullname = fullname($user, true);
330 $PAGE->set_title("$course->shortname: $streditmyprofile");
331 $PAGE->set_heading($course->fullname);
333 echo $OUTPUT->header();
334 echo $OUTPUT->heading($userfullname);
337 // Finally display THE form.
338 $userform->display();
340 // And proper footer.
341 echo $OUTPUT->footer();