MDL-76432 core_courseformat: add file_handlers webservice

In 4.0- version each time the course page is loaded the file handlers
are calculate din the backend and injected directly into JS using a json
encapsulation. With this new webservice the handlers can be obtained
directly from the frontend when needed.
This commit is contained in:
Ferran Recio 2022-12-01 15:44:09 +01:00
parent e359b9889a
commit a3f116367e
4 changed files with 186 additions and 1 deletions

View File

@ -0,0 +1,95 @@
<?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/>.
namespace core_courseformat\external;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/course/dnduploadlib.php');
use core_external\external_api;
use core_external\external_function_parameters;
use core_external\external_multiple_structure;
use core_external\external_single_structure;
use core_external\external_value;
use dndupload_handler;
/**
* Class for exporting a course file handlers.
*
* @package core_courseformat
* @copyright 2022 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 4.2
*/
class file_handlers extends external_api {
/**
* Webservice parameters.
*
* @return external_function_parameters
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters(
[
'courseid' => new external_value(PARAM_INT, 'course id', VALUE_REQUIRED),
]
);
}
/**
* Return the list of available file handlers.
*
* @param int $courseid the course id
* @return array of file hanlders.
*/
public static function execute(int $courseid): array {
global $CFG;
require_once($CFG->dirroot . '/course/lib.php');
$params = external_api::validate_parameters(self::execute_parameters(), [
'courseid' => $courseid,
]);
$courseid = $params['courseid'];
self::validate_context(\context_course::instance($courseid));
$format = course_get_format($courseid);
$course = $format->get_course();
$handler = new dndupload_handler($course, null);
$data = $handler->get_js_data();
return $data->filehandlers ?? [];
}
/**
* Webservice returns.
*
* @return external_multiple_structure
*/
public static function execute_returns(): external_multiple_structure {
return new external_multiple_structure(
new external_single_structure([
'extension' => new external_value(PARAM_TEXT, 'File extension'),
'module' => new external_value(PARAM_TEXT, 'Target module'),
'message' => new external_value(PARAM_TEXT, 'Output message'),
])
);
}
}

View File

@ -0,0 +1,84 @@
<?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/>.
namespace core_courseformat\external;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
use external_api;
use dndupload_handler;
/**
* Tests for the file_hanlders class.
*
* @package core_course
* @category test
* @copyright 2022 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \core_courseformat\external\file_handlers
*/
class file_handlers_test extends \externallib_advanced_testcase {
/**
* Setup to ensure that fixtures are loaded.
*/
public static function setupBeforeClass(): void { // phpcs:ignore
global $CFG;
require_once($CFG->dirroot . '/course/lib.php');
require_once($CFG->dirroot . '/course/dnduploadlib.php');
}
/**
* Test the behaviour of get_state::execute().
*
* @covers ::execute
*/
public function test_execute(): void {
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course(['numsections' => 3, 'format' => 'topics']);
$this->setAdminUser();
$result = file_handlers::execute($course->id);
$result = external_api::clean_returnvalue(file_handlers::execute_returns(), $result);
$handlers = new dndupload_handler($course, null);
$expected = $handlers->get_js_data();
$this->assertCount(count($expected->filehandlers), $result);
foreach ($expected->filehandlers as $key => $handler) {
$tocompare = $result[$key];
$this->assertEquals($handler->extension, $tocompare['extension']);
}
}
/**
* Test the behaviour of get_state::execute() in a wrong course.
*
* @covers ::execute
*/
public function test_execute_wrong_course(): void {
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course(['numsections' => 3, 'format' => 'topics']);
$this->setAdminUser();
$this->expectException('dml_missing_record_exception');
$result = file_handlers::execute(-1);
$result = external_api::clean_returnvalue(file_handlers::execute_returns(), $result);
}
}

View File

@ -515,6 +515,12 @@ $functions = array(
'type' => 'read',
'ajax' => true,
),
'core_courseformat_file_handlers' => [
'classname' => 'core_courseformat\external\file_handlers',
'description' => 'Get the current course file hanlders.',
'type' => 'read',
'ajax' => true,
],
'core_courseformat_get_state' => [
'classname' => 'core_courseformat\external\get_state',
'description' => 'Get the current course state.',

View File

@ -29,7 +29,7 @@
defined('MOODLE_INTERNAL') || die();
$version = 2023020300.00; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2023020300.01; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.
$release = '4.2dev (Build: 20230203)'; // Human-friendly version name