translated strings changed based on /en/moodle.php
[moodle.git] / user / edit.php
CommitLineData
f9903ed0 1<?PHP // $Id$
2
dc2590e5 3 require_once("../config.php");
951b22a8 4 require_once("$CFG->libdir/gdlib.php");
f9903ed0 5
6 require_variable($id); // user id
7 require_variable($course); // course id
8
f971d502 9
10
f9903ed0 11 if (! $user = get_record("user", "id", $id)) {
12 error("User ID was incorrect");
13 }
14
15 if (! $course = get_record("course", "id", $course)) {
0087d8a6 16 error("Course ID was incorrect");
f9903ed0 17 }
18
2c104c01 19 if ($user->confirmed and user_not_fully_set_up($user)) {
faebaf0f 20 // Special case which can only occur when a new account
21 // has just been created by EXTERNAL authentication
22 // This is the only page in Moodle that has the exception
23 // so that users can set up their accounts
24 $newaccount = true;
25
deae201f 26 if (empty($USER)) {
ea229804 27 error("Sessions don't seem to be working on this server!");
28 }
29
faebaf0f 30 } else {
31 $newaccount = false;
dc2590e5 32 require_login($course->id);
faebaf0f 33 }
f9903ed0 34
a3447e10 35 if ($USER->id <> $user->id and !isadmin()) {
f9903ed0 36 error("You can only edit your own information");
37 }
38
603d4c72 39 if (isguest()) {
40 error("The guest user cannot edit their profile.");
41 }
42
a3447e10 43 if (isguest($user->id)) {
44 error("Sorry, the guest user cannot be edited.");
45 }
46
d35757eb 47 // load the relevant auth libraries
48 if ($user->auth) {
49 $auth = $user->auth;
50 if (!file_exists("$CFG->dirroot/auth/$auth/lib.php")) {
51 $auth = "manual"; // Can't find auth module, default to internal
52 }
53 require_once("$CFG->dirroot/auth/$auth/lib.php");
54 }
f9903ed0 55
d35757eb 56
f9903ed0 57/// If data submitted, then process and store.
58
dc2590e5 59 if ($usernew = data_submitted()) {
999beee0 60
ab394456 61 if (isset($USER->username)) {
62 check_for_restricted_user($USER->username, "$CFG->wwwroot/course/view.php?id=$course->id");
63 }
64
999beee0 65 foreach ($usernew as $key => $data) {
6acfbb43 66 $usernew->$key = addslashes(clean_text(stripslashes($usernew->$key), FORMAT_MOODLE));
999beee0 67 }
68
9d05e261 69 $usernew->firstname = trim(strip_tags($usernew->firstname));
70 $usernew->lastname = trim(strip_tags($usernew->lastname));
999beee0 71
dc2590e5 72 if (isset($usernew->username)) {
73 $usernew->username = trim(moodle_strtolower($usernew->username));
74 }
75
de38e262 76
81d425b4 77 require_once($CFG->dirroot.'/lib/uploadlib.php');
96038147 78 $um = new upload_manager('imagefile',false,false,null,false,0,true,true);
81d425b4 79
d35757eb 80 if (find_form_errors($user, $usernew, $err, $um)) {
81d425b4 81 if (empty($err['imagefile']) && $usernew->picture = save_profile_image($user->id, $um,'users')) {
1aacb503 82 set_field('user', 'picture', $usernew->picture, 'id', $user->id); /// Note picture in DB
ec67cbf2 83 } else {
84 if (!empty($usernew->deletepicture)) {
85 set_field('user', 'picture', 0, 'id', $user->id); /// Delete picture
86 $usernew->picture = 0;
87 }
a406cdec 88 }
89
a3447e10 90 $user = $usernew;
91
92 } else {
dc2590e5 93 $timenow = time();
81d425b4 94
95 if (!$usernew->picture = save_profile_image($user->id,$um,'users')) {
ec67cbf2 96 if (!empty($usernew->deletepicture)) {
97 set_field('user', 'picture', 0, 'id', $user->id); /// Delete picture
98 $usernew->picture = 0;
99 } else {
100 $usernew->picture = $user->picture;
101 }
f9903ed0 102 }
81d425b4 103
f9903ed0 104 $usernew->timemodified = time();
81d425b4 105
a3447e10 106 if (isadmin()) {
f0eec3b6 107 if (!empty($usernew->newpassword)) {
a3447e10 108 $usernew->password = md5($usernew->newpassword);
d35757eb 109 // update external passwords
110 if (!empty($CFG->{'auth_'. $user->auth.'_stdchangepassword'})) {
111 if(function_exists('auth_user_update_password')){
112 if (!auth_user_update_password($user->username, $usernew->newpassword)){
113 error('Failed to update password on external auth: ' . $user->auth .
114 '. See the server logs for more details.');
115 }
116 } else {
117 error('Your external authentication module is misconfigued!');
118 }
119 }
120 // store forcepasswordchange in user's preferences
121 if (isset($usernew->forcepasswordchange)){
122 set_user_preference('auth_forcepasswordchange', 1, $user);
123 }
a3447e10 124 }
125 } else {
126 if (isset($usernew->newpassword)) {
127 error("You can not change the password like that");
128 }
129 }
ef9955b0 130 if ($usernew->url and !(substr($usernew->url, 0, 4) == "http")) {
131 $usernew->url = "http://".$usernew->url;
132 }
873960de 133
f9903ed0 134 if (update_record("user", $usernew)) {
d35757eb 135 if (function_exists("auth_user_update")){
136 auth_user_update($user, $usernew);
137 }
138 add_to_log($course->id, "user", "update", "view.php?id=$user->id&course=$course->id", "");
873960de 139
a3447e10 140 if ($user->id == $USER->id) {
141 // Copy data into $USER session variable
142 $usernew = (array)$usernew;
143 foreach ($usernew as $variable => $value) {
521d04cf 144 $USER->$variable = stripslashes($value);
a3447e10 145 }
1f33691c 146 if (isset($USER->newadminuser)) {
147 unset($USER->newadminuser);
dd85cc81 148 redirect("$CFG->wwwroot/", get_string("changessaved"));
1f33691c 149 }
839f2456 150 redirect("$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id", get_string("changessaved"));
a3447e10 151 } else {
003296c4 152 redirect("$CFG->wwwroot/$CFG->admin/user.php", get_string("changessaved"));
873960de 153 }
f9903ed0 154 } else {
155 error("Could not update the user record ($user->id)");
156 }
dc2590e5 157 }
f9903ed0 158 }
159
160/// Otherwise fill and print the form.
161
faebaf0f 162 $streditmyprofile = get_string("editmyprofile");
163 $strparticipants = get_string("participants");
164 $strnewuser = get_string("newuser");
8553b700 165
faebaf0f 166 if (($user->firstname and $user->lastname) or $newaccount) {
167 if ($newaccount) {
168 $userfullname = $strnewuser;
169 } else {
5fde0ca6 170 $userfullname = fullname($user, isteacher($course->id));
faebaf0f 171 }
7cbb4c96 172 if ($course->category) {
dc2590e5 173 print_header("$course->shortname: $streditmyprofile", "$course->fullname: $streditmyprofile",
031c49fa 174 "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a>
175 -> <a href=\"index.php?id=$course->id\">$strparticipants</a>
357b5286 176 -> <a href=\"view.php?id=$user->id&amp;course=$course->id\">$userfullname</a>
faebaf0f 177 -> $streditmyprofile", "");
7cbb4c96 178 } else {
1f33691c 179 if (isset($USER->newadminuser)) {
180 print_header();
181 } else {
182 print_header("$course->shortname: $streditmyprofile", "$course->fullname",
357b5286 183 "<a href=\"view.php?id=$user->id&amp;course=$course->id\">$userfullname</a>
1f33691c 184 -> $streditmyprofile", "");
185 }
7cbb4c96 186 }
f9903ed0 187 } else {
faebaf0f 188 $userfullname = $strnewuser;
7cbb4c96 189 $straddnewuser = get_string("addnewuser");
190
191 $stradministration = get_string("administration");
dc2590e5 192 print_header("$course->shortname: $streditmyprofile", "$course->fullname",
55e4b5f9 193 "<a href=\"$CFG->wwwroot/$CFG->admin/\">$stradministration</a> -> ".
194 "<a href=\"$CFG->wwwroot/$CFG->admin/users.php\">$strusers</a> -> $straddnewuser", "");
f9903ed0 195 }
196
bda8d43a 197 $teacher = strtolower($course->teacher);
a3447e10 198 if (!isadmin()) {
199 $teacheronly = "(".get_string("teacheronly", "", $teacher).")";
9c9f7d77 200 } else {
201 $teacheronly = "";
a3447e10 202 }
bda8d43a 203
7cbb4c96 204 print_heading( get_string("userprofilefor", "", "$userfullname") );
1f33691c 205
206 if (isset($USER->newadminuser)) {
d2b6ba70 207 print_simple_box(get_string("configintroadmin"), "center", "50%");
1f33691c 208 echo "<br />";
209 }
210
4d0dde91 211 print_simple_box_start("center", "", "$THEME->cellheading");
9c9f7d77 212 if (!empty($err)) {
d35757eb 213 echo "<center>";
214 notify(get_string("someerrorswerefound"));
215 echo "</center>";
a406cdec 216 }
dc2590e5 217 include("edit.html");
f9903ed0 218 print_simple_box_end();
f9903ed0 219
1f33691c 220 if (!isset($USER->newadminuser)) {
221 print_footer($course);
222 }
223
224 exit;
f9903ed0 225
226
227
228/// FUNCTIONS ////////////////////
229
81d425b4 230function find_form_errors(&$user, &$usernew, &$err, &$um) {
c9ca1fa5 231 global $CFG;
f9903ed0 232
a3447e10 233 if (isadmin()) {
2b25f2a0 234 if (empty($usernew->username)) {
a3447e10 235 $err["username"] = get_string("missingusername");
236
2b25f2a0 237 } else if (record_exists("user", "username", $usernew->username) and $user->username == "changeme") {
d35757eb 238 $err["username"] = get_string("usernameexists");
2b25f2a0 239
240 } else {
c9ca1fa5 241 if (empty($CFG->extendedusernamechars)) {
242 $string = eregi_replace("[^(-\.[:alnum:])]", "", $usernew->username);
243 if (strcmp($usernew->username, $string)) {
244 $err["username"] = get_string("alphanumerical");
245 }
246 }
2b25f2a0 247 }
248
ecac660c 249 if (empty($usernew->newpassword) and empty($user->password) and is_internal_auth() )
a3447e10 250 $err["newpassword"] = get_string("missingpassword");
e98e0915 251
09ba0c8a 252 if (($usernew->newpassword == "admin") or ($user->password == md5("admin") and empty($usernew->newpassword)) ) {
e98e0915 253 $err["newpassword"] = get_string("unsafepassword");
09ba0c8a 254 }
a3447e10 255 }
256
f9903ed0 257 if (empty($usernew->email))
8553b700 258 $err["email"] = get_string("missingemail");
f9903ed0 259
87f3a895 260 if (empty($usernew->description) and !isadmin())
a3447e10 261 $err["description"] = get_string("missingdescription");
262
bda8d43a 263 if (empty($usernew->city))
8553b700 264 $err["city"] = get_string("missingcity");
bda8d43a 265
9c055aa5 266 if (empty($usernew->firstname))
267 $err["firstname"] = get_string("missingfirstname");
268
269 if (empty($usernew->lastname))
270 $err["lastname"] = get_string("missinglastname");
271
bda8d43a 272 if (empty($usernew->country))
8553b700 273 $err["country"] = get_string("missingcountry");
bda8d43a 274
21f01485 275 if (! validate_email($usernew->email)) {
8553b700 276 $err["email"] = get_string("invalidemail");
f9903ed0 277
21f01485 278 } else if ($otheruser = get_record("user", "email", $usernew->email)) {
f9903ed0 279 if ($otheruser->id <> $user->id) {
8553b700 280 $err["email"] = get_string("emailexists");
f9903ed0 281 }
282 }
21f01485 283
284 if (empty($err["email"]) and !isadmin()) {
85a1d4c9 285 if ($error = email_is_not_allowed($usernew->email)) {
286 $err["email"] = $error;
21f01485 287 }
288 }
81d425b4 289 if (!$um->preprocess_files()) {
290 $err['imagefile'] = $um->notify;
291 }
f9903ed0 292
d35757eb 293 if ($CFG->auth_user_firstname_editlock && !($user->firstname === $usernew->firstname)){
294 $err["firstname"] = get_string("editlock");
295 }
296 if ($CFG->auth_user_lastname_editlock && !($user->lastname === $usernew->lastname)){
297 $err["lastname"] = get_string("editlock");
298 }
299 if ($CFG->auth_user_email_editlock && !($user->email === $usernew->email)){
300 $err["email"] = get_string("editlock");
301 }
302 if ($CFG->auth_user_phone1_editlock && !($user->phone1 === $usernew->phone1)){
303 $err["phone1"] = get_string("editlock");
304 }
305 if ($CFG->auth_user_phone2_editlock && !($user->phone2 === $usernew->phone2)){
306 $err["phone2"] = get_string("editlock");
307 }
308 if ($CFG->auth_user_department_editlock && !($user->department === $usernew->department)){
309 $err["department"] = get_string("editlock");
310 }
311 if ($CFG->auth_user_address_editlock && !($user->address === $usernew->address)){
312 $err["address"] = get_string("editlock");
313 }
314 if ($CFG->auth_user_city_editlock && !($user->city === $usernew->city)){
315 $err["city"] = get_string("editlock");
316 }
317 if ($CFG->auth_user_description_editlock && !($user->description === $usernew->description)){
318 $err["description"] = get_string("editlock");
319 }
320 if ($CFG->auth_user_idnumber_editlock && !($user->idnumber === $usernew->idnumber)){
321 $err["idnumber"] = get_string("editlock");
322 }
323 if ($CFG->auth_user_lang_editlock && !($user->lang === $usernew->lang)){
324 $err["lang"] = get_string("editlock");
325 }
326 if ($CFG->auth_user_guid_editlock && !($user->guid === $usernew->guid)){
327 $err["guid"] = get_string("editlock");
328 }
329
f9903ed0 330 $user->email = $usernew->email;
331
332 return count($err);
333}
334
335
336?>