Initial revision
[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")) {
63 mkdir("$CFG->dataroot/users", 0777);
64 }
65 if (!file_exists("$CFG->dataroot/users/$USER->id")) {
66 mkdir("$CFG->dataroot/users/$USER->id", 0777);
67 }
68
69 ImageCopyBicubic($im1, $im, 0, 0, $cx-$half, $cy-$half, 100, 100, $half*2, $half*2);
70 ImageCopyBicubic($im2, $im, 0, 0, $cx-$half, $cy-$half, 35, 35, $half*2, $half*2);
71
72 // Draw borders over the top.
73 $black1 = ImageColorAllocate ($im1, 0, 0, 0);
74 $black2 = ImageColorAllocate ($im2, 0, 0, 0);
75 ImageLine ($im1, 0, 0, 0, 99, $black1);
76 ImageLine ($im1, 0, 99, 99, 99, $black1);
77 ImageLine ($im1, 99, 99, 99, 0, $black1);
78 ImageLine ($im1, 99, 0, 0, 0, $black1);
79 ImageLine ($im2, 0, 0, 0, 34, $black2);
80 ImageLine ($im2, 0, 34, 34, 34, $black2);
81 ImageLine ($im2, 34, 34, 34, 0, $black2);
82 ImageLine ($im2, 34, 0, 0, 0, $black2);
83
84 ImageJpeg($im1, "$CFG->dataroot/users/$USER->id/f1.jpg", 90);
85 ImageJpeg($im2, "$CFG->dataroot/users/$USER->id/f2.jpg", 95);
86 $usernew->picture = "1";
87 } else {
88 $usernew->picture = $user->picture;
89 }
90
91 $usernew->timemodified = time();
92
93 if (update_record("user", $usernew)) {
94 add_to_log("Updated own profile", $course->id);
95 redirect("view.php?id=$user->id&course=$course->id", "Changes saved");
96 } else {
97 error("Could not update the user record ($user->id)");
98 }
99 }
100 }
101
102/// Otherwise fill and print the form.
103
104 if ($course->category) {
105 print_header("Edit user profile", "Edit user profile",
106 "<A HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</A>
107 -> <A HREF=\"index.php?id=$course->id\">Participants</A>
108 -> <A HREF=\"view.php?id=$USER->id&course=$course->id\">$USER->firstname $USER->lastname</A>
109 -> Edit profile", "");
110 } else {
111 print_header("Edit user profile", "Edit user profile",
112 "<A HREF=\"view.php?id=$USER->id&course=$course->id\">$USER->firstname $USER->lastname</A>
113 -> Edit profile", "");
114 }
115
116 print_simple_box_start("center", "", "$THEME->cellheading");
117 echo "<H2>User profile for $user->firstname $user->lastname</H2>";
118 include("edit.html");
119 print_simple_box_end();
120
121 print_footer($course);
122
123
124
125
126/// FUNCTIONS ////////////////////
127
128function find_form_errors(&$user, &$usernew, &$err) {
129
130 if (empty($usernew->email))
131 $err["email"] = "Missing email address";
132
133 else if (! validate_email($usernew->email))
134 $err["email"] = "Invalid email address, check carefully";
135
136 else if ($otheruser = get_record("user", "email", $usernew->email)) {
137 if ($otheruser->id <> $user->id) {
138 $err["email"] = "Email address already in use by someone else.";
139 }
140 }
141
142 $user->email = $usernew->email;
143
144 return count($err);
145}
146
147
148?>