MDL-58712 core_user: New WS core_user_get_private_files_info

This commit is contained in:
Juan Leyva 2017-06-13 11:56:19 +01:00
parent 0237571004
commit f74ac6e704
5 changed files with 137 additions and 2 deletions

View 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(

View 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() {

View 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()
)
);
}
}

View 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);
}
}

View 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.