From 4bed1682bb4dbb448cd36c449a0ea260894fea0f Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Wed, 30 Sep 2015 21:55:06 +0200 Subject: [PATCH] MDL-51624 mod_lti: New Web Service mod_lti_view_lti --- lib/db/services.php | 1 + mod/lti/classes/external.php | 63 ++++++++++++++++++++ mod/lti/db/services.php | 10 +++- mod/lti/launch.php | 15 +---- mod/lti/lib.php | 28 +++++++++ mod/lti/tests/externallib_test.php | 59 +++++++++++++++++++ mod/lti/tests/lib_test.php | 92 ++++++++++++++++++++++++++++++ mod/lti/version.php | 2 +- version.php | 2 +- 9 files changed, 256 insertions(+), 16 deletions(-) create mode 100644 mod/lti/tests/lib_test.php diff --git a/lib/db/services.php b/lib/db/services.php index cf69112bcb9..00c117e4df6 100644 --- a/lib/db/services.php +++ b/lib/db/services.php @@ -1248,6 +1248,7 @@ $services = array( 'mod_choice_get_choices_by_courses', 'mod_lti_get_tool_launch_data', 'mod_lti_get_ltis_by_courses', + 'mod_lti_view_lti', 'mod_imscp_view_imscp', 'mod_imscp_get_imscps_by_courses', ), diff --git a/mod/lti/classes/external.php b/mod/lti/classes/external.php index ef06b6ccfeb..e5d61d590d3 100644 --- a/mod/lti/classes/external.php +++ b/mod/lti/classes/external.php @@ -271,4 +271,67 @@ class mod_lti_external extends external_api { ) ); } + + /** + * Returns description of method parameters + * + * @return external_function_parameters + * @since Moodle 3.0 + */ + public static function view_lti_parameters() { + return new external_function_parameters( + array( + 'ltiid' => new external_value(PARAM_INT, 'lti instance id') + ) + ); + } + + /** + * Trigger the course module viewed event and update the module completion status. + * + * @param int $ltiid the lti instance id + * @return array of warnings and status result + * @since Moodle 3.0 + * @throws moodle_exception + */ + public static function view_lti($ltiid) { + global $DB; + + $params = self::validate_parameters(self::view_lti_parameters(), + array( + 'ltiid' => $ltiid + )); + $warnings = array(); + + // Request and permission validation. + $lti = $DB->get_record('lti', array('id' => $params['ltiid']), '*', MUST_EXIST); + list($course, $cm) = get_course_and_cm_from_instance($lti, 'lti'); + + $context = context_module::instance($cm->id); + self::validate_context($context); + require_capability('mod/lti:view', $context); + + // Trigger course_module_viewed event and completion. + lti_view($lti, $course, $cm, $context); + + $result = array(); + $result['status'] = true; + $result['warnings'] = $warnings; + return $result; + } + + /** + * Returns description of method result value + * + * @return external_description + * @since Moodle 3.0 + */ + public static function view_lti_returns() { + return new external_single_structure( + array( + 'status' => new external_value(PARAM_BOOL, 'status: true if success'), + 'warnings' => new external_warnings() + ) + ); + } } diff --git a/mod/lti/db/services.php b/mod/lti/db/services.php index cc0ef4e9465..aeee9b8c256 100644 --- a/mod/lti/db/services.php +++ b/mod/lti/db/services.php @@ -41,5 +41,13 @@ $functions = array( no courses are provided then all the external tool instances the user has access to will be returned.', 'type' => 'read', 'capabilities' => 'mod/lti:view' - ) + ), + + 'mod_lti_view_lti' => array( + 'classname' => 'mod_lti_external', + 'methodname' => 'view_lti', + 'description' => 'Trigger the course module viewed event and update the module completion status.', + 'type' => 'write', + 'capabilities' => 'mod/lti:view' + ), ); diff --git a/mod/lti/launch.php b/mod/lti/launch.php index d06cb134028..56f25af8d2a 100644 --- a/mod/lti/launch.php +++ b/mod/lti/launch.php @@ -61,19 +61,8 @@ $context = context_module::instance($cm->id); require_login($course, true, $cm); require_capability('mod/lti:view', $context); -// Mark viewed by user (if required). -$completion = new completion_info($course); -$completion->set_module_viewed($cm); - -$params = array( - 'context' => $context, - 'objectid' => $lti->id -); -$event = \mod_lti\event\course_module_viewed::create($params); -$event->add_record_snapshot('course_modules', $cm); -$event->add_record_snapshot('course', $course); -$event->add_record_snapshot('lti', $lti); -$event->trigger(); +// Completion and trigger events. +lti_view($lti, $course, $cm, $context); $lti->cmid = $cm->id; lti_launch_tool($lti); diff --git a/mod/lti/lib.php b/mod/lti/lib.php index ae723ee2da4..cf9df60646d 100644 --- a/mod/lti/lib.php +++ b/mod/lti/lib.php @@ -506,3 +506,31 @@ function lti_get_post_actions() { function lti_get_view_actions() { return array('view all', 'view'); } + +/** + * Mark the activity completed (if required) and trigger the course_module_viewed event. + * + * @param stdClass $lti lti object + * @param stdClass $course course object + * @param stdClass $cm course module object + * @param stdClass $context context object + * @since Moodle 3.0 + */ +function lti_view($lti, $course, $cm, $context) { + + // Trigger course_module_viewed event. + $params = array( + 'context' => $context, + 'objectid' => $lti->id + ); + + $event = \mod_lti\event\course_module_viewed::create($params); + $event->add_record_snapshot('course_modules', $cm); + $event->add_record_snapshot('course', $course); + $event->add_record_snapshot('lti', $lti); + $event->trigger(); + + // Completion. + $completion = new completion_info($course); + $completion->set_module_viewed($cm); +} diff --git a/mod/lti/tests/externallib_test.php b/mod/lti/tests/externallib_test.php index 9943fe6d60e..0fab2260393 100644 --- a/mod/lti/tests/externallib_test.php +++ b/mod/lti/tests/externallib_test.php @@ -216,4 +216,63 @@ class mod_lti_external_testcase extends externallib_advanced_testcase { $this->assertFalse(isset($ltis['ltis'][0]['intro'])); } + /** + * Test view_lti + */ + public function test_view_lti() { + global $DB; + + // Test invalid instance id. + try { + mod_lti_external::view_lti(0); + $this->fail('Exception expected due to invalid mod_lti instance id.'); + } catch (moodle_exception $e) { + $this->assertEquals('invalidrecord', $e->errorcode); + } + + // Test not-enrolled user. + $usernotenrolled = self::getDataGenerator()->create_user(); + $this->setUser($usernotenrolled); + try { + mod_lti_external::view_lti($this->lti->id); + $this->fail('Exception expected due to not enrolled user.'); + } catch (moodle_exception $e) { + $this->assertEquals('requireloginerror', $e->errorcode); + } + + // Test user with full capabilities. + $this->setUser($this->student); + + // Trigger and capture the event. + $sink = $this->redirectEvents(); + + $result = mod_lti_external::view_lti($this->lti->id); + $result = external_api::clean_returnvalue(mod_lti_external::view_lti_returns(), $result); + + $events = $sink->get_events(); + $this->assertCount(1, $events); + $event = array_shift($events); + + // Checking that the event contains the expected values. + $this->assertInstanceOf('\mod_lti\event\course_module_viewed', $event); + $this->assertEquals($this->context, $event->get_context()); + $moodlelti = new \moodle_url('/mod/lti/view.php', array('id' => $this->cm->id)); + $this->assertEquals($moodlelti, $event->get_url()); + $this->assertEventContextNotUsed($event); + $this->assertNotEmpty($event->get_name()); + + // 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/lti:view', CAP_PROHIBIT, $this->studentrole->id, $this->context->id); + accesslib_clear_all_caches_for_unit_testing(); + + try { + mod_lti_external::view_lti($this->lti->id); + $this->fail('Exception expected due to missing capability.'); + } catch (moodle_exception $e) { + $this->assertEquals('nopermissions', $e->errorcode); + } + + } + } diff --git a/mod/lti/tests/lib_test.php b/mod/lti/tests/lib_test.php new file mode 100644 index 00000000000..f154683f50e --- /dev/null +++ b/mod/lti/tests/lib_test.php @@ -0,0 +1,92 @@ +. + +/** + * Unit tests for mod_lti lib + * + * @package mod_lti + * @category external + * @copyright 2015 Juan Leyva + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @since Moodle 3.0 + */ + +defined('MOODLE_INTERNAL') || die(); + + +/** + * Unit tests for mod_lti lib + * + * @package mod_lti + * @category external + * @copyright 2015 Juan Leyva + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @since Moodle 3.0 + */ +class mod_lti_lib_testcase extends advanced_testcase { + + /** + * Prepares things before this test case is initialised + * @return void + */ + public static function setUpBeforeClass() { + global $CFG; + require_once($CFG->dirroot . '/mod/lti/lib.php'); + } + + /** + * Test lti_view + * @return void + */ + public function test_lti_view() { + global $CFG; + + $CFG->enablecompletion = 1; + $this->resetAfterTest(); + + $this->setAdminUser(); + // Setup test data. + $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1)); + $lti = $this->getDataGenerator()->create_module('lti', array('course' => $course->id), + array('completion' => 2, 'completionview' => 1)); + $context = context_module::instance($lti->cmid); + $cm = get_coursemodule_from_instance('lti', $lti->id); + + // Trigger and capture the event. + $sink = $this->redirectEvents(); + + lti_view($lti, $course, $cm, $context); + + $events = $sink->get_events(); + // 2 additional events thanks to completion. + $this->assertCount(3, $events); + $event = array_shift($events); + + // Checking that the event contains the expected values. + $this->assertInstanceOf('\mod_lti\event\course_module_viewed', $event); + $this->assertEquals($context, $event->get_context()); + $moodleurl = new \moodle_url('/mod/lti/view.php', array('id' => $cm->id)); + $this->assertEquals($moodleurl, $event->get_url()); + $this->assertEventContextNotUsed($event); + $this->assertNotEmpty($event->get_name()); + + // Check completion status. + $completion = new completion_info($course); + $completiondata = $completion->get_data($cm); + $this->assertEquals(1, $completiondata->completionstate); + + } +} diff --git a/mod/lti/version.php b/mod/lti/version.php index f4b5298836d..28f813e2797 100644 --- a/mod/lti/version.php +++ b/mod/lti/version.php @@ -48,7 +48,7 @@ defined('MOODLE_INTERNAL') || die; -$plugin->version = 2015100700; // The current module version (Date: YYYYMMDDXX). +$plugin->version = 2015100701; // The current module version (Date: YYYYMMDDXX). $plugin->requires = 2015050500; // Requires this Moodle version. $plugin->component = 'mod_lti'; // Full name of the plugin (used for diagnostics). $plugin->cron = 0; diff --git a/version.php b/version.php index 40d823bd9f2..dccf1525404 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2015100700.00; // YYYYMMDD = weekly release date of this DEV branch. +$version = 2015100700.01; // YYYYMMDD = weekly release date of this DEV branch. // RR = release increments - 00 in DEV branches. // .XX = incremental changes. -- 2.43.0