MDL-69240 tool_moodlenet: Clean MoodleNet profile field
[moodle.git] / mod / lti / locallib.php
bloba5db3ec0585a7a36368945513d1ca5eb177b3fee
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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.
35 /**
36 * This file contains the library of functions and constants for the lti module
38 * @package mod_lti
39 * @copyright 2009 Marc Alier, Jordi Piguillem, Nikolas Galanis
40 * marc.alier@upc.edu
41 * @copyright 2009 Universitat Politecnica de Catalunya http://www.upc.edu
42 * @author Marc Alier
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;
55 use Firebase\JWT\JWT;
56 use Firebase\JWT\JWK;
58 global $CFG;
59 require_once($CFG->dirroot.'/mod/lti/OAuth.php');
60 require_once($CFG->libdir.'/weblib.php');
61 require_once($CFG->dirroot . '/course/modlib.php');
62 require_once($CFG->dirroot . '/mod/lti/TrivialStore.php');
64 define('LTI_URL_DOMAIN_REGEX', '/(?:https?:\/\/)?(?:www\.)?([^\/]+)(?:\/|$)/i');
66 define('LTI_LAUNCH_CONTAINER_DEFAULT', 1);
67 define('LTI_LAUNCH_CONTAINER_EMBED', 2);
68 define('LTI_LAUNCH_CONTAINER_EMBED_NO_BLOCKS', 3);
69 define('LTI_LAUNCH_CONTAINER_WINDOW', 4);
70 define('LTI_LAUNCH_CONTAINER_REPLACE_MOODLE_WINDOW', 5);
72 define('LTI_TOOL_STATE_ANY', 0);
73 define('LTI_TOOL_STATE_CONFIGURED', 1);
74 define('LTI_TOOL_STATE_PENDING', 2);
75 define('LTI_TOOL_STATE_REJECTED', 3);
76 define('LTI_TOOL_PROXY_TAB', 4);
78 define('LTI_TOOL_PROXY_STATE_CONFIGURED', 1);
79 define('LTI_TOOL_PROXY_STATE_PENDING', 2);
80 define('LTI_TOOL_PROXY_STATE_ACCEPTED', 3);
81 define('LTI_TOOL_PROXY_STATE_REJECTED', 4);
83 define('LTI_SETTING_NEVER', 0);
84 define('LTI_SETTING_ALWAYS', 1);
85 define('LTI_SETTING_DELEGATE', 2);
87 define('LTI_COURSEVISIBLE_NO', 0);
88 define('LTI_COURSEVISIBLE_PRECONFIGURED', 1);
89 define('LTI_COURSEVISIBLE_ACTIVITYCHOOSER', 2);
91 define('LTI_VERSION_1', 'LTI-1p0');
92 define('LTI_VERSION_2', 'LTI-2p0');
93 define('LTI_VERSION_1P3', '1.3.0');
94 define('LTI_RSA_KEY', 'RSA_KEY');
95 define('LTI_JWK_KEYSET', 'JWK_KEYSET');
97 define('LTI_DEFAULT_ORGID_SITEID', 'SITEID');
98 define('LTI_DEFAULT_ORGID_SITEHOST', 'SITEHOST');
100 define('LTI_ACCESS_TOKEN_LIFE', 3600);
102 // Standard prefix for JWT claims.
103 define('LTI_JWT_CLAIM_PREFIX', 'https://purl.imsglobal.org/spec/lti');
106 * Return the mapping for standard message types to JWT message_type claim.
108 * @return array
110 function lti_get_jwt_message_type_mapping() {
111 return array(
112 'basic-lti-launch-request' => 'LtiResourceLinkRequest',
113 'ContentItemSelectionRequest' => 'LtiDeepLinkingRequest',
114 'LtiDeepLinkingResponse' => 'ContentItemSelection',
119 * Return the mapping for standard message parameters to JWT claim.
121 * @return array
123 function lti_get_jwt_claim_mapping() {
124 return array(
125 'accept_copy_advice' => [
126 'suffix' => 'dl',
127 'group' => 'deep_linking_settings',
128 'claim' => 'accept_copy_advice',
129 'isarray' => false
131 'accept_media_types' => [
132 'suffix' => 'dl',
133 'group' => 'deep_linking_settings',
134 'claim' => 'accept_media_types',
135 'isarray' => true
137 'accept_multiple' => [
138 'suffix' => 'dl',
139 'group' => 'deep_linking_settings',
140 'claim' => 'accept_multiple',
141 'isarray' => false
143 'accept_presentation_document_targets' => [
144 'suffix' => 'dl',
145 'group' => 'deep_linking_settings',
146 'claim' => 'accept_presentation_document_targets',
147 'isarray' => true
149 'accept_types' => [
150 'suffix' => 'dl',
151 'group' => 'deep_linking_settings',
152 'claim' => 'accept_types',
153 'isarray' => true
155 'accept_unsigned' => [
156 'suffix' => 'dl',
157 'group' => 'deep_linking_settings',
158 'claim' => 'accept_unsigned',
159 'isarray' => false
161 'auto_create' => [
162 'suffix' => 'dl',
163 'group' => 'deep_linking_settings',
164 'claim' => 'auto_create',
165 'isarray' => false
167 'can_confirm' => [
168 'suffix' => 'dl',
169 'group' => 'deep_linking_settings',
170 'claim' => 'can_confirm',
171 'isarray' => false
173 'content_item_return_url' => [
174 'suffix' => 'dl',
175 'group' => 'deep_linking_settings',
176 'claim' => 'deep_link_return_url',
177 'isarray' => false
179 'content_items' => [
180 'suffix' => 'dl',
181 'group' => '',
182 'claim' => 'content_items',
183 'isarray' => true
185 'data' => [
186 'suffix' => 'dl',
187 'group' => 'deep_linking_settings',
188 'claim' => 'data',
189 'isarray' => false
191 'text' => [
192 'suffix' => 'dl',
193 'group' => 'deep_linking_settings',
194 'claim' => 'text',
195 'isarray' => false
197 'title' => [
198 'suffix' => 'dl',
199 'group' => 'deep_linking_settings',
200 'claim' => 'title',
201 'isarray' => false
203 'lti_msg' => [
204 'suffix' => 'dl',
205 'group' => '',
206 'claim' => 'msg',
207 'isarray' => false
209 'lti_log' => [
210 'suffix' => 'dl',
211 'group' => '',
212 'claim' => 'log',
213 'isarray' => false
215 'lti_errormsg' => [
216 'suffix' => 'dl',
217 'group' => '',
218 'claim' => 'errormsg',
219 'isarray' => false
221 'lti_errorlog' => [
222 'suffix' => 'dl',
223 'group' => '',
224 'claim' => 'errorlog',
225 'isarray' => false
227 'context_id' => [
228 'suffix' => '',
229 'group' => 'context',
230 'claim' => 'id',
231 'isarray' => false
233 'context_label' => [
234 'suffix' => '',
235 'group' => 'context',
236 'claim' => 'label',
237 'isarray' => false
239 'context_title' => [
240 'suffix' => '',
241 'group' => 'context',
242 'claim' => 'title',
243 'isarray' => false
245 'context_type' => [
246 'suffix' => '',
247 'group' => 'context',
248 'claim' => 'type',
249 'isarray' => true
251 'lis_course_offering_sourcedid' => [
252 'suffix' => '',
253 'group' => 'lis',
254 'claim' => 'course_offering_sourcedid',
255 'isarray' => false
257 'lis_course_section_sourcedid' => [
258 'suffix' => '',
259 'group' => 'lis',
260 'claim' => 'course_section_sourcedid',
261 'isarray' => false
263 'launch_presentation_css_url' => [
264 'suffix' => '',
265 'group' => 'launch_presentation',
266 'claim' => 'css_url',
267 'isarray' => false
269 'launch_presentation_document_target' => [
270 'suffix' => '',
271 'group' => 'launch_presentation',
272 'claim' => 'document_target',
273 'isarray' => false
275 'launch_presentation_height' => [
276 'suffix' => '',
277 'group' => 'launch_presentation',
278 'claim' => 'height',
279 'isarray' => false
281 'launch_presentation_locale' => [
282 'suffix' => '',
283 'group' => 'launch_presentation',
284 'claim' => 'locale',
285 'isarray' => false
287 'launch_presentation_return_url' => [
288 'suffix' => '',
289 'group' => 'launch_presentation',
290 'claim' => 'return_url',
291 'isarray' => false
293 'launch_presentation_width' => [
294 'suffix' => '',
295 'group' => 'launch_presentation',
296 'claim' => 'width',
297 'isarray' => false
299 'lis_person_contact_email_primary' => [
300 'suffix' => '',
301 'group' => null,
302 'claim' => 'email',
303 'isarray' => false
305 'lis_person_name_family' => [
306 'suffix' => '',
307 'group' => null,
308 'claim' => 'family_name',
309 'isarray' => false
311 'lis_person_name_full' => [
312 'suffix' => '',
313 'group' => null,
314 'claim' => 'name',
315 'isarray' => false
317 'lis_person_name_given' => [
318 'suffix' => '',
319 'group' => null,
320 'claim' => 'given_name',
321 'isarray' => false
323 'lis_person_sourcedid' => [
324 'suffix' => '',
325 'group' => 'lis',
326 'claim' => 'person_sourcedid',
327 'isarray' => false
329 'user_id' => [
330 'suffix' => '',
331 'group' => null,
332 'claim' => 'sub',
333 'isarray' => false
335 'user_image' => [
336 'suffix' => '',
337 'group' => null,
338 'claim' => 'picture',
339 'isarray' => false
341 'roles' => [
342 'suffix' => '',
343 'group' => '',
344 'claim' => 'roles',
345 'isarray' => true
347 'role_scope_mentor' => [
348 'suffix' => '',
349 'group' => '',
350 'claim' => 'role_scope_mentor',
351 'isarray' => false
353 'deployment_id' => [
354 'suffix' => '',
355 'group' => '',
356 'claim' => 'deployment_id',
357 'isarray' => false
359 'lti_message_type' => [
360 'suffix' => '',
361 'group' => '',
362 'claim' => 'message_type',
363 'isarray' => false
365 'lti_version' => [
366 'suffix' => '',
367 'group' => '',
368 'claim' => 'version',
369 'isarray' => false
371 'resource_link_description' => [
372 'suffix' => '',
373 'group' => 'resource_link',
374 'claim' => 'description',
375 'isarray' => false
377 'resource_link_id' => [
378 'suffix' => '',
379 'group' => 'resource_link',
380 'claim' => 'id',
381 'isarray' => false
383 'resource_link_title' => [
384 'suffix' => '',
385 'group' => 'resource_link',
386 'claim' => 'title',
387 'isarray' => false
389 'tool_consumer_info_product_family_code' => [
390 'suffix' => '',
391 'group' => 'tool_platform',
392 'claim' => 'family_code',
393 'isarray' => false
395 'tool_consumer_info_version' => [
396 'suffix' => '',
397 'group' => 'tool_platform',
398 'claim' => 'version',
399 'isarray' => false
401 'tool_consumer_instance_contact_email' => [
402 'suffix' => '',
403 'group' => 'tool_platform',
404 'claim' => 'contact_email',
405 'isarray' => false
407 'tool_consumer_instance_description' => [
408 'suffix' => '',
409 'group' => 'tool_platform',
410 'claim' => 'description',
411 'isarray' => false
413 'tool_consumer_instance_guid' => [
414 'suffix' => '',
415 'group' => 'tool_platform',
416 'claim' => 'guid',
417 'isarray' => false
419 'tool_consumer_instance_name' => [
420 'suffix' => '',
421 'group' => 'tool_platform',
422 'claim' => 'name',
423 'isarray' => false
425 'tool_consumer_instance_url' => [
426 'suffix' => '',
427 'group' => 'tool_platform',
428 'claim' => 'url',
429 'isarray' => false
431 'custom_context_memberships_url' => [
432 'suffix' => 'nrps',
433 'group' => 'namesroleservice',
434 'claim' => 'context_memberships_url',
435 'isarray' => false
437 'custom_context_memberships_versions' => [
438 'suffix' => 'nrps',
439 'group' => 'namesroleservice',
440 'claim' => 'service_versions',
441 'isarray' => true
443 'custom_gradebookservices_scope' => [
444 'suffix' => 'ags',
445 'group' => 'endpoint',
446 'claim' => 'scope',
447 'isarray' => true
449 'custom_lineitems_url' => [
450 'suffix' => 'ags',
451 'group' => 'endpoint',
452 'claim' => 'lineitems',
453 'isarray' => false
455 'custom_lineitem_url' => [
456 'suffix' => 'ags',
457 'group' => 'endpoint',
458 'claim' => 'lineitem',
459 'isarray' => false
461 'custom_results_url' => [
462 'suffix' => 'ags',
463 'group' => 'endpoint',
464 'claim' => 'results',
465 'isarray' => false
467 'custom_result_url' => [
468 'suffix' => 'ags',
469 'group' => 'endpoint',
470 'claim' => 'result',
471 'isarray' => false
473 'custom_scores_url' => [
474 'suffix' => 'ags',
475 'group' => 'endpoint',
476 'claim' => 'scores',
477 'isarray' => false
479 'custom_score_url' => [
480 'suffix' => 'ags',
481 'group' => 'endpoint',
482 'claim' => 'score',
483 'isarray' => false
485 'lis_outcome_service_url' => [
486 'suffix' => 'bos',
487 'group' => 'basicoutcomesservice',
488 'claim' => 'lis_outcome_service_url',
489 'isarray' => false
491 'lis_result_sourcedid' => [
492 'suffix' => 'bos',
493 'group' => 'basicoutcomesservice',
494 'claim' => 'lis_result_sourcedid',
495 'isarray' => false
501 * Return the type of the instance, using domain matching if no explicit type is set.
503 * @param object $instance the external tool activity settings
504 * @return object|null
505 * @since Moodle 3.9
507 function lti_get_instance_type(object $instance) : ?object {
508 if (empty($instance->typeid)) {
509 if (!$tool = lti_get_tool_by_url_match($instance->toolurl, $instance->course)) {
510 $tool = lti_get_tool_by_url_match($instance->securetoolurl, $instance->course);
512 return $tool;
514 return lti_get_type($instance->typeid);
518 * Return the launch data required for opening the external tool.
520 * @param stdClass $instance the external tool activity settings
521 * @param string $nonce the nonce value to use (applies to LTI 1.3 only)
522 * @return array the endpoint URL and parameters (including the signature)
523 * @since Moodle 3.0
525 function lti_get_launch_data($instance, $nonce = '') {
526 global $PAGE, $CFG, $USER;
528 $tool = lti_get_instance_type($instance);
529 if ($tool) {
530 $typeid = $tool->id;
531 $ltiversion = $tool->ltiversion;
532 } else {
533 $typeid = null;
534 $ltiversion = LTI_VERSION_1;
537 if ($typeid) {
538 $typeconfig = lti_get_type_config($typeid);
539 } else {
540 // There is no admin configuration for this tool. Use configuration in the lti instance record plus some defaults.
541 $typeconfig = (array)$instance;
543 $typeconfig['sendname'] = $instance->instructorchoicesendname;
544 $typeconfig['sendemailaddr'] = $instance->instructorchoicesendemailaddr;
545 $typeconfig['customparameters'] = $instance->instructorcustomparameters;
546 $typeconfig['acceptgrades'] = $instance->instructorchoiceacceptgrades;
547 $typeconfig['allowroster'] = $instance->instructorchoiceallowroster;
548 $typeconfig['forcessl'] = '0';
551 if (isset($tool->toolproxyid)) {
552 $toolproxy = lti_get_tool_proxy($tool->toolproxyid);
553 $key = $toolproxy->guid;
554 $secret = $toolproxy->secret;
555 } else {
556 $toolproxy = null;
557 if (!empty($instance->resourcekey)) {
558 $key = $instance->resourcekey;
559 } else if ($ltiversion === LTI_VERSION_1P3) {
560 $key = $tool->clientid;
561 } else if (!empty($typeconfig['resourcekey'])) {
562 $key = $typeconfig['resourcekey'];
563 } else {
564 $key = '';
566 if (!empty($instance->password)) {
567 $secret = $instance->password;
568 } else if (!empty($typeconfig['password'])) {
569 $secret = $typeconfig['password'];
570 } else {
571 $secret = '';
575 $endpoint = !empty($instance->toolurl) ? $instance->toolurl : $typeconfig['toolurl'];
576 $endpoint = trim($endpoint);
578 // If the current request is using SSL and a secure tool URL is specified, use it.
579 if (lti_request_is_using_ssl() && !empty($instance->securetoolurl)) {
580 $endpoint = trim($instance->securetoolurl);
583 // If SSL is forced, use the secure tool url if specified. Otherwise, make sure https is on the normal launch URL.
584 if (isset($typeconfig['forcessl']) && ($typeconfig['forcessl'] == '1')) {
585 if (!empty($instance->securetoolurl)) {
586 $endpoint = trim($instance->securetoolurl);
589 $endpoint = lti_ensure_url_is_https($endpoint);
590 } else {
591 if (!strstr($endpoint, '://')) {
592 $endpoint = 'http://' . $endpoint;
596 $orgid = lti_get_organizationid($typeconfig);
598 $course = $PAGE->course;
599 $islti2 = isset($tool->toolproxyid);
600 $allparams = lti_build_request($instance, $typeconfig, $course, $typeid, $islti2);
601 if ($islti2) {
602 $requestparams = lti_build_request_lti2($tool, $allparams);
603 } else {
604 $requestparams = $allparams;
606 $requestparams = array_merge($requestparams, lti_build_standard_message($instance, $orgid, $ltiversion));
607 $customstr = '';
608 if (isset($typeconfig['customparameters'])) {
609 $customstr = $typeconfig['customparameters'];
611 $requestparams = array_merge($requestparams, lti_build_custom_parameters($toolproxy, $tool, $instance, $allparams, $customstr,
612 $instance->instructorcustomparameters, $islti2));
614 $launchcontainer = lti_get_launch_container($instance, $typeconfig);
615 $returnurlparams = array('course' => $course->id,
616 'launch_container' => $launchcontainer,
617 'instanceid' => $instance->id,
618 'sesskey' => sesskey());
620 // Add the return URL. We send the launch container along to help us avoid frames-within-frames when the user returns.
621 $url = new \moodle_url('/mod/lti/return.php', $returnurlparams);
622 $returnurl = $url->out(false);
624 if (isset($typeconfig['forcessl']) && ($typeconfig['forcessl'] == '1')) {
625 $returnurl = lti_ensure_url_is_https($returnurl);
628 $target = '';
629 switch($launchcontainer) {
630 case LTI_LAUNCH_CONTAINER_EMBED:
631 case LTI_LAUNCH_CONTAINER_EMBED_NO_BLOCKS:
632 $target = 'iframe';
633 break;
634 case LTI_LAUNCH_CONTAINER_REPLACE_MOODLE_WINDOW:
635 $target = 'frame';
636 break;
637 case LTI_LAUNCH_CONTAINER_WINDOW:
638 $target = 'window';
639 break;
641 if (!empty($target)) {
642 $requestparams['launch_presentation_document_target'] = $target;
645 $requestparams['launch_presentation_return_url'] = $returnurl;
647 // Add the parameters configured by the LTI services.
648 if ($typeid && !$islti2) {
649 $services = lti_get_services();
650 foreach ($services as $service) {
651 $serviceparameters = $service->get_launch_parameters('basic-lti-launch-request',
652 $course->id, $USER->id , $typeid, $instance->id);
653 foreach ($serviceparameters as $paramkey => $paramvalue) {
654 $requestparams['custom_' . $paramkey] = lti_parse_custom_parameter($toolproxy, $tool, $requestparams, $paramvalue,
655 $islti2);
660 // Allow request params to be updated by sub-plugins.
661 $plugins = core_component::get_plugin_list('ltisource');
662 foreach (array_keys($plugins) as $plugin) {
663 $pluginparams = component_callback('ltisource_'.$plugin, 'before_launch',
664 array($instance, $endpoint, $requestparams), array());
666 if (!empty($pluginparams) && is_array($pluginparams)) {
667 $requestparams = array_merge($requestparams, $pluginparams);
671 if ((!empty($key) && !empty($secret)) || ($ltiversion === LTI_VERSION_1P3)) {
672 if ($ltiversion !== LTI_VERSION_1P3) {
673 $parms = lti_sign_parameters($requestparams, $endpoint, 'POST', $key, $secret);
674 } else {
675 $parms = lti_sign_jwt($requestparams, $endpoint, $key, $typeid, $nonce);
678 $endpointurl = new \moodle_url($endpoint);
679 $endpointparams = $endpointurl->params();
681 // Strip querystring params in endpoint url from $parms to avoid duplication.
682 if (!empty($endpointparams) && !empty($parms)) {
683 foreach (array_keys($endpointparams) as $paramname) {
684 if (isset($parms[$paramname])) {
685 unset($parms[$paramname]);
690 } else {
691 // If no key and secret, do the launch unsigned.
692 $returnurlparams['unsigned'] = '1';
693 $parms = $requestparams;
696 return array($endpoint, $parms);
700 * Launch an external tool activity.
702 * @param stdClass $instance the external tool activity settings
703 * @return string The HTML code containing the javascript code for the launch
705 function lti_launch_tool($instance) {
707 list($endpoint, $parms) = lti_get_launch_data($instance);
708 $debuglaunch = ( $instance->debuglaunch == 1 );
710 $content = lti_post_launch_html($parms, $endpoint, $debuglaunch);
712 echo $content;
716 * Prepares an LTI registration request message
718 * @param object $toolproxy Tool Proxy instance object
720 function lti_register($toolproxy) {
721 $endpoint = $toolproxy->regurl;
723 // Change the status to pending.
724 $toolproxy->state = LTI_TOOL_PROXY_STATE_PENDING;
725 lti_update_tool_proxy($toolproxy);
727 $requestparams = lti_build_registration_request($toolproxy);
729 $content = lti_post_launch_html($requestparams, $endpoint, false);
731 echo $content;
736 * Gets the parameters for the regirstration request
738 * @param object $toolproxy Tool Proxy instance object
739 * @return array Registration request parameters
741 function lti_build_registration_request($toolproxy) {
742 $key = $toolproxy->guid;
743 $secret = $toolproxy->secret;
745 $requestparams = array();
746 $requestparams['lti_message_type'] = 'ToolProxyRegistrationRequest';
747 $requestparams['lti_version'] = 'LTI-2p0';
748 $requestparams['reg_key'] = $key;
749 $requestparams['reg_password'] = $secret;
750 $requestparams['reg_url'] = $toolproxy->regurl;
752 // Add the profile URL.
753 $profileservice = lti_get_service_by_name('profile');
754 $profileservice->set_tool_proxy($toolproxy);
755 $requestparams['tc_profile_url'] = $profileservice->parse_value('$ToolConsumerProfile.url');
757 // Add the return URL.
758 $returnurlparams = array('id' => $toolproxy->id, 'sesskey' => sesskey());
759 $url = new \moodle_url('/mod/lti/externalregistrationreturn.php', $returnurlparams);
760 $returnurl = $url->out(false);
762 $requestparams['launch_presentation_return_url'] = $returnurl;
764 return $requestparams;
768 /** get Organization ID using default if no value provided
769 * @param object $typeconfig
770 * @return string
772 function lti_get_organizationid($typeconfig) {
773 global $CFG;
774 // Default the organizationid if not specified.
775 if (empty($typeconfig['organizationid'])) {
776 if (($typeconfig['organizationid_default'] ?? LTI_DEFAULT_ORGID_SITEHOST) == LTI_DEFAULT_ORGID_SITEHOST) {
777 $urlparts = parse_url($CFG->wwwroot);
778 return $urlparts['host'];
779 } else {
780 return md5(get_site_identifier());
783 return $typeconfig['organizationid'];
787 * Build source ID
789 * @param int $instanceid
790 * @param int $userid
791 * @param string $servicesalt
792 * @param null|int $typeid
793 * @param null|int $launchid
794 * @return stdClass
796 function lti_build_sourcedid($instanceid, $userid, $servicesalt, $typeid = null, $launchid = null) {
797 $data = new \stdClass();
799 $data->instanceid = $instanceid;
800 $data->userid = $userid;
801 $data->typeid = $typeid;
802 if (!empty($launchid)) {
803 $data->launchid = $launchid;
804 } else {
805 $data->launchid = mt_rand();
808 $json = json_encode($data);
810 $hash = hash('sha256', $json . $servicesalt, false);
812 $container = new \stdClass();
813 $container->data = $data;
814 $container->hash = $hash;
816 return $container;
820 * This function builds the request that must be sent to the tool producer
822 * @param object $instance Basic LTI instance object
823 * @param array $typeconfig Basic LTI tool configuration
824 * @param object $course Course object
825 * @param int|null $typeid Basic LTI tool ID
826 * @param boolean $islti2 True if an LTI 2 tool is being launched
828 * @return array Request details
830 function lti_build_request($instance, $typeconfig, $course, $typeid = null, $islti2 = false) {
831 global $USER, $CFG;
833 if (empty($instance->cmid)) {
834 $instance->cmid = 0;
837 $role = lti_get_ims_role($USER, $instance->cmid, $instance->course, $islti2);
839 $requestparams = array(
840 'user_id' => $USER->id,
841 'lis_person_sourcedid' => $USER->idnumber,
842 'roles' => $role,
843 'context_id' => $course->id,
844 'context_label' => trim(html_to_text($course->shortname, 0)),
845 'context_title' => trim(html_to_text($course->fullname, 0)),
847 if (!empty($instance->name)) {
848 $requestparams['resource_link_title'] = trim(html_to_text($instance->name, 0));
850 if (!empty($instance->cmid)) {
851 $intro = format_module_intro('lti', $instance, $instance->cmid);
852 $intro = trim(html_to_text($intro, 0, false));
854 // This may look weird, but this is required for new lines
855 // so we generate the same OAuth signature as the tool provider.
856 $intro = str_replace("\n", "\r\n", $intro);
857 $requestparams['resource_link_description'] = $intro;
859 if (!empty($instance->id)) {
860 $requestparams['resource_link_id'] = $instance->id;
862 if (!empty($instance->resource_link_id)) {
863 $requestparams['resource_link_id'] = $instance->resource_link_id;
865 if ($course->format == 'site') {
866 $requestparams['context_type'] = 'Group';
867 } else {
868 $requestparams['context_type'] = 'CourseSection';
869 $requestparams['lis_course_section_sourcedid'] = $course->idnumber;
872 if (!empty($instance->id) && !empty($instance->servicesalt) && ($islti2 ||
873 $typeconfig['acceptgrades'] == LTI_SETTING_ALWAYS ||
874 ($typeconfig['acceptgrades'] == LTI_SETTING_DELEGATE && $instance->instructorchoiceacceptgrades == LTI_SETTING_ALWAYS))
876 $placementsecret = $instance->servicesalt;
877 $sourcedid = json_encode(lti_build_sourcedid($instance->id, $USER->id, $placementsecret, $typeid));
878 $requestparams['lis_result_sourcedid'] = $sourcedid;
880 // Add outcome service URL.
881 $serviceurl = new \moodle_url('/mod/lti/service.php');
882 $serviceurl = $serviceurl->out();
884 $forcessl = false;
885 if (!empty($CFG->mod_lti_forcessl)) {
886 $forcessl = true;
889 if ((isset($typeconfig['forcessl']) && ($typeconfig['forcessl'] == '1')) or $forcessl) {
890 $serviceurl = lti_ensure_url_is_https($serviceurl);
893 $requestparams['lis_outcome_service_url'] = $serviceurl;
896 // Send user's name and email data if appropriate.
897 if ($islti2 || $typeconfig['sendname'] == LTI_SETTING_ALWAYS ||
898 ($typeconfig['sendname'] == LTI_SETTING_DELEGATE && isset($instance->instructorchoicesendname)
899 && $instance->instructorchoicesendname == LTI_SETTING_ALWAYS)
901 $requestparams['lis_person_name_given'] = $USER->firstname;
902 $requestparams['lis_person_name_family'] = $USER->lastname;
903 $requestparams['lis_person_name_full'] = fullname($USER);
904 $requestparams['ext_user_username'] = $USER->username;
907 if ($islti2 || $typeconfig['sendemailaddr'] == LTI_SETTING_ALWAYS ||
908 ($typeconfig['sendemailaddr'] == LTI_SETTING_DELEGATE && isset($instance->instructorchoicesendemailaddr)
909 && $instance->instructorchoicesendemailaddr == LTI_SETTING_ALWAYS)
911 $requestparams['lis_person_contact_email_primary'] = $USER->email;
914 return $requestparams;
918 * This function builds the request that must be sent to an LTI 2 tool provider
920 * @param object $tool Basic LTI tool object
921 * @param array $params Custom launch parameters
923 * @return array Request details
925 function lti_build_request_lti2($tool, $params) {
927 $requestparams = array();
929 $capabilities = lti_get_capabilities();
930 $enabledcapabilities = explode("\n", $tool->enabledcapability);
931 foreach ($enabledcapabilities as $capability) {
932 if (array_key_exists($capability, $capabilities)) {
933 $val = $capabilities[$capability];
934 if ($val && (substr($val, 0, 1) != '$')) {
935 if (isset($params[$val])) {
936 $requestparams[$capabilities[$capability]] = $params[$capabilities[$capability]];
942 return $requestparams;
947 * This function builds the standard parameters for an LTI 1 or 2 request that must be sent to the tool producer
949 * @param stdClass $instance Basic LTI instance object
950 * @param string $orgid Organisation ID
951 * @param boolean $islti2 True if an LTI 2 tool is being launched
952 * @param string $messagetype The request message type. Defaults to basic-lti-launch-request if empty.
954 * @return array Request details
955 * @deprecated since Moodle 3.7 MDL-62599 - please do not use this function any more.
956 * @see lti_build_standard_message()
958 function lti_build_standard_request($instance, $orgid, $islti2, $messagetype = 'basic-lti-launch-request') {
959 if (!$islti2) {
960 $ltiversion = LTI_VERSION_1;
961 } else {
962 $ltiversion = LTI_VERSION_2;
964 return lti_build_standard_message($instance, $orgid, $ltiversion, $messagetype);
968 * This function builds the standard parameters for an LTI message that must be sent to the tool producer
970 * @param stdClass $instance Basic LTI instance object
971 * @param string $orgid Organisation ID
972 * @param boolean $ltiversion LTI version to be used for tool messages
973 * @param string $messagetype The request message type. Defaults to basic-lti-launch-request if empty.
975 * @return array Message parameters
977 function lti_build_standard_message($instance, $orgid, $ltiversion, $messagetype = 'basic-lti-launch-request') {
978 global $CFG;
980 $requestparams = array();
982 if ($instance) {
983 $requestparams['resource_link_id'] = $instance->id;
984 if (property_exists($instance, 'resource_link_id') and !empty($instance->resource_link_id)) {
985 $requestparams['resource_link_id'] = $instance->resource_link_id;
989 $requestparams['launch_presentation_locale'] = current_language();
991 // Make sure we let the tool know what LMS they are being called from.
992 $requestparams['ext_lms'] = 'moodle-2';
993 $requestparams['tool_consumer_info_product_family_code'] = 'moodle';
994 $requestparams['tool_consumer_info_version'] = strval($CFG->version);
996 // Add oauth_callback to be compliant with the 1.0A spec.
997 $requestparams['oauth_callback'] = 'about:blank';
999 $requestparams['lti_version'] = $ltiversion;
1000 $requestparams['lti_message_type'] = $messagetype;
1002 if ($orgid) {
1003 $requestparams["tool_consumer_instance_guid"] = $orgid;
1005 if (!empty($CFG->mod_lti_institution_name)) {
1006 $requestparams['tool_consumer_instance_name'] = trim(html_to_text($CFG->mod_lti_institution_name, 0));
1007 } else {
1008 $requestparams['tool_consumer_instance_name'] = get_site()->shortname;
1010 $requestparams['tool_consumer_instance_description'] = trim(html_to_text(get_site()->fullname, 0));
1012 return $requestparams;
1016 * This function builds the custom parameters
1018 * @param object $toolproxy Tool proxy instance object
1019 * @param object $tool Tool instance object
1020 * @param object $instance Tool placement instance object
1021 * @param array $params LTI launch parameters
1022 * @param string $customstr Custom parameters defined for tool
1023 * @param string $instructorcustomstr Custom parameters defined for this placement
1024 * @param boolean $islti2 True if an LTI 2 tool is being launched
1026 * @return array Custom parameters
1028 function lti_build_custom_parameters($toolproxy, $tool, $instance, $params, $customstr, $instructorcustomstr, $islti2) {
1030 // Concatenate the custom parameters from the administrator and the instructor
1031 // Instructor parameters are only taken into consideration if the administrator
1032 // has given permission.
1033 $custom = array();
1034 if ($customstr) {
1035 $custom = lti_split_custom_parameters($toolproxy, $tool, $params, $customstr, $islti2);
1037 if ($instructorcustomstr) {
1038 $custom = array_merge(lti_split_custom_parameters($toolproxy, $tool, $params,
1039 $instructorcustomstr, $islti2), $custom);
1041 if ($islti2) {
1042 $custom = array_merge(lti_split_custom_parameters($toolproxy, $tool, $params,
1043 $tool->parameter, true), $custom);
1044 $settings = lti_get_tool_settings($tool->toolproxyid);
1045 $custom = array_merge($custom, lti_get_custom_parameters($toolproxy, $tool, $params, $settings));
1046 if (!empty($instance->course)) {
1047 $settings = lti_get_tool_settings($tool->toolproxyid, $instance->course);
1048 $custom = array_merge($custom, lti_get_custom_parameters($toolproxy, $tool, $params, $settings));
1049 if (!empty($instance->id)) {
1050 $settings = lti_get_tool_settings($tool->toolproxyid, $instance->course, $instance->id);
1051 $custom = array_merge($custom, lti_get_custom_parameters($toolproxy, $tool, $params, $settings));
1056 return $custom;
1060 * Builds a standard LTI Content-Item selection request.
1062 * @param int $id The tool type ID.
1063 * @param stdClass $course The course object.
1064 * @param moodle_url $returnurl The return URL in the tool consumer (TC) that the tool provider (TP)
1065 * will use to return the Content-Item message.
1066 * @param string $title The tool's title, if available.
1067 * @param string $text The text to display to represent the content item. This value may be a long description of the content item.
1068 * @param array $mediatypes Array of MIME types types supported by the TC. If empty, the TC will support ltilink by default.
1069 * @param array $presentationtargets Array of ways in which the selected content item(s) can be requested to be opened
1070 * (via the presentationDocumentTarget element for a returned content item).
1071 * If empty, "frame", "iframe", and "window" will be supported by default.
1072 * @param bool $autocreate Indicates whether any content items returned by the TP would be automatically persisted without
1073 * @param bool $multiple Indicates whether the user should be permitted to select more than one item. False by default.
1074 * any option for the user to cancel the operation. False by default.
1075 * @param bool $unsigned Indicates whether the TC is willing to accept an unsigned return message, or not.
1076 * A signed message should always be required when the content item is being created automatically in the
1077 * TC without further interaction from the user. False by default.
1078 * @param bool $canconfirm Flag for can_confirm parameter. False by default.
1079 * @param bool $copyadvice Indicates whether the TC is able and willing to make a local copy of a content item. False by default.
1080 * @param string $nonce
1081 * @return stdClass The object containing the signed request parameters and the URL to the TP's Content-Item selection interface.
1082 * @throws moodle_exception When the LTI tool type does not exist.`
1083 * @throws coding_exception For invalid media type and presentation target parameters.
1085 function lti_build_content_item_selection_request($id, $course, moodle_url $returnurl, $title = '', $text = '', $mediatypes = [],
1086 $presentationtargets = [], $autocreate = false, $multiple = false,
1087 $unsigned = false, $canconfirm = false, $copyadvice = false, $nonce = '') {
1088 global $USER;
1090 $tool = lti_get_type($id);
1091 // Validate parameters.
1092 if (!$tool) {
1093 throw new moodle_exception('errortooltypenotfound', 'mod_lti');
1095 if (!is_array($mediatypes)) {
1096 throw new coding_exception('The list of accepted media types should be in an array');
1098 if (!is_array($presentationtargets)) {
1099 throw new coding_exception('The list of accepted presentation targets should be in an array');
1102 // Check title. If empty, use the tool's name.
1103 if (empty($title)) {
1104 $title = $tool->name;
1107 $typeconfig = lti_get_type_config($id);
1108 $key = '';
1109 $secret = '';
1110 $islti2 = false;
1111 $islti13 = false;
1112 if (isset($tool->toolproxyid)) {
1113 $islti2 = true;
1114 $toolproxy = lti_get_tool_proxy($tool->toolproxyid);
1115 $key = $toolproxy->guid;
1116 $secret = $toolproxy->secret;
1117 } else {
1118 $islti13 = $tool->ltiversion === LTI_VERSION_1P3;
1119 $toolproxy = null;
1120 if ($islti13 && !empty($tool->clientid)) {
1121 $key = $tool->clientid;
1122 } else if (!$islti13 && !empty($typeconfig['resourcekey'])) {
1123 $key = $typeconfig['resourcekey'];
1125 if (!empty($typeconfig['password'])) {
1126 $secret = $typeconfig['password'];
1129 $tool->enabledcapability = '';
1130 if (!empty($typeconfig['enabledcapability_ContentItemSelectionRequest'])) {
1131 $tool->enabledcapability = $typeconfig['enabledcapability_ContentItemSelectionRequest'];
1134 $tool->parameter = '';
1135 if (!empty($typeconfig['parameter_ContentItemSelectionRequest'])) {
1136 $tool->parameter = $typeconfig['parameter_ContentItemSelectionRequest'];
1139 // Set the tool URL.
1140 if (!empty($typeconfig['toolurl_ContentItemSelectionRequest'])) {
1141 $toolurl = new moodle_url($typeconfig['toolurl_ContentItemSelectionRequest']);
1142 } else {
1143 $toolurl = new moodle_url($typeconfig['toolurl']);
1146 // Check if SSL is forced.
1147 if (!empty($typeconfig['forcessl'])) {
1148 // Make sure the tool URL is set to https.
1149 if (strtolower($toolurl->get_scheme()) === 'http') {
1150 $toolurl->set_scheme('https');
1152 // Make sure the return URL is set to https.
1153 if (strtolower($returnurl->get_scheme()) === 'http') {
1154 $returnurl->set_scheme('https');
1157 $toolurlout = $toolurl->out(false);
1159 // Get base request parameters.
1160 $instance = new stdClass();
1161 $instance->course = $course->id;
1162 $requestparams = lti_build_request($instance, $typeconfig, $course, $id, $islti2);
1164 // Get LTI2-specific request parameters and merge to the request parameters if applicable.
1165 if ($islti2) {
1166 $lti2params = lti_build_request_lti2($tool, $requestparams);
1167 $requestparams = array_merge($requestparams, $lti2params);
1170 // Get standard request parameters and merge to the request parameters.
1171 $orgid = lti_get_organizationid($typeconfig);
1172 $standardparams = lti_build_standard_message(null, $orgid, $tool->ltiversion, 'ContentItemSelectionRequest');
1173 $requestparams = array_merge($requestparams, $standardparams);
1175 // Get custom request parameters and merge to the request parameters.
1176 $customstr = '';
1177 if (!empty($typeconfig['customparameters'])) {
1178 $customstr = $typeconfig['customparameters'];
1180 $customparams = lti_build_custom_parameters($toolproxy, $tool, $instance, $requestparams, $customstr, '', $islti2);
1181 $requestparams = array_merge($requestparams, $customparams);
1183 // Add the parameters configured by the LTI services.
1184 if ($id && !$islti2) {
1185 $services = lti_get_services();
1186 foreach ($services as $service) {
1187 $serviceparameters = $service->get_launch_parameters('ContentItemSelectionRequest',
1188 $course->id, $USER->id , $id);
1189 foreach ($serviceparameters as $paramkey => $paramvalue) {
1190 $requestparams['custom_' . $paramkey] = lti_parse_custom_parameter($toolproxy, $tool, $requestparams, $paramvalue,
1191 $islti2);
1196 // Allow request params to be updated by sub-plugins.
1197 $plugins = core_component::get_plugin_list('ltisource');
1198 foreach (array_keys($plugins) as $plugin) {
1199 $pluginparams = component_callback('ltisource_' . $plugin, 'before_launch', [$instance, $toolurlout, $requestparams], []);
1201 if (!empty($pluginparams) && is_array($pluginparams)) {
1202 $requestparams = array_merge($requestparams, $pluginparams);
1206 if (!$islti13) {
1207 // Media types. Set to ltilink by default if empty.
1208 if (empty($mediatypes)) {
1209 $mediatypes = [
1210 'application/vnd.ims.lti.v1.ltilink',
1213 $requestparams['accept_media_types'] = implode(',', $mediatypes);
1214 } else {
1215 // Only LTI links are currently supported.
1216 $requestparams['accept_types'] = 'ltiResourceLink';
1219 // Presentation targets. Supports frame, iframe, window by default if empty.
1220 if (empty($presentationtargets)) {
1221 $presentationtargets = [
1222 'frame',
1223 'iframe',
1224 'window',
1227 $requestparams['accept_presentation_document_targets'] = implode(',', $presentationtargets);
1229 // Other request parameters.
1230 $requestparams['accept_copy_advice'] = $copyadvice === true ? 'true' : 'false';
1231 $requestparams['accept_multiple'] = $multiple === true ? 'true' : 'false';
1232 $requestparams['accept_unsigned'] = $unsigned === true ? 'true' : 'false';
1233 $requestparams['auto_create'] = $autocreate === true ? 'true' : 'false';
1234 $requestparams['can_confirm'] = $canconfirm === true ? 'true' : 'false';
1235 $requestparams['content_item_return_url'] = $returnurl->out(false);
1236 $requestparams['title'] = $title;
1237 $requestparams['text'] = $text;
1238 if (!$islti13) {
1239 $signedparams = lti_sign_parameters($requestparams, $toolurlout, 'POST', $key, $secret);
1240 } else {
1241 $signedparams = lti_sign_jwt($requestparams, $toolurlout, $key, $id, $nonce);
1243 $toolurlparams = $toolurl->params();
1245 // Strip querystring params in endpoint url from $signedparams to avoid duplication.
1246 if (!empty($toolurlparams) && !empty($signedparams)) {
1247 foreach (array_keys($toolurlparams) as $paramname) {
1248 if (isset($signedparams[$paramname])) {
1249 unset($signedparams[$paramname]);
1254 // Check for params that should not be passed. Unset if they are set.
1255 $unwantedparams = [
1256 'resource_link_id',
1257 'resource_link_title',
1258 'resource_link_description',
1259 'launch_presentation_return_url',
1260 'lis_result_sourcedid',
1262 foreach ($unwantedparams as $param) {
1263 if (isset($signedparams[$param])) {
1264 unset($signedparams[$param]);
1268 // Prepare result object.
1269 $result = new stdClass();
1270 $result->params = $signedparams;
1271 $result->url = $toolurlout;
1273 return $result;
1277 * Verifies the OAuth signature of an incoming message.
1279 * @param int $typeid The tool type ID.
1280 * @param string $consumerkey The consumer key.
1281 * @return stdClass Tool type
1282 * @throws moodle_exception
1283 * @throws lti\OAuthException
1285 function lti_verify_oauth_signature($typeid, $consumerkey) {
1286 $tool = lti_get_type($typeid);
1287 // Validate parameters.
1288 if (!$tool) {
1289 throw new moodle_exception('errortooltypenotfound', 'mod_lti');
1291 $typeconfig = lti_get_type_config($typeid);
1293 if (isset($tool->toolproxyid)) {
1294 $toolproxy = lti_get_tool_proxy($tool->toolproxyid);
1295 $key = $toolproxy->guid;
1296 $secret = $toolproxy->secret;
1297 } else {
1298 $toolproxy = null;
1299 if (!empty($typeconfig['resourcekey'])) {
1300 $key = $typeconfig['resourcekey'];
1301 } else {
1302 $key = '';
1304 if (!empty($typeconfig['password'])) {
1305 $secret = $typeconfig['password'];
1306 } else {
1307 $secret = '';
1311 if ($consumerkey !== $key) {
1312 throw new moodle_exception('errorincorrectconsumerkey', 'mod_lti');
1315 $store = new lti\TrivialOAuthDataStore();
1316 $store->add_consumer($key, $secret);
1317 $server = new lti\OAuthServer($store);
1318 $method = new lti\OAuthSignatureMethod_HMAC_SHA1();
1319 $server->add_signature_method($method);
1320 $request = lti\OAuthRequest::from_request();
1321 try {
1322 $server->verify_request($request);
1323 } catch (lti\OAuthException $e) {
1324 throw new lti\OAuthException("OAuth signature failed: " . $e->getMessage());
1327 return $tool;
1331 * Verifies the JWT signature using a JWK keyset.
1333 * @param string $jwtparam JWT parameter value.
1334 * @param string $keyseturl The tool keyseturl.
1335 * @param string $clientid The tool client id.
1337 * @return object The JWT's payload as a PHP object
1338 * @throws moodle_exception
1339 * @throws UnexpectedValueException Provided JWT was invalid
1340 * @throws SignatureInvalidException Provided JWT was invalid because the signature verification failed
1341 * @throws BeforeValidException Provided JWT is trying to be used before it's eligible as defined by 'nbf'
1342 * @throws BeforeValidException Provided JWT is trying to be used before it's been created as defined by 'iat'
1343 * @throws ExpiredException Provided JWT has since expired, as defined by the 'exp' claim
1345 function lti_verify_with_keyset($jwtparam, $keyseturl, $clientid) {
1346 // Attempts to retrieve cached keyset.
1347 $cache = cache::make('mod_lti', 'keyset');
1348 $keyset = $cache->get($clientid);
1350 try {
1351 if (empty($keyset)) {
1352 throw new moodle_exception('errornocachedkeysetfound', 'mod_lti');
1354 $keysetarr = json_decode($keyset, true);
1355 $keys = JWK::parseKeySet($keysetarr);
1356 $jwt = JWT::decode($jwtparam, $keys, ['RS256']);
1357 } catch (Exception $e) {
1358 // Something went wrong, so attempt to update cached keyset and then try again.
1359 $keyset = file_get_contents($keyseturl);
1360 $keysetarr = json_decode($keyset, true);
1361 $keys = JWK::parseKeySet($keysetarr);
1362 $jwt = JWT::decode($jwtparam, $keys, ['RS256']);
1363 // If sucessful, updates the cached keyset.
1364 $cache->set($clientid, $keyset);
1366 return $jwt;
1370 * Verifies the JWT signature of an incoming message.
1372 * @param int $typeid The tool type ID.
1373 * @param string $consumerkey The consumer key.
1374 * @param string $jwtparam JWT parameter value
1376 * @return stdClass Tool type
1377 * @throws moodle_exception
1378 * @throws UnexpectedValueException Provided JWT was invalid
1379 * @throws SignatureInvalidException Provided JWT was invalid because the signature verification failed
1380 * @throws BeforeValidException Provided JWT is trying to be used before it's eligible as defined by 'nbf'
1381 * @throws BeforeValidException Provided JWT is trying to be used before it's been created as defined by 'iat'
1382 * @throws ExpiredException Provided JWT has since expired, as defined by the 'exp' claim
1384 function lti_verify_jwt_signature($typeid, $consumerkey, $jwtparam) {
1385 $tool = lti_get_type($typeid);
1387 // Validate parameters.
1388 if (!$tool) {
1389 throw new moodle_exception('errortooltypenotfound', 'mod_lti');
1391 if (isset($tool->toolproxyid)) {
1392 throw new moodle_exception('JWT security not supported with LTI 2');
1395 $typeconfig = lti_get_type_config($typeid);
1397 $key = $tool->clientid ?? '';
1399 if ($consumerkey !== $key) {
1400 throw new moodle_exception('errorincorrectconsumerkey', 'mod_lti');
1403 if (empty($typeconfig['keytype']) || $typeconfig['keytype'] === LTI_RSA_KEY) {
1404 $publickey = $typeconfig['publickey'] ?? '';
1405 if (empty($publickey)) {
1406 throw new moodle_exception('No public key configured');
1408 // Attemps to verify jwt with RSA key.
1409 JWT::decode($jwtparam, $publickey, ['RS256']);
1410 } else if ($typeconfig['keytype'] === LTI_JWK_KEYSET) {
1411 $keyseturl = $typeconfig['publickeyset'] ?? '';
1412 if (empty($keyseturl)) {
1413 throw new moodle_exception('No public keyset configured');
1415 // Attempts to verify jwt with jwk keyset.
1416 lti_verify_with_keyset($jwtparam, $keyseturl, $tool->clientid);
1417 } else {
1418 throw new moodle_exception('Invalid public key type');
1421 return $tool;
1425 * Processes the tool provider's response to the ContentItemSelectionRequest and builds the configuration data from the
1426 * selected content item. This configuration data can be then used when adding a tool into the course.
1428 * @param int $typeid The tool type ID.
1429 * @param string $messagetype The value for the lti_message_type parameter.
1430 * @param string $ltiversion The value for the lti_version parameter.
1431 * @param string $consumerkey The consumer key.
1432 * @param string $contentitemsjson The JSON string for the content_items parameter.
1433 * @return stdClass The array of module information objects.
1434 * @throws moodle_exception
1435 * @throws lti\OAuthException
1437 function lti_tool_configuration_from_content_item($typeid, $messagetype, $ltiversion, $consumerkey, $contentitemsjson) {
1438 $tool = lti_get_type($typeid);
1439 // Validate parameters.
1440 if (!$tool) {
1441 throw new moodle_exception('errortooltypenotfound', 'mod_lti');
1443 // Check lti_message_type. Show debugging if it's not set to ContentItemSelection.
1444 // No need to throw exceptions for now since lti_message_type does not seem to be used in this processing at the moment.
1445 if ($messagetype !== 'ContentItemSelection') {
1446 debugging("lti_message_type is invalid: {$messagetype}. It should be set to 'ContentItemSelection'.",
1447 DEBUG_DEVELOPER);
1450 // Check LTI versions from our side and the response's side. Show debugging if they don't match.
1451 // No need to throw exceptions for now since LTI version does not seem to be used in this processing at the moment.
1452 $expectedversion = $tool->ltiversion;
1453 $islti2 = ($expectedversion === LTI_VERSION_2);
1454 if ($ltiversion !== $expectedversion) {
1455 debugging("lti_version from response does not match the tool's configuration. Tool: {$expectedversion}," .
1456 " Response: {$ltiversion}", DEBUG_DEVELOPER);
1459 $items = json_decode($contentitemsjson);
1460 if (empty($items)) {
1461 throw new moodle_exception('errorinvaliddata', 'mod_lti', '', $contentitemsjson);
1463 if (!isset($items->{'@graph'}) || !is_array($items->{'@graph'}) || (count($items->{'@graph'}) > 1)) {
1464 throw new moodle_exception('errorinvalidresponseformat', 'mod_lti');
1467 $config = null;
1468 if (!empty($items->{'@graph'})) {
1469 $item = $items->{'@graph'}[0];
1470 $typeconfig = lti_get_type_type_config($tool->id);
1472 $config = new stdClass();
1473 $config->name = '';
1474 if (isset($item->title)) {
1475 $config->name = $item->title;
1477 if (empty($config->name)) {
1478 $config->name = $tool->name;
1480 if (isset($item->text)) {
1481 $config->introeditor = [
1482 'text' => $item->text,
1483 'format' => FORMAT_PLAIN
1486 if (isset($item->icon->{'@id'})) {
1487 $iconurl = new moodle_url($item->icon->{'@id'});
1488 // Assign item's icon URL to secureicon or icon depending on its scheme.
1489 if (strtolower($iconurl->get_scheme()) === 'https') {
1490 $config->secureicon = $iconurl->out(false);
1491 } else {
1492 $config->icon = $iconurl->out(false);
1495 if (isset($item->url)) {
1496 $url = new moodle_url($item->url);
1497 $config->toolurl = $url->out(false);
1498 $config->typeid = 0;
1499 } else {
1500 $config->typeid = $tool->id;
1502 $config->instructorchoiceacceptgrades = LTI_SETTING_NEVER;
1503 if (!$islti2 && isset($typeconfig->lti_acceptgrades)) {
1504 $acceptgrades = $typeconfig->lti_acceptgrades;
1505 if ($acceptgrades == LTI_SETTING_ALWAYS) {
1506 // We create a line item regardless if the definition contains one or not.
1507 $config->instructorchoiceacceptgrades = LTI_SETTING_ALWAYS;
1509 if ($acceptgrades == LTI_SETTING_DELEGATE || $acceptgrades == LTI_SETTING_ALWAYS) {
1510 if (isset($item->lineItem)) {
1511 $lineitem = $item->lineItem;
1512 $config->instructorchoiceacceptgrades = LTI_SETTING_ALWAYS;
1513 $maxscore = 100;
1514 if (isset($lineitem->scoreConstraints)) {
1515 $sc = $lineitem->scoreConstraints;
1516 if (isset($sc->totalMaximum)) {
1517 $maxscore = $sc->totalMaximum;
1518 } else if (isset($sc->normalMaximum)) {
1519 $maxscore = $sc->normalMaximum;
1522 $config->grade_modgrade_point = $maxscore;
1523 $config->lineitemresourceid = '';
1524 $config->lineitemtag = '';
1525 if (isset($lineitem->assignedActivity) && isset($lineitem->assignedActivity->activityId)) {
1526 $config->lineitemresourceid = $lineitem->assignedActivity->activityId ? : '';
1528 if (isset($lineitem->tag)) {
1529 $config->lineitemtag = $lineitem->tag ? : '';
1534 $config->instructorchoicesendname = LTI_SETTING_NEVER;
1535 $config->instructorchoicesendemailaddr = LTI_SETTING_NEVER;
1536 $config->launchcontainer = LTI_LAUNCH_CONTAINER_DEFAULT;
1537 if (isset($item->placementAdvice->presentationDocumentTarget)) {
1538 if ($item->placementAdvice->presentationDocumentTarget === 'window') {
1539 $config->launchcontainer = LTI_LAUNCH_CONTAINER_WINDOW;
1540 } else if ($item->placementAdvice->presentationDocumentTarget === 'frame') {
1541 $config->launchcontainer = LTI_LAUNCH_CONTAINER_EMBED_NO_BLOCKS;
1542 } else if ($item->placementAdvice->presentationDocumentTarget === 'iframe') {
1543 $config->launchcontainer = LTI_LAUNCH_CONTAINER_EMBED;
1546 if (isset($item->custom)) {
1547 $customparameters = [];
1548 foreach ($item->custom as $key => $value) {
1549 $customparameters[] = "{$key}={$value}";
1551 $config->instructorcustomparameters = implode("\n", $customparameters);
1553 $config->contentitemjson = json_encode($item);
1555 return $config;
1559 * Converts the new Deep-Linking format for Content-Items to the old format.
1561 * @param string $param JSON string representing new Deep-Linking format
1562 * @return string JSON representation of content-items
1564 function lti_convert_content_items($param) {
1565 $items = array();
1566 $json = json_decode($param);
1567 if (!empty($json) && is_array($json)) {
1568 foreach ($json as $item) {
1569 if (isset($item->type)) {
1570 $newitem = clone $item;
1571 switch ($item->type) {
1572 case 'ltiResourceLink':
1573 $newitem->{'@type'} = 'LtiLinkItem';
1574 $newitem->mediaType = 'application\/vnd.ims.lti.v1.ltilink';
1575 break;
1576 case 'link':
1577 case 'rich':
1578 $newitem->{'@type'} = 'ContentItem';
1579 $newitem->mediaType = 'text/html';
1580 break;
1581 case 'file':
1582 $newitem->{'@type'} = 'FileItem';
1583 break;
1585 unset($newitem->type);
1586 if (isset($item->html)) {
1587 $newitem->text = $item->html;
1588 unset($newitem->html);
1590 if (isset($item->presentation)) {
1591 $newitem->placementAdvice = new stdClass();
1592 if (isset($item->presentation->documentTarget)) {
1593 $newitem->placementAdvice->presentationDocumentTarget = $item->presentation->documentTarget;
1595 if (isset($item->presentation->windowTarget)) {
1596 $newitem->placementAdvice->windowTarget = $item->presentation->windowTarget;
1598 if (isset($item->presentation->width)) {
1599 $newitem->placementAdvice->dislayWidth = $item->presentation->width;
1601 if (isset($item->presentation->height)) {
1602 $newitem->placementAdvice->dislayHeight = $item->presentation->height;
1604 unset($newitem->presentation);
1606 if (isset($item->icon) && isset($item->icon->url)) {
1607 $newitem->icon->{'@id'} = $item->icon->url;
1608 unset($newitem->icon->url);
1610 if (isset($item->thumbnail) && isset($item->thumbnail->url)) {
1611 $newitem->thumbnail->{'@id'} = $item->thumbnail->url;
1612 unset($newitem->thumbnail->url);
1614 if (isset($item->lineItem)) {
1615 unset($newitem->lineItem);
1616 $newitem->lineItem = new stdClass();
1617 $newitem->lineItem->{'@type'} = 'LineItem';
1618 $newitem->lineItem->reportingMethod = 'http://purl.imsglobal.org/ctx/lis/v2p1/Result#totalScore';
1619 if (isset($item->lineItem->label)) {
1620 $newitem->lineItem->label = $item->lineItem->label;
1622 if (isset($item->lineItem->resourceId)) {
1623 $newitem->lineItem->assignedActivity = new stdClass();
1624 $newitem->lineItem->assignedActivity->activityId = $item->lineItem->resourceId;
1626 if (isset($item->lineItem->tag)) {
1627 $newitem->lineItem->tag = $item->lineItem->tag;
1629 if (isset($item->lineItem->scoreMaximum)) {
1630 $newitem->lineItem->scoreConstraints = new stdClass();
1631 $newitem->lineItem->scoreConstraints->{'@type'} = 'NumericLimits';
1632 $newitem->lineItem->scoreConstraints->totalMaximum = $item->lineItem->scoreMaximum;
1635 $items[] = $newitem;
1640 $newitems = new stdClass();
1641 $newitems->{'@context'} = 'http://purl.imsglobal.org/ctx/lti/v1/ContentItem';
1642 $newitems->{'@graph'} = $items;
1644 return json_encode($newitems);
1647 function lti_get_tool_table($tools, $id) {
1648 global $OUTPUT;
1649 $html = '';
1651 $typename = get_string('typename', 'lti');
1652 $baseurl = get_string('baseurl', 'lti');
1653 $action = get_string('action', 'lti');
1654 $createdon = get_string('createdon', 'lti');
1656 if (!empty($tools)) {
1657 $html .= "
1658 <div id=\"{$id}_tools_container\" style=\"margin-top:.5em;margin-bottom:.5em\">
1659 <table id=\"{$id}_tools\">
1660 <thead>
1661 <tr>
1662 <th>$typename</th>
1663 <th>$baseurl</th>
1664 <th>$createdon</th>
1665 <th>$action</th>
1666 </tr>
1667 </thead>
1670 foreach ($tools as $type) {
1671 $date = userdate($type->timecreated, get_string('strftimedatefullshort', 'core_langconfig'));
1672 $accept = get_string('accept', 'lti');
1673 $update = get_string('update', 'lti');
1674 $delete = get_string('delete', 'lti');
1676 if (empty($type->toolproxyid)) {
1677 $baseurl = new \moodle_url('/mod/lti/typessettings.php', array(
1678 'action' => 'accept',
1679 'id' => $type->id,
1680 'sesskey' => sesskey(),
1681 'tab' => $id
1683 $ref = $type->baseurl;
1684 } else {
1685 $baseurl = new \moodle_url('/mod/lti/toolssettings.php', array(
1686 'action' => 'accept',
1687 'id' => $type->id,
1688 'sesskey' => sesskey(),
1689 'tab' => $id
1691 $ref = $type->tpname;
1694 $accepthtml = $OUTPUT->action_icon($baseurl,
1695 new \pix_icon('t/check', $accept, '', array('class' => 'iconsmall')), null,
1696 array('title' => $accept, 'class' => 'editing_accept'));
1698 $deleteaction = 'delete';
1700 if ($type->state == LTI_TOOL_STATE_CONFIGURED) {
1701 $accepthtml = '';
1704 if ($type->state != LTI_TOOL_STATE_REJECTED) {
1705 $deleteaction = 'reject';
1706 $delete = get_string('reject', 'lti');
1709 $updateurl = clone($baseurl);
1710 $updateurl->param('action', 'update');
1711 $updatehtml = $OUTPUT->action_icon($updateurl,
1712 new \pix_icon('t/edit', $update, '', array('class' => 'iconsmall')), null,
1713 array('title' => $update, 'class' => 'editing_update'));
1715 if (($type->state != LTI_TOOL_STATE_REJECTED) || empty($type->toolproxyid)) {
1716 $deleteurl = clone($baseurl);
1717 $deleteurl->param('action', $deleteaction);
1718 $deletehtml = $OUTPUT->action_icon($deleteurl,
1719 new \pix_icon('t/delete', $delete, '', array('class' => 'iconsmall')), null,
1720 array('title' => $delete, 'class' => 'editing_delete'));
1721 } else {
1722 $deletehtml = '';
1724 $html .= "
1725 <tr>
1726 <td>
1727 {$type->name}
1728 </td>
1729 <td>
1730 {$ref}
1731 </td>
1732 <td>
1733 {$date}
1734 </td>
1735 <td align=\"center\">
1736 {$accepthtml}{$updatehtml}{$deletehtml}
1737 </td>
1738 </tr>
1741 $html .= '</table></div>';
1742 } else {
1743 $html .= get_string('no_' . $id, 'lti');
1746 return $html;
1750 * This function builds the tab for a category of tool proxies
1752 * @param object $toolproxies Tool proxy instance objects
1753 * @param string $id Category ID
1755 * @return string HTML for tab
1757 function lti_get_tool_proxy_table($toolproxies, $id) {
1758 global $OUTPUT;
1760 if (!empty($toolproxies)) {
1761 $typename = get_string('typename', 'lti');
1762 $url = get_string('registrationurl', 'lti');
1763 $action = get_string('action', 'lti');
1764 $createdon = get_string('createdon', 'lti');
1766 $html = <<< EOD
1767 <div id="{$id}_tool_proxies_container" style="margin-top: 0.5em; margin-bottom: 0.5em">
1768 <table id="{$id}_tool_proxies">
1769 <thead>
1770 <tr>
1771 <th>{$typename}</th>
1772 <th>{$url}</th>
1773 <th>{$createdon}</th>
1774 <th>{$action}</th>
1775 </tr>
1776 </thead>
1777 EOD;
1778 foreach ($toolproxies as $toolproxy) {
1779 $date = userdate($toolproxy->timecreated, get_string('strftimedatefullshort', 'core_langconfig'));
1780 $accept = get_string('register', 'lti');
1781 $update = get_string('update', 'lti');
1782 $delete = get_string('delete', 'lti');
1784 $baseurl = new \moodle_url('/mod/lti/registersettings.php', array(
1785 'action' => 'accept',
1786 'id' => $toolproxy->id,
1787 'sesskey' => sesskey(),
1788 'tab' => $id
1791 $registerurl = new \moodle_url('/mod/lti/register.php', array(
1792 'id' => $toolproxy->id,
1793 'sesskey' => sesskey(),
1794 'tab' => 'tool_proxy'
1797 $accepthtml = $OUTPUT->action_icon($registerurl,
1798 new \pix_icon('t/check', $accept, '', array('class' => 'iconsmall')), null,
1799 array('title' => $accept, 'class' => 'editing_accept'));
1801 $deleteaction = 'delete';
1803 if ($toolproxy->state != LTI_TOOL_PROXY_STATE_CONFIGURED) {
1804 $accepthtml = '';
1807 if (($toolproxy->state == LTI_TOOL_PROXY_STATE_CONFIGURED) || ($toolproxy->state == LTI_TOOL_PROXY_STATE_PENDING)) {
1808 $delete = get_string('cancel', 'lti');
1811 $updateurl = clone($baseurl);
1812 $updateurl->param('action', 'update');
1813 $updatehtml = $OUTPUT->action_icon($updateurl,
1814 new \pix_icon('t/edit', $update, '', array('class' => 'iconsmall')), null,
1815 array('title' => $update, 'class' => 'editing_update'));
1817 $deleteurl = clone($baseurl);
1818 $deleteurl->param('action', $deleteaction);
1819 $deletehtml = $OUTPUT->action_icon($deleteurl,
1820 new \pix_icon('t/delete', $delete, '', array('class' => 'iconsmall')), null,
1821 array('title' => $delete, 'class' => 'editing_delete'));
1822 $html .= <<< EOD
1823 <tr>
1824 <td>
1825 {$toolproxy->name}
1826 </td>
1827 <td>
1828 {$toolproxy->regurl}
1829 </td>
1830 <td>
1831 {$date}
1832 </td>
1833 <td align="center">
1834 {$accepthtml}{$updatehtml}{$deletehtml}
1835 </td>
1836 </tr>
1837 EOD;
1839 $html .= '</table></div>';
1840 } else {
1841 $html = get_string('no_' . $id, 'lti');
1844 return $html;
1848 * Extracts the enabled capabilities into an array, including those implicitly declared in a parameter
1850 * @param object $tool Tool instance object
1852 * @return array List of enabled capabilities
1854 function lti_get_enabled_capabilities($tool) {
1855 if (!isset($tool)) {
1856 return array();
1858 if (!empty($tool->enabledcapability)) {
1859 $enabledcapabilities = explode("\n", $tool->enabledcapability);
1860 } else {
1861 $enabledcapabilities = array();
1863 if (!empty($tool->parameter)) {
1864 $paramstr = str_replace("\r\n", "\n", $tool->parameter);
1865 $paramstr = str_replace("\n\r", "\n", $paramstr);
1866 $paramstr = str_replace("\r", "\n", $paramstr);
1867 $params = explode("\n", $paramstr);
1868 foreach ($params as $param) {
1869 $pos = strpos($param, '=');
1870 if (($pos === false) || ($pos < 1)) {
1871 continue;
1873 $value = trim(core_text::substr($param, $pos + 1, strlen($param)));
1874 if (substr($value, 0, 1) == '$') {
1875 $value = substr($value, 1);
1876 if (!in_array($value, $enabledcapabilities)) {
1877 $enabledcapabilities[] = $value;
1882 return $enabledcapabilities;
1886 * Splits the custom parameters field to the various parameters
1888 * @param object $toolproxy Tool proxy instance object
1889 * @param object $tool Tool instance object
1890 * @param array $params LTI launch parameters
1891 * @param string $customstr String containing the parameters
1892 * @param boolean $islti2 True if an LTI 2 tool is being launched
1894 * @return array of custom parameters
1896 function lti_split_custom_parameters($toolproxy, $tool, $params, $customstr, $islti2 = false) {
1897 $customstr = str_replace("\r\n", "\n", $customstr);
1898 $customstr = str_replace("\n\r", "\n", $customstr);
1899 $customstr = str_replace("\r", "\n", $customstr);
1900 $lines = explode("\n", $customstr); // Or should this split on "/[\n;]/"?
1901 $retval = array();
1902 foreach ($lines as $line) {
1903 $pos = strpos($line, '=');
1904 if ( $pos === false || $pos < 1 ) {
1905 continue;
1907 $key = trim(core_text::substr($line, 0, $pos));
1908 $val = trim(core_text::substr($line, $pos + 1, strlen($line)));
1909 $val = lti_parse_custom_parameter($toolproxy, $tool, $params, $val, $islti2);
1910 $key2 = lti_map_keyname($key);
1911 $retval['custom_'.$key2] = $val;
1912 if (($islti2 || ($tool->ltiversion === LTI_VERSION_1P3)) && ($key != $key2)) {
1913 $retval['custom_'.$key] = $val;
1916 return $retval;
1920 * Adds the custom parameters to an array
1922 * @param object $toolproxy Tool proxy instance object
1923 * @param object $tool Tool instance object
1924 * @param array $params LTI launch parameters
1925 * @param array $parameters Array containing the parameters
1927 * @return array Array of custom parameters
1929 function lti_get_custom_parameters($toolproxy, $tool, $params, $parameters) {
1930 $retval = array();
1931 foreach ($parameters as $key => $val) {
1932 $key2 = lti_map_keyname($key);
1933 $val = lti_parse_custom_parameter($toolproxy, $tool, $params, $val, true);
1934 $retval['custom_'.$key2] = $val;
1935 if ($key != $key2) {
1936 $retval['custom_'.$key] = $val;
1939 return $retval;
1943 * Parse a custom parameter to replace any substitution variables
1945 * @param object $toolproxy Tool proxy instance object
1946 * @param object $tool Tool instance object
1947 * @param array $params LTI launch parameters
1948 * @param string $value Custom parameter value
1949 * @param boolean $islti2 True if an LTI 2 tool is being launched
1951 * @return string Parsed value of custom parameter
1953 function lti_parse_custom_parameter($toolproxy, $tool, $params, $value, $islti2) {
1954 // This is required as {${$valarr[0]}->{$valarr[1]}}" may be using the USER or COURSE var.
1955 global $USER, $COURSE;
1957 if ($value) {
1958 if (substr($value, 0, 1) == '\\') {
1959 $value = substr($value, 1);
1960 } else if (substr($value, 0, 1) == '$') {
1961 $value1 = substr($value, 1);
1962 $enabledcapabilities = lti_get_enabled_capabilities($tool);
1963 if (!$islti2 || in_array($value1, $enabledcapabilities)) {
1964 $capabilities = lti_get_capabilities();
1965 if (array_key_exists($value1, $capabilities)) {
1966 $val = $capabilities[$value1];
1967 if ($val) {
1968 if (substr($val, 0, 1) != '$') {
1969 $value = $params[$val];
1970 } else {
1971 $valarr = explode('->', substr($val, 1), 2);
1972 $value = "{${$valarr[0]}->{$valarr[1]}}";
1973 $value = str_replace('<br />' , ' ', $value);
1974 $value = str_replace('<br>' , ' ', $value);
1975 $value = format_string($value);
1977 } else {
1978 $value = lti_calculate_custom_parameter($value1);
1980 } else {
1981 $val = $value;
1982 $services = lti_get_services();
1983 foreach ($services as $service) {
1984 $service->set_tool_proxy($toolproxy);
1985 $service->set_type($tool);
1986 $value = $service->parse_value($val);
1987 if ($val != $value) {
1988 break;
1995 return $value;
1999 * Calculates the value of a custom parameter that has not been specified earlier
2001 * @param string $value Custom parameter value
2003 * @return string Calculated value of custom parameter
2005 function lti_calculate_custom_parameter($value) {
2006 global $USER, $COURSE;
2008 switch ($value) {
2009 case 'Moodle.Person.userGroupIds':
2010 return implode(",", groups_get_user_groups($COURSE->id, $USER->id)[0]);
2012 return null;
2016 * Used for building the names of the different custom parameters
2018 * @param string $key Parameter name
2019 * @param bool $tolower Do we want to convert the key into lower case?
2020 * @return string Processed name
2022 function lti_map_keyname($key, $tolower = true) {
2023 if ($tolower) {
2024 $newkey = '';
2025 $key = core_text::strtolower(trim($key));
2026 foreach (str_split($key) as $ch) {
2027 if ( ($ch >= 'a' && $ch <= 'z') || ($ch >= '0' && $ch <= '9') ) {
2028 $newkey .= $ch;
2029 } else {
2030 $newkey .= '_';
2033 } else {
2034 $newkey = $key;
2036 return $newkey;
2040 * Gets the IMS role string for the specified user and LTI course module.
2042 * @param mixed $user User object or user id
2043 * @param int $cmid The course module id of the LTI activity
2044 * @param int $courseid The course id of the LTI activity
2045 * @param boolean $islti2 True if an LTI 2 tool is being launched
2047 * @return string A role string suitable for passing with an LTI launch
2049 function lti_get_ims_role($user, $cmid, $courseid, $islti2) {
2050 $roles = array();
2052 if (empty($cmid)) {
2053 // If no cmid is passed, check if the user is a teacher in the course
2054 // This allows other modules to programmatically "fake" a launch without
2055 // a real LTI instance.
2056 $context = context_course::instance($courseid);
2058 if (has_capability('moodle/course:manageactivities', $context, $user)) {
2059 array_push($roles, 'Instructor');
2060 } else {
2061 array_push($roles, 'Learner');
2063 } else {
2064 $context = context_module::instance($cmid);
2066 if (has_capability('mod/lti:manage', $context)) {
2067 array_push($roles, 'Instructor');
2068 } else {
2069 array_push($roles, 'Learner');
2073 if (is_siteadmin($user) || has_capability('mod/lti:admin', $context)) {
2074 // Make sure admins do not have the Learner role, then set admin role.
2075 $roles = array_diff($roles, array('Learner'));
2076 if (!$islti2) {
2077 array_push($roles, 'urn:lti:sysrole:ims/lis/Administrator', 'urn:lti:instrole:ims/lis/Administrator');
2078 } else {
2079 array_push($roles, 'http://purl.imsglobal.org/vocab/lis/v2/person#Administrator');
2083 return join(',', $roles);
2087 * Returns configuration details for the tool
2089 * @param int $typeid Basic LTI tool typeid
2091 * @return array Tool Configuration
2093 function lti_get_type_config($typeid) {
2094 global $DB;
2096 $query = "SELECT name, value
2097 FROM {lti_types_config}
2098 WHERE typeid = :typeid1
2099 UNION ALL
2100 SELECT 'toolurl' AS name, baseurl AS value
2101 FROM {lti_types}
2102 WHERE id = :typeid2
2103 UNION ALL
2104 SELECT 'icon' AS name, icon AS value
2105 FROM {lti_types}
2106 WHERE id = :typeid3
2107 UNION ALL
2108 SELECT 'secureicon' AS name, secureicon AS value
2109 FROM {lti_types}
2110 WHERE id = :typeid4";
2112 $typeconfig = array();
2113 $configs = $DB->get_records_sql($query,
2114 array('typeid1' => $typeid, 'typeid2' => $typeid, 'typeid3' => $typeid, 'typeid4' => $typeid));
2116 if (!empty($configs)) {
2117 foreach ($configs as $config) {
2118 $typeconfig[$config->name] = $config->value;
2122 return $typeconfig;
2125 function lti_get_tools_by_url($url, $state, $courseid = null) {
2126 $domain = lti_get_domain_from_url($url);
2128 return lti_get_tools_by_domain($domain, $state, $courseid);
2131 function lti_get_tools_by_domain($domain, $state = null, $courseid = null) {
2132 global $DB, $SITE;
2134 $statefilter = '';
2135 $coursefilter = '';
2137 if ($state) {
2138 $statefilter = 'AND state = :state';
2141 if ($courseid && $courseid != $SITE->id) {
2142 $coursefilter = 'OR course = :courseid';
2145 $query = "SELECT *
2146 FROM {lti_types}
2147 WHERE tooldomain = :tooldomain
2148 AND (course = :siteid $coursefilter)
2149 $statefilter";
2151 return $DB->get_records_sql($query, array(
2152 'courseid' => $courseid,
2153 'siteid' => $SITE->id,
2154 'tooldomain' => $domain,
2155 'state' => $state
2160 * Returns all basicLTI tools configured by the administrator
2162 * @param int $course
2164 * @return array
2166 function lti_filter_get_types($course) {
2167 global $DB;
2169 if (!empty($course)) {
2170 $where = "WHERE t.course = :course";
2171 $params = array('course' => $course);
2172 } else {
2173 $where = '';
2174 $params = array();
2176 $query = "SELECT t.id, t.name, t.baseurl, t.state, t.toolproxyid, t.timecreated, tp.name tpname
2177 FROM {lti_types} t LEFT OUTER JOIN {lti_tool_proxies} tp ON t.toolproxyid = tp.id
2178 {$where}";
2179 return $DB->get_records_sql($query, $params);
2183 * Given an array of tools, filter them based on their state
2185 * @param array $tools An array of lti_types records
2186 * @param int $state One of the LTI_TOOL_STATE_* constants
2187 * @return array
2189 function lti_filter_tool_types(array $tools, $state) {
2190 $return = array();
2191 foreach ($tools as $key => $tool) {
2192 if ($tool->state == $state) {
2193 $return[$key] = $tool;
2196 return $return;
2200 * Returns all lti types visible in this course
2202 * @param int $courseid The id of the course to retieve types for
2203 * @param array $coursevisible options for 'coursevisible' field,
2204 * default [LTI_COURSEVISIBLE_PRECONFIGURED, LTI_COURSEVISIBLE_ACTIVITYCHOOSER]
2205 * @return stdClass[] All the lti types visible in the given course
2207 function lti_get_lti_types_by_course($courseid, $coursevisible = null) {
2208 global $DB, $SITE;
2210 if ($coursevisible === null) {
2211 $coursevisible = [LTI_COURSEVISIBLE_PRECONFIGURED, LTI_COURSEVISIBLE_ACTIVITYCHOOSER];
2214 list($coursevisiblesql, $coursevisparams) = $DB->get_in_or_equal($coursevisible, SQL_PARAMS_NAMED, 'coursevisible');
2215 $courseconds = [];
2216 if (has_capability('mod/lti:addmanualinstance', context_course::instance($courseid))) {
2217 $courseconds[] = "course = :courseid";
2219 if (has_capability('mod/lti:addpreconfiguredinstance', context_course::instance($courseid))) {
2220 $courseconds[] = "course = :siteid";
2222 if (!$courseconds) {
2223 return [];
2225 $coursecond = implode(" OR ", $courseconds);
2226 $query = "SELECT *
2227 FROM {lti_types}
2228 WHERE coursevisible $coursevisiblesql
2229 AND ($coursecond)
2230 AND state = :active";
2232 return $DB->get_records_sql($query,
2233 array('siteid' => $SITE->id, 'courseid' => $courseid, 'active' => LTI_TOOL_STATE_CONFIGURED) + $coursevisparams);
2237 * Returns tool types for lti add instance and edit page
2239 * @return array Array of lti types
2241 function lti_get_types_for_add_instance() {
2242 global $COURSE;
2243 $admintypes = lti_get_lti_types_by_course($COURSE->id);
2245 $types = array();
2246 if (has_capability('mod/lti:addmanualinstance', context_course::instance($COURSE->id))) {
2247 $types[0] = (object)array('name' => get_string('automatic', 'lti'), 'course' => 0, 'toolproxyid' => null);
2250 foreach ($admintypes as $type) {
2251 $types[$type->id] = $type;
2254 return $types;
2258 * Returns a list of configured types in the given course
2260 * @param int $courseid The id of the course to retieve types for
2261 * @param int $sectionreturn section to return to for forming the URLs
2262 * @return array Array of lti types. Each element is object with properties: name, title, icon, help, helplink, link
2264 function lti_get_configured_types($courseid, $sectionreturn = 0) {
2265 global $OUTPUT;
2266 $types = array();
2267 $admintypes = lti_get_lti_types_by_course($courseid, [LTI_COURSEVISIBLE_ACTIVITYCHOOSER]);
2269 foreach ($admintypes as $ltitype) {
2270 $type = new stdClass();
2271 $type->id = $ltitype->id;
2272 $type->modclass = MOD_CLASS_ACTIVITY;
2273 $type->name = 'lti_type_' . $ltitype->id;
2274 // Clean the name. We don't want tags here.
2275 $type->title = clean_param($ltitype->name, PARAM_NOTAGS);
2276 $trimmeddescription = trim($ltitype->description);
2277 if ($trimmeddescription != '') {
2278 // Clean the description. We don't want tags here.
2279 $type->help = clean_param($trimmeddescription, PARAM_NOTAGS);
2280 $type->helplink = get_string('modulename_shortcut_link', 'lti');
2282 if (empty($ltitype->icon)) {
2283 $type->icon = $OUTPUT->pix_icon('icon', '', 'lti', array('class' => 'icon'));
2284 } else {
2285 $type->icon = html_writer::empty_tag('img', array('src' => $ltitype->icon, 'alt' => '', 'class' => 'icon'));
2287 $type->link = new moodle_url('/course/modedit.php', array('add' => 'lti', 'return' => 0, 'course' => $courseid,
2288 'sr' => $sectionreturn, 'typeid' => $ltitype->id));
2289 $types[] = $type;
2291 return $types;
2294 function lti_get_domain_from_url($url) {
2295 $matches = array();
2297 if (preg_match(LTI_URL_DOMAIN_REGEX, $url, $matches)) {
2298 return $matches[1];
2302 function lti_get_tool_by_url_match($url, $courseid = null, $state = LTI_TOOL_STATE_CONFIGURED) {
2303 $possibletools = lti_get_tools_by_url($url, $state, $courseid);
2305 return lti_get_best_tool_by_url($url, $possibletools, $courseid);
2308 function lti_get_url_thumbprint($url) {
2309 // Parse URL requires a schema otherwise everything goes into 'path'. Fixed 5.4.7 or later.
2310 if (preg_match('/https?:\/\//', $url) !== 1) {
2311 $url = 'http://'.$url;
2313 $urlparts = parse_url(strtolower($url));
2314 if (!isset($urlparts['path'])) {
2315 $urlparts['path'] = '';
2318 if (!isset($urlparts['query'])) {
2319 $urlparts['query'] = '';
2322 if (!isset($urlparts['host'])) {
2323 $urlparts['host'] = '';
2326 if (substr($urlparts['host'], 0, 4) === 'www.') {
2327 $urlparts['host'] = substr($urlparts['host'], 4);
2330 $urllower = $urlparts['host'] . '/' . $urlparts['path'];
2332 if ($urlparts['query'] != '') {
2333 $urllower .= '?' . $urlparts['query'];
2336 return $urllower;
2339 function lti_get_best_tool_by_url($url, $tools, $courseid = null) {
2340 if (count($tools) === 0) {
2341 return null;
2344 $urllower = lti_get_url_thumbprint($url);
2346 foreach ($tools as $tool) {
2347 $tool->_matchscore = 0;
2349 $toolbaseurllower = lti_get_url_thumbprint($tool->baseurl);
2351 if ($urllower === $toolbaseurllower) {
2352 // 100 points for exact thumbprint match.
2353 $tool->_matchscore += 100;
2354 } else if (substr($urllower, 0, strlen($toolbaseurllower)) === $toolbaseurllower) {
2355 // 50 points if tool thumbprint starts with the base URL thumbprint.
2356 $tool->_matchscore += 50;
2359 // Prefer course tools over site tools.
2360 if (!empty($courseid)) {
2361 // Minus 10 points for not matching the course id (global tools).
2362 if ($tool->course != $courseid) {
2363 $tool->_matchscore -= 10;
2368 $bestmatch = array_reduce($tools, function($value, $tool) {
2369 if ($tool->_matchscore > $value->_matchscore) {
2370 return $tool;
2371 } else {
2372 return $value;
2375 }, (object)array('_matchscore' => -1));
2377 // None of the tools are suitable for this URL.
2378 if ($bestmatch->_matchscore <= 0) {
2379 return null;
2382 return $bestmatch;
2385 function lti_get_shared_secrets_by_key($key) {
2386 global $DB;
2388 // Look up the shared secret for the specified key in both the types_config table (for configured tools)
2389 // And in the lti resource table for ad-hoc tools.
2390 $lti13 = LTI_VERSION_1P3;
2391 $query = "SELECT " . $DB->sql_compare_text('t2.value', 256) . " AS value
2392 FROM {lti_types_config} t1
2393 JOIN {lti_types_config} t2 ON t1.typeid = t2.typeid
2394 JOIN {lti_types} type ON t2.typeid = type.id
2395 WHERE t1.name = 'resourcekey'
2396 AND " . $DB->sql_compare_text('t1.value', 256) . " = :key1
2397 AND t2.name = 'password'
2398 AND type.state = :configured1
2399 AND type.ltiversion <> :ltiversion
2400 UNION
2401 SELECT tp.secret AS value
2402 FROM {lti_tool_proxies} tp
2403 JOIN {lti_types} t ON tp.id = t.toolproxyid
2404 WHERE tp.guid = :key2
2405 AND t.state = :configured2
2406 UNION
2407 SELECT password AS value
2408 FROM {lti}
2409 WHERE resourcekey = :key3";
2411 $sharedsecrets = $DB->get_records_sql($query, array('configured1' => LTI_TOOL_STATE_CONFIGURED, 'ltiversion' => $lti13,
2412 'configured2' => LTI_TOOL_STATE_CONFIGURED, 'key1' => $key, 'key2' => $key, 'key3' => $key));
2414 $values = array_map(function($item) {
2415 return $item->value;
2416 }, $sharedsecrets);
2418 // There should really only be one shared secret per key. But, we can't prevent
2419 // more than one getting entered. For instance, if the same key is used for two tool providers.
2420 return $values;
2424 * Delete a Basic LTI configuration
2426 * @param int $id Configuration id
2428 function lti_delete_type($id) {
2429 global $DB;
2431 // We should probably just copy the launch URL to the tool instances in this case... using a single query.
2433 $instances = $DB->get_records('lti', array('typeid' => $id));
2434 foreach ($instances as $instance) {
2435 $instance->typeid = 0;
2436 $DB->update_record('lti', $instance);
2439 $DB->delete_records('lti_types', array('id' => $id));
2440 $DB->delete_records('lti_types_config', array('typeid' => $id));
2443 function lti_set_state_for_type($id, $state) {
2444 global $DB;
2446 $DB->update_record('lti_types', (object)array('id' => $id, 'state' => $state));
2450 * Transforms a basic LTI object to an array
2452 * @param object $ltiobject Basic LTI object
2454 * @return array Basic LTI configuration details
2456 function lti_get_config($ltiobject) {
2457 $typeconfig = (array)$ltiobject;
2458 $additionalconfig = lti_get_type_config($ltiobject->typeid);
2459 $typeconfig = array_merge($typeconfig, $additionalconfig);
2460 return $typeconfig;
2465 * Generates some of the tool configuration based on the instance details
2467 * @param int $id
2469 * @return object configuration
2472 function lti_get_type_config_from_instance($id) {
2473 global $DB;
2475 $instance = $DB->get_record('lti', array('id' => $id));
2476 $config = lti_get_config($instance);
2478 $type = new \stdClass();
2479 $type->lti_fix = $id;
2480 if (isset($config['toolurl'])) {
2481 $type->lti_toolurl = $config['toolurl'];
2483 if (isset($config['instructorchoicesendname'])) {
2484 $type->lti_sendname = $config['instructorchoicesendname'];
2486 if (isset($config['instructorchoicesendemailaddr'])) {
2487 $type->lti_sendemailaddr = $config['instructorchoicesendemailaddr'];
2489 if (isset($config['instructorchoiceacceptgrades'])) {
2490 $type->lti_acceptgrades = $config['instructorchoiceacceptgrades'];
2492 if (isset($config['instructorchoiceallowroster'])) {
2493 $type->lti_allowroster = $config['instructorchoiceallowroster'];
2496 if (isset($config['instructorcustomparameters'])) {
2497 $type->lti_allowsetting = $config['instructorcustomparameters'];
2499 return $type;
2503 * Generates some of the tool configuration based on the admin configuration details
2505 * @param int $id
2507 * @return stdClass Configuration details
2509 function lti_get_type_type_config($id) {
2510 global $DB;
2512 $basicltitype = $DB->get_record('lti_types', array('id' => $id));
2513 $config = lti_get_type_config($id);
2515 $type = new \stdClass();
2517 $type->lti_typename = $basicltitype->name;
2519 $type->typeid = $basicltitype->id;
2521 $type->toolproxyid = $basicltitype->toolproxyid;
2523 $type->lti_toolurl = $basicltitype->baseurl;
2525 $type->lti_ltiversion = $basicltitype->ltiversion;
2527 $type->lti_clientid = $basicltitype->clientid;
2528 $type->lti_clientid_disabled = $type->lti_clientid;
2530 $type->lti_description = $basicltitype->description;
2532 $type->lti_parameters = $basicltitype->parameter;
2534 $type->lti_icon = $basicltitype->icon;
2536 $type->lti_secureicon = $basicltitype->secureicon;
2538 if (isset($config['resourcekey'])) {
2539 $type->lti_resourcekey = $config['resourcekey'];
2541 if (isset($config['password'])) {
2542 $type->lti_password = $config['password'];
2544 if (isset($config['publickey'])) {
2545 $type->lti_publickey = $config['publickey'];
2547 if (isset($config['publickeyset'])) {
2548 $type->lti_publickeyset = $config['publickeyset'];
2550 if (isset($config['keytype'])) {
2551 $type->lti_keytype = $config['keytype'];
2553 if (isset($config['initiatelogin'])) {
2554 $type->lti_initiatelogin = $config['initiatelogin'];
2556 if (isset($config['redirectionuris'])) {
2557 $type->lti_redirectionuris = $config['redirectionuris'];
2560 if (isset($config['sendname'])) {
2561 $type->lti_sendname = $config['sendname'];
2563 if (isset($config['instructorchoicesendname'])) {
2564 $type->lti_instructorchoicesendname = $config['instructorchoicesendname'];
2566 if (isset($config['sendemailaddr'])) {
2567 $type->lti_sendemailaddr = $config['sendemailaddr'];
2569 if (isset($config['instructorchoicesendemailaddr'])) {
2570 $type->lti_instructorchoicesendemailaddr = $config['instructorchoicesendemailaddr'];
2572 if (isset($config['acceptgrades'])) {
2573 $type->lti_acceptgrades = $config['acceptgrades'];
2575 if (isset($config['instructorchoiceacceptgrades'])) {
2576 $type->lti_instructorchoiceacceptgrades = $config['instructorchoiceacceptgrades'];
2578 if (isset($config['allowroster'])) {
2579 $type->lti_allowroster = $config['allowroster'];
2581 if (isset($config['instructorchoiceallowroster'])) {
2582 $type->lti_instructorchoiceallowroster = $config['instructorchoiceallowroster'];
2585 if (isset($config['customparameters'])) {
2586 $type->lti_customparameters = $config['customparameters'];
2589 if (isset($config['forcessl'])) {
2590 $type->lti_forcessl = $config['forcessl'];
2593 if (isset($config['organizationid_default'])) {
2594 $type->lti_organizationid_default = $config['organizationid_default'];
2595 } else {
2596 // Tool was configured before this option was available and the default then was host.
2597 $type->lti_organizationid_default = LTI_DEFAULT_ORGID_SITEHOST;
2599 if (isset($config['organizationid'])) {
2600 $type->lti_organizationid = $config['organizationid'];
2602 if (isset($config['organizationurl'])) {
2603 $type->lti_organizationurl = $config['organizationurl'];
2605 if (isset($config['organizationdescr'])) {
2606 $type->lti_organizationdescr = $config['organizationdescr'];
2608 if (isset($config['launchcontainer'])) {
2609 $type->lti_launchcontainer = $config['launchcontainer'];
2612 if (isset($config['coursevisible'])) {
2613 $type->lti_coursevisible = $config['coursevisible'];
2616 if (isset($config['contentitem'])) {
2617 $type->lti_contentitem = $config['contentitem'];
2620 if (isset($config['toolurl_ContentItemSelectionRequest'])) {
2621 $type->lti_toolurl_ContentItemSelectionRequest = $config['toolurl_ContentItemSelectionRequest'];
2624 if (isset($config['debuglaunch'])) {
2625 $type->lti_debuglaunch = $config['debuglaunch'];
2628 if (isset($config['module_class_type'])) {
2629 $type->lti_module_class_type = $config['module_class_type'];
2632 // Get the parameters from the LTI services.
2633 foreach ($config as $name => $value) {
2634 if (strpos($name, 'ltiservice_') === 0) {
2635 $type->{$name} = $config[$name];
2639 return $type;
2642 function lti_prepare_type_for_save($type, $config) {
2643 if (isset($config->lti_toolurl)) {
2644 $type->baseurl = $config->lti_toolurl;
2645 $type->tooldomain = lti_get_domain_from_url($config->lti_toolurl);
2647 if (isset($config->lti_description)) {
2648 $type->description = $config->lti_description;
2650 if (isset($config->lti_typename)) {
2651 $type->name = $config->lti_typename;
2653 if (isset($config->lti_ltiversion)) {
2654 $type->ltiversion = $config->lti_ltiversion;
2656 if (isset($config->lti_clientid)) {
2657 $type->clientid = $config->lti_clientid;
2659 if ((!empty($type->ltiversion) && $type->ltiversion === LTI_VERSION_1P3) && empty($type->clientid)) {
2660 $type->clientid = random_string(15);
2661 } else if (empty($type->clientid)) {
2662 $type->clientid = null;
2664 if (isset($config->lti_coursevisible)) {
2665 $type->coursevisible = $config->lti_coursevisible;
2668 if (isset($config->lti_icon)) {
2669 $type->icon = $config->lti_icon;
2671 if (isset($config->lti_secureicon)) {
2672 $type->secureicon = $config->lti_secureicon;
2675 $type->forcessl = !empty($config->lti_forcessl) ? $config->lti_forcessl : 0;
2676 $config->lti_forcessl = $type->forcessl;
2677 if (isset($config->lti_contentitem)) {
2678 $type->contentitem = !empty($config->lti_contentitem) ? $config->lti_contentitem : 0;
2679 $config->lti_contentitem = $type->contentitem;
2681 if (isset($config->lti_toolurl_ContentItemSelectionRequest)) {
2682 if (!empty($config->lti_toolurl_ContentItemSelectionRequest)) {
2683 $type->toolurl_ContentItemSelectionRequest = $config->lti_toolurl_ContentItemSelectionRequest;
2684 } else {
2685 $type->toolurl_ContentItemSelectionRequest = '';
2687 $config->lti_toolurl_ContentItemSelectionRequest = $type->toolurl_ContentItemSelectionRequest;
2690 $type->timemodified = time();
2692 unset ($config->lti_typename);
2693 unset ($config->lti_toolurl);
2694 unset ($config->lti_description);
2695 unset ($config->lti_ltiversion);
2696 unset ($config->lti_clientid);
2697 unset ($config->lti_icon);
2698 unset ($config->lti_secureicon);
2701 function lti_update_type($type, $config) {
2702 global $DB, $CFG;
2704 lti_prepare_type_for_save($type, $config);
2706 if (lti_request_is_using_ssl() && !empty($type->secureicon)) {
2707 $clearcache = !isset($config->oldicon) || ($config->oldicon !== $type->secureicon);
2708 } else {
2709 $clearcache = isset($type->icon) && (!isset($config->oldicon) || ($config->oldicon !== $type->icon));
2711 unset($config->oldicon);
2713 if ($DB->update_record('lti_types', $type)) {
2714 foreach ($config as $key => $value) {
2715 if (substr($key, 0, 4) == 'lti_' && !is_null($value)) {
2716 $record = new \StdClass();
2717 $record->typeid = $type->id;
2718 $record->name = substr($key, 4);
2719 $record->value = $value;
2720 lti_update_config($record);
2722 if (substr($key, 0, 11) == 'ltiservice_' && !is_null($value)) {
2723 $record = new \StdClass();
2724 $record->typeid = $type->id;
2725 $record->name = $key;
2726 $record->value = $value;
2727 lti_update_config($record);
2730 require_once($CFG->libdir.'/modinfolib.php');
2731 if ($clearcache) {
2732 $sql = "SELECT DISTINCT course
2733 FROM {lti}
2734 WHERE typeid = ?";
2736 $courses = $DB->get_fieldset_sql($sql, array($type->id));
2738 foreach ($courses as $courseid) {
2739 rebuild_course_cache($courseid, true);
2745 function lti_add_type($type, $config) {
2746 global $USER, $SITE, $DB;
2748 lti_prepare_type_for_save($type, $config);
2750 if (!isset($type->state)) {
2751 $type->state = LTI_TOOL_STATE_PENDING;
2754 if (!isset($type->ltiversion)) {
2755 $type->ltiversion = LTI_VERSION_1;
2758 if (!isset($type->timecreated)) {
2759 $type->timecreated = time();
2762 if (!isset($type->createdby)) {
2763 $type->createdby = $USER->id;
2766 if (!isset($type->course)) {
2767 $type->course = $SITE->id;
2770 // Create a salt value to be used for signing passed data to extension services
2771 // The outcome service uses the service salt on the instance. This can be used
2772 // for communication with services not related to a specific LTI instance.
2773 $config->lti_servicesalt = uniqid('', true);
2775 $id = $DB->insert_record('lti_types', $type);
2777 if ($id) {
2778 foreach ($config as $key => $value) {
2779 if (!is_null($value)) {
2780 if (substr($key, 0, 4) === 'lti_') {
2781 $fieldname = substr($key, 4);
2782 } else if (substr($key, 0, 11) !== 'ltiservice_') {
2783 continue;
2784 } else {
2785 $fieldname = $key;
2788 $record = new \StdClass();
2789 $record->typeid = $id;
2790 $record->name = $fieldname;
2791 $record->value = $value;
2793 lti_add_config($record);
2798 return $id;
2802 * Given an array of tool proxies, filter them based on their state
2804 * @param array $toolproxies An array of lti_tool_proxies records
2805 * @param int $state One of the LTI_TOOL_PROXY_STATE_* constants
2807 * @return array
2809 function lti_filter_tool_proxy_types(array $toolproxies, $state) {
2810 $return = array();
2811 foreach ($toolproxies as $key => $toolproxy) {
2812 if ($toolproxy->state == $state) {
2813 $return[$key] = $toolproxy;
2816 return $return;
2820 * Get the tool proxy instance given its GUID
2822 * @param string $toolproxyguid Tool proxy GUID value
2824 * @return object
2826 function lti_get_tool_proxy_from_guid($toolproxyguid) {
2827 global $DB;
2829 $toolproxy = $DB->get_record('lti_tool_proxies', array('guid' => $toolproxyguid));
2831 return $toolproxy;
2835 * Get the tool proxy instance given its registration URL
2837 * @param string $regurl Tool proxy registration URL
2839 * @return array The record of the tool proxy with this url
2841 function lti_get_tool_proxies_from_registration_url($regurl) {
2842 global $DB;
2844 return $DB->get_records_sql(
2845 'SELECT * FROM {lti_tool_proxies}
2846 WHERE '.$DB->sql_compare_text('regurl', 256).' = :regurl',
2847 array('regurl' => $regurl)
2852 * Generates some of the tool proxy configuration based on the admin configuration details
2854 * @param int $id
2856 * @return mixed Tool Proxy details
2858 function lti_get_tool_proxy($id) {
2859 global $DB;
2861 $toolproxy = $DB->get_record('lti_tool_proxies', array('id' => $id));
2862 return $toolproxy;
2866 * Returns lti tool proxies.
2868 * @param bool $orphanedonly Only retrieves tool proxies that have no type associated with them
2869 * @return array of basicLTI types
2871 function lti_get_tool_proxies($orphanedonly) {
2872 global $DB;
2874 if ($orphanedonly) {
2875 $usedproxyids = array_values($DB->get_fieldset_select('lti_types', 'toolproxyid', 'toolproxyid IS NOT NULL'));
2876 $proxies = $DB->get_records('lti_tool_proxies', null, 'state DESC, timemodified DESC');
2877 foreach ($proxies as $key => $value) {
2878 if (in_array($value->id, $usedproxyids)) {
2879 unset($proxies[$key]);
2882 return $proxies;
2883 } else {
2884 return $DB->get_records('lti_tool_proxies', null, 'state DESC, timemodified DESC');
2889 * Generates some of the tool proxy configuration based on the admin configuration details
2891 * @param int $id
2893 * @return mixed Tool Proxy details
2895 function lti_get_tool_proxy_config($id) {
2896 $toolproxy = lti_get_tool_proxy($id);
2898 $tp = new \stdClass();
2899 $tp->lti_registrationname = $toolproxy->name;
2900 $tp->toolproxyid = $toolproxy->id;
2901 $tp->state = $toolproxy->state;
2902 $tp->lti_registrationurl = $toolproxy->regurl;
2903 $tp->lti_capabilities = explode("\n", $toolproxy->capabilityoffered);
2904 $tp->lti_services = explode("\n", $toolproxy->serviceoffered);
2906 return $tp;
2910 * Update the database with a tool proxy instance
2912 * @param object $config Tool proxy definition
2914 * @return int Record id number
2916 function lti_add_tool_proxy($config) {
2917 global $USER, $DB;
2919 $toolproxy = new \stdClass();
2920 if (isset($config->lti_registrationname)) {
2921 $toolproxy->name = trim($config->lti_registrationname);
2923 if (isset($config->lti_registrationurl)) {
2924 $toolproxy->regurl = trim($config->lti_registrationurl);
2926 if (isset($config->lti_capabilities)) {
2927 $toolproxy->capabilityoffered = implode("\n", $config->lti_capabilities);
2928 } else {
2929 $toolproxy->capabilityoffered = implode("\n", array_keys(lti_get_capabilities()));
2931 if (isset($config->lti_services)) {
2932 $toolproxy->serviceoffered = implode("\n", $config->lti_services);
2933 } else {
2934 $func = function($s) {
2935 return $s->get_id();
2937 $servicenames = array_map($func, lti_get_services());
2938 $toolproxy->serviceoffered = implode("\n", $servicenames);
2940 if (isset($config->toolproxyid) && !empty($config->toolproxyid)) {
2941 $toolproxy->id = $config->toolproxyid;
2942 if (!isset($toolproxy->state) || ($toolproxy->state != LTI_TOOL_PROXY_STATE_ACCEPTED)) {
2943 $toolproxy->state = LTI_TOOL_PROXY_STATE_CONFIGURED;
2944 $toolproxy->guid = random_string();
2945 $toolproxy->secret = random_string();
2947 $id = lti_update_tool_proxy($toolproxy);
2948 } else {
2949 $toolproxy->state = LTI_TOOL_PROXY_STATE_CONFIGURED;
2950 $toolproxy->timemodified = time();
2951 $toolproxy->timecreated = $toolproxy->timemodified;
2952 if (!isset($toolproxy->createdby)) {
2953 $toolproxy->createdby = $USER->id;
2955 $toolproxy->guid = random_string();
2956 $toolproxy->secret = random_string();
2957 $id = $DB->insert_record('lti_tool_proxies', $toolproxy);
2960 return $id;
2964 * Updates a tool proxy in the database
2966 * @param object $toolproxy Tool proxy
2968 * @return int Record id number
2970 function lti_update_tool_proxy($toolproxy) {
2971 global $DB;
2973 $toolproxy->timemodified = time();
2974 $id = $DB->update_record('lti_tool_proxies', $toolproxy);
2976 return $id;
2980 * Delete a Tool Proxy
2982 * @param int $id Tool Proxy id
2984 function lti_delete_tool_proxy($id) {
2985 global $DB;
2986 $DB->delete_records('lti_tool_settings', array('toolproxyid' => $id));
2987 $tools = $DB->get_records('lti_types', array('toolproxyid' => $id));
2988 foreach ($tools as $tool) {
2989 lti_delete_type($tool->id);
2991 $DB->delete_records('lti_tool_proxies', array('id' => $id));
2995 * Add a tool configuration in the database
2997 * @param object $config Tool configuration
2999 * @return int Record id number
3001 function lti_add_config($config) {
3002 global $DB;
3004 return $DB->insert_record('lti_types_config', $config);
3008 * Updates a tool configuration in the database
3010 * @param object $config Tool configuration
3012 * @return mixed Record id number
3014 function lti_update_config($config) {
3015 global $DB;
3017 $old = $DB->get_record('lti_types_config', array('typeid' => $config->typeid, 'name' => $config->name));
3019 if ($old) {
3020 $config->id = $old->id;
3021 $return = $DB->update_record('lti_types_config', $config);
3022 } else {
3023 $return = $DB->insert_record('lti_types_config', $config);
3025 return $return;
3029 * Gets the tool settings
3031 * @param int $toolproxyid Id of tool proxy record (or tool ID if negative)
3032 * @param int $courseid Id of course (null if system settings)
3033 * @param int $instanceid Id of course module (null if system or context settings)
3035 * @return array Array settings
3037 function lti_get_tool_settings($toolproxyid, $courseid = null, $instanceid = null) {
3038 global $DB;
3040 $settings = array();
3041 if ($toolproxyid > 0) {
3042 $settingsstr = $DB->get_field('lti_tool_settings', 'settings', array('toolproxyid' => $toolproxyid,
3043 'course' => $courseid, 'coursemoduleid' => $instanceid));
3044 } else {
3045 $settingsstr = $DB->get_field('lti_tool_settings', 'settings', array('typeid' => -$toolproxyid,
3046 'course' => $courseid, 'coursemoduleid' => $instanceid));
3048 if ($settingsstr !== false) {
3049 $settings = json_decode($settingsstr, true);
3051 return $settings;
3055 * Sets the tool settings (
3057 * @param array $settings Array of settings
3058 * @param int $toolproxyid Id of tool proxy record (or tool ID if negative)
3059 * @param int $courseid Id of course (null if system settings)
3060 * @param int $instanceid Id of course module (null if system or context settings)
3062 function lti_set_tool_settings($settings, $toolproxyid, $courseid = null, $instanceid = null) {
3063 global $DB;
3065 $json = json_encode($settings);
3066 if ($toolproxyid >= 0) {
3067 $record = $DB->get_record('lti_tool_settings', array('toolproxyid' => $toolproxyid,
3068 'course' => $courseid, 'coursemoduleid' => $instanceid));
3069 } else {
3070 $record = $DB->get_record('lti_tool_settings', array('typeid' => -$toolproxyid,
3071 'course' => $courseid, 'coursemoduleid' => $instanceid));
3073 if ($record !== false) {
3074 $DB->update_record('lti_tool_settings', (object)array('id' => $record->id, 'settings' => $json, 'timemodified' => time()));
3075 } else {
3076 $record = new \stdClass();
3077 if ($toolproxyid > 0) {
3078 $record->toolproxyid = $toolproxyid;
3079 } else {
3080 $record->typeid = -$toolproxyid;
3082 $record->course = $courseid;
3083 $record->coursemoduleid = $instanceid;
3084 $record->settings = $json;
3085 $record->timecreated = time();
3086 $record->timemodified = $record->timecreated;
3087 $DB->insert_record('lti_tool_settings', $record);
3092 * Signs the petition to launch the external tool using OAuth
3094 * @param array $oldparms Parameters to be passed for signing
3095 * @param string $endpoint url of the external tool
3096 * @param string $method Method for sending the parameters (e.g. POST)
3097 * @param string $oauthconsumerkey
3098 * @param string $oauthconsumersecret
3099 * @return array|null
3101 function lti_sign_parameters($oldparms, $endpoint, $method, $oauthconsumerkey, $oauthconsumersecret) {
3103 $parms = $oldparms;
3105 $testtoken = '';
3107 // TODO: Switch to core oauthlib once implemented - MDL-30149.
3108 $hmacmethod = new lti\OAuthSignatureMethod_HMAC_SHA1();
3109 $testconsumer = new lti\OAuthConsumer($oauthconsumerkey, $oauthconsumersecret, null);
3110 $accreq = lti\OAuthRequest::from_consumer_and_token($testconsumer, $testtoken, $method, $endpoint, $parms);
3111 $accreq->sign_request($hmacmethod, $testconsumer, $testtoken);
3113 $newparms = $accreq->get_parameters();
3115 return $newparms;
3119 * Converts the message paramters to their equivalent JWT claim and signs the payload to launch the external tool using JWT
3121 * @param array $parms Parameters to be passed for signing
3122 * @param string $endpoint url of the external tool
3123 * @param string $oauthconsumerkey
3124 * @param string $typeid ID of LTI tool type
3125 * @param string $nonce Nonce value to use
3126 * @return array|null
3128 function lti_sign_jwt($parms, $endpoint, $oauthconsumerkey, $typeid = 0, $nonce = '') {
3129 global $CFG;
3131 if (empty($typeid)) {
3132 $typeid = 0;
3134 $messagetypemapping = lti_get_jwt_message_type_mapping();
3135 if (isset($parms['lti_message_type']) && array_key_exists($parms['lti_message_type'], $messagetypemapping)) {
3136 $parms['lti_message_type'] = $messagetypemapping[$parms['lti_message_type']];
3138 if (isset($parms['roles'])) {
3139 $roles = explode(',', $parms['roles']);
3140 $newroles = array();
3141 foreach ($roles as $role) {
3142 if (strpos($role, 'urn:lti:role:ims/lis/') === 0) {
3143 $role = 'http://purl.imsglobal.org/vocab/lis/v2/membership#' . substr($role, 21);
3144 } else if (strpos($role, 'urn:lti:instrole:ims/lis/') === 0) {
3145 $role = 'http://purl.imsglobal.org/vocab/lis/v2/institution/person#' . substr($role, 25);
3146 } else if (strpos($role, 'urn:lti:sysrole:ims/lis/') === 0) {
3147 $role = 'http://purl.imsglobal.org/vocab/lis/v2/system/person#' . substr($role, 24);
3148 } else if ((strpos($role, '://') === false) && (strpos($role, 'urn:') !== 0)) {
3149 $role = "http://purl.imsglobal.org/vocab/lis/v2/membership#{$role}";
3151 $newroles[] = $role;
3153 $parms['roles'] = implode(',', $newroles);
3156 $now = time();
3157 if (empty($nonce)) {
3158 $nonce = bin2hex(openssl_random_pseudo_bytes(10));
3160 $claimmapping = lti_get_jwt_claim_mapping();
3161 $payload = array(
3162 'nonce' => $nonce,
3163 'iat' => $now,
3164 'exp' => $now + 60,
3166 $payload['iss'] = $CFG->wwwroot;
3167 $payload['aud'] = $oauthconsumerkey;
3168 $payload[LTI_JWT_CLAIM_PREFIX . '/claim/deployment_id'] = strval($typeid);
3169 $payload[LTI_JWT_CLAIM_PREFIX . '/claim/target_link_uri'] = $endpoint;
3171 foreach ($parms as $key => $value) {
3172 $claim = LTI_JWT_CLAIM_PREFIX;
3173 if (array_key_exists($key, $claimmapping)) {
3174 $mapping = $claimmapping[$key];
3175 if ($mapping['isarray']) {
3176 $value = explode(',', $value);
3177 sort($value);
3179 if (!empty($mapping['suffix'])) {
3180 $claim .= "-{$mapping['suffix']}";
3182 $claim .= '/claim/';
3183 if (is_null($mapping['group'])) {
3184 $payload[$mapping['claim']] = $value;
3185 } else if (empty($mapping['group'])) {
3186 $payload["{$claim}{$mapping['claim']}"] = $value;
3187 } else {
3188 $claim .= $mapping['group'];
3189 $payload[$claim][$mapping['claim']] = $value;
3191 } else if (strpos($key, 'custom_') === 0) {
3192 $payload["{$claim}/claim/custom"][substr($key, 7)] = $value;
3193 } else if (strpos($key, 'ext_') === 0) {
3194 $payload["{$claim}/claim/ext"][substr($key, 4)] = $value;
3198 $privatekey = get_config('mod_lti', 'privatekey');
3199 $kid = get_config('mod_lti', 'kid');
3200 $jwt = JWT::encode($payload, $privatekey, 'RS256', $kid);
3202 $newparms = array();
3203 $newparms['id_token'] = $jwt;
3205 return $newparms;
3209 * Verfies the JWT and converts its claims to their equivalent message parameter.
3211 * @param int $typeid
3212 * @param string $jwtparam JWT parameter
3214 * @return array message parameters
3215 * @throws moodle_exception
3217 function lti_convert_from_jwt($typeid, $jwtparam) {
3219 $params = array();
3220 $parts = explode('.', $jwtparam);
3221 $ok = (count($parts) === 3);
3222 if ($ok) {
3223 $payload = JWT::urlsafeB64Decode($parts[1]);
3224 $claims = json_decode($payload, true);
3225 $ok = !is_null($claims) && !empty($claims['iss']);
3227 if ($ok) {
3228 lti_verify_jwt_signature($typeid, $claims['iss'], $jwtparam);
3229 $params['oauth_consumer_key'] = $claims['iss'];
3230 foreach (lti_get_jwt_claim_mapping() as $key => $mapping) {
3231 $claim = LTI_JWT_CLAIM_PREFIX;
3232 if (!empty($mapping['suffix'])) {
3233 $claim .= "-{$mapping['suffix']}";
3235 $claim .= '/claim/';
3236 if (is_null($mapping['group'])) {
3237 $claim = $mapping['claim'];
3238 } else if (empty($mapping['group'])) {
3239 $claim .= $mapping['claim'];
3240 } else {
3241 $claim .= $mapping['group'];
3243 if (isset($claims[$claim])) {
3244 $value = null;
3245 if (empty($mapping['group'])) {
3246 $value = $claims[$claim];
3247 } else {
3248 $group = $claims[$claim];
3249 if (is_array($group) && array_key_exists($mapping['claim'], $group)) {
3250 $value = $group[$mapping['claim']];
3253 if (!empty($value) && $mapping['isarray']) {
3254 if (is_array($value)) {
3255 if (is_array($value[0])) {
3256 $value = json_encode($value);
3257 } else {
3258 $value = implode(',', $value);
3262 if (!is_null($value) && is_string($value) && (strlen($value) > 0)) {
3263 $params[$key] = $value;
3266 $claim = LTI_JWT_CLAIM_PREFIX . '/claim/custom';
3267 if (isset($claims[$claim])) {
3268 $custom = $claims[$claim];
3269 if (is_array($custom)) {
3270 foreach ($custom as $key => $value) {
3271 $params["custom_{$key}"] = $value;
3275 $claim = LTI_JWT_CLAIM_PREFIX . '/claim/ext';
3276 if (isset($claims[$claim])) {
3277 $ext = $claims[$claim];
3278 if (is_array($ext)) {
3279 foreach ($ext as $key => $value) {
3280 $params["ext_{$key}"] = $value;
3286 if (isset($params['content_items'])) {
3287 $params['content_items'] = lti_convert_content_items($params['content_items']);
3289 $messagetypemapping = lti_get_jwt_message_type_mapping();
3290 if (isset($params['lti_message_type']) && array_key_exists($params['lti_message_type'], $messagetypemapping)) {
3291 $params['lti_message_type'] = $messagetypemapping[$params['lti_message_type']];
3293 return $params;
3297 * Posts the launch petition HTML
3299 * @param array $newparms Signed parameters
3300 * @param string $endpoint URL of the external tool
3301 * @param bool $debug Debug (true/false)
3302 * @return string
3304 function lti_post_launch_html($newparms, $endpoint, $debug=false) {
3305 $r = "<form action=\"" . $endpoint .
3306 "\" name=\"ltiLaunchForm\" id=\"ltiLaunchForm\" method=\"post\" encType=\"application/x-www-form-urlencoded\">\n";
3308 // Contruct html for the launch parameters.
3309 foreach ($newparms as $key => $value) {
3310 $key = htmlspecialchars($key);
3311 $value = htmlspecialchars($value);
3312 if ( $key == "ext_submit" ) {
3313 $r .= "<input type=\"submit\"";
3314 } else {
3315 $r .= "<input type=\"hidden\" name=\"{$key}\"";
3317 $r .= " value=\"";
3318 $r .= $value;
3319 $r .= "\"/>\n";
3322 if ( $debug ) {
3323 $r .= "<script language=\"javascript\"> \n";
3324 $r .= " //<![CDATA[ \n";
3325 $r .= "function basicltiDebugToggle() {\n";
3326 $r .= " var ele = document.getElementById(\"basicltiDebug\");\n";
3327 $r .= " if (ele.style.display == \"block\") {\n";
3328 $r .= " ele.style.display = \"none\";\n";
3329 $r .= " }\n";
3330 $r .= " else {\n";
3331 $r .= " ele.style.display = \"block\";\n";
3332 $r .= " }\n";
3333 $r .= "} \n";
3334 $r .= " //]]> \n";
3335 $r .= "</script>\n";
3336 $r .= "<a id=\"displayText\" href=\"javascript:basicltiDebugToggle();\">";
3337 $r .= get_string("toggle_debug_data", "lti")."</a>\n";
3338 $r .= "<div id=\"basicltiDebug\" style=\"display:none\">\n";
3339 $r .= "<b>".get_string("basiclti_endpoint", "lti")."</b><br/>\n";
3340 $r .= $endpoint . "<br/>\n&nbsp;<br/>\n";
3341 $r .= "<b>".get_string("basiclti_parameters", "lti")."</b><br/>\n";
3342 foreach ($newparms as $key => $value) {
3343 $key = htmlspecialchars($key);
3344 $value = htmlspecialchars($value);
3345 $r .= "$key = $value<br/>\n";
3347 $r .= "&nbsp;<br/>\n";
3348 $r .= "</div>\n";
3350 $r .= "</form>\n";
3352 if ( ! $debug ) {
3353 $r .= " <script type=\"text/javascript\"> \n" .
3354 " //<![CDATA[ \n" .
3355 " document.ltiLaunchForm.submit(); \n" .
3356 " //]]> \n" .
3357 " </script> \n";
3359 return $r;
3363 * Generate the form for initiating a login request for an LTI 1.3 message
3365 * @param int $courseid Course ID
3366 * @param int $id LTI instance ID
3367 * @param stdClass|null $instance LTI instance
3368 * @param stdClass $config Tool type configuration
3369 * @param string $messagetype LTI message type
3370 * @param string $title Title of content item
3371 * @param string $text Description of content item
3372 * @return string
3374 function lti_initiate_login($courseid, $id, $instance, $config, $messagetype = 'basic-lti-launch-request', $title = '',
3375 $text = '') {
3376 global $SESSION;
3378 $params = lti_build_login_request($courseid, $id, $instance, $config, $messagetype);
3379 $SESSION->lti_message_hint = "{$courseid},{$config->typeid},{$id}," . base64_encode($title) . ',' .
3380 base64_encode($text);
3382 $r = "<form action=\"" . $config->lti_initiatelogin .
3383 "\" name=\"ltiInitiateLoginForm\" id=\"ltiInitiateLoginForm\" method=\"post\" " .
3384 "encType=\"application/x-www-form-urlencoded\">\n";
3386 foreach ($params as $key => $value) {
3387 $key = htmlspecialchars($key);
3388 $value = htmlspecialchars($value);
3389 $r .= " <input type=\"hidden\" name=\"{$key}\" value=\"{$value}\"/>\n";
3391 $r .= "</form>\n";
3393 $r .= "<script type=\"text/javascript\">\n" .
3394 "//<![CDATA[\n" .
3395 "document.ltiInitiateLoginForm.submit();\n" .
3396 "//]]>\n" .
3397 "</script>\n";
3399 return $r;
3403 * Prepares an LTI 1.3 login request
3405 * @param int $courseid Course ID
3406 * @param int $id LTI instance ID
3407 * @param stdClass|null $instance LTI instance
3408 * @param stdClass $config Tool type configuration
3409 * @param string $messagetype LTI message type
3410 * @return array Login request parameters
3412 function lti_build_login_request($courseid, $id, $instance, $config, $messagetype) {
3413 global $USER, $CFG;
3415 if (!empty($instance)) {
3416 $endpoint = !empty($instance->toolurl) ? $instance->toolurl : $config->lti_toolurl;
3417 } else {
3418 $endpoint = $config->lti_toolurl;
3419 if (($messagetype === 'ContentItemSelectionRequest') && !empty($config->lti_toolurl_ContentItemSelectionRequest)) {
3420 $endpoint = $config->lti_toolurl_ContentItemSelectionRequest;
3423 $endpoint = trim($endpoint);
3425 // If SSL is forced make sure https is on the normal launch URL.
3426 if (isset($config->lti_forcessl) && ($config->lti_forcessl == '1')) {
3427 $endpoint = lti_ensure_url_is_https($endpoint);
3428 } else if (!strstr($endpoint, '://')) {
3429 $endpoint = 'http://' . $endpoint;
3432 $params = array();
3433 $params['iss'] = $CFG->wwwroot;
3434 $params['target_link_uri'] = $endpoint;
3435 $params['login_hint'] = $USER->id;
3436 $params['lti_message_hint'] = $id;
3437 $params['client_id'] = $config->lti_clientid;
3438 $params['lti_deployment_id'] = $config->typeid;
3439 return $params;
3442 function lti_get_type($typeid) {
3443 global $DB;
3445 return $DB->get_record('lti_types', array('id' => $typeid));
3448 function lti_get_launch_container($lti, $toolconfig) {
3449 if (empty($lti->launchcontainer)) {
3450 $lti->launchcontainer = LTI_LAUNCH_CONTAINER_DEFAULT;
3453 if ($lti->launchcontainer == LTI_LAUNCH_CONTAINER_DEFAULT) {
3454 if (isset($toolconfig['launchcontainer'])) {
3455 $launchcontainer = $toolconfig['launchcontainer'];
3457 } else {
3458 $launchcontainer = $lti->launchcontainer;
3461 if (empty($launchcontainer) || $launchcontainer == LTI_LAUNCH_CONTAINER_DEFAULT) {
3462 $launchcontainer = LTI_LAUNCH_CONTAINER_EMBED_NO_BLOCKS;
3465 $devicetype = core_useragent::get_device_type();
3467 // Scrolling within the object element doesn't work on iOS or Android
3468 // Opening the popup window also had some issues in testing
3469 // For mobile devices, always take up the entire screen to ensure the best experience.
3470 if ($devicetype === core_useragent::DEVICETYPE_MOBILE || $devicetype === core_useragent::DEVICETYPE_TABLET ) {
3471 $launchcontainer = LTI_LAUNCH_CONTAINER_REPLACE_MOODLE_WINDOW;
3474 return $launchcontainer;
3477 function lti_request_is_using_ssl() {
3478 global $CFG;
3479 return (stripos($CFG->wwwroot, 'https://') === 0);
3482 function lti_ensure_url_is_https($url) {
3483 if (!strstr($url, '://')) {
3484 $url = 'https://' . $url;
3485 } else {
3486 // If the URL starts with http, replace with https.
3487 if (stripos($url, 'http://') === 0) {
3488 $url = 'https://' . substr($url, 7);
3492 return $url;
3496 * Determines if we should try to log the request
3498 * @param string $rawbody
3499 * @return bool
3501 function lti_should_log_request($rawbody) {
3502 global $CFG;
3504 if (empty($CFG->mod_lti_log_users)) {
3505 return false;
3508 $logusers = explode(',', $CFG->mod_lti_log_users);
3509 if (empty($logusers)) {
3510 return false;
3513 try {
3514 $xml = new \SimpleXMLElement($rawbody);
3515 $ns = $xml->getNamespaces();
3516 $ns = array_shift($ns);
3517 $xml->registerXPathNamespace('lti', $ns);
3518 $requestuserid = '';
3519 if ($node = $xml->xpath('//lti:userId')) {
3520 $node = $node[0];
3521 $requestuserid = clean_param((string) $node, PARAM_INT);
3522 } else if ($node = $xml->xpath('//lti:sourcedId')) {
3523 $node = $node[0];
3524 $resultjson = json_decode((string) $node);
3525 $requestuserid = clean_param($resultjson->data->userid, PARAM_INT);
3527 } catch (Exception $e) {
3528 return false;
3531 if (empty($requestuserid) or !in_array($requestuserid, $logusers)) {
3532 return false;
3535 return true;
3539 * Logs the request to a file in temp dir.
3541 * @param string $rawbody
3543 function lti_log_request($rawbody) {
3544 if ($tempdir = make_temp_directory('mod_lti', false)) {
3545 if ($tempfile = tempnam($tempdir, 'mod_lti_request'.date('YmdHis'))) {
3546 $content = "Request Headers:\n";
3547 foreach (moodle\mod\lti\OAuthUtil::get_headers() as $header => $value) {
3548 $content .= "$header: $value\n";
3550 $content .= "Request Body:\n";
3551 $content .= $rawbody;
3553 file_put_contents($tempfile, $content);
3554 chmod($tempfile, 0644);
3560 * Log an LTI response.
3562 * @param string $responsexml The response XML
3563 * @param Exception $e If there was an exception, pass that too
3565 function lti_log_response($responsexml, $e = null) {
3566 if ($tempdir = make_temp_directory('mod_lti', false)) {
3567 if ($tempfile = tempnam($tempdir, 'mod_lti_response'.date('YmdHis'))) {
3568 $content = '';
3569 if ($e instanceof Exception) {
3570 $info = get_exception_info($e);
3572 $content .= "Exception:\n";
3573 $content .= "Message: $info->message\n";
3574 $content .= "Debug info: $info->debuginfo\n";
3575 $content .= "Backtrace:\n";
3576 $content .= format_backtrace($info->backtrace, true);
3577 $content .= "\n";
3579 $content .= "Response XML:\n";
3580 $content .= $responsexml;
3582 file_put_contents($tempfile, $content);
3583 chmod($tempfile, 0644);
3589 * Fetches LTI type configuration for an LTI instance
3591 * @param stdClass $instance
3592 * @return array Can be empty if no type is found
3594 function lti_get_type_config_by_instance($instance) {
3595 $typeid = null;
3596 if (empty($instance->typeid)) {
3597 $tool = lti_get_tool_by_url_match($instance->toolurl, $instance->course);
3598 if ($tool) {
3599 $typeid = $tool->id;
3601 } else {
3602 $typeid = $instance->typeid;
3604 if (!empty($typeid)) {
3605 return lti_get_type_config($typeid);
3607 return array();
3611 * Enforce type config settings onto the LTI instance
3613 * @param stdClass $instance
3614 * @param array $typeconfig
3616 function lti_force_type_config_settings($instance, array $typeconfig) {
3617 $forced = array(
3618 'instructorchoicesendname' => 'sendname',
3619 'instructorchoicesendemailaddr' => 'sendemailaddr',
3620 'instructorchoiceacceptgrades' => 'acceptgrades',
3623 foreach ($forced as $instanceparam => $typeconfigparam) {
3624 if (array_key_exists($typeconfigparam, $typeconfig) && $typeconfig[$typeconfigparam] != LTI_SETTING_DELEGATE) {
3625 $instance->$instanceparam = $typeconfig[$typeconfigparam];
3631 * Initializes an array with the capabilities supported by the LTI module
3633 * @return array List of capability names (without a dollar sign prefix)
3635 function lti_get_capabilities() {
3637 $capabilities = array(
3638 'basic-lti-launch-request' => '',
3639 'ContentItemSelectionRequest' => '',
3640 'ToolProxyRegistrationRequest' => '',
3641 'Context.id' => 'context_id',
3642 'Context.title' => 'context_title',
3643 'Context.label' => 'context_label',
3644 'Context.sourcedId' => 'lis_course_section_sourcedid',
3645 'Context.longDescription' => '$COURSE->summary',
3646 'Context.timeFrame.begin' => '$COURSE->startdate',
3647 'CourseSection.title' => 'context_title',
3648 'CourseSection.label' => 'context_label',
3649 'CourseSection.sourcedId' => 'lis_course_section_sourcedid',
3650 'CourseSection.longDescription' => '$COURSE->summary',
3651 'CourseSection.timeFrame.begin' => '$COURSE->startdate',
3652 'ResourceLink.id' => 'resource_link_id',
3653 'ResourceLink.title' => 'resource_link_title',
3654 'ResourceLink.description' => 'resource_link_description',
3655 'User.id' => 'user_id',
3656 'User.username' => '$USER->username',
3657 'Person.name.full' => 'lis_person_name_full',
3658 'Person.name.given' => 'lis_person_name_given',
3659 'Person.name.family' => 'lis_person_name_family',
3660 'Person.email.primary' => 'lis_person_contact_email_primary',
3661 'Person.sourcedId' => 'lis_person_sourcedid',
3662 'Person.name.middle' => '$USER->middlename',
3663 'Person.address.street1' => '$USER->address',
3664 'Person.address.locality' => '$USER->city',
3665 'Person.address.country' => '$USER->country',
3666 'Person.address.timezone' => '$USER->timezone',
3667 'Person.phone.primary' => '$USER->phone1',
3668 'Person.phone.mobile' => '$USER->phone2',
3669 'Person.webaddress' => '$USER->url',
3670 'Membership.role' => 'roles',
3671 'Result.sourcedId' => 'lis_result_sourcedid',
3672 'Result.autocreate' => 'lis_outcome_service_url',
3673 'BasicOutcome.sourcedId' => 'lis_result_sourcedid',
3674 'BasicOutcome.url' => 'lis_outcome_service_url',
3675 'Moodle.Person.userGroupIds' => null);
3677 return $capabilities;
3682 * Initializes an array with the services supported by the LTI module
3684 * @return array List of services
3686 function lti_get_services() {
3688 $services = array();
3689 $definedservices = core_component::get_plugin_list('ltiservice');
3690 foreach ($definedservices as $name => $location) {
3691 $classname = "\\ltiservice_{$name}\\local\\service\\{$name}";
3692 $services[] = new $classname();
3695 return $services;
3700 * Initializes an instance of the named service
3702 * @param string $servicename Name of service
3704 * @return bool|\mod_lti\local\ltiservice\service_base Service
3706 function lti_get_service_by_name($servicename) {
3708 $service = false;
3709 $classname = "\\ltiservice_{$servicename}\\local\\service\\{$servicename}";
3710 if (class_exists($classname)) {
3711 $service = new $classname();
3714 return $service;
3719 * Finds a service by id
3721 * @param \mod_lti\local\ltiservice\service_base[] $services Array of services
3722 * @param string $resourceid ID of resource
3724 * @return mod_lti\local\ltiservice\service_base Service
3726 function lti_get_service_by_resource_id($services, $resourceid) {
3728 $service = false;
3729 foreach ($services as $aservice) {
3730 foreach ($aservice->get_resources() as $resource) {
3731 if ($resource->get_id() === $resourceid) {
3732 $service = $aservice;
3733 break 2;
3738 return $service;
3743 * Initializes an array with the scopes for services supported by the LTI module
3745 * @param object $type LTI tool type
3746 * @param array $typeconfig LTI tool type configuration
3748 * @return array List of scopes
3750 function lti_get_permitted_service_scopes($type, $typeconfig) {
3752 $services = lti_get_services();
3753 $scopes = array();
3754 foreach ($services as $service) {
3755 $service->set_type($type);
3756 $service->set_typeconfig($typeconfig);
3757 $servicescopes = $service->get_permitted_scopes();
3758 if (!empty($servicescopes)) {
3759 $scopes = array_merge($scopes, $servicescopes);
3763 return $scopes;
3768 * Extracts the named contexts from a tool proxy
3770 * @param object $json
3772 * @return array Contexts
3774 function lti_get_contexts($json) {
3776 $contexts = array();
3777 if (isset($json->{'@context'})) {
3778 foreach ($json->{'@context'} as $context) {
3779 if (is_object($context)) {
3780 $contexts = array_merge(get_object_vars($context), $contexts);
3785 return $contexts;
3790 * Converts an ID to a fully-qualified ID
3792 * @param array $contexts
3793 * @param string $id
3795 * @return string Fully-qualified ID
3797 function lti_get_fqid($contexts, $id) {
3799 $parts = explode(':', $id, 2);
3800 if (count($parts) > 1) {
3801 if (array_key_exists($parts[0], $contexts)) {
3802 $id = $contexts[$parts[0]] . $parts[1];
3806 return $id;
3811 * Returns the icon for the given tool type
3813 * @param stdClass $type The tool type
3815 * @return string The url to the tool type's corresponding icon
3817 function get_tool_type_icon_url(stdClass $type) {
3818 global $OUTPUT;
3820 $iconurl = $type->secureicon;
3822 if (empty($iconurl)) {
3823 $iconurl = $type->icon;
3826 if (empty($iconurl)) {
3827 $iconurl = $OUTPUT->image_url('icon', 'lti')->out();
3830 return $iconurl;
3834 * Returns the edit url for the given tool type
3836 * @param stdClass $type The tool type
3838 * @return string The url to edit the tool type
3840 function get_tool_type_edit_url(stdClass $type) {
3841 $url = new moodle_url('/mod/lti/typessettings.php',
3842 array('action' => 'update', 'id' => $type->id, 'sesskey' => sesskey(), 'returnto' => 'toolconfigure'));
3843 return $url->out();
3847 * Returns the edit url for the given tool proxy.
3849 * @param stdClass $proxy The tool proxy
3851 * @return string The url to edit the tool type
3853 function get_tool_proxy_edit_url(stdClass $proxy) {
3854 $url = new moodle_url('/mod/lti/registersettings.php',
3855 array('action' => 'update', 'id' => $proxy->id, 'sesskey' => sesskey(), 'returnto' => 'toolconfigure'));
3856 return $url->out();
3860 * Returns the course url for the given tool type
3862 * @param stdClass $type The tool type
3864 * @return string The url to the course of the tool type, void if it is a site wide type
3866 function get_tool_type_course_url(stdClass $type) {
3867 if ($type->course != 1) {
3868 $url = new moodle_url('/course/view.php', array('id' => $type->course));
3869 return $url->out();
3871 return null;
3875 * Returns the icon and edit urls for the tool type and the course url if it is a course type.
3877 * @param stdClass $type The tool type
3879 * @return array The urls of the tool type
3881 function get_tool_type_urls(stdClass $type) {
3882 $courseurl = get_tool_type_course_url($type);
3884 $urls = array(
3885 'icon' => get_tool_type_icon_url($type),
3886 'edit' => get_tool_type_edit_url($type),
3889 if ($courseurl) {
3890 $urls['course'] = $courseurl;
3893 $url = new moodle_url('/mod/lti/certs.php');
3894 $urls['publickeyset'] = $url->out();
3895 $url = new moodle_url('/mod/lti/token.php');
3896 $urls['accesstoken'] = $url->out();
3897 $url = new moodle_url('/mod/lti/auth.php');
3898 $urls['authrequest'] = $url->out();
3900 return $urls;
3904 * Returns the icon and edit urls for the tool proxy.
3906 * @param stdClass $proxy The tool proxy
3908 * @return array The urls of the tool proxy
3910 function get_tool_proxy_urls(stdClass $proxy) {
3911 global $OUTPUT;
3913 $urls = array(
3914 'icon' => $OUTPUT->image_url('icon', 'lti')->out(),
3915 'edit' => get_tool_proxy_edit_url($proxy),
3918 return $urls;
3922 * Returns information on the current state of the tool type
3924 * @param stdClass $type The tool type
3926 * @return array An array with a text description of the state, and boolean for whether it is in each state:
3927 * pending, configured, rejected, unknown
3929 function get_tool_type_state_info(stdClass $type) {
3930 $isconfigured = false;
3931 $ispending = false;
3932 $isrejected = false;
3933 $isunknown = false;
3934 switch ($type->state) {
3935 case LTI_TOOL_STATE_CONFIGURED:
3936 $state = get_string('active', 'mod_lti');
3937 $isconfigured = true;
3938 break;
3939 case LTI_TOOL_STATE_PENDING:
3940 $state = get_string('pending', 'mod_lti');
3941 $ispending = true;
3942 break;
3943 case LTI_TOOL_STATE_REJECTED:
3944 $state = get_string('rejected', 'mod_lti');
3945 $isrejected = true;
3946 break;
3947 default:
3948 $state = get_string('unknownstate', 'mod_lti');
3949 $isunknown = true;
3950 break;
3953 return array(
3954 'text' => $state,
3955 'pending' => $ispending,
3956 'configured' => $isconfigured,
3957 'rejected' => $isrejected,
3958 'unknown' => $isunknown
3963 * Returns information on the configuration of the tool type
3965 * @param stdClass $type The tool type
3967 * @return array An array with configuration details
3969 function get_tool_type_config($type) {
3970 global $CFG;
3971 $platformid = $CFG->wwwroot;
3972 $clientid = $type->clientid;
3973 $deploymentid = $type->id;
3974 $publickeyseturl = new moodle_url('/mod/lti/certs.php');
3975 $publickeyseturl = $publickeyseturl->out();
3977 $accesstokenurl = new moodle_url('/mod/lti/token.php');
3978 $accesstokenurl = $accesstokenurl->out();
3980 $authrequesturl = new moodle_url('/mod/lti/auth.php');
3981 $authrequesturl = $authrequesturl->out();
3983 return array(
3984 'platformid' => $platformid,
3985 'clientid' => $clientid,
3986 'deploymentid' => $deploymentid,
3987 'publickeyseturl' => $publickeyseturl,
3988 'accesstokenurl' => $accesstokenurl,
3989 'authrequesturl' => $authrequesturl
3994 * Returns a summary of each LTI capability this tool type requires in plain language
3996 * @param stdClass $type The tool type
3998 * @return array An array of text descriptions of each of the capabilities this tool type requires
4000 function get_tool_type_capability_groups($type) {
4001 $capabilities = lti_get_enabled_capabilities($type);
4002 $groups = array();
4003 $hascourse = false;
4004 $hasactivities = false;
4005 $hasuseraccount = false;
4006 $hasuserpersonal = false;
4008 foreach ($capabilities as $capability) {
4009 // Bail out early if we've already found all groups.
4010 if (count($groups) >= 4) {
4011 continue;
4014 if (!$hascourse && preg_match('/^CourseSection/', $capability)) {
4015 $hascourse = true;
4016 $groups[] = get_string('courseinformation', 'mod_lti');
4017 } else if (!$hasactivities && preg_match('/^ResourceLink/', $capability)) {
4018 $hasactivities = true;
4019 $groups[] = get_string('courseactivitiesorresources', 'mod_lti');
4020 } else if (!$hasuseraccount && preg_match('/^User/', $capability) || preg_match('/^Membership/', $capability)) {
4021 $hasuseraccount = true;
4022 $groups[] = get_string('useraccountinformation', 'mod_lti');
4023 } else if (!$hasuserpersonal && preg_match('/^Person/', $capability)) {
4024 $hasuserpersonal = true;
4025 $groups[] = get_string('userpersonalinformation', 'mod_lti');
4029 return $groups;
4034 * Returns the ids of each instance of this tool type
4036 * @param stdClass $type The tool type
4038 * @return array An array of ids of the instances of this tool type
4040 function get_tool_type_instance_ids($type) {
4041 global $DB;
4043 return array_keys($DB->get_fieldset_select('lti', 'id', 'typeid = ?', array($type->id)));
4047 * Serialises this tool type
4049 * @param stdClass $type The tool type
4051 * @return array An array of values representing this type
4053 function serialise_tool_type(stdClass $type) {
4054 global $CFG;
4056 $capabilitygroups = get_tool_type_capability_groups($type);
4057 $instanceids = get_tool_type_instance_ids($type);
4058 // Clean the name. We don't want tags here.
4059 $name = clean_param($type->name, PARAM_NOTAGS);
4060 if (!empty($type->description)) {
4061 // Clean the description. We don't want tags here.
4062 $description = clean_param($type->description, PARAM_NOTAGS);
4063 } else {
4064 $description = get_string('editdescription', 'mod_lti');
4066 return array(
4067 'id' => $type->id,
4068 'name' => $name,
4069 'description' => $description,
4070 'urls' => get_tool_type_urls($type),
4071 'state' => get_tool_type_state_info($type),
4072 'platformid' => $CFG->wwwroot,
4073 'clientid' => $type->clientid,
4074 'deploymentid' => $type->id,
4075 'hascapabilitygroups' => !empty($capabilitygroups),
4076 'capabilitygroups' => $capabilitygroups,
4077 // Course ID of 1 means it's not linked to a course.
4078 'courseid' => $type->course == 1 ? 0 : $type->course,
4079 'instanceids' => $instanceids,
4080 'instancecount' => count($instanceids)
4085 * Serialises this tool proxy.
4087 * @param stdClass $proxy The tool proxy
4089 * @return array An array of values representing this type
4091 function serialise_tool_proxy(stdClass $proxy) {
4092 return array(
4093 'id' => $proxy->id,
4094 'name' => $proxy->name,
4095 'description' => get_string('activatetoadddescription', 'mod_lti'),
4096 'urls' => get_tool_proxy_urls($proxy),
4097 'state' => array(
4098 'text' => get_string('pending', 'mod_lti'),
4099 'pending' => true,
4100 'configured' => false,
4101 'rejected' => false,
4102 'unknown' => false
4104 'hascapabilitygroups' => true,
4105 'capabilitygroups' => array(),
4106 'courseid' => 0,
4107 'instanceids' => array(),
4108 'instancecount' => 0
4113 * Loads the cartridge information into the tool type, if the launch url is for a cartridge file
4115 * @param stdClass $type The tool type object to be filled in
4116 * @since Moodle 3.1
4118 function lti_load_type_if_cartridge($type) {
4119 if (!empty($type->lti_toolurl) && lti_is_cartridge($type->lti_toolurl)) {
4120 lti_load_type_from_cartridge($type->lti_toolurl, $type);
4125 * Loads the cartridge information into the new tool, if the launch url is for a cartridge file
4127 * @param stdClass $lti The tools config
4128 * @since Moodle 3.1
4130 function lti_load_tool_if_cartridge($lti) {
4131 if (!empty($lti->toolurl) && lti_is_cartridge($lti->toolurl)) {
4132 lti_load_tool_from_cartridge($lti->toolurl, $lti);
4137 * Determines if the given url is for a IMS basic cartridge
4139 * @param string $url The url to be checked
4140 * @return True if the url is for a cartridge
4141 * @since Moodle 3.1
4143 function lti_is_cartridge($url) {
4144 // If it is empty, it's not a cartridge.
4145 if (empty($url)) {
4146 return false;
4148 // If it has xml at the end of the url, it's a cartridge.
4149 if (preg_match('/\.xml$/', $url)) {
4150 return true;
4152 // Even if it doesn't have .xml, load the url to check if it's a cartridge..
4153 try {
4154 $toolinfo = lti_load_cartridge($url,
4155 array(
4156 "launch_url" => "launchurl"
4159 if (!empty($toolinfo['launchurl'])) {
4160 return true;
4162 } catch (moodle_exception $e) {
4163 return false; // Error loading the xml, so it's not a cartridge.
4165 return false;
4169 * Allows you to load settings for an external tool type from an IMS cartridge.
4171 * @param string $url The URL to the cartridge
4172 * @param stdClass $type The tool type object to be filled in
4173 * @throws moodle_exception if the cartridge could not be loaded correctly
4174 * @since Moodle 3.1
4176 function lti_load_type_from_cartridge($url, $type) {
4177 $toolinfo = lti_load_cartridge($url,
4178 array(
4179 "title" => "lti_typename",
4180 "launch_url" => "lti_toolurl",
4181 "description" => "lti_description",
4182 "icon" => "lti_icon",
4183 "secure_icon" => "lti_secureicon"
4185 array(
4186 "icon_url" => "lti_extension_icon",
4187 "secure_icon_url" => "lti_extension_secureicon"
4190 // If an activity name exists, unset the cartridge name so we don't override it.
4191 if (isset($type->lti_typename)) {
4192 unset($toolinfo['lti_typename']);
4195 // Always prefer cartridge core icons first, then, if none are found, look at the extension icons.
4196 if (empty($toolinfo['lti_icon']) && !empty($toolinfo['lti_extension_icon'])) {
4197 $toolinfo['lti_icon'] = $toolinfo['lti_extension_icon'];
4199 unset($toolinfo['lti_extension_icon']);
4201 if (empty($toolinfo['lti_secureicon']) && !empty($toolinfo['lti_extension_secureicon'])) {
4202 $toolinfo['lti_secureicon'] = $toolinfo['lti_extension_secureicon'];
4204 unset($toolinfo['lti_extension_secureicon']);
4206 // Ensure Custom icons aren't overridden by cartridge params.
4207 if (!empty($type->lti_icon)) {
4208 unset($toolinfo['lti_icon']);
4211 if (!empty($type->lti_secureicon)) {
4212 unset($toolinfo['lti_secureicon']);
4215 foreach ($toolinfo as $property => $value) {
4216 $type->$property = $value;
4221 * Allows you to load in the configuration for an external tool from an IMS cartridge.
4223 * @param string $url The URL to the cartridge
4224 * @param stdClass $lti LTI object
4225 * @throws moodle_exception if the cartridge could not be loaded correctly
4226 * @since Moodle 3.1
4228 function lti_load_tool_from_cartridge($url, $lti) {
4229 $toolinfo = lti_load_cartridge($url,
4230 array(
4231 "title" => "name",
4232 "launch_url" => "toolurl",
4233 "secure_launch_url" => "securetoolurl",
4234 "description" => "intro",
4235 "icon" => "icon",
4236 "secure_icon" => "secureicon"
4238 array(
4239 "icon_url" => "extension_icon",
4240 "secure_icon_url" => "extension_secureicon"
4243 // If an activity name exists, unset the cartridge name so we don't override it.
4244 if (isset($lti->name)) {
4245 unset($toolinfo['name']);
4248 // Always prefer cartridge core icons first, then, if none are found, look at the extension icons.
4249 if (empty($toolinfo['icon']) && !empty($toolinfo['extension_icon'])) {
4250 $toolinfo['icon'] = $toolinfo['extension_icon'];
4252 unset($toolinfo['extension_icon']);
4254 if (empty($toolinfo['secureicon']) && !empty($toolinfo['extension_secureicon'])) {
4255 $toolinfo['secureicon'] = $toolinfo['extension_secureicon'];
4257 unset($toolinfo['extension_secureicon']);
4259 foreach ($toolinfo as $property => $value) {
4260 $lti->$property = $value;
4265 * Search for a tag within an XML DOMDocument
4267 * @param string $url The url of the cartridge to be loaded
4268 * @param array $map The map of tags to keys in the return array
4269 * @param array $propertiesmap The map of properties to keys in the return array
4270 * @return array An associative array with the given keys and their values from the cartridge
4271 * @throws moodle_exception if the cartridge could not be loaded correctly
4272 * @since Moodle 3.1
4274 function lti_load_cartridge($url, $map, $propertiesmap = array()) {
4275 global $CFG;
4276 require_once($CFG->libdir. "/filelib.php");
4278 $curl = new curl();
4279 $response = $curl->get($url);
4281 // TODO MDL-46023 Replace this code with a call to the new library.
4282 $origerrors = libxml_use_internal_errors(true);
4283 $origentity = libxml_disable_entity_loader(true);
4284 libxml_clear_errors();
4286 $document = new DOMDocument();
4287 @$document->loadXML($response, LIBXML_DTDLOAD | LIBXML_DTDATTR);
4289 $cartridge = new DomXpath($document);
4291 $errors = libxml_get_errors();
4293 libxml_clear_errors();
4294 libxml_use_internal_errors($origerrors);
4295 libxml_disable_entity_loader($origentity);
4297 if (count($errors) > 0) {
4298 $message = 'Failed to load cartridge.';
4299 foreach ($errors as $error) {
4300 $message .= "\n" . trim($error->message, "\n\r\t .") . " at line " . $error->line;
4302 throw new moodle_exception('errorreadingfile', '', '', $url, $message);
4305 $toolinfo = array();
4306 foreach ($map as $tag => $key) {
4307 $value = get_tag($tag, $cartridge);
4308 if ($value) {
4309 $toolinfo[$key] = $value;
4312 if (!empty($propertiesmap)) {
4313 foreach ($propertiesmap as $property => $key) {
4314 $value = get_tag("property", $cartridge, $property);
4315 if ($value) {
4316 $toolinfo[$key] = $value;
4321 return $toolinfo;
4325 * Search for a tag within an XML DOMDocument
4327 * @param stdClass $tagname The name of the tag to search for
4328 * @param XPath $xpath The XML to find the tag in
4329 * @param XPath $attribute The attribute to search for (if we should search for a child node with the given
4330 * value for the name attribute
4331 * @since Moodle 3.1
4333 function get_tag($tagname, $xpath, $attribute = null) {
4334 if ($attribute) {
4335 $result = $xpath->query('//*[local-name() = \'' . $tagname . '\'][@name="' . $attribute . '"]');
4336 } else {
4337 $result = $xpath->query('//*[local-name() = \'' . $tagname . '\']');
4339 if ($result->length > 0) {
4340 return $result->item(0)->nodeValue;
4342 return null;
4346 * Create a new access token.
4348 * @param int $typeid Tool type ID
4349 * @param string[] $scopes Scopes permitted for new token
4351 * @return stdClass Access token
4353 function lti_new_access_token($typeid, $scopes) {
4354 global $DB;
4356 // Make sure the token doesn't exist (even if it should be almost impossible with the random generation).
4357 $numtries = 0;
4358 do {
4359 $numtries ++;
4360 $generatedtoken = md5(uniqid(rand(), 1));
4361 if ($numtries > 5) {
4362 throw new moodle_exception('Failed to generate LTI access token');
4364 } while ($DB->record_exists('lti_access_tokens', array('token' => $generatedtoken)));
4365 $newtoken = new stdClass();
4366 $newtoken->typeid = $typeid;
4367 $newtoken->scope = json_encode(array_values($scopes));
4368 $newtoken->token = $generatedtoken;
4370 $newtoken->timecreated = time();
4371 $newtoken->validuntil = $newtoken->timecreated + LTI_ACCESS_TOKEN_LIFE;
4372 $newtoken->lastaccess = null;
4374 $DB->insert_record('lti_access_tokens', $newtoken);
4376 return $newtoken;