MDL-51624 mod_lti: New Web Service mod_lti_view_lti

This commit is contained in:
Juan Leyva
2015-10-07 14:54:18 +02:00
parent c4c838ae82
commit 4bed1682bb
9 changed files with 256 additions and 16 deletions
+1
View File
@@ -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',
),
+63
View File
@@ -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()
)
);
}
}
+9 -1
View File
@@ -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'
),
);
+2 -13
View File
@@ -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);
+28
View File
@@ -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);
}
+59
View File
@@ -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);
}
}
}
+92
View File
@@ -0,0 +1,92 @@
<?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/>.
/**
* Unit tests for mod_lti lib
*
* @package mod_lti
* @category external
* @copyright 2015 Juan Leyva <[email protected]>
* @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 <[email protected]>
* @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);
}
}
+1 -1
View File
@@ -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;
+1 -1
View File
@@ -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.