diff --git a/files/classes/external/get/unused_draft.php b/files/classes/external/get/unused_draft.php new file mode 100644 index 0000000000000..9bb952286babf --- /dev/null +++ b/files/classes/external/get/unused_draft.php @@ -0,0 +1,100 @@ +. + +/** + * Generate a new draft itemid for the current user. + * + * @package core_files + * @since Moodle 3.11 + * @copyright 2020 Juan Leyva + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace core_files\external\get; + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; +require_once($CFG->libdir . '/externallib.php'); +require_once($CFG->libdir . '/filelib.php'); + +use external_api; +use external_function_parameters; +use external_multiple_structure; +use external_single_structure; +use external_value; +use external_warnings; +use context_user; + +/** + * Generate a new draft itemid for the current user. + * + * @copyright 2020 Juan Leyva + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class unused_draft extends external_api { + + /** + * Describes the parameters for execute. + * + * @return external_function_parameters + * @since Moodle 3.11 + */ + public static function execute_parameters() : external_function_parameters { + return new external_function_parameters ([]); + } + + /** + * Generate a new draft itemid for the current user. + * + * @return array of information containing the draft item area and possible warnings. + * @since Moodle 3.11 + */ + public static function execute() : array { + global $USER; + + $usercontext = context_user::instance($USER->id); + self::validate_context($usercontext); + + return [ + 'component' => 'user', + 'contextid' => $usercontext->id, + 'userid' => $USER->id, + 'filearea' => 'draft', + 'itemid' => file_get_unused_draft_itemid(), + 'warnings' => [], + ]; + } + + /** + * Describes the execute return value. + * + * @return external_single_structure + * @since Moodle 3.11 + */ + public static function execute_returns() : external_single_structure { + return new external_single_structure( + [ + 'component' => new external_value(PARAM_COMPONENT, 'File area component.'), + 'contextid' => new external_value(PARAM_INT, 'File area context.'), + 'userid' => new external_value(PARAM_INT, 'File area user id.'), + 'filearea' => new external_value(PARAM_ALPHA, 'File area name.'), + 'itemid' => new external_value(PARAM_INT, 'File are item id.'), + 'warnings' => new external_warnings(), + ] + ); + } +} diff --git a/files/tests/externallib_test.php b/files/tests/externallib_test.php index 91a9e1396c31e..3e951d7d91844 100644 --- a/files/tests/externallib_test.php +++ b/files/tests/externallib_test.php @@ -346,4 +346,41 @@ public function test_delete_draft_files() { $files = external_api::clean_returnvalue(core_files_external::get_files_returns(), $files); $this->assertCount(0, $files['files']); } + + /** + * Test get_unused_draft_itemid. + */ + public function test_get_unused_draft_itemid() { + global $USER; + + $this->resetAfterTest(); + $this->setAdminUser(); + + // Add files to user draft area. + $result = core_files\external\get\unused_draft::execute(); + $result = external_api::clean_returnvalue(core_files\external\get\unused_draft::execute_returns(), $result); + + $filerecordinline = [ + 'contextid' => $result['contextid'], + 'component' => $result['component'], + 'filearea' => $result['filearea'], + 'itemid' => $result['itemid'], + 'filepath' => '/', + 'filename' => 'faketxt.txt', + ]; + $fs = get_file_storage(); + $fs->create_file_from_string($filerecordinline, 'fake txt contents 1.'); + + // Now create a folder with a file inside. + $fs->create_directory($result['contextid'], $result['component'], $result['filearea'], $result['itemid'], '/fakefolder/'); + $filerecordinline['filepath'] = '/fakefolder/'; + $filerecordinline['filename'] = 'fakeimage.png'; + $fs->create_file_from_string($filerecordinline, 'img...'); + + $context = context_user::instance($USER->id); + // Check two files were created (one file and one directory). + $files = core_files_external::get_files($context->id, 'user', 'draft', $result['itemid'], '/', ''); + $files = external_api::clean_returnvalue(core_files_external::get_files_returns(), $files); + $this->assertCount(2, $files['files']); + } } diff --git a/lib/db/services.php b/lib/db/services.php index 815404feace1a..17b2443d55859 100644 --- a/lib/db/services.php +++ b/lib/db/services.php @@ -821,6 +821,13 @@ 'type' => 'write', 'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE), ), + 'core_files_get_unused_draft_itemid' => array( + 'classname' => 'core_files\external\get\unused_draft', + 'methodname' => 'execute', + 'description' => 'Generate a new draft itemid for the current user.', + 'type' => 'write', + 'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE), + ), 'core_form_get_filetypes_browser_data' => array( 'classname' => 'core_form\external', 'methodname' => 'get_filetypes_browser_data', diff --git a/version.php b/version.php index e7a8e5183192e..8a7b8abfe780f 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2021052500.50; // YYYYMMDD = weekly release date of this DEV branch. +$version = 2021052500.51; // YYYYMMDD = weekly release date of this DEV branch. // RR = release increments - 00 in DEV branches. // .XX = incremental changes. $release = '4.0dev (Build: 20210108)'; // Human-friendly version name