3 require_once('../config.php');
4 require_once($CFG->libdir
.'/gdlib.php');
5 require_once($CFG->dirroot
.'/user/edit_form.php');
6 require_once($CFG->dirroot
.'/user/editlib.php');
7 require_once($CFG->dirroot
.'/user/profile/lib.php');
11 $userid = optional_param('id', $USER->id
, PARAM_INT
); // user id
12 $course = optional_param('course', SITEID
, PARAM_INT
); // course id (defaults to Site)
14 if (!$course = get_record('course', 'id', $course)) {
15 error('Course ID was incorrect');
18 if ($course->id
!= SITEID
) {
19 require_login($course);
20 } else if (!isloggedin()) {
21 if (empty($SESSION->wantsurl
)) {
22 $SESSION->wantsurl
= $CFG->httpswwwroot
.'/edit/user.php';
24 redirect($CFG->httpswwwroot
.'/login/index.php');
29 print_error('guestnoeditprofile');
32 // The user profile we are editing
33 if (!$user = get_record('user', 'id', $userid)) {
34 error('User ID was incorrect');
37 // Guest can not be edited
38 if (isguestuser($user)) {
39 print_error('guestnoeditprofile');
42 // User interests separated by commas
43 if (!empty($CFG->usetags
)) {
44 require_once($CFG->dirroot
.'/tag/lib.php');
45 $user->interests
= tag_names_csv(get_item_tags('user', $user->id
));
48 // remote users cannot be edited
49 if (is_mnet_remote_user($user)) {
50 redirect($CFG->wwwroot
. "/user/view.php?course={$course->id}");
53 $systemcontext = get_context_instance(CONTEXT_SYSTEM
);
54 $personalcontext = get_context_instance(CONTEXT_USER
, $user->id
);
56 // check access control
57 if ($user->id
== $USER->id
) {
59 require_capability('moodle/user:editownprofile', $systemcontext);
62 // teachers, parents, etc.
63 require_capability('moodle/user:editprofile', $personalcontext);
64 // no editing of guest user account
65 if (isguestuser($user->id
)) {
66 print_error('guestnoeditprofileother');
68 // no editing of primary admin!
69 $mainadmin = get_admin();
70 if ($user->id
== $mainadmin->id
) {
71 print_error('adminprimarynoedit');
75 //load user preferences
76 useredit_load_preferences($user);
78 //Load custom profile fields data
79 profile_load_data($user);
83 $userform = new user_edit_form();
84 $userform->set_data($user);
86 if ($usernew = $userform->get_data()) {
88 add_to_log($course->id
, 'user', 'update', "view.php?id=$user->id&course=$course->id", '');
90 $authplugin = get_auth_plugin($user->auth
);
92 $usernew->timemodified
= time();
94 if (!update_record('user', $usernew)) {
95 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.');
107 useredit_update_user_preference($usernew);
110 if (!empty($CFG->usetags
)) {
111 useredit_update_interests($usernew, $usernew->interests
);
114 //update user picture
115 if (!empty($CFG->gdversion
) and empty($CFG->disableuserimages
)) {
116 useredit_update_picture($usernew, $userform);
119 // update mail bounces
120 useredit_update_bounces($user, $usernew);
122 /// update forum track preference
123 useredit_update_trackforums($user, $usernew);
125 // save custom profile fields data
126 profile_save_data($usernew);
128 if ($USER->id
== $user->id
) {
129 // Override old $USER session variable if needed
130 $usernew = (array)get_record('user', 'id', $user->id
); // reload from db
131 foreach ($usernew as $variable => $value) {
132 $USER->$variable = $value;
135 events_trigger('user_updated', $usernew);
136 redirect("$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id");
140 /// Display page header
141 $streditmyprofile = get_string('editmyprofile');
142 $strparticipants = get_string('participants');
143 $userfullname = fullname($user, true);
146 $navlinks[] = array('name' => $strparticipants, 'link' => "index.php?id=$course->id", 'type' => 'misc');
147 $navlinks[] = array('name' => $userfullname,
148 'link' => "view.php?id=$user->id&course=$course->id",
150 $navlinks[] = array('name' => $streditmyprofile, 'link' => null, 'type' => 'misc');
151 $navigation = build_navigation($navlinks);
152 print_header("$course->shortname: $streditmyprofile", $course->fullname
, $navigation, "");
154 /// Print tabs at the top
156 $currenttab = 'editprofile';
159 /// Finally display THE form
160 $userform->display();
162 /// and proper footer
163 print_footer($course);