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');
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
);
22 require_capability('moodle/user:create', get_context_instance(CONTEXT_SYSTEM
));
25 $user->auth
= 'manual';
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 //user interests separated by commas
56 if (!empty($CFG->usetags
)) {
57 require_once($CFG->dirroot
.'/tag/lib.php');
58 $user->interests
= tag_names_csv(get_item_tags('user',$id));
62 $userform = new user_editadvanced_form();
63 $userform->set_data($user);
65 if ($usernew = $userform->get_data()) {
66 add_to_log($course->id
, 'user', 'update', "view.php?id=$user->id&course=$course->id", '');
68 if (empty($usernew->auth
)) {
70 $authplugin = get_auth_plugin($user->auth
);
71 unset($usernew->auth
); //can not change/remove
73 $authplugin = get_auth_plugin($usernew->auth
);
76 $usernew->username
= trim($usernew->username
);
77 $usernew->timemodified
= time();
79 if ($usernew->id
== -1) {
80 //TODO check out if it makes sense to create account with this auth plugin and what to do with the password
82 $usernew->mnethostid
= $CFG->mnet_localhost_id
; // always local user
83 $usernew->confirmed
= 1;
84 $usernew->password
= hash_internal_user_password($usernew->newpassword
);
85 if (!$usernew->id
= insert_record('user', $usernew)) {
86 error('Error creating user record');
89 if (!update_record('user', $usernew)) {
90 error('Error updating user record');
92 // pass a true $userold here
93 if (! $authplugin->user_update($user, $userform->get_data(false))) {
94 // auth update failed, rollback for moodle
95 update_record('user', addslashes_object($user));
96 error('Failed to update user data on external auth: '.$user->auth
.
97 '. See the server logs for more details.');
100 //set new password if specified
101 if (!empty($usernew->newpassword
)) {
102 if ($authplugin->can_change_password()) {
103 if (!$authplugin->user_update_password($usernew, $usernew->newpassword
)){
104 error('Failed to update password on external auth: ' . $usernew->auth
.
105 '. See the server logs for more details.');
111 events_trigger('user_updated', $usernew);
115 useredit_update_user_preference($usernew);
118 if (!empty($CFG->usetags
)) {
119 useredit_update_interests($usernew, $usernew->interests
);
122 //update user picture
123 if (!empty($CFG->gdversion
)) {
124 useredit_update_picture($usernew, $userform);
127 // update mail bounces
128 useredit_update_bounces($user, $usernew);
130 // update forum track preference
131 useredit_update_trackforums($user, $usernew);
133 // save custom profile fields data
134 profile_save_data($usernew);
136 if ($user->id
== $USER->id
) {
137 // Override old $USER session variable
138 $usernew = (array)get_record('user', 'id', $usernew->id
); // reload from db
139 foreach ($usernew as $variable => $value) {
140 $USER->$variable = $value;
142 if (!empty($USER->newadminuser
)) {
143 unset($USER->newadminuser
);
144 // redirect to admin/ to continue with installation
145 redirect("$CFG->wwwroot/$CFG->admin/");
147 redirect("$CFG->wwwroot/user/view.php?id=$USER->id&course=$course->id");
150 redirect("$CFG->wwwroot/$CFG->admin/user.php");
156 /// Display page header
157 if ($user->id
== -1 or ($user->id
!= $USER->id
)) {
158 if ($user->id
== -1) {
159 admin_externalpage_setup('addnewuser');
160 admin_externalpage_print_header();
162 admin_externalpage_setup('editusers');
163 admin_externalpage_print_header();
164 $userfullname = fullname($user, true);
165 print_heading($userfullname);
167 } else if (!empty($USER->newadminuser
)) {
168 $strprimaryadminsetup = get_string('primaryadminsetup');
169 print_header($strprimaryadminsetup, $strprimaryadminsetup);
170 print_simple_box(get_string('configintroadmin', 'admin'), 'center', '50%');
173 $streditmyprofile = get_string('editmyprofile');
174 $strparticipants = get_string('participants');
175 $strnewuser = get_string('newuser');
176 $userfullname = fullname($user, true);
177 if ($course->id
!= SITEID
) {
178 print_header("$course->shortname: $streditmyprofile", "$course->fullname: $streditmyprofile",
179 "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a>
180 -> <a href=\"index.php?id=$course->id\">$strparticipants</a>
181 -> <a href=\"view.php?id=$user->id&course=$course->id\">$userfullname</a>
182 -> $streditmyprofile", "");
184 print_header("$course->shortname: $streditmyprofile", $course->fullname
,
185 "<a href=\"view.php?id=$user->id&course=$course->id\">$userfullname</a>
186 -> $streditmyprofile", "");
188 /// Print tabs at the top
190 $currenttab = 'editprofile';
194 /// Finally display THE form
195 $userform->display();
197 /// and proper footer
198 if (!empty($USER->newadminuser
)) {
199 print_footer('none');
201 print_footer($course);