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');
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');
50 if ($course->id
== SITEID
) {
52 $PAGE->set_context(context_system
::instance());
54 require_login($course);
56 $PAGE->set_pagelayout('admin');
59 if ($course->id
== SITEID
) {
60 $coursecontext = context_system
::instance(); // SYSTEM context.
62 $coursecontext = context_course
::instance($course->id
); // Course context.
64 $systemcontext = context_system
::instance();
68 $user = new stdClass();
70 $user->auth
= 'manual';
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 if ($user->id
!= $USER->id
) {
81 $PAGE->navigation
->extend_for_user($user);
83 if ($node = $PAGE->navigation
->find('myprofile', navigation_node
::TYPE_ROOTNODE
)) {
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();
109 // Load user preferences.
110 useredit_load_preferences($user);
112 // Load custom profile fields data.
113 profile_load_data($user);
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);
134 // This is a new user, we don't want to add files here.
135 $editoroptions = array(
138 'trusttext' => false,
139 'forcehttps' => false,
140 'context' => $coursecontext
144 // Prepare filemanager draft area.
146 $filemanagercontext = $editoroptions['context'];
147 $filemanageroptions = array('maxbytes' => $CFG->maxbytes
,
150 'accepted_types' => 'web_image');
151 file_prepare_draft_area($draftitemid, $filemanagercontext->id
, 'user', 'newicon', 0, $filemanageroptions);
152 $user->imagefile
= $draftitemid;
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.
168 $authplugin = get_auth_plugin($usernew->auth
);
171 $usernew->timemodified
= time();
172 $createpassword = false;
174 if ($usernew->id
== -1) {
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
= '';
186 $usernew->password
= hash_internal_user_password($usernew->newpassword
);
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
);
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);
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);
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.
260 \core\event\user_created
::create_from_userid($usernew->id
)->trigger();
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.
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/");
289 redirect("$CFG->wwwroot/user/view.php?id=$USER->id&course=$course->id");
292 \core\session\manager
::gc(); // Remove stale sessions.
293 redirect("$CFG->wwwroot/$CFG->admin/user.php");
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();
307 $streditmyprofile = get_string('editmyprofile');
308 $userfullname = fullname($user, true);
309 $PAGE->set_heading($SITE->fullname
);
310 $PAGE->set_title("$course->shortname: $streditmyprofile - $userfullname");
311 echo $OUTPUT->header();
312 echo $OUTPUT->heading($userfullname);
314 } else if (!empty($USER->newadminuser
)) {
315 $strinstallation = get_string('installation', 'install');
316 $strprimaryadminsetup = get_string('primaryadminsetup');
318 $PAGE->navbar
->add($strprimaryadminsetup);
319 $PAGE->set_title($strinstallation);
320 $PAGE->set_heading($strinstallation);
321 $PAGE->set_cacheable(false);
323 echo $OUTPUT->header();
324 echo $OUTPUT->box(get_string('configintroadmin', 'admin'), 'generalbox boxwidthnormal boxaligncenter');
327 $streditmyprofile = get_string('editmyprofile');
328 $strparticipants = get_string('participants');
329 $strnewuser = get_string('newuser');
330 $userfullname = fullname($user, true);
332 $PAGE->set_title("$course->shortname: $streditmyprofile");
333 $PAGE->set_heading($course->fullname
);
335 echo $OUTPUT->header();
336 echo $OUTPUT->heading($userfullname);
339 // Finally display THE form.
340 $userform->display();
342 // And proper footer.
343 echo $OUTPUT->footer();