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
);
20 if ($course->id
== SITEID
) {
21 $coursecontext = get_context_instance(CONTEXT_SYSTEM
); // SYSTEM context
23 $coursecontext = get_context_instance(CONTEXT_COURSE
, $course->id
); // Course context
25 $systemcontext = get_context_instance(CONTEXT_SYSTEM
);
29 require_capability('moodle/user:create', $systemcontext);
32 $user->auth
= 'manual';
35 // editing existing user
36 require_capability('moodle/user:update', $systemcontext);
37 if (!$user = get_record('user', 'id', $id)) {
38 error('User ID was incorrect');
42 // remote users cannot be edited
43 if ($user->id
!= -1 and is_mnet_remote_user($user)) {
44 redirect($CFG->wwwroot
. "/user/view.php?id=$id&course={$course->id}");
47 if ($user->id
!= $USER->id
and is_primary_admin($user->id
)) { // Can't edit primary admin
48 print_error('adminprimarynoedit');
51 if (isguestuser($user->id
)) { // the real guest user can not be edited
52 print_error('guestnoeditprofileother');
55 //load user preferences
56 useredit_load_preferences($user);
58 //Load custom profile fields data
59 profile_load_data($user);
61 //user interests separated by commas
62 if (!empty($CFG->usetags
)) {
63 require_once($CFG->dirroot
.'/tag/lib.php');
64 $user->interests
= html_entity_decode(tag_get_tags_csv('user', $id, TAG_RETURN_TEXT
)); // formslib uses htmlentities itself
68 $userform = new user_editadvanced_form();
69 $userform->set_data($user);
71 if ($usernew = $userform->get_data()) {
72 add_to_log($course->id
, 'user', 'update', "view.php?id=$user->id&course=$course->id", '');
74 if (empty($usernew->auth
)) {
76 $authplugin = get_auth_plugin($user->auth
);
77 unset($usernew->auth
); //can not change/remove
79 $authplugin = get_auth_plugin($usernew->auth
);
82 $usernew->username
= trim($usernew->username
);
83 $usernew->timemodified
= time();
85 if ($usernew->id
== -1) {
86 //TODO check out if it makes sense to create account with this auth plugin and what to do with the password
88 $usernew->mnethostid
= $CFG->mnet_localhost_id
; // always local user
89 $usernew->confirmed
= 1;
90 $usernew->password
= hash_internal_user_password($usernew->newpassword
);
91 if (!$usernew->id
= insert_record('user', $usernew)) {
92 error('Error creating user record');
95 if (!update_record('user', $usernew)) {
96 error('Error updating user record');
98 // pass a true $userold here
99 if (! $authplugin->user_update($user, $userform->get_data(false))) {
100 // auth update failed, rollback for moodle
101 update_record('user', addslashes_object($user));
102 error('Failed to update user data on external auth: '.$user->auth
.
103 '. See the server logs for more details.');
106 //set new password if specified
107 if (!empty($usernew->newpassword
)) {
108 if ($authplugin->can_change_password()) {
109 if (!$authplugin->user_update_password($usernew, $usernew->newpassword
)){
110 error('Failed to update password on external auth: ' . $usernew->auth
.
111 '. See the server logs for more details.');
117 events_trigger('user_updated', $usernew);
121 useredit_update_user_preference($usernew);
124 if (!empty($CFG->usetags
)) {
125 useredit_update_interests($usernew, $usernew->interests
);
128 //update user picture
129 if (!empty($CFG->gdversion
)) {
130 useredit_update_picture($usernew, $userform);
133 // update mail bounces
134 useredit_update_bounces($user, $usernew);
136 // update forum track preference
137 useredit_update_trackforums($user, $usernew);
139 // save custom profile fields data
140 profile_save_data($usernew);
142 if ($user->id
== $USER->id
) {
143 // Override old $USER session variable
144 $usernew = (array)get_record('user', 'id', $usernew->id
); // reload from db
145 foreach ($usernew as $variable => $value) {
146 $USER->$variable = $value;
148 if (!empty($USER->newadminuser
)) {
149 unset($USER->newadminuser
);
150 // apply defaults again - some of them might depend on admin user info, backup, roles, etc.
151 admin_apply_default_settings(NULL , false);
152 // redirect to admin/ to continue with installation
153 redirect("$CFG->wwwroot/$CFG->admin/");
155 redirect("$CFG->wwwroot/user/view.php?id=$USER->id&course=$course->id");
158 redirect("$CFG->wwwroot/$CFG->admin/user.php");
164 /// Display page header
165 if ($user->id
== -1 or ($user->id
!= $USER->id
)) {
166 if ($user->id
== -1) {
167 admin_externalpage_setup('addnewuser');
168 admin_externalpage_print_header();
170 admin_externalpage_setup('editusers');
171 admin_externalpage_print_header();
172 $userfullname = fullname($user, true);
173 print_heading($userfullname);
175 } else if (!empty($USER->newadminuser
)) {
176 $strprimaryadminsetup = get_string('primaryadminsetup');
177 print_header($strprimaryadminsetup, $strprimaryadminsetup);
178 print_simple_box(get_string('configintroadmin', 'admin'), 'center', '50%');
181 $streditmyprofile = get_string('editmyprofile');
182 $strparticipants = get_string('participants');
183 $strnewuser = get_string('newuser');
184 $userfullname = fullname($user, true);
187 if (has_capability('moodle/course:viewparticipants', $coursecontext) ||
has_capability('moodle/site:viewparticipants', $systemcontext)) {
188 $navlinks[] = array('name' => $strparticipants, 'link' => "index.php?id=$course->id", 'type' => 'misc');
190 $navlinks[] = array('name' => $userfullname,
191 'link' => "view.php?id=$user->id&course=$course->id",
193 $navlinks[] = array('name' => $streditmyprofile, 'link' => null, 'type' => 'misc');
194 $navigation = build_navigation($navlinks);
195 print_header("$course->shortname: $streditmyprofile", $course->fullname
, $navigation, "");
197 /// Print tabs at the top
199 $currenttab = 'editprofile';
203 /// Finally display THE form
204 $userform->display();
206 /// and proper footer
207 if (!empty($USER->newadminuser
)) {
208 print_footer('none');
210 print_footer($course);