on-demand release 2.3dev
[moodle.git] / user / editadvanced.php
CommitLineData
ce221eb5 1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
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.
9//
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.
14//
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/>.
17
18/**
19 * Allows you to edit a users profile
20 *
21 * @copyright 1999 Martin Dougiamas http://dougiamas.com
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 * @package user
24 */
25
26require_once('../config.php');
27require_once($CFG->libdir.'/gdlib.php');
28require_once($CFG->libdir.'/adminlib.php');
29require_once($CFG->dirroot.'/user/editadvanced_form.php');
30require_once($CFG->dirroot.'/user/editlib.php');
31require_once($CFG->dirroot.'/user/profile/lib.php');
ce221eb5 32
17c70aa0
PS
33//HTTPS is required in this page when $CFG->loginhttps enabled
34$PAGE->https_required();
ce221eb5 35
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)
38
17c70aa0 39$PAGE->set_url('/user/editadvanced.php', array('course'=>$course, 'id'=>$id));
ce221eb5 40
3406acde
SH
41$course = $DB->get_record('course', array('id'=>$course), '*', MUST_EXIST);
42
ce221eb5 43if (!empty($USER->newadminuser)) {
44 $PAGE->set_course($SITE);
78946b9b 45 $PAGE->set_pagelayout('maintenance');
ce221eb5 46} else {
47 require_login($course);
3406acde 48 $PAGE->set_pagelayout('admin');
ce221eb5 49}
50
51if ($course->id == SITEID) {
52 $coursecontext = get_context_instance(CONTEXT_SYSTEM); // SYSTEM context
53} else {
54 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); // Course context
55}
56$systemcontext = get_context_instance(CONTEXT_SYSTEM);
57
58if ($id == -1) {
59 // creating new user
bf718f50 60 $user = new stdClass();
ce221eb5 61 $user->id = -1;
6b8ad965 62 $user->auth = 'manual';
ce221eb5 63 $user->confirmed = 1;
64 $user->deleted = 0;
3406acde
SH
65 require_capability('moodle/user:create', $systemcontext);
66 admin_externalpage_setup('addnewuser', '', array('id' => -1));
ce221eb5 67} else {
68 // editing existing user
69 require_capability('moodle/user:update', $systemcontext);
3406acde
SH
70 $user = $DB->get_record('user', array('id'=>$id), '*', MUST_EXIST);
71 $PAGE->set_context(get_context_instance(CONTEXT_USER, $user->id));
5ac851fb
SH
72 if ($user->id == $USER->id) {
73 if ($course->id != SITEID && $node = $PAGE->navigation->find($course->id, navigation_node::TYPE_COURSE)) {
74 $node->make_active();
75 $PAGE->navbar->includesettingsbase = true;
76 }
77 } else {
78 $PAGE->navigation->extend_for_user($user);
79 }
ce221eb5 80}
ad6226fb 81
ce221eb5 82// remote users cannot be edited
83if ($user->id != -1 and is_mnet_remote_user($user)) {
84 redirect($CFG->wwwroot . "/user/view.php?id=$id&course={$course->id}");
85}
ad6226fb 86
4f622c38
PS
87if ($user->id != $USER->id and is_siteadmin($user) and !is_siteadmin($USER)) { // Only admins may edit other admins
88 print_error('useradmineditadmin');
ce221eb5 89}
ad6226fb 90
ce221eb5 91if (isguestuser($user->id)) { // the real guest user can not be edited
92 print_error('guestnoeditprofileother');
93}
ad6226fb 94
ce221eb5 95if ($user->deleted) {
96 echo $OUTPUT->header();
97 echo $OUTPUT->heading(get_string('userdeleted'));
98 echo $OUTPUT->footer();
99 die;
100}
101
ce221eb5 102//load user preferences
103useredit_load_preferences($user);
104
105//Load custom profile fields data
106profile_load_data($user);
107
108//User interests
109if (!empty($CFG->usetags)) {
110 require_once($CFG->dirroot.'/tag/lib.php');
111 $user->interests = tag_get_tags_array('user', $id);
112}
113
8bdc9cac 114if ($user->id !== -1) {
4f0c2d00 115 $usercontext = get_context_instance(CONTEXT_USER, $user->id);
e9de1cf4
RT
116 $editoroptions = array(
117 'maxfiles' => EDITOR_UNLIMITED_FILES,
118 'maxbytes' => $CFG->maxbytes,
119 'trusttext' => false,
120 'forcehttps' => false,
121 'context' => $usercontext
122 );
123
64f93798 124 $user = file_prepare_standard_editor($user, 'description', $editoroptions, $usercontext, 'user', 'profile', 0);
8bdc9cac 125} else {
4f0c2d00 126 $usercontext = null;
8bdc9cac 127 // This is a new user, we don't want to add files here
dedb69a2
RT
128 $editoroptions = array(
129 'maxfiles'=>0,
130 'maxbytes'=>0,
131 'trusttext'=>false,
132 'forcehttps'=>false,
133 'context' => $coursecontext
134 );
8bdc9cac
SH
135}
136
ce221eb5 137//create form
8bdc9cac 138$userform = new user_editadvanced_form(null, array('editoroptions'=>$editoroptions));
ce221eb5 139$userform->set_data($user);
140
141if ($usernew = $userform->get_data()) {
ce221eb5 142
143 if (empty($usernew->auth)) {
144 //user editing self
145 $authplugin = get_auth_plugin($user->auth);
146 unset($usernew->auth); //can not change/remove
147 } else {
148 $authplugin = get_auth_plugin($usernew->auth);
1e1c51a3 149 }
6b8ad965
PS
150
151 $usernew->timemodified = time();
ad6226fb 152
ce221eb5 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
155 unset($usernew->id);
64f93798 156 $usernew = file_postupdate_standard_editor($usernew, 'description', $editoroptions, null, 'user', 'profile', null);
ce221eb5 157 $usernew->mnethostid = $CFG->mnet_localhost_id; // always local user
d3d393ab
RW
158 $usernew->confirmed = 1;
159 $usernew->timecreated = time();
6b8ad965 160 $usernew->password = hash_internal_user_password($usernew->newpassword);
ce221eb5 161 $usernew->id = $DB->insert_record('user', $usernew);
162 $usercreated = true;
44ef537d 163 add_to_log($course->id, 'user', 'add', "view.php?id=$usernew->id&course=$course->id", '');
ad6226fb 164
ce221eb5 165 } else {
64f93798 166 $usernew = file_postupdate_standard_editor($usernew, 'description', $editoroptions, $usercontext, 'user', 'profile', 0);
ce221eb5 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);
d8734783 173 }
44ef537d 174 add_to_log($course->id, 'user', 'update', "view.php?id=$user->id&course=$course->id", '');
ad6226fb 175
ce221eb5 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);
ad6226fb 181 }
8bdb31ed 182 unset_user_preference('create_password', $usernew); // prevent cron from generating the password
ad6226fb 183 }
184 }
4ad72c28
PS
185
186 // force logout if user just suspended
187 if (isset($usernew->suspended) and $usernew->suspended and !$user->suspended) {
188 session_kill_user($user->id);
189 }
190
ce221eb5 191 $usercreated = false;
192 }
ad6226fb 193
ce221eb5 194 $usercontext = get_context_instance(CONTEXT_USER, $usernew->id);
98bc6446 195
ce221eb5 196 //update preferences
197 useredit_update_user_preference($usernew);
ad6226fb 198
ce221eb5 199 // update tags
de2d81fa 200 if (!empty($CFG->usetags) and empty($USER->newadminuser)) {
ce221eb5 201 useredit_update_interests($usernew, $usernew->interests);
202 }
1e1c51a3 203
ce221eb5 204 //update user picture
de2d81fa 205 if (!empty($CFG->gdversion) and empty($USER->newadminuser)) {
ce221eb5 206 useredit_update_picture($usernew, $userform);
207 }
ad6226fb 208
ce221eb5 209 // update mail bounces
210 useredit_update_bounces($user, $usernew);
ad6226fb 211
ce221eb5 212 // update forum track preference
213 useredit_update_trackforums($user, $usernew);
ad6226fb 214
ce221eb5 215 // save custom profile fields data
216 profile_save_data($usernew);
ad6226fb 217
ce221eb5 218 // reload from db
219 $usernew = $DB->get_record('user', array('id'=>$usernew->id));
5e61d1a4 220
ce221eb5 221 // trigger events
222 if ($usercreated) {
ce221eb5 223 events_trigger('user_created', $usernew);
224 } else {
225 events_trigger('user_updated', $usernew);
226 }
2942a5cd 227
ce221eb5 228 if ($user->id == $USER->id) {
229 // Override old $USER session variable
230 foreach ((array)$usernew as $variable => $value) {
231 $USER->$variable = $value;
232 }
a1248ca4
PS
233 // preload custom fields
234 profile_load_custom_fields($USER);
235
ce221eb5 236 if (!empty($USER->newadminuser)) {
237 unset($USER->newadminuser);
238 // apply defaults again - some of them might depend on admin user info, backup, roles, etc.
239 admin_apply_default_settings(NULL , false);
240 // redirect to admin/ to continue with installation
241 redirect("$CFG->wwwroot/$CFG->admin/");
afb5b0ae 242 } else {
ce221eb5 243 redirect("$CFG->wwwroot/user/view.php?id=$USER->id&course=$course->id");
ad6226fb 244 }
ce221eb5 245 } else {
6b8ad965 246 session_gc(); // remove stale sessions
ce221eb5 247 redirect("$CFG->wwwroot/$CFG->admin/user.php");
ad6226fb 248 }
ce221eb5 249 //never reached
250}
ad6226fb 251
17c70aa0
PS
252// make sure we really are on the https page when https login required
253$PAGE->verify_https_required();
254
ad6226fb 255
256/// Display page header
ce221eb5 257if ($user->id == -1 or ($user->id != $USER->id)) {
258 if ($user->id == -1) {
61ef8f9f 259 echo $OUTPUT->header();
ad6226fb 260 } else {
8cb89f5b 261 $PAGE->set_heading($SITE->fullname);
61ef8f9f 262 echo $OUTPUT->header();
ce221eb5 263 $userfullname = fullname($user, true);
264 echo $OUTPUT->heading($userfullname);
265 }
266} else if (!empty($USER->newadminuser)) {
267 $strinstallation = get_string('installation', 'install');
268 $strprimaryadminsetup = get_string('primaryadminsetup');
269
270 $PAGE->navbar->add($strprimaryadminsetup);
271 $PAGE->set_title($strinstallation);
272 $PAGE->set_heading($strinstallation);
273 $PAGE->set_cacheable(false);
274
275 echo $OUTPUT->header();
276 echo $OUTPUT->box(get_string('configintroadmin', 'admin'), 'generalbox boxwidthnormal boxaligncenter');
277 echo '<br />';
278} else {
279 $streditmyprofile = get_string('editmyprofile');
280 $strparticipants = get_string('participants');
281 $strnewuser = get_string('newuser');
282 $userfullname = fullname($user, true);
283
ce221eb5 284 $PAGE->set_title("$course->shortname: $streditmyprofile");
285 $PAGE->set_heading($course->fullname);
286
287 echo $OUTPUT->header();
03d9401e 288 echo $OUTPUT->heading($userfullname);
ce221eb5 289}
ad6226fb 290
291/// Finally display THE form
ce221eb5 292$userform->display();
ad6226fb 293
294/// and proper footer
ce221eb5 295echo $OUTPUT->footer();
ad6226fb 296