MDL-68449 mod_h5pactivity: Add a WS for get access information

This WS should return access information required by an external client to check if and how to display the activity module.
Require for Mobile App
This commit is contained in:
cescobedo 2020-05-15 19:41:26 +02:00
parent 84ab8a51e2
commit feae09652e
4 changed files with 251 additions and 1 deletions

View File

@ -0,0 +1,115 @@
<?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 getting access information for a h5p activity.
*
* @package mod_h5pactivity
* @since Moodle 3.9
* @copyright 2020 Carlos Escobedo <carlos@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_h5pactivity\external;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/externallib.php');
use external_api;
use external_function_parameters;
use external_value;
use external_single_structure;
use external_warnings;
use context_module;
/**
* This is the external method for getting access information for a h5p activity.
*
* @copyright 2020 Carlos Escobedo <carlos@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class get_h5pactivity_access_information extends external_api {
/**
* Parameters.
*
* @return external_function_parameters
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters(
[
'h5pactivityid' => new external_value(PARAM_INT, 'h5p activity instance id')
]
);
}
/**
* Return access information for a given h5p activity.
*
* @param int $h5pactivityid The h5p activity id.
* @return array of warnings and the access information
* @since Moodle 3.9
* @throws moodle_exception
*/
public static function execute(int $h5pactivityid): array {
global $DB;
$params = external_api::validate_parameters(self::execute_parameters(), [
'h5pactivityid' => $h5pactivityid
]);
// Request and permission validation.
$h5pactivity = $DB->get_record('h5pactivity', ['id' => $params['h5pactivityid']], '*', MUST_EXIST);
list($course, $cm) = get_course_and_cm_from_instance($h5pactivity, 'h5pactivity');
$context = context_module::instance($cm->id);
self::validate_context($context);
$result = [];
// Return all the available capabilities.
$capabilities = load_capability_def('mod_h5pactivity');
foreach ($capabilities as $capname => $capdata) {
$field = 'can' . str_replace('mod/h5pactivity:', '', $capname);
$result[$field] = has_capability($capname, $context);
}
$result['warnings'] = [];
return $result;
}
/**
* Describes the get_h5pactivity_access_information return value.
*
* @return external_single_structure
* @since Moodle 3.9
*/
public static function execute_returns() {
$structure = [
'warnings' => new external_warnings()
];
$capabilities = load_capability_def('mod_h5pactivity');
foreach ($capabilities as $capname => $capdata) {
$field = 'can' . str_replace('mod/h5pactivity:', '', $capname);
$structure[$field] = new external_value(PARAM_BOOL, 'Whether the user has the capability ' . $capname . ' allowed.',
VALUE_OPTIONAL);
}
return new external_single_structure($structure);
}
}

View File

@ -0,0 +1,38 @@
<?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/>.
/**
* H5P activity external functions and service definitions.
*
* @package mod_h5pactivity
* @since Moodle 3.9
* @copyright 2020 Carlos Escobedo <carlos@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
$functions = [
'mod_h5pactivity_get_h5pactivity_access_information' => [
'classname' => 'mod_h5pactivity\external\get_h5pactivity_access_information',
'methodname' => 'execute',
'classpath' => '',
'description' => 'Return access information for a given h5p activity.',
'type' => 'read',
'capabilities' => 'mod/h5pactivity:view',
'services' => [MOODLE_OFFICIAL_MOBILE_SERVICE],
]
];

View File

@ -0,0 +1,97 @@
<?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/>.
/**
* External function test for get_h5pactivity_access_information.
*
* @package mod_h5pactivity
* @category external
* @since Moodle 3.9
* @copyright 2020 Carlos Escobedo <carlos@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_h5pactivity\external;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
use dml_missing_record_exception;
use external_api;
use externallib_advanced_testcase;
/**
* External function test for get_h5pactivity_access_information.
*
* @package mod_h5pactivity
* @copyright 2020 Carlos Escobedo <carlos@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class get_h5pactivity_access_information_testcase extends externallib_advanced_testcase {
/**
* Test the behaviour of get_h5pactivity_access_information().
*/
public function test_get_h5pactivity_access_information() {
$this->resetAfterTest();
$this->setAdminUser();
$course = $this->getDataGenerator()->create_course();
$activity = $this->getDataGenerator()->create_module('h5pactivity', ['course' => $course]);
$teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
// Check the access information for a student.
$this->setUser($student);
$result = get_h5pactivity_access_information::execute($activity->id);
$result = external_api::clean_returnvalue(get_h5pactivity_access_information::execute_returns(), $result);
$this->assertCount(0, $result['warnings']);
unset($result['warnings']);
// Check default values for capabilities for student.
$enabledcaps = ['canview', 'cansubmit'];
foreach ($result as $capname => $capvalue) {
if (in_array($capname, $enabledcaps)) {
$this->assertTrue($capvalue);
} else {
$this->assertFalse($capvalue);
}
}
// Check the access information for a teacher.
$this->setUser($teacher);
$result = get_h5pactivity_access_information::execute($activity->id);
$result = external_api::clean_returnvalue(get_h5pactivity_access_information::execute_returns(), $result);
$this->assertCount(0, $result['warnings']);
unset($result['warnings']);
// Check default values for capabilities for teacher.
$enabledcaps = ['canview', 'canaddinstance', 'canreviewattempts'];
foreach ($result as $capname => $capvalue) {
if (in_array($capname, $enabledcaps)) {
$this->assertTrue($capvalue);
} else {
$this->assertFalse($capvalue);
}
}
// Call the WS using an unexisting h5pactivityid.
$this->expectException(dml_missing_record_exception::class);
$result = get_h5pactivity_access_information::execute($activity->id + 1);
}
}

View File

@ -25,5 +25,5 @@
defined('MOODLE_INTERNAL') || die();
$plugin->component = 'mod_h5pactivity';
$plugin->version = 2020042202;
$plugin->version = 2020042203;
$plugin->requires = 2020013000;