diff --git a/mod/lti/lib.php b/mod/lti/lib.php index c7e1b92d4e0..fed1e92d80c 100644 --- a/mod/lti/lib.php +++ b/mod/lti/lib.php @@ -172,6 +172,22 @@ function lti_delete_instance($id) { return $DB->delete_records("lti", array("id" => $basiclti->id)); } +function lti_get_types() { + $type = new stdClass(); + $type->modclass = MOD_CLASS_ACTIVITY; + $type->type = 'lti'; + $type->typestr = get_string('modulename', 'mod_lti'); + + $types = array($type); + + foreach (get_plugin_list('ltisource') as $name => $dir) { + if ($moretypes = component_callback("ltisource_$name", 'get_types')) { + $types = array_merge($types, $moretypes); + } + } + return $types; +} + /** * Given a coursemodule object, this function returns the extra * information needed to print this activity in various places. diff --git a/mod/lti/mod_form.php b/mod/lti/mod_form.php index aa5a7281c50..31c7f231277 100644 --- a/mod/lti/mod_form.php +++ b/mod/lti/mod_form.php @@ -57,6 +57,10 @@ class mod_lti_mod_form extends moodleform_mod { public function definition() { global $DB, $PAGE, $OUTPUT, $USER, $COURSE; + if ($type = optional_param('type', false, PARAM_ALPHA)) { + component_callback("ltisource_$type", 'add_instance_hook'); + } + $this->typeid = 0; $mform =& $this->_form; diff --git a/mod/lti/service.php b/mod/lti/service.php index a98db239a30..074da79c162 100644 --- a/mod/lti/service.php +++ b/mod/lti/service.php @@ -150,10 +150,16 @@ switch ($messagetype) { $eventdata = array(); $eventdata['other'] = array(); $eventdata['other']['body'] = $rawbody; + $eventdata['other']['messageid'] = lti_parse_message_id($xml); $eventdata['other']['messagetype'] = $messagetype; $eventdata['other']['consumerkey'] = $consumerkey; $eventdata['other']['sharedsecret'] = $sharedsecret; + // Before firing the event, allow subplugins a chance to handle. + if (lti_extend_lti_services((object) $eventdata['other'])) { + break; + } + //If an event handler handles the web service, it should set this global to true //So this code knows whether to send an "operation not supported" or not. global $lti_web_service_handled; diff --git a/mod/lti/servicelib.php b/mod/lti/servicelib.php index 2ce3731b996..232537f9109 100644 --- a/mod/lti/servicelib.php +++ b/mod/lti/servicelib.php @@ -223,3 +223,38 @@ function lti_verify_sourcedid($ltiinstance, $parsed) { throw new Exception('SourcedId hash not valid'); } } + +/** + * Extend the LTI services through the ltisource plugins + * + * @param stdClass $data LTI request data + * @return bool + * @throws coding_exception + */ +function lti_extend_lti_services($data) { + $plugins = get_plugin_list_with_function('ltisource', $data->messagetype); + if (!empty($plugins)) { + try { + // There can only be one + if (count($plugins) > 1) { + throw new coding_exception('More than one ltisource plugin handler found'); + } + $callback = current($plugins); + call_user_func($callback, $data); + } catch (moodle_exception $e) { + $error = $e->getMessage(); + if (debugging('', DEBUG_DEVELOPER)) { + $error .= ' '.format_backtrace(get_exception_info($e)->backtrace); + } + $responsexml = lti_get_response_xml( + 'failure', + $error, + $data->messageid, + $data->messagetype + ); + echo $responsexml->asXML(); + } + return true; + } + return false; +} \ No newline at end of file