Moved creation of guest user to login page - this means it can be
[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
24
25/// If data submitted, then process and store.
26
27 if (match_referer() && isset($HTTP_POST_VARS)) {
28
29 $usernew = (object)$HTTP_POST_VARS;
30
31 if (!find_form_errors($user, $usernew, $err) ) {
32
33 $timenow = time();
34
35 if ($imagefile && $imagefile!="none") {
36 $imageinfo = GetImageSize($imagefile);
37 $image->width = $imageinfo[0];
38 $image->height = $imageinfo[1];
39 $image->type = $imageinfo[2];
40
41 switch ($image->type) {
42 case 2: $im = ImageCreateFromJPEG($imagefile); break;
43 case 3: $im = ImageCreateFromPNG($imagefile); break;
44 default: error("Image must be in JPG or PNG format");
45 }
46 if (function_exists("ImageCreateTrueColor")) {
47 $im1 = ImageCreateTrueColor(100,100);
48 $im2 = ImageCreateTrueColor(35,35);
49 } else {
50 $im1 = ImageCreate(100,100);
51 $im2 = ImageCreate(35,35);
52 }
53
54 $cx = $image->width / 2;
55 $cy = $image->height / 2;
56
57 if ($image->width < $image->height) {
58 $half = floor($image->width / 2.0);
59 } else {
60 $half = floor($image->height / 2.0);
61 }
62
63 if (!file_exists("$CFG->dataroot/users")) {
54bdcdbe 64 if (! mkdir("$CFG->dataroot/users", 0777)) {
65 $badpermissions = true;
66 }
f9903ed0 67 }
68 if (!file_exists("$CFG->dataroot/users/$USER->id")) {
54bdcdbe 69 if (! mkdir("$CFG->dataroot/users/$USER->id", 0777)) {
70 $badpermissions = true;
71 }
f9903ed0 72 }
73
54bdcdbe 74 if ($badpermissions) {
75 $usernew->picture = "0";
76
77 } else {
78 ImageCopyBicubic($im1, $im, 0, 0, $cx-$half, $cy-$half, 100, 100, $half*2, $half*2);
79 ImageCopyBicubic($im2, $im, 0, 0, $cx-$half, $cy-$half, 35, 35, $half*2, $half*2);
f9903ed0 80
54bdcdbe 81 // Draw borders over the top.
82 $black1 = ImageColorAllocate ($im1, 0, 0, 0);
83 $black2 = ImageColorAllocate ($im2, 0, 0, 0);
84 ImageLine ($im1, 0, 0, 0, 99, $black1);
85 ImageLine ($im1, 0, 99, 99, 99, $black1);
86 ImageLine ($im1, 99, 99, 99, 0, $black1);
87 ImageLine ($im1, 99, 0, 0, 0, $black1);
88 ImageLine ($im2, 0, 0, 0, 34, $black2);
89 ImageLine ($im2, 0, 34, 34, 34, $black2);
90 ImageLine ($im2, 34, 34, 34, 0, $black2);
91 ImageLine ($im2, 34, 0, 0, 0, $black2);
92
93 ImageJpeg($im1, "$CFG->dataroot/users/$USER->id/f1.jpg", 90);
94 ImageJpeg($im2, "$CFG->dataroot/users/$USER->id/f2.jpg", 95);
95 $usernew->picture = "1";
96 }
f9903ed0 97 } else {
98 $usernew->picture = $user->picture;
99 }
100
101 $usernew->timemodified = time();
102
873960de 103
104
f9903ed0 105 if (update_record("user", $usernew)) {
253ae7db 106 add_to_log($course->id, "user", "update", "view.php?id=$user->id&course=$course->id", "");
873960de 107
108 // Copy data into $USER session variable
109 $usernew = (array)$usernew;
110 foreach ($usernew as $variable => $value) {
111 $USER->$variable = $value;
112 }
f9903ed0 113 redirect("view.php?id=$user->id&course=$course->id", "Changes saved");
114 } else {
115 error("Could not update the user record ($user->id)");
116 }
117 }
118 }
119
120/// Otherwise fill and print the form.
121
122 if ($course->category) {
123 print_header("Edit user profile", "Edit user profile",
124 "<A HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</A>
125 -> <A HREF=\"index.php?id=$course->id\">Participants</A>
126 -> <A HREF=\"view.php?id=$USER->id&course=$course->id\">$USER->firstname $USER->lastname</A>
127 -> Edit profile", "");
128 } else {
129 print_header("Edit user profile", "Edit user profile",
130 "<A HREF=\"view.php?id=$USER->id&course=$course->id\">$USER->firstname $USER->lastname</A>
131 -> Edit profile", "");
132 }
133
bda8d43a 134 $teacher = strtolower($course->teacher);
135
f9903ed0 136 print_simple_box_start("center", "", "$THEME->cellheading");
137 echo "<H2>User profile for $user->firstname $user->lastname</H2>";
138 include("edit.html");
139 print_simple_box_end();
140
141 print_footer($course);
142
143
144
145
146/// FUNCTIONS ////////////////////
147
148function find_form_errors(&$user, &$usernew, &$err) {
149
150 if (empty($usernew->email))
151 $err["email"] = "Missing email address";
152
bda8d43a 153 if (empty($usernew->city))
154 $err["city"] = "Missing city or town";
155
156 if (empty($usernew->country))
157 $err["country"] = "Missing country";
158
f9903ed0 159 else if (! validate_email($usernew->email))
160 $err["email"] = "Invalid email address, check carefully";
161
162 else if ($otheruser = get_record("user", "email", $usernew->email)) {
163 if ($otheruser->id <> $user->id) {
164 $err["email"] = "Email address already in use by someone else.";
165 }
166 }
167
168 $user->email = $usernew->email;
169
170 return count($err);
171}
172
173
174?>