MDL-52154 mod_lti: Add cartridge support to LTI

This commit is contained in:
John Okely 2015-11-18 10:47:11 +08:00
parent 6c1342d7e5
commit 8aec804af2
2 changed files with 161 additions and 0 deletions

View File

@ -25,6 +25,7 @@
require_once('../../config.php');
require_once($CFG->dirroot.'/mod/lti/edit_form.php');
require_once($CFG->dirroot.'/mod/lti/lib.php');
$courseid = required_param('course', PARAM_INT);
@ -56,6 +57,8 @@ if ($data = $form->get_data()) {
$type->id = $typeid;
$name = json_encode($data->lti_typename);
lti_load_type_if_cartridge($data);
lti_update_type($type, $data);
$fromdb = lti_get_type($typeid);
@ -77,6 +80,8 @@ if ($data = $form->get_data()) {
$type->state = LTI_TOOL_STATE_CONFIGURED;
$type->course = $COURSE->id;
lti_load_type_if_cartridge($data);
$id = lti_add_type($type, $data);
$fromdb = lti_get_type($id);

View File

@ -97,6 +97,8 @@ function lti_add_instance($lti, $mform) {
$lti->toolurl = '';
}
lti_load_tool_if_cartridge($lti);
$lti->timecreated = time();
$lti->timemodified = $lti->timecreated;
$lti->servicesalt = uniqid('', true);
@ -137,6 +139,8 @@ function lti_update_instance($lti, $mform) {
global $DB, $CFG;
require_once($CFG->dirroot.'/mod/lti/locallib.php');
lti_load_tool_if_cartridge($lti);
$lti->timemodified = time();
$lti->id = $lti->instance;
@ -542,3 +546,155 @@ function lti_view($lti, $course, $cm, $context) {
$completion = new completion_info($course);
$completion->set_module_viewed($cm);
}
/**
* Loads the cartridge information into the tool type, if the launch url is for a cartridge file
*
* @param stdClass $type The tool type object to be filled in
* @since Moodle 3.1
*/
function lti_load_type_if_cartridge($type) {
if (lti_is_cartridge($type->lti_toolurl)) {
lti_load_type_from_cartridge($type->lti_toolurl, $type);
}
}
/**
* Loads the cartridge information into the new tool, if the launch url is for a cartridge file
*
* @param stdClass $lti The tools config
* @since Moodle 3.1
*/
function lti_load_tool_if_cartridge($lti) {
if (lti_is_cartridge($lti->toolurl)) {
lti_load_tool_from_cartridge($lti->toolurl, $lti);
}
}
/**
* Determines if the given url is for a IMS basic cartridge
*
* @param string $type The tool type object to be filled in
* @return True if the url is for a cartridge
* @since Moodle 3.1
*/
function lti_is_cartridge($url) {
// If it has xml at the end of the url, it's a cartridge.
if (preg_match('/\.xml$/', $url)) {
return true;
}
// Even if it doesn't have .xml, load the url to check if it's a cartridge..
$toolinfo = lti_load_cartridge($url,
array(
"launch_url" => "launchurl"
)
);
if (!empty($toolinfo['launchurl'])) {
return true;
}
return false;
}
/**
* Allows you to load settings for an external tool type from an IMS cartridge.
*
* @param string $url The URL to the cartridge
* @param stdClass $type The tool type object to be filled in
* @since Moodle 3.1
*/
function lti_load_type_from_cartridge($url, $type) {
$toolinfo = lti_load_cartridge($url,
array(
"title" => "lti_typename",
"launch_url" => "lti_toolurl",
"secure_launch_url" => "lti_securltoolurl",
"description" => "lti_description",
),
array(
"icon_url" => "lti_icon"
)
);
foreach($toolinfo as $property => $value) {
$type->$property = $value;
}
}
/**
* Allows you to load in the configuration for an external tool from an IMS cartridge.
*
* @param string $url The URL to the cartridge
* @param stdClass $lti LTI object
* @since Moodle 3.1
*/
function lti_load_tool_from_cartridge($url, $lti) {
$toolinfo = lti_load_cartridge($url,
array(
"launch_url" => "toolurl",
"secure_launch_url" => "securetoolurl",
"title" => "name"
)
);
foreach($toolinfo as $property => $value) {
$lti->$property = $value;
}
}
/**
* Search for a tag within an XML DOMDocument
*
* @param stdClass $tagName The name of the tag to search for
* @param array $map The map of tags to keys in the return array
* @param array $properties The map of properties to keys in the return array
* @return array An associative array with the given keys and their values from the cartridge
* @since Moodle 3.1
*/
function lti_load_cartridge($url, $map, $propertiesmap = array()) {
global $CFG;
require_once($CFG->libdir. "/filelib.php");
$curl = new curl();
$response = $curl->get($url);
$document = new DOMDocument();
$document->loadXML($response);
$cartridge = new DomXpath($document);
$errors = libxml_get_errors();
foreach ($errors as $error) {
print_error(sprintf("%s at line %d. ", trim($error->message, "\n\r\t ."), $error->line));
}
$toolinfo = array();
foreach ($map as $tag => $key) {
$value = get_tag($tag, $cartridge);
if ($value) {
$toolinfo[$key] = $value;
}
}
if (!empty($propertiesmap)) {
foreach ($propertiesmap as $property => $key) {
$value = get_tag("property", $cartridge, $property);
error_log("\n\nProperty is: " . $value . "\n\n");
if ($value) {
$toolinfo[$key] = $value;
}
}
}
return $toolinfo;
}
/**
* Search for a tag within an XML DOMDocument
*
* @param stdClass $tagName The name of the tag to search for
* @param XPath $xpath The XML to find the tag in
* @since Moodle 3.1
*/
function get_tag($tagname, $xpath, $attribute = null) {
if ($attribute) {
$result = $xpath->query('//*[local-name() = \'' . $tagname . '\'][@name="' . $attribute . '"]');
} else {
$result = $xpath->query('//*[local-name() = \'' . $tagname . '\']');
}
if ($result->length > 0) {
return $result->item(0)->nodeValue;
}
return null;
}