first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
@@ -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/>.
/**
* Activity base class.
*
* @package mod_lti
* @copyright 2017 onwards Ankit Agarwal <ankit.agrr@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_lti\analytics\indicator;
defined('MOODLE_INTERNAL') || die();
/**
* Activity base class.
*
* @package mod_lti
* @copyright 2017 onwards Ankit Agarwal <ankit.agrr@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class activity_base extends \core_analytics\local\indicator\community_of_inquiry_activity {
/**
* feedback_viewed_events
*
* @return string[]
*/
protected function feedback_viewed_events() {
// Any view after the data graded counts as feedback viewed.
return array('\mod_lti\event\course_module_viewed');
}
}
@@ -0,0 +1,57 @@
<?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/>.
/**
* Cognitive depth indicator - lti.
*
* @package mod_lti
* @copyright 2017 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_lti\analytics\indicator;
defined('MOODLE_INTERNAL') || die();
/**
* Cognitive depth indicator - lti.
*
* @package mod_lti
* @copyright 2017 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class cognitive_depth extends activity_base {
/**
* Returns the name.
*
* If there is a corresponding '_help' string this will be shown as well.
*
* @return \lang_string
*/
public static function get_name(): \lang_string {
return new \lang_string('indicator:cognitivedepth', 'mod_lti');
}
public function get_indicator_type() {
return self::INDICATOR_COGNITIVE;
}
public function get_cognitive_depth_level(\cm_info $cm) {
return self::COGNITIVE_LEVEL_3;
}
}
@@ -0,0 +1,56 @@
<?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/>.
/**
* Social breadth indicator - lti.
*
* @package mod_lti
* @copyright 2017 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_lti\analytics\indicator;
defined('MOODLE_INTERNAL') || die();
/**
* Social breadth indicator - lti.
*
* @package mod_lti
* @copyright 2017 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class social_breadth extends activity_base {
/**
* Returns the name.
*
* If there is a corresponding '_help' string this will be shown as well.
*
* @return \lang_string
*/
public static function get_name(): \lang_string {
return new \lang_string('indicator:socialbreadth', 'mod_lti');
}
public function get_indicator_type() {
return self::INDICATOR_SOCIAL;
}
public function get_social_breadth_level(\cm_info $cm) {
return self::SOCIAL_LEVEL_2;
}
}
@@ -0,0 +1,40 @@
<?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/>.
/**
* The mod_lti instance list viewed event.
*
* @package mod_lti
* @copyright 2013 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_lti\event;
defined('MOODLE_INTERNAL') || die();
/**
* The mod_lti instance list viewed event class.
*
* @package mod_lti
* @since Moodle 2.7
* @copyright 2013 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class course_module_instance_list_viewed extends \core\event\course_module_instance_list_viewed {
// No need for any code here as everything is handled by the parent class.
}
@@ -0,0 +1,51 @@
<?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/>.
/**
* The mod_lti course module viewed event.
*
* @package mod_lti
* @copyright 2013 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_lti\event;
defined('MOODLE_INTERNAL') || die();
/**
* The mod_lti course module viewed event class.
*
* @package mod_lti
* @since Moodle 2.7
* @copyright 2013 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class course_module_viewed extends \core\event\course_module_viewed {
/**
* Init method.
*/
protected function init() {
$this->data['objecttable'] = 'lti';
$this->data['crud'] = 'r';
$this->data['edulevel'] = self::LEVEL_PARTICIPATING;
}
public static function get_objectid_mapping() {
return array('db' => 'lti', 'restore' => 'lti');
}
}
@@ -0,0 +1,90 @@
<?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/>.
/**
* The mod_lti unknown service api called event.
*
* @package mod_lti
* @copyright 2013 Adrian Greeve <adrian@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_lti\event;
defined('MOODLE_INTERNAL') || die();
/**
* The mod_lti unknown service api called event class.
*
* Event for when something happens with an unknown lti service API call.
*
* @package mod_lti
* @since Moodle 2.6
* @copyright 2013 Adrian Greeve <adrian@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class unknown_service_api_called extends \core\event\base {
/** @var \stdClass Data to be used by event observers. */
protected $eventdata;
/**
* Sets custom data used by event observers.
*
* @param \stdClass $data
*/
public function set_message_data(\stdClass $data) {
$this->eventdata = $data;
}
/**
* Returns custom data for event observers.
*
* @return \stdClass
*/
public function get_message_data() {
if ($this->is_restored()) {
throw new \coding_exception('Function get_message_data() can not be used on restored events.');
}
return $this->eventdata;
}
/**
* Init method.
*/
protected function init() {
$this->data['crud'] = 'r';
$this->data['edulevel'] = self::LEVEL_OTHER;
$this->context = \context_system::instance();
}
/**
* Returns localised description of what happened.
*
* @return string
*/
public function get_description() {
return 'An unknown call to a service api was made.';
}
/**
* Returns localised general event name.
*
* @return string
*/
public static function get_name() {
return get_string('ltiunknownserviceapicall', 'mod_lti');
}
}
+993
View File
@@ -0,0 +1,993 @@
<?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/>.
/**
* External tool module external API
*
* @package mod_lti
* @category external
* @copyright 2015 Juan Leyva <juan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 3.0
*/
use core_course\external\helper_for_get_mods_by_courses;
use core_external\external_api;
use core_external\external_function_parameters;
use core_external\external_multiple_structure;
use core_external\external_single_structure;
use core_external\external_value;
use core_external\external_warnings;
use core_external\util;
defined('MOODLE_INTERNAL') || die;
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 <juan@moodle.com>
* @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 structure be used for returning a tool type from a web service.
*
* @return external_function_parameters
* @since Moodle 3.1
*/
private static function tool_type_return_structure() {
return new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'Tool type id'),
'name' => new external_value(PARAM_NOTAGS, 'Tool type name'),
'description' => new external_value(PARAM_NOTAGS, 'Tool type description'),
'platformid' => new external_value(PARAM_TEXT, 'Platform ID'),
'clientid' => new external_value(PARAM_TEXT, 'Client ID'),
'deploymentid' => new external_value(PARAM_INT, 'Deployment ID'),
'urls' => new external_single_structure(
array(
'icon' => new external_value(PARAM_URL, 'Tool type icon URL'),
'edit' => new external_value(PARAM_URL, 'Tool type edit URL'),
'course' => new external_value(PARAM_URL, 'Tool type edit URL', VALUE_OPTIONAL),
'publickeyset' => new external_value(PARAM_URL, 'Public Keyset URL'),
'accesstoken' => new external_value(PARAM_URL, 'Access Token URL'),
'authrequest' => new external_value(PARAM_URL, 'Authorisation Request URL'),
)
),
'state' => new external_single_structure(
array(
'text' => new external_value(PARAM_TEXT, 'Tool type state name string'),
'pending' => new external_value(PARAM_BOOL, 'Is the state pending'),
'configured' => new external_value(PARAM_BOOL, 'Is the state configured'),
'rejected' => new external_value(PARAM_BOOL, 'Is the state rejected'),
'unknown' => new external_value(PARAM_BOOL, 'Is the state unknown'),
)
),
'hascapabilitygroups' => new external_value(PARAM_BOOL, 'Indicate if capabilitygroups is populated'),
'capabilitygroups' => new external_multiple_structure(
new external_value(PARAM_TEXT, 'Tool type capability groups enabled'),
'Array of capability groups', VALUE_DEFAULT, array()
),
'courseid' => new external_value(PARAM_INT, 'Tool type course', VALUE_DEFAULT, 0),
'instanceids' => new external_multiple_structure(
new external_value(PARAM_INT, 'LTI instance ID'),
'IDs for the LTI instances using this type', VALUE_DEFAULT, array()
),
'instancecount' => new external_value(PARAM_INT, 'The number of times this tool is being used')
), 'Tool'
);
}
/**
* Returns description of a tool proxy
*
* @return external_function_parameters
* @since Moodle 3.1
*/
private static function tool_proxy_return_structure() {
return new external_function_parameters(
array(
'id' => new external_value(PARAM_INT, 'Tool proxy id'),
'name' => new external_value(PARAM_TEXT, 'Tool proxy name'),
'regurl' => new external_value(PARAM_URL, 'Tool proxy registration URL'),
'state' => new external_value(PARAM_INT, 'Tool proxy state'),
'guid' => new external_value(PARAM_TEXT, 'Tool proxy globally unique identifier'),
'secret' => new external_value(PARAM_TEXT, 'Tool proxy shared secret'),
'vendorcode' => new external_value(PARAM_TEXT, 'Tool proxy consumer code'),
'capabilityoffered' => new external_value(PARAM_TEXT, 'Tool proxy capabilities offered'),
'serviceoffered' => new external_value(PARAM_TEXT, 'Tool proxy services offered'),
'toolproxy' => new external_value(PARAM_TEXT, 'Tool proxy'),
'timecreated' => new external_value(PARAM_INT, 'Tool proxy time created'),
'timemodified' => new external_value(PARAM_INT, 'Tool proxy modified'),
)
);
}
/**
* Returns description of method parameters
*
* @return external_function_parameters
* @since Moodle 3.1
*/
public static function get_tool_proxies_parameters() {
return new external_function_parameters(
array(
'orphanedonly' => new external_value(PARAM_BOOL, 'Orphaned tool types only', VALUE_DEFAULT, 0)
)
);
}
/**
* Returns the tool types.
*
* @param bool $orphanedonly Retrieve only tool proxies that do not have a corresponding tool type
* @return array of tool types
* @since Moodle 3.1
* @throws moodle_exception
*/
public static function get_tool_proxies($orphanedonly) {
$params = self::validate_parameters(self::get_tool_proxies_parameters(),
array(
'orphanedonly' => $orphanedonly
));
$orphanedonly = $params['orphanedonly'];
$context = context_system::instance();
self::validate_context($context);
require_capability('moodle/site:config', $context);
return lti_get_tool_proxies($orphanedonly);
}
/**
* Returns description of method result value.
*
* @return \core_external\external_description
* @since Moodle 3.1
*/
public static function get_tool_proxies_returns() {
return new external_multiple_structure(
self::tool_proxy_return_structure()
);
}
/**
* 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 \core_external\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()
)
);
}
/**
* Describes the parameters for get_ltis_by_courses.
*
* @return external_function_parameters
* @since Moodle 3.0
*/
public static function get_ltis_by_courses_parameters() {
return new external_function_parameters (
array(
'courseids' => new external_multiple_structure(
new external_value(PARAM_INT, 'course id'), 'Array of course ids', VALUE_DEFAULT, array()
),
)
);
}
/**
* Returns a list of external tools in a provided list of courses,
* if no list is provided all external tools that the user can view will be returned.
*
* @param array $courseids the course ids
* @return array the lti details
* @since Moodle 3.0
*/
public static function get_ltis_by_courses($courseids = array()) {
global $CFG;
$returnedltis = array();
$warnings = array();
$params = self::validate_parameters(self::get_ltis_by_courses_parameters(), array('courseids' => $courseids));
$mycourses = array();
if (empty($params['courseids'])) {
$mycourses = enrol_get_my_courses();
$params['courseids'] = array_keys($mycourses);
}
// Ensure there are courseids to loop through.
if (!empty($params['courseids'])) {
list($courses, $warnings) = util::validate_courses($params['courseids'], $mycourses);
// Get the ltis in this course, this function checks users visibility permissions.
// We can avoid then additional validate_context calls.
$ltis = get_all_instances_in_courses("lti", $courses);
foreach ($ltis as $lti) {
$context = context_module::instance($lti->coursemodule);
// Entry to return.
$module = helper_for_get_mods_by_courses::standard_coursemodule_element_values(
$lti, 'mod_lti', 'moodle/course:manageactivities', 'mod/lti:view');
$viewablefields = [];
if (has_capability('mod/lti:view', $context)) {
$viewablefields = array('launchcontainer', 'showtitlelaunch', 'showdescriptionlaunch', 'icon', 'secureicon');
}
// Check additional permissions for returning optional private settings.
if (has_capability('moodle/course:manageactivities', $context)) {
$additionalfields = array('timecreated', 'timemodified', 'typeid', 'toolurl', 'securetoolurl',
'instructorchoicesendname', 'instructorchoicesendemailaddr', 'instructorchoiceallowroster',
'instructorchoiceallowsetting', 'instructorcustomparameters', 'instructorchoiceacceptgrades', 'grade',
'resourcekey', 'password', 'debuglaunch', 'servicesalt');
$viewablefields = array_merge($viewablefields, $additionalfields);
}
foreach ($viewablefields as $field) {
$module[$field] = $lti->{$field};
}
$returnedltis[] = $module;
}
}
$result = array();
$result['ltis'] = $returnedltis;
$result['warnings'] = $warnings;
return $result;
}
/**
* Describes the get_ltis_by_courses return value.
*
* @return external_single_structure
* @since Moodle 3.0
*/
public static function get_ltis_by_courses_returns() {
return new external_single_structure(
array(
'ltis' => new external_multiple_structure(
new external_single_structure(array_merge(
helper_for_get_mods_by_courses::standard_coursemodule_elements_returns(true),
[
'timecreated' => new external_value(PARAM_INT, 'Time of creation', VALUE_OPTIONAL),
'timemodified' => new external_value(PARAM_INT, 'Time of last modification', VALUE_OPTIONAL),
'typeid' => new external_value(PARAM_INT, 'Type id', VALUE_OPTIONAL),
'toolurl' => new external_value(PARAM_URL, 'Tool url', VALUE_OPTIONAL),
'securetoolurl' => new external_value(PARAM_RAW, 'Secure tool url', VALUE_OPTIONAL),
'instructorchoicesendname' => new external_value(PARAM_TEXT, 'Instructor choice send name',
VALUE_OPTIONAL),
'instructorchoicesendemailaddr' => new external_value(PARAM_INT, 'instructor choice send mail address',
VALUE_OPTIONAL),
'instructorchoiceallowroster' => new external_value(PARAM_INT, 'Instructor choice allow roster',
VALUE_OPTIONAL),
'instructorchoiceallowsetting' => new external_value(PARAM_INT, 'Instructor choice allow setting',
VALUE_OPTIONAL),
'instructorcustomparameters' => new external_value(PARAM_RAW, 'instructor custom parameters',
VALUE_OPTIONAL),
'instructorchoiceacceptgrades' => new external_value(PARAM_INT, 'instructor choice accept grades',
VALUE_OPTIONAL),
'grade' => new external_value(PARAM_INT, 'Enable grades', VALUE_OPTIONAL),
'launchcontainer' => new external_value(PARAM_INT, 'Launch container mode', VALUE_OPTIONAL),
'resourcekey' => new external_value(PARAM_RAW, 'Resource key', VALUE_OPTIONAL),
'password' => new external_value(PARAM_RAW, 'Shared secret', VALUE_OPTIONAL),
'debuglaunch' => new external_value(PARAM_INT, 'Debug launch', VALUE_OPTIONAL),
'showtitlelaunch' => new external_value(PARAM_INT, 'Show title launch', VALUE_OPTIONAL),
'showdescriptionlaunch' => new external_value(PARAM_INT, 'Show description launch', VALUE_OPTIONAL),
'servicesalt' => new external_value(PARAM_RAW, 'Service salt', VALUE_OPTIONAL),
'icon' => new external_value(PARAM_URL, 'Alternative icon URL', VALUE_OPTIONAL),
'secureicon' => new external_value(PARAM_URL, 'Secure icon URL', VALUE_OPTIONAL),
]
), 'Tool')
),
'warnings' => new external_warnings(),
)
);
}
/**
* Returns description of method parameters
*
* @return external_function_parameters
* @since Moodle 3.0
*/
public static function view_lti_parameters() {
return new external_function_parameters(
array(
'ltiid' => new external_value(PARAM_INT, 'lti instance id')
)
);
}
/**
* Trigger the course module viewed event and update the module completion status.
*
* @param int $ltiid the lti instance id
* @return array of warnings and status result
* @since Moodle 3.0
* @throws moodle_exception
*/
public static function view_lti($ltiid) {
global $DB;
$params = self::validate_parameters(self::view_lti_parameters(),
array(
'ltiid' => $ltiid
));
$warnings = array();
// Request and permission validation.
$lti = $DB->get_record('lti', array('id' => $params['ltiid']), '*', 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);
// Trigger course_module_viewed event and completion.
lti_view($lti, $course, $cm, $context);
$result = array();
$result['status'] = true;
$result['warnings'] = $warnings;
return $result;
}
/**
* Returns description of method result value
*
* @return \core_external\external_description
* @since Moodle 3.0
*/
public static function view_lti_returns() {
return new external_single_structure(
array(
'status' => new external_value(PARAM_BOOL, 'status: true if success'),
'warnings' => new external_warnings()
)
);
}
/**
* Returns description of method parameters
*
* @return external_function_parameters
* @since Moodle 3.1
*/
public static function create_tool_proxy_parameters() {
return new external_function_parameters(
array(
'name' => new external_value(PARAM_TEXT, 'Tool proxy name', VALUE_DEFAULT, ''),
'regurl' => new external_value(PARAM_URL, 'Tool proxy registration URL'),
'capabilityoffered' => new external_multiple_structure(
new external_value(PARAM_TEXT, 'Tool proxy capabilities offered'),
'Array of capabilities', VALUE_DEFAULT, array()
),
'serviceoffered' => new external_multiple_structure(
new external_value(PARAM_TEXT, 'Tool proxy services offered'),
'Array of services', VALUE_DEFAULT, array()
)
)
);
}
/**
* Creates a new tool proxy
*
* @param string $name Tool proxy name
* @param string $registrationurl Registration url
* @param string[] $capabilityoffered List of capabilities this tool proxy should be offered
* @param string[] $serviceoffered List of services this tool proxy should be offered
* @return object The new tool proxy
* @since Moodle 3.1
* @throws moodle_exception
*/
public static function create_tool_proxy($name, $registrationurl, $capabilityoffered, $serviceoffered) {
$params = self::validate_parameters(self::create_tool_proxy_parameters(),
array(
'name' => $name,
'regurl' => $registrationurl,
'capabilityoffered' => $capabilityoffered,
'serviceoffered' => $serviceoffered
));
$name = $params['name'];
$regurl = $params['regurl'];
$capabilityoffered = $params['capabilityoffered'];
$serviceoffered = $params['serviceoffered'];
$context = context_system::instance();
self::validate_context($context);
require_capability('moodle/site:config', $context);
// Can't create duplicate proxies with the same URL.
$duplicates = lti_get_tool_proxies_from_registration_url($registrationurl);
if (!empty($duplicates)) {
throw new moodle_exception('duplicateregurl', 'mod_lti');
}
$config = new stdClass();
$config->lti_registrationurl = $registrationurl;
if (!empty($name)) {
$config->lti_registrationname = $name;
}
if (!empty($capabilityoffered)) {
$config->lti_capabilities = $capabilityoffered;
}
if (!empty($serviceoffered)) {
$config->lti_services = $serviceoffered;
}
$id = lti_add_tool_proxy($config);
$toolproxy = lti_get_tool_proxy($id);
// Pending makes more sense than configured as the first state, since
// the next step is to register, which requires the state be pending.
$toolproxy->state = LTI_TOOL_PROXY_STATE_PENDING;
lti_update_tool_proxy($toolproxy);
return $toolproxy;
}
/**
* Returns description of method result value
*
* @return \core_external\external_description
* @since Moodle 3.1
*/
public static function create_tool_proxy_returns() {
return self::tool_proxy_return_structure();
}
/**
* Returns description of method parameters
*
* @return external_function_parameters
* @since Moodle 3.1
*/
public static function delete_tool_proxy_parameters() {
return new external_function_parameters(
array(
'id' => new external_value(PARAM_INT, 'Tool proxy id'),
)
);
}
/**
* Trigger the course module viewed event and update the module completion status.
*
* @param int $id the lti instance id
* @return object The tool proxy
* @since Moodle 3.1
* @throws moodle_exception
*/
public static function delete_tool_proxy($id) {
$params = self::validate_parameters(self::delete_tool_proxy_parameters(),
array(
'id' => $id,
));
$id = $params['id'];
$context = context_system::instance();
self::validate_context($context);
require_capability('moodle/site:config', $context);
$toolproxy = lti_get_tool_proxy($id);
lti_delete_tool_proxy($id);
return $toolproxy;
}
/**
* Returns description of method result value
*
* @return \core_external\external_description
* @since Moodle 3.1
*/
public static function delete_tool_proxy_returns() {
return self::tool_proxy_return_structure();
}
/**
* Returns description of method parameters
*
* @return external_function_parameters
* @since Moodle 3.0
*/
public static function get_tool_proxy_registration_request_parameters() {
return new external_function_parameters(
array(
'id' => new external_value(PARAM_INT, 'Tool proxy id'),
)
);
}
/**
* Returns the registration request for a tool proxy.
*
* @param int $id the lti instance id
* @return array of registration parameters
* @since Moodle 3.1
* @throws moodle_exception
*/
public static function get_tool_proxy_registration_request($id) {
$params = self::validate_parameters(self::get_tool_proxy_registration_request_parameters(),
array(
'id' => $id,
));
$id = $params['id'];
$context = context_system::instance();
self::validate_context($context);
require_capability('moodle/site:config', $context);
$toolproxy = lti_get_tool_proxy($id);
return lti_build_registration_request($toolproxy);
}
/**
* Returns description of method result value
*
* @return \core_external\external_description
* @since Moodle 3.1
*/
public static function get_tool_proxy_registration_request_returns() {
return new external_function_parameters(
array(
'lti_message_type' => new external_value(PARAM_ALPHANUMEXT, 'LTI message type'),
'lti_version' => new external_value(PARAM_ALPHANUMEXT, 'LTI version'),
'reg_key' => new external_value(PARAM_TEXT, 'Tool proxy registration key'),
'reg_password' => new external_value(PARAM_TEXT, 'Tool proxy registration password'),
'reg_url' => new external_value(PARAM_TEXT, 'Tool proxy registration url'),
'tc_profile_url' => new external_value(PARAM_URL, 'Tool consumers profile URL'),
'launch_presentation_return_url' => new external_value(PARAM_URL, 'URL to redirect on registration completion'),
)
);
}
/**
* Returns description of method parameters
*
* @return external_function_parameters
* @since Moodle 3.1
*/
public static function get_tool_types_parameters() {
return new external_function_parameters(
array(
'toolproxyid' => new external_value(PARAM_INT, 'Tool proxy id', VALUE_DEFAULT, 0)
)
);
}
/**
* Returns the tool types.
*
* @param int $toolproxyid The tool proxy id
* @return array of tool types
* @since Moodle 3.1
* @throws moodle_exception
*/
public static function get_tool_types($toolproxyid) {
global $PAGE;
$params = self::validate_parameters(self::get_tool_types_parameters(),
array(
'toolproxyid' => $toolproxyid
));
$toolproxyid = $params['toolproxyid'];
$types = array();
$context = context_system::instance();
self::validate_context($context);
require_capability('moodle/site:config', $context);
if (!empty($toolproxyid)) {
$types = lti_get_lti_types_from_proxy_id($toolproxyid);
} else {
$types = lti_get_lti_types();
}
return array_map("serialise_tool_type", array_values($types));
}
/**
* Returns description of method result value
*
* @return \core_external\external_description
* @since Moodle 3.1
*/
public static function get_tool_types_returns() {
return new external_multiple_structure(
self::tool_type_return_structure()
);
}
/**
* Returns description of method parameters
*
* @return external_function_parameters
* @since Moodle 3.1
*/
public static function create_tool_type_parameters() {
return new external_function_parameters(
array(
'cartridgeurl' => new external_value(PARAM_URL, 'URL to cardridge to load tool information', VALUE_DEFAULT, ''),
'key' => new external_value(PARAM_TEXT, 'Consumer key', VALUE_DEFAULT, ''),
'secret' => new external_value(PARAM_TEXT, 'Shared secret', VALUE_DEFAULT, ''),
)
);
}
/**
* Creates a tool type.
*
* @param string $cartridgeurl Url of the xml cartridge representing the LTI tool
* @param string $key The consumer key to identify this consumer
* @param string $secret The secret
* @return array created tool type
* @since Moodle 3.1
* @throws moodle_exception If the tool type could not be created
*/
public static function create_tool_type($cartridgeurl, $key, $secret) {
$params = self::validate_parameters(self::create_tool_type_parameters(),
array(
'cartridgeurl' => $cartridgeurl,
'key' => $key,
'secret' => $secret
));
$cartridgeurl = $params['cartridgeurl'];
$key = $params['key'];
$secret = $params['secret'];
$context = context_system::instance();
self::validate_context($context);
require_capability('moodle/site:config', $context);
$id = null;
if (!empty($cartridgeurl)) {
$type = new stdClass();
$data = new stdClass();
$type->state = LTI_TOOL_STATE_CONFIGURED;
$data->lti_coursevisible = 1;
$data->lti_sendname = LTI_SETTING_DELEGATE;
$data->lti_sendemailaddr = LTI_SETTING_DELEGATE;
$data->lti_acceptgrades = LTI_SETTING_DELEGATE;
$data->lti_forcessl = 0;
if (!empty($key)) {
$data->lti_resourcekey = $key;
}
if (!empty($secret)) {
$data->lti_password = $secret;
}
lti_load_type_from_cartridge($cartridgeurl, $data);
if (empty($data->lti_toolurl)) {
throw new moodle_exception('unabletocreatetooltype', 'mod_lti');
} else {
$id = lti_add_type($type, $data);
}
}
if (!empty($id)) {
$type = lti_get_type($id);
return serialise_tool_type($type);
} else {
throw new moodle_exception('unabletocreatetooltype', 'mod_lti');
}
}
/**
* Returns description of method result value
*
* @return \core_external\external_description
* @since Moodle 3.1
*/
public static function create_tool_type_returns() {
return self::tool_type_return_structure();
}
/**
* Returns description of method parameters
*
* @return external_function_parameters
* @since Moodle 3.1
*/
public static function update_tool_type_parameters() {
return new external_function_parameters(
array(
'id' => new external_value(PARAM_INT, 'Tool type id'),
'name' => new external_value(PARAM_RAW, 'Tool type name', VALUE_DEFAULT, null),
'description' => new external_value(PARAM_RAW, 'Tool type description', VALUE_DEFAULT, null),
'state' => new external_value(PARAM_INT, 'Tool type state', VALUE_DEFAULT, null)
)
);
}
/**
* Update a tool type.
*
* @param int $id The id of the tool type to update
* @param string $name The name of the tool type
* @param string $description The name of the tool type
* @param int $state The state of the tool type
* @return array updated tool type
* @since Moodle 3.1
* @throws moodle_exception
*/
public static function update_tool_type($id, $name, $description, $state) {
$params = self::validate_parameters(self::update_tool_type_parameters(),
array(
'id' => $id,
'name' => $name,
'description' => $description,
'state' => $state,
));
$id = $params['id'];
$name = $params['name'];
$description = $params['description'];
$state = $params['state'];
$context = context_system::instance();
self::validate_context($context);
require_capability('moodle/site:config', $context);
$type = lti_get_type($id);
if (empty($type)) {
throw new moodle_exception('unabletofindtooltype', 'mod_lti', '', array('id' => $id));
}
if (!empty($name)) {
$type->name = $name;
}
if (!empty($description)) {
$type->description = $description;
}
if (!empty($state)) {
// Valid state range.
if (in_array($state, array(1, 2, 3))) {
$type->state = $state;
} else {
throw new moodle_exception("Invalid state: $state - must be 1, 2, or 3");
}
}
lti_update_type($type, new stdClass());
return serialise_tool_type($type);
}
/**
* Returns description of method result value
*
* @return \core_external\external_description
* @since Moodle 3.1
*/
public static function update_tool_type_returns() {
return self::tool_type_return_structure();
}
/**
* Returns description of method parameters
*
* @return external_function_parameters
* @since Moodle 3.1
*/
public static function delete_tool_type_parameters() {
return new external_function_parameters(
array(
'id' => new external_value(PARAM_INT, 'Tool type id'),
)
);
}
/**
* Delete a tool type.
*
* @param int $id The id of the tool type to be deleted
* @return array deleted tool type
* @since Moodle 3.1
* @throws moodle_exception
*/
public static function delete_tool_type($id) {
$params = self::validate_parameters(self::delete_tool_type_parameters(),
array(
'id' => $id,
));
$id = $params['id'];
$context = context_system::instance();
self::validate_context($context);
require_capability('moodle/site:config', $context);
$type = lti_get_type($id);
if (!empty($type)) {
lti_delete_type($id);
// If this is the last type for this proxy then remove the proxy
// as well so that it isn't orphaned.
$types = lti_get_lti_types_from_proxy_id($type->toolproxyid);
if (empty($types)) {
lti_delete_tool_proxy($type->toolproxyid);
}
}
return array('id' => $id);
}
/**
* Returns description of method result value
*
* @return \core_external\external_description
* @since Moodle 3.1
*/
public static function delete_tool_type_returns() {
return new external_function_parameters(
array(
'id' => new external_value(PARAM_INT, 'Tool type id'),
)
);
}
/**
* Returns description of method parameters
*
* @return external_function_parameters
* @since Moodle 3.1
*/
public static function is_cartridge_parameters() {
return new external_function_parameters(
array(
'url' => new external_value(PARAM_URL, 'Tool url'),
)
);
}
/**
* Determine if the url to a tool is for a cartridge.
*
* @param string $url Url that may or may not be an xml cartridge
* @return bool True if the url is for a cartridge.
* @since Moodle 3.1
* @throws moodle_exception
*/
public static function is_cartridge($url) {
$params = self::validate_parameters(self::is_cartridge_parameters(),
array(
'url' => $url,
));
$url = $params['url'];
$context = context_system::instance();
self::validate_context($context);
require_capability('moodle/site:config', $context);
$iscartridge = lti_is_cartridge($url);
return array('iscartridge' => $iscartridge);
}
/**
* Returns description of method result value
*
* @return \core_external\external_description
* @since Moodle 3.1
*/
public static function is_cartridge_returns() {
return new external_function_parameters(
array(
'iscartridge' => new external_value(PARAM_BOOL, 'True if the URL is a cartridge'),
)
);
}
}
+81
View File
@@ -0,0 +1,81 @@
<?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/>.
namespace mod_lti\external;
use core_external\external_api;
use core_external\external_function_parameters;
use core_external\external_value;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/mod/lti/locallib.php');
/**
* External function to delete a course tool type.
*
* @package mod_lti
* @copyright 2023 Jake Dallimore <jrhdallimore@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class delete_course_tool_type extends external_api {
/**
* Get parameter definition.
*
* @return external_function_parameters
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters([
'tooltypeid' => new external_value(PARAM_INT, 'Tool type ID'),
]);
}
/**
* Delete a course tool type.
*
* @param int $tooltypeid the id of the course external tool type.
* @return bool true
* @throws \invalid_parameter_exception if the provided id refers to a site level tool which cannot be deleted.
*/
public static function execute(int $tooltypeid): bool {
['tooltypeid' => $tooltypeid] = self::validate_parameters(self::execute_parameters(), ['tooltypeid' => $tooltypeid]);
global $DB;
$course = (int) $DB->get_field('lti_types', 'course', ['id' => $tooltypeid]);
if ($course == get_site()->id) {
throw new \invalid_parameter_exception('This is a site-level tool and cannot be deleted via this service');
}
$context = \context_course::instance($course);
self::validate_context($context);
require_capability('mod/lti:addcoursetool', $context);
\lti_delete_type($tooltypeid);
return true;
}
/**
* Get service returns definition.
*
* @return external_value
*/
public static function execute_returns(): external_value {
return new external_value(PARAM_BOOL, 'Success');
}
}
+118
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/>.
namespace mod_lti\external;
use core_external\external_api;
use core_external\external_function_parameters;
use core_external\external_single_structure;
use core_external\external_value;
/**
* External function for fetching all tool types and proxies.
*
* @package mod_lti
* @author Andrew Madden <andrewmadden@catalyst-au.net>
* @copyright 2021 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class get_tool_types_and_proxies extends external_api {
/**
* Get parameter definition for get_tool_types_and_proxies().
*
* @return external_function_parameters
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters([
'toolproxyid' => new external_value(
PARAM_INT,
'Tool proxy id',
VALUE_DEFAULT,
0
),
'orphanedonly' => new external_value(
PARAM_BOOL,
'Orphaned tool types only',
VALUE_DEFAULT,
0
),
'limit' => new external_value(
PARAM_INT,
'How many tool types displayed per page',
VALUE_DEFAULT,
60,
NULL_NOT_ALLOWED
),
'offset' => new external_value(
PARAM_INT,
'Current offset of tool elements',
VALUE_DEFAULT,
0,
NULL_NOT_ALLOWED
),
]);
}
/**
* Get data for all tool types and tool proxies.
*
* @param int $toolproxyid The tool proxy id
* @param bool $orphanedonly Whether to get orphaned proxies only.
* @param int $limit How many elements to return if using pagination.
* @param int $offset Which chunk of elements to return is using pagination.
* @return array
*/
public static function execute($toolproxyid, $orphanedonly, $limit, $offset): array {
$params = self::validate_parameters(self::execute_parameters(), [
'toolproxyid' => $toolproxyid,
'orphanedonly' => $orphanedonly,
'limit' => $limit,
'offset' => $offset,
]);
$toolproxyid = $params['toolproxyid'] !== null ? $params['toolproxyid'] : 0;
$orphanedonly = $params['orphanedonly'] !== null ? $params['orphanedonly'] : false;
$limit = $params['limit'] !== null ? $params['limit'] : 0;
$offset = $params['offset'] !== null ? $params['offset'] : 0;
$context = \context_system::instance();
self::validate_context($context);
require_capability('moodle/site:config', $context);
[$proxies, $types] = lti_get_lti_types_and_proxies($limit, $offset, $orphanedonly, $toolproxyid);
return [
'types' => $types,
'proxies' => $proxies,
'limit' => $limit,
'offset' => $offset,
];
}
/**
* Get return definition for get_tool_types_and_proxies.
*
* @return external_single_structure
*/
public static function execute_returns(): external_single_structure {
return new external_single_structure([
'types' => \mod_lti_external::get_tool_types_returns(),
'proxies' => \mod_lti_external::get_tool_proxies_returns(),
'limit' => new external_value(PARAM_INT, 'Limit of how many tool types to show', VALUE_OPTIONAL),
'offset' => new external_value(PARAM_INT, 'Offset of tool types', VALUE_OPTIONAL),
]);
}
}
@@ -0,0 +1,89 @@
<?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/>.
namespace mod_lti\external;
use core_external\external_api;
use core_external\external_function_parameters;
use core_external\external_single_structure;
use core_external\external_value;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/mod/lti/locallib.php');
/**
* External function for fetching the count of all tool types and proxies.
*
* @package mod_lti
* @author Andrew Madden <andrewmadden@catalyst-au.net>
* @copyright 2021 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class get_tool_types_and_proxies_count extends external_api {
/**
* Get parameter definition for get_tool_types_and_proxies_count().
*
* @return external_function_parameters
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters(
[
'toolproxyid' => new external_value(PARAM_INT, 'Tool proxy id', VALUE_DEFAULT, 0),
'orphanedonly' => new external_value(PARAM_BOOL, 'Orphaned tool types only', VALUE_DEFAULT, 0),
]
);
}
/**
* Get count of every tool type and tool proxy.
*
* @param int $toolproxyid The tool proxy id
* @param bool $orphanedonly Whether to get orphaned proxies only.
* @return array
*/
public static function execute($toolproxyid, $orphanedonly): array {
$params = self::validate_parameters(self::execute_parameters(),
[
'toolproxyid' => $toolproxyid,
'orphanedonly' => $orphanedonly,
]);
$toolproxyid = $params['toolproxyid'];
$orphanedonly = $params['orphanedonly'];
$context = \context_system::instance();
self::validate_context($context);
require_capability('moodle/site:config', $context);
return [
'count' => lti_get_lti_types_and_proxies_count($orphanedonly, $toolproxyid),
];
}
/**
* Get return definition for get_tool_types_and_proxies_count.
*
* @return external_single_structure
*/
public static function execute_returns(): external_single_structure {
return new external_single_structure([
'count' => new external_value(PARAM_INT, 'Total number of tool types and proxies', VALUE_REQUIRED),
]);
}
}
@@ -0,0 +1,83 @@
<?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/>.
namespace mod_lti\external;
use core_external\external_api;
use core_external\external_function_parameters;
use core_external\external_value;
use mod_lti\local\types_helper;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/mod/lti/locallib.php');
/**
* External function to toggle showinactivitychooser setting.
*
* @package mod_lti
* @copyright 2023 Ilya Tregubov <ilya.a.tregubov@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class toggle_showinactivitychooser extends external_api {
/**
* Get parameter definition.
*
* @return external_function_parameters
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters([
'tooltypeid' => new external_value(PARAM_INT, 'Tool type ID'),
'courseid' => new external_value(PARAM_INT, 'Course ID'),
'showinactivitychooser' => new external_value(PARAM_BOOL, 'Show in activity chooser'),
]);
}
/**
* Toggles showinactivitychooser setting.
*
* @param int $tooltypeid the id of the course external tool type.
* @param int $courseid the id of the course we are in.
* @param bool $showinactivitychooser Show in activity chooser setting.
* @return bool true or false
*/
public static function execute(int $tooltypeid, int $courseid, bool $showinactivitychooser): bool {
[
'tooltypeid' => $tooltypeid,
'courseid' => $courseid,
'showinactivitychooser' => $showinactivitychooser,
] = self::validate_parameters(self::execute_parameters(), [
'tooltypeid' => $tooltypeid,
'courseid' => $courseid,
'showinactivitychooser' => $showinactivitychooser,
]);
$context = \core\context\course::instance($courseid);
self::validate_context($context);
return types_helper::override_type_showinactivitychooser($tooltypeid, $courseid, $context, $showinactivitychooser);
}
/**
* Get service returns definition.
*
* @return external_value
*/
public static function execute_returns(): external_value {
return new external_value(PARAM_BOOL, 'Success');
}
}
+60
View File
@@ -0,0 +1,60 @@
<?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/>.
namespace mod_lti;
defined('MOODLE_INTERNAL') || die();
/**
* Helper class for LTI activity.
*
* @package mod_lti
* @author Andrew Madden <andrewmadden@catalyst-au.net>
* @copyright 2020 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class helper {
/**
* Get SQL to query DB for LTI tool proxy records.
*
* @param bool $orphanedonly If true, return SQL to get orphaned proxies only.
* @param bool $count If true, return SQL to get the count of the records instead of the records themselves.
* @return string SQL.
*/
public static function get_tool_proxy_sql(bool $orphanedonly = false, bool $count = false): string {
if ($count) {
$select = "SELECT count(*) as type_count";
$sort = "";
} else {
// We only want the fields from lti_tool_proxies table. Must define every column to be compatible with mysqli.
$select = "SELECT ltp.id, ltp.name, ltp.regurl, ltp.state, ltp.guid, ltp.secret, ltp.vendorcode,
ltp.capabilityoffered, ltp.serviceoffered, ltp.toolproxy, ltp.createdby,
ltp.timecreated, ltp.timemodified";
$sort = " ORDER BY ltp.name ASC, ltp.state DESC, ltp.timemodified DESC";
}
$from = " FROM {lti_tool_proxies} ltp";
if ($orphanedonly) {
$join = " LEFT JOIN {lti_types} lt ON ltp.id = lt.toolproxyid";
$where = " WHERE lt.toolproxyid IS null";
} else {
$join = "";
$where = "";
}
return $select . $from . $join . $where . $sort;
}
}
@@ -0,0 +1,138 @@
<?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/>.
/**
* This files exposes functions for LTI 1.3 Key Management.
*
* @package mod_lti
* @copyright 2020 Claude Vervoort (Cengage)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_lti\local\ltiopenid;
use Firebase\JWT\JWT;
/**
* This class exposes functions for LTI 1.3 Key Management.
*
* @package mod_lti
* @copyright 2020 Claude Vervoort (Cengage)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class jwks_helper {
/**
*
* See https://www.imsglobal.org/spec/security/v1p1#approved-jwt-signing-algorithms.
* @var string[]
*/
private static $ltisupportedalgs = [
'RS256' => 'RSA',
'RS384' => 'RSA',
'RS512' => 'RSA',
'ES256' => 'EC',
'ES384' => 'EC',
'ES512' => 'EC'
];
/**
* Returns the private key to use to sign outgoing JWT.
*
* @return array keys are kid and key in PEM format.
*/
public static function get_private_key() {
$privatekey = get_config('mod_lti', 'privatekey');
$kid = get_config('mod_lti', 'kid');
return [
"key" => $privatekey,
"kid" => $kid
];
}
/**
* Returns the JWK Key Set for this site.
* @return array keyset exposting the site public key.
*/
public static function get_jwks() {
$jwks = array('keys' => array());
$privatekey = self::get_private_key();
$res = openssl_pkey_get_private($privatekey['key']);
$details = openssl_pkey_get_details($res);
// Avoid passing null values to base64_encode.
if (!isset($details['rsa']['e']) || !isset($details['rsa']['n'])) {
throw new \moodle_exception('Error: essential openssl keys not set');
}
$jwk = array();
$jwk['kty'] = 'RSA';
$jwk['alg'] = 'RS256';
$jwk['kid'] = $privatekey['kid'];
$jwk['e'] = rtrim(strtr(base64_encode($details['rsa']['e']), '+/', '-_'), '=');
$jwk['n'] = rtrim(strtr(base64_encode($details['rsa']['n']), '+/', '-_'), '=');
$jwk['use'] = 'sig';
$jwks['keys'][] = $jwk;
return $jwks;
}
/**
* Take an array of JWKS keys and infer the 'alg' property for a single key, if missing, based on an input JWT.
*
* This only sets the 'alg' property for a single key when all the following conditions are met:
* - The key's 'kid' matches the 'kid' provided in the JWT's header.
* - The key's 'alg' is missing.
* - The JWT's header 'alg' matches the algorithm family of the key (the key's kty).
* - The JWT's header 'alg' matches one of the approved LTI asymmetric algorithms.
*
* Keys not matching the above are left unchanged.
*
* @param array $jwks the keyset array.
* @param string $jwt the JWT string.
* @return array the fixed keyset array.
*/
public static function fix_jwks_alg(array $jwks, string $jwt): array {
$jwtparts = explode('.', $jwt);
$jwtheader = json_decode(JWT::urlsafeB64Decode($jwtparts[0]), true);
if (!isset($jwtheader['kid'])) {
throw new \moodle_exception('Error: kid must be provided in JWT header.');
}
foreach ($jwks['keys'] as $index => $key) {
// Only fix the key being referred to in the JWT.
if ($jwtheader['kid'] != $key['kid']) {
continue;
}
// Only fix the key if the alg is missing.
if (!empty($key['alg'])) {
continue;
}
// The header alg must match the key type (family) specified in the JWK's kty.
if (!isset(static::$ltisupportedalgs[$jwtheader['alg']]) ||
static::$ltisupportedalgs[$jwtheader['alg']] != $key['kty']) {
throw new \moodle_exception('Error: Alg specified in the JWT header is incompatible with the JWK key type');
}
$jwks['keys'][$index]['alg'] = $jwtheader['alg'];
}
return $jwks;
}
}
@@ -0,0 +1,32 @@
<?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/>.
/**
* This library exposes functions for LTI Dynamic Registration.
*
* @package mod_lti
* @copyright 2020 Claude Vervoort (Cengage), Carlos Costa, Adrian Hutchinson (Macgraw Hill)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_lti\local\ltiopenid;
/**
* Exception when transforming the registration to LTI config.
*
* Code is the HTTP Error code.
*/
class registration_exception extends \Exception {
}
@@ -0,0 +1,449 @@
<?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/>.
/**
* A Helper for LTI Dynamic Registration.
*
* @package mod_lti
* @copyright 2020 Claude Vervoort (Cengage), Carlos Costa, Adrian Hutchinson (Macgraw Hill)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_lti\local\ltiopenid;
defined('MOODLE_INTERNAL') || die;
require_once($CFG->dirroot . '/mod/lti/locallib.php');
use Firebase\JWT\JWK;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use stdClass;
/**
* This class exposes functions for LTI Dynamic Registration.
*
* @package mod_lti
* @copyright 2020 Claude Vervoort (Cengage), Carlos Costa, Adrian Hutchinson (Macgraw Hill)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class registration_helper {
/** score scope */
const SCOPE_SCORE = 'https://purl.imsglobal.org/spec/lti-ags/scope/score';
/** result scope */
const SCOPE_RESULT = 'https://purl.imsglobal.org/spec/lti-ags/scope/result.readonly';
/** lineitem read-only scope */
const SCOPE_LINEITEM_RO = 'https://purl.imsglobal.org/spec/lti-ags/scope/lineitem.readonly';
/** lineitem full access scope */
const SCOPE_LINEITEM = 'https://purl.imsglobal.org/spec/lti-ags/scope/lineitem';
/** Names and Roles (membership) scope */
const SCOPE_NRPS = 'https://purl.imsglobal.org/spec/lti-nrps/scope/contextmembership.readonly';
/** Tool Settings scope */
const SCOPE_TOOL_SETTING = 'https://purl.imsglobal.org/spec/lti-ts/scope/toolsetting';
/** Indicates the token is to create a new registration */
const REG_TOKEN_OP_NEW_REG = 'reg';
/** Indicates the token is to update an existing registration */
const REG_TOKEN_OP_UPDATE_REG = 'reg-update';
/**
* Get an instance of this helper
*
* @return object
*/
public static function get() {
return new registration_helper();
}
/**
* Function used to validate parameters.
*
* This function is needed because the payload contains nested
* objects, and optional_param() does not support arrays of arrays.
*
* @param array $payload that may contain the parameter key
* @param string $key the key of the value to be looked for in the payload
* @param bool $required if required, not finding a value will raise a registration_exception
*
* @return mixed
*/
private function get_parameter(array $payload, string $key, bool $required) {
if (!isset($payload[$key]) || empty($payload[$key])) {
if ($required) {
throw new registration_exception('missing required attribute '.$key, 400);
}
return null;
}
$parameter = $payload[$key];
// Cleans parameters to avoid XSS and other issues.
if (is_array($parameter)) {
return clean_param_array($parameter, PARAM_TEXT, true);
}
return clean_param($parameter, PARAM_TEXT);
}
/**
* Transforms an LTI 1.3 Registration to a Moodle LTI Config.
*
* @param array $registrationpayload the registration data received from the tool.
* @param string $clientid the clientid to be issued for that tool.
*
* @return object the Moodle LTI config.
*/
public function registration_to_config(array $registrationpayload, string $clientid): object {
$responsetypes = $this->get_parameter($registrationpayload, 'response_types', true);
$initiateloginuri = $this->get_parameter($registrationpayload, 'initiate_login_uri', true);
$redirecturis = $this->get_parameter($registrationpayload, 'redirect_uris', true);
$clientname = $this->get_parameter($registrationpayload, 'client_name', true);
$jwksuri = $this->get_parameter($registrationpayload, 'jwks_uri', true);
$tokenendpointauthmethod = $this->get_parameter($registrationpayload, 'token_endpoint_auth_method', true);
$applicationtype = $this->get_parameter($registrationpayload, 'application_type', false);
$logouri = $this->get_parameter($registrationpayload, 'logo_uri', false);
$ltitoolconfiguration = $this->get_parameter($registrationpayload,
'https://purl.imsglobal.org/spec/lti-tool-configuration', true);
$domain = $this->get_parameter($ltitoolconfiguration, 'domain', false);
$targetlinkuri = $this->get_parameter($ltitoolconfiguration, 'target_link_uri', false);
$customparameters = $this->get_parameter($ltitoolconfiguration, 'custom_parameters', false);
$scopes = explode(" ", $this->get_parameter($registrationpayload, 'scope', false) ?? '');
$claims = $this->get_parameter($ltitoolconfiguration, 'claims', false);
$messages = $ltitoolconfiguration['messages'] ?? [];
$description = $this->get_parameter($ltitoolconfiguration, 'description', false);
// Validate domain and target link.
if (empty($domain)) {
throw new registration_exception('missing_domain', 400);
}
$targetlinkuri = $targetlinkuri ?: 'https://'.$domain;
// Stripping www as this is ignored for domain matching.
$domain = lti_get_domain_from_url($domain);
if ($domain !== lti_get_domain_from_url($targetlinkuri)) {
throw new registration_exception('domain_targetlinkuri_mismatch', 400);
}
// Validate response type.
// According to specification, for this scenario, id_token must be explicitly set.
if (!in_array('id_token', $responsetypes)) {
throw new registration_exception('invalid_response_types', 400);
}
// According to specification, this parameter needs to be an array.
if (!is_array($redirecturis)) {
throw new registration_exception('invalid_redirect_uris', 400);
}
// According to specification, for this scenario private_key_jwt must be explicitly set.
if ($tokenendpointauthmethod !== 'private_key_jwt') {
throw new registration_exception('invalid_token_endpoint_auth_method', 400);
}
if (!empty($applicationtype) && $applicationtype !== 'web') {
throw new registration_exception('invalid_application_type', 400);
}
$config = new stdClass();
$config->lti_clientid = $clientid;
$config->lti_toolurl = $targetlinkuri;
$config->lti_tooldomain = $domain;
$config->lti_typename = $clientname;
$config->lti_description = $description;
$config->lti_ltiversion = LTI_VERSION_1P3;
$config->lti_organizationid_default = LTI_DEFAULT_ORGID_SITEID;
$config->lti_icon = $logouri;
$config->lti_coursevisible = LTI_COURSEVISIBLE_PRECONFIGURED;
$config->lti_contentitem = 0;
// Sets Content Item.
if (!empty($messages)) {
$messagesresponse = [];
foreach ($messages as $value) {
if ($value['type'] === 'LtiDeepLinkingRequest') {
$config->lti_contentitem = 1;
$config->lti_toolurl_ContentItemSelectionRequest = $value['target_link_uri'] ?? '';
array_push($messagesresponse, $value);
}
}
}
$config->lti_keytype = 'JWK_KEYSET';
$config->lti_publickeyset = $jwksuri;
$config->lti_initiatelogin = $initiateloginuri;
$config->lti_redirectionuris = implode(PHP_EOL, $redirecturis);
$config->lti_customparameters = '';
// Sets custom parameters.
if (isset($customparameters)) {
$paramssarray = [];
foreach ($customparameters as $key => $value) {
array_push($paramssarray, $key . '=' . $value);
}
$config->lti_customparameters = implode(PHP_EOL, $paramssarray);
}
// Sets launch container.
$config->lti_launchcontainer = LTI_LAUNCH_CONTAINER_EMBED_NO_BLOCKS;
// Sets Service info based on scopes.
$config->lti_acceptgrades = LTI_SETTING_NEVER;
$config->ltiservice_gradesynchronization = 0;
$config->ltiservice_memberships = 0;
$config->ltiservice_toolsettings = 0;
if (isset($scopes)) {
// Sets Assignment and Grade Services info.
if (in_array(self::SCOPE_SCORE, $scopes)) {
$config->lti_acceptgrades = LTI_SETTING_DELEGATE;
$config->ltiservice_gradesynchronization = 1;
}
if (in_array(self::SCOPE_RESULT, $scopes)) {
$config->lti_acceptgrades = LTI_SETTING_DELEGATE;
$config->ltiservice_gradesynchronization = 1;
}
if (in_array(self::SCOPE_LINEITEM_RO, $scopes)) {
$config->lti_acceptgrades = LTI_SETTING_DELEGATE;
$config->ltiservice_gradesynchronization = 1;
}
if (in_array(self::SCOPE_LINEITEM, $scopes)) {
$config->lti_acceptgrades = LTI_SETTING_DELEGATE;
$config->ltiservice_gradesynchronization = 2;
}
// Sets Names and Role Provisioning info.
if (in_array(self::SCOPE_NRPS, $scopes)) {
$config->ltiservice_memberships = 1;
}
// Sets Tool Settings info.
if (in_array(self::SCOPE_TOOL_SETTING, $scopes)) {
$config->ltiservice_toolsettings = 1;
}
}
// Sets privacy settings.
$config->lti_sendname = LTI_SETTING_NEVER;
$config->lti_sendemailaddr = LTI_SETTING_NEVER;
if (isset($claims)) {
// Sets name privacy settings.
if (in_array('name', $claims)) {
$config->lti_sendname = LTI_SETTING_ALWAYS;
}
if (in_array('given_name', $claims)) {
$config->lti_sendname = LTI_SETTING_ALWAYS;
}
if (in_array('family_name', $claims)) {
$config->lti_sendname = LTI_SETTING_ALWAYS;
}
// Sets email privacy settings.
if (in_array('email', $claims)) {
$config->lti_sendemailaddr = LTI_SETTING_ALWAYS;
}
}
return $config;
}
/**
* Adds to the config the LTI 1.1 key and sign it with the 1.1 secret.
*
* @param array $lticonfig reference to lticonfig to which to add the 1.1 OAuth info.
* @param string $key - LTI 1.1 OAuth Key
* @param string $secret - LTI 1.1 OAuth Secret
*
*/
private function add_previous_key_claim(array &$lticonfig, string $key, string $secret) {
if ($key) {
$oauthconsumer = [];
$oauthconsumer['key'] = $key;
$oauthconsumer['nonce'] = random_string(random_int(10, 20));
$oauthconsumer['sign'] = hash('sha256', $key.$secret.$oauthconsumer['nonce']);
$lticonfig['oauth_consumer'] = $oauthconsumer;
}
}
/**
* Transforms a moodle LTI 1.3 Config to an OAuth/LTI Client Registration.
*
* @param object $config Moodle LTI Config.
* @param int $typeid which is the LTI deployment id.
* @param object $type tool instance in case the tool already exists.
*
* @return array the Client Registration as an associative array.
*/
public function config_to_registration(object $config, int $typeid, object $type = null): array {
$configarray = [];
foreach ((array)$config as $k => $v) {
if (substr($k, 0, 4) == 'lti_') {
$k = substr($k, 4);
}
$configarray[$k] = $v;
}
$config = (object) $configarray;
$registrationresponse = [];
$lticonfigurationresponse = [];
$ltiversion = $type ? $type->ltiversion : $config->ltiversion;
$lticonfigurationresponse['version'] = $ltiversion;
if ($ltiversion === LTI_VERSION_1P3) {
$registrationresponse['client_id'] = $type ? $type->clientid : $config->clientid;
$registrationresponse['response_types'] = ['id_token'];
$registrationresponse['jwks_uri'] = $config->publickeyset;
$registrationresponse['initiate_login_uri'] = $config->initiatelogin;
$registrationresponse['grant_types'] = ['client_credentials', 'implicit'];
$registrationresponse['redirect_uris'] = explode(PHP_EOL, $config->redirectionuris);
$registrationresponse['application_type'] = 'web';
$registrationresponse['token_endpoint_auth_method'] = 'private_key_jwt';
} else if ($ltiversion === LTI_VERSION_1 && $type) {
$this->add_previous_key_claim($lticonfigurationresponse, $config->resourcekey, $config->password);
} else if ($ltiversion === LTI_VERSION_2 && $type) {
$toolproxy = $this->get_tool_proxy($type->toolproxyid);
$this->add_previous_key_claim($lticonfigurationresponse, $toolproxy['guid'], $toolproxy['secret']);
}
$registrationresponse['client_name'] = $type ? $type->name : $config->typename;
$registrationresponse['logo_uri'] = $type ? ($type->secureicon ?? $type->icon ?? '') : $config->icon ?? '';
$lticonfigurationresponse['deployment_id'] = strval($typeid);
$lticonfigurationresponse['target_link_uri'] = $type ? $type->baseurl : $config->toolurl ?? '';
$lticonfigurationresponse['domain'] = $type ? $type->tooldomain : $config->tooldomain ?? '';
$lticonfigurationresponse['description'] = $type ? $type->description ?? '' : $config->description ?? '';
if ($config->contentitem ?? 0 == 1) {
$contentitemmessage = [];
$contentitemmessage['type'] = 'LtiDeepLinkingRequest';
if (isset($config->toolurl_ContentItemSelectionRequest)) {
$contentitemmessage['target_link_uri'] = $config->toolurl_ContentItemSelectionRequest;
}
$lticonfigurationresponse['messages'] = [$contentitemmessage];
}
if (isset($config->customparameters) && !empty($config->customparameters)) {
$params = [];
foreach (explode(PHP_EOL, $config->customparameters) as $param) {
$split = explode('=', $param);
$params[$split[0]] = $split[1];
}
$lticonfigurationresponse['custom_parameters'] = $params;
}
$scopesresponse = [];
if ($config->ltiservice_gradesynchronization ?? 0 > 0) {
$scopesresponse[] = self::SCOPE_SCORE;
$scopesresponse[] = self::SCOPE_RESULT;
$scopesresponse[] = self::SCOPE_LINEITEM_RO;
}
if ($config->ltiservice_gradesynchronization ?? 0 == 2) {
$scopesresponse[] = self::SCOPE_LINEITEM;
}
if ($config->ltiservice_memberships ?? 0 == 1) {
$scopesresponse[] = self::SCOPE_NRPS;
}
if ($config->ltiservice_toolsettings ?? 0 == 1) {
$scopesresponse[] = self::SCOPE_TOOL_SETTING;
}
$registrationresponse['scope'] = implode(' ', $scopesresponse);
$claimsresponse = ['sub', 'iss'];
if ($config->sendname ?? '' == LTI_SETTING_ALWAYS) {
$claimsresponse[] = 'name';
$claimsresponse[] = 'family_name';
$claimsresponse[] = 'given_name';
}
if ($config->sendemailaddr ?? '' == LTI_SETTING_ALWAYS) {
$claimsresponse[] = 'email';
}
$lticonfigurationresponse['claims'] = $claimsresponse;
$registrationresponse['https://purl.imsglobal.org/spec/lti-tool-configuration'] = $lticonfigurationresponse;
return $registrationresponse;
}
/**
* Validates the registration token is properly signed and not used yet.
* Return the client id to use for this registration.
*
* @param string $registrationtokenjwt registration token
*
* @return array with 2 keys: clientid for the registration, type but only if it's an update
*/
public function validate_registration_token(string $registrationtokenjwt): array {
global $DB;
// JWK::parseKeySet uses RS256 algorithm by default.
$keys = JWK::parseKeySet(jwks_helper::get_jwks());
$registrationtoken = JWT::decode($registrationtokenjwt, $keys);
$response = [];
// Get clientid from registrationtoken.
$clientid = $registrationtoken->sub;
if ($registrationtoken->scope == self::REG_TOKEN_OP_NEW_REG) {
// Checks if clientid is already registered.
if (!empty($DB->get_record('lti_types', array('clientid' => $clientid)))) {
throw new registration_exception("token_already_used", 401);
}
$response['clientid'] = $clientid;
} else if ($registrationtoken->scope == self::REG_TOKEN_OP_UPDATE_REG) {
$tool = lti_get_type($registrationtoken->sub);
if (!$tool) {
throw new registration_exception("Unknown client", 400);
}
$response['clientid'] = $tool->clientid ?? $this->new_clientid();
$response['type'] = $tool;
} else {
throw new registration_exception("Incorrect scope", 403);
}
return $response;
}
/**
* Initializes an array with the scopes for services supported by the LTI module
*
* @return array List of scopes
*/
public function lti_get_service_scopes() {
$services = lti_get_services();
$scopes = array();
foreach ($services as $service) {
$servicescopes = $service->get_scopes();
if (!empty($servicescopes)) {
$scopes = array_merge($scopes, $servicescopes);
}
}
return $scopes;
}
/**
* Generates a new client id string.
*
* @return string generated client id
*/
public function new_clientid(): string {
return random_string(15);
}
/**
* Base64 encoded signature for LTI 1.1 migration.
* @param string $key LTI 1.1 key
* @param string $salt Salt value
* @param string $secret LTI 1.1 secret
*
* @return string base64encoded hash
*/
public function sign(string $key, string $salt, string $secret): string {
return base64_encode(hash_hmac('sha-256', $key.$salt, $secret, true));
}
/**
* Returns a tool proxy
*
* @param int $proxyid
*
* @return mixed Tool Proxy details
*/
public function get_tool_proxy(int $proxyid): array {
return lti_get_tool_proxy($proxyid);
}
}
@@ -0,0 +1,389 @@
<?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/>.
/**
* This file contains an abstract definition of an LTI resource
*
* @package mod_lti
* @copyright 2014 Vital Source Technologies http://vitalsource.com
* @author Stephen Vickers
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_lti\local\ltiservice;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/mod/lti/locallib.php');
/**
* The mod_lti\local\ltiservice\resource_base class.
*
* @package mod_lti
* @since Moodle 2.8
* @copyright 2014 Vital Source Technologies http://vitalsource.com
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class resource_base {
/** HTTP Post method */
const HTTP_POST = 'POST';
/** HTTP Get method */
const HTTP_GET = 'GET';
/** HTTP Put method */
const HTTP_PUT = 'PUT';
/** HTTP Delete method */
const HTTP_DELETE = 'DELETE';
/** @var service_base Service associated with this resource. */
private $service;
/** @var string Type for this resource. */
protected $type;
/** @var string ID for this resource. */
protected $id;
/** @var string Template for this resource. */
protected $template;
/** @var array Custom parameter substitution variables associated with this resource. */
protected $variables;
/** @var array Media types supported by this resource. */
protected $formats;
/** @var array HTTP actions supported by this resource. */
protected $methods;
/** @var array Template variables parsed from the resource template. */
protected $params;
/**
* Class constructor.
*
* @param service_base $service Service instance
*/
public function __construct($service) {
$this->service = $service;
$this->type = 'RestService';
$this->id = null;
$this->template = null;
$this->methods = array();
$this->variables = array();
$this->formats = array();
$this->methods = array();
$this->params = null;
}
/**
* Get the resource ID.
*
* @return string
*/
public function get_id() {
return $this->id;
}
/**
* Get the resource template.
*
* @return string
*/
public function get_template() {
return $this->template;
}
/**
* Get the resource path.
*
* @return string
*/
public function get_path() {
return $this->get_template();
}
/**
* Get the resource type.
*
* @return string
*/
public function get_type() {
return $this->type;
}
/**
* Get the resource's service.
*
* @return service_base
*/
public function get_service() {
return $this->service;
}
/**
* Get the resource methods.
*
* @return array
*/
public function get_methods() {
return $this->methods;
}
/**
* Get the resource media types.
*
* @return array
*/
public function get_formats() {
return $this->formats;
}
/**
* Get the resource template variables.
*
* @return array
*/
public function get_variables() {
return $this->variables;
}
/**
* Get the resource fully qualified endpoint.
*
* @return string
*/
public function get_endpoint() {
$this->parse_template();
$template = preg_replace('/[\(\)]/', '', $this->get_template());
$url = $this->get_service()->get_service_path() . $template;
foreach ($this->params as $key => $value) {
$url = str_replace('{' . $key . '}', $value, $url);
}
$toolproxy = $this->get_service()->get_tool_proxy();
if (!empty($toolproxy)) {
$url = str_replace('{config_type}', 'toolproxy', $url);
$url = str_replace('{tool_proxy_id}', $toolproxy->guid, $url);
} else {
$url = str_replace('{config_type}', 'tool', $url);
$url = str_replace('{tool_proxy_id}', $this->get_service()->get_type()->id, $url);
}
return $url;
}
/**
* Execute the request for this resource.
*
* @param response $response Response object for this request.
*/
abstract public function execute($response);
/**
* Check to make sure the request is valid.
*
* @param int $typeid The typeid we want to use
* @param string $body Body of HTTP request message
* @param string[] $scopes Array of scope(s) required for incoming request
*
* @return boolean
*/
public function check_tool($typeid, $body = null, $scopes = null) {
$ok = $this->get_service()->check_tool($typeid, $body, $scopes);
if ($ok) {
if ($this->get_service()->get_tool_proxy()) {
$toolproxyjson = $this->get_service()->get_tool_proxy()->toolproxy;
}
if (!empty($toolproxyjson)) {
// Check tool proxy to ensure service being requested is included.
$toolproxy = json_decode($toolproxyjson);
if (!empty($toolproxy) && isset($toolproxy->security_contract->tool_service)) {
$contexts = lti_get_contexts($toolproxy);
$tpservices = $toolproxy->security_contract->tool_service;
foreach ($tpservices as $service) {
$fqid = lti_get_fqid($contexts, $service->service);
$id = explode('#', $fqid, 2);
if ($this->get_id() === $id[1]) {
$ok = true;
break;
}
}
}
if (!$ok) {
debugging('Requested service not permitted: ' . $this->get_id(), DEBUG_DEVELOPER);
}
} else {
// Check that the scope required for the service request is included in those granted for the
// access token being used.
$permittedscopes = $this->get_service()->get_permitted_scopes();
$ok = is_null($permittedscopes) || empty($scopes) || !empty(array_intersect($permittedscopes, $scopes));
}
}
return $ok;
}
/**
* Check to make sure the request is valid.
*
* @param string $toolproxyguid Consumer key
* @param string $body Body of HTTP request message
*
* @return boolean
* @deprecated since Moodle 3.7 MDL-62599 - please do not use this function any more.
* @see resource_base::check_tool()
*/
public function check_tool_proxy($toolproxyguid, $body = null) {
debugging('check_tool_proxy() is deprecated to allow LTI 1 connections to support services. ' .
'Please use resource_base::check_tool() instead.', DEBUG_DEVELOPER);
$ok = false;
if ($this->get_service()->check_tool_proxy($toolproxyguid, $body)) {
$toolproxyjson = $this->get_service()->get_tool_proxy()->toolproxy;
if (empty($toolproxyjson)) {
$ok = true;
} else {
$toolproxy = json_decode($toolproxyjson);
if (!empty($toolproxy) && isset($toolproxy->security_contract->tool_service)) {
$contexts = lti_get_contexts($toolproxy);
$tpservices = $toolproxy->security_contract->tool_service;
foreach ($tpservices as $service) {
$fqid = lti_get_fqid($contexts, $service->service);
$id = explode('#', $fqid, 2);
if ($this->get_id() === $id[1]) {
$ok = true;
break;
}
}
}
if (!$ok) {
debugging('Requested service not included in tool proxy: ' . $this->get_id());
}
}
}
return $ok;
}
/**
* Check to make sure the request is valid.
*
* @param int $typeid The typeid we want to use
* @param int $contextid The course we are at
* @param string $permissionrequested The permission to be checked
* @param string $body Body of HTTP request message
*
* @return boolean
* @deprecated since Moodle 3.7 MDL-62599 - please do not use this function any more.
* @see resource_base::check_tool()
*/
public function check_type($typeid, $contextid, $permissionrequested, $body = null) {
debugging('check_type() is deprecated to allow LTI 1 connections to support services. ' .
'Please use resource_base::check_tool() instead.', DEBUG_DEVELOPER);
$ok = false;
if ($this->get_service()->check_type($typeid, $contextid, $body)) {
$neededpermissions = $this->get_permissions($typeid);
foreach ($neededpermissions as $permission) {
if ($permission == $permissionrequested) {
$ok = true;
break;
}
}
if (!$ok) {
debugging('Requested service ' . $permissionrequested . ' not included in tool type: ' . $typeid,
DEBUG_DEVELOPER);
}
}
return $ok;
}
/**
* get permissions from the config of the tool for that resource
*
* @param int $ltitype Type of LTI
* @return array with the permissions related to this resource by the $ltitype or empty if none.
* @deprecated since Moodle 3.7 MDL-62599 - please do not use this function any more.
* @see resource_base::check_tool()
*/
public function get_permissions($ltitype) {
debugging('get_permissions() is deprecated to allow LTI 1 connections to support services. ' .
'Please use resource_base::check_tool() instead.', DEBUG_DEVELOPER);
return array();
}
/**
* Parse a value for custom parameter substitution variables.
*
* @param string $value String to be parsed
*
* @return string
*/
public function parse_value($value) {
return $value;
}
/**
* Parse the template for variables.
*
* @return array
*/
protected function parse_template() {
if (empty($this->params)) {
$this->params = array();
if (!empty($_SERVER['PATH_INFO'])) {
$path = explode('/', $_SERVER['PATH_INFO']);
$template = preg_replace('/\([0-9a-zA-Z_\-,\/]+\)/', '', $this->get_template());
$parts = explode('/', $template);
for ($i = 0; $i < count($parts); $i++) {
if ((substr($parts[$i], 0, 1) == '{') && (substr($parts[$i], -1) == '}')) {
$value = '';
if ($i < count($path)) {
$value = $path[$i];
}
$this->params[substr($parts[$i], 1, -1)] = $value;
}
}
}
}
return $this->params;
}
}
@@ -0,0 +1,263 @@
<?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/>.
/**
* This file contains an abstract definition of an LTI service
*
* @package mod_lti
* @copyright 2014 Vital Source Technologies http://vitalsource.com
* @author Stephen Vickers
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_lti\local\ltiservice;
defined('MOODLE_INTERNAL') || die;
/**
* The mod_lti\local\ltiservice\response class.
*
* @package mod_lti
* @since Moodle 2.8
* @copyright 2014 Vital Source Technologies http://vitalsource.com
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class response {
/** @var int HTTP response code. */
private $code;
/** @var string HTTP response reason. */
private $reason;
/** @var string HTTP request method. */
private $requestmethod;
/** @var string HTTP request accept header. */
private $accept;
/** @var string HTTP response content type. */
private $contenttype;
/** @var string HTTP request body. */
private $data;
/** @var string HTTP response body. */
private $body;
/** @var array HTTP response codes. */
private $responsecodes;
/** @var array HTTP additional headers. */
private $additionalheaders;
/**
* Class constructor.
*/
public function __construct() {
$this->code = 200;
$this->reason = '';
$this->requestmethod = $_SERVER['REQUEST_METHOD'];
$this->accept = '';
$this->contenttype = '';
$this->data = '';
$this->body = '';
$this->responsecodes = array(
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
300 => 'Multiple Choices',
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
415 => 'Unsupported Media Type',
500 => 'Internal Server Error',
501 => 'Not Implemented'
);
$this->additionalheaders = array();
}
/**
* Get the response code.
*
* @return int
*/
public function get_code() {
return $this->code;
}
/**
* Set the response code.
*
* @param int $code Response code
*/
public function set_code($code) {
$this->code = $code;
$this->reason = '';
}
/**
* Get the response reason.
*
* @return string
*/
public function get_reason() {
$code = $this->code;
if (($code < 200) || ($code >= 600)) {
$code = 500; // Status code must be between 200 and 599.
}
if (empty($this->reason) && array_key_exists($code, $this->responsecodes)) {
$this->reason = $this->responsecodes[$code];
}
// Use generic reason for this category (based on first digit) if a specific reason is not defined.
if (empty($this->reason)) {
$this->reason = $this->responsecodes[intval($code / 100) * 100];
}
return $this->reason;
}
/**
* Set the response reason.
*
* @param string $reason Reason
*/
public function set_reason($reason) {
$this->reason = $reason;
}
/**
* Get the request method.
*
* @return string
*/
public function get_request_method() {
return $this->requestmethod;
}
/**
* Get the request accept header.
*
* @return string
*/
public function get_accept() {
return $this->accept;
}
/**
* Set the request accept header.
*
* @param string $accept Accept header value
*/
public function set_accept($accept) {
$this->accept = $accept;
}
/**
* Get the response content type.
*
* @return string
*/
public function get_content_type() {
return $this->contenttype;
}
/**
* Set the response content type.
*
* @param string $contenttype Content type
*/
public function set_content_type($contenttype) {
$this->contenttype = $contenttype;
}
/**
* Get the request body.
*
* @return string
*/
public function get_request_data() {
return $this->data;
}
/**
* Set the response body.
*
* @param string $data Body data
*/
public function set_request_data($data) {
$this->data = $data;
}
/**
* Get the response body.
*
* @return string
*/
public function get_body() {
return $this->body;
}
/**
* Set the response body.
*
* @param string $body Body data
*/
public function set_body($body) {
$this->body = $body;
}
/**
* Add an additional header.
*
* @param string $header The new header
*/
public function add_additional_header($header) {
array_push($this->additionalheaders, $header);
}
/**
* Send the response.
*/
public function send() {
header("HTTP/1.0 {$this->code} {$this->get_reason()}");
foreach ($this->additionalheaders as $header) {
header($header);
}
if ((($this->code >= 200) && ($this->code < 300)) || !empty($this->body)) {
if (!empty($this->contenttype)) {
header("Content-Type: {$this->contenttype}; charset=utf-8");
}
if (!empty($this->body)) {
echo $this->body;
}
} else if ($this->code >= 400) {
header("Content-Type: application/json; charset=utf-8");
$body = new \stdClass();
$body->status = $this->code;
$body->reason = $this->get_reason();
$body->request = new \stdClass();
$body->request->method = $_SERVER['REQUEST_METHOD'];
$body->request->url = $_SERVER['REQUEST_URI'];
if (isset($_SERVER['HTTP_ACCEPT'])) {
$body->request->accept = $_SERVER['HTTP_ACCEPT'];
}
if (isset($_SERVER['CONTENT_TYPE'])) {
$body->request->contentType = explode(';', $_SERVER['CONTENT_TYPE'], 2)[0];
}
echo json_encode($body, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
}
}
}
@@ -0,0 +1,548 @@
<?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/>.
/**
* This file contains an abstract definition of an LTI service
*
* @package mod_lti
* @copyright 2014 Vital Source Technologies http://vitalsource.com
* @author Stephen Vickers
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_lti\local\ltiservice;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/mod/lti/locallib.php');
require_once($CFG->dirroot . '/mod/lti/OAuthBody.php');
// TODO: Switch to core oauthlib once implemented - MDL-30149.
use moodle\mod\lti as lti;
use stdClass;
/**
* The mod_lti\local\ltiservice\service_base class.
*
* @package mod_lti
* @since Moodle 2.8
* @copyright 2014 Vital Source Technologies http://vitalsource.com
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class service_base {
/** Label representing an LTI 2 message type */
const LTI_VERSION2P0 = 'LTI-2p0';
/** Service enabled */
const SERVICE_ENABLED = 1;
/** @var string ID for the service. */
protected $id;
/** @var string Human readable name for the service. */
protected $name;
/** @var boolean <code>true</code> if requests for this service do not need to be signed. */
protected $unsigned;
/** @var stdClass Tool proxy object for the current service request. */
private $toolproxy;
/** @var stdClass LTI type object for the current service request. */
private $type;
/** @var array LTI type config array for the current service request. */
private $typeconfig;
/** @var array Instances of the resources associated with this service. */
protected $resources;
/**
* Class constructor.
*/
public function __construct() {
$this->id = null;
$this->name = null;
$this->unsigned = false;
$this->toolproxy = null;
$this->type = null;
$this->typeconfig = null;
$this->resources = null;
}
/**
* Get the service ID.
*
* @return string
*/
public function get_id() {
return $this->id;
}
/**
* Get the service compoent ID.
*
* @return string
*/
public function get_component_id() {
return 'ltiservice_' . $this->id;
}
/**
* Get the service name.
*
* @return string
*/
public function get_name() {
return $this->name;
}
/**
* Get whether the service requests need to be signed.
*
* @return boolean
*/
public function is_unsigned() {
return $this->unsigned;
}
/**
* Get the tool proxy object.
*
* @return stdClass
*/
public function get_tool_proxy() {
return $this->toolproxy;
}
/**
* Set the tool proxy object.
*
* @param object $toolproxy The tool proxy for this service request
*
* @var stdClass
*/
public function set_tool_proxy($toolproxy) {
$this->toolproxy = $toolproxy;
}
/**
* Get the type object.
*
* @return stdClass
*/
public function get_type() {
return $this->type;
}
/**
* Set the LTI type object.
*
* @param object $type The LTI type for this service request
*
* @var stdClass
*/
public function set_type($type) {
$this->type = $type;
}
/**
* Get the type config array.
*
* @return array|null
*/
public function get_typeconfig() {
return $this->typeconfig;
}
/**
* Set the LTI type config object.
*
* @param array $typeconfig The LTI type config for this service request
*
* @var array
*/
public function set_typeconfig($typeconfig) {
$this->typeconfig = $typeconfig;
}
/**
* Get the resources for this service.
*
* @return resource_base[]
*/
abstract public function get_resources();
/**
* Get the scope(s) permitted for this service in the context of a particular tool type.
*
* A null value indicates that no scopes are required to access the service.
*
* @return array|null
*/
public function get_permitted_scopes() {
return null;
}
/**
* Get the scope(s) permitted for this service.
*
* A null value indicates that no scopes are required to access the service.
*
* @return array|null
*/
public function get_scopes() {
return null;
}
/**
* Returns the configuration options for this service.
*
* @param \MoodleQuickForm $mform Moodle quickform object definition
*/
public function get_configuration_options(&$mform) {
}
/**
* Called when a new LTI Instance is added.
*
* @param object $lti LTI Instance.
*/
public function instance_added(object $lti): void {
}
/**
* Called when a new LTI Instance is updated.
*
* @param object $lti LTI Instance.
*/
public function instance_updated(object $lti): void {
}
/**
* Called when the launch data is created, offering a possibility to alter the
* target link URI.
*
* @param string $messagetype message type for this launch
* @param string $targetlinkuri current target link uri
* @param null|string $customstr concatenated list of custom parameters
* @param int $courseid
* @param null|object $lti LTI Instance.
*
* @return array containing the target link URL and the custom params string to use.
*/
public function override_endpoint(string $messagetype, string $targetlinkuri,
?string $customstr, int $courseid, ?object $lti = null): array {
return [$targetlinkuri, $customstr];
}
/**
* Called when a new LTI Instance is deleted.
*
* @param int $id LTI Instance.
*/
public function instance_deleted(int $id): void {
}
/**
* Set the form data when displaying the LTI Instance form.
*
* @param object $defaultvalues Default form values.
*/
public function set_instance_form_values(object $defaultvalues): void {
}
/**
* Return an array with the names of the parameters that the service will be saving in the configuration
*
* @return array Names list of the parameters that the service will be saving in the configuration
* @deprecated since Moodle 3.7 - please do not use this function any more.
*/
public function get_configuration_parameter_names() {
debugging('get_configuration_parameter_names() has been deprecated.', DEBUG_DEVELOPER);
return array();
}
/**
* Default implementation will check for the existence of at least one mod_lti entry for that tool and context.
*
* It may be overridden if other inferences can be done.
*
* Ideally a Site Tool should be explicitly engaged with a course, the check on the presence of a link is a proxy
* to infer a Site Tool engagement until an explicit Site Tool - Course relationship exists.
*
* @param int $typeid The tool lti type id.
* @param int $courseid The course id.
* @return bool returns True if tool is used in context, false otherwise.
*/
public function is_used_in_context($typeid, $courseid) {
global $DB;
$ok = $DB->record_exists('lti', array('course' => $courseid, 'typeid' => $typeid));
return $ok || $DB->record_exists('lti_types', array('course' => $courseid, 'id' => $typeid));
}
/**
* Checks if there is a site tool or a course tool for this site.
*
* @param int $typeid The tool lti type id.
* @param int $courseid The course id.
* @return bool returns True if tool is allowed in context, false otherwise.
*/
public function is_allowed_in_context($typeid, $courseid) {
global $DB;
// Check if it is a Course tool for this course or a Site tool.
$type = $DB->get_record('lti_types', array('id' => $typeid));
return $type && ($type->course == $courseid || $type->course == SITEID);
}
/**
* Return an array of key/values to add to the launch parameters.
*
* @param string $messagetype 'basic-lti-launch-request' or 'ContentItemSelectionRequest'.
* @param string $courseid The course id.
* @param string $userid The user id.
* @param string $typeid The tool lti type id.
* @param string $modlti The id of the lti activity.
*
* The type is passed to check the configuration and not return parameters for services not used.
*
* @return array Key/value pairs to add as launch parameters.
*/
public function get_launch_parameters($messagetype, $courseid, $userid, $typeid, $modlti = null) {
return array();
}
/**
* Return an array of key/claim mapping allowing LTI 1.1 custom parameters
* to be transformed to LTI 1.3 claims.
*
* @return array Key/value pairs of params to claim mapping.
*/
public function get_jwt_claim_mappings(): array {
return [];
}
/**
* Get the path for service requests.
*
* @return string
*/
public static function get_service_path() {
$url = new \moodle_url('/mod/lti/services.php');
return $url->out(false);
}
/**
* Parse a string for custom substitution parameter variables supported by this service's resources.
*
* @param string $value Value to be parsed
*
* @return string
*/
public function parse_value($value) {
if (empty($this->resources)) {
$this->resources = $this->get_resources();
}
if (!empty($this->resources)) {
foreach ($this->resources as $resource) {
$value = $resource->parse_value($value);
}
}
return $value;
}
/**
* Check that the request has been properly signed and is permitted.
*
* @param string $typeid LTI type ID
* @param string $body Request body (null if none)
* @param string[] $scopes Array of required scope(s) for incoming request
*
* @return boolean
*/
public function check_tool($typeid, $body = null, $scopes = null) {
$ok = true;
$toolproxy = null;
$consumerkey = lti\get_oauth_key_from_headers($typeid, $scopes);
if ($consumerkey === false) {
$ok = $this->is_unsigned();
} else {
if (empty($typeid) && is_int($consumerkey)) {
$typeid = $consumerkey;
}
if (!empty($typeid)) {
$this->type = lti_get_type($typeid);
$this->typeconfig = lti_get_type_config($typeid);
$ok = !empty($this->type->id);
if ($ok && !empty($this->type->toolproxyid)) {
$this->toolproxy = lti_get_tool_proxy($this->type->toolproxyid);
}
} else {
$toolproxy = lti_get_tool_proxy_from_guid($consumerkey);
if ($toolproxy !== false) {
$this->toolproxy = $toolproxy;
}
}
}
if ($ok && is_string($consumerkey)) {
if (!empty($this->toolproxy)) {
$key = $this->toolproxy->guid;
$secret = $this->toolproxy->secret;
} else {
$key = $this->typeconfig['resourcekey'];
$secret = $this->typeconfig['password'];
}
if (!$this->is_unsigned() && ($key == $consumerkey)) {
$ok = $this->check_signature($key, $secret, $body);
} else {
$ok = $this->is_unsigned();
}
}
return $ok;
}
/**
* Check that the request has been properly signed.
*
* @param string $toolproxyguid Tool Proxy GUID
* @param string $body Request body (null if none)
*
* @return boolean
* @deprecated since Moodle 3.7 MDL-62599 - please do not use this function any more.
* @see service_base::check_tool()
*/
public function check_tool_proxy($toolproxyguid, $body = null) {
debugging('check_tool_proxy() is deprecated to allow LTI 1 connections to support services. ' .
'Please use service_base::check_tool() instead.', DEBUG_DEVELOPER);
$ok = false;
$toolproxy = null;
$consumerkey = lti\get_oauth_key_from_headers();
if (empty($toolproxyguid)) {
$toolproxyguid = $consumerkey;
}
if (!empty($toolproxyguid)) {
$toolproxy = lti_get_tool_proxy_from_guid($toolproxyguid);
if ($toolproxy !== false) {
if (!$this->is_unsigned() && ($toolproxy->guid == $consumerkey)) {
$ok = $this->check_signature($toolproxy->guid, $toolproxy->secret, $body);
} else {
$ok = $this->is_unsigned();
}
}
}
if ($ok) {
$this->toolproxy = $toolproxy;
}
return $ok;
}
/**
* Check that the request has been properly signed.
*
* @param int $typeid The tool id
* @param int $courseid The course we are at
* @param string $body Request body (null if none)
*
* @return bool
* @deprecated since Moodle 3.7 MDL-62599 - please do not use this function any more.
* @see service_base::check_tool()
*/
public function check_type($typeid, $courseid, $body = null) {
debugging('check_type() is deprecated to allow LTI 1 connections to support services. ' .
'Please use service_base::check_tool() instead.', DEBUG_DEVELOPER);
$ok = false;
$tool = null;
$consumerkey = lti\get_oauth_key_from_headers();
if (empty($typeid)) {
return $ok;
} else if ($this->is_allowed_in_context($typeid, $courseid)) {
$tool = lti_get_type_type_config($typeid);
if ($tool !== false) {
if (!$this->is_unsigned() && ($tool->lti_resourcekey == $consumerkey)) {
$ok = $this->check_signature($tool->lti_resourcekey, $tool->lti_password, $body);
} else {
$ok = $this->is_unsigned();
}
}
}
return $ok;
}
/**
* Check the request signature.
*
* @param string $consumerkey Consumer key
* @param string $secret Shared secret
* @param string $body Request body
*
* @return boolean
*/
private function check_signature($consumerkey, $secret, $body) {
$ok = true;
try {
// TODO: Switch to core oauthlib once implemented - MDL-30149.
lti\handle_oauth_body_post($consumerkey, $secret, $body);
} catch (\Exception $e) {
debugging($e->getMessage() . "\n");
$ok = false;
}
return $ok;
}
}
+142
View File
@@ -0,0 +1,142 @@
<?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/>.
namespace mod_lti\local;
use core\context\course;
/**
* Helper class specifically dealing with LTI types (preconfigured tools).
*
* @package mod_lti
* @copyright 2023 Jake Dallimore <jrhdallimore@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class types_helper {
/**
* Returns all LTI tool types (preconfigured tools) visible in the given course and for the given user.
*
* This list will contain both site level tools and course-level tools.
*
* @param int $courseid the id of the course.
* @param int $userid the id of the user.
* @param array $coursevisible options for 'coursevisible' field, which will default to
* [LTI_COURSEVISIBLE_PRECONFIGURED, LTI_COURSEVISIBLE_ACTIVITYCHOOSER] if omitted.
* @return \stdClass[] the array of tool type objects.
*/
public static function get_lti_types_by_course(int $courseid, int $userid, array $coursevisible = []): array {
global $DB, $SITE;
if (!has_capability('mod/lti:addpreconfiguredinstance', course::instance($courseid), $userid)) {
return [];
}
if (empty($coursevisible)) {
$coursevisible = [LTI_COURSEVISIBLE_PRECONFIGURED, LTI_COURSEVISIBLE_ACTIVITYCHOOSER];
}
[$coursevisiblesql, $coursevisparams] = $DB->get_in_or_equal($coursevisible, SQL_PARAMS_NAMED, 'coursevisible');
[$coursevisiblesql1, $coursevisparams1] = $DB->get_in_or_equal($coursevisible, SQL_PARAMS_NAMED, 'coursevisible');
[$coursevisibleoverriddensql, $coursevisoverriddenparams] = $DB->get_in_or_equal(
$coursevisible,
SQL_PARAMS_NAMED,
'coursevisibleoverridden');
$coursecond = implode(" OR ", ["t.course = :courseid", "t.course = :siteid"]);
$coursecategory = $DB->get_field('course', 'category', ['id' => $courseid]);
$query = "SELECT *
FROM (SELECT t.*, c.coursevisible as coursevisibleoverridden
FROM {lti_types} t
LEFT JOIN {lti_types_categories} tc ON t.id = tc.typeid
LEFT JOIN {lti_coursevisible} c ON c.typeid = t.id AND c.courseid = $courseid
WHERE (t.coursevisible $coursevisiblesql
OR (c.coursevisible $coursevisiblesql1 AND t.coursevisible NOT IN (:lticoursevisibleno)))
AND ($coursecond)
AND t.state = :active
AND (tc.id IS NULL OR tc.categoryid = :categoryid)) tt
WHERE tt.coursevisibleoverridden IS NULL
OR tt.coursevisibleoverridden $coursevisibleoverriddensql";
return $DB->get_records_sql(
$query,
[
'siteid' => $SITE->id,
'courseid' => $courseid,
'active' => LTI_TOOL_STATE_CONFIGURED,
'categoryid' => $coursecategory,
'coursevisible' => LTI_COURSEVISIBLE_ACTIVITYCHOOSER,
'lticoursevisibleno' => LTI_COURSEVISIBLE_NO,
] + $coursevisparams + $coursevisparams1 + $coursevisoverriddenparams
);
}
/**
* Override coursevisible for a given tool on course level.
*
* @param int $tooltypeid Type ID
* @param int $courseid Course ID
* @param \core\context\course $context Course context
* @param bool $showinactivitychooser Show or not show in activity chooser
* @return bool True if the coursevisible was changed, false otherwise.
*/
public static function override_type_showinactivitychooser(int $tooltypeid, int $courseid, \core\context\course $context, bool $showinactivitychooser): bool {
global $DB;
require_capability('mod/lti:addcoursetool', $context);
$ltitype = lti_get_type($tooltypeid);
if ($ltitype && ($ltitype->coursevisible != LTI_COURSEVISIBLE_NO)) {
$coursevisible = $showinactivitychooser ? LTI_COURSEVISIBLE_ACTIVITYCHOOSER : LTI_COURSEVISIBLE_PRECONFIGURED;
$ltitype->coursevisible = $coursevisible;
$config = new \stdClass();
$config->lti_coursevisible = $coursevisible;
if (intval($ltitype->course) != intval(get_site()->id)) {
// It is course tool - just update it.
lti_update_type($ltitype, $config);
} else {
$coursecategory = $DB->get_field('course', 'category', ['id' => $courseid]);
$sql = "SELECT COUNT(*) AS count
FROM {lti_types_categories} tc
WHERE tc.typeid = :typeid";
$restrictedtool = $DB->count_records_sql($sql, ['typeid' => $tooltypeid]);
if ($restrictedtool) {
$record = $DB->get_record('lti_types_categories', ['typeid' => $tooltypeid, 'categoryid' => $coursecategory]);
if (!$record) {
throw new \moodle_exception('You are not allowed to change this setting for this tool.');
}
}
// This is site tool, but we would like to have course level setting for it.
$lticoursevisible = $DB->get_record('lti_coursevisible', ['typeid' => $tooltypeid, 'courseid' => $courseid]);
if (!$lticoursevisible) {
$lticoursevisible = new \stdClass();
$lticoursevisible->typeid = $tooltypeid;
$lticoursevisible->courseid = $courseid;
$lticoursevisible->coursevisible = $coursevisible;
$DB->insert_record('lti_coursevisible', $lticoursevisible);
} else {
$lticoursevisible->coursevisible = $coursevisible;
$DB->update_record('lti_coursevisible', $lticoursevisible);
}
}
return true;
}
return false;
}
}
@@ -0,0 +1,77 @@
<?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/>.
namespace mod_lti\output;
use core_reportbuilder\system_report_factory;
use mod_lti\reportbuilder\local\systemreports\course_external_tools_list;
/**
* The course tools page renderable, containing a page header renderable and a course tools system report.
*
* @package mod_lti
* @copyright 2023 Jake Dallimore <jrhdallimore@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class course_tools_page implements \renderable {
/** @var course_external_tools_list the course tools system report instance. */
protected course_external_tools_list $coursetoolsreport;
/** @var course_tools_page_header the page header renderable instance. */
protected course_tools_page_header $coursetoolspageheader;
/**
* Renderable constructor.
*
* @param int $courseid the id of the course.
*/
public function __construct(int $courseid) {
global $DB;
$context = \context_course::instance($courseid);
// Page intro, zero state and 'add new' button.
$canadd = has_capability('mod/lti:addcoursetool', $context);
$sql = 'SELECT COUNT(1)
FROM {lti_types} tt
WHERE tt.course IN(:siteid, :courseid)
AND tt.coursevisible NOT IN(:coursevisible)';
$toolcount = $DB->count_records_sql($sql, ['siteid' => get_site()->id, 'courseid' => $courseid, 'coursevisible' => 0]);
$this->coursetoolspageheader = new course_tools_page_header($courseid, $toolcount, $canadd);
// Course tools report itself.
$this->coursetoolsreport = system_report_factory::create(course_external_tools_list::class, $context);
}
/**
* Get the course tools page header renderable.
*
* @return course_tools_page_header the renderable.
*/
public function get_header(): course_tools_page_header {
return $this->coursetoolspageheader;
}
/**
* Get the course tools list system report.
*
* @return course_external_tools_list the course tools list report.
*/
public function get_table(): course_external_tools_list {
return $this->coursetoolsreport;
}
}
@@ -0,0 +1,62 @@
<?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/>.
namespace mod_lti\output;
use core\output\notification;
use renderer_base;
/**
* Course tools page header renderable, containing the data for the page zero state and 'add tool' button.
*
* @package mod_lti
* @copyright 2023 Jake Dallimore <jrhdallimore@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class course_tools_page_header implements \templatable {
/**
* Constructor.
*
* @param int $courseid the course id.
* @param int $toolcount the number of tools available in the course.
* @param bool $canadd whether the user can add tools to the course or not.
*/
public function __construct(protected int $courseid, protected int $toolcount, protected bool $canadd) {
}
/**
* Export the header's data for template use.
*
* @param renderer_base $output
* @return object the data.
*/
public function export_for_template(renderer_base $output): \stdClass {
$context = (object) [];
if ($this->canadd) {
$context->addlink = (new \moodle_url('/mod/lti/coursetooledit.php', ['course' => $this->courseid]))->out();
}
if ($this->toolcount == 0) {
$notification = new notification(get_string('nocourseexternaltoolsnotice', 'mod_lti'), notification::NOTIFY_INFO, true);
$context->notoolsnotice = $notification->export_for_template($output);
}
return $context;
}
}
@@ -0,0 +1,50 @@
<?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 containing data for external registration return page.
*
* @package mod_lti
* @copyright 2015 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_lti\output;
require_once($CFG->dirroot.'/mod/lti/locallib.php');
use renderable;
use templatable;
use renderer_base;
use stdClass;
/**
* Class containing data for tool_configure page
*
* @copyright 2015 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class external_registration_return_page implements renderable, templatable {
/**
* Export this data so it can be used as the context for a mustache template.
*
* @param renderer_base $output The renderer
* @return stdClass Data to be used by the template
*/
public function export_for_template(renderer_base $output) {
return new stdClass();
}
}
@@ -0,0 +1,75 @@
<?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 containing data for rendering LTI upgrade choices page.
*
* @copyright 2021 Cengage
* @package mod_lti
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_lti\output;
defined('MOODLE_INTERNAL') || die;
require_once($CFG->dirroot.'/mod/lti/locallib.php');
use renderable;
use templatable;
use renderer_base;
use stdClass;
/**
* Class containing data for rendering LTI upgrade choices page.
*
* @copyright 2021 Cengage
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class registration_upgrade_choice_page implements renderable, templatable {
/** @var array array of tools. */
protected array $tools = [];
/** @var string tool URL. */
protected string $startregurl;
/**
* Constructor
*
* @param array $tools array of tools that can be upgraded
* @param string $startregurl tool URL to start the registration process
*/
public function __construct(array $tools, string $startregurl) {
$this->tools = $tools;
$this->startregurl = $startregurl;
}
/**
* Export this data so it can be used as the context for a mustache template.
*
* @param renderer_base $output The renderer
* @return stdClass Data to be used by the template
*/
public function export_for_template(renderer_base $output) {
$renderdata = new stdClass();
$renderdata->startregurlenc = urlencode($this->startregurl);
$renderdata->sesskey = sesskey();
$renderdata->tools = [];
foreach ($this->tools as $tool) {
$renderdata->tools[] = (object)$tool;
}
return $renderdata;
}
}
+103
View File
@@ -0,0 +1,103 @@
<?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/>.
/**
* Renderer class for template library.
*
* @package mod_lti
* @copyright 2015 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_lti\output;
defined('MOODLE_INTERNAL') || die;
use plugin_renderer_base;
/**
* Renderer class for template library.
*
* @package mod_lti
* @copyright 2015 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class renderer extends plugin_renderer_base {
/**
* Defer to template.
*
* @param tool_configure_page $page
*
* @return string html for the page
*/
public function render_tool_configure_page($page) {
$data = $page->export_for_template($this);
return parent::render_from_template('mod_lti/tool_configure', $data);
}
/**
* Render the external registration return page
*
* @param tool_configure_page $page
*
* @return string html for the page
*/
public function render_external_registration_return_page($page) {
$data = $page->export_for_template($this);
return parent::render_from_template('mod_lti/external_registration_return', $data);
}
/**
* Render the external registration return page
*
* @param tool_configure_page $page
*
* @return string html for the page
*/
public function render_registration_upgrade_choice_page($page) {
$data = $page->export_for_template($this);
return parent::render_from_template('mod_lti/registration_upgrade_choice_page', $data);
}
/**
* Render the reposting of the cross site request.
*
* @param repost_crosssite_page $page the page renderable.
*
* @return string rendered html for the page.
*/
public function render_repost_crosssite_page(repost_crosssite_page $page): string {
$data = $page->export_for_template($this);
return parent::render_from_template('mod_lti/repost_crosssite', $data);
}
/**
* Render the course tools page header.
*
* @param course_tools_page $page the page renderable.
* @return string the rendered html for the page.
*/
protected function render_course_tools_page(course_tools_page $page): string {
// Render the table header templatable + the report.
$headerrenderable = $page->get_header();
$table = $page->get_table();
$headercontext = $headerrenderable->export_for_template($this);
$headeroutput = parent::render_from_template('mod_lti/course_tools_page_header', $headercontext);
return $headeroutput . $table->output();
}
}
@@ -0,0 +1,82 @@
<?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/>.
/**
* Render a page containing a simple form which reposts to self via JS.
*
* The purpose of this form is to resend a cross-site request to self, which allows the browsers to include the Moodle
* session cookie alongside the original POST data, allowing LTI flows to function despite browsers blocking
* cross-site cookies.
*
* @copyright 2021 Cengage
* @package mod_lti
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_lti\output;
defined('MOODLE_INTERNAL') || die;
require_once($CFG->dirroot.'/mod/lti/locallib.php');
use renderable;
use templatable;
use renderer_base;
use stdClass;
/**
* Render a page containing a simple form which reposts to self via JS.
*
* The purpose of this form is to resend a cross-site request to self, which allows the browsers to include the Moodle
* session cookie alongside the original POST data, allowing LTI flows to function despite browsers blocking
* cross-site cookies.
*
* @copyright 2021 Cengage
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class repost_crosssite_page implements renderable, templatable {
/** @var array POST params. */
protected $params;
/** @var string URL to repost to. */
protected string $url;
/**
* Constructor
*
* @param string $url moodle URL to repost to
* @param array $post the POST params to be re-posted
*/
public function __construct(string $url, array $post) {
$this->params = array_map(function($k) use ($post) {
return ["key" => $k, "value" => $post[$k]];
}, array_keys($post));
$this->url = $url;
}
/**
* Export this data so it can be used as the context for a mustache template.
*
* @param renderer_base $output The renderer
* @return stdClass Data to be used by the template
*/
public function export_for_template(renderer_base $output) {
$renderdata = new stdClass();
$renderdata->url = $this->url;
$renderdata->params = $this->params;
return $renderdata;
}
}
@@ -0,0 +1,66 @@
<?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 containing data for tool_configure page
*
* @package mod_lti
* @copyright 2015 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_lti\output;
defined('MOODLE_INTERNAL') || die;
require_once($CFG->dirroot.'/mod/lti/locallib.php');
use moodle_url;
use renderable;
use templatable;
use renderer_base;
use stdClass;
use help_icon;
/**
* Class containing data for tool_configure page
*
* @copyright 2015 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class tool_configure_page implements renderable, templatable {
/**
* Export this data so it can be used as the context for a mustache template.
*
* @param renderer_base $output The renderer
* @return stdClass
*/
public function export_for_template(renderer_base $output) {
$data = new stdClass();
$keyhelp = new help_icon('resourcekey', 'mod_lti');
$secrethelp = new help_icon('password', 'mod_lti');
$url = new moodle_url('/mod/lti/typessettings.php', array('sesskey' => sesskey(), 'returnto' => 'toolconfigure'));
$data->configuremanualurl = $url->out();
$url = new moodle_url('/admin/settings.php?section=modsettinglti');
$data->managetoolsurl = $url->out();
$url = new moodle_url('/mod/lti/toolproxies.php');
$data->managetoolproxiesurl = $url->out();
$data->keyhelp = $keyhelp->export_for_template($output);
$data->secrethelp = $secrethelp->export_for_template($output);
return $data;
}
}
+59
View File
@@ -0,0 +1,59 @@
<?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/>.
/**
* LTI service plugin info.
*
* @package mod_lti
* @copyright 2014 Vital Source Technologies http://vitalsource.com
* @author Stephen Vickers
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_lti\plugininfo;
use core\plugininfo\base;
defined('MOODLE_INTERNAL') || die();
/**
* The mod_lti\plugininfo\ltiservice class.
*
* @package mod_lti
* @since Moodle 2.8
* @copyright 2014 Vital Source Technologies http://vitalsource.com
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class ltiservice extends base {
/**
* Should there be a way to uninstall the plugin via the administration UI?
*
* Uninstallation is not allowed for core subplugins.
*
* @return boolean
*/
public function is_uninstall_allowed() {
if ($this->is_standard()) {
return false;
}
return true;
}
}
+81
View File
@@ -0,0 +1,81 @@
<?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/>.
/**
* LTI source plugin info.
*
* @package mod_lti
* @copyright 2013 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_lti\plugininfo;
use core\plugininfo\base;
defined('MOODLE_INTERNAL') || die();
class ltisource extends base {
/**
* Returns the node name used in admin settings menu for this plugin settings (if applicable)
*
* @return null|string node name or null if plugin does not create settings node (default)
*/
public function get_settings_section_name() {
return 'ltisourcesetting'.$this->name;
}
/**
* Loads plugin settings to the settings tree
*
* This function usually includes settings.php file in plugins folder.
* Alternatively it can create a link to some settings page (instance of admin_externalpage)
*
* @param \part_of_admin_tree $adminroot
* @param string $parentnodename
* @param bool $hassiteconfig whether the current user has moodle/site:config capability
*/
public function load_settings(\part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig) {
global $CFG, $USER, $DB, $OUTPUT, $PAGE; // In case settings.php wants to refer to them.
$ADMIN = $adminroot; // May be used in settings.php.
$plugininfo = $this; // Also can be used inside settings.php.
if (!$this->is_installed_and_upgraded()) {
return;
}
if (!$hassiteconfig or !file_exists($this->full_path('settings.php'))) {
return;
}
$section = $this->get_settings_section_name();
$settings = new \admin_settingpage($section, $this->displayname,
'moodle/site:config', $this->is_enabled() === false);
include($this->full_path('settings.php')); // This may also set $settings to null.
if ($settings) {
$ADMIN->add($parentnodename, $settings);
}
}
/**
* Should there be a way to uninstall the plugin via the administration UI.
*
* @return bool
*/
public function is_uninstall_allowed() {
return true;
}
}
+465
View File
@@ -0,0 +1,465 @@
<?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/>.
/**
* Privacy Subsystem implementation for mod_lti.
*
* @package mod_lti
* @copyright 2018 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_lti\privacy;
use core_privacy\local\metadata\collection;
use core_privacy\local\request\approved_contextlist;
use core_privacy\local\request\approved_userlist;
use core_privacy\local\request\contextlist;
use core_privacy\local\request\helper;
use core_privacy\local\request\transform;
use core_privacy\local\request\userlist;
use core_privacy\local\request\writer;
defined('MOODLE_INTERNAL') || die();
/**
* Privacy Subsystem implementation for mod_lti.
*
* @copyright 2018 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements
\core_privacy\local\metadata\provider,
\core_privacy\local\request\core_userlist_provider,
\core_privacy\local\request\plugin\provider {
/**
* Return the fields which contain personal data.
*
* @param collection $items a reference to the collection to use to store the metadata.
* @return collection the updated collection of metadata items.
*/
public static function get_metadata(collection $items): collection {
$items->add_external_location_link(
'lti_provider',
[
'userid' => 'privacy:metadata:userid',
'username' => 'privacy:metadata:username',
'useridnumber' => 'privacy:metadata:useridnumber',
'firstname' => 'privacy:metadata:firstname',
'lastname' => 'privacy:metadata:lastname',
'fullname' => 'privacy:metadata:fullname',
'email' => 'privacy:metadata:email',
'role' => 'privacy:metadata:role',
'courseid' => 'privacy:metadata:courseid',
'courseidnumber' => 'privacy:metadata:courseidnumber',
'courseshortname' => 'privacy:metadata:courseshortname',
'coursefullname' => 'privacy:metadata:coursefullname',
],
'privacy:metadata:externalpurpose'
);
$items->add_database_table(
'lti_submission',
[
'userid' => 'privacy:metadata:lti_submission:userid',
'datesubmitted' => 'privacy:metadata:lti_submission:datesubmitted',
'dateupdated' => 'privacy:metadata:lti_submission:dateupdated',
'gradepercent' => 'privacy:metadata:lti_submission:gradepercent',
'originalgrade' => 'privacy:metadata:lti_submission:originalgrade',
],
'privacy:metadata:lti_submission'
);
$items->add_database_table(
'lti_tool_proxies',
[
'name' => 'privacy:metadata:lti_tool_proxies:name',
'createdby' => 'privacy:metadata:createdby',
'timecreated' => 'privacy:metadata:timecreated',
'timemodified' => 'privacy:metadata:timemodified'
],
'privacy:metadata:lti_tool_proxies'
);
$items->add_database_table(
'lti_types',
[
'name' => 'privacy:metadata:lti_types:name',
'createdby' => 'privacy:metadata:createdby',
'timecreated' => 'privacy:metadata:timecreated',
'timemodified' => 'privacy:metadata:timemodified'
],
'privacy:metadata:lti_types'
);
return $items;
}
/**
* Get the list of contexts that contain user information for the specified user.
*
* @param int $userid the userid.
* @return contextlist the list of contexts containing user info for the user.
*/
public static function get_contexts_for_userid(int $userid): contextlist {
// Fetch all LTI submissions.
$sql = "SELECT c.id
FROM {context} c
INNER JOIN {course_modules} cm
ON cm.id = c.instanceid
AND c.contextlevel = :contextlevel
INNER JOIN {modules} m
ON m.id = cm.module
AND m.name = :modname
INNER JOIN {lti} lti
ON lti.id = cm.instance
INNER JOIN {lti_submission} ltisub
ON ltisub.ltiid = lti.id
WHERE ltisub.userid = :userid";
$params = [
'modname' => 'lti',
'contextlevel' => CONTEXT_MODULE,
'userid' => $userid,
];
$contextlist = new contextlist();
$contextlist->add_from_sql($sql, $params);
// Fetch all LTI types.
$sql = "SELECT c.id
FROM {context} c
JOIN {course} course
ON c.contextlevel = :contextlevel
AND c.instanceid = course.id
JOIN {lti_types} ltit
ON ltit.course = course.id
WHERE ltit.createdby = :userid";
$params = [
'contextlevel' => CONTEXT_COURSE,
'userid' => $userid
];
$contextlist->add_from_sql($sql, $params);
// The LTI tool proxies sit in the system context.
$contextlist->add_system_context();
return $contextlist;
}
/**
* Get the list of users who have data within a context.
*
* @param userlist $userlist The userlist containing the list of users who have data in this context/plugin combination.
*/
public static function get_users_in_context(userlist $userlist) {
$context = $userlist->get_context();
if (!is_a($context, \context_module::class)) {
return;
}
// Fetch all LTI submissions.
$sql = "SELECT ltisub.userid
FROM {context} c
INNER JOIN {course_modules} cm
ON cm.id = c.instanceid
AND c.contextlevel = :contextlevel
INNER JOIN {modules} m
ON m.id = cm.module
AND m.name = :modname
INNER JOIN {lti} lti
ON lti.id = cm.instance
INNER JOIN {lti_submission} ltisub
ON ltisub.ltiid = lti.id
WHERE c.id = :contextid";
$params = [
'modname' => 'lti',
'contextlevel' => CONTEXT_MODULE,
'contextid' => $context->id,
];
$userlist->add_from_sql('userid', $sql, $params);
// Fetch all LTI types.
$sql = "SELECT ltit.createdby AS userid
FROM {context} c
JOIN {course} course
ON c.contextlevel = :contextlevel
AND c.instanceid = course.id
JOIN {lti_types} ltit
ON ltit.course = course.id
WHERE c.id = :contextid";
$params = [
'contextlevel' => CONTEXT_COURSE,
'contextid' => $context->id,
];
$userlist->add_from_sql('userid', $sql, $params);
}
/**
* Export personal data for the given approved_contextlist. User and context information is contained within the contextlist.
*
* @param approved_contextlist $contextlist a list of contexts approved for export.
*/
public static function export_user_data(approved_contextlist $contextlist) {
self::export_user_data_lti_submissions($contextlist);
self::export_user_data_lti_types($contextlist);
self::export_user_data_lti_tool_proxies($contextlist);
}
/**
* Delete all data for all users in the specified context.
*
* @param \context $context the context to delete in.
*/
public static function delete_data_for_all_users_in_context(\context $context) {
global $DB;
if (!$context instanceof \context_module) {
return;
}
if ($cm = get_coursemodule_from_id('lti', $context->instanceid)) {
$DB->delete_records('lti_submission', ['ltiid' => $cm->instance]);
}
}
/**
* Delete all user data for the specified user, in the specified contexts.
*
* @param approved_contextlist $contextlist a list of contexts approved for deletion.
*/
public static function delete_data_for_user(approved_contextlist $contextlist) {
global $DB;
if (empty($contextlist->count())) {
return;
}
$userid = $contextlist->get_user()->id;
foreach ($contextlist->get_contexts() as $context) {
if (!$context instanceof \context_module) {
continue;
}
$instanceid = $DB->get_field('course_modules', 'instance', ['id' => $context->instanceid], MUST_EXIST);
$DB->delete_records('lti_submission', ['ltiid' => $instanceid, 'userid' => $userid]);
}
}
/**
* Delete multiple users within a single context.
*
* @param approved_userlist $userlist The approved context and user information to delete information for.
*/
public static function delete_data_for_users(approved_userlist $userlist) {
global $DB;
$context = $userlist->get_context();
if ($context instanceof \context_module) {
$instanceid = $DB->get_field('course_modules', 'instance', ['id' => $context->instanceid], MUST_EXIST);
list($insql, $inparams) = $DB->get_in_or_equal($userlist->get_userids(), SQL_PARAMS_NAMED);
$sql = "ltiid = :instanceid AND userid {$insql}";
$params = array_merge(['instanceid' => $instanceid], $inparams);
$DB->delete_records_select('lti_submission', $sql, $params);
}
}
/**
* Export personal data for the given approved_contextlist related to LTI submissions.
*
* @param approved_contextlist $contextlist a list of contexts approved for export.
*/
protected static function export_user_data_lti_submissions(approved_contextlist $contextlist) {
global $DB;
// Filter out any contexts that are not related to modules.
$cmids = array_reduce($contextlist->get_contexts(), function($carry, $context) {
if ($context->contextlevel == CONTEXT_MODULE) {
$carry[] = $context->instanceid;
}
return $carry;
}, []);
if (empty($cmids)) {
return;
}
$user = $contextlist->get_user();
// Get all the LTI activities associated with the above course modules.
$ltiidstocmids = self::get_lti_ids_to_cmids_from_cmids($cmids);
$ltiids = array_keys($ltiidstocmids);
list($insql, $inparams) = $DB->get_in_or_equal($ltiids, SQL_PARAMS_NAMED);
$params = array_merge($inparams, ['userid' => $user->id]);
$recordset = $DB->get_recordset_select('lti_submission', "ltiid $insql AND userid = :userid", $params, 'dateupdated, id');
self::recordset_loop_and_export($recordset, 'ltiid', [], function($carry, $record) use ($user, $ltiidstocmids) {
$carry[] = [
'gradepercent' => $record->gradepercent,
'originalgrade' => $record->originalgrade,
'datesubmitted' => transform::datetime($record->datesubmitted),
'dateupdated' => transform::datetime($record->dateupdated)
];
return $carry;
}, function($ltiid, $data) use ($user, $ltiidstocmids) {
$context = \context_module::instance($ltiidstocmids[$ltiid]);
$contextdata = helper::get_context_data($context, $user);
$finaldata = (object) array_merge((array) $contextdata, ['submissions' => $data]);
helper::export_context_files($context, $user);
writer::with_context($context)->export_data([], $finaldata);
});
}
/**
* Export personal data for the given approved_contextlist related to LTI types.
*
* @param approved_contextlist $contextlist a list of contexts approved for export.
*/
protected static function export_user_data_lti_types(approved_contextlist $contextlist) {
global $DB;
// Filter out any contexts that are not related to courses.
$courseids = array_reduce($contextlist->get_contexts(), function($carry, $context) {
if ($context->contextlevel == CONTEXT_COURSE) {
$carry[] = $context->instanceid;
}
return $carry;
}, []);
if (empty($courseids)) {
return;
}
$user = $contextlist->get_user();
list($insql, $inparams) = $DB->get_in_or_equal($courseids, SQL_PARAMS_NAMED);
$params = array_merge($inparams, ['userid' => $user->id]);
$ltitypes = $DB->get_recordset_select('lti_types', "course $insql AND createdby = :userid", $params, 'timecreated ASC');
self::recordset_loop_and_export($ltitypes, 'course', [], function($carry, $record) {
$context = \context_course::instance($record->course);
$options = ['context' => $context];
$carry[] = [
'name' => format_string($record->name, true, $options),
'createdby' => transform::user($record->createdby),
'timecreated' => transform::datetime($record->timecreated),
'timemodified' => transform::datetime($record->timemodified)
];
return $carry;
}, function($courseid, $data) {
$context = \context_course::instance($courseid);
$finaldata = (object) ['lti_types' => $data];
writer::with_context($context)->export_data([], $finaldata);
});
}
/**
* Export personal data for the given approved_contextlist related to LTI tool proxies.
*
* @param approved_contextlist $contextlist a list of contexts approved for export.
*/
protected static function export_user_data_lti_tool_proxies(approved_contextlist $contextlist) {
global $DB;
// Filter out any contexts that are not related to system context.
$systemcontexts = array_filter($contextlist->get_contexts(), function($context) {
return $context->contextlevel == CONTEXT_SYSTEM;
});
if (empty($systemcontexts)) {
return;
}
$user = $contextlist->get_user();
$systemcontext = \context_system::instance();
$data = [];
$ltiproxies = $DB->get_recordset('lti_tool_proxies', ['createdby' => $user->id], 'timecreated ASC');
foreach ($ltiproxies as $ltiproxy) {
$data[] = [
'name' => format_string($ltiproxy->name, true, ['context' => $systemcontext]),
'createdby' => transform::user($ltiproxy->createdby),
'timecreated' => transform::datetime($ltiproxy->timecreated),
'timemodified' => transform::datetime($ltiproxy->timemodified)
];
}
$ltiproxies->close();
$finaldata = (object) ['lti_tool_proxies' => $data];
writer::with_context($systemcontext)->export_data([], $finaldata);
}
/**
* Return a dict of LTI IDs mapped to their course module ID.
*
* @param array $cmids The course module IDs.
* @return array In the form of [$ltiid => $cmid].
*/
protected static function get_lti_ids_to_cmids_from_cmids(array $cmids) {
global $DB;
list($insql, $inparams) = $DB->get_in_or_equal($cmids, SQL_PARAMS_NAMED);
$sql = "SELECT lti.id, cm.id AS cmid
FROM {lti} lti
JOIN {modules} m
ON m.name = :lti
JOIN {course_modules} cm
ON cm.instance = lti.id
AND cm.module = m.id
WHERE cm.id $insql";
$params = array_merge($inparams, ['lti' => 'lti']);
return $DB->get_records_sql_menu($sql, $params);
}
/**
* Loop and export from a recordset.
*
* @param \moodle_recordset $recordset The recordset.
* @param string $splitkey The record key to determine when to export.
* @param mixed $initial The initial data to reduce from.
* @param callable $reducer The function to return the dataset, receives current dataset, and the current record.
* @param callable $export The function to export the dataset, receives the last value from $splitkey and the dataset.
* @return void
*/
protected static function recordset_loop_and_export(\moodle_recordset $recordset, $splitkey, $initial,
callable $reducer, callable $export) {
$data = $initial;
$lastid = null;
foreach ($recordset as $record) {
if ($lastid && $record->{$splitkey} != $lastid) {
$export($lastid, $data);
$data = $initial;
}
$data = $reducer($data, $record);
$lastid = $record->{$splitkey};
}
$recordset->close();
if (!empty($lastid)) {
$export($lastid, $data);
}
}
}
@@ -0,0 +1,187 @@
<?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/>.
namespace mod_lti\reportbuilder\local\entities;
use core_reportbuilder\local\filters\select;
use core_reportbuilder\local\filters\text;
use lang_string;
use core_reportbuilder\local\entities\base;
use core_reportbuilder\local\report\column;
use core_reportbuilder\local\report\filter;
/**
* Course external tools entity class implementation.
*
* Defines all the columns and filters that can be added to reports that use this entity.
*
* @package mod_lti
* @copyright 2023 Jake Dallimore <jrhdallimore@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class tool_types extends base {
/**
* Database tables that this entity uses
*
* @return string[]
*/
protected function get_default_tables(): array {
return [
'lti_types',
'lti',
];
}
/**
* The default title for this entity
*
* @return lang_string
*/
protected function get_default_entity_title(): lang_string {
return new lang_string('entitycourseexternaltools', 'mod_lti');
}
/**
* Initialize the entity
*
* @return base
*/
public function initialise(): base {
$columns = $this->get_all_columns();
foreach ($columns as $column) {
$this->add_column($column);
}
$filters = $this->get_all_filters();
foreach ($filters as $filter) {
$this->add_filter($filter);
}
return $this;
}
/**
* Returns list of all available columns
*
* @return column[]
*/
protected function get_all_columns(): array {
$tablealias = $this->get_table_alias('lti_types');
// Name column.
$columns[] = (new column(
'name',
new lang_string('name', 'core'),
$this->get_entity_name()
))
->add_joins($this->get_joins())
->set_type(column::TYPE_TEXT)
->add_fields("{$tablealias}.name, {$tablealias}.icon")
->set_is_sortable(true)
->add_callback(static function(string $name, \stdClass $data) {
global $OUTPUT;
$iconurl = $data->icon ?: $OUTPUT->image_url('monologo', 'lti')->out();
$iconclass = $data->icon ? ' nofilter' : '';
$iconcontainerclass = 'activityiconcontainer smaller';
$name = $data->name;
$img = \html_writer::img($iconurl, get_string('courseexternaltooliconalt', 'mod_lti', $name),
['class' => 'activityicon' . $iconclass]);
$name = \html_writer::span($name, 'align-self-center');
return \html_writer::div(\html_writer::div($img, 'mr-2 '.$iconcontainerclass) . $name, 'd-flex');
});
// Description column.
$columns[] = (new column(
'description',
new lang_string('description', 'core'),
$this->get_entity_name()
))
->add_joins($this->get_joins())
->set_type(column::TYPE_TEXT)
->add_field("{$tablealias}.description")
->set_is_sortable(true);
// Course column.
$columns[] = (new column(
'course',
new lang_string('course', 'core'),
$this->get_entity_name()
))
->add_joins($this->get_joins())
->set_type(column::TYPE_INTEGER)
->add_field("{$tablealias}.course")
->set_is_sortable(true);
// LTI Version column.
$columns[] = (new column(
'ltiversion',
new lang_string('version'),
$this->get_entity_name()
))
->add_joins($this->get_joins())
->set_type(column::TYPE_TEXT)
->add_field("{$tablealias}.ltiversion")
->set_is_sortable(true);
return $columns;
}
/**
* Return list of all available filters
*
* @return filter[]
*/
protected function get_all_filters(): array {
$tablealias = $this->get_table_alias('lti_types');
return [
// Name filter.
(new filter(
text::class,
'name',
new lang_string('name'),
$this->get_entity_name(),
"{$tablealias}.name"
))
->add_joins($this->get_joins()),
// Description filter.
(new filter(
text::class,
'description',
new lang_string('description'),
$this->get_entity_name(),
"{$tablealias}.description"
))
->add_joins($this->get_joins()),
// LTI Version filter.
(new filter(
select::class,
'ltiversion',
new lang_string('version'),
$this->get_entity_name(),
"{$tablealias}.ltiversion"
))
->add_joins($this->get_joins())
->set_options_callback(static function(): array {
return ['LTI-1p0' => 'Legacy LTI', '1.3.0' => "LTI Advantage"];
})
];
}
}
@@ -0,0 +1,270 @@
<?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/>.
namespace mod_lti\reportbuilder\local\systemreports;
use core_reportbuilder\local\helpers\database;
use core_reportbuilder\local\report\column;
use mod_lti\reportbuilder\local\entities\tool_types;
use core_reportbuilder\system_report;
/**
* Course external tools list system report class implementation.
*
* @package mod_lti
* @copyright 2023 Jake Dallimore <jrhdallimore@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class course_external_tools_list extends system_report {
/** @var \stdClass the course to constrain the report to. */
protected \stdClass $course;
/** @var int the usage count for the tool represented in a row, and set by row_callback(). */
protected int $perrowtoolusage = 0;
/**
* Initialise report, we need to set the main table, load our entities and set columns/filters
*/
protected function initialise(): void {
global $DB, $CFG;
require_once($CFG->dirroot . '/mod/lti/locallib.php');
$this->course = get_course($this->get_context()->instanceid);
// Our main entity, it contains all the column definitions that we need.
$entitymain = new tool_types();
$entitymainalias = $entitymain->get_table_alias('lti_types');
$this->set_main_table('lti_types', $entitymainalias);
$this->add_entity($entitymain);
// Now we can call our helper methods to add the content we want to include in the report.
$this->add_columns($entitymain);
$this->add_filters();
$this->add_actions();
// We need id and course in the actions column, without entity prefixes, so add these here.
// We also need access to the tool usage count in a few places (the usage column as well as the actions column).
$ti = database::generate_param_name(); // Tool instance param.
$this->add_base_fields("{$entitymainalias}.id, {$entitymainalias}.course, ".
"(SELECT COUNT($ti.id)
FROM {lti} $ti
WHERE $ti.typeid = {$entitymainalias}.id) AS toolusage");
// Join the types_categories table, to include only tools available to the current course's category.
$cattablealias = database::generate_alias();
$joinsql = "LEFT JOIN {lti_types_categories} {$cattablealias}
ON ({$cattablealias}.typeid = {$entitymainalias}.id)";
$this->add_join($joinsql);
// Scope the report to the course context and include only those tools available to the category.
$paramprefix = database::generate_param_name();
$coursevisibleparam = database::generate_param_name();
$categoryparam = database::generate_param_name();
$toolstateparam = database::generate_param_name();
[$insql, $params] = $DB->get_in_or_equal([get_site()->id, $this->course->id], SQL_PARAMS_NAMED, "{$paramprefix}_");
$wheresql = "{$entitymainalias}.course {$insql} ".
"AND {$entitymainalias}.coursevisible NOT IN (:{$coursevisibleparam}) ".
"AND ({$cattablealias}.id IS NULL OR {$cattablealias}.categoryid = :{$categoryparam}) ".
"AND {$entitymainalias}.state = :{$toolstateparam}";
$params = array_merge(
$params,
[
$coursevisibleparam => LTI_COURSEVISIBLE_NO,
$categoryparam => $this->course->category,
$toolstateparam => LTI_TOOL_STATE_CONFIGURED
]
);
$this->add_base_condition_sql($wheresql, $params);
$this->set_downloadable(false, get_string('pluginname', 'mod_lti'));
$this->set_default_per_page(10);
$this->set_default_no_results_notice(null);
}
/**
* Validates access to view this report
*
* @return bool
*/
protected function can_view(): bool {
return has_capability('mod/lti:addpreconfiguredinstance', $this->get_context());
}
public function row_callback(\stdClass $row): void {
$this->perrowtoolusage = $row->toolusage;
}
/**
* Adds the columns we want to display in the report.
*
* They are all provided by the entities we previously added in the {@see initialise} method, referencing each by their
* unique identifier
* @param tool_types $tooltypesentity
* @return void
*/
protected function add_columns(tool_types $tooltypesentity): void {
$entitymainalias = $tooltypesentity->get_table_alias('lti_types');
$columns = [
'tool_types:name',
'tool_types:description',
];
$this->add_columns_from_entities($columns);
// Tool usage column using a custom SQL subquery (defined in initialise method) to count tool instances within the course.
// TODO: This should be replaced with proper column aggregation once that's added to system_report instances in MDL-76392.
$this->add_column(new column(
'usage',
new \lang_string('usage', 'mod_lti'),
$tooltypesentity->get_entity_name()
))
->set_type(column::TYPE_INTEGER)
->set_is_sortable(true)
->add_field("{$entitymainalias}.id")
->add_callback(fn() => $this->perrowtoolusage);
// Enable toggle column.
$this->add_column((new column(
'showinactivitychooser',
new \lang_string('showinactivitychooser', 'mod_lti'),
$tooltypesentity->get_entity_name()
))
// Site tools can be overridden on course level.
->add_join("LEFT JOIN {lti_coursevisible} lc ON lc.typeid = {$entitymainalias}.id AND lc.courseid = " . $this->course->id)
->set_type(column::TYPE_INTEGER)
->add_fields("{$entitymainalias}.id, {$entitymainalias}.coursevisible, lc.coursevisible as coursevisibleoverridden")
->set_is_sortable(false)
->set_callback(function(int $id, \stdClass $row): string {
global $PAGE;
$coursevisible = $row->coursevisible;
$courseid = $this->course->id;
if (!empty($row->coursevisibleoverridden)) {
$coursevisible = $row->coursevisibleoverridden;
}
if ($coursevisible == LTI_COURSEVISIBLE_ACTIVITYCHOOSER) {
$coursevisible = true;
} else {
$coursevisible = false;
}
$renderer = $PAGE->get_renderer('core_reportbuilder');
$attributes = [
['name' => 'id', 'value' => $row->id],
['name' => 'courseid', 'value' => $courseid],
['name' => 'action', 'value' => 'showinactivitychooser-toggle'],
['name' => 'state', 'value' => $coursevisible],
];
$label = $coursevisible ? get_string('dontshowinactivitychooser', 'mod_lti')
: get_string('showinactivitychooser', 'mod_lti');
$disabled = !has_capability('mod/lti:addcoursetool', \context_course::instance($courseid));
return $renderer->render_from_template('core/toggle', [
'id' => 'showinactivitychooser-toggle-' . $row->id,
'checked' => $coursevisible,
'disabled' => $disabled,
'dataattributes' => $attributes,
'label' => $label,
'labelclasses' => 'sr-only'
]);
})
);
// Attempt to create a dummy actions column, working around the limitations of the official actions feature.
$this->add_column(new column(
'actions', new \lang_string('actions'),
$tooltypesentity->get_entity_name()
))
->set_type(column::TYPE_TEXT)
->set_is_sortable(false)
->add_fields("{$entitymainalias}.id, {$entitymainalias}.course, {$entitymainalias}.name")
->add_callback(function($field, $row) {
global $OUTPUT;
// Lock actions for site-level preconfigured tools.
if (get_site()->id == $row->course) {
return \html_writer::div(
\html_writer::div(
$OUTPUT->pix_icon('t/locked', get_string('courseexternaltoolsnoeditpermissions', 'mod_lti')
), 'tool-action-icon-container'), 'd-flex justify-content-end'
);
}
// Lock actions when the user can't add course tools.
if (!has_capability('mod/lti:addcoursetool', \context_course::instance($row->course))) {
return \html_writer::div(
\html_writer::div(
$OUTPUT->pix_icon('t/locked', get_string('courseexternaltoolsnoeditpermissions', 'mod_lti')
), 'tool-action-icon-container'), 'd-flex justify-content-end'
);
}
// Build and display an action menu.
$menu = new \action_menu();
$menu->set_menu_trigger($OUTPUT->pix_icon('i/moremenu', get_string('actions', 'core')),
'btn btn-icon d-flex align-items-center justify-content-center'); // TODO check 'actions' lang string with UX.
$menu->add(new \action_menu_link(
new \moodle_url('/mod/lti/coursetooledit.php', ['course' => $row->course, 'typeid' => $row->id]),
null,
get_string('edit', 'core'),
null
));
$menu->add(new \action_menu_link(
new \moodle_url('#'),
null,
get_string('delete', 'core'),
null,
[
'data-action' => 'course-tool-delete',
'data-course-tool-id' => $row->id,
'data-course-tool-name' => $row->name,
'data-course-tool-usage' => $this->perrowtoolusage,
'class' => 'text-danger',
],
));
return $OUTPUT->render($menu);
});
// Default sorting.
$this->set_initial_sort_column('tool_types:name', SORT_ASC);
}
/**
* Add any actions for this report.
*
* @return void
*/
protected function add_actions(): void {
}
/**
* Adds the filters we want to display in the report
*
* They are all provided by the entities we previously added in the {@see initialise} method, referencing each by their
* unique identifier
*/
protected function add_filters(): void {
$this->add_filters_from_entities([]);
}
}
+46
View File
@@ -0,0 +1,46 @@
<?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/>.
/**
* Search area for mod_lti activities.
*
* @package mod_lti
* @copyright 2015 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_lti\search;
defined('MOODLE_INTERNAL') || die();
/**
* Search area for mod_lti activities.
*
* @package mod_lti
* @copyright 2015 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class activity extends \core_search\base_activity {
/**
* Returns true if this area uses file indexing.
*
* @return bool
*/
public function uses_file_indexing() {
return true;
}
}
@@ -0,0 +1,122 @@
<?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/>.
/**
* Exception handler for LTI services
*
* @package mod_lti
* @copyright Copyright (c) 2015 Moodlerooms Inc. (http://www.moodlerooms.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_lti;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__.'/../locallib.php');
require_once(__DIR__.'/../servicelib.php');
/**
* Handles exceptions when handling incoming LTI messages.
*
* Ensures that LTI always returns a XML message that can be consumed by the caller.
*
* @package mod_lti
* @copyright Copyright (c) 2015 Moodlerooms Inc. (http://www.moodlerooms.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class service_exception_handler {
/**
* Enable error response logging.
*
* @var bool
*/
protected $log = false;
/**
* The LTI service message ID, if known.
*
* @var string
*/
protected $id = '';
/**
* The LTI service message type, if known.
*
* @var string
*/
protected $type = 'unknownRequest';
/**
* Constructor.
*
* @param boolean $log Enable error response logging.
*/
public function __construct($log) {
$this->log = $log;
}
/**
* Set the LTI message ID being handled.
*
* @param string $id
*/
public function set_message_id($id) {
if (!empty($id)) {
$this->id = $id;
}
}
/**
* Set the LTI message type being handled.
*
* @param string $type
*/
public function set_message_type($type) {
if (!empty($type)) {
$this->type = $type;
}
}
/**
* Echo an exception message encapsulated in XML.
*
* @param \Exception|\Throwable $exception The exception that was thrown
*/
public function handle($exception) {
$message = $exception->getMessage();
// Add the exception backtrace for developers.
if (debugging('', DEBUG_DEVELOPER)) {
$message .= "\n".format_backtrace(get_exception_info($exception)->backtrace, true);
}
// Switch to response.
$type = str_replace('Request', 'Response', $this->type);
// Build the appropriate xml.
$response = lti_get_response_xml('failure', $message, $this->id, $type);
$xml = $response->asXML();
// Log the request if necessary.
if ($this->log) {
lti_log_response($xml, $exception);
}
echo $xml;
}
}
@@ -0,0 +1,56 @@
<?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/>.
/**
* A scheduled task for lti module.
*
* @package mod_lti
* @copyright 2019 Stephen Vickers
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_lti\task;
use core\task\scheduled_task;
defined('MOODLE_INTERNAL') || die();
/**
* Class containing the scheduled task for lti module.
*
* @package mod_lti
* @copyright 2018 Stephen Vickers
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class clean_access_tokens extends scheduled_task {
/**
* Get a descriptive name for this task (shown to admins).
*
* @return string
*/
public function get_name() {
return get_string('cleanaccesstokens', 'mod_lti');
}
/**
* Run lti cron.
*/
public function execute() {
global $DB;
$DB->delete_records_select('lti_access_tokens', 'validuntil < ?', [time()]);
}
}