diff --git a/mod/h5pactivity/classes/external/get_h5pactivity_access_information.php b/mod/h5pactivity/classes/external/get_h5pactivity_access_information.php new file mode 100644 index 00000000000..ff4ae4bd5fc --- /dev/null +++ b/mod/h5pactivity/classes/external/get_h5pactivity_access_information.php @@ -0,0 +1,115 @@ +. + +/** + * This is the external method for getting access information for a h5p activity. + * + * @package mod_h5pactivity + * @since Moodle 3.9 + * @copyright 2020 Carlos Escobedo + * @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 + * @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); + } +} \ No newline at end of file diff --git a/mod/h5pactivity/db/services.php b/mod/h5pactivity/db/services.php new file mode 100644 index 00000000000..7b8bbe71c68 --- /dev/null +++ b/mod/h5pactivity/db/services.php @@ -0,0 +1,38 @@ +. + +/** + * H5P activity external functions and service definitions. + * + * @package mod_h5pactivity + * @since Moodle 3.9 + * @copyright 2020 Carlos Escobedo + * @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], + ] +]; \ No newline at end of file diff --git a/mod/h5pactivity/tests/external/get_h5pactivity_access_information_test.php b/mod/h5pactivity/tests/external/get_h5pactivity_access_information_test.php new file mode 100644 index 00000000000..fcef64d79c4 --- /dev/null +++ b/mod/h5pactivity/tests/external/get_h5pactivity_access_information_test.php @@ -0,0 +1,97 @@ +. + +/** + * External function test for get_h5pactivity_access_information. + * + * @package mod_h5pactivity + * @category external + * @since Moodle 3.9 + * @copyright 2020 Carlos Escobedo + * @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 + * @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); + } +} \ No newline at end of file diff --git a/mod/h5pactivity/version.php b/mod/h5pactivity/version.php index ad71475886a..9fe7926a8da 100644 --- a/mod/h5pactivity/version.php +++ b/mod/h5pactivity/version.php @@ -25,5 +25,5 @@ defined('MOODLE_INTERNAL') || die(); $plugin->component = 'mod_h5pactivity'; -$plugin->version = 2020042202; +$plugin->version = 2020042203; $plugin->requires = 2020013000;