Better looking search boxes
[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
39 if ($imagefile && $imagefile!="none") {
40 $imageinfo = GetImageSize($imagefile);
41 $image->width = $imageinfo[0];
42 $image->height = $imageinfo[1];
43 $image->type = $imageinfo[2];
44
45 switch ($image->type) {
46 case 2: $im = ImageCreateFromJPEG($imagefile); break;
47 case 3: $im = ImageCreateFromPNG($imagefile); break;
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 }
f9903ed0 117 redirect("view.php?id=$user->id&course=$course->id", "Changes saved");
118 } else {
119 error("Could not update the user record ($user->id)");
120 }
121 }
122 }
123
124/// Otherwise fill and print the form.
125
8553b700 126 $editmyprofile = get_string("editmyprofile");
127 $participants = get_string("participants");
128
f9903ed0 129 if ($course->category) {
8553b700 130 print_header($editmyprofile, $editmyprofile,
f9903ed0 131 "<A HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</A>
8553b700 132 -> <A HREF=\"index.php?id=$course->id\">$participants</A>
f9903ed0 133 -> <A HREF=\"view.php?id=$USER->id&course=$course->id\">$USER->firstname $USER->lastname</A>
8553b700 134 -> $editmyprofile", "");
f9903ed0 135 } else {
8553b700 136 print_header($editmyprofile, $editmyprofile,
f9903ed0 137 "<A HREF=\"view.php?id=$USER->id&course=$course->id\">$USER->firstname $USER->lastname</A>
8553b700 138 -> $editmyprofile", "");
f9903ed0 139 }
140
bda8d43a 141 $teacher = strtolower($course->teacher);
fa0626c6 142 $teacheronly = "(".get_string("teacheronly", "", $teacher).")";
bda8d43a 143
f9903ed0 144 print_simple_box_start("center", "", "$THEME->cellheading");
8553b700 145 print_heading( get_string("userprofilefor", "", "$user->firstname $user->lastname") );
f9903ed0 146 include("edit.html");
147 print_simple_box_end();
f9903ed0 148 print_footer($course);
149
150
151
152
153/// FUNCTIONS ////////////////////
154
155function find_form_errors(&$user, &$usernew, &$err) {
156
157 if (empty($usernew->email))
8553b700 158 $err["email"] = get_string("missingemail");
f9903ed0 159
bda8d43a 160 if (empty($usernew->city))
8553b700 161 $err["city"] = get_string("missingcity");
bda8d43a 162
163 if (empty($usernew->country))
8553b700 164 $err["country"] = get_string("missingcountry");
bda8d43a 165
f9903ed0 166 else if (! validate_email($usernew->email))
8553b700 167 $err["email"] = get_string("invalidemail");
f9903ed0 168
169 else if ($otheruser = get_record("user", "email", $usernew->email)) {
170 if ($otheruser->id <> $user->id) {
8553b700 171 $err["email"] = get_string("emailexists");
f9903ed0 172 }
173 }
174
175 $user->email = $usernew->email;
176
177 return count($err);
178}
179
180
181?>