]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-8605 New user directories implemented
authornicolasconnault <nicolasconnault>
Thu, 11 Oct 2007 09:01:29 +0000 (09:01 +0000)
committernicolasconnault <nicolasconnault>
Thu, 11 Oct 2007 09:01:29 +0000 (09:01 +0000)
auth/mnet/auth.php
enrol/imsenterprise/enrol.php
lang/en_utf8/moodle.php
lib/db/upgrade.php
lib/db/upgradelib.php
lib/gdlib.php
lib/moodlelib.php
lib/simpletest/testmoodlelib.php
user/editlib.php
user/pix.php
version.php

index 3f611062f1e0a8af67a462a42430cbf8cab3cdd6..c8f5800cdccf9b7687e94ead64935fb505142663 100644 (file)
@@ -115,7 +115,7 @@ class auth_plugin_mnet extends auth_plugin_base {
         $userdata['session.gc_maxlifetime']  = ini_get('session.gc_maxlifetime');
         $userdata['picture']                 = $user->picture;
         if (!empty($user->picture)) {
-            $imagefile = "{$CFG->dataroot}/users/{$user->id}/f1.jpg";
+            $imagefile = make_user_directory($user->id, true) . "/f1.jpg";
             if (file_exists($imagefile)) {
                 $userdata['imagehash'] = sha1(file_get_contents($imagefile));
             }
@@ -319,7 +319,7 @@ class auth_plugin_mnet extends auth_plugin_base {
 
             // TODO: fetch image if it has changed
             if ($key == 'imagehash') {
-                $dirname = "{$CFG->dataroot}/users/{$localuser->id}";
+                $dirname = make_user_directory($localuser->id, true);
                 $filename = "$dirname/f1.jpg";
 
                 $localhash = '';
@@ -1263,8 +1263,8 @@ class auth_plugin_mnet extends auth_plugin_base {
         global $CFG;
 
         if ($user = get_record('user', 'username', addslashes($username), 'mnethostid', $CFG->mnet_localhost_id)) {
-            $filename1 = "{$CFG->dataroot}/users/{$user->id}/f1.jpg";
-            $filename2 = "{$CFG->dataroot}/users/{$user->id}/f2.jpg";
+            $filename1 = make_user_directory($user->id, true) . "/f1.jpg";
+            $filename2 = make_user_directory($user->id, true) . "/f2.jpg";
             $return = array();
             if (file_exists($filename1)) {
                 $return['f1'] = base64_encode(file_get_contents($filename1));
index 56fbc89c627276691026c7d45d7a0aec4b6e7c28..416073d5599598aaed88e3c39197896665318bec 100644 (file)
@@ -669,7 +669,7 @@ function process_person_tag($tagcontents){
                                    $person->picture = 1;
                                    //Llibreria creada per nosaltres mateixos.
                                    require_once($CFG->dirroot.'/lib/gdlib.php');
-                                   if ($usernew->picture = save_profile_image($id, $person->urlphoto,'users', true)) {
+                                   if ($usernew->picture = save_profile_image($id, $person->urlphoto,'user')) {
                                      set_field('user', 'picture', $usernew->picture, 'id', $id);  /// Note picture in DB
                                    }
                                  }
index 32315e76fed112619ea7d3b46276860f4b63bf94..9c931b7e3840a0a305e12130ba5fa5de524ab84a 100644 (file)
@@ -1096,6 +1096,7 @@ $string['numwords'] = '$a words';
 $string['numyears'] = '$a years';
 $string['ok'] = 'OK';
 $string['oldpassword'] = 'Current Password';
+$string['olduserdirectory'] = 'This is the OLD users directory, and is no longer needed. \r\nYou may safely delete it. \r\nThe files it contains have been copied to the NEW user directory.';
 $string['opentoguests'] = 'Guest access';
 $string['optional'] = 'optional';
 $string['order'] = 'Order';
index dcd11c46809bf15a917fde76a611f966f83f71f3..7d97d4a23d82634018e8b1096c7a8fca9972d560 100644 (file)
@@ -2547,8 +2547,47 @@ function xmldb_main_upgrade($oldversion=0) {
 
         upgrade_main_savepoint($result, 2007100903);
     }
+    
+    if ($result && $oldversion < 2007101101) {
+        // Get list of users by browsing moodledata/user
+        $oldusersdir = $CFG->dataroot . '/users';
+        $folders = get_directory_list($oldusersdir, '', false, true, false);
+        
+        foreach ($folders as $userid) {
+            $olddir = $oldusersdir . '/' . $userid;
+
+            // Create new user directory
+            if (!$newdir = make_user_directory($userid)) {
+                $result = false;
+                break;
+            }
 
+            // Move contents of old directory to new one
+            if (file_exists($olddir) && file_exists($newdir)) {
+                $files = get_directory_list($olddir);
+                foreach ($files as $file) {
+                    copy($olddir . '/' . $file, $newdir . '/' . $file);
+                }
+            } else {
+                notify("Could not move the contents of $olddir into $newdir!");
+                $result = false;
+                break;
+            }
+        }
 
+        // Leave a README in old users directory
+        $readmefilename = $oldusersdir . '/README.txt';
+        if ($handle = fopen($readmefilename, 'w+b')) {
+            if (!fwrite($handle, get_string('olduserdirectory'))) {
+                // Could not write to the readme file. No cause for huge concern 
+                notify("Could not write to the README.txt file in $readmefilename.");
+            }
+            fclose($handle);
+        } else {
+            // Could not create the readme file. No cause for huge concern
+            notify("Could not create the README.txt file in $readmefilename.");
+        } 
+    }    
 
     return $result;
 }
index 9d5eb0923883a903dcc19acdd186f7b2c579ae61..bd65e33b2f3bb706c310d121ac6e54358b0418e0 100644 (file)
@@ -2,7 +2,7 @@
 
 /*
  * This file is used for special upgrade functions - for example groups and gradebook.
- * These functions must use SQL and dabase related functions only- no other Moodle API,
+ * These functions must use SQL and database related functions only- no other Moodle API,
  * because it might depend on db structures that are not yet present during upgrade.
  * (Do not use functions from accesslib.php, grades classes or group functions at all!)
  */
index 37e53591e5fa339cce6c3c3dbbec5b9777506690..9e39ad5835dbc065861f073130c23b4868fc5827 100644 (file)
@@ -96,12 +96,10 @@ function delete_profile_image($id, $dir='users') {
  *
  * @param int $id user or group id
  * @param object $uploadmanager object referencing the image
- * @param string $dir type of entity - groups, users, ...
+ * @param string $dir type of entity - groups, user, ...
  * @return boolean success
  */
-function save_profile_image($id, $uploadmanager, $dir='users') {
-//
-
+function save_profile_image($id, $uploadmanager, $dir='user') {
     global $CFG;
 
     if (empty($CFG->gdversion)) {
@@ -115,18 +113,23 @@ function save_profile_image($id, $uploadmanager, $dir='users') {
     umask(0000);
 
     if (!file_exists($CFG->dataroot .'/'. $dir)) {
-        if (! mkdir($CFG->dataroot .'/'. $dir, $CFG->directorypermissions)) {
+        if (!mkdir($CFG->dataroot .'/'. $dir, $CFG->directorypermissions)) {
             return false;
         }
     }
+    
+    if ($dir == 'user') {
+        $destination = make_user_directory($id, true);
+    } else {
+        $destination = "$CFG->dataroot/$dir/$id";
+    }
 
-    if (!file_exists($CFG->dataroot .'/'. $dir .'/'. $id)) {
-        if (! mkdir($CFG->dataroot .'/'. $dir .'/'. $id, $CFG->directorypermissions)) {
+    if (!file_exists($destination)) {
+        if (!mkdir($destination, $CFG->directorypermissions)) {
             return false;
         }
     }
 
-    $destination = $CFG->dataroot .'/'. $dir .'/'. $id;
     if (!$uploadmanager->save_files($destination)) {
         return false;
     }
@@ -202,12 +205,12 @@ function save_profile_image($id, $uploadmanager, $dir='users') {
     ImageCopyBicubic($im2, $im, 0, 0, $cx-$half, $cy-$half, 35, 35, $half*2, $half*2);
 
     if (function_exists('ImageJpeg')) {
-        @touch($CFG->dataroot .'/'. $dir .'/'. $id .'/f1.jpg');  // Helps in Safe mode
-        @touch($CFG->dataroot .'/'. $dir .'/'. $id .'/f2.jpg');  // Helps in Safe mode
-        if (ImageJpeg($im1, $CFG->dataroot .'/'. $dir .'/'. $id .'/f1.jpg', 90) and
-            ImageJpeg($im2, $CFG->dataroot .'/'. $dir .'/'. $id .'/f2.jpg', 95) ) {
-            @chmod($CFG->dataroot .'/'. $dir .'/'. $id .'/f1.jpg', 0666);
-            @chmod($CFG->dataroot .'/'. $dir .'/'. $id .'/f2.jpg', 0666);
+        @touch($destination .'/f1.jpg');  // Helps in Safe mode
+        @touch($destination .'/f2.jpg');  // Helps in Safe mode
+        if (ImageJpeg($im1, $destination .'/f1.jpg', 90) and
+            ImageJpeg($im2, $destination .'/f2.jpg', 95) ) {
+            @chmod($destination .'/f1.jpg', 0666);
+            @chmod($destination .'/f2.jpg', 0666);
             return 1;
         }
     } else {
index 33634e6b2ec6afb1083d2e9ab68d08880f14a319..59fecc8c9b15e4c2403b66bb22c98d8b746ab725 100644 (file)
@@ -7,7 +7,7 @@
 // Moodle - Modular Object-Oriented Dynamic Learning Environment         //
 //          http://moodle.org                                            //
 //                                                                       //
-// Copyright (C) 1999 onwards Martin Dougiamas  http://dougiamas.com       //
+// Copyright (C) 1999 onwards Martin Dougiamas  http://dougiamas.com     //
 //                                                                       //
 // This program is free software; you can redistribute it and/or modify  //
 // it under the terms of the GNU General Public License as published by  //
@@ -4124,6 +4124,35 @@ function make_mod_upload_directory($courseid) {
     return $moddata;
 }
 
+/**
+ * Makes a directory for a particular user.
+ *
+ * @uses $CFG
+ * @param int $userid The id of the user in question - maps to id field of 'user' table.
+ * @param bool $test Whether we are only testing the return value (do not create the directory)
+ * @return string|false Returns full path to directory if successful, false if not
+ */
+function make_user_directory($userid, $test=false) {
+    global $CFG;
+
+    if (is_bool($userid) || $userid < 0 || !ereg('^[0-9]{1,10}$', $userid) || $userid > 2147483647) {
+        if (!$test) {
+            notify("Given userid was not a valid integer! (" . gettype($userid) . " $userid)");
+        }
+        return false;
+    }
+
+    // Generate a two-level path for the userid. First level groups them by slices of 1000 users, second level is userid
+    $level1 = floor($userid / 1000) * 1000;
+    
+    $userdir = "user/$level1/$userid";
+    if ($test) {
+        return $CFG->dataroot . '/' . $userdir;
+    } else {
+        return make_upload_directory($userdir);
+    }
+}
+
 /**
  * Returns current name of file on disk if it exists.
  *
index f2b8b74cc471cd2e031ac16d1dfa6ca3a3665b0f..7a33665a10e2b5dd4315911d0862bdc7529bb926 100644 (file)
@@ -7,7 +7,7 @@
 // Moodle - Modular Object-Oriented Dynamic Learning Environment         //
 //          http://moodle.org                                            //
 //                                                                       //
-// Copyright (C) 1999 onwards Martin Dougiamas  http://dougiamas.com       //
+// Copyright (C) 1999 onwards Martin Dougiamas  http://dougiamas.com     //
 //                                                                       //
 // This program is free software; you can redistribute it and/or modify  //
 // it under the terms of the GNU General Public License as published by  //
@@ -179,6 +179,25 @@ class moodlelib_test extends UnitTestCase {
             '#()*#,9789\\\'\".,');
         
     }
+
+    function test_make_user_directory() {
+        global $CFG;
+
+        // Test success conditions
+        $this->assertEqual("$CFG->dataroot/user/0/0", make_user_directory(0, true));
+        $this->assertEqual("$CFG->dataroot/user/0/1", make_user_directory(1, true));
+        $this->assertEqual("$CFG->dataroot/user/0/999", make_user_directory(999, true));
+        $this->assertEqual("$CFG->dataroot/user/1000/1000", make_user_directory(1000, true));
+        $this->assertEqual("$CFG->dataroot/user/2147483000/2147483647", make_user_directory(2147483647, true)); // Largest int possible
+
+        // Test fail conditions
+        $this->assertFalse(make_user_directory(2147483648, true)); // outside int boundary
+        $this->assertFalse(make_user_directory(-1, true));
+        $this->assertFalse(make_user_directory('string', true));
+        $this->assertFalse(make_user_directory(false, true));
+        $this->assertFalse(make_user_directory(true, true));
+        
+    }
 }
 
 ?>
index df0019b8afe5fc81c01934d38f797ea4d1cc15f2..a48ea6640802fabd5003de10208d2f581934be75 100644 (file)
@@ -23,10 +23,10 @@ function useredit_update_picture(&$usernew, &$userform) {
     global $CFG;
 
     if (isset($usernew->deletepicture) and $usernew->deletepicture) {
-        $location = $CFG->dataroot.'/users/'.$usernew->id;
+        $location = make_user_directory($usernew->id, true);
         @remove_dir($location);
         set_field('user', 'picture', 0, 'id', $usernew->id);
-    } else if ($usernew->picture = save_profile_image($usernew->id, $userform->get_um(), 'users')) {
+    } else if ($usernew->picture = save_profile_image($usernew->id, $userform->get_um(), 'user')) {
         set_field('user', 'picture', 1, 'id', $usernew->id);
     }
 }
index b3435be9c8a6c916b26fff68e3b9d5b851b2e33e..d909551eaff4acb8dbe20a2e8db1dcc61f8a9413 100644 (file)
     if (count($args) == 2) {
         $userid   = (integer)$args[0];
         $image    = $args[1];
-        $pathname = $CFG->dataroot.'/users/'.$userid.'/'.$image;
+        $pathname = make_user_directory($userid, true) . "/$image";
         if (file_exists($pathname) and !is_dir($pathname)) {
             send_file($pathname, $image);
-        }
+        } 
     }
 
     // picture was deleted - use default instead
index 9ca8761aa8bee40534154b7fdf1e48ab2d1046cd..b927d5a61209c75f7e985196db74c852cbca62cc 100644 (file)
@@ -6,7 +6,7 @@
 // This is compared against the values stored in the database to determine
 // whether upgrades should be performed (see lib/db/*.php)
 
-    $version = 2007101000;  // YYYYMMDD = date
+    $version = 2007101100;  // YYYYMMDD = date
                             //       XY = increments within a single day
 
     $release = '2.0 dev';   // Human-friendly version name