]> git.mjollnir.org Git - moodle.git/commitdiff
Made the user profile a little more robust.
authormoodler <moodler>
Wed, 20 Nov 2002 14:52:32 +0000 (14:52 +0000)
committermoodler <moodler>
Wed, 20 Nov 2002 14:52:32 +0000 (14:52 +0000)
Uploaded images are now saved even if the rest of the form
has errors.

If errors are found in the form then a message is printed up
the top to make it clearer that they need to fix something.

user/edit.html
user/edit.php
user/lib.php

index 7931c62a8a4afedb916bff57b3dbc92030eb1b6e..45c1a4125660ab0f70f1bef138ed0a1c300db160 100644 (file)
@@ -19,10 +19,11 @@ if (isadmin()) {
        if (isset($err["newpassword"])) {
         formerr($err["newpassword"]);
     } else if (empty($user->newpassword)) {
-           echo "(".get_string("leavetokeep").")";
+           echo " (".get_string("leavetokeep").")";
     }
        echo "</td>";
     echo "</tr>";
+    echo "<tr><td colspan=2><HR></td></tr>";
 }
 ?>
 <tr valign=top>
@@ -124,9 +125,12 @@ if (isadmin()) {
 </tr>
 <tr valign=top>
        <td><P><? print_string("userdescription") ?>:</td>
-       <td><TEXTAREA NAME=description COLS=50 ROWS=10 WRAP=virtual><? p($user->description) ?></TEXTAREA> 
+       <td><? if ($err["description"]) {
+               formerr($err["description"]);
+               echo "<BR>";
+           } ?>
+    <TEXTAREA NAME=description COLS=50 ROWS=10 WRAP=virtual><? p($user->description) ?></TEXTAREA> 
     <? helpbutton("text", get_string("helptext")) ?>
-       <? formerr($err["description"]) ?>
        </td>
 </tr>
 <tr>
index c0cf126aa1db7e57b3cba0f49f74192c9700dc7f..c303e34be0e2f81f2b19a6256aaa62c332fce71d 100644 (file)
         $usernew->lastname  = strip_tags($usernew->lastname);
 
         if (find_form_errors($user, $usernew, $err) ) {
+            if ($filename = valid_uploaded_file($imagefile)) { 
+                $usernew->picture = save_user_image($user->id, $filename);
+            }
+
             $user = $usernew;
 
         } else {
                    $timenow = time();
 
             if ($filename = valid_uploaded_file($imagefile)) { 
-                $imageinfo = GetImageSize($filename);
-                $image->width  = $imageinfo[0];
-                $image->height = $imageinfo[1];
-                $image->type   = $imageinfo[2];
-    
-                switch ($image->type) {
-                    case 2: $im = ImageCreateFromJPEG($filename); break;
-                    case 3: $im = ImageCreateFromPNG($filename); break;
-                    default: error("Image must be in JPG or PNG format");
-                }
-                if (function_exists("ImageCreateTrueColor") and $CFG->gdversion >= 2) {
-                    $im1 = ImageCreateTrueColor(100,100);
-                    $im2 = ImageCreateTrueColor(35,35);
-                } else {
-                    $im1 = ImageCreate(100,100);
-                    $im2 = ImageCreate(35,35);
-                }
-                
-                $cx = $image->width / 2;
-                $cy = $image->height / 2;
-    
-                if ($image->width < $image->height) {
-                    $half = floor($image->width / 2.0);
-                } else {
-                    $half = floor($image->height / 2.0);
-                }
-    
-                if (!file_exists("$CFG->dataroot/users")) {
-                    if (! mkdir("$CFG->dataroot/users", 0777)) {
-                        $badpermissions = true;
-                    }
-                }
-                if (!file_exists("$CFG->dataroot/users/$user->id")) {
-                    if (! mkdir("$CFG->dataroot/users/$user->id", 0777)) {
-                        $badpermissions = true;
-                    }
-                }
-                
-                if ($badpermissions) {
-                    $usernew->picture = "0";
-
-                } else {
-                    ImageCopyBicubic($im1, $im, 0, 0, $cx-$half, $cy-$half, 100, 100, $half*2, $half*2);
-                    ImageCopyBicubic($im2, $im, 0, 0, $cx-$half, $cy-$half, 35, 35, $half*2, $half*2);
-    
-                    // Draw borders over the top.
-                    $black1 = ImageColorAllocate ($im1, 0, 0, 0);
-                    $black2 = ImageColorAllocate ($im2, 0, 0, 0);
-                    ImageLine ($im1, 0, 0, 0, 99, $black1);
-                    ImageLine ($im1, 0, 99, 99, 99, $black1);
-                    ImageLine ($im1, 99, 99, 99, 0, $black1);
-                    ImageLine ($im1, 99, 0, 0, 0, $black1);
-                    ImageLine ($im2, 0, 0, 0, 34, $black2);
-                    ImageLine ($im2, 0, 34, 34, 34, $black2);
-                    ImageLine ($im2, 34, 34, 34, 0, $black2);
-                    ImageLine ($im2, 34, 0, 0, 0, $black2);
-                
-                    ImageJpeg($im1, "$CFG->dataroot/users/$user->id/f1.jpg", 90);
-                    ImageJpeg($im2, "$CFG->dataroot/users/$user->id/f2.jpg", 95);
-                    $usernew->picture = "1";
-                }
+                $usernew->picture = save_user_image($user->id, $filename);
             } else {
                 $usernew->picture = $user->picture;
             }
 
     print_heading( get_string("userprofilefor", "", "$userfullname") );
     print_simple_box_start("center", "", "$THEME->cellheading");
+    if ($err) {
+       echo "<CENTER>";
+       notify(get_string("someerrorswerefound"));
+       echo "</CENTER>";
+    }
        include("edit.html");
     print_simple_box_end();
     print_footer($course);
index f8e027904750c26cbd2c36976c53a17c1bcc1378..53623fc122f1feacd1abd878e58a52c5bbfdc7dd 100644 (file)
@@ -49,6 +49,77 @@ function ImageCopyBicubic ($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $
     } 
 }
 
+
+function save_user_image($userid, $filename) {
+// Given a filename to a known image, this function scales and crops
+// it and saves it in the right place to be a user image.
+
+    global $CFG;
+
+    $imageinfo = GetImageSize($filename);
+    $image->width  = $imageinfo[0];
+    $image->height = $imageinfo[1];
+    $image->type   = $imageinfo[2];
+
+    switch ($image->type) {
+        case 2: $im = ImageCreateFromJPEG($filename); break;
+        case 3: $im = ImageCreateFromPNG($filename); break;
+        default: return 0;
+    }
+    if (function_exists("ImageCreateTrueColor") and $CFG->gdversion >= 2) {
+        $im1 = ImageCreateTrueColor(100,100);
+        $im2 = ImageCreateTrueColor(35,35);
+    } else {
+        $im1 = ImageCreate(100,100);
+        $im2 = ImageCreate(35,35);
+    }
+    
+    $cx = $image->width / 2;
+    $cy = $image->height / 2;
+
+    if ($image->width < $image->height) {
+        $half = floor($image->width / 2.0);
+    } else {
+        $half = floor($image->height / 2.0);
+    }
+
+    if (!file_exists("$CFG->dataroot/users")) {
+        if (! mkdir("$CFG->dataroot/users", 0777)) {
+            $badpermissions = true;
+        }
+    }
+    if (!file_exists("$CFG->dataroot/users/$user->id")) {
+        if (! mkdir("$CFG->dataroot/users/$user->id", 0777)) {
+            $badpermissions = true;
+        }
+    }
+    
+    if ($badpermissions) {
+        return 0;
+
+    } else {
+        ImageCopyBicubic($im1, $im, 0, 0, $cx-$half, $cy-$half, 100, 100, $half*2, $half*2);
+        ImageCopyBicubic($im2, $im, 0, 0, $cx-$half, $cy-$half, 35, 35, $half*2, $half*2);
+
+        // Draw borders over the top.
+        $black1 = ImageColorAllocate ($im1, 0, 0, 0);
+        $black2 = ImageColorAllocate ($im2, 0, 0, 0);
+        ImageLine ($im1, 0, 0, 0, 99, $black1);
+        ImageLine ($im1, 0, 99, 99, 99, $black1);
+        ImageLine ($im1, 99, 99, 99, 0, $black1);
+        ImageLine ($im1, 99, 0, 0, 0, $black1);
+        ImageLine ($im2, 0, 0, 0, 34, $black2);
+        ImageLine ($im2, 0, 34, 34, 34, $black2);
+        ImageLine ($im2, 34, 34, 34, 0, $black2);
+        ImageLine ($im2, 34, 0, 0, 0, $black2);
+    
+        ImageJpeg($im1, "$CFG->dataroot/users/$userid/f1.jpg", 90);
+        ImageJpeg($im2, "$CFG->dataroot/users/$userid/f2.jpg", 95);
+        return 1;
+    }
+}
+
+
 function print_user($user, $course, $string) {
 
     global $USER, $COUNTRIES;