2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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
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');
49 if ($course->id
== SITEID
) {
51 $PAGE->set_context(context_system
::instance());
53 require_login($course);
55 $PAGE->set_pagelayout('admin');
58 if ($course->id
== SITEID
) {
59 $coursecontext = context_system
::instance(); // SYSTEM context.
61 $coursecontext = context_course
::instance($course->id
); // Course context.
63 $systemcontext = context_system
::instance();
67 $user = new stdClass();
69 $user->auth
= 'manual';
72 $user->timezone
= '99';
73 require_capability('moodle/user:create', $systemcontext);
74 admin_externalpage_setup('addnewuser', '', array('id' => -1));
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 $PAGE->navbar
->includesettingsbase
= true;
81 if ($user->id
!= $USER->id
) {
82 $PAGE->navigation
->extend_for_user($user);
84 if ($node = $PAGE->navigation
->find('myprofile', navigation_node
::TYPE_ROOTNODE
)) {
90 // Remote users cannot be edited.
91 if ($user->id
!= -1 and is_mnet_remote_user($user)) {
92 redirect($CFG->wwwroot
. "/user/view.php?id=$id&course={$course->id}");
95 if ($user->id
!= $USER->id
and is_siteadmin($user) and !is_siteadmin($USER)) { // Only admins may edit other admins.
96 print_error('useradmineditadmin');
99 if (isguestuser($user->id
)) { // The real guest user can not be edited.
100 print_error('guestnoeditprofileother');
103 if ($user->deleted
) {
104 echo $OUTPUT->header();
105 echo $OUTPUT->heading(get_string('userdeleted'));
106 echo $OUTPUT->footer();
110 // Load user preferences.
111 useredit_load_preferences($user);
113 // Load custom profile fields data.
114 profile_load_data($user);
117 $user->interests
= core_tag_tag
::get_item_tags_array('core', 'user', $id);
119 if ($user->id
!== -1) {
120 $usercontext = context_user
::instance($user->id
);
121 $editoroptions = array(
122 'maxfiles' => EDITOR_UNLIMITED_FILES
,
123 'maxbytes' => $CFG->maxbytes
,
124 'trusttext' => false,
125 'forcehttps' => false,
126 'context' => $usercontext
129 $user = file_prepare_standard_editor($user, 'description', $editoroptions, $usercontext, 'user', 'profile', 0);
132 // This is a new user, we don't want to add files here.
133 $editoroptions = array(
136 'trusttext' => false,
137 'forcehttps' => false,
138 'context' => $coursecontext
142 // Prepare filemanager draft area.
144 $filemanagercontext = $editoroptions['context'];
145 $filemanageroptions = array('maxbytes' => $CFG->maxbytes
,
148 'accepted_types' => 'optimised_image');
149 file_prepare_draft_area($draftitemid, $filemanagercontext->id
, 'user', 'newicon', 0, $filemanageroptions);
150 $user->imagefile
= $draftitemid;
152 $userform = new user_editadvanced_form(new moodle_url($PAGE->url
, array('returnto' => $returnto)), array(
153 'editoroptions' => $editoroptions,
154 'filemanageroptions' => $filemanageroptions,
158 // Deciding where to send the user back in most cases.
159 if ($returnto === 'profile') {
160 if ($course->id
!= SITEID
) {
161 $returnurl = new moodle_url('/user/view.php', array('id' => $user->id
, 'course' => $course->id
));
163 $returnurl = new moodle_url('/user/profile.php', array('id' => $user->id
));
166 $returnurl = new moodle_url('/user/preferences.php', array('userid' => $user->id
));
169 if ($userform->is_cancelled()) {
170 redirect($returnurl);
171 } else if ($usernew = $userform->get_data()) {
172 $usercreated = false;
174 if (empty($usernew->auth
)) {
175 // User editing self.
176 $authplugin = get_auth_plugin($user->auth
);
177 unset($usernew->auth
); // Can not change/remove.
179 $authplugin = get_auth_plugin($usernew->auth
);
182 $usernew->timemodified
= time();
183 $createpassword = false;
185 if ($usernew->id
== -1) {
187 $createpassword = !empty($usernew->createpassword
);
188 unset($usernew->createpassword
);
189 $usernew = file_postupdate_standard_editor($usernew, 'description', $editoroptions, null, 'user', 'profile', null);
190 $usernew->mnethostid
= $CFG->mnet_localhost_id
; // Always local user.
191 $usernew->confirmed
= 1;
192 $usernew->timecreated
= time();
193 if ($authplugin->is_internal()) {
194 if ($createpassword or empty($usernew->newpassword
)) {
195 $usernew->password
= '';
197 $usernew->password
= hash_internal_user_password($usernew->newpassword
);
200 $usernew->password
= AUTH_PASSWORD_NOT_CACHED
;
202 $usernew->id
= user_create_user($usernew, false, false);
204 if (!$authplugin->is_internal() and $authplugin->can_change_password() and !empty($usernew->newpassword
)) {
205 if (!$authplugin->user_update_password($usernew, $usernew->newpassword
)) {
206 // Do not stop here, we need to finish user creation.
207 debugging(get_string('cannotupdatepasswordonextauth', 'error', $usernew->auth
), DEBUG_NONE
);
212 $usernew = file_postupdate_standard_editor($usernew, 'description', $editoroptions, $usercontext, 'user', 'profile', 0);
213 // Pass a true old $user here.
214 if (!$authplugin->user_update($user, $usernew)) {
215 // Auth update failed.
216 print_error('cannotupdateuseronexauth', '', '', $user->auth
);
218 user_update_user($usernew, false, false);
220 // Set new password if specified.
221 if (!empty($usernew->newpassword
)) {
222 if ($authplugin->can_change_password()) {
223 if (!$authplugin->user_update_password($usernew, $usernew->newpassword
)) {
224 print_error('cannotupdatepasswordonextauth', '', '', $usernew->auth
);
226 unset_user_preference('create_password', $usernew); // Prevent cron from generating the password.
228 if (!empty($CFG->passwordchangelogout
)) {
229 // We can use SID of other user safely here because they are unique,
230 // the problem here is we do not want to logout admin here when changing own password.
231 \core\session\manager
::kill_user_sessions($usernew->id
, session_id());
233 if (!empty($usernew->signoutofotherservices
)) {
234 webservice
::delete_user_ws_tokens($usernew->id
);
239 // Force logout if user just suspended.
240 if (isset($usernew->suspended
) and $usernew->suspended
and !$user->suspended
) {
241 \core\session\manager
::kill_user_sessions($user->id
);
245 $usercontext = context_user
::instance($usernew->id
);
247 // Update preferences.
248 useredit_update_user_preference($usernew);
251 if (empty($USER->newadminuser
) && isset($usernew->interests
)) {
252 useredit_update_interests($usernew, $usernew->interests
);
255 // Update user picture.
256 if (empty($USER->newadminuser
)) {
257 core_user
::update_picture($usernew, $filemanageroptions);
260 // Update mail bounces.
261 useredit_update_bounces($user, $usernew);
263 // Update forum track preference.
264 useredit_update_trackforums($user, $usernew);
266 // Save custom profile fields data.
267 profile_save_data($usernew);
270 $usernew = $DB->get_record('user', array('id' => $usernew->id
));
272 if ($createpassword) {
273 setnew_password_and_mail($usernew);
274 unset_user_preference('create_password', $usernew);
275 set_user_preference('auth_forcepasswordchange', 1, $usernew);
278 // Trigger update/create event, after all fields are stored.
280 \core\event\user_created
::create_from_userid($usernew->id
)->trigger();
282 \core\event\user_updated
::create_from_userid($usernew->id
)->trigger();
285 if ($user->id
== $USER->id
) {
286 // Override old $USER session variable.
287 foreach ((array)$usernew as $variable => $value) {
288 if ($variable === 'description' or $variable === 'password') {
289 // These are not set for security nad perf reasons.
292 $USER->$variable = $value;
294 // Preload custom fields.
295 profile_load_custom_fields($USER);
297 if (!empty($USER->newadminuser
)) {
298 unset($USER->newadminuser
);
299 // Apply defaults again - some of them might depend on admin user info, backup, roles, etc.
300 admin_apply_default_settings(null, false);
301 // Admin account is fully configured - set flag here in case the redirect does not work.
302 unset_config('adminsetuppending');
303 // Redirect to admin/ to continue with installation.
304 redirect("$CFG->wwwroot/$CFG->admin/");
305 } else if (empty($SITE->fullname
)) {
306 // Somebody double clicked when editing admin user during install.
307 redirect("$CFG->wwwroot/$CFG->admin/");
309 redirect($returnurl);
312 \core\session\manager
::gc(); // Remove stale sessions.
313 redirect("$CFG->wwwroot/$CFG->admin/user.php");
319 // Display page header.
320 if ($user->id
== -1 or ($user->id
!= $USER->id
)) {
321 if ($user->id
== -1) {
322 echo $OUTPUT->header();
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');
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();