Fixed tiny buglet on insert_record which made it ignore NULL values
[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
20 if ($USER->id <> $user->id) {
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
f9903ed0 28
29/// If data submitted, then process and store.
30
31 if (match_referer() && isset($HTTP_POST_VARS)) {
32
33 $usernew = (object)$HTTP_POST_VARS;
34
35 if (!find_form_errors($user, $usernew, $err) ) {
36
37 $timenow = time();
38
8223d271 39 if ($filename = valid_uploaded_file($imagefile)) {
40 $imageinfo = GetImageSize($filename);
f9903ed0 41 $image->width = $imageinfo[0];
42 $image->height = $imageinfo[1];
43 $image->type = $imageinfo[2];
44
45 switch ($image->type) {
8223d271 46 case 2: $im = ImageCreateFromJPEG($filename); break;
47 case 3: $im = ImageCreateFromPNG($filename); break;
f9903ed0 48 default: error("Image must be in JPG or PNG format");
49 }
22f4320b 50 if (function_exists("ImageCreateTrueColor") and $CFG->gdversion >= 2) {
f9903ed0 51 $im1 = ImageCreateTrueColor(100,100);
52 $im2 = ImageCreateTrueColor(35,35);
53 } else {
54 $im1 = ImageCreate(100,100);
55 $im2 = ImageCreate(35,35);
56 }
57
58 $cx = $image->width / 2;
59 $cy = $image->height / 2;
60
61 if ($image->width < $image->height) {
62 $half = floor($image->width / 2.0);
63 } else {
64 $half = floor($image->height / 2.0);
65 }
66
67 if (!file_exists("$CFG->dataroot/users")) {
54bdcdbe 68 if (! mkdir("$CFG->dataroot/users", 0777)) {
69 $badpermissions = true;
70 }
f9903ed0 71 }
72 if (!file_exists("$CFG->dataroot/users/$USER->id")) {
54bdcdbe 73 if (! mkdir("$CFG->dataroot/users/$USER->id", 0777)) {
74 $badpermissions = true;
75 }
f9903ed0 76 }
77
54bdcdbe 78 if ($badpermissions) {
79 $usernew->picture = "0";
80
81 } else {
82 ImageCopyBicubic($im1, $im, 0, 0, $cx-$half, $cy-$half, 100, 100, $half*2, $half*2);
83 ImageCopyBicubic($im2, $im, 0, 0, $cx-$half, $cy-$half, 35, 35, $half*2, $half*2);
f9903ed0 84
54bdcdbe 85 // Draw borders over the top.
86 $black1 = ImageColorAllocate ($im1, 0, 0, 0);
87 $black2 = ImageColorAllocate ($im2, 0, 0, 0);
88 ImageLine ($im1, 0, 0, 0, 99, $black1);
89 ImageLine ($im1, 0, 99, 99, 99, $black1);
90 ImageLine ($im1, 99, 99, 99, 0, $black1);
91 ImageLine ($im1, 99, 0, 0, 0, $black1);
92 ImageLine ($im2, 0, 0, 0, 34, $black2);
93 ImageLine ($im2, 0, 34, 34, 34, $black2);
94 ImageLine ($im2, 34, 34, 34, 0, $black2);
95 ImageLine ($im2, 34, 0, 0, 0, $black2);
96
97 ImageJpeg($im1, "$CFG->dataroot/users/$USER->id/f1.jpg", 90);
98 ImageJpeg($im2, "$CFG->dataroot/users/$USER->id/f2.jpg", 95);
99 $usernew->picture = "1";
100 }
f9903ed0 101 } else {
102 $usernew->picture = $user->picture;
103 }
104
105 $usernew->timemodified = time();
106
873960de 107
108
f9903ed0 109 if (update_record("user", $usernew)) {
253ae7db 110 add_to_log($course->id, "user", "update", "view.php?id=$user->id&course=$course->id", "");
873960de 111
112 // Copy data into $USER session variable
113 $usernew = (array)$usernew;
114 foreach ($usernew as $variable => $value) {
115 $USER->$variable = $value;
116 }
8223d271 117 save_session("USER");
f9903ed0 118 redirect("view.php?id=$user->id&course=$course->id", "Changes saved");
119 } else {
120 error("Could not update the user record ($user->id)");
121 }
122 }
123 }
124
125/// Otherwise fill and print the form.
126
8553b700 127 $editmyprofile = get_string("editmyprofile");
128 $participants = get_string("participants");
129
f9903ed0 130 if ($course->category) {
8553b700 131 print_header($editmyprofile, $editmyprofile,
f9903ed0 132 "<A HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</A>
8553b700 133 -> <A HREF=\"index.php?id=$course->id\">$participants</A>
f9903ed0 134 -> <A HREF=\"view.php?id=$USER->id&course=$course->id\">$USER->firstname $USER->lastname</A>
8553b700 135 -> $editmyprofile", "");
f9903ed0 136 } else {
8553b700 137 print_header($editmyprofile, $editmyprofile,
f9903ed0 138 "<A HREF=\"view.php?id=$USER->id&course=$course->id\">$USER->firstname $USER->lastname</A>
8553b700 139 -> $editmyprofile", "");
f9903ed0 140 }
141
bda8d43a 142 $teacher = strtolower($course->teacher);
fa0626c6 143 $teacheronly = "(".get_string("teacheronly", "", $teacher).")";
bda8d43a 144
f9903ed0 145 print_simple_box_start("center", "", "$THEME->cellheading");
8553b700 146 print_heading( get_string("userprofilefor", "", "$user->firstname $user->lastname") );
f9903ed0 147 include("edit.html");
148 print_simple_box_end();
f9903ed0 149 print_footer($course);
150
151
152
153
154/// FUNCTIONS ////////////////////
155
156function find_form_errors(&$user, &$usernew, &$err) {
157
158 if (empty($usernew->email))
8553b700 159 $err["email"] = get_string("missingemail");
f9903ed0 160
bda8d43a 161 if (empty($usernew->city))
8553b700 162 $err["city"] = get_string("missingcity");
bda8d43a 163
9c055aa5 164 if (empty($usernew->firstname))
165 $err["firstname"] = get_string("missingfirstname");
166
167 if (empty($usernew->lastname))
168 $err["lastname"] = get_string("missinglastname");
169
bda8d43a 170 if (empty($usernew->country))
8553b700 171 $err["country"] = get_string("missingcountry");
bda8d43a 172
f9903ed0 173 else if (! validate_email($usernew->email))
8553b700 174 $err["email"] = get_string("invalidemail");
f9903ed0 175
176 else if ($otheruser = get_record("user", "email", $usernew->email)) {
177 if ($otheruser->id <> $user->id) {
8553b700 178 $err["email"] = get_string("emailexists");
f9903ed0 179 }
180 }
181
182 $user->email = $usernew->email;
183
184 return count($err);
185}
186
187
188?>