From 85b4eec5b7b5e7d7376163a085d0edb3835e2fe2 Mon Sep 17 00:00:00 2001 From: Jake Dallimore Date: Fri, 31 Jan 2020 13:28:32 +0800 Subject: [PATCH] MDL-67585 core_course: new web service to return course content items --- course/externallib.php | 51 +++++++++++++++++++++++++++++++++++++++ course/tests/externallib_test.php | 43 +++++++++++++++++++++++++++++++++ lib/db/services.php | 8 ++++++ 3 files changed, 102 insertions(+) diff --git a/course/externallib.php b/course/externallib.php index 7adb8b27e85..ed0459fbf35 100644 --- a/course/externallib.php +++ b/course/externallib.php @@ -4329,4 +4329,55 @@ class core_course_external extends external_api { public static function remove_content_item_from_user_favourites_returns() { return \core_course\local\exporters\course_content_item_exporter::get_read_structure(); } + + /** + * Returns description of method result value + * + * @return external_description + */ + public static function get_course_content_items_returns() { + return new external_single_structure([ + 'content_items' => new external_multiple_structure( + \core_course\local\exporters\course_content_item_exporter::get_read_structure() + ), + ]); + } + + /** + * Returns description of method parameters + * + * @return external_function_parameters + */ + public static function get_course_content_items_parameters() { + return new external_function_parameters([ + 'courseid' => new external_value(PARAM_INT, 'ID of the course', VALUE_REQUIRED), + ]); + } + + /** + * Given a course ID fetch all accessible modules for that course + * + * @param int $courseid The course we want to fetch the modules for + * @return array Contains array of modules and their metadata + */ + public static function get_course_content_items(int $courseid) { + global $USER; + + [ + 'courseid' => $courseid, + ] = self::validate_parameters(self::get_course_content_items_parameters(), [ + 'courseid' => $courseid, + ]); + + $coursecontext = context_course::instance($courseid); + self::validate_context($coursecontext); + $course = get_course($courseid); + + $contentitemservice = new \core_course\local\service\content_item_service( + new \core_course\local\repository\content_item_readonly_repository() + ); + + $contentitems = $contentitemservice->get_content_items_for_user_in_course($USER, $course); + return ['content_items' => $contentitems]; + } } diff --git a/course/tests/externallib_test.php b/course/tests/externallib_test.php index bc2bc6d4489..ab49748dfd3 100644 --- a/course/tests/externallib_test.php +++ b/course/tests/externallib_test.php @@ -3162,4 +3162,47 @@ class core_course_externallib_testcase extends externallib_advanced_testcase { }); $this->assertCount(0, $favourited); } + + /** + * Test the web service returning course content items for inclusion in activity choosers, etc. + */ + public function test_get_course_content_items() { + $this->resetAfterTest(); + + $course = self::getDataGenerator()->create_course(); + $user = self::getDataGenerator()->create_and_enrol($course, 'editingteacher'); + + // Fetch available content items as the editing teacher. + $this->setUser($user); + $result = core_course_external::get_course_content_items($course->id); + $result = external_api::clean_returnvalue(core_course_external::get_course_content_items_returns(), $result); + + $contentitemservice = new \core_course\local\service\content_item_service( + new \core_course\local\repository\content_item_readonly_repository() + ); + + // Check if the webservice returns exactly what the service defines, albeit in array form. + $serviceitemsasarray = array_map(function($item) { + return (array) $item; + }, $contentitemservice->get_content_items_for_user_in_course($user, $course)); + + $this->assertEquals($serviceitemsasarray, $result['content_items']); + } + + /** + * Test the web service returning course content items, specifically in case where the user can't manage activities. + */ + public function test_get_course_content_items_no_permission_to_manage() { + $this->resetAfterTest(); + + $course = self::getDataGenerator()->create_course(); + $user = self::getDataGenerator()->create_and_enrol($course, 'student'); + + // Fetch available content items as a student, who won't have the permission to manage activities. + $this->setUser($user); + $result = core_course_external::get_course_content_items($course->id); + $result = external_api::clean_returnvalue(core_course_external::get_course_content_items_returns(), $result); + + $this->assertEmpty($result['content_items']); + } } diff --git a/lib/db/services.php b/lib/db/services.php index cbe108ede9f..da5dbc2e5e7 100644 --- a/lib/db/services.php +++ b/lib/db/services.php @@ -653,6 +653,14 @@ $functions = array( 'type' => 'read', 'ajax' => true, ), + 'core_course_get_course_content_items' => array( + 'classname' => 'core_course_external', + 'methodname' => 'get_course_content_items', + 'classpath' => 'course/externallib.php', + 'description' => 'Fetch all the content items (activities, resources and their subtypes) for the activity picker', + 'type' => 'read', + 'ajax' => true, + ), 'core_enrol_get_course_enrolment_methods' => array( 'classname' => 'core_enrol_external', 'methodname' => 'get_course_enrolment_methods', -- 2.11.4.GIT