Skip to content

Commit

Permalink
MDL-70387 files: New WS core_files_get_unused_draft_itemid
Browse files Browse the repository at this point in the history
  • Loading branch information
jleyva committed Jan 13, 2021
1 parent ed24004 commit 7f02879
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 1 deletion.
100 changes: 100 additions & 0 deletions files/classes/external/get/unused_draft.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?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/>.

/**
* Generate a new draft itemid for the current user.
*
* @package core_files
* @since Moodle 3.11
* @copyright 2020 Juan Leyva <[email protected]>
* @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 <[email protected]>
* @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(),
]
);
}
}
37 changes: 37 additions & 0 deletions files/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}
7 changes: 7 additions & 0 deletions lib/db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7f02879

Please sign in to comment.