MDL-10210 Added the user preferences, using moodleform.
[moodle-pu.git] / user / editadvanced.php
blobc76674e71f9568a155dbd58ac4c5f8f701a876ab
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 ($id == -1) {
21 // creating new user
22 require_capability('moodle/user:create', get_context_instance(CONTEXT_SYSTEM));
23 $user = new object();
24 $user->id = -1;
25 $user->auth = 'manual';
26 $user->confirmed = 1;
27 } else {
28 // editing existing user
29 require_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM));
30 if (!$user = get_record('user', 'id', $id)) {
31 error('User ID was incorrect');
35 // remote users cannot be edited
36 if ($user->id != -1 and is_mnet_remote_user($user)) {
37 redirect($CFG->wwwroot . "/user/view.php?id=$id&course={$course->id}");
40 $mainadmin = get_admin();
41 if ($user->id != $USER->id and $user->id == $mainadmin->id) { // Can't edit primary admin
42 print_error('adminprimarynoedit');
45 if (isguestuser($user->id)) { // the real guest user can not be edited
46 print_error('guestnoeditprofileother');
49 //load user preferences
50 useredit_load_preferences($user);
52 //Load custom profile fields data
53 profile_load_data($user);
55 //create form
56 $userform = new user_editadvanced_form();
57 $userform->set_data($user);
59 if ($usernew = $userform->get_data()) {
60 add_to_log($course->id, 'user', 'update', "view.php?id=$user->id&course=$course->id", '');
62 if (empty($usernew->auth)) {
63 //user editing self
64 $authplugin = get_auth_plugin($user->auth);
65 unset($usernew->auth); //can not change/remove
66 } else {
67 $authplugin = get_auth_plugin($usernew->auth);
70 $usernew->username = trim($usernew->username);
71 $usernew->timemodified = time();
73 if ($usernew->id == -1) {
74 //TODO check out if it makes sense to create account with this auth plugin and what to do with the password
75 unset($usernew->id);
76 $usernew->mnethostid = $CFG->mnet_localhost_id; // always local user
77 $usernew->confirmed = 1;
78 $usernew->password = hash_internal_user_password($usernew->newpassword);
79 if (!$usernew->id = insert_record('user', $usernew)) {
80 error('Error creating user record');
82 } else {
83 if (!update_record('user', $usernew)) {
84 error('Error updating user record');
86 // pass a true $userold here
87 if (! $authplugin->user_update($user, $userform->get_data(false))) {
88 // auth update failed, rollback for moodle
89 update_record('user', addslashes_object($user));
90 error('Failed to update user data on external auth: '.$user->auth.
91 '. See the server logs for more details.');
94 //set new password if specified
95 if (!empty($usernew->newpassword)) {
96 if ($authplugin->can_change_password()) {
97 if (!$authplugin->user_update_password($usernew, $usernew->newpassword)){
98 error('Failed to update password on external auth: ' . $usernew->auth .
99 '. See the server logs for more details.');
105 //update preferences
106 useredit_update_user_preference($usernew);
108 //update user picture
109 if (!empty($CFG->gdversion)) {
110 useredit_update_picture($usernew, $userform);
113 // update mail bounces
114 useredit_update_bounces($user, $usernew);
116 /// update forum track preference
117 useredit_update_trackforums($user, $usernew);
119 // save custom profile fields data
120 profile_save_data($usernew);
122 if ($user->id == $USER->id) {
123 // Override old $USER session variable
124 $usernew = (array)get_record('user', 'id', $usernew->id); // reload from db
125 foreach ($usernew as $variable => $value) {
126 $USER->$variable = $value;
128 if (!empty($USER->newadminuser)) {
129 unset($USER->newadminuser);
130 // redirect to admin/ to continue with installation
131 redirect("$CFG->wwwroot/$CFG->admin/");
132 } else {
133 redirect("$CFG->wwwroot/user/view.php?id=$USER->id&course=$course->id");
135 } else {
136 redirect("$CFG->wwwroot/$CFG->admin/user.php");
138 //never reached
142 /// Display page header
143 if ($user->id == -1 or ($user->id != $USER->id)) {
144 if ($user->id == -1) {
145 admin_externalpage_setup('addnewuser');
146 admin_externalpage_print_header();
147 } else {
148 admin_externalpage_setup('editusers');
149 admin_externalpage_print_header();
150 $userfullname = fullname($user, true);
151 print_heading($userfullname);
153 } else if (!empty($USER->newadminuser)) {
154 $strprimaryadminsetup = get_string('primaryadminsetup');
155 print_header($strprimaryadminsetup, $strprimaryadminsetup);
156 print_simple_box(get_string('configintroadmin', 'admin'), 'center', '50%');
157 echo '<br />';
158 } else {
159 $streditmyprofile = get_string('editmyprofile');
160 $strparticipants = get_string('participants');
161 $strnewuser = get_string('newuser');
162 $userfullname = fullname($user, true);
163 if ($course->id != SITEID) {
164 print_header("$course->shortname: $streditmyprofile", "$course->fullname: $streditmyprofile",
165 "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a>
166 -> <a href=\"index.php?id=$course->id\">$strparticipants</a>
167 -> <a href=\"view.php?id=$user->id&amp;course=$course->id\">$userfullname</a>
168 -> $streditmyprofile", "");
169 } else {
170 print_header("$course->shortname: $streditmyprofile", $course->fullname,
171 "<a href=\"view.php?id=$user->id&amp;course=$course->id\">$userfullname</a>
172 -> $streditmyprofile", "");
174 /// Print tabs at the top
175 $showroles = 1;
176 $currenttab = 'editprofile';
177 require('tabs.php');
180 /// Finally display THE form
181 $userform->display();
183 /// and proper footer
184 if (!empty($USER->newadminuser)) {
185 print_footer('none');
186 } else {
187 print_footer($course);