From d3496e3ff65d188effa03637274eedd1373a6562 Mon Sep 17 00:00:00 2001 From: Mark Nielsen Date: Wed, 21 May 2014 11:05:45 -0700 Subject: [PATCH] MDL-44902: Remove custom LTI hook API --- mod/lti/classes/factory.php | 118 ------------------ .../classes/observer/before_launch_event.php | 69 ---------- mod/lti/classes/observer/dispatcher.php | 87 ------------- mod/lti/classes/observer/events.php | 44 ------- .../classes/observer/listener_interface.php | 47 ------- mod/lti/locallib.php | 15 ++- 6 files changed, 9 insertions(+), 371 deletions(-) delete mode 100644 mod/lti/classes/factory.php delete mode 100644 mod/lti/classes/observer/before_launch_event.php delete mode 100644 mod/lti/classes/observer/dispatcher.php delete mode 100644 mod/lti/classes/observer/events.php delete mode 100644 mod/lti/classes/observer/listener_interface.php diff --git a/mod/lti/classes/factory.php b/mod/lti/classes/factory.php deleted file mode 100644 index b880150fe48..00000000000 --- a/mod/lti/classes/factory.php +++ /dev/null @@ -1,118 +0,0 @@ -. - -/** - * Class Builder - * - * @package mod - * @subpackage lti - * @copyright Copyright (c) 2009 Moodlerooms Inc. (http://www.moodlerooms.com) - * @license http://opensource.org/licenses/gpl-3.0.html GNU Public License - */ - -namespace mod_lti; - -use coding_exception; -use mod_lti\observer\dispatcher; - -/** - * Builds various classes - * - * @package mod_lti - * @copyright Copyright (c) 2009 Moodlerooms Inc. (http://www.moodlerooms.com) - * @license http://opensource.org/licenses/gpl-3.0.html GNU Public License - */ -class factory { - /** - * Given a component and class name suffix, create a full - * class name and ensure that it exists. - * - * Classes are namespace based. - * - * @param string $component The component to find the class file in - * @param string $suffix This is appended to the component name, together make the class name we want - * @return string - * @throws \coding_exception - */ - protected function build_class_name($component, $suffix) { - $classname = "\\$component\\$suffix"; - if (!class_exists($classname)) { - throw new coding_exception("Expected to find $classname in the classes directory of $component"); - } - return $classname; - } - - /** - * Instantiate a class and optionally verify parent class - * - * @param string $class Create a new instance of this class - * @param null|string $parent Ensure that this is the parent class - * @return mixed - * @throws coding_exception - */ - protected function build_generic_instance($class, $parent = null) { - if (!is_null($parent)) { - $reflection = new \ReflectionClass($class); - if (!$reflection->isSubclassOf($parent)) { - throw new coding_exception("The $class must be a subclass of $parent"); - } - } - return new $class(); - } - - /** - * Builds a single ltisource plugin listener - * - * @param string $component The component to find the class file in - * @return \mod_lti\observer\listener_interface - */ - public function build_listener($component) { - return $this->build_generic_instance( - $this->build_class_name($component, 'listener'), - '\mod_lti\observer\listener_interface' - ); - } - - /** - * Builds ltisource plugin listeners - * - * @return \mod_lti\observer\listener_interface[] - */ - public function build_listeners() { - $plugins = \core_component::get_plugin_list('ltisource'); - $listeners = array(); - foreach (array_keys($plugins) as $pluginname) { - try { - $listeners[] = $this->build_listener('ltisource_'.$pluginname); - } catch (\Exception $e) { - // Class is optional, so ignore if not found. - } - } - return $listeners; - } - - /** - * Builds an event dispatcher with listeners. - * - * @return dispatcher - */ - public function build_dispatcher() { - $dispatcher = new dispatcher(); - $dispatcher->set_listeners($this->build_listeners()); - - return $dispatcher; - } -} \ No newline at end of file diff --git a/mod/lti/classes/observer/before_launch_event.php b/mod/lti/classes/observer/before_launch_event.php deleted file mode 100644 index 2ca2b35a3a9..00000000000 --- a/mod/lti/classes/observer/before_launch_event.php +++ /dev/null @@ -1,69 +0,0 @@ -. - -/** - * Event for ltisource plugins. - * - * @package mod - * @subpackage lti - * @copyright Copyright (c) 2009 Moodlerooms Inc. (http://www.moodlerooms.com) - * @license http://opensource.org/licenses/gpl-3.0.html GNU Public License - */ - -namespace mod_lti\observer; - -/** - * This event occurs prior to an LTI launch. - * - * @package mod_lti - * @copyright Copyright (c) 2009 Moodlerooms Inc. (http://www.moodlerooms.com) - * @license http://opensource.org/licenses/gpl-3.0.html GNU Public License - */ -class before_launch_event { - /** - * LTI activity instance - * - * @var \stdClass - */ - public $instance; - - /** - * Launch URL - * - * @var string - */ - public $endpoint; - - /** - * Launch request parameters - * - * @var array - */ - public $params; - - /** - * Constructor - * - * @param \stdClass $instance - * @param string $endpoint - * @param array $params - */ - public function __construct($instance, $endpoint, array $params) { - $this->instance = $instance; - $this->endpoint = $endpoint; - $this->params = $params; - } -} \ No newline at end of file diff --git a/mod/lti/classes/observer/dispatcher.php b/mod/lti/classes/observer/dispatcher.php deleted file mode 100644 index f438022ad73..00000000000 --- a/mod/lti/classes/observer/dispatcher.php +++ /dev/null @@ -1,87 +0,0 @@ -. - -/** - * Dispatches events to ltisource plugin listeners - * - * @package mod - * @subpackage lti - * @copyright Copyright (c) 2009 Moodlerooms Inc. (http://www.moodlerooms.com) - * @license http://opensource.org/licenses/gpl-3.0.html GNU Public License - */ - -namespace mod_lti\observer; - -/** - * Event dispatcher - * - * @package mod_lti - * @copyright Copyright (c) 2009 Moodlerooms Inc. (http://www.moodlerooms.com) - * @license http://opensource.org/licenses/gpl-3.0.html GNU Public License - */ -class dispatcher { - /** - * @var listener_interface[] - */ - protected $listeners = array(); - - /** - * Add a listener - * - * @param listener_interface $listener - */ - public function add_listener(listener_interface $listener) { - $this->listeners[] = $listener; - } - - /** - * Set a list of listeners - * - * @param listener_interface[] $listeners - */ - public function set_listeners($listeners) { - $this->listeners = array(); - foreach ($listeners as $listener) { - $this->add_listener($listener); - } - } - - /** - * Dispatches an event. - * - * Very trivial right now. - * - * @param string $name Event name - * @param mixed $event The event object - * @throws \coding_exception - */ - public function dispatch($name, $event) { - foreach ($this->listeners as $listener) { - $subscribed = $listener->get_subscribed_events(); - - if (!array_key_exists($name, $subscribed)) { - continue; - } - $callable = array($listener, $subscribed[$name]); - if (!is_callable($callable)) { - throw new \coding_exception( - sprintf('The method %s is not callable on %s class', $subscribed[$name], get_class($listener)) - ); - } - call_user_func($callable, $event); - } - } -} \ No newline at end of file diff --git a/mod/lti/classes/observer/events.php b/mod/lti/classes/observer/events.php deleted file mode 100644 index 8e95dbe9c60..00000000000 --- a/mod/lti/classes/observer/events.php +++ /dev/null @@ -1,44 +0,0 @@ -. - -/** - * Defines internal events that ltisource - * plugins can subscribe to. - * - * @package mod - * @subpackage lti - * @copyright Copyright (c) 2009 Moodlerooms Inc. (http://www.moodlerooms.com) - * @license http://opensource.org/licenses/gpl-3.0.html GNU Public License - */ - -namespace mod_lti\observer; - -/** - * These are all of the event names - * - * @package mod_lti - * @copyright Copyright (c) 2009 Moodlerooms Inc. (http://www.moodlerooms.com) - * @license http://opensource.org/licenses/gpl-3.0.html GNU Public License - */ -final class events { - /** - * This is thrown before an LTI launch - * - * The event listener will recevie an instance - * of \mod_lti\observer\before_launch_event - */ - const BEFORE_LAUNCH = 'before.launch'; -} \ No newline at end of file diff --git a/mod/lti/classes/observer/listener_interface.php b/mod/lti/classes/observer/listener_interface.php deleted file mode 100644 index 72760ffa7f2..00000000000 --- a/mod/lti/classes/observer/listener_interface.php +++ /dev/null @@ -1,47 +0,0 @@ -. - -/** - * Listener interface for ltisource plugins - * - * @package mod - * @subpackage lti - * @copyright Copyright (c) 2009 Moodlerooms Inc. (http://www.moodlerooms.com) - * @license http://opensource.org/licenses/gpl-3.0.html GNU Public License - */ - -namespace mod_lti\observer; - -/** - * This interface allows a class to define the ltisource - * plugin events that the class would like to subscribe - * to - * - * @package mod_lti - * @copyright Copyright (c) 2009 Moodlerooms Inc. (http://www.moodlerooms.com) - * @license http://opensource.org/licenses/gpl-3.0.html GNU Public License - */ -interface listener_interface { - /** - * Register for events - * - * Example return: - * array('eventname' => 'methodToCall'); - * - * @return array - */ - public function get_subscribed_events(); -} \ No newline at end of file diff --git a/mod/lti/locallib.php b/mod/lti/locallib.php index 2bf89d2834e..68e9dbd6e8f 100644 --- a/mod/lti/locallib.php +++ b/mod/lti/locallib.php @@ -194,13 +194,16 @@ function lti_view($instance) { $requestparams['launch_presentation_return_url'] = $returnurl; - $factory = new \mod_lti\factory(); - $dispatcher = $factory->build_dispatcher(); - $event = new \mod_lti\observer\before_launch_event($instance, $endpoint, $requestparams); - $dispatcher->dispatch(\mod_lti\observer\events::BEFORE_LAUNCH, $event); + // Allow request params to be updated by sub-plugins. + $plugins = core_component::get_plugin_list('ltisource'); + foreach (array_keys($plugins) as $plugin) { + $pluginparams = component_callback('ltisource_'.$plugin, 'before_launch', + array($instance, $endpoint, $requestparams), array()); - // Allow params to be updated. - $requestparams = $event->params; + if (!empty($pluginparams) && is_array($pluginparams)) { + $requestparams = array_merge($requestparams, $pluginparams); + } + } if (!empty($key) && !empty($secret)) { $parms = lti_sign_parameters($requestparams, $endpoint, "POST", $key, $secret);