MDL-68450 mod_h5pactivity: Add WS view_h5pactivity
authorcescobedo <carlos.escobedo@gmail.com>
Fri, 15 May 2020 18:16:03 +0000 (20:16 +0200)
committercescobedo <carlos.escobedo@gmail.com>
Mon, 18 May 2020 12:17:46 +0000 (14:17 +0200)
This is the external method for triggering the course module viewed event
and update the module completion status. Affects mobile app.

mod/h5pactivity/classes/external/view_h5pactivity.php [new file with mode: 0644]
mod/h5pactivity/classes/local/manager.php
mod/h5pactivity/db/services.php [new file with mode: 0644]
mod/h5pactivity/tests/external/view_h5pactivity_test.php [new file with mode: 0644]
mod/h5pactivity/version.php
mod/h5pactivity/view.php

diff --git a/mod/h5pactivity/classes/external/view_h5pactivity.php b/mod/h5pactivity/classes/external/view_h5pactivity.php
new file mode 100644 (file)
index 0000000..3b33183
--- /dev/null
@@ -0,0 +1,107 @@
+<?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 triggering the course module viewed event.
+ *
+ * @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;
+use mod_h5pactivity\local\manager;
+
+/**
+ * This is the external method for triggering the course module viewed event.
+ *
+ * @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
+ */
+class view_h5pactivity 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')
+            ]
+        );
+    }
+
+    /**
+     * Trigger the course module viewed event and update the module completion status.
+     *
+     * @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 {
+
+        $params = external_api::validate_parameters(self::execute_parameters(), [
+            'h5pactivityid' => $h5pactivityid
+        ]);
+        $warnings = [];
+
+        // Request and permission validation.
+        list($course, $cm) = get_course_and_cm_from_instance($params['h5pactivityid'], 'h5pactivity');
+
+        $context = context_module::instance($cm->id);
+        self::validate_context($context);
+
+        $manager = manager::create_from_coursemodule($cm);
+        $manager->set_module_viewed($course);
+
+        $result = array(
+            'status' => true,
+            'warnings' => $warnings,
+        );
+        return $result;
+    }
+
+    /**
+     * Describes the view_h5pactivity return value.
+     *
+     * @return external_single_structure
+     * @since Moodle 3.9
+     */
+    public static function execute_returns() {
+        return new external_single_structure(
+            [
+                'status' => new external_value(PARAM_BOOL, 'status: true if success'),
+                'warnings' => new external_warnings()
+            ]
+        );
+    }
+}
index 4f446c6..4c471bf 100644 (file)
@@ -33,6 +33,7 @@ use cm_info;
 use moodle_recordset;
 use core_user;
 use stdClass;
+use mod_h5pactivity\event\course_module_viewed;
 
 /**
  * Class manager for H5P activity
@@ -445,4 +446,29 @@ class manager {
         }
         return $result;
     }
+
+    /**
+     * Trigger module viewed event and set the module viewed for completion.
+     *
+     * @param stdClass $course course object
+     * @return void
+     */
+    public function set_module_viewed(stdClass $course): void {
+        global $CFG;
+        require_once($CFG->libdir . '/completionlib.php');
+
+        // Trigger module viewed event.
+        $event = course_module_viewed::create([
+            'objectid' => $this->instance->id,
+            'context' => $this->context
+        ]);
+        $event->add_record_snapshot('course', $course);
+        $event->add_record_snapshot('course_modules', $this->coursemodule);
+        $event->add_record_snapshot('h5pactivity', $this->instance);
+        $event->trigger();
+
+        // Completion.
+        $completion = new \completion_info($course);
+        $completion->set_module_viewed($this->coursemodule);
+    }
 }
diff --git a/mod/h5pactivity/db/services.php b/mod/h5pactivity/db/services.php
new file mode 100644 (file)
index 0000000..d37058c
--- /dev/null
@@ -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_view_h5pactivity' => [
+        'classname'     => 'mod_h5pactivity\external\view_h5pactivity',
+        'methodname'    => 'execute',
+        'classpath'     => '',
+        'description'   => 'Trigger the course module viewed event and update the module completion status.',
+        'type'          => 'write',
+        'capabilities'  => 'mod/h5pactivity:view',
+        'services'      => [MOODLE_OFFICIAL_MOBILE_SERVICE],
+    ]
+];
diff --git a/mod/h5pactivity/tests/external/view_h5pactivity_test.php b/mod/h5pactivity/tests/external/view_h5pactivity_test.php
new file mode 100644 (file)
index 0000000..09fa4dd
--- /dev/null
@@ -0,0 +1,155 @@
+<?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 view_h5pactivity.
+ *
+ * @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 external_api;
+use externallib_advanced_testcase;
+use stdClass;
+use context_module;
+use course_modinfo;
+
+/**
+ * External function test for view_h5pactivity.
+ *
+ * @package    mod_h5pactivity
+ * @copyright  2020 Carlos Escobedo <carlos@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class view_h5pactivity_testcase extends externallib_advanced_testcase {
+
+    /**
+     * Test test_view_h5pactivity invalid id.
+     */
+    public function test_view_h5pactivity_invalid_id() {
+        $this->resetAfterTest();
+        $this->setAdminUser();
+
+        $this->expectException('moodle_exception');
+        $result = view_h5pactivity::execute(0);
+    }
+
+    /**
+     * Test test_view_h5pactivity user not enrolled.
+     */
+    public function test_view_h5pactivity_user_not_enrolled() {
+        $this->resetAfterTest();
+        $this->setAdminUser();
+
+        // Setup scenario.
+        $scenario = $this->setup_scenario();
+
+        // Test not-enrolled user.
+        $usernotenrolled = self::getDataGenerator()->create_user();
+        $this->setUser($usernotenrolled);
+        $this->expectException('moodle_exception');
+        $result = view_h5pactivity::execute($scenario->h5pactivity->id);
+    }
+
+    /**
+     * Test test_view_h5pactivity user student.
+     */
+    public function test_view_h5pactivity_user_student() {
+        $this->resetAfterTest();
+        $this->setAdminUser();
+
+        // Setup scenario.
+        $scenario = $this->setup_scenario();
+
+        $cm = get_coursemodule_from_instance('h5pactivity', $scenario->h5pactivity->id);
+        $this->setUser($scenario->student);
+
+        // Trigger and capture the event.
+        $sink = $this->redirectEvents();
+
+        $result = view_h5pactivity::execute($scenario->h5pactivity->id);
+        $result = external_api::clean_returnvalue(view_h5pactivity::execute_returns(), $result);
+        $this->assertTrue($result['status']);
+
+        $events = $sink->get_events();
+        $this->assertCount(1, $events);
+        $event = array_shift($events);
+
+        // Checking that the event contains the expected values.
+        $this->assertInstanceOf('\mod_h5pactivity\event\course_module_viewed', $event);
+        $this->assertEquals($scenario->contextmodule, $event->get_context());
+        $h5pactivity = new \moodle_url('/mod/h5pactivity/view.php', array('id' => $cm->id));
+        $this->assertEquals($h5pactivity, $event->get_url());
+        $this->assertEventContextNotUsed($event);
+        $this->assertNotEmpty($event->get_name());
+    }
+
+    /**
+     * Test test_view_h5pactivity user missing capabilities.
+     */
+    public function test_view_h5pactivity_user_missing_capabilities() {
+        global $DB;
+
+        $this->resetAfterTest();
+        $this->setAdminUser();
+
+        // Setup scenario.
+        $scenario = $this->setup_scenario();
+
+        $studentrole = $DB->get_record('role', array('shortname' => 'student'));
+        // Test user with no capabilities.
+        // We need a explicit prohibit since this capability is only defined in authenticated user and guest roles.
+        assign_capability('mod/h5pactivity:view', CAP_PROHIBIT, $studentrole->id, $scenario->contextmodule->id);
+        // Empty all the caches that may be affected  by this change.
+        accesslib_clear_all_caches_for_unit_testing();
+        course_modinfo::clear_instance_cache();
+
+        $this->setUser($scenario->student);
+        $this->expectException('moodle_exception');
+        $result = view_h5pactivity::execute($scenario->h5pactivity->id);
+    }
+
+    /**
+     * Create a scenario to use into the tests.
+     *
+     * @return stdClass $scenario
+     */
+    protected function setup_scenario() {
+
+        $course = $this->getDataGenerator()->create_course();
+        $h5pactivity = $this->getDataGenerator()->create_module('h5pactivity', ['course' => $course]);
+        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
+        $contextmodule = context_module::instance($h5pactivity->cmid);
+
+        $scenario = new stdClass();
+        $scenario->contextmodule = $contextmodule;
+        $scenario->student = $student;
+        $scenario->h5pactivity = $h5pactivity;
+
+        return $scenario;
+    }
+}
index ad71475..9fe7926 100644 (file)
@@ -25,5 +25,5 @@
 defined('MOODLE_INTERNAL') || die();
 
 $plugin->component = 'mod_h5pactivity';
-$plugin->version = 2020042202;
+$plugin->version = 2020042203;
 $plugin->requires = 2020013000;
index 67bdc50..3378138 100644 (file)
  */
 
 use mod_h5pactivity\local\manager;
-use mod_h5pactivity\event\course_module_viewed;
 use core_h5p\factory;
 use core_h5p\player;
 use core_h5p\helper;
 
 require(__DIR__.'/../../config.php');
 require_once(__DIR__.'/lib.php');
-require_once($CFG->libdir.'/completionlib.php');
 
 $id = required_param('id', PARAM_INT);
 
@@ -44,17 +42,8 @@ $moduleinstance = $manager->get_instance();
 
 $context = $manager->get_context();
 
-$event = course_module_viewed::create([
-    'objectid' => $moduleinstance->id,
-    'context' => $context
-]);
-$event->add_record_snapshot('course', $course);
-$event->add_record_snapshot('h5pactivity', $moduleinstance);
-$event->trigger();
-
-// Completion.
-$completion = new completion_info($course);
-$completion->set_module_viewed($cm);
+// Trigger module viewed event and completion.
+$manager->set_module_viewed($course);
 
 // Convert display options to a valid object.
 $factory = new factory();