MDL-69776 files: New WS core_files_delete_draft_files
authorJuan Leyva <juanleyvadelgado@gmail.com>
Wed, 23 Sep 2020 17:58:09 +0000 (19:58 +0200)
committerJuan Leyva <juanleyvadelgado@gmail.com>
Fri, 25 Sep 2020 08:37:16 +0000 (10:37 +0200)
files/classes/external/delete/draft.php [new file with mode: 0644]
files/tests/externallib_test.php
lib/db/services.php
version.php

diff --git a/files/classes/external/delete/draft.php b/files/classes/external/delete/draft.php
new file mode 100644 (file)
index 0000000..3ad88a8
--- /dev/null
@@ -0,0 +1,117 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * This is the external method for deleting draft files.
+ *
+ * @package    core_files
+ * @since      Moodle 3.10
+ * @copyright  2020 Juan Leyva <juan@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace core_files\external\delete;
+
+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;
+
+/**
+ * This is the external method for deleting draft files.
+ *
+ * @copyright  2020 Juan Leyva <juan@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class draft extends external_api {
+
+    /**
+     * Describes the parameters for execute.
+     *
+     * @return external_function_parameters
+     * @since Moodle 3.10
+     */
+    public static function execute_parameters() : external_function_parameters {
+        return new external_function_parameters (
+            [
+                'draftitemid' => new external_value(PARAM_INT, 'Item id of the draft file area'),
+                'files' => new external_multiple_structure(
+                    new external_single_structure(
+                        [
+                            'filepath'  => new external_value(PARAM_PATH, 'Path to the file or directory to delete.'),
+                            'filename'  => new external_value(PARAM_FILE, 'Name of the file to delete.'),
+                        ]
+                    ), 'Files or directories to be deleted.'
+                ),
+            ]
+        );
+    }
+
+    /**
+     * Delete the indicated files (or directories) from a user draft file area.
+     *
+     * @param  int    $draftitemid item id of the draft file area
+     * @param  array  $files       files to be deleted
+     * @return array of warnings and parent paths of the files deleted
+     * @since Moodle 3.10
+     */
+    public static function execute(int $draftitemid, array $files) : array {
+        global $CFG, $USER;
+        require_once($CFG->dirroot . '/repository/lib.php');
+
+        $params = self::validate_parameters(self::execute_parameters(), compact('draftitemid', 'files'));
+        [$draftitemid, $files] = array_values($params);
+
+        $usercontext = context_user::instance($USER->id);
+        self::validate_context($usercontext);
+
+        $files = array_map(function($file) {
+            return (object) $file;
+        }, $files);
+        $parentpaths = repository_delete_selected_files($usercontext, 'user', 'draft', $draftitemid, $files);
+
+        return [
+            'parentpaths' => array_keys($parentpaths),
+            'warnings' => [],
+        ];
+    }
+
+    /**
+     * Describes the execute return value.
+     *
+     * @return external_single_structure
+     * @since Moodle 3.10
+     */
+    public static function execute_returns() : external_single_structure {
+        return new external_single_structure(
+            [
+                'parentpaths' => new external_multiple_structure(
+                    new external_value(PARAM_PATH, 'Path to parent directory of the deleted files.')
+                ),
+                'warnings' => new external_warnings(),
+            ]
+        );
+    }
+}
index b652e6d..91a9e13 100644 (file)
@@ -293,4 +293,57 @@ class core_files_externallib_testcase extends advanced_testcase {
 
         $this->assertEquals($testfilelisting, $testdata);
     }
+
+    /**
+     * Test delete draft files
+     */
+    public function test_delete_draft_files() {
+        global $USER;
+
+        $this->resetAfterTest();
+        $this->setAdminUser();
+
+        // Add files to user draft area.
+        $draftitemid = file_get_unused_draft_itemid();
+        $context = context_user::instance($USER->id);
+        $filerecordinline = array(
+            'contextid' => $context->id,
+            'component' => 'user',
+            'filearea'  => 'draft',
+            'itemid'    => $draftitemid,
+            '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($context->id, 'user', 'draft', $draftitemid, '/fakefolder/');
+        $filerecordinline['filepath'] = '/fakefolder/';
+        $filerecordinline['filename'] = 'fakeimage.png';
+        $fs->create_file_from_string($filerecordinline, 'img...');
+
+        // Check two files were created (one file and one directory).
+        $files = core_files_external::get_files($context->id, 'user', 'draft', $draftitemid, '/', '');
+        $files = external_api::clean_returnvalue(core_files_external::get_files_returns(), $files);
+        $this->assertCount(2, $files['files']);
+
+        // Check the folder has one file.
+        $files = core_files_external::get_files($context->id, 'user', 'draft', $draftitemid, '/fakefolder/', '');
+        $files = external_api::clean_returnvalue(core_files_external::get_files_returns(), $files);
+        $this->assertCount(1, $files['files']);
+
+        // Delete a file and a folder.
+        $filestodelete = [
+            ['filepath' => '/', 'filename' => 'faketxt.txt'],
+            ['filepath' => '/fakefolder/', 'filename' => ''],
+        ];
+        $paths = core_files\external\delete\draft::execute($draftitemid, $filestodelete);
+        $paths = external_api::clean_returnvalue(core_files\external\delete\draft::execute_returns(), $paths);
+
+        // Check everything was deleted.
+        $files = core_files_external::get_files($context->id, 'user', 'draft', $draftitemid, '/', '');
+        $files = external_api::clean_returnvalue(core_files_external::get_files_returns(), $files);
+        $this->assertCount(0, $files['files']);
+    }
 }
index 94f0c0d..55d3f57 100644 (file)
@@ -807,6 +807,13 @@ $functions = array(
         'type'        => 'write',
         'classpath'   => 'files/externallib.php',
     ),
+    'core_files_delete_draft_files' => array(
+        'classname' => 'core_files\external\delete\draft',
+        'methodname' => 'execute',
+        'description' => 'Delete the indicated files (or directories) from a user draft file area.',
+        'type'        => 'write',
+        'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
+    ),
     'core_form_get_filetypes_browser_data' => array(
         'classname' => 'core_form\external',
         'methodname' => 'get_filetypes_browser_data',
index 3c05ae9..6df7210 100644 (file)
@@ -29,7 +29,7 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$version  = 2021052500.16;              // YYYYMMDD      = weekly release date of this DEV branch.
+$version  = 2021052500.17;              // YYYYMMDD      = weekly release date of this DEV branch.
                                         //         RR    = release increments - 00 in DEV branches.
                                         //           .XX = incremental changes.
 $release  = '4.0dev (Build: 20200924)'; // Human-friendly version name