Tidied up headers of user/edit.php
[moodle.git] / user / edit.php
blob3de6d84516e8926a6677d1c54e90dfac85f7fad6
1 <?PHP // $Id$
3 require("../config.php");
4 require("../lib/countries.php");
5 require("lib.php");
7 require_variable($id); // user id
8 require_variable($course); // course id
10 if (! $user = get_record("user", "id", $id)) {
11 error("User ID was incorrect");
14 if (! $course = get_record("course", "id", $course)) {
15 error("Course ID was incorrect");
18 require_login($course->id);
20 if ($USER->id <> $user->id and !isadmin()) {
21 error("You can only edit your own information");
24 if (isguest()) {
25 error("The guest user cannot edit their profile.");
28 if (isguest($user->id)) {
29 error("Sorry, the guest user cannot be edited.");
33 /// If data submitted, then process and store.
35 if (match_referer() && isset($HTTP_POST_VARS)) {
37 $usernew = (object)$HTTP_POST_VARS;
39 $usernew->firstname = strip_tags($usernew->firstname);
40 $usernew->lastname = strip_tags($usernew->lastname);
42 if (find_form_errors($user, $usernew, $err) ) {
43 $user = $usernew;
45 } else {
47 $timenow = time();
49 if ($filename = valid_uploaded_file($imagefile)) {
50 $imageinfo = GetImageSize($filename);
51 $image->width = $imageinfo[0];
52 $image->height = $imageinfo[1];
53 $image->type = $imageinfo[2];
55 switch ($image->type) {
56 case 2: $im = ImageCreateFromJPEG($filename); break;
57 case 3: $im = ImageCreateFromPNG($filename); break;
58 default: error("Image must be in JPG or PNG format");
60 if (function_exists("ImageCreateTrueColor") and $CFG->gdversion >= 2) {
61 $im1 = ImageCreateTrueColor(100,100);
62 $im2 = ImageCreateTrueColor(35,35);
63 } else {
64 $im1 = ImageCreate(100,100);
65 $im2 = ImageCreate(35,35);
68 $cx = $image->width / 2;
69 $cy = $image->height / 2;
71 if ($image->width < $image->height) {
72 $half = floor($image->width / 2.0);
73 } else {
74 $half = floor($image->height / 2.0);
77 if (!file_exists("$CFG->dataroot/users")) {
78 if (! mkdir("$CFG->dataroot/users", 0777)) {
79 $badpermissions = true;
82 if (!file_exists("$CFG->dataroot/users/$user->id")) {
83 if (! mkdir("$CFG->dataroot/users/$user->id", 0777)) {
84 $badpermissions = true;
88 if ($badpermissions) {
89 $usernew->picture = "0";
91 } else {
92 ImageCopyBicubic($im1, $im, 0, 0, $cx-$half, $cy-$half, 100, 100, $half*2, $half*2);
93 ImageCopyBicubic($im2, $im, 0, 0, $cx-$half, $cy-$half, 35, 35, $half*2, $half*2);
95 // Draw borders over the top.
96 $black1 = ImageColorAllocate ($im1, 0, 0, 0);
97 $black2 = ImageColorAllocate ($im2, 0, 0, 0);
98 ImageLine ($im1, 0, 0, 0, 99, $black1);
99 ImageLine ($im1, 0, 99, 99, 99, $black1);
100 ImageLine ($im1, 99, 99, 99, 0, $black1);
101 ImageLine ($im1, 99, 0, 0, 0, $black1);
102 ImageLine ($im2, 0, 0, 0, 34, $black2);
103 ImageLine ($im2, 0, 34, 34, 34, $black2);
104 ImageLine ($im2, 34, 34, 34, 0, $black2);
105 ImageLine ($im2, 34, 0, 0, 0, $black2);
107 ImageJpeg($im1, "$CFG->dataroot/users/$user->id/f1.jpg", 90);
108 ImageJpeg($im2, "$CFG->dataroot/users/$user->id/f2.jpg", 95);
109 $usernew->picture = "1";
111 } else {
112 $usernew->picture = $user->picture;
115 $usernew->timemodified = time();
117 if (isadmin()) {
118 if ($usernew->newpassword) {
119 $usernew->password = md5($usernew->newpassword);
121 } else {
122 if (isset($usernew->newpassword)) {
123 error("You can not change the password like that");
127 if (update_record("user", $usernew)) {
128 add_to_log($course->id, "user", "update", "view.php?id=$user->id&course=$course->id", "");
130 if ($user->id == $USER->id) {
131 // Copy data into $USER session variable
132 $usernew = (array)$usernew;
133 foreach ($usernew as $variable => $value) {
134 $USER->$variable = $value;
136 save_session("USER");
137 redirect("view.php?id=$user->id&course=$course->id", "Changes saved");
138 } else {
139 redirect("../admin/user.php", "Changes saved");
141 } else {
142 error("Could not update the user record ($user->id)");
147 /// Otherwise fill and print the form.
149 $editmyprofile = get_string("editmyprofile");
150 $participants = get_string("participants");
152 if ($user->firstname and $user->lastname) {
153 $userfullname = "$user->firstname $user->lastname";
154 if ($course->category) {
155 print_header("$course->fullname: $editmyprofile", "$course->fullname: $editmyprofile",
156 "<A HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</A>
157 -> <A HREF=\"index.php?id=$course->id\">$participants</A>
158 -> <A HREF=\"view.php?id=$user->id&course=$course->id\">$userfullname</A>
159 -> $editmyprofile", "");
160 } else {
161 print_header("$course->fullname: $editmyprofile", "$course->fullname",
162 "<A HREF=\"view.php?id=$user->id&course=$course->id\">$userfullname</A>
163 -> $editmyprofile", "");
165 } else {
166 $userfullname = get_string("newuser");
167 $straddnewuser = get_string("addnewuser");
169 $stradministration = get_string("administration");
170 print_header("$course->fullname: $editmyprofile", "$course->fullname",
171 "<A HREF=\"$CFG->wwwroot/admin\">$stradministration</A> ->
172 $straddnewuser", "");
175 $teacher = strtolower($course->teacher);
176 if (!isadmin()) {
177 $teacheronly = "(".get_string("teacheronly", "", $teacher).")";
180 print_simple_box_start("center", "", "$THEME->cellheading");
181 print_heading( get_string("userprofilefor", "", "$userfullname") );
182 include("edit.html");
183 print_simple_box_end();
184 print_footer($course);
189 /// FUNCTIONS ////////////////////
191 function find_form_errors(&$user, &$usernew, &$err) {
193 if (isadmin()) {
194 if (empty($usernew->username)) {
195 $err["username"] = get_string("missingusername");
197 } else if (record_exists("user", "username", $usernew->username) and $user->username == "changeme") {
198 $err["username"] = get_string("usernameexists");
200 } else {
201 $string = eregi_replace("[^([:alnum:])]", "", $user->username);
202 if (strcmp($user->username, $string))
203 $err["username"] = get_string("alphanumerical");
206 if (empty($usernew->newpassword) and empty($user->password))
207 $err["newpassword"] = get_string("missingpassword");
210 if (empty($usernew->email))
211 $err["email"] = get_string("missingemail");
213 if (empty($usernew->description))
214 $err["description"] = get_string("missingdescription");
216 if (empty($usernew->city))
217 $err["city"] = get_string("missingcity");
219 if (empty($usernew->firstname))
220 $err["firstname"] = get_string("missingfirstname");
222 if (empty($usernew->lastname))
223 $err["lastname"] = get_string("missinglastname");
225 if (empty($usernew->country))
226 $err["country"] = get_string("missingcountry");
228 if (! validate_email($usernew->email))
229 $err["email"] = get_string("invalidemail");
231 else if ($otheruser = get_record("user", "email", $usernew->email)) {
232 if ($otheruser->id <> $user->id) {
233 $err["email"] = get_string("emailexists");
237 $user->email = $usernew->email;
239 return count($err);