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