Cleanup of the admin interface - especially the user management
[moodle.git] / user / edit.php
CommitLineData
f9903ed0 1<?PHP // $Id$
2
3 require("../config.php");
bda8d43a 4 require("../lib/countries.php");
f9903ed0 5 require("lib.php");
6
7 require_variable($id); // user id
8 require_variable($course); // course id
9
10 if (! $user = get_record("user", "id", $id)) {
11 error("User ID was incorrect");
12 }
13
14 if (! $course = get_record("course", "id", $course)) {
15 error("User ID was incorrect");
16 }
17
18 require_login($course->id);
19
a3447e10 20 if ($USER->id <> $user->id and !isadmin()) {
f9903ed0 21 error("You can only edit your own information");
22 }
23
603d4c72 24 if (isguest()) {
25 error("The guest user cannot edit their profile.");
26 }
27
a3447e10 28 if (isguest($user->id)) {
29 error("Sorry, the guest user cannot be edited.");
30 }
31
f9903ed0 32
33/// If data submitted, then process and store.
34
35 if (match_referer() && isset($HTTP_POST_VARS)) {
36
37 $usernew = (object)$HTTP_POST_VARS;
38
a3447e10 39 $usernew->firstname = strip_tags($usernew->firstname);
40 $usernew->lastname = strip_tags($usernew->lastname);
41
42 if (find_form_errors($user, $usernew, $err) ) {
43 $user = $usernew;
44
45 } else {
f9903ed0 46
47 $timenow = time();
48
8223d271 49 if ($filename = valid_uploaded_file($imagefile)) {
50 $imageinfo = GetImageSize($filename);
f9903ed0 51 $image->width = $imageinfo[0];
52 $image->height = $imageinfo[1];
53 $image->type = $imageinfo[2];
54
55 switch ($image->type) {
8223d271 56 case 2: $im = ImageCreateFromJPEG($filename); break;
57 case 3: $im = ImageCreateFromPNG($filename); break;
f9903ed0 58 default: error("Image must be in JPG or PNG format");
59 }
22f4320b 60 if (function_exists("ImageCreateTrueColor") and $CFG->gdversion >= 2) {
f9903ed0 61 $im1 = ImageCreateTrueColor(100,100);
62 $im2 = ImageCreateTrueColor(35,35);
63 } else {
64 $im1 = ImageCreate(100,100);
65 $im2 = ImageCreate(35,35);
66 }
67
68 $cx = $image->width / 2;
69 $cy = $image->height / 2;
70
71 if ($image->width < $image->height) {
72 $half = floor($image->width / 2.0);
73 } else {
74 $half = floor($image->height / 2.0);
75 }
76
77 if (!file_exists("$CFG->dataroot/users")) {
54bdcdbe 78 if (! mkdir("$CFG->dataroot/users", 0777)) {
79 $badpermissions = true;
80 }
f9903ed0 81 }
a3447e10 82 if (!file_exists("$CFG->dataroot/users/$user->id")) {
83 if (! mkdir("$CFG->dataroot/users/$user->id", 0777)) {
54bdcdbe 84 $badpermissions = true;
85 }
f9903ed0 86 }
87
54bdcdbe 88 if ($badpermissions) {
89 $usernew->picture = "0";
90
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);
f9903ed0 94
54bdcdbe 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);
106
a3447e10 107 ImageJpeg($im1, "$CFG->dataroot/users/$user->id/f1.jpg", 90);
108 ImageJpeg($im2, "$CFG->dataroot/users/$user->id/f2.jpg", 95);
54bdcdbe 109 $usernew->picture = "1";
110 }
f9903ed0 111 } else {
112 $usernew->picture = $user->picture;
113 }
114
115 $usernew->timemodified = time();
116
a3447e10 117 if (isadmin()) {
118 if ($usernew->newpassword) {
119 $usernew->password = md5($usernew->newpassword);
120 }
121 } else {
122 if (isset($usernew->newpassword)) {
123 error("You can not change the password like that");
124 }
125 }
873960de 126
f9903ed0 127 if (update_record("user", $usernew)) {
253ae7db 128 add_to_log($course->id, "user", "update", "view.php?id=$user->id&course=$course->id", "");
873960de 129
a3447e10 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;
135 }
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");
873960de 140 }
f9903ed0 141 } else {
142 error("Could not update the user record ($user->id)");
143 }
144 }
145 }
146
147/// Otherwise fill and print the form.
148
8553b700 149 $editmyprofile = get_string("editmyprofile");
150 $participants = get_string("participants");
151
f9903ed0 152 if ($course->category) {
8553b700 153 print_header($editmyprofile, $editmyprofile,
f9903ed0 154 "<A HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</A>
8553b700 155 -> <A HREF=\"index.php?id=$course->id\">$participants</A>
a3447e10 156 -> <A HREF=\"view.php?id=$user->id&course=$course->id\">$user->firstname $user->lastname</A>
8553b700 157 -> $editmyprofile", "");
f9903ed0 158 } else {
8553b700 159 print_header($editmyprofile, $editmyprofile,
a3447e10 160 "<A HREF=\"view.php?id=$user->id&course=$course->id\">$user->firstname $user->lastname</A>
8553b700 161 -> $editmyprofile", "");
f9903ed0 162 }
163
bda8d43a 164 $teacher = strtolower($course->teacher);
a3447e10 165 if (!isadmin()) {
166 $teacheronly = "(".get_string("teacheronly", "", $teacher).")";
167 }
bda8d43a 168
f9903ed0 169 print_simple_box_start("center", "", "$THEME->cellheading");
8553b700 170 print_heading( get_string("userprofilefor", "", "$user->firstname $user->lastname") );
f9903ed0 171 include("edit.html");
172 print_simple_box_end();
f9903ed0 173 print_footer($course);
174
175
176
177
178/// FUNCTIONS ////////////////////
179
180function find_form_errors(&$user, &$usernew, &$err) {
181
a3447e10 182 if (isadmin()) {
183 if (empty($usernew->username))
184 $err["username"] = get_string("missingusername");
185
186 if (empty($usernew->newpassword) and empty($user->password))
187 $err["newpassword"] = get_string("missingpassword");
188 }
189
f9903ed0 190 if (empty($usernew->email))
8553b700 191 $err["email"] = get_string("missingemail");
f9903ed0 192
a3447e10 193 if (empty($usernew->description))
194 $err["description"] = get_string("missingdescription");
195
bda8d43a 196 if (empty($usernew->city))
8553b700 197 $err["city"] = get_string("missingcity");
bda8d43a 198
9c055aa5 199 if (empty($usernew->firstname))
200 $err["firstname"] = get_string("missingfirstname");
201
202 if (empty($usernew->lastname))
203 $err["lastname"] = get_string("missinglastname");
204
bda8d43a 205 if (empty($usernew->country))
8553b700 206 $err["country"] = get_string("missingcountry");
bda8d43a 207
a3447e10 208 if (! validate_email($usernew->email))
8553b700 209 $err["email"] = get_string("invalidemail");
f9903ed0 210
211 else if ($otheruser = get_record("user", "email", $usernew->email)) {
212 if ($otheruser->id <> $user->id) {
8553b700 213 $err["email"] = get_string("emailexists");
f9903ed0 214 }
215 }
216
217 $user->email = $usernew->email;
218
219 return count($err);
220}
221
222
223?>