3 require_once("../config.php");
4 require_once("lib.php");
6 require_variable($id); // user id
7 require_variable($course); // course id
9 if (! $user = get_record("user", "id", $id)) {
10 error("User ID was incorrect");
13 if (! $course = get_record("course", "id", $course)) {
14 error("Course ID was incorrect");
17 if ($user->confirmed
and user_not_fully_set_up($user)) {
18 // Special case which can only occur when a new account
19 // has just been created by EXTERNAL authentication
20 // This is the only page in Moodle that has the exception
21 // so that users can set up their accounts
26 require_login($course->id
);
29 if ($USER->id
<> $user->id
and !isadmin()) {
30 error("You can only edit your own information");
34 error("The guest user cannot edit their profile.");
37 if (isguest($user->id
)) {
38 error("Sorry, the guest user cannot be edited.");
42 /// If data submitted, then process and store.
44 if ($usernew = data_submitted()) {
45 $usernew->firstname
= strip_tags($usernew->firstname
);
46 $usernew->lastname
= strip_tags($usernew->lastname
);
47 if (isset($usernew->username
)) {
48 $usernew->username
= trim(moodle_strtolower($usernew->username
));
51 if (empty($_FILES['imagefile'])) {
52 $_FILES['imagefile'] = NULL; // To avoid using uninitialised variable later
55 if (find_form_errors($user, $usernew, $err)) {
56 if ($filename = valid_uploaded_file($_FILES['imagefile'])) {
57 $usernew->picture
= save_user_image($user->id
, $filename);
65 if ($filename = valid_uploaded_file($_FILES['imagefile'])) {
66 $usernew->picture
= save_user_image($user->id
, $filename);
68 $usernew->picture
= $user->picture
;
71 $usernew->timemodified
= time();
74 if (!empty($usernew->newpassword
)) {
75 $usernew->password
= md5($usernew->newpassword
);
78 if (isset($usernew->newpassword
)) {
79 error("You can not change the password like that");
82 if ($usernew->url
and !(substr($usernew->url
, 0, 4) == "http")) {
83 $usernew->url
= "http://".$usernew->url
;
86 if (update_record("user", $usernew)) {
87 add_to_log($course->id
, "user", "update", "view.php?id=$user->id&course=$course->id", "");
89 if ($user->id
== $USER->id
) {
90 // Copy data into $USER session variable
91 $usernew = (array)$usernew;
92 foreach ($usernew as $variable => $value) {
93 $USER->$variable = stripslashes($value);
95 if (isset($USER->newadminuser
)) {
96 unset($USER->newadminuser
);
97 redirect("$CFG->wwwroot/", get_string("changessaved"));
99 redirect("$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id", get_string("changessaved"));
101 redirect("$CFG->wwwroot/admin/user.php", get_string("changessaved"));
104 error("Could not update the user record ($user->id)");
109 /// Otherwise fill and print the form.
111 $streditmyprofile = get_string("editmyprofile");
112 $strparticipants = get_string("participants");
113 $strnewuser = get_string("newuser");
115 if (($user->firstname
and $user->lastname
) or $newaccount) {
117 $userfullname = $strnewuser;
119 $userfullname = "$user->firstname $user->lastname";
121 if ($course->category
) {
122 print_header("$course->shortname: $streditmyprofile", "$course->fullname: $streditmyprofile",
123 "<A HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</A>
124 -> <A HREF=\"index.php?id=$course->id\">$strparticipants</A>
125 -> <A HREF=\"view.php?id=$user->id&course=$course->id\">$userfullname</A>
126 -> $streditmyprofile", "");
128 if (isset($USER->newadminuser
)) {
131 print_header("$course->shortname: $streditmyprofile", "$course->fullname",
132 "<A HREF=\"view.php?id=$user->id&course=$course->id\">$userfullname</A>
133 -> $streditmyprofile", "");
137 $userfullname = $strnewuser;
138 $straddnewuser = get_string("addnewuser");
140 $stradministration = get_string("administration");
141 print_header("$course->shortname: $streditmyprofile", "$course->fullname",
142 "<a href=\"$CFG->wwwroot/$CFG->admin/\">$stradministration</a> -> ".
143 "<a href=\"$CFG->wwwroot/$CFG->admin/users.php\">$strusers</a> -> $straddnewuser", "");
146 $teacher = strtolower($course->teacher
);
148 $teacheronly = "(".get_string("teacheronly", "", $teacher).")";
153 print_heading( get_string("userprofilefor", "", "$userfullname") );
155 if (isset($USER->newadminuser
)) {
156 print_simple_box(get_string("configintroadmin"), "center", "50%");
160 print_simple_box_start("center", "", "$THEME->cellheading");
163 notify(get_string("someerrorswerefound"));
166 include("edit.html");
167 print_simple_box_end();
169 if (!isset($USER->newadminuser
)) {
170 print_footer($course);
177 /// FUNCTIONS ////////////////////
179 function find_form_errors(&$user, &$usernew, &$err) {
182 if (empty($usernew->username
)) {
183 $err["username"] = get_string("missingusername");
185 } else if (record_exists("user", "username", $usernew->username
) and $user->username
== "changeme") {
186 $err["username"] = get_string("usernameexists");
189 $string = eregi_replace("[^(-\.[:alnum:])]", "", $usernew->username
);
190 if (strcmp($usernew->username
, $string))
191 $err["username"] = get_string("alphanumerical");
194 if (empty($usernew->newpassword
) and empty($user->password
) and is_internal_auth() )
195 $err["newpassword"] = get_string("missingpassword");
197 if (($usernew->newpassword
== "admin") or ($user->password
== md5("admin") and empty($usernew->newpassword
)) ) {
198 $err["newpassword"] = get_string("unsafepassword");
202 if (empty($usernew->email
))
203 $err["email"] = get_string("missingemail");
205 if (empty($usernew->description
))
206 $err["description"] = get_string("missingdescription");
208 if (empty($usernew->city
))
209 $err["city"] = get_string("missingcity");
211 if (empty($usernew->firstname
))
212 $err["firstname"] = get_string("missingfirstname");
214 if (empty($usernew->lastname
))
215 $err["lastname"] = get_string("missinglastname");
217 if (empty($usernew->country
))
218 $err["country"] = get_string("missingcountry");
220 if (! validate_email($usernew->email
))
221 $err["email"] = get_string("invalidemail");
223 else if ($otheruser = get_record("user", "email", $usernew->email
)) {
224 if ($otheruser->id
<> $user->id
) {
225 $err["email"] = get_string("emailexists");
229 $user->email
= $usernew->email
;