diff --git a/lib/db/services.php b/lib/db/services.php index 84b9be16f3b..4de6b784bcb 100644 --- a/lib/db/services.php +++ b/lib/db/services.php @@ -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', ), diff --git a/mod/lti/classes/external.php b/mod/lti/classes/external.php new file mode 100644 index 00000000000..52ad37fddf9 --- /dev/null +++ b/mod/lti/classes/external.php @@ -0,0 +1,125 @@ +. + +/** + * External tool module external API + * + * @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; + +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 + * @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') + ) + ); + } + + /** + * Return the launch data for a given external tool. + * + * @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() + ) + ); + } +} diff --git a/mod/lti/db/services.php b/mod/lti/db/services.php new file mode 100644 index 00000000000..49f42f55944 --- /dev/null +++ b/mod/lti/db/services.php @@ -0,0 +1,37 @@ +. + +/** + * External tool external functions and service definitions. + * + * @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 + */ + +$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' + ), + +); diff --git a/mod/lti/locallib.php b/mod/lti/locallib.php index aa718d9a46b..79a46076e2a 100644 --- a/mod/lti/locallib.php +++ b/mod/lti/locallib.php @@ -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); diff --git a/mod/lti/tests/externallib_test.php b/mod/lti/tests/externallib_test.php new file mode 100644 index 00000000000..92915281117 --- /dev/null +++ b/mod/lti/tests/externallib_test.php @@ -0,0 +1,99 @@ +. + +/** + * External tool module external functions tests + * + * @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(); + +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 + * @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']); + + } + +} diff --git a/mod/lti/version.php b/mod/lti/version.php index 139b7e911d1..bfb386b3ecb 100644 --- a/mod/lti/version.php +++ b/mod/lti/version.php @@ -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; diff --git a/version.php b/version.php index 1f58f2433a6..9e73323cff1 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2015100600.00; // YYYYMMDD = weekly release date of this DEV branch. +$version = 2015100601.00; // YYYYMMDD = weekly release date of this DEV branch. // RR = release increments - 00 in DEV branches. // .XX = incremental changes.