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');
27 $systemcontext = get_context_instance(CONTEXT_SYSTEM
);
28 $personalcontext = get_context_instance(CONTEXT_USER
, $user->id
);
31 print_error('guestnoeditprofile');
34 if (!$user = get_record('user', 'id', $userid)) {
35 error('User ID was incorrect');
38 //user interests separated by commas
39 if (!empty($CFG->usetags
)) {
40 require_once($CFG->dirroot
.'/tag/lib.php');
41 $user->interests
= tag_names_csv(get_item_tags('user',$userid));
44 // remote users cannot be edited
45 if (is_mnet_remote_user($user)) {
46 redirect($CFG->wwwroot
. "/user/view.php?course={$course->id}");
49 // check access control
50 if ($user->id
== $USER->id
) {
52 require_capability('moodle/user:editownprofile', $systemcontext);
55 // teachers, parents, etc.
56 require_capability('moodle/user:editprofile', $personalcontext);
57 // no editing of guest user account
58 if (isguestuser($user->id
)) {
59 print_error('guestnoeditprofileother');
61 // no editing of primary admin!
62 $mainadmin = get_admin();
63 if ($user->id
== $mainadmin->id
) {
64 print_error('adminprimarynoedit');
68 //load user preferences
69 useredit_load_preferences($user);
71 //Load custom profile fields data
72 profile_load_data($user);
76 $userform = new user_edit_form();
77 $userform->set_data($user);
79 if ($usernew = $userform->get_data()) {
81 add_to_log($course->id
, 'user', 'update', "view.php?id=$user->id&course=$course->id", '');
83 $authplugin = get_auth_plugin($user->auth
);
85 $usernew->timemodified
= time();
87 if (!update_record('user', $usernew)) {
88 error('Error updating user record');
91 // pass a true $userold here
92 if (! $authplugin->user_update($user, $userform->get_data(false))) {
93 // auth update failed, rollback for moodle
94 update_record('user', addslashes_object($user));
95 error('Failed to update user data on external auth: '.$user->auth
.
96 '. See the server logs for more details.');
100 useredit_update_user_preference($usernew);
103 if (!empty($CFG->usetags
)) {
104 useredit_update_interests($usernew, $usernew->interests
);
107 //update user picture
108 if (!empty($CFG->gdversion
) and empty($CFG->disableuserimages
)) {
109 useredit_update_picture($usernew, $userform);
112 // update mail bounces
113 useredit_update_bounces($user, $usernew);
115 /// update forum track preference
116 useredit_update_trackforums($user, $usernew);
118 // save custom profile fields data
119 profile_save_data($usernew);
121 if ($USER->id
== $user->id
) {
122 // Override old $USER session variable if needed
123 $usernew = (array)get_record('user', 'id', $user->id
); // reload from db
124 foreach ($usernew as $variable => $value) {
125 $USER->$variable = $value;
128 events_trigger('user_updated', $usernew);
129 redirect("$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id");
133 /// Display page header
134 $streditmyprofile = get_string('editmyprofile');
135 $strparticipants = get_string('participants');
136 $userfullname = fullname($user, true);
137 if ($course->id
!= SITEID
) {
138 print_header("$course->shortname: $streditmyprofile", "$course->fullname: $streditmyprofile",
139 "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a>
140 -> <a href=\"index.php?id=$course->id\">$strparticipants</a>
141 -> <a href=\"view.php?id=$user->id&course=$course->id\">$userfullname</a>
142 -> $streditmyprofile", "");
144 print_header("$course->shortname: $streditmyprofile", $course->fullname
,
145 "<a href=\"view.php?id=$user->id&course=$course->id\">$userfullname</a>
146 -> $streditmyprofile", "");
148 /// Print tabs at the top
150 $currenttab = 'editprofile';
153 /// Finally display THE form
154 $userform->display();
156 /// and proper footer
157 print_footer($course);