MDL-67585 core_course: new web service to return course content items
authorJake Dallimore <jake@moodle.com>
Fri, 31 Jan 2020 05:28:32 +0000 (13:28 +0800)
committerJake Dallimore <jake@moodle.com>
Thu, 20 Feb 2020 03:42:23 +0000 (11:42 +0800)
course/externallib.php
course/tests/externallib_test.php
lib/db/services.php

index 7adb8b2..ed0459f 100644 (file)
@@ -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];
+    }
 }
index bc2bc6d..ab49748 100644 (file)
@@ -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']);
+    }
 }
index cbe108e..da5dbc2 100644 (file)
@@ -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',