2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 // This file is part of BasicLTI4Moodle
19 // BasicLTI4Moodle is an IMS BasicLTI (Basic Learning Tools for Interoperability)
20 // consumer for Moodle 1.9 and Moodle 2.0. BasicLTI is a IMS Standard that allows web
21 // based learning tools to be easily integrated in LMS as native ones. The IMS BasicLTI
22 // specification is part of the IMS standard Common Cartridge 1.1 Sakai and other main LMS
23 // are already supporting or going to support BasicLTI. This project Implements the consumer
24 // for Moodle. Moodle is a Free Open source Learning Management System by Martin Dougiamas.
25 // BasicLTI4Moodle is a project iniciated and leaded by Ludo(Marc Alier) and Jordi Piguillem
26 // at the GESSI research group at UPC.
27 // SimpleLTI consumer for Moodle is an implementation of the early specification of LTI
28 // by Charles Severance (Dr Chuck) htp://dr-chuck.com , developed by Jordi Piguillem in a
29 // Google Summer of Code 2008 project co-mentored by Charles Severance and Marc Alier.
31 // BasicLTI4Moodle is copyright 2009 by Marc Alier Forment, Jordi Piguillem and Nikolas Galanis
32 // of the Universitat Politecnica de Catalunya http://www.upc.edu
33 // Contact info: Marc Alier Forment granludo @ gmail.com or marc.alier @ upc.edu.
36 * This file contains the library of functions and constants for the lti module
39 * @copyright 2009 Marc Alier, Jordi Piguillem, Nikolas Galanis
41 * @copyright 2009 Universitat Politecnica de Catalunya http://www.upc.edu
43 * @author Jordi Piguillem
44 * @author Nikolas Galanis
45 * @author Chris Scribner
46 * @copyright 2015 Vital Source Technologies http://vitalsource.com
47 * @author Stephen Vickers
48 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
51 defined('MOODLE_INTERNAL') || die;
53 // TODO: Switch to core oauthlib once implemented - MDL-30149.
54 use moodle\mod\lti as lti;
56 require_once($CFG->dirroot.'/mod/lti/OAuth.php');
57 require_once($CFG->libdir.'/weblib.php');
58 require_once($CFG->dirroot . '/course/modlib.php');
59 require_once($CFG->dirroot . '/mod/lti/TrivialStore.php');
61 define('LTI_URL_DOMAIN_REGEX', '/(?:https?:\/\/)?(?:www\.)?([^\/]+)(?:\/|$)/i');
63 define('LTI_LAUNCH_CONTAINER_DEFAULT', 1);
64 define('LTI_LAUNCH_CONTAINER_EMBED', 2);
65 define('LTI_LAUNCH_CONTAINER_EMBED_NO_BLOCKS', 3);
66 define('LTI_LAUNCH_CONTAINER_WINDOW', 4);
67 define('LTI_LAUNCH_CONTAINER_REPLACE_MOODLE_WINDOW', 5);
69 define('LTI_TOOL_STATE_ANY', 0);
70 define('LTI_TOOL_STATE_CONFIGURED', 1);
71 define('LTI_TOOL_STATE_PENDING', 2);
72 define('LTI_TOOL_STATE_REJECTED', 3);
73 define('LTI_TOOL_PROXY_TAB', 4);
75 define('LTI_TOOL_PROXY_STATE_CONFIGURED', 1);
76 define('LTI_TOOL_PROXY_STATE_PENDING', 2);
77 define('LTI_TOOL_PROXY_STATE_ACCEPTED', 3);
78 define('LTI_TOOL_PROXY_STATE_REJECTED', 4);
80 define('LTI_SETTING_NEVER', 0);
81 define('LTI_SETTING_ALWAYS', 1);
82 define('LTI_SETTING_DELEGATE', 2);
84 define('LTI_COURSEVISIBLE_NO', 0);
85 define('LTI_COURSEVISIBLE_PRECONFIGURED', 1);
86 define('LTI_COURSEVISIBLE_ACTIVITYCHOOSER', 2);
88 define('LTI_VERSION_1', 'LTI-1p0');
89 define('LTI_VERSION_2', 'LTI-2p0');
92 * Return the launch data required for opening the external tool.
94 * @param stdClass $instance the external tool activity settings
95 * @return array the endpoint URL and parameters (including the signature)
98 function lti_get_launch_data($instance) {
101 if (empty($instance->typeid)) {
102 $tool = lti_get_tool_by_url_match($instance->toolurl, $instance->course);
109 $typeid = $instance->typeid;
110 $tool = lti_get_type($typeid);
114 $typeconfig = lti_get_type_config($typeid);
116 // There is no admin configuration for this tool. Use configuration in the lti instance record plus some defaults.
117 $typeconfig = (array)$instance;
119 $typeconfig['sendname'] = $instance->instructorchoicesendname;
120 $typeconfig['sendemailaddr'] = $instance->instructorchoicesendemailaddr;
121 $typeconfig['customparameters'] = $instance->instructorcustomparameters;
122 $typeconfig['acceptgrades'] = $instance->instructorchoiceacceptgrades;
123 $typeconfig['allowroster'] = $instance->instructorchoiceallowroster;
124 $typeconfig['forcessl'] = '0';
127 // Default the organizationid if not specified.
128 if (empty($typeconfig['organizationid'])) {
129 $urlparts = parse_url($CFG->wwwroot);
131 $typeconfig['organizationid'] = $urlparts['host'];
134 if (isset($tool->toolproxyid)) {
135 $toolproxy = lti_get_tool_proxy($tool->toolproxyid);
136 $key = $toolproxy->guid;
137 $secret = $toolproxy->secret;
140 if (!empty($instance->resourcekey)) {
141 $key = $instance->resourcekey;
142 } else if (!empty($typeconfig['resourcekey'])) {
143 $key = $typeconfig['resourcekey'];
147 if (!empty($instance->password)) {
148 $secret = $instance->password;
149 } else if (!empty($typeconfig['password'])) {
150 $secret = $typeconfig['password'];
156 $endpoint = !empty($instance->toolurl) ? $instance->toolurl : $typeconfig['toolurl'];
157 $endpoint = trim($endpoint);
159 // If the current request is using SSL and a secure tool URL is specified, use it.
160 if (lti_request_is_using_ssl() && !empty($instance->securetoolurl)) {
161 $endpoint = trim($instance->securetoolurl);
164 // If SSL is forced, use the secure tool url if specified. Otherwise, make sure https is on the normal launch URL.
165 if (isset($typeconfig['forcessl']) && ($typeconfig['forcessl'] == '1')) {
166 if (!empty($instance->securetoolurl)) {
167 $endpoint = trim($instance->securetoolurl);
170 $endpoint = lti_ensure_url_is_https($endpoint);
172 if (!strstr($endpoint, '://')) {
173 $endpoint = 'http://' . $endpoint;
177 $orgid = $typeconfig['organizationid'];
179 $course = $PAGE->course;
180 $islti2 = isset($tool->toolproxyid);
181 $allparams = lti_build_request($instance, $typeconfig, $course, $typeid, $islti2);
183 $requestparams = lti_build_request_lti2($tool, $allparams);
185 $requestparams = $allparams;
187 $requestparams = array_merge($requestparams, lti_build_standard_request($instance, $orgid, $islti2));
189 if (isset($typeconfig['customparameters'])) {
190 $customstr = $typeconfig['customparameters'];
192 $requestparams = array_merge($requestparams, lti_build_custom_parameters($toolproxy, $tool, $instance, $allparams, $customstr,
193 $instance->instructorcustomparameters, $islti2));
195 $launchcontainer = lti_get_launch_container($instance, $typeconfig);
196 $returnurlparams = array('course' => $course->id,
197 'launch_container' => $launchcontainer,
198 'instanceid' => $instance->id,
199 'sesskey' => sesskey());
201 // Add the return URL. We send the launch container along to help us avoid frames-within-frames when the user returns.
202 $url = new \moodle_url('/mod/lti/return.php', $returnurlparams);
203 $returnurl = $url->out(false);
205 if (isset($typeconfig['forcessl']) && ($typeconfig['forcessl'] == '1')) {
206 $returnurl = lti_ensure_url_is_https($returnurl);
210 switch($launchcontainer) {
211 case LTI_LAUNCH_CONTAINER_EMBED:
212 case LTI_LAUNCH_CONTAINER_EMBED_NO_BLOCKS:
215 case LTI_LAUNCH_CONTAINER_REPLACE_MOODLE_WINDOW:
218 case LTI_LAUNCH_CONTAINER_WINDOW:
222 if (!empty($target)) {
223 $requestparams['launch_presentation_document_target'] = $target;
226 $requestparams['launch_presentation_return_url'] = $returnurl;
228 // Allow request params to be updated by sub-plugins.
229 $plugins = core_component::get_plugin_list('ltisource');
230 foreach (array_keys($plugins) as $plugin) {
231 $pluginparams = component_callback('ltisource_'.$plugin, 'before_launch',
232 array($instance, $endpoint, $requestparams), array());
234 if (!empty($pluginparams) && is_array($pluginparams)) {
235 $requestparams = array_merge($requestparams, $pluginparams);
239 if (!empty($key) && !empty($secret)) {
240 $parms = lti_sign_parameters($requestparams, $endpoint, "POST", $key, $secret);
242 $endpointurl = new \moodle_url($endpoint);
243 $endpointparams = $endpointurl->params();
245 // Strip querystring params in endpoint url from $parms to avoid duplication.
246 if (!empty($endpointparams) && !empty($parms)) {
247 foreach (array_keys($endpointparams) as $paramname) {
248 if (isset($parms[$paramname])) {
249 unset($parms[$paramname]);
255 // If no key and secret, do the launch unsigned.
256 $returnurlparams['unsigned'] = '1';
257 $parms = $requestparams;
260 return array($endpoint, $parms);
264 * Launch an external tool activity.
266 * @param stdClass $instance the external tool activity settings
267 * @return string The HTML code containing the javascript code for the launch
269 function lti_launch_tool($instance) {
271 list($endpoint, $parms) = lti_get_launch_data($instance);
272 $debuglaunch = ( $instance->debuglaunch == 1 );
274 $content = lti_post_launch_html($parms, $endpoint, $debuglaunch);
280 * Prepares an LTI registration request message
282 * $param object $instance Tool Proxy instance object
284 function lti_register($toolproxy) {
285 $endpoint = $toolproxy->regurl;
287 // Change the status to pending.
288 $toolproxy->state = LTI_TOOL_PROXY_STATE_PENDING;
289 lti_update_tool_proxy($toolproxy);
291 $requestparams = lti_build_registration_request($toolproxy);
293 $content = lti_post_launch_html($requestparams, $endpoint, false);
300 * Gets the parameters for the regirstration request
302 * @param object $toolproxy Tool Proxy instance object
303 * @return array Registration request parameters
305 function lti_build_registration_request($toolproxy) {
306 $key = $toolproxy->guid;
307 $secret = $toolproxy->secret;
309 $requestparams = array();
310 $requestparams['lti_message_type'] = 'ToolProxyRegistrationRequest';
311 $requestparams['lti_version'] = 'LTI-2p0';
312 $requestparams['reg_key'] = $key;
313 $requestparams['reg_password'] = $secret;
314 $requestparams['reg_url'] = $toolproxy->regurl;
316 // Add the profile URL.
317 $profileservice = lti_get_service_by_name('profile');
318 $profileservice->set_tool_proxy($toolproxy);
319 $requestparams['tc_profile_url'] = $profileservice->parse_value('$ToolConsumerProfile.url');
321 // Add the return URL.
322 $returnurlparams = array('id' => $toolproxy->id, 'sesskey' => sesskey());
323 $url = new \moodle_url('/mod/lti/externalregistrationreturn.php', $returnurlparams);
324 $returnurl = $url->out(false);
326 $requestparams['launch_presentation_return_url'] = $returnurl;
328 return $requestparams;
334 * @param int $instanceid
336 * @param string $servicesalt
337 * @param null|int $typeid
338 * @param null|int $launchid
341 function lti_build_sourcedid($instanceid, $userid, $servicesalt, $typeid = null, $launchid = null) {
342 $data = new \stdClass();
344 $data->instanceid = $instanceid;
345 $data->userid = $userid;
346 $data->typeid = $typeid;
347 if (!empty($launchid)) {
348 $data->launchid = $launchid;
350 $data->launchid = mt_rand();
353 $json = json_encode($data);
355 $hash = hash('sha256', $json . $servicesalt, false);
357 $container = new \stdClass();
358 $container->data = $data;
359 $container->hash = $hash;
365 * This function builds the request that must be sent to the tool producer
367 * @param object $instance Basic LTI instance object
368 * @param array $typeconfig Basic LTI tool configuration
369 * @param object $course Course object
370 * @param int|null $typeid Basic LTI tool ID
371 * @param boolean $islti2 True if an LTI 2 tool is being launched
373 * @return array Request details
375 function lti_build_request($instance, $typeconfig, $course, $typeid = null, $islti2 = false) {
378 if (empty($instance->cmid)) {
382 $role = lti_get_ims_role($USER, $instance->cmid, $instance->course, $islti2);
384 $requestparams = array(
385 'user_id' => $USER->id,
386 'lis_person_sourcedid' => $USER->idnumber,
388 'context_id' => $course->id,
389 'context_label' => trim(html_to_text($course->shortname, 0)),
390 'context_title' => trim(html_to_text($course->fullname, 0)),
392 if (!empty($instance->name)) {
393 $requestparams['resource_link_title'] = trim(html_to_text($instance->name, 0));
395 if (!empty($instance->cmid)) {
396 $intro = format_module_intro('lti', $instance, $instance->cmid);
397 $intro = trim(html_to_text($intro, 0, false));
399 // This may look weird, but this is required for new lines
400 // so we generate the same OAuth signature as the tool provider.
401 $intro = str_replace("\n", "\r\n", $intro);
402 $requestparams['resource_link_description'] = $intro;
404 if (!empty($instance->id)) {
405 $requestparams['resource_link_id'] = $instance->id;
407 if (!empty($instance->resource_link_id)) {
408 $requestparams['resource_link_id'] = $instance->resource_link_id;
410 if ($course->format == 'site') {
411 $requestparams['context_type'] = 'Group';
413 $requestparams['context_type'] = 'CourseSection';
414 $requestparams['lis_course_section_sourcedid'] = $course->idnumber;
417 if (!empty($instance->id) && !empty($instance->servicesalt) && ($islti2 ||
418 $typeconfig['acceptgrades'] == LTI_SETTING_ALWAYS ||
419 ($typeconfig['acceptgrades'] == LTI_SETTING_DELEGATE && $instance->instructorchoiceacceptgrades == LTI_SETTING_ALWAYS))
421 $placementsecret = $instance->servicesalt;
422 $sourcedid = json_encode(lti_build_sourcedid($instance->id, $USER->id, $placementsecret, $typeid));
423 $requestparams['lis_result_sourcedid'] = $sourcedid;
425 // Add outcome service URL.
426 $serviceurl = new \moodle_url('/mod/lti/service.php');
427 $serviceurl = $serviceurl->out();
430 if (!empty($CFG->mod_lti_forcessl)) {
434 if ((isset($typeconfig['forcessl']) && ($typeconfig['forcessl'] == '1')) or $forcessl) {
435 $serviceurl = lti_ensure_url_is_https($serviceurl);
438 $requestparams['lis_outcome_service_url'] = $serviceurl;
441 // Send user's name and email data if appropriate.
442 if ($islti2 || $typeconfig['sendname'] == LTI_SETTING_ALWAYS ||
443 ($typeconfig['sendname'] == LTI_SETTING_DELEGATE && isset($instance->instructorchoicesendname)
444 && $instance->instructorchoicesendname == LTI_SETTING_ALWAYS)
446 $requestparams['lis_person_name_given'] = $USER->firstname;
447 $requestparams['lis_person_name_family'] = $USER->lastname;
448 $requestparams['lis_person_name_full'] = $USER->firstname . ' ' . $USER->lastname;
449 $requestparams['ext_user_username'] = $USER->username;
452 if ($islti2 || $typeconfig['sendemailaddr'] == LTI_SETTING_ALWAYS ||
453 ($typeconfig['sendemailaddr'] == LTI_SETTING_DELEGATE && isset($instance->instructorchoicesendemailaddr)
454 && $instance->instructorchoicesendemailaddr == LTI_SETTING_ALWAYS)
456 $requestparams['lis_person_contact_email_primary'] = $USER->email;
459 return $requestparams;
463 * This function builds the request that must be sent to an LTI 2 tool provider
465 * @param object $tool Basic LTI tool object
466 * @param array $params Custom launch parameters
468 * @return array Request details
470 function lti_build_request_lti2($tool, $params) {
472 $requestparams = array();
474 $capabilities = lti_get_capabilities();
475 $enabledcapabilities = explode("\n", $tool->enabledcapability);
476 foreach ($enabledcapabilities as $capability) {
477 if (array_key_exists($capability, $capabilities)) {
478 $val = $capabilities[$capability];
479 if ($val && (substr($val, 0, 1) != '$')) {
480 if (isset($params[$val])) {
481 $requestparams[$capabilities[$capability]] = $params[$capabilities[$capability]];
487 return $requestparams;
492 * This function builds the standard parameters for an LTI 1 or 2 request that must be sent to the tool producer
494 * @param stdClass $instance Basic LTI instance object
495 * @param string $orgid Organisation ID
496 * @param boolean $islti2 True if an LTI 2 tool is being launched
497 * @param string $messagetype The request message type. Defaults to basic-lti-launch-request if empty.
499 * @return array Request details
501 function lti_build_standard_request($instance, $orgid, $islti2, $messagetype = 'basic-lti-launch-request') {
504 $requestparams = array();
507 $requestparams['resource_link_id'] = $instance->id;
508 if (property_exists($instance, 'resource_link_id') and !empty($instance->resource_link_id)) {
509 $requestparams['resource_link_id'] = $instance->resource_link_id;
513 $requestparams['launch_presentation_locale'] = current_language();
515 // Make sure we let the tool know what LMS they are being called from.
516 $requestparams['ext_lms'] = 'moodle-2';
517 $requestparams['tool_consumer_info_product_family_code'] = 'moodle';
518 $requestparams['tool_consumer_info_version'] = strval($CFG->version);
520 // Add oauth_callback to be compliant with the 1.0A spec.
521 $requestparams['oauth_callback'] = 'about:blank';
524 $requestparams['lti_version'] = 'LTI-1p0';
526 $requestparams['lti_version'] = 'LTI-2p0';
528 $requestparams['lti_message_type'] = $messagetype;
531 $requestparams["tool_consumer_instance_guid"] = $orgid;
533 if (!empty($CFG->mod_lti_institution_name)) {
534 $requestparams['tool_consumer_instance_name'] = trim(html_to_text($CFG->mod_lti_institution_name, 0));
536 $requestparams['tool_consumer_instance_name'] = get_site()->shortname;
538 $requestparams['tool_consumer_instance_description'] = trim(html_to_text(get_site()->fullname, 0));
540 return $requestparams;
544 * This function builds the custom parameters
546 * @param object $toolproxy Tool proxy instance object
547 * @param object $tool Tool instance object
548 * @param object $instance Tool placement instance object
549 * @param array $params LTI launch parameters
550 * @param string $customstr Custom parameters defined for tool
551 * @param string $instructorcustomstr Custom parameters defined for this placement
552 * @param boolean $islti2 True if an LTI 2 tool is being launched
554 * @return array Custom parameters
556 function lti_build_custom_parameters($toolproxy, $tool, $instance, $params, $customstr, $instructorcustomstr, $islti2) {
558 // Concatenate the custom parameters from the administrator and the instructor
559 // Instructor parameters are only taken into consideration if the administrator
560 // has given permission.
563 $custom = lti_split_custom_parameters($toolproxy, $tool, $params, $customstr, $islti2);
565 if (!isset($typeconfig['allowinstructorcustom']) || $typeconfig['allowinstructorcustom'] != LTI_SETTING_NEVER) {
566 if ($instructorcustomstr) {
567 $custom = array_merge(lti_split_custom_parameters($toolproxy, $tool, $params,
568 $instructorcustomstr, $islti2), $custom);
572 $custom = array_merge(lti_split_custom_parameters($toolproxy, $tool, $params,
573 $tool->parameter, true), $custom);
574 $settings = lti_get_tool_settings($tool->toolproxyid);
575 $custom = array_merge($custom, lti_get_custom_parameters($toolproxy, $tool, $params, $settings));
576 if (!empty($instance->course)) {
577 $settings = lti_get_tool_settings($tool->toolproxyid, $instance->course);
578 $custom = array_merge($custom, lti_get_custom_parameters($toolproxy, $tool, $params, $settings));
579 if (!empty($instance->id)) {
580 $settings = lti_get_tool_settings($tool->toolproxyid, $instance->course, $instance->id);
581 $custom = array_merge($custom, lti_get_custom_parameters($toolproxy, $tool, $params, $settings));
590 * Builds a standard LTI Content-Item selection request.
592 * @param int $id The tool type ID.
593 * @param stdClass $course The course object.
594 * @param moodle_url $returnurl The return URL in the tool consumer (TC) that the tool provider (TP)
595 * will use to return the Content-Item message.
596 * @param string $title The tool's title, if available.
597 * @param string $text The text to display to represent the content item. This value may be a long description of the content item.
598 * @param array $mediatypes Array of MIME types types supported by the TC. If empty, the TC will support ltilink by default.
599 * @param array $presentationtargets Array of ways in which the selected content item(s) can be requested to be opened
600 * (via the presentationDocumentTarget element for a returned content item).
601 * If empty, "frame", "iframe", and "window" will be supported by default.
602 * @param bool $autocreate Indicates whether any content items returned by the TP would be automatically persisted without
603 * @param bool $multiple Indicates whether the user should be permitted to select more than one item. False by default.
604 * any option for the user to cancel the operation. False by default.
605 * @param bool $unsigned Indicates whether the TC is willing to accept an unsigned return message, or not.
606 * A signed message should always be required when the content item is being created automatically in the
607 * TC without further interaction from the user. False by default.
608 * @param bool $canconfirm Flag for can_confirm parameter. False by default.
609 * @param bool $copyadvice Indicates whether the TC is able and willing to make a local copy of a content item. False by default.
610 * @return stdClass The object containing the signed request parameters and the URL to the TP's Content-Item selection interface.
611 * @throws moodle_exception When the LTI tool type does not exist.`
612 * @throws coding_exception For invalid media type and presentation target parameters.
614 function lti_build_content_item_selection_request($id, $course, moodle_url $returnurl, $title = '', $text = '', $mediatypes = [],
615 $presentationtargets = [], $autocreate = false, $multiple = false,
616 $unsigned = false, $canconfirm = false, $copyadvice = false) {
617 $tool = lti_get_type($id);
618 // Validate parameters.
620 throw new moodle_exception('errortooltypenotfound', 'mod_lti');
622 if (!is_array($mediatypes)) {
623 throw new coding_exception('The list of accepted media types should be in an array');
625 if (!is_array($presentationtargets)) {
626 throw new coding_exception('The list of accepted presentation targets should be in an array');
629 // Check title. If empty, use the tool's name.
631 $title = $tool->name;
634 $typeconfig = lti_get_type_config($id);
638 if (isset($tool->toolproxyid)) {
640 $toolproxy = lti_get_tool_proxy($tool->toolproxyid);
641 $key = $toolproxy->guid;
642 $secret = $toolproxy->secret;
645 if (!empty($typeconfig['resourcekey'])) {
646 $key = $typeconfig['resourcekey'];
648 if (!empty($typeconfig['password'])) {
649 $secret = $typeconfig['password'];
652 $tool->enabledcapability = '';
653 if (!empty($typeconfig['enabledcapability_ContentItemSelectionRequest'])) {
654 $tool->enabledcapability = $typeconfig['enabledcapability_ContentItemSelectionRequest'];
657 $tool->parameter = '';
658 if (!empty($typeconfig['parameter_ContentItemSelectionRequest'])) {
659 $tool->parameter = $typeconfig['parameter_ContentItemSelectionRequest'];
663 if (!empty($typeconfig['toolurl_ContentItemSelectionRequest'])) {
664 $toolurl = new moodle_url($typeconfig['toolurl_ContentItemSelectionRequest']);
666 $toolurl = new moodle_url($typeconfig['toolurl']);
669 // Check if SSL is forced.
670 if (!empty($typeconfig['forcessl'])) {
671 // Make sure the tool URL is set to https.
672 if (strtolower($toolurl->get_scheme()) === 'http') {
673 $toolurl->set_scheme('https');
675 // Make sure the return URL is set to https.
676 if (strtolower($returnurl->get_scheme()) === 'http') {
677 $returnurl->set_scheme('https');
680 $toolurlout = $toolurl->out(false);
682 // Get base request parameters.
683 $instance = new stdClass();
684 $instance->course = $course->id;
685 $requestparams = lti_build_request($instance, $typeconfig, $course, $id, $islti2);
687 // Get LTI2-specific request parameters and merge to the request parameters if applicable.
689 $lti2params = lti_build_request_lti2($tool, $requestparams);
690 $requestparams = array_merge($requestparams, $lti2params);
693 // Get standard request parameters and merge to the request parameters.
694 $orgid = !empty($typeconfig['organizationid']) ? $typeconfig['organizationid'] : '';
695 $standardparams = lti_build_standard_request(null, $orgid, $islti2, 'ContentItemSelectionRequest');
696 $requestparams = array_merge($requestparams, $standardparams);
698 // Get custom request parameters and merge to the request parameters.
700 if (!empty($typeconfig['customparameters'])) {
701 $customstr = $typeconfig['customparameters'];
703 $customparams = lti_build_custom_parameters($toolproxy, $tool, $instance, $requestparams, $customstr, '', $islti2);
704 $requestparams = array_merge($requestparams, $customparams);
706 // Allow request params to be updated by sub-plugins.
707 $plugins = core_component::get_plugin_list('ltisource');
708 foreach (array_keys($plugins) as $plugin) {
709 $pluginparams = component_callback('ltisource_' . $plugin, 'before_launch', [$instance, $toolurlout, $requestparams], []);
711 if (!empty($pluginparams) && is_array($pluginparams)) {
712 $requestparams = array_merge($requestparams, $pluginparams);
716 // Media types. Set to ltilink by default if empty.
717 if (empty($mediatypes)) {
719 'application/vnd.ims.lti.v1.ltilink',
722 $requestparams['accept_media_types'] = implode(',', $mediatypes);
724 // Presentation targets. Supports frame, iframe, window by default if empty.
725 if (empty($presentationtargets)) {
726 $presentationtargets = [
732 $requestparams['accept_presentation_document_targets'] = implode(',', $presentationtargets);
734 // Other request parameters.
735 $requestparams['accept_copy_advice'] = $copyadvice === true ? 'true' : 'false';
736 $requestparams['accept_multiple'] = $multiple === true ? 'true' : 'false';
737 $requestparams['accept_unsigned'] = $unsigned === true ? 'true' : 'false';
738 $requestparams['auto_create'] = $autocreate === true ? 'true' : 'false';
739 $requestparams['can_confirm'] = $canconfirm === true ? 'true' : 'false';
740 $requestparams['content_item_return_url'] = $returnurl->out(false);
741 $requestparams['title'] = $title;
742 $requestparams['text'] = $text;
743 $signedparams = lti_sign_parameters($requestparams, $toolurlout, 'POST', $key, $secret);
744 $toolurlparams = $toolurl->params();
746 // Strip querystring params in endpoint url from $signedparams to avoid duplication.
747 if (!empty($toolurlparams) && !empty($signedparams)) {
748 foreach (array_keys($toolurlparams) as $paramname) {
749 if (isset($signedparams[$paramname])) {
750 unset($signedparams[$paramname]);
755 // Check for params that should not be passed. Unset if they are set.
758 'resource_link_title',
759 'resource_link_description',
760 'launch_presentation_return_url',
761 'lis_result_sourcedid',
763 foreach ($unwantedparams as $param) {
764 if (isset($signedparams[$param])) {
765 unset($signedparams[$param]);
769 // Prepare result object.
770 $result = new stdClass();
771 $result->params = $signedparams;
772 $result->url = $toolurlout;
778 * Processes the tool provider's response to the ContentItemSelectionRequest and builds the configuration data from the
779 * selected content item. This configuration data can be then used when adding a tool into the course.
781 * @param int $typeid The tool type ID.
782 * @param string $messagetype The value for the lti_message_type parameter.
783 * @param string $ltiversion The value for the lti_version parameter.
784 * @param string $consumerkey The consumer key.
785 * @param string $contentitemsjson The JSON string for the content_items parameter.
786 * @return stdClass The array of module information objects.
787 * @throws moodle_exception
788 * @throws lti\OAuthException
790 function lti_tool_configuration_from_content_item($typeid, $messagetype, $ltiversion, $consumerkey, $contentitemsjson) {
791 $tool = lti_get_type($typeid);
792 // Validate parameters.
794 throw new moodle_exception('errortooltypenotfound', 'mod_lti');
796 // Check lti_message_type. Show debugging if it's not set to ContentItemSelection.
797 // No need to throw exceptions for now since lti_message_type does not seem to be used in this processing at the moment.
798 if ($messagetype !== 'ContentItemSelection') {
799 debugging("lti_message_type is invalid: {$messagetype}. It should be set to 'ContentItemSelection'.",
803 $typeconfig = lti_get_type_config($typeid);
805 if (isset($tool->toolproxyid)) {
807 $toolproxy = lti_get_tool_proxy($tool->toolproxyid);
808 $key = $toolproxy->guid;
809 $secret = $toolproxy->secret;
813 if (!empty($typeconfig['resourcekey'])) {
814 $key = $typeconfig['resourcekey'];
818 if (!empty($typeconfig['password'])) {
819 $secret = $typeconfig['password'];
825 // Check LTI versions from our side and the response's side. Show debugging if they don't match.
826 // No need to throw exceptions for now since LTI version does not seem to be used in this processing at the moment.
827 $expectedversion = LTI_VERSION_1;
829 $expectedversion = LTI_VERSION_2;
831 if ($ltiversion !== $expectedversion) {
832 debugging("lti_version from response does not match the tool's configuration. Tool: {$expectedversion}," .
833 " Response: {$ltiversion}", DEBUG_DEVELOPER);
836 if ($consumerkey !== $key) {
837 throw new moodle_exception('errorincorrectconsumerkey', 'mod_lti');
840 $store = new lti\TrivialOAuthDataStore();
841 $store->add_consumer($key, $secret);
842 $server = new lti\OAuthServer($store);
843 $method = new lti\OAuthSignatureMethod_HMAC_SHA1();
844 $server->add_signature_method($method);
845 $request = lti\OAuthRequest::from_request();
847 $server->verify_request($request);
848 } catch (lti\OAuthException $e) {
849 throw new lti\OAuthException("OAuth signature failed: " . $e->getMessage());
852 $items = json_decode($contentitemsjson);
854 throw new moodle_exception('errorinvaliddata', 'mod_lti', '', $contentitemsjson);
856 if ($items->{'@context'} !== 'http://purl.imsglobal.org/ctx/lti/v1/ContentItem') {
857 throw new moodle_exception('errorinvalidmediatype', 'mod_lti', '', $items->{'@context'});
859 if (!isset($items->{'@graph'}) || !is_array($items->{'@graph'}) || (count($items->{'@graph'}) > 1)) {
860 throw new moodle_exception('errorinvalidresponseformat', 'mod_lti');
864 if (!empty($items->{'@graph'})) {
865 $item = $items->{'@graph'}[0];
867 $config = new stdClass();
869 if (isset($item->title)) {
870 $config->name = $item->title;
872 if (empty($config->name)) {
873 $config->name = $tool->name;
875 if (isset($item->text)) {
876 $config->introeditor = [
877 'text' => $item->text,
878 'format' => FORMAT_PLAIN
881 if (isset($item->icon->{'@id'})) {
882 $iconurl = new moodle_url($item->icon->{'@id'});
883 // Assign item's icon URL to secureicon or icon depending on its scheme.
884 if (strtolower($iconurl->get_scheme()) === 'https') {
885 $config->secureicon = $iconurl->out(false);
887 $config->icon = $iconurl->out(false);
890 if (isset($item->url)) {
891 $url = new moodle_url($item->url);
892 // Assign item URL to securetoolurl or toolurl depending on its scheme.
893 if (strtolower($url->get_scheme()) === 'https') {
894 $config->securetoolurl = $url->out(false);
896 $config->toolurl = $url->out(false);
900 $config->typeid = $typeid;
902 $config->instructorchoicesendname = LTI_SETTING_NEVER;
903 $config->instructorchoicesendemailaddr = LTI_SETTING_NEVER;
904 $config->instructorchoiceacceptgrades = LTI_SETTING_NEVER;
905 $config->launchcontainer = LTI_LAUNCH_CONTAINER_DEFAULT;
906 if (isset($item->placementAdvice->presentationDocumentTarget)) {
907 if ($item->placementAdvice->presentationDocumentTarget === 'window') {
908 $config->launchcontainer = LTI_LAUNCH_CONTAINER_WINDOW;
909 } else if ($item->placementAdvice->presentationDocumentTarget === 'frame') {
910 $config->launchcontainer = LTI_LAUNCH_CONTAINER_EMBED_NO_BLOCKS;
911 } else if ($item->placementAdvice->presentationDocumentTarget === 'iframe') {
912 $config->launchcontainer = LTI_LAUNCH_CONTAINER_EMBED;
915 if (isset($item->custom)) {
916 $customparameters = [];
917 foreach ($item->custom as $key => $value) {
918 $customparameters[] = "{$key}={$value}";
920 $config->instructorcustomparameters = implode("\n", $customparameters);
926 function lti_get_tool_table($tools, $id) {
927 global $CFG, $OUTPUT, $USER;
930 $typename = get_string('typename', 'lti');
931 $baseurl = get_string('baseurl', 'lti');
932 $action = get_string('action', 'lti');
933 $createdon = get_string('createdon', 'lti');
935 if (!empty($tools)) {
937 <div id=\"{$id}_tools_container\" style=\"margin-top:.5em;margin-bottom:.5em\">
938 <table id=\"{$id}_tools\">
949 foreach ($tools as $type) {
950 $date = userdate($type->timecreated, get_string('strftimedatefullshort', 'core_langconfig'));
951 $accept = get_string('accept', 'lti');
952 $update = get_string('update', 'lti');
953 $delete = get_string('delete', 'lti');
955 if (empty($type->toolproxyid)) {
956 $baseurl = new \moodle_url('/mod/lti/typessettings.php', array(
957 'action' => 'accept',
959 'sesskey' => sesskey(),
962 $ref = $type->baseurl;
964 $baseurl = new \moodle_url('/mod/lti/toolssettings.php', array(
965 'action' => 'accept',
967 'sesskey' => sesskey(),
970 $ref = $type->tpname;
973 $accepthtml = $OUTPUT->action_icon($baseurl,
974 new \pix_icon('t/check', $accept, '', array('class' => 'iconsmall')), null,
975 array('title' => $accept, 'class' => 'editing_accept'));
977 $deleteaction = 'delete';
979 if ($type->state == LTI_TOOL_STATE_CONFIGURED) {
983 if ($type->state != LTI_TOOL_STATE_REJECTED) {
984 $deleteaction = 'reject';
985 $delete = get_string('reject', 'lti');
988 $updateurl = clone($baseurl);
989 $updateurl->param('action', 'update');
990 $updatehtml = $OUTPUT->action_icon($updateurl,
991 new \pix_icon('t/edit', $update, '', array('class' => 'iconsmall')), null,
992 array('title' => $update, 'class' => 'editing_update'));
994 if (($type->state != LTI_TOOL_STATE_REJECTED) || empty($type->toolproxyid)) {
995 $deleteurl = clone($baseurl);
996 $deleteurl->param('action', $deleteaction);
997 $deletehtml = $OUTPUT->action_icon($deleteurl,
998 new \pix_icon('t/delete', $delete, '', array('class' => 'iconsmall')), null,
999 array('title' => $delete, 'class' => 'editing_delete'));
1014 <td align=\"center\">
1015 {$accepthtml}{$updatehtml}{$deletehtml}
1020 $html .= '</table></div>';
1022 $html .= get_string('no_' . $id, 'lti');
1029 * This function builds the tab for a category of tool proxies
1031 * @param object $toolproxies Tool proxy instance objects
1032 * @param string $id Category ID
1034 * @return string HTML for tab
1036 function lti_get_tool_proxy_table($toolproxies, $id) {
1039 if (!empty($toolproxies)) {
1040 $typename = get_string('typename', 'lti');
1041 $url = get_string('registrationurl', 'lti');
1042 $action = get_string('action', 'lti');
1043 $createdon = get_string('createdon', 'lti');
1046 <div id="{$id}_tool_proxies_container" style="margin-top: 0.5em; margin-bottom: 0.5em">
1047 <table id="{$id}_tool_proxies">
1050 <th>{$typename}</th>
1052 <th>{$createdon}</th>
1057 foreach ($toolproxies as $toolproxy) {
1058 $date = userdate($toolproxy->timecreated, get_string('strftimedatefullshort', 'core_langconfig'));
1059 $accept = get_string('register', 'lti');
1060 $update = get_string('update', 'lti');
1061 $delete = get_string('delete', 'lti');
1063 $baseurl = new \moodle_url('/mod/lti/registersettings.php', array(
1064 'action' => 'accept',
1065 'id' => $toolproxy->id,
1066 'sesskey' => sesskey(),
1070 $registerurl = new \moodle_url('/mod/lti/register.php', array(
1071 'id' => $toolproxy->id,
1072 'sesskey' => sesskey(),
1073 'tab' => 'tool_proxy'
1076 $accepthtml = $OUTPUT->action_icon($registerurl,
1077 new \pix_icon('t/check', $accept, '', array('class' => 'iconsmall')), null,
1078 array('title' => $accept, 'class' => 'editing_accept'));
1080 $deleteaction = 'delete';
1082 if ($toolproxy->state != LTI_TOOL_PROXY_STATE_CONFIGURED) {
1086 if (($toolproxy->state == LTI_TOOL_PROXY_STATE_CONFIGURED) || ($toolproxy->state == LTI_TOOL_PROXY_STATE_PENDING)) {
1087 $delete = get_string('cancel', 'lti');
1090 $updateurl = clone($baseurl);
1091 $updateurl->param('action', 'update');
1092 $updatehtml = $OUTPUT->action_icon($updateurl,
1093 new \pix_icon('t/edit', $update, '', array('class' => 'iconsmall')), null,
1094 array('title' => $update, 'class' => 'editing_update'));
1096 $deleteurl = clone($baseurl);
1097 $deleteurl->param('action', $deleteaction);
1098 $deletehtml = $OUTPUT->action_icon($deleteurl,
1099 new \pix_icon('t/delete', $delete, '', array('class' => 'iconsmall')), null,
1100 array('title' => $delete, 'class' => 'editing_delete'));
1107 {$toolproxy->regurl}
1113 {$accepthtml}{$updatehtml}{$deletehtml}
1118 $html .= '</table></div>';
1120 $html = get_string('no_' . $id, 'lti');
1127 * Extracts the enabled capabilities into an array, including those implicitly declared in a parameter
1129 * @param object $tool Tool instance object
1131 * @return Array of enabled capabilities
1133 function lti_get_enabled_capabilities($tool) {
1134 if (!empty($tool->enabledcapability)) {
1135 $enabledcapabilities = explode("\n", $tool->enabledcapability);
1137 $enabledcapabilities = array();
1139 $paramstr = str_replace("\r\n", "\n", $tool->parameter);
1140 $paramstr = str_replace("\n\r", "\n", $paramstr);
1141 $paramstr = str_replace("\r", "\n", $paramstr);
1142 $params = explode("\n", $paramstr);
1143 foreach ($params as $param) {
1144 $pos = strpos($param, '=');
1145 if (($pos === false) || ($pos < 1)) {
1148 $value = trim(core_text::substr($param, $pos + 1, strlen($param)));
1149 if (substr($value, 0, 1) == '$') {
1150 $value = substr($value, 1);
1151 if (!in_array($value, $enabledcapabilities)) {
1152 $enabledcapabilities[] = $value;
1156 return $enabledcapabilities;
1160 * Splits the custom parameters field to the various parameters
1162 * @param object $toolproxy Tool proxy instance object
1163 * @param object $tool Tool instance object
1164 * @param array $params LTI launch parameters
1165 * @param string $customstr String containing the parameters
1166 * @param boolean $islti2 True if an LTI 2 tool is being launched
1168 * @return Array of custom parameters
1170 function lti_split_custom_parameters($toolproxy, $tool, $params, $customstr, $islti2 = false) {
1171 $customstr = str_replace("\r\n", "\n", $customstr);
1172 $customstr = str_replace("\n\r", "\n", $customstr);
1173 $customstr = str_replace("\r", "\n", $customstr);
1174 $lines = explode("\n", $customstr); // Or should this split on "/[\n;]/"?
1176 foreach ($lines as $line) {
1177 $pos = strpos($line, '=');
1178 if ( $pos === false || $pos < 1 ) {
1181 $key = trim(core_text::substr($line, 0, $pos));
1182 $val = trim(core_text::substr($line, $pos + 1, strlen($line)));
1183 $val = lti_parse_custom_parameter($toolproxy, $tool, $params, $val, $islti2);
1184 $key2 = lti_map_keyname($key);
1185 $retval['custom_'.$key2] = $val;
1186 if ($islti2 && ($key != $key2)) {
1187 $retval['custom_'.$key] = $val;
1194 * Adds the custom parameters to an array
1196 * @param object $toolproxy Tool proxy instance object
1197 * @param object $tool Tool instance object
1198 * @param array $params LTI launch parameters
1199 * @param array $parameters Array containing the parameters
1201 * @return array Array of custom parameters
1203 function lti_get_custom_parameters($toolproxy, $tool, $params, $parameters) {
1205 foreach ($parameters as $key => $val) {
1206 $key2 = lti_map_keyname($key);
1207 $val = lti_parse_custom_parameter($toolproxy, $tool, $params, $val, true);
1208 $retval['custom_'.$key2] = $val;
1209 if ($key != $key2) {
1210 $retval['custom_'.$key] = $val;
1217 * Parse a custom parameter to replace any substitution variables
1219 * @param object $toolproxy Tool proxy instance object
1220 * @param object $tool Tool instance object
1221 * @param array $params LTI launch parameters
1222 * @param string $value Custom parameter value
1223 * @param boolean $islti2 True if an LTI 2 tool is being launched
1225 * @return Parsed value of custom parameter
1227 function lti_parse_custom_parameter($toolproxy, $tool, $params, $value, $islti2) {
1228 global $USER, $COURSE;
1231 if (substr($value, 0, 1) == '\\') {
1232 $value = substr($value, 1);
1233 } else if (substr($value, 0, 1) == '$') {
1234 $value1 = substr($value, 1);
1235 $enabledcapabilities = lti_get_enabled_capabilities($tool);
1236 if (!$islti2 || in_array($value1, $enabledcapabilities)) {
1237 $capabilities = lti_get_capabilities();
1238 if (array_key_exists($value1, $capabilities)) {
1239 $val = $capabilities[$value1];
1241 if (substr($val, 0, 1) != '$') {
1242 $value = $params[$val];
1244 $valarr = explode('->', substr($val, 1), 2);
1245 $value = "{${$valarr[0]}->{$valarr[1]}}";
1246 $value = str_replace('<br />' , ' ', $value);
1247 $value = str_replace('<br>' , ' ', $value);
1248 $value = format_string($value);
1251 } else if ($islti2) {
1253 $services = lti_get_services();
1254 foreach ($services as $service) {
1255 $service->set_tool_proxy($toolproxy);
1256 $value = $service->parse_value($val);
1257 if ($val != $value) {
1269 * Used for building the names of the different custom parameters
1271 * @param string $key Parameter name
1273 * @return string Processed name
1275 function lti_map_keyname($key) {
1277 $key = core_text::strtolower(trim($key));
1278 foreach (str_split($key) as $ch) {
1279 if ( ($ch >= 'a' && $ch <= 'z') || ($ch >= '0' && $ch <= '9') ) {
1289 * Gets the IMS role string for the specified user and LTI course module.
1291 * @param mixed $user User object or user id
1292 * @param int $cmid The course module id of the LTI activity
1293 * @param int $courseid The course id of the LTI activity
1294 * @param boolean $islti2 True if an LTI 2 tool is being launched
1296 * @return string A role string suitable for passing with an LTI launch
1298 function lti_get_ims_role($user, $cmid, $courseid, $islti2) {
1302 // If no cmid is passed, check if the user is a teacher in the course
1303 // This allows other modules to programmatically "fake" a launch without
1304 // a real LTI instance.
1305 $context = context_course::instance($courseid);
1307 if (has_capability('moodle/course:manageactivities', $context, $user)) {
1308 array_push($roles, 'Instructor');
1310 array_push($roles, 'Learner');
1313 $context = context_module::instance($cmid);
1315 if (has_capability('mod/lti:manage', $context)) {
1316 array_push($roles, 'Instructor');
1318 array_push($roles, 'Learner');
1322 if (is_siteadmin($user) || has_capability('mod/lti:admin', $context)) {
1323 // Make sure admins do not have the Learner role, then set admin role.
1324 $roles = array_diff($roles, array('Learner'));
1326 array_push($roles, 'urn:lti:sysrole:ims/lis/Administrator', 'urn:lti:instrole:ims/lis/Administrator');
1328 array_push($roles, 'http://purl.imsglobal.org/vocab/lis/v2/person#Administrator');
1332 return join(',', $roles);
1336 * Returns configuration details for the tool
1338 * @param int $typeid Basic LTI tool typeid
1340 * @return array Tool Configuration
1342 function lti_get_type_config($typeid) {
1345 $query = "SELECT name, value
1346 FROM {lti_types_config}
1347 WHERE typeid = :typeid1
1349 SELECT 'toolurl' AS name, baseurl AS value
1353 SELECT 'icon' AS name, icon AS value
1357 SELECT 'secureicon' AS name, secureicon AS value
1359 WHERE id = :typeid4";
1361 $typeconfig = array();
1362 $configs = $DB->get_records_sql($query,
1363 array('typeid1' => $typeid, 'typeid2' => $typeid, 'typeid3' => $typeid, 'typeid4' => $typeid));
1365 if (!empty($configs)) {
1366 foreach ($configs as $config) {
1367 $typeconfig[$config->name] = $config->value;
1374 function lti_get_tools_by_url($url, $state, $courseid = null) {
1375 $domain = lti_get_domain_from_url($url);
1377 return lti_get_tools_by_domain($domain, $state, $courseid);
1380 function lti_get_tools_by_domain($domain, $state = null, $courseid = null) {
1383 $filters = array('tooldomain' => $domain);
1389 $statefilter = 'AND state = :state';
1392 if ($courseid && $courseid != $SITE->id) {
1393 $coursefilter = 'OR course = :courseid';
1398 WHERE tooldomain = :tooldomain
1399 AND (course = :siteid $coursefilter)
1402 return $DB->get_records_sql($query, array(
1403 'courseid' => $courseid,
1404 'siteid' => $SITE->id,
1405 'tooldomain' => $domain,
1411 * Returns all basicLTI tools configured by the administrator
1414 function lti_filter_get_types($course) {
1417 if (!empty($course)) {
1418 $where = "WHERE t.course = :course";
1419 $params = array('course' => $course);
1424 $query = "SELECT t.id, t.name, t.baseurl, t.state, t.toolproxyid, t.timecreated, tp.name tpname
1425 FROM {lti_types} t LEFT OUTER JOIN {lti_tool_proxies} tp ON t.toolproxyid = tp.id
1427 return $DB->get_records_sql($query, $params);
1431 * Given an array of tools, filter them based on their state
1433 * @param array $tools An array of lti_types records
1434 * @param int $state One of the LTI_TOOL_STATE_* constants
1437 function lti_filter_tool_types(array $tools, $state) {
1439 foreach ($tools as $key => $tool) {
1440 if ($tool->state == $state) {
1441 $return[$key] = $tool;
1448 * Returns all lti types visible in this course
1450 * @param int $courseid The id of the course to retieve types for
1451 * @param array $coursevisible options for 'coursevisible' field,
1452 * default [LTI_COURSEVISIBLE_PRECONFIGURED, LTI_COURSEVISIBLE_ACTIVITYCHOOSER]
1453 * @return stdClass[] All the lti types visible in the given course
1455 function lti_get_lti_types_by_course($courseid, $coursevisible = null) {
1458 if ($coursevisible === null) {
1459 $coursevisible = [LTI_COURSEVISIBLE_PRECONFIGURED, LTI_COURSEVISIBLE_ACTIVITYCHOOSER];
1462 list($coursevisiblesql, $coursevisparams) = $DB->get_in_or_equal($coursevisible, SQL_PARAMS_NAMED, 'coursevisible');
1465 WHERE coursevisible $coursevisiblesql
1466 AND (course = :siteid OR course = :courseid)
1467 AND state = :active";
1469 return $DB->get_records_sql($query,
1470 array('siteid' => $SITE->id, 'courseid' => $courseid, 'active' => LTI_TOOL_STATE_CONFIGURED) + $coursevisparams);
1474 * Returns tool types for lti add instance and edit page
1476 * @return array Array of lti types
1478 function lti_get_types_for_add_instance() {
1480 $admintypes = lti_get_lti_types_by_course($COURSE->id);
1483 $types[0] = (object)array('name' => get_string('automatic', 'lti'), 'course' => 0, 'toolproxyid' => null);
1485 foreach ($admintypes as $type) {
1486 $types[$type->id] = $type;
1493 * Returns a list of configured types in the given course
1495 * @param int $courseid The id of the course to retieve types for
1496 * @param int $sectionreturn section to return to for forming the URLs
1497 * @return array Array of lti types. Each element is object with properties: name, title, icon, help, helplink, link
1499 function lti_get_configured_types($courseid, $sectionreturn = 0) {
1502 $admintypes = lti_get_lti_types_by_course($courseid, [LTI_COURSEVISIBLE_ACTIVITYCHOOSER]);
1504 foreach ($admintypes as $ltitype) {
1505 $type = new stdClass();
1506 $type->modclass = MOD_CLASS_ACTIVITY;
1507 $type->name = 'lti_type_' . $ltitype->id;
1508 // Clean the name. We don't want tags here.
1509 $type->title = clean_param($ltitype->name, PARAM_NOTAGS);
1510 $trimmeddescription = trim($ltitype->description);
1511 if ($trimmeddescription != '') {
1512 // Clean the description. We don't want tags here.
1513 $type->help = clean_param($trimmeddescription, PARAM_NOTAGS);
1514 $type->helplink = get_string('modulename_shortcut_link', 'lti');
1516 if (empty($ltitype->icon)) {
1517 $type->icon = $OUTPUT->pix_icon('icon', '', 'lti', array('class' => 'icon'));
1519 $type->icon = html_writer::empty_tag('img', array('src' => $ltitype->icon, 'alt' => $ltitype->name, 'class' => 'icon'));
1521 $type->link = new moodle_url('/course/modedit.php', array('add' => 'lti', 'return' => 0, 'course' => $courseid,
1522 'sr' => $sectionreturn, 'typeid' => $ltitype->id));
1528 function lti_get_domain_from_url($url) {
1531 if (preg_match(LTI_URL_DOMAIN_REGEX, $url, $matches)) {
1536 function lti_get_tool_by_url_match($url, $courseid = null, $state = LTI_TOOL_STATE_CONFIGURED) {
1537 $possibletools = lti_get_tools_by_url($url, $state, $courseid);
1539 return lti_get_best_tool_by_url($url, $possibletools, $courseid);
1542 function lti_get_url_thumbprint($url) {
1543 // Parse URL requires a schema otherwise everything goes into 'path'. Fixed 5.4.7 or later.
1544 if (preg_match('/https?:\/\//', $url) !== 1) {
1545 $url = 'http://'.$url;
1547 $urlparts = parse_url(strtolower($url));
1548 if (!isset($urlparts['path'])) {
1549 $urlparts['path'] = '';
1552 if (!isset($urlparts['query'])) {
1553 $urlparts['query'] = '';
1556 if (!isset($urlparts['host'])) {
1557 $urlparts['host'] = '';
1560 if (substr($urlparts['host'], 0, 4) === 'www.') {
1561 $urlparts['host'] = substr($urlparts['host'], 4);
1564 $urllower = $urlparts['host'] . '/' . $urlparts['path'];
1566 if ($urlparts['query'] != '') {
1567 $urllower .= '?' . $urlparts['query'];
1573 function lti_get_best_tool_by_url($url, $tools, $courseid = null) {
1574 if (count($tools) === 0) {
1578 $urllower = lti_get_url_thumbprint($url);
1580 foreach ($tools as $tool) {
1581 $tool->_matchscore = 0;
1583 $toolbaseurllower = lti_get_url_thumbprint($tool->baseurl);
1585 if ($urllower === $toolbaseurllower) {
1586 // 100 points for exact thumbprint match.
1587 $tool->_matchscore += 100;
1588 } else if (substr($urllower, 0, strlen($toolbaseurllower)) === $toolbaseurllower) {
1589 // 50 points if tool thumbprint starts with the base URL thumbprint.
1590 $tool->_matchscore += 50;
1593 // Prefer course tools over site tools.
1594 if (!empty($courseid)) {
1595 // Minus 10 points for not matching the course id (global tools).
1596 if ($tool->course != $courseid) {
1597 $tool->_matchscore -= 10;
1602 $bestmatch = array_reduce($tools, function($value, $tool) {
1603 if ($tool->_matchscore > $value->_matchscore) {
1609 }, (object)array('_matchscore' => -1));
1611 // None of the tools are suitable for this URL.
1612 if ($bestmatch->_matchscore <= 0) {
1619 function lti_get_shared_secrets_by_key($key) {
1622 // Look up the shared secret for the specified key in both the types_config table (for configured tools)
1623 // And in the lti resource table for ad-hoc tools.
1624 $query = "SELECT t2.value
1625 FROM {lti_types_config} t1
1626 JOIN {lti_types_config} t2 ON t1.typeid = t2.typeid
1627 JOIN {lti_types} type ON t2.typeid = type.id
1628 WHERE t1.name = 'resourcekey'
1629 AND t1.value = :key1
1630 AND t2.name = 'password'
1631 AND type.state = :configured1
1633 SELECT tp.secret AS value
1634 FROM {lti_tool_proxies} tp
1635 JOIN {lti_types} t ON tp.id = t.toolproxyid
1636 WHERE tp.guid = :key2
1637 AND t.state = :configured2
1639 SELECT password AS value
1641 WHERE resourcekey = :key3";
1643 $sharedsecrets = $DB->get_records_sql($query, array('configured1' => LTI_TOOL_STATE_CONFIGURED,
1644 'configured2' => LTI_TOOL_STATE_CONFIGURED, 'key1' => $key, 'key2' => $key, 'key3' => $key));
1646 $values = array_map(function($item) {
1647 return $item->value;
1650 // There should really only be one shared secret per key. But, we can't prevent
1651 // more than one getting entered. For instance, if the same key is used for two tool providers.
1656 * Delete a Basic LTI configuration
1658 * @param int $id Configuration id
1660 function lti_delete_type($id) {
1663 // We should probably just copy the launch URL to the tool instances in this case... using a single query.
1665 $instances = $DB->get_records('lti', array('typeid' => $id));
1666 foreach ($instances as $instance) {
1667 $instance->typeid = 0;
1668 $DB->update_record('lti', $instance);
1671 $DB->delete_records('lti_types', array('id' => $id));
1672 $DB->delete_records('lti_types_config', array('typeid' => $id));
1675 function lti_set_state_for_type($id, $state) {
1678 $DB->update_record('lti_types', array('id' => $id, 'state' => $state));
1682 * Transforms a basic LTI object to an array
1684 * @param object $ltiobject Basic LTI object
1686 * @return array Basic LTI configuration details
1688 function lti_get_config($ltiobject) {
1689 $typeconfig = array();
1690 $typeconfig = (array)$ltiobject;
1691 $additionalconfig = lti_get_type_config($ltiobject->typeid);
1692 $typeconfig = array_merge($typeconfig, $additionalconfig);
1698 * Generates some of the tool configuration based on the instance details
1702 * @return Instance configuration
1705 function lti_get_type_config_from_instance($id) {
1708 $instance = $DB->get_record('lti', array('id' => $id));
1709 $config = lti_get_config($instance);
1711 $type = new \stdClass();
1712 $type->lti_fix = $id;
1713 if (isset($config['toolurl'])) {
1714 $type->lti_toolurl = $config['toolurl'];
1716 if (isset($config['instructorchoicesendname'])) {
1717 $type->lti_sendname = $config['instructorchoicesendname'];
1719 if (isset($config['instructorchoicesendemailaddr'])) {
1720 $type->lti_sendemailaddr = $config['instructorchoicesendemailaddr'];
1722 if (isset($config['instructorchoiceacceptgrades'])) {
1723 $type->lti_acceptgrades = $config['instructorchoiceacceptgrades'];
1725 if (isset($config['instructorchoiceallowroster'])) {
1726 $type->lti_allowroster = $config['instructorchoiceallowroster'];
1729 if (isset($config['instructorcustomparameters'])) {
1730 $type->lti_allowsetting = $config['instructorcustomparameters'];
1736 * Generates some of the tool configuration based on the admin configuration details
1740 * @return Configuration details
1742 function lti_get_type_type_config($id) {
1745 $basicltitype = $DB->get_record('lti_types', array('id' => $id));
1746 $config = lti_get_type_config($id);
1748 $type = new \stdClass();
1750 $type->lti_typename = $basicltitype->name;
1752 $type->typeid = $basicltitype->id;
1754 $type->toolproxyid = $basicltitype->toolproxyid;
1756 $type->lti_toolurl = $basicltitype->baseurl;
1758 $type->lti_description = $basicltitype->description;
1760 $type->lti_parameters = $basicltitype->parameter;
1762 $type->lti_icon = $basicltitype->icon;
1764 $type->lti_secureicon = $basicltitype->secureicon;
1766 if (isset($config['resourcekey'])) {
1767 $type->lti_resourcekey = $config['resourcekey'];
1769 if (isset($config['password'])) {
1770 $type->lti_password = $config['password'];
1773 if (isset($config['sendname'])) {
1774 $type->lti_sendname = $config['sendname'];
1776 if (isset($config['instructorchoicesendname'])) {
1777 $type->lti_instructorchoicesendname = $config['instructorchoicesendname'];
1779 if (isset($config['sendemailaddr'])) {
1780 $type->lti_sendemailaddr = $config['sendemailaddr'];
1782 if (isset($config['instructorchoicesendemailaddr'])) {
1783 $type->lti_instructorchoicesendemailaddr = $config['instructorchoicesendemailaddr'];
1785 if (isset($config['acceptgrades'])) {
1786 $type->lti_acceptgrades = $config['acceptgrades'];
1788 if (isset($config['instructorchoiceacceptgrades'])) {
1789 $type->lti_instructorchoiceacceptgrades = $config['instructorchoiceacceptgrades'];
1791 if (isset($config['allowroster'])) {
1792 $type->lti_allowroster = $config['allowroster'];
1794 if (isset($config['instructorchoiceallowroster'])) {
1795 $type->lti_instructorchoiceallowroster = $config['instructorchoiceallowroster'];
1798 if (isset($config['customparameters'])) {
1799 $type->lti_customparameters = $config['customparameters'];
1802 if (isset($config['forcessl'])) {
1803 $type->lti_forcessl = $config['forcessl'];
1806 if (isset($config['organizationid'])) {
1807 $type->lti_organizationid = $config['organizationid'];
1809 if (isset($config['organizationurl'])) {
1810 $type->lti_organizationurl = $config['organizationurl'];
1812 if (isset($config['organizationdescr'])) {
1813 $type->lti_organizationdescr = $config['organizationdescr'];
1815 if (isset($config['launchcontainer'])) {
1816 $type->lti_launchcontainer = $config['launchcontainer'];
1819 if (isset($config['coursevisible'])) {
1820 $type->lti_coursevisible = $config['coursevisible'];
1823 if (isset($config['contentitem'])) {
1824 $type->lti_contentitem = $config['contentitem'];
1827 if (isset($config['debuglaunch'])) {
1828 $type->lti_debuglaunch = $config['debuglaunch'];
1831 if (isset($config['module_class_type'])) {
1832 $type->lti_module_class_type = $config['module_class_type'];
1838 function lti_prepare_type_for_save($type, $config) {
1839 if (isset($config->lti_toolurl)) {
1840 $type->baseurl = $config->lti_toolurl;
1841 $type->tooldomain = lti_get_domain_from_url($config->lti_toolurl);
1843 if (isset($config->lti_description)) {
1844 $type->description = $config->lti_description;
1846 if (isset($config->lti_typename)) {
1847 $type->name = $config->lti_typename;
1849 if (isset($config->lti_coursevisible)) {
1850 $type->coursevisible = $config->lti_coursevisible;
1853 if (isset($config->lti_icon)) {
1854 $type->icon = $config->lti_icon;
1856 if (isset($config->lti_secureicon)) {
1857 $type->secureicon = $config->lti_secureicon;
1860 $type->forcessl = !empty($config->lti_forcessl) ? $config->lti_forcessl : 0;
1861 $config->lti_forcessl = $type->forcessl;
1862 if (isset($config->lti_contentitem)) {
1863 $type->contentitem = !empty($config->lti_contentitem) ? $config->lti_contentitem : 0;
1864 $config->lti_contentitem = $type->contentitem;
1867 $type->timemodified = time();
1869 unset ($config->lti_typename);
1870 unset ($config->lti_toolurl);
1871 unset ($config->lti_description);
1872 unset ($config->lti_icon);
1873 unset ($config->lti_secureicon);
1876 function lti_update_type($type, $config) {
1879 lti_prepare_type_for_save($type, $config);
1881 $clearcache = false;
1882 if (lti_request_is_using_ssl() && !empty($type->secureicon)) {
1883 $clearcache = !isset($config->oldicon) || ($config->oldicon !== $type->secureicon);
1885 $clearcache = isset($type->icon) && (!isset($config->oldicon) || ($config->oldicon !== $type->icon));
1887 unset($config->oldicon);
1889 if ($DB->update_record('lti_types', $type)) {
1890 foreach ($config as $key => $value) {
1891 if (substr($key, 0, 4) == 'lti_' && !is_null($value)) {
1892 $record = new \StdClass();
1893 $record->typeid = $type->id;
1894 $record->name = substr($key, 4);
1895 $record->value = $value;
1896 lti_update_config($record);
1899 require_once($CFG->libdir.'/modinfolib.php');
1901 $sql = "SELECT DISTINCT course
1905 $courses = $DB->get_fieldset_sql($sql, array($type->id));
1907 foreach ($courses as $courseid) {
1908 rebuild_course_cache($courseid, true);
1914 function lti_add_type($type, $config) {
1915 global $USER, $SITE, $DB;
1917 lti_prepare_type_for_save($type, $config);
1919 if (!isset($type->state)) {
1920 $type->state = LTI_TOOL_STATE_PENDING;
1923 if (!isset($type->timecreated)) {
1924 $type->timecreated = time();
1927 if (!isset($type->createdby)) {
1928 $type->createdby = $USER->id;
1931 if (!isset($type->course)) {
1932 $type->course = $SITE->id;
1935 // Create a salt value to be used for signing passed data to extension services
1936 // The outcome service uses the service salt on the instance. This can be used
1937 // for communication with services not related to a specific LTI instance.
1938 $config->lti_servicesalt = uniqid('', true);
1940 $id = $DB->insert_record('lti_types', $type);
1943 foreach ($config as $key => $value) {
1944 if (substr($key, 0, 4) == 'lti_' && !is_null($value)) {
1945 $record = new \StdClass();
1946 $record->typeid = $id;
1947 $record->name = substr($key, 4);
1948 $record->value = $value;
1950 lti_add_config($record);
1959 * Given an array of tool proxies, filter them based on their state
1961 * @param array $toolproxies An array of lti_tool_proxies records
1962 * @param int $state One of the LTI_TOOL_PROXY_STATE_* constants
1966 function lti_filter_tool_proxy_types(array $toolproxies, $state) {
1968 foreach ($toolproxies as $key => $toolproxy) {
1969 if ($toolproxy->state == $state) {
1970 $return[$key] = $toolproxy;
1977 * Get the tool proxy instance given its GUID
1979 * @param string $toolproxyguid Tool proxy GUID value
1983 function lti_get_tool_proxy_from_guid($toolproxyguid) {
1986 $toolproxy = $DB->get_record('lti_tool_proxies', array('guid' => $toolproxyguid));
1992 * Get the tool proxy instance given its registration URL
1994 * @param string $regurl Tool proxy registration URL
1996 * @return array The record of the tool proxy with this url
1998 function lti_get_tool_proxies_from_registration_url($regurl) {
2001 return $DB->get_records_sql(
2002 'SELECT * FROM {lti_tool_proxies}
2003 WHERE '.$DB->sql_compare_text('regurl', 256).' = :regurl',
2004 array('regurl' => $regurl)
2009 * Generates some of the tool proxy configuration based on the admin configuration details
2013 * @return Tool Proxy details
2015 function lti_get_tool_proxy($id) {
2018 $toolproxy = $DB->get_record('lti_tool_proxies', array('id' => $id));
2023 * Returns lti tool proxies.
2025 * @param bool $orphanedonly Only retrieves tool proxies that have no type associated with them
2026 * @return array of basicLTI types
2028 function lti_get_tool_proxies($orphanedonly) {
2031 if ($orphanedonly) {
2032 $tools = $DB->get_records('lti_types');
2033 $usedproxyids = array_values($DB->get_fieldset_select('lti_types', 'toolproxyid', 'toolproxyid IS NOT NULL'));
2034 $proxies = $DB->get_records('lti_tool_proxies', null, 'state DESC, timemodified DESC');
2035 foreach ($proxies as $key => $value) {
2036 if (in_array($value->id, $usedproxyids)) {
2037 unset($proxies[$key]);
2042 return $DB->get_records('lti_tool_proxies', null, 'state DESC, timemodified DESC');
2047 * Generates some of the tool proxy configuration based on the admin configuration details
2051 * @return Tool Proxy details
2053 function lti_get_tool_proxy_config($id) {
2054 $toolproxy = lti_get_tool_proxy($id);
2056 $tp = new \stdClass();
2057 $tp->lti_registrationname = $toolproxy->name;
2058 $tp->toolproxyid = $toolproxy->id;
2059 $tp->state = $toolproxy->state;
2060 $tp->lti_registrationurl = $toolproxy->regurl;
2061 $tp->lti_capabilities = explode("\n", $toolproxy->capabilityoffered);
2062 $tp->lti_services = explode("\n", $toolproxy->serviceoffered);
2068 * Update the database with a tool proxy instance
2070 * @param object $config Tool proxy definition
2072 * @return int Record id number
2074 function lti_add_tool_proxy($config) {
2077 $toolproxy = new \stdClass();
2078 if (isset($config->lti_registrationname)) {
2079 $toolproxy->name = trim($config->lti_registrationname);
2081 if (isset($config->lti_registrationurl)) {
2082 $toolproxy->regurl = trim($config->lti_registrationurl);
2084 if (isset($config->lti_capabilities)) {
2085 $toolproxy->capabilityoffered = implode("\n", $config->lti_capabilities);
2087 $toolproxy->capabilityoffered = implode("\n", array_keys(lti_get_capabilities()));
2089 if (isset($config->lti_services)) {
2090 $toolproxy->serviceoffered = implode("\n", $config->lti_services);
2092 $func = function($s) {
2093 return $s->get_id();
2095 $servicenames = array_map($func, lti_get_services());
2096 $toolproxy->serviceoffered = implode("\n", $servicenames);
2098 if (isset($config->toolproxyid) && !empty($config->toolproxyid)) {
2099 $toolproxy->id = $config->toolproxyid;
2100 if (!isset($toolproxy->state) || ($toolproxy->state != LTI_TOOL_PROXY_STATE_ACCEPTED)) {
2101 $toolproxy->state = LTI_TOOL_PROXY_STATE_CONFIGURED;
2102 $toolproxy->guid = random_string();
2103 $toolproxy->secret = random_string();
2105 $id = lti_update_tool_proxy($toolproxy);
2107 $toolproxy->state = LTI_TOOL_PROXY_STATE_CONFIGURED;
2108 $toolproxy->timemodified = time();
2109 $toolproxy->timecreated = $toolproxy->timemodified;
2110 if (!isset($toolproxy->createdby)) {
2111 $toolproxy->createdby = $USER->id;
2113 $toolproxy->guid = random_string();
2114 $toolproxy->secret = random_string();
2115 $id = $DB->insert_record('lti_tool_proxies', $toolproxy);
2122 * Updates a tool proxy in the database
2124 * @param object $toolproxy Tool proxy
2126 * @return int Record id number
2128 function lti_update_tool_proxy($toolproxy) {
2131 $toolproxy->timemodified = time();
2132 $id = $DB->update_record('lti_tool_proxies', $toolproxy);
2138 * Delete a Tool Proxy
2140 * @param int $id Tool Proxy id
2142 function lti_delete_tool_proxy($id) {
2144 $DB->delete_records('lti_tool_settings', array('toolproxyid' => $id));
2145 $tools = $DB->get_records('lti_types', array('toolproxyid' => $id));
2146 foreach ($tools as $tool) {
2147 lti_delete_type($tool->id);
2149 $DB->delete_records('lti_tool_proxies', array('id' => $id));
2153 * Add a tool configuration in the database
2155 * @param object $config Tool configuration
2157 * @return int Record id number
2159 function lti_add_config($config) {
2162 return $DB->insert_record('lti_types_config', $config);
2166 * Updates a tool configuration in the database
2168 * @param object $config Tool configuration
2170 * @return Record id number
2172 function lti_update_config($config) {
2176 $old = $DB->get_record('lti_types_config', array('typeid' => $config->typeid, 'name' => $config->name));
2179 $config->id = $old->id;
2180 $return = $DB->update_record('lti_types_config', $config);
2182 $return = $DB->insert_record('lti_types_config', $config);
2188 * Gets the tool settings
2190 * @param int $toolproxyid Id of tool proxy record
2191 * @param int $courseid Id of course (null if system settings)
2192 * @param int $instanceid Id of course module (null if system or context settings)
2194 * @return array Array settings
2196 function lti_get_tool_settings($toolproxyid, $courseid = null, $instanceid = null) {
2199 $settings = array();
2200 $settingsstr = $DB->get_field('lti_tool_settings', 'settings', array('toolproxyid' => $toolproxyid,
2201 'course' => $courseid, 'coursemoduleid' => $instanceid));
2202 if ($settingsstr !== false) {
2203 $settings = json_decode($settingsstr, true);
2209 * Sets the tool settings (
2211 * @param array $settings Array of settings
2212 * @param int $toolproxyid Id of tool proxy record
2213 * @param int $courseid Id of course (null if system settings)
2214 * @param int $instanceid Id of course module (null if system or context settings)
2216 function lti_set_tool_settings($settings, $toolproxyid, $courseid = null, $instanceid = null) {
2219 $json = json_encode($settings);
2220 $record = $DB->get_record('lti_tool_settings', array('toolproxyid' => $toolproxyid,
2221 'course' => $courseid, 'coursemoduleid' => $instanceid));
2222 if ($record !== false) {
2223 $DB->update_record('lti_tool_settings', array('id' => $record->id, 'settings' => $json, 'timemodified' => time()));
2225 $record = new \stdClass();
2226 $record->toolproxyid = $toolproxyid;
2227 $record->course = $courseid;
2228 $record->coursemoduleid = $instanceid;
2229 $record->settings = $json;
2230 $record->timecreated = time();
2231 $record->timemodified = $record->timecreated;
2232 $DB->insert_record('lti_tool_settings', $record);
2237 * Signs the petition to launch the external tool using OAuth
2239 * @param $oldparms Parameters to be passed for signing
2240 * @param $endpoint url of the external tool
2241 * @param $method Method for sending the parameters (e.g. POST)
2242 * @param $oauth_consumoer_key Key
2243 * @param $oauth_consumoer_secret Secret
2245 function lti_sign_parameters($oldparms, $endpoint, $method, $oauthconsumerkey, $oauthconsumersecret) {
2251 // TODO: Switch to core oauthlib once implemented - MDL-30149.
2252 $hmacmethod = new lti\OAuthSignatureMethod_HMAC_SHA1();
2253 $testconsumer = new lti\OAuthConsumer($oauthconsumerkey, $oauthconsumersecret, null);
2254 $accreq = lti\OAuthRequest::from_consumer_and_token($testconsumer, $testtoken, $method, $endpoint, $parms);
2255 $accreq->sign_request($hmacmethod, $testconsumer, $testtoken);
2257 $newparms = $accreq->get_parameters();
2263 * Posts the launch petition HTML
2265 * @param $newparms Signed parameters
2266 * @param $endpoint URL of the external tool
2267 * @param $debug Debug (true/false)
2269 function lti_post_launch_html($newparms, $endpoint, $debug=false) {
2270 $r = "<form action=\"" . $endpoint .
2271 "\" name=\"ltiLaunchForm\" id=\"ltiLaunchForm\" method=\"post\" encType=\"application/x-www-form-urlencoded\">\n";
2273 // Contruct html for the launch parameters.
2274 foreach ($newparms as $key => $value) {
2275 $key = htmlspecialchars($key);
2276 $value = htmlspecialchars($value);
2277 if ( $key == "ext_submit" ) {
2278 $r .= "<input type=\"submit\"";
2280 $r .= "<input type=\"hidden\" name=\"{$key}\"";
2288 $r .= "<script language=\"javascript\"> \n";
2289 $r .= " //<![CDATA[ \n";
2290 $r .= "function basicltiDebugToggle() {\n";
2291 $r .= " var ele = document.getElementById(\"basicltiDebug\");\n";
2292 $r .= " if (ele.style.display == \"block\") {\n";
2293 $r .= " ele.style.display = \"none\";\n";
2296 $r .= " ele.style.display = \"block\";\n";
2300 $r .= "</script>\n";
2301 $r .= "<a id=\"displayText\" href=\"javascript:basicltiDebugToggle();\">";
2302 $r .= get_string("toggle_debug_data", "lti")."</a>\n";
2303 $r .= "<div id=\"basicltiDebug\" style=\"display:none\">\n";
2304 $r .= "<b>".get_string("basiclti_endpoint", "lti")."</b><br/>\n";
2305 $r .= $endpoint . "<br/>\n <br/>\n";
2306 $r .= "<b>".get_string("basiclti_parameters", "lti")."</b><br/>\n";
2307 foreach ($newparms as $key => $value) {
2308 $key = htmlspecialchars($key);
2309 $value = htmlspecialchars($value);
2310 $r .= "$key = $value<br/>\n";
2312 $r .= " <br/>\n";
2318 $r .= " <script type=\"text/javascript\"> \n" .
2320 " document.ltiLaunchForm.submit(); \n" .
2327 function lti_get_type($typeid) {
2330 return $DB->get_record('lti_types', array('id' => $typeid));
2333 function lti_get_launch_container($lti, $toolconfig) {
2334 if (empty($lti->launchcontainer)) {
2335 $lti->launchcontainer = LTI_LAUNCH_CONTAINER_DEFAULT;
2338 if ($lti->launchcontainer == LTI_LAUNCH_CONTAINER_DEFAULT) {
2339 if (isset($toolconfig['launchcontainer'])) {
2340 $launchcontainer = $toolconfig['launchcontainer'];
2343 $launchcontainer = $lti->launchcontainer;
2346 if (empty($launchcontainer) || $launchcontainer == LTI_LAUNCH_CONTAINER_DEFAULT) {
2347 $launchcontainer = LTI_LAUNCH_CONTAINER_EMBED_NO_BLOCKS;
2350 $devicetype = core_useragent::get_device_type();
2352 // Scrolling within the object element doesn't work on iOS or Android
2353 // Opening the popup window also had some issues in testing
2354 // For mobile devices, always take up the entire screen to ensure the best experience.
2355 if ($devicetype === core_useragent::DEVICETYPE_MOBILE || $devicetype === core_useragent::DEVICETYPE_TABLET ) {
2356 $launchcontainer = LTI_LAUNCH_CONTAINER_REPLACE_MOODLE_WINDOW;
2359 return $launchcontainer;
2362 function lti_request_is_using_ssl() {
2364 return (stripos($CFG->httpswwwroot, 'https://') === 0);
2367 function lti_ensure_url_is_https($url) {
2368 if (!strstr($url, '://')) {
2369 $url = 'https://' . $url;
2371 // If the URL starts with http, replace with https.
2372 if (stripos($url, 'http://') === 0) {
2373 $url = 'https://' . substr($url, 7);
2381 * Determines if we should try to log the request
2383 * @param string $rawbody
2386 function lti_should_log_request($rawbody) {
2389 if (empty($CFG->mod_lti_log_users)) {
2393 $logusers = explode(',', $CFG->mod_lti_log_users);
2394 if (empty($logusers)) {
2399 $xml = new \SimpleXMLElement($rawbody);
2400 $ns = $xml->getNamespaces();
2401 $ns = array_shift($ns);
2402 $xml->registerXPathNamespace('lti', $ns);
2403 $requestuserid = '';
2404 if ($node = $xml->xpath('//lti:userId')) {
2406 $requestuserid = clean_param((string) $node, PARAM_INT);
2407 } else if ($node = $xml->xpath('//lti:sourcedId')) {
2409 $resultjson = json_decode((string) $node);
2410 $requestuserid = clean_param($resultjson->data->userid, PARAM_INT);
2412 } catch (Exception $e) {
2416 if (empty($requestuserid) or !in_array($requestuserid, $logusers)) {
2424 * Logs the request to a file in temp dir.
2426 * @param string $rawbody
2428 function lti_log_request($rawbody) {
2429 if ($tempdir = make_temp_directory('mod_lti', false)) {
2430 if ($tempfile = tempnam($tempdir, 'mod_lti_request'.date('YmdHis'))) {
2431 $content = "Request Headers:\n";
2432 foreach (moodle\mod\lti\OAuthUtil::get_headers() as $header => $value) {
2433 $content .= "$header: $value\n";
2435 $content .= "Request Body:\n";
2436 $content .= $rawbody;
2438 file_put_contents($tempfile, $content);
2439 chmod($tempfile, 0644);
2445 * Log an LTI response.
2447 * @param string $responsexml The response XML
2448 * @param Exception $e If there was an exception, pass that too
2450 function lti_log_response($responsexml, $e = null) {
2451 if ($tempdir = make_temp_directory('mod_lti', false)) {
2452 if ($tempfile = tempnam($tempdir, 'mod_lti_response'.date('YmdHis'))) {
2454 if ($e instanceof Exception) {
2455 $info = get_exception_info($e);
2457 $content .= "Exception:\n";
2458 $content .= "Message: $info->message\n";
2459 $content .= "Debug info: $info->debuginfo\n";
2460 $content .= "Backtrace:\n";
2461 $content .= format_backtrace($info->backtrace, true);
2464 $content .= "Response XML:\n";
2465 $content .= $responsexml;
2467 file_put_contents($tempfile, $content);
2468 chmod($tempfile, 0644);
2474 * Fetches LTI type configuration for an LTI instance
2476 * @param stdClass $instance
2477 * @return array Can be empty if no type is found
2479 function lti_get_type_config_by_instance($instance) {
2481 if (empty($instance->typeid)) {
2482 $tool = lti_get_tool_by_url_match($instance->toolurl, $instance->course);
2484 $typeid = $tool->id;
2487 $typeid = $instance->typeid;
2489 if (!empty($typeid)) {
2490 return lti_get_type_config($typeid);
2496 * Enforce type config settings onto the LTI instance
2498 * @param stdClass $instance
2499 * @param array $typeconfig
2501 function lti_force_type_config_settings($instance, array $typeconfig) {
2503 'instructorchoicesendname' => 'sendname',
2504 'instructorchoicesendemailaddr' => 'sendemailaddr',
2505 'instructorchoiceacceptgrades' => 'acceptgrades',
2508 foreach ($forced as $instanceparam => $typeconfigparam) {
2509 if (array_key_exists($typeconfigparam, $typeconfig) && $typeconfig[$typeconfigparam] != LTI_SETTING_DELEGATE) {
2510 $instance->$instanceparam = $typeconfig[$typeconfigparam];
2516 * Initializes an array with the capabilities supported by the LTI module
2518 * @return array List of capability names (without a dollar sign prefix)
2520 function lti_get_capabilities() {
2522 $capabilities = array(
2523 'basic-lti-launch-request' => '',
2524 'ContentItemSelectionRequest' => '',
2525 'ToolProxyRegistrationRequest' => '',
2526 'Context.id' => 'context_id',
2527 'Context.title' => 'context_title',
2528 'Context.label' => 'context_label',
2529 'Context.sourcedId' => 'lis_course_section_sourcedid',
2530 'Context.longDescription' => '$COURSE->summary',
2531 'Context.timeFrame.begin' => '$COURSE->startdate',
2532 'CourseSection.title' => 'context_title',
2533 'CourseSection.label' => 'context_label',
2534 'CourseSection.sourcedId' => 'lis_course_section_sourcedid',
2535 'CourseSection.longDescription' => '$COURSE->summary',
2536 'CourseSection.timeFrame.begin' => '$COURSE->startdate',
2537 'ResourceLink.id' => 'resource_link_id',
2538 'ResourceLink.title' => 'resource_link_title',
2539 'ResourceLink.description' => 'resource_link_description',
2540 'User.id' => 'user_id',
2541 'User.username' => '$USER->username',
2542 'Person.name.full' => 'lis_person_name_full',
2543 'Person.name.given' => 'lis_person_name_given',
2544 'Person.name.family' => 'lis_person_name_family',
2545 'Person.email.primary' => 'lis_person_contact_email_primary',
2546 'Person.sourcedId' => 'lis_person_sourcedid',
2547 'Person.name.middle' => '$USER->middlename',
2548 'Person.address.street1' => '$USER->address',
2549 'Person.address.locality' => '$USER->city',
2550 'Person.address.country' => '$USER->country',
2551 'Person.address.timezone' => '$USER->timezone',
2552 'Person.phone.primary' => '$USER->phone1',
2553 'Person.phone.mobile' => '$USER->phone2',
2554 'Person.webaddress' => '$USER->url',
2555 'Membership.role' => 'roles',
2556 'Result.sourcedId' => 'lis_result_sourcedid',
2557 'Result.autocreate' => 'lis_outcome_service_url');
2559 return $capabilities;
2564 * Initializes an array with the services supported by the LTI module
2566 * @return array List of services
2568 function lti_get_services() {
2570 $services = array();
2571 $definedservices = core_component::get_plugin_list('ltiservice');
2572 foreach ($definedservices as $name => $location) {
2573 $classname = "\\ltiservice_{$name}\\local\\service\\{$name}";
2574 $services[] = new $classname();
2582 * Initializes an instance of the named service
2584 * @param string $servicename Name of service
2586 * @return mod_lti\local\ltiservice\service_base Service
2588 function lti_get_service_by_name($servicename) {
2591 $classname = "\\ltiservice_{$servicename}\\local\\service\\{$servicename}";
2592 if (class_exists($classname)) {
2593 $service = new $classname();
2601 * Finds a service by id
2603 * @param array $services Array of services
2604 * @param string $resourceid ID of resource
2606 * @return mod_lti\local\ltiservice\service_base Service
2608 function lti_get_service_by_resource_id($services, $resourceid) {
2611 foreach ($services as $aservice) {
2612 foreach ($aservice->get_resources() as $resource) {
2613 if ($resource->get_id() === $resourceid) {
2614 $service = $aservice;
2625 * Extracts the named contexts from a tool proxy
2627 * @param object $json
2629 * @return array Contexts
2631 function lti_get_contexts($json) {
2633 $contexts = array();
2634 if (isset($json->{'@context'})) {
2635 foreach ($json->{'@context'} as $context) {
2636 if (is_object($context)) {
2637 $contexts = array_merge(get_object_vars($context), $contexts);
2647 * Converts an ID to a fully-qualified ID
2649 * @param array $contexts
2652 * @return string Fully-qualified ID
2654 function lti_get_fqid($contexts, $id) {
2656 $parts = explode(':', $id, 2);
2657 if (count($parts) > 1) {
2658 if (array_key_exists($parts[0], $contexts)) {
2659 $id = $contexts[$parts[0]] . $parts[1];
2668 * Returns the icon for the given tool type
2670 * @param stdClass $type The tool type
2672 * @return string The url to the tool type's corresponding icon
2674 function get_tool_type_icon_url(stdClass $type) {
2677 $iconurl = $type->secureicon;
2679 if (empty($iconurl)) {
2680 $iconurl = $type->icon;
2683 if (empty($iconurl)) {
2684 $iconurl = $OUTPUT->image_url('icon', 'lti')->out();
2691 * Returns the edit url for the given tool type
2693 * @param stdClass $type The tool type
2695 * @return string The url to edit the tool type
2697 function get_tool_type_edit_url(stdClass $type) {
2698 $url = new moodle_url('/mod/lti/typessettings.php',
2699 array('action' => 'update', 'id' => $type->id, 'sesskey' => sesskey(), 'returnto' => 'toolconfigure'));
2704 * Returns the edit url for the given tool proxy.
2706 * @param stdClass $proxy The tool proxy
2708 * @return string The url to edit the tool type
2710 function get_tool_proxy_edit_url(stdClass $proxy) {
2711 $url = new moodle_url('/mod/lti/registersettings.php',
2712 array('action' => 'update', 'id' => $proxy->id, 'sesskey' => sesskey(), 'returnto' => 'toolconfigure'));
2717 * Returns the course url for the given tool type
2719 * @param stdClass $type The tool type
2721 * @return string|void The url to the course of the tool type, void if it is a site wide type
2723 function get_tool_type_course_url(stdClass $type) {
2724 if ($type->course == 1) {
2727 $url = new moodle_url('/course/view.php', array('id' => $type->course));
2733 * Returns the icon and edit urls for the tool type and the course url if it is a course type.
2735 * @param stdClass $type The tool type
2737 * @return string The urls of the tool type
2739 function get_tool_type_urls(stdClass $type) {
2740 $courseurl = get_tool_type_course_url($type);
2743 'icon' => get_tool_type_icon_url($type),
2744 'edit' => get_tool_type_edit_url($type),
2748 $urls['course'] = $courseurl;
2755 * Returns the icon and edit urls for the tool proxy.
2757 * @param stdClass $proxy The tool proxy
2759 * @return string The urls of the tool proxy
2761 function get_tool_proxy_urls(stdClass $proxy) {
2765 'icon' => $OUTPUT->image_url('icon', 'lti')->out(),
2766 'edit' => get_tool_proxy_edit_url($proxy),
2773 * Returns information on the current state of the tool type
2775 * @param stdClass $type The tool type
2777 * @return array An array with a text description of the state, and boolean for whether it is in each state:
2778 * pending, configured, rejected, unknown
2780 function get_tool_type_state_info(stdClass $type) {
2782 $isconfigured = false;
2784 $isrejected = false;
2786 switch ($type->state) {
2787 case LTI_TOOL_STATE_CONFIGURED:
2788 $state = get_string('active', 'mod_lti');
2789 $isconfigured = true;
2791 case LTI_TOOL_STATE_PENDING:
2792 $state = get_string('pending', 'mod_lti');
2795 case LTI_TOOL_STATE_REJECTED:
2796 $state = get_string('rejected', 'mod_lti');
2800 $state = get_string('unknownstate', 'mod_lti');
2807 'pending' => $ispending,
2808 'configured' => $isconfigured,
2809 'rejected' => $isrejected,
2810 'unknown' => $isunknown
2815 * Returns a summary of each LTI capability this tool type requires in plain language
2817 * @param stdClass $type The tool type
2819 * @return array An array of text descriptions of each of the capabilities this tool type requires
2821 function get_tool_type_capability_groups($type) {
2822 $capabilities = lti_get_enabled_capabilities($type);
2825 $hasactivities = false;
2826 $hasuseraccount = false;
2827 $hasuserpersonal = false;
2829 foreach ($capabilities as $capability) {
2830 // Bail out early if we've already found all groups.
2831 if (count($groups) >= 4) {
2835 if (!$hascourse && preg_match('/^CourseSection/', $capability)) {
2837 $groups[] = get_string('courseinformation', 'mod_lti');
2838 } else if (!$hasactivities && preg_match('/^ResourceLink/', $capability)) {
2839 $hasactivities = true;
2840 $groups[] = get_string('courseactivitiesorresources', 'mod_lti');
2841 } else if (!$hasuseraccount && preg_match('/^User/', $capability) || preg_match('/^Membership/', $capability)) {
2842 $hasuseraccount = true;
2843 $groups[] = get_string('useraccountinformation', 'mod_lti');
2844 } else if (!$hasuserpersonal && preg_match('/^Person/', $capability)) {
2845 $hasuserpersonal = true;
2846 $groups[] = get_string('userpersonalinformation', 'mod_lti');
2855 * Returns the ids of each instance of this tool type
2857 * @param stdClass $type The tool type
2859 * @return array An array of ids of the instances of this tool type
2861 function get_tool_type_instance_ids($type) {
2864 return array_keys($DB->get_fieldset_select('lti', 'id', 'typeid = ?', array($type->id)));
2868 * Serialises this tool type
2870 * @param stdClass $type The tool type
2872 * @return array An array of values representing this type
2874 function serialise_tool_type(stdClass $type) {
2875 $capabilitygroups = get_tool_type_capability_groups($type);
2876 $instanceids = get_tool_type_instance_ids($type);
2877 // Clean the name. We don't want tags here.
2878 $name = clean_param($type->name, PARAM_NOTAGS);
2879 if (!empty($type->description)) {
2880 // Clean the description. We don't want tags here.
2881 $description = clean_param($type->description, PARAM_NOTAGS);
2883 $description = get_string('editdescription', 'mod_lti');
2888 'description' => $description,
2889 'urls' => get_tool_type_urls($type),
2890 'state' => get_tool_type_state_info($type),
2891 'hascapabilitygroups' => !empty($capabilitygroups),
2892 'capabilitygroups' => $capabilitygroups,
2893 // Course ID of 1 means it's not linked to a course.
2894 'courseid' => $type->course == 1 ? 0 : $type->course,
2895 'instanceids' => $instanceids,
2896 'instancecount' => count($instanceids)
2901 * Serialises this tool proxy.
2903 * @param stdClass $proxy The tool proxy
2905 * @return array An array of values representing this type
2907 function serialise_tool_proxy(stdClass $proxy) {
2910 'name' => $proxy->name,
2911 'description' => get_string('activatetoadddescription', 'mod_lti'),
2912 'urls' => get_tool_proxy_urls($proxy),
2914 'text' => get_string('pending', 'mod_lti'),
2916 'configured' => false,
2917 'rejected' => false,
2920 'hascapabilitygroups' => true,
2921 'capabilitygroups' => array(),
2923 'instanceids' => array(),
2924 'instancecount' => 0
2929 * Loads the cartridge information into the tool type, if the launch url is for a cartridge file
2931 * @param stdClass $type The tool type object to be filled in
2934 function lti_load_type_if_cartridge($type) {
2935 if (!empty($type->lti_toolurl) && lti_is_cartridge($type->lti_toolurl)) {
2936 lti_load_type_from_cartridge($type->lti_toolurl, $type);
2941 * Loads the cartridge information into the new tool, if the launch url is for a cartridge file
2943 * @param stdClass $lti The tools config
2946 function lti_load_tool_if_cartridge($lti) {
2947 if (!empty($lti->toolurl) && lti_is_cartridge($lti->toolurl)) {
2948 lti_load_tool_from_cartridge($lti->toolurl, $lti);
2953 * Determines if the given url is for a IMS basic cartridge
2955 * @param string $url The url to be checked
2956 * @return True if the url is for a cartridge
2959 function lti_is_cartridge($url) {
2960 // If it is empty, it's not a cartridge.
2964 // If it has xml at the end of the url, it's a cartridge.
2965 if (preg_match('/\.xml$/', $url)) {
2968 // Even if it doesn't have .xml, load the url to check if it's a cartridge..
2970 $toolinfo = lti_load_cartridge($url,
2972 "launch_url" => "launchurl"
2975 if (!empty($toolinfo['launchurl'])) {
2978 } catch (moodle_exception $e) {
2979 return false; // Error loading the xml, so it's not a cartridge.
2985 * Allows you to load settings for an external tool type from an IMS cartridge.
2987 * @param string $url The URL to the cartridge
2988 * @param stdClass $type The tool type object to be filled in
2989 * @throws moodle_exception if the cartridge could not be loaded correctly
2992 function lti_load_type_from_cartridge($url, $type) {
2993 $toolinfo = lti_load_cartridge($url,
2995 "title" => "lti_typename",
2996 "launch_url" => "lti_toolurl",
2997 "description" => "lti_description",
2998 "icon" => "lti_icon",
2999 "secure_icon" => "lti_secureicon"
3002 "icon_url" => "lti_extension_icon",
3003 "secure_icon_url" => "lti_extension_secureicon"
3006 // If an activity name exists, unset the cartridge name so we don't override it.
3007 if (isset($type->lti_typename)) {
3008 unset($toolinfo['lti_typename']);
3011 // Always prefer cartridge core icons first, then, if none are found, look at the extension icons.
3012 if (empty($toolinfo['lti_icon']) && !empty($toolinfo['lti_extension_icon'])) {
3013 $toolinfo['lti_icon'] = $toolinfo['lti_extension_icon'];
3015 unset($toolinfo['lti_extension_icon']);
3017 if (empty($toolinfo['lti_secureicon']) && !empty($toolinfo['lti_extension_secureicon'])) {
3018 $toolinfo['lti_secureicon'] = $toolinfo['lti_extension_secureicon'];
3020 unset($toolinfo['lti_extension_secureicon']);
3022 foreach ($toolinfo as $property => $value) {
3023 $type->$property = $value;
3028 * Allows you to load in the configuration for an external tool from an IMS cartridge.
3030 * @param string $url The URL to the cartridge
3031 * @param stdClass $lti LTI object
3032 * @throws moodle_exception if the cartridge could not be loaded correctly
3035 function lti_load_tool_from_cartridge($url, $lti) {
3036 $toolinfo = lti_load_cartridge($url,
3039 "launch_url" => "toolurl",
3040 "secure_launch_url" => "securetoolurl",
3041 "description" => "intro",
3043 "secure_icon" => "secureicon"
3046 "icon_url" => "extension_icon",
3047 "secure_icon_url" => "extension_secureicon"
3050 // If an activity name exists, unset the cartridge name so we don't override it.
3051 if (isset($lti->name)) {
3052 unset($toolinfo['name']);
3055 // Always prefer cartridge core icons first, then, if none are found, look at the extension icons.
3056 if (empty($toolinfo['icon']) && !empty($toolinfo['extension_icon'])) {
3057 $toolinfo['icon'] = $toolinfo['extension_icon'];
3059 unset($toolinfo['extension_icon']);
3061 if (empty($toolinfo['secureicon']) && !empty($toolinfo['extension_secureicon'])) {
3062 $toolinfo['secureicon'] = $toolinfo['extension_secureicon'];
3064 unset($toolinfo['extension_secureicon']);
3066 foreach ($toolinfo as $property => $value) {
3067 $lti->$property = $value;
3072 * Search for a tag within an XML DOMDocument
3074 * @param string $url The url of the cartridge to be loaded
3075 * @param array $map The map of tags to keys in the return array
3076 * @param array $propertiesmap The map of properties to keys in the return array
3077 * @return array An associative array with the given keys and their values from the cartridge
3078 * @throws moodle_exception if the cartridge could not be loaded correctly
3081 function lti_load_cartridge($url, $map, $propertiesmap = array()) {
3083 require_once($CFG->libdir. "/filelib.php");
3086 $response = $curl->get($url);
3088 // TODO MDL-46023 Replace this code with a call to the new library.
3089 $origerrors = libxml_use_internal_errors(true);
3090 $origentity = libxml_disable_entity_loader(true);
3091 libxml_clear_errors();
3093 $document = new DOMDocument();
3094 @$document->loadXML($response, LIBXML_DTDLOAD | LIBXML_DTDATTR);
3096 $cartridge = new DomXpath($document);
3098 $errors = libxml_get_errors();
3100 libxml_clear_errors();
3101 libxml_use_internal_errors($origerrors);
3102 libxml_disable_entity_loader($origentity);
3104 if (count($errors) > 0) {
3105 $message = 'Failed to load cartridge.';
3106 foreach ($errors as $error) {
3107 $message .= "\n" . trim($error->message, "\n\r\t .") . " at line " . $error->line;
3109 throw new moodle_exception('errorreadingfile', '', '', $url, $message);
3112 $toolinfo = array();
3113 foreach ($map as $tag => $key) {
3114 $value = get_tag($tag, $cartridge);
3116 $toolinfo[$key] = $value;
3119 if (!empty($propertiesmap)) {
3120 foreach ($propertiesmap as $property => $key) {
3121 $value = get_tag("property", $cartridge, $property);
3123 $toolinfo[$key] = $value;
3132 * Search for a tag within an XML DOMDocument
3134 * @param stdClass $tagname The name of the tag to search for
3135 * @param XPath $xpath The XML to find the tag in
3136 * @param XPath $attribute The attribute to search for (if we should search for a child node with the given
3137 * value for the name attribute
3140 function get_tag($tagname, $xpath, $attribute = null) {
3142 $result = $xpath->query('//*[local-name() = \'' . $tagname . '\'][@name="' . $attribute . '"]');
3144 $result = $xpath->query('//*[local-name() = \'' . $tagname . '\']');
3146 if ($result->length > 0) {
3147 return $result->item(0)->nodeValue;