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');
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 $PAGE->set_course($SITE);
45 $PAGE->set_pagelayout('maintenance');
47 require_login($course);
48 $PAGE->set_pagelayout('admin');
51 if ($course->id
== SITEID
) {
52 $coursecontext = get_context_instance(CONTEXT_SYSTEM
); // SYSTEM context
54 $coursecontext = get_context_instance(CONTEXT_COURSE
, $course->id
); // Course context
56 $systemcontext = get_context_instance(CONTEXT_SYSTEM
);
60 $user = new stdClass();
62 $user->auth
= 'manual';
65 require_capability('moodle/user:create', $systemcontext);
66 admin_externalpage_setup('addnewuser', '', array('id' => -1));
68 // editing existing user
69 require_capability('moodle/user:update', $systemcontext);
70 $user = $DB->get_record('user', array('id'=>$id), '*', MUST_EXIST
);
71 $PAGE->set_context(get_context_instance(CONTEXT_USER
, $user->id
));
72 if ($user->id
== $USER->id
) {
73 if ($course->id
!= SITEID
&& $node = $PAGE->navigation
->find($course->id
, navigation_node
::TYPE_COURSE
)) {
75 $PAGE->navbar
->includesettingsbase
= true;
78 $PAGE->navigation
->extend_for_user($user);
82 // remote users cannot be edited
83 if ($user->id
!= -1 and is_mnet_remote_user($user)) {
84 redirect($CFG->wwwroot
. "/user/view.php?id=$id&course={$course->id}");
87 if ($user->id
!= $USER->id
and is_siteadmin($user) and !is_siteadmin($USER)) { // Only admins may edit other admins
88 print_error('useradmineditadmin');
91 if (isguestuser($user->id
)) { // the real guest user can not be edited
92 print_error('guestnoeditprofileother');
96 echo $OUTPUT->header();
97 echo $OUTPUT->heading(get_string('userdeleted'));
98 echo $OUTPUT->footer();
102 //load user preferences
103 useredit_load_preferences($user);
105 //Load custom profile fields data
106 profile_load_data($user);
109 if (!empty($CFG->usetags
)) {
110 require_once($CFG->dirroot
.'/tag/lib.php');
111 $user->interests
= tag_get_tags_array('user', $id);
114 if ($user->id
!== -1) {
115 $usercontext = get_context_instance(CONTEXT_USER
, $user->id
);
116 $editoroptions = array(
117 'maxfiles' => EDITOR_UNLIMITED_FILES
,
118 'maxbytes' => $CFG->maxbytes
,
119 'trusttext' => false,
120 'forcehttps' => false,
121 'context' => $usercontext
124 $user = file_prepare_standard_editor($user, 'description', $editoroptions, $usercontext, 'user', 'profile', 0);
127 // This is a new user, we don't want to add files here
128 $editoroptions = array(
133 'context' => $coursecontext
138 $userform = new user_editadvanced_form(null, array('editoroptions'=>$editoroptions));
139 $userform->set_data($user);
141 if ($usernew = $userform->get_data()) {
143 if (empty($usernew->auth
)) {
145 $authplugin = get_auth_plugin($user->auth
);
146 unset($usernew->auth
); //can not change/remove
148 $authplugin = get_auth_plugin($usernew->auth
);
151 $usernew->timemodified
= time();
153 if ($usernew->id
== -1) {
154 //TODO check out if it makes sense to create account with this auth plugin and what to do with the password
156 $usernew = file_postupdate_standard_editor($usernew, 'description', $editoroptions, null, 'user', 'profile', null);
157 $usernew->mnethostid
= $CFG->mnet_localhost_id
; // always local user
158 $usernew->confirmed
= 1;
159 $usernew->timecreated
= time();
160 $usernew->password
= hash_internal_user_password($usernew->newpassword
);
161 $usernew->id
= $DB->insert_record('user', $usernew);
163 add_to_log($course->id
, 'user', 'add', "view.php?id=$usernew->id&course=$course->id", '');
166 $usernew = file_postupdate_standard_editor($usernew, 'description', $editoroptions, $usercontext, 'user', 'profile', 0);
167 $DB->update_record('user', $usernew);
168 // pass a true $userold here
169 if (! $authplugin->user_update($user, $userform->get_data())) {
170 // auth update failed, rollback for moodle
171 $DB->update_record('user', $user);
172 print_error('cannotupdateuseronexauth', '', '', $user->auth
);
174 add_to_log($course->id
, 'user', 'update', "view.php?id=$user->id&course=$course->id", '');
176 //set new password if specified
177 if (!empty($usernew->newpassword
)) {
178 if ($authplugin->can_change_password()) {
179 if (!$authplugin->user_update_password($usernew, $usernew->newpassword
)){
180 print_error('cannotupdatepasswordonextauth', '', '', $usernew->auth
);
182 unset_user_preference('create_password', $usernew); // prevent cron from generating the password
185 $usercreated = false;
188 $usercontext = get_context_instance(CONTEXT_USER
, $usernew->id
);
191 useredit_update_user_preference($usernew);
194 if (!empty($CFG->usetags
) and empty($USER->newadminuser
)) {
195 useredit_update_interests($usernew, $usernew->interests
);
198 //update user picture
199 if (!empty($CFG->gdversion
) and empty($USER->newadminuser
)) {
200 useredit_update_picture($usernew, $userform);
203 // update mail bounces
204 useredit_update_bounces($user, $usernew);
206 // update forum track preference
207 useredit_update_trackforums($user, $usernew);
209 // save custom profile fields data
210 profile_save_data($usernew);
213 $usernew = $DB->get_record('user', array('id'=>$usernew->id
));
217 events_trigger('user_created', $usernew);
219 events_trigger('user_updated', $usernew);
222 if ($user->id
== $USER->id
) {
223 // Override old $USER session variable
224 foreach ((array)$usernew as $variable => $value) {
225 $USER->$variable = $value;
227 // preload custom fields
228 profile_load_custom_fields($USER);
230 if (!empty($USER->newadminuser
)) {
231 unset($USER->newadminuser
);
232 // apply defaults again - some of them might depend on admin user info, backup, roles, etc.
233 admin_apply_default_settings(NULL , false);
234 // redirect to admin/ to continue with installation
235 redirect("$CFG->wwwroot/$CFG->admin/");
237 redirect("$CFG->wwwroot/user/view.php?id=$USER->id&course=$course->id");
240 session_gc(); // remove stale sessions
241 redirect("$CFG->wwwroot/$CFG->admin/user.php");
246 // make sure we really are on the https page when https login required
247 $PAGE->verify_https_required();
250 /// Display page header
251 if ($user->id
== -1 or ($user->id
!= $USER->id
)) {
252 if ($user->id
== -1) {
253 echo $OUTPUT->header();
255 $PAGE->set_heading($SITE->fullname
);
256 echo $OUTPUT->header();
257 $userfullname = fullname($user, true);
258 echo $OUTPUT->heading($userfullname);
260 } else if (!empty($USER->newadminuser
)) {
261 $strinstallation = get_string('installation', 'install');
262 $strprimaryadminsetup = get_string('primaryadminsetup');
264 $PAGE->navbar
->add($strprimaryadminsetup);
265 $PAGE->set_title($strinstallation);
266 $PAGE->set_heading($strinstallation);
267 $PAGE->set_cacheable(false);
269 echo $OUTPUT->header();
270 echo $OUTPUT->box(get_string('configintroadmin', 'admin'), 'generalbox boxwidthnormal boxaligncenter');
273 $streditmyprofile = get_string('editmyprofile');
274 $strparticipants = get_string('participants');
275 $strnewuser = get_string('newuser');
276 $userfullname = fullname($user, true);
278 $PAGE->set_title("$course->shortname: $streditmyprofile");
279 $PAGE->set_heading($course->fullname
);
281 echo $OUTPUT->header();
282 echo $OUTPUT->heading($userfullname);
285 /// Finally display THE form
286 $userform->display();
288 /// and proper footer
289 echo $OUTPUT->footer();