Updated the 19 build version to 20081220
[moodle.git] / user / editadvanced.php
blob90a0fca9b7ffb073c8f11d29dd0450f4ba704d00
1 <?php // $Id$
3 require_once('../config.php');
4 require_once($CFG->libdir.'/gdlib.php');
5 require_once($CFG->libdir.'/adminlib.php');
6 require_once($CFG->dirroot.'/user/editadvanced_form.php');
7 require_once($CFG->dirroot.'/user/editlib.php');
8 require_once($CFG->dirroot.'/user/profile/lib.php');
10 httpsrequired();
12 $id = optional_param('id', $USER->id, PARAM_INT); // user id; -1 if creating new user
13 $course = optional_param('course', SITEID, PARAM_INT); // course id (defaults to Site)
15 if (!$course = get_record('course', 'id', $course)) {
16 error('Course ID was incorrect');
18 require_login($course->id);
20 if ($course->id == SITEID) {
21 $coursecontext = get_context_instance(CONTEXT_SYSTEM); // SYSTEM context
22 } else {
23 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); // Course context
25 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
27 if ($id == -1) {
28 // creating new user
29 require_capability('moodle/user:create', $systemcontext);
30 $user = new object();
31 $user->id = -1;
32 $user->auth = 'manual';
33 $user->confirmed = 1;
34 $user->deleted = 0;
35 } else {
36 // editing existing user
37 require_capability('moodle/user:update', $systemcontext);
38 if (!$user = get_record('user', 'id', $id)) {
39 error('User ID was incorrect');
43 // remote users cannot be edited
44 if ($user->id != -1 and is_mnet_remote_user($user)) {
45 redirect($CFG->wwwroot . "/user/view.php?id=$id&course={$course->id}");
48 if ($user->id != $USER->id and is_primary_admin($user->id)) { // Can't edit primary admin
49 print_error('adminprimarynoedit');
52 if (isguestuser($user->id)) { // the real guest user can not be edited
53 print_error('guestnoeditprofileother');
56 if ($user->deleted) {
57 print_header();
58 print_heading(get_string('userdeleted'));
59 print_footer($course);
60 die;
63 //load user preferences
64 useredit_load_preferences($user);
66 //Load custom profile fields data
67 profile_load_data($user);
69 //user interests separated by commas
70 if (!empty($CFG->usetags)) {
71 require_once($CFG->dirroot.'/tag/lib.php');
72 $user->interests = tag_get_tags_csv('user', $id, TAG_RETURN_TEXT); // formslib uses htmlentities itself
75 //create form
76 $userform = new user_editadvanced_form();
77 $userform->set_data($user);
79 if ($usernew = $userform->get_data()) {
80 add_to_log($course->id, 'user', 'update', "view.php?id=$user->id&course=$course->id", '');
82 if (empty($usernew->auth)) {
83 //user editing self
84 $authplugin = get_auth_plugin($user->auth);
85 unset($usernew->auth); //can not change/remove
86 } else {
87 $authplugin = get_auth_plugin($usernew->auth);
90 $usernew->username = trim($usernew->username);
91 $usernew->timemodified = time();
93 if ($usernew->id == -1) {
94 //TODO check out if it makes sense to create account with this auth plugin and what to do with the password
95 unset($usernew->id);
96 $usernew->mnethostid = $CFG->mnet_localhost_id; // always local user
97 $usernew->confirmed = 1;
98 $usernew->password = hash_internal_user_password($usernew->newpassword);
99 if (!$usernew->id = insert_record('user', $usernew)) {
100 error('Error creating user record');
102 $usercreated = true;
103 } else {
104 if (!update_record('user', $usernew)) {
105 error('Error updating user record');
107 // pass a true $userold here
108 if (! $authplugin->user_update($user, $userform->get_data(false))) {
109 // auth update failed, rollback for moodle
110 update_record('user', addslashes_object($user));
111 error('Failed to update user data on external auth: '.$user->auth.
112 '. See the server logs for more details.');
115 //set new password if specified
116 if (!empty($usernew->newpassword)) {
117 if ($authplugin->can_change_password()) {
118 if (!$authplugin->user_update_password($usernew, $usernew->newpassword)){
119 error('Failed to update password on external auth: ' . $usernew->auth .
120 '. See the server logs for more details.');
124 $usercreated = false;
127 //update preferences
128 useredit_update_user_preference($usernew);
130 // update tags
131 if (!empty($CFG->usetags)) {
132 useredit_update_interests($usernew, $usernew->interests);
135 //update user picture
136 if (!empty($CFG->gdversion)) {
137 useredit_update_picture($usernew, $userform);
140 // update mail bounces
141 useredit_update_bounces($user, $usernew);
143 // update forum track preference
144 useredit_update_trackforums($user, $usernew);
146 // save custom profile fields data
147 profile_save_data($usernew);
149 // reload from db
150 $usernew = get_record('user', 'id', $usernew->id);
152 // trigger events
153 if ($usercreated) {
154 events_trigger('user_created', $usernew);
155 } else {
156 events_trigger('user_updated', $usernew);
159 if ($user->id == $USER->id) {
160 // Override old $USER session variable
161 foreach ((array)$usernew as $variable => $value) {
162 $USER->$variable = $value;
164 if (!empty($USER->newadminuser)) {
165 unset($USER->newadminuser);
166 // apply defaults again - some of them might depend on admin user info, backup, roles, etc.
167 admin_apply_default_settings(NULL , false);
168 // redirect to admin/ to continue with installation
169 redirect("$CFG->wwwroot/$CFG->admin/");
170 } else {
171 redirect("$CFG->wwwroot/user/view.php?id=$USER->id&course=$course->id");
173 } else {
174 redirect("$CFG->wwwroot/$CFG->admin/user.php");
176 //never reached
180 /// Display page header
181 if ($user->id == -1 or ($user->id != $USER->id)) {
182 if ($user->id == -1) {
183 admin_externalpage_setup('addnewuser');
184 admin_externalpage_print_header();
185 } else {
186 admin_externalpage_setup('editusers');
187 admin_externalpage_print_header();
188 $userfullname = fullname($user, true);
189 print_heading($userfullname);
191 } else if (!empty($USER->newadminuser)) {
192 $strprimaryadminsetup = get_string('primaryadminsetup');
193 print_header($strprimaryadminsetup, $strprimaryadminsetup);
194 print_simple_box(get_string('configintroadmin', 'admin'), 'center', '50%');
195 echo '<br />';
196 } else {
197 $streditmyprofile = get_string('editmyprofile');
198 $strparticipants = get_string('participants');
199 $strnewuser = get_string('newuser');
200 $userfullname = fullname($user, true);
202 $navlinks = array();
203 if (has_capability('moodle/course:viewparticipants', $coursecontext) || has_capability('moodle/site:viewparticipants', $systemcontext)) {
204 $navlinks[] = array('name' => $strparticipants, 'link' => "index.php?id=$course->id", 'type' => 'misc');
206 $navlinks[] = array('name' => $userfullname,
207 'link' => "view.php?id=$user->id&amp;course=$course->id",
208 'type' => 'misc');
209 $navlinks[] = array('name' => $streditmyprofile, 'link' => null, 'type' => 'misc');
210 $navigation = build_navigation($navlinks);
211 print_header("$course->shortname: $streditmyprofile", $course->fullname, $navigation, "");
213 /// Print tabs at the top
214 $showroles = 1;
215 $currenttab = 'editprofile';
216 require('tabs.php');
219 /// Finally display THE form
220 $userform->display();
222 /// and proper footer
223 if (!empty($USER->newadminuser)) {
224 print_footer('none');
225 } else {
226 print_footer($course);