MDL-51615 mod_lti: New Web Service mod_lti_get_tool_launch_data

This commit is contained in:
Juan Leyva 2015-09-30 14:17:08 +02:00
parent 35d3e8b00b
commit d5ded9e72d
7 changed files with 281 additions and 5 deletions

View File

@ -1237,6 +1237,7 @@ $services = array(
'mod_choice_submit_choice_response',
'mod_choice_view_choice',
'mod_choice_get_choices_by_courses',
'mod_lti_get_tool_launch_data',
'mod_imscp_view_imscp',
'mod_imscp_get_imscps_by_courses',
),

View File

@ -0,0 +1,125 @@
<?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 tool module external API
*
* @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;
require_once($CFG->libdir . '/externallib.php');
require_once($CFG->dirroot . '/mod/lti/lib.php');
require_once($CFG->dirroot . '/mod/lti/locallib.php');
/**
* External tool module external functions
*
* @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_external extends external_api {
/**
* Returns description of method parameters
*
* @return external_function_parameters
* @since Moodle 3.0
*/
public static function get_tool_launch_data_parameters() {
return new external_function_parameters(
array(
'toolid' => new external_value(PARAM_INT, 'external tool instance id')
)
);
}
/**
* Trigger the course module viewed event.
*
* @param int $toolid the external tool instance id
* @return array of warnings and launch data
* @since Moodle 3.0
* @throws moodle_exception
*/
public static function get_tool_launch_data($toolid) {
global $DB, $CFG;
require_once($CFG->dirroot . '/mod/lti/lib.php');
$params = self::validate_parameters(self::get_tool_launch_data_parameters(),
array(
'toolid' => $toolid
));
$warnings = array();
// Request and permission validation.
$lti = $DB->get_record('lti', array('id' => $params['toolid']), '*', 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);
$lti->cmid = $cm->id;
list($endpoint, $parms) = lti_get_launch_data($lti);
$parameters = array();
foreach ($parms as $name => $value) {
$parameters[] = array(
'name' => $name,
'value' => $value
);
}
$result = array();
$result['endpoint'] = $endpoint;
$result['parameters'] = $parameters;
$result['warnings'] = $warnings;
return $result;
}
/**
* Returns description of method result value
*
* @return external_description
* @since Moodle 3.0
*/
public static function get_tool_launch_data_returns() {
return new external_single_structure(
array(
'endpoint' => new external_value(PARAM_RAW, 'Endpoint URL'), // Using PARAM_RAW as is defined in the module.
'parameters' => new external_multiple_structure(
new external_single_structure(
array(
'name' => new external_value(PARAM_NOTAGS, 'Parameter name'),
'value' => new external_value(PARAM_RAW, 'Parameter value')
)
)
),
'warnings' => new external_warnings()
)
);
}
}

37
mod/lti/db/services.php Normal file
View File

@ -0,0 +1,37 @@
<?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 tool external functions and service definitions.
*
* @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
*/
$functions = array(
'mod_lti_get_tool_launch_data' => array(
'classname' => 'mod_lti_external',
'methodname' => 'get_tool_launch_data',
'description' => 'Return the launch data for a given external tool.',
'type' => 'read',
'capabilities' => 'mod/lti:view'
),
);

View File

@ -79,11 +79,13 @@ define('LTI_SETTING_ALWAYS', 1);
define('LTI_SETTING_DELEGATE', 2);
/**
* Prints a Basic LTI activity
* Return the launch data required for opening the external tool.
*
* $param int $basicltiid Basic LTI activity id
* @param stdClass $instance the external tool activity settings
* @return array the endpoint URL and parameters (including the signature)
* @since Moodle 3.0
*/
function lti_view($instance) {
function lti_get_launch_data($instance) {
global $PAGE, $CFG;
if (empty($instance->typeid)) {
@ -245,6 +247,18 @@ function lti_view($instance) {
$parms = $requestparams;
}
return array($endpoint, $parms);
}
/**
* Prints an external tool activity.
*
* @param stdClass $instance the external tool activity settings
* @return string The HTML post form content
*/
function lti_view($instance) {
list($endpoint, $parms) = lti_get_launch_data($instance);
$debuglaunch = ( $instance->debuglaunch == 1 );
$content = lti_post_launch_html($parms, $endpoint, $debuglaunch);

View File

@ -0,0 +1,99 @@
<?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 tool module external functions tests
*
* @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();
global $CFG;
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
require_once($CFG->dirroot . '/mod/lti/lib.php');
/**
* External tool module external functions tests
*
* @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_external_testcase extends externallib_advanced_testcase {
/**
* Set up for every test
*/
public function setUp() {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();
// Setup test data.
$this->course = $this->getDataGenerator()->create_course();
$this->lti = $this->getDataGenerator()->create_module('lti', array('course' => $this->course->id));
$this->context = context_module::instance($this->lti->cmid);
$this->cm = get_coursemodule_from_instance('lti', $this->lti->id);
// Create users.
$this->student = self::getDataGenerator()->create_user();
$this->teacher = self::getDataGenerator()->create_user();
// Users enrolments.
$this->studentrole = $DB->get_record('role', array('shortname' => 'student'));
$this->teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
$this->getDataGenerator()->enrol_user($this->student->id, $this->course->id, $this->studentrole->id, 'manual');
$this->getDataGenerator()->enrol_user($this->teacher->id, $this->course->id, $this->teacherrole->id, 'manual');
}
/**
* Test view_lti
*/
public function test_get_tool_launch_data() {
global $USER;
$result = mod_lti_external::get_tool_launch_data($this->lti->id);
$result = external_api::clean_returnvalue(mod_lti_external::get_tool_launch_data_returns(), $result);
// Basic test, the function returns what it's expected.
self::assertEquals($this->lti->toolurl, $result['endpoint']);
self::assertCount(35, $result['parameters']);
// Check some parameters.
$parameters = array();
foreach ($result['parameters'] as $param) {
$parameters[$param['name']] = $param['value'];
}
self::assertEquals($this->lti->resourcekey, $parameters['oauth_consumer_key']);
self::assertEquals($this->course->fullname, $parameters['context_title']);
self::assertEquals($this->course->shortname, $parameters['context_label']);
self::assertEquals($USER->id, $parameters['user_id']);
self::assertEquals($USER->firstname, $parameters['lis_person_name_given']);
self::assertEquals($USER->lastname, $parameters['lis_person_name_family']);
self::assertEquals(fullname($USER), $parameters['lis_person_name_full']);
self::assertEquals($USER->username, $parameters['ext_user_username']);
}
}

View File

@ -48,7 +48,7 @@
defined('MOODLE_INTERNAL') || die;
$plugin->version = 2015051100; // The current module version (Date: YYYYMMDDXX).
$plugin->version = 2015051101; // 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;

View File

@ -29,7 +29,7 @@
defined('MOODLE_INTERNAL') || die();
$version = 2015100200.00; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2015100200.01; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.