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
.'/user/edit.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_get_tags_csv('user', $user->id
, TAG_RETURN_TEXT
);
48 // remote users cannot be edited
49 if (is_mnet_remote_user($user)) {
50 redirect($CFG->wwwroot
. "/user/view.php?course={$course->id}");
53 if ($course->id
== SITEID
) {
54 $coursecontext = get_context_instance(CONTEXT_SYSTEM
); // SYSTEM context
56 $coursecontext = get_context_instance(CONTEXT_COURSE
, $course->id
); // Course context
58 $systemcontext = get_context_instance(CONTEXT_SYSTEM
);
59 $personalcontext = get_context_instance(CONTEXT_USER
, $user->id
);
61 // check access control
62 if ($user->id
== $USER->id
) {
63 //editing own profile - require_login() MUST NOT be used here, it would result in infinite loop!
64 if (!has_capability('moodle/user:editownprofile', $systemcontext)) {
65 error('Can not edit own profile, sorry.');
69 // teachers, parents, etc.
70 require_capability('moodle/user:editprofile', $personalcontext);
71 // no editing of guest user account
72 if (isguestuser($user->id
)) {
73 print_error('guestnoeditprofileother');
75 // no editing of primary admin!
76 if (is_primary_admin($user->id
)) {
77 print_error('adminprimarynoedit');
81 //load user preferences
82 useredit_load_preferences($user);
84 //Load custom profile fields data
85 profile_load_data($user);
89 $userform = new user_edit_form();
90 $userform->set_data($user);
92 if ($usernew = $userform->get_data()) {
94 add_to_log($course->id
, 'user', 'update', "view.php?id=$user->id&course=$course->id", '');
96 $authplugin = get_auth_plugin($user->auth
);
98 $usernew->timemodified
= time();
100 if (!update_record('user', $usernew)) {
101 error('Error updating user record');
104 // pass a true $userold here
105 if (! $authplugin->user_update($user, $userform->get_data(false))) {
106 // auth update failed, rollback for moodle
107 update_record('user', addslashes_object($user));
108 error('Failed to update user data on external auth: '.$user->auth
.
109 '. See the server logs for more details.');
113 useredit_update_user_preference($usernew);
116 if (!empty($CFG->usetags
)) {
117 useredit_update_interests($usernew, $usernew->interests
);
120 //update user picture
121 if (!empty($CFG->gdversion
) and empty($CFG->disableuserimages
)) {
122 useredit_update_picture($usernew, $userform);
125 // update mail bounces
126 useredit_update_bounces($user, $usernew);
128 /// update forum track preference
129 useredit_update_trackforums($user, $usernew);
131 // save custom profile fields data
132 profile_save_data($usernew);
134 if ($USER->id
== $user->id
) {
135 // Override old $USER session variable if needed
136 $usernew = (array)get_record('user', 'id', $user->id
); // reload from db
137 foreach ($usernew as $variable => $value) {
138 $USER->$variable = $value;
141 events_trigger('user_updated', $usernew);
142 redirect("$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id");
146 /// Display page header
147 $streditmyprofile = get_string('editmyprofile');
148 $strparticipants = get_string('participants');
149 $userfullname = fullname($user, true);
152 if (has_capability('moodle/course:viewparticipants', $coursecontext) ||
has_capability('moodle/site:viewparticipants', $systemcontext)) {
153 $navlinks[] = array('name' => $strparticipants, 'link' => "index.php?id=$course->id", 'type' => 'misc');
155 $navlinks[] = array('name' => $userfullname,
156 'link' => "view.php?id=$user->id&course=$course->id",
158 $navlinks[] = array('name' => $streditmyprofile, 'link' => null, 'type' => 'misc');
159 $navigation = build_navigation($navlinks);
160 print_header("$course->shortname: $streditmyprofile", $course->fullname
, $navigation, "");
162 /// Print tabs at the top
164 $currenttab = 'editprofile';
167 /// Finally display THE form
168 $userform->display();
170 /// and proper footer
171 print_footer($course);