3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * Allows you to edit a users profile
21 * @copyright 1999 Martin Dougiamas http://dougiamas.com
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require_once('../config.php');
27 require_once($CFG->libdir
.'/gdlib.php');
28 require_once($CFG->libdir
.'/adminlib.php');
29 require_once($CFG->dirroot
.'/user/editadvanced_form.php');
30 require_once($CFG->dirroot
.'/user/editlib.php');
31 require_once($CFG->dirroot
.'/user/profile/lib.php');
32 require_once($CFG->dirroot
.'/user/lib.php');
34 //HTTPS is required in this page when $CFG->loginhttps enabled
35 $PAGE->https_required();
37 $id = optional_param('id', $USER->id
, PARAM_INT
); // user id; -1 if creating new user
38 $course = optional_param('course', SITEID
, PARAM_INT
); // course id (defaults to Site)
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 $PAGE->set_course($SITE);
46 $PAGE->set_pagelayout('maintenance');
48 if ($course->id
== SITEID
) {
50 $PAGE->set_context(context_system
::instance());
52 require_login($course);
54 $PAGE->set_pagelayout('admin');
57 if ($course->id
== SITEID
) {
58 $coursecontext = context_system
::instance(); // SYSTEM context
60 $coursecontext = context_course
::instance($course->id
); // Course context
62 $systemcontext = context_system
::instance();
66 $user = new stdClass();
68 $user->auth
= 'manual';
71 require_capability('moodle/user:create', $systemcontext);
72 admin_externalpage_setup('addnewuser', '', array('id' => -1));
74 // editing existing user
75 require_capability('moodle/user:update', $systemcontext);
76 $user = $DB->get_record('user', array('id'=>$id), '*', MUST_EXIST
);
77 $PAGE->set_context(context_user
::instance($user->id
));
78 if ($user->id
!= $USER->id
) {
79 $PAGE->navigation
->extend_for_user($user);
81 if ($node = $PAGE->navigation
->find('myprofile', navigation_node
::TYPE_ROOTNODE
)) {
87 // remote users cannot be edited
88 if ($user->id
!= -1 and is_mnet_remote_user($user)) {
89 redirect($CFG->wwwroot
. "/user/view.php?id=$id&course={$course->id}");
92 if ($user->id
!= $USER->id
and is_siteadmin($user) and !is_siteadmin($USER)) { // Only admins may edit other admins
93 print_error('useradmineditadmin');
96 if (isguestuser($user->id
)) { // the real guest user can not be edited
97 print_error('guestnoeditprofileother');
100 if ($user->deleted
) {
101 echo $OUTPUT->header();
102 echo $OUTPUT->heading(get_string('userdeleted'));
103 echo $OUTPUT->footer();
107 //load user preferences
108 useredit_load_preferences($user);
110 //Load custom profile fields data
111 profile_load_data($user);
114 if (!empty($CFG->usetags
)) {
115 require_once($CFG->dirroot
.'/tag/lib.php');
116 $user->interests
= tag_get_tags_array('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(
138 'context' => $coursecontext
142 // Prepare filemanager draft area.
144 $filemanagercontext = $editoroptions['context'];
145 $filemanageroptions = array('maxbytes' => $CFG->maxbytes
,
148 'accepted_types' => 'web_image');
149 file_prepare_draft_area($draftitemid, $filemanagercontext->id
, 'user', 'newicon', 0, $filemanageroptions);
150 $user->imagefile
= $draftitemid;
152 $userform = new user_editadvanced_form(null, array(
153 'editoroptions' => $editoroptions,
154 'filemanageroptions' => $filemanageroptions,
155 'userid' => $user->id
));
156 $userform->set_data($user);
158 if ($usernew = $userform->get_data()) {
160 if (empty($usernew->auth
)) {
162 $authplugin = get_auth_plugin($user->auth
);
163 unset($usernew->auth
); //can not change/remove
165 $authplugin = get_auth_plugin($usernew->auth
);
168 $usernew->timemodified
= time();
169 $createpassword = false;
171 if ($usernew->id
== -1) {
173 $createpassword = !empty($usernew->createpassword
);
174 unset($usernew->createpassword
);
175 $usernew = file_postupdate_standard_editor($usernew, 'description', $editoroptions, null, 'user', 'profile', null);
176 $usernew->mnethostid
= $CFG->mnet_localhost_id
; // always local user
177 $usernew->confirmed
= 1;
178 $usernew->timecreated
= time();
179 if ($authplugin->is_internal()) {
180 if ($createpassword or empty($usernew->newpassword
)) {
181 $usernew->password
= '';
183 $usernew->password
= hash_internal_user_password($usernew->newpassword
);
186 $usernew->password
= AUTH_PASSWORD_NOT_CACHED
;
188 $usernew->id
= user_create_user($usernew, false);
190 if (!$authplugin->is_internal() and $authplugin->can_change_password() and !empty($usernew->newpassword
)) {
191 if (!$authplugin->user_update_password($usernew, $usernew->newpassword
)) {
192 // Do not stop here, we need to finish user creation.
193 debugging(get_string('cannotupdatepasswordonextauth', '', '', $usernew->auth
), DEBUG_NONE
);
198 $usernew = file_postupdate_standard_editor($usernew, 'description', $editoroptions, $usercontext, 'user', 'profile', 0);
199 // Pass a true old $user here.
200 if (!$authplugin->user_update($user, $usernew)) {
201 // Auth update failed.
202 print_error('cannotupdateuseronexauth', '', '', $user->auth
);
204 user_update_user($usernew, false);
206 //set new password if specified
207 if (!empty($usernew->newpassword
)) {
208 if ($authplugin->can_change_password()) {
209 if (!$authplugin->user_update_password($usernew, $usernew->newpassword
)){
210 print_error('cannotupdatepasswordonextauth', '', '', $usernew->auth
);
212 unset_user_preference('create_password', $usernew); // prevent cron from generating the password
216 // force logout if user just suspended
217 if (isset($usernew->suspended
) and $usernew->suspended
and !$user->suspended
) {
218 \core\session\manager
::kill_user_sessions($user->id
);
222 $usercontext = context_user
::instance($usernew->id
);
225 useredit_update_user_preference($usernew);
228 if (!empty($CFG->usetags
) and empty($USER->newadminuser
)) {
229 useredit_update_interests($usernew, $usernew->interests
);
232 //update user picture
233 if (empty($USER->newadminuser
)) {
234 useredit_update_picture($usernew, $userform, $filemanageroptions);
237 // update mail bounces
238 useredit_update_bounces($user, $usernew);
240 // update forum track preference
241 useredit_update_trackforums($user, $usernew);
243 // save custom profile fields data
244 profile_save_data($usernew);
247 $usernew = $DB->get_record('user', array('id'=>$usernew->id
));
249 if ($createpassword) {
250 setnew_password_and_mail($usernew);
251 unset_user_preference('create_password', $usernew);
252 set_user_preference('auth_forcepasswordchange', 1, $usernew);
255 if ($user->id
== $USER->id
) {
256 // Override old $USER session variable
257 foreach ((array)$usernew as $variable => $value) {
258 if ($variable === 'description' or $variable === 'password') {
259 // These are not set for security nad perf reasons.
262 $USER->$variable = $value;
264 // preload custom fields
265 profile_load_custom_fields($USER);
267 if (!empty($USER->newadminuser
)) {
268 unset($USER->newadminuser
);
269 // apply defaults again - some of them might depend on admin user info, backup, roles, etc.
270 admin_apply_default_settings(NULL , false);
271 // redirect to admin/ to continue with installation
272 redirect("$CFG->wwwroot/$CFG->admin/");
274 redirect("$CFG->wwwroot/user/view.php?id=$USER->id&course=$course->id");
277 \core\session\manager
::gc(); // Remove stale sessions.
278 redirect("$CFG->wwwroot/$CFG->admin/user.php");
283 // make sure we really are on the https page when https login required
284 $PAGE->verify_https_required();
287 /// Display page header
288 if ($user->id
== -1 or ($user->id
!= $USER->id
)) {
289 if ($user->id
== -1) {
290 echo $OUTPUT->header();
292 $PAGE->set_heading($SITE->fullname
);
293 echo $OUTPUT->header();
294 $userfullname = fullname($user, true);
295 echo $OUTPUT->heading($userfullname);
297 } else if (!empty($USER->newadminuser
)) {
298 $strinstallation = get_string('installation', 'install');
299 $strprimaryadminsetup = get_string('primaryadminsetup');
301 $PAGE->navbar
->add($strprimaryadminsetup);
302 $PAGE->set_title($strinstallation);
303 $PAGE->set_heading($strinstallation);
304 $PAGE->set_cacheable(false);
306 echo $OUTPUT->header();
307 echo $OUTPUT->box(get_string('configintroadmin', 'admin'), 'generalbox boxwidthnormal boxaligncenter');
310 $streditmyprofile = get_string('editmyprofile');
311 $strparticipants = get_string('participants');
312 $strnewuser = get_string('newuser');
313 $userfullname = fullname($user, true);
315 $PAGE->set_title("$course->shortname: $streditmyprofile");
316 $PAGE->set_heading($course->fullname
);
318 echo $OUTPUT->header();
319 echo $OUTPUT->heading($userfullname);
322 /// Finally display THE form
323 $userform->display();
325 /// and proper footer
326 echo $OUTPUT->footer();