MDL-44902: Several additions to External Tool (LTI)

* LTI service related changes:
** Fixing exceptions in OAuth library.
** Added new launch option, Existing window: replaces entire page with the LTI object.
** The LTI tool ID used to perform the launch is now sent with the LTI launch parameters.  This is sent back to Moodle on subsequent requests.
** Added $CFG->mod_lti_forcessl to force SSL on all LTI launches.
** Added new LTI launch parameter: tool_consumer_instance_name.  Default value is site full name, but can be customized with $CFG->mod_lti_institution_name.
** The LTI grade service endpoints now set the affected user to the session.  This was required for event listeners.
** Fix the grade deletion service.  Was deleting the grade item instead of just the grade.
** Send error response when LTI instance does not accept grades and grades are being sent.
** Added a method for writing incoming LTI requests to disk for debugging.  Disabled by default.
* Changes for ltisource plugins:
** Can now to plug into backup/restore.
** Can now have settings.php files.
** Can now hook into the LTI launch and edit parameters.
* Several grade changes:
** Added standard_grading_coursemodule_elements to LTI instance edit form.  This means LTI instances can be configured with a grade.
** No longer assumes that grade is out of 100.
** Replaced modl/lti:grade capability with mod/lti:view.
* JS on mod/lti/view.php for resizing the content object has been converted to YUI3.
* Fixed misspellings in language file.
* Added hooks for log post and view actions.
* Bug fix for lti_get_url_thumbprint() when the URL is missing a schema.
This commit is contained in:
Mark Nielsen 2014-04-01 15:07:54 -07:00
parent f500ff4e52
commit 8fa50fdd34
23 changed files with 759 additions and 74 deletions

118
mod/lti/classes/factory.php Normal file
View file

@ -0,0 +1,118 @@
<?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/>.
/**
* 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;
}
}

View file

@ -0,0 +1,69 @@
<?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/>.
/**
* 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;
}
}

View file

@ -0,0 +1,87 @@
<?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/>.
/**
* 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);
}
}
}

View file

@ -0,0 +1,44 @@
<?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/>.
/**
* 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';
}

View file

@ -0,0 +1,47 @@
<?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/>.
/**
* 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();
}