MDL-58712 core_user: New WS core_user_get_private_files_info
authorJuan Leyva <juanleyvadelgado@gmail.com>
Tue, 13 Jun 2017 10:56:19 +0000 (11:56 +0100)
committerJuan Leyva <juanleyvadelgado@gmail.com>
Thu, 15 Jun 2017 08:40:30 +0000 (09:40 +0100)
lib/db/services.php
lib/tests/filelib_test.php
user/externallib.php
user/tests/externallib_test.php
version.php

index 002640e..7bb91d1 100644 (file)
@@ -1206,6 +1206,15 @@ $functions = array(
         'type' => 'write',
         'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
     ),
+    'core_user_get_private_files_info' => array(
+        'classname' => 'core_user_external',
+        'methodname' => 'get_private_files_info',
+        'classpath' => 'user/externallib.php',
+        'description' => 'Returns general information about files in the user private files area.',
+        'type' => 'read',
+        'capabilities' => 'moodle/user:manageownfiles',
+        'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
+    ),
 
     // Competencies functions.
     'core_competency_create_competency_framework' => array(
index 30a3849..a367aaa 100644 (file)
@@ -1287,7 +1287,7 @@ EOF;
         $this->assertEquals($size, $fileinfo['filesize_without_references']);
     }
 
-        /**
+    /**
      * Test file_get_file_area_info.
      */
     public function test_file_get_file_area_info() {
index 5ead62c..2eea9f3 100644 (file)
@@ -1860,4 +1860,76 @@ class core_user_external extends external_api {
             )
         );
     }
+
+    /**
+     * Returns description of method parameters.
+     *
+     * @return external_function_parameters
+     * @since Moodle 3.4
+     */
+    public static function get_private_files_info_parameters() {
+        return new external_function_parameters(
+            array(
+                'userid' => new external_value(PARAM_INT, 'Id of the user, default to current user.', VALUE_DEFAULT, 0)
+            )
+        );
+    }
+
+    /**
+     * Returns general information about files in the user private files area.
+     *
+     * @param int $userid Id of the user, default to current user.
+     * @return array of warnings and file area information
+     * @since Moodle 3.4
+     * @throws moodle_exception
+     */
+    public static function get_private_files_info($userid = 0) {
+        global $CFG, $USER;
+        require_once($CFG->libdir . '/filelib.php');
+
+        $params = self::validate_parameters(self::get_private_files_info_parameters(), array('userid' => $userid));
+        $warnings = array();
+
+        $context = context_system::instance();
+        self::validate_context($context);
+
+        if (empty($params['userid']) || $params['userid'] == $USER->id) {
+            $usercontext = context_user::instance($USER->id);
+            require_capability('moodle/user:manageownfiles', $usercontext);
+        } else {
+            $user = core_user::get_user($params['userid'], '*', MUST_EXIST);
+            core_user::require_active_user($user);
+            // Only admins can retrieve other users information.
+            require_capability('moodle/site:config', $context);
+            $usercontext = context_user::instance($user->id);
+        }
+
+        $fileareainfo = file_get_file_area_info($usercontext->id, 'user', 'private');
+
+        $result = array();
+        $result['filecount'] = $fileareainfo['filecount'];
+        $result['foldercount'] = $fileareainfo['foldercount'];
+        $result['filesize'] = $fileareainfo['filesize'];
+        $result['filesizewithoutreferences'] = $fileareainfo['filesize_without_references'];
+        $result['warnings'] = $warnings;
+        return $result;
+    }
+
+    /**
+     * Returns description of method result value.
+     *
+     * @return external_description
+     * @since Moodle 3.4
+     */
+    public static function get_private_files_info_returns() {
+        return new external_single_structure(
+            array(
+                'filecount' => new external_value(PARAM_INT, 'Number of files in the area.'),
+                'foldercount' => new external_value(PARAM_INT, 'Number of folders in the area.'),
+                'filesize' => new external_value(PARAM_INT, 'Total size of the files in the area.'),
+                'filesizewithoutreferences' => new external_value(PARAM_INT, 'Total size of the area excluding file references'),
+                'warnings' => new external_warnings()
+            )
+        );
+    }
 }
index be52830..bf9edd8 100644 (file)
@@ -1148,6 +1148,60 @@ class core_user_externallib_testcase extends externallib_advanced_testcase {
         } catch (Exception $e) {
             $this->fail('Expecting \'usernotfullysetup\' moodle_exception to be thrown.');
         }
+    }
+
+    /**
+     * Test get_private_files_info
+     */
+    public function test_get_private_files_info() {
+
+        $this->resetAfterTest(true);
+        $user = self::getDataGenerator()->create_user();
+        $this->setUser($user);
+        $usercontext = context_user::instance($user->id);
+
+        $filerecord = array(
+            'contextid' => $usercontext->id,
+            'component' => 'user',
+            'filearea'  => 'private',
+            'itemid'    => 0,
+            'filepath'  => '/',
+            'filename'  => 'thefile',
+        );
+
+        $fs = get_file_storage();
+        $file = $fs->create_file_from_string($filerecord, 'abc');
+
+        // Get my private files information.
+        $result = core_user_external::get_private_files_info();
+        $result = external_api::clean_returnvalue(core_user_external::get_private_files_info_returns(), $result);
+        $this->assertEquals(1, $result['filecount']);
+        $this->assertEquals($file->get_filesize(), $result['filesize']);
+        $this->assertEquals(1, $result['foldercount']);   // Base directory.
+        $this->assertEquals($file->get_filesize(), $result['filesizewithoutreferences']);
+
+        // As admin, get user information.
+        $this->setAdminUser();
+        $result = core_user_external::get_private_files_info($user->id);
+        $result = external_api::clean_returnvalue(core_user_external::get_private_files_info_returns(), $result);
+        $this->assertEquals(1, $result['filecount']);
+        $this->assertEquals($file->get_filesize(), $result['filesize']);
+        $this->assertEquals(1, $result['foldercount']);   // Base directory.
+        $this->assertEquals($file->get_filesize(), $result['filesizewithoutreferences']);
+    }
+
+    /**
+     * Test get_private_files_info missing permissions.
+     */
+    public function test_get_private_files_info_missing_permissions() {
+
+        $this->resetAfterTest(true);
+        $user1 = self::getDataGenerator()->create_user();
+        $user2 = self::getDataGenerator()->create_user();
+        $this->setUser($user1);
 
+        $this->setExpectedException('required_capability_exception');
+        // Try to retrieve other user private files info.
+        core_user_external::get_private_files_info($user2->id);
     }
 }
index e90db34..597192c 100644 (file)
@@ -29,7 +29,7 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$version  = 2017060800.00;              // YYYYMMDD      = weekly release date of this DEV branch.
+$version  = 2017060800.01;              // YYYYMMDD      = weekly release date of this DEV branch.
                                         //         RR    = release increments - 00 in DEV branches.
                                         //           .XX = incremental changes.