MDL-8096 - user edit form splitting, refactoring, cleanup and fixing - work in progre...
[moodle.git] / user / editadvanced.php
CommitLineData
ad6226fb 1<?php // $Id$
2
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
8 $id = optional_param('id', $USER->id, PARAM_INT); // user id; -1 if creating new user
9 $course = optional_param('course', SITEID, PARAM_INT); // course id (defaults to Site)
10
11 if (!$course = get_record('course', 'id', $course)) {
12 error('Course ID was incorrect');
13 }
14 require_login($course->id);
15 httpsrequired(); // HTTPS is potentially required in this page because there are passwords
16
17 if ($id == -1) {
18 // creating new user
19 require_capability('moodle/user:create', get_context_instance(CONTEXT_SYSTEM, SITEID));
20 $user = new object();
21 $user->id = -1;
22 $user->auth = 'manual';
23 $user->confirmed = 1;
24 } else {
25 // editing existing user
26 require_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM, SITEID));
27 if (!$user = get_record('user', 'id', $id)) {
28 error('User ID was incorrect');
29 }
30 }
31
32 // remote users cannot be edited
33 if ($user->id != -1 and is_mnet_remote_user($user)) {
34 redirect($CFG->wwwroot . "/user/view.php?id=$id&course={$course->id}");
35 }
36
37 $mainadmin = get_admin();
38 if ($user->id != $USER->id and $user->id == $mainadmin->id) { // Can't edit primary admin
39 print_error('adminprimarynoedit');
40 }
41
42 if (isguestuser($user->id)) { // the real guest user can not be edited
43 print_error('guestnoeditprofileother');
44 }
45
46 //load preferences
47 if (!empty($user->id) and $preferences = get_user_preferences(null, null, $user->id)) {
48 foreach($preferences as $name=>$value) {
49 $user->{'preference_'.$name} = $value;
50 }
51 }
52 //TODO: Load the custom profile fields
53
54 //create form
55 $userform = new user_editadvanced_form(null, $course);
56 $userform->set_data($user);
57
58 if ($usernew = $userform->get_data()) {
59 add_to_log($course->id, 'user', 'update', "view.php?id=$user->id&course=$course->id", '');
60
61 $authplugin = get_auth_plugin($CFG->auth);
62
63 $usernew->timemodified = time();
64
65 if ($usernew->id == -1) {
66 unset($usernew->id);
67 $usernew->mnethostid = $CFG->mnet_localhost_id; // always local user
68 $usernew->confirmed = 1;
69 if (!$usernew->id = insert_record('user', $usernew)) {
70 error('Error creating user record');
71 }
72 } else {
73 if (update_record('user', $usernew)) {
74 if (method_exists($authplugin, 'user_update')){
75 // pass a true $userold here
76 if (! $authplugin->user_update($user, $userform->get_data(false))) {
77 // auth update failed, rollback for moodle
78 update_record('user', addslashes_object($user));
79 error('Failed to update user data on external auth: '.$usernew->auth.
80 '. See the server logs for more details.');
81 }
82 };
83 } else {
84 error('Error updating user record');
85 }
86 }
87
88 //set new password if specified
89 if (!empty($usernew->newpassword)) {
90 if ($authplugin->can_change_password()) {
91 if (method_exists($authplugin, 'user_update_password')){
92 if (!$authplugin->user_update_password($user->username, $usernew->newpassword)){
93 error('Failed to update password on external auth: ' . $usernew->auth .
94 '. See the server logs for more details.');
95 }
96 } else {
97 error('Your external authentication module is misconfigued!');
98 }
99 }
100 }
101
102 //update preferences
103 $ua = (array)$usernew;
104 foreach($ua as $key=>$value) {
105 if (strpos($key, 'preference_') === 0) {
106 $name = substr($key, strlen('preference_'));
107 set_user_preference($name, $value, $usernew->id);
108 }
109 }
110
111 //update user picture
112 if ($usernew->deletepicture) {
113 //TODO - delete the files
114 set_field('user', 'picture', 0, 'id', $usernew->id);
115 } else if ($usernew->picture = save_profile_image($usernew->id, $userform->get_um(), 'users')) {
116 set_field('user', 'picture', 1, 'id', $usernew->id);
117 }
118
119 // update mail bounces
120 if ($user->email != $usernew->email) {
121 set_bounce_count($usernew,true);
122 set_send_count($usernew,true);
123 }
124
125 /// Update forum track preference.
126 if (($usernew->trackforums != $user->trackforums) && !$usernew->trackforums) {
127 require_once($CFG->dirroot.'/mod/forum/lib.php');
128 forum_tp_delete_read_records($usernew->id);
129 }
130
131 //TODO: Save the custom profile fields
132
133 if ($user->id == $USER->id) {
134 // Override old $USER session variable
135 $usernew = (array)get_record('user', 'id', $newuser->id); // reload from db
136 foreach ($usernew as $variable => $value) {
137 $USER->$variable = $value;
138 }
139 if (!empty($USER->newadminuser)) {
140 unset($USER->newadminuser);
141 // redirect to admin/ to continue with installation
142 redirect("$CFG->wwwroot/$CFG->admin/");
143 } else {
144 redirect("$CFG->wwwroot/user/view.php?id=$USER->id&course=$course->id");
145 }
146 } else {
147 redirect("$CFG->wwwroot/$CFG->admin/user.php");
148 }
149 //never reached
150 }
151
152
153/// Display page header
154 if ($user->id == -1 or ($user->id != $USER->id)) {
155 $adminroot = admin_get_root();
156 if ($user->id == -1) {
157 admin_externalpage_setup('addnewuser', $adminroot);
158 admin_externalpage_print_header($adminroot);
159 } else {
160 admin_externalpage_setup('editusers', $adminroot);
161 admin_externalpage_print_header($adminroot);
162 $userfullname = fullname($user, true);
163 print_heading($userfullname);
164 }
165 } else if (!empty($USER->newadminuser)) {
166 print_header();
167 print_simple_box(get_string('configintroadmin', 'admin'), 'center', '50%');
168 echo '<br />';
169 } else {
170 $streditmyprofile = get_string('editmyprofile');
171 $strparticipants = get_string('participants');
172 $strnewuser = get_string('newuser');
173 $userfullname = fullname($user, true);
174 if ($course->id != SITEID) {
175 print_header("$course->shortname: $streditmyprofile", "$course->fullname: $streditmyprofile",
176 "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a>
177 -> <a href=\"index.php?id=$course->id\">$strparticipants</a>
178 -> <a href=\"view.php?id=$user->id&amp;course=$course->id\">$userfullname</a>
179 -> $streditmyprofile", "");
180 } else {
181 print_header("$course->shortname: $streditmyprofile", "$course->fullname",
182 "<a href=\"view.php?id=$user->id&amp;course=$course->id\">$userfullname</a>
183 -> $streditmyprofile", "");
184 }
185 /// Print tabs at the top
186 $showroles = 1;
187 $currenttab = 'editprofile';
188 require('tabs.php');
189 }
190
191/// Finally display THE form
192 $userform->display();
193
194/// and proper footer
195 if ($user->id == -1) {
196 admin_externalpage_print_footer($adminroot);
197 } else if (!empty($USER->newadminuser)) {
198 print_footer('none');
199 } else {
200 print_footer($course);
201 }
202
203?>