Skip to content

Commit

Permalink
MDL-51624 mod_lti: New Web Service mod_lti_view_lti
Browse files Browse the repository at this point in the history
  • Loading branch information
jleyva committed Oct 7, 2015
1 parent c4c838a commit 4bed168
Show file tree
Hide file tree
Showing 9 changed files with 256 additions and 16 deletions.
1 change: 1 addition & 0 deletions lib/db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -1248,6 +1248,7 @@
'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',
),
Expand Down
63 changes: 63 additions & 0 deletions mod/lti/classes/external.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,67 @@ public static function get_ltis_by_courses_returns() {
)
);
}

/**
* 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()
)
);
}
}
10 changes: 9 additions & 1 deletion mod/lti/db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,13 @@
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'
),
);
15 changes: 2 additions & 13 deletions mod/lti/launch.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,8 @@
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);
Expand Down
28 changes: 28 additions & 0 deletions mod/lti/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 59 additions & 0 deletions mod/lti/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,63 @@ public function test_mod_lti_get_ltis_by_courses() {
$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 changes: 92 additions & 0 deletions mod/lti/tests/lib_test.php
Original file line number Diff line number Diff line change
@@ -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 <juan@moodle.com>
* @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 <juan@moodle.com>
* @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);

}
}
2 changes: 1 addition & 1 deletion mod/lti/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down

0 comments on commit 4bed168

Please sign in to comment.