2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 // This file is part of BasicLTI4Moodle
19 // BasicLTI4Moodle is an IMS BasicLTI (Basic Learning Tools for Interoperability)
20 // consumer for Moodle 1.9 and Moodle 2.0. BasicLTI is a IMS Standard that allows web
21 // based learning tools to be easily integrated in LMS as native ones. The IMS BasicLTI
22 // specification is part of the IMS standard Common Cartridge 1.1 Sakai and other main LMS
23 // are already supporting or going to support BasicLTI. This project Implements the consumer
24 // for Moodle. Moodle is a Free Open source Learning Management System by Martin Dougiamas.
25 // BasicLTI4Moodle is a project iniciated and leaded by Ludo(Marc Alier) and Jordi Piguillem
26 // at the GESSI research group at UPC.
27 // SimpleLTI consumer for Moodle is an implementation of the early specification of LTI
28 // by Charles Severance (Dr Chuck) htp://dr-chuck.com , developed by Jordi Piguillem in a
29 // Google Summer of Code 2008 project co-mentored by Charles Severance and Marc Alier.
31 // BasicLTI4Moodle is copyright 2009 by Marc Alier Forment, Jordi Piguillem and Nikolas Galanis
32 // of the Universitat Politecnica de Catalunya http://www.upc.edu
33 // Contact info: Marc Alier Forment granludo @ gmail.com or marc.alier @ upc.edu.
36 * This file contains the library of functions and constants for the lti module
39 * @copyright 2009 Marc Alier, Jordi Piguillem, Nikolas Galanis
41 * @copyright 2009 Universitat Politecnica de Catalunya http://www.upc.edu
43 * @author Jordi Piguillem
44 * @author Nikolas Galanis
45 * @author Chris Scribner
46 * @copyright 2015 Vital Source Technologies http://vitalsource.com
47 * @author Stephen Vickers
48 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
51 defined('MOODLE_INTERNAL') ||
die;
53 // TODO: Switch to core oauthlib once implemented - MDL-30149.
54 use moodle\mod\lti
as lti
;
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.
110 function lti_get_jwt_message_type_mapping() {
112 'basic-lti-launch-request' => 'LtiResourceLinkRequest',
113 'ContentItemSelectionRequest' => 'LtiDeepLinkingRequest',
114 'LtiDeepLinkingResponse' => 'ContentItemSelection',
119 * Return the mapping for standard message parameters to JWT claim.
123 function lti_get_jwt_claim_mapping() {
125 'accept_copy_advice' => [
127 'group' => 'deep_linking_settings',
128 'claim' => 'accept_copy_advice',
131 'accept_media_types' => [
133 'group' => 'deep_linking_settings',
134 'claim' => 'accept_media_types',
137 'accept_multiple' => [
139 'group' => 'deep_linking_settings',
140 'claim' => 'accept_multiple',
143 'accept_presentation_document_targets' => [
145 'group' => 'deep_linking_settings',
146 'claim' => 'accept_presentation_document_targets',
151 'group' => 'deep_linking_settings',
152 'claim' => 'accept_types',
155 'accept_unsigned' => [
157 'group' => 'deep_linking_settings',
158 'claim' => 'accept_unsigned',
163 'group' => 'deep_linking_settings',
164 'claim' => 'auto_create',
169 'group' => 'deep_linking_settings',
170 'claim' => 'can_confirm',
173 'content_item_return_url' => [
175 'group' => 'deep_linking_settings',
176 'claim' => 'deep_link_return_url',
182 'claim' => 'content_items',
187 'group' => 'deep_linking_settings',
193 'group' => 'deep_linking_settings',
199 'group' => 'deep_linking_settings',
218 'claim' => 'errormsg',
224 'claim' => 'errorlog',
229 'group' => 'context',
235 'group' => 'context',
241 'group' => 'context',
247 'group' => 'context',
251 'lis_course_offering_sourcedid' => [
254 'claim' => 'course_offering_sourcedid',
257 'lis_course_section_sourcedid' => [
260 'claim' => 'course_section_sourcedid',
263 'launch_presentation_css_url' => [
265 'group' => 'launch_presentation',
266 'claim' => 'css_url',
269 'launch_presentation_document_target' => [
271 'group' => 'launch_presentation',
272 'claim' => 'document_target',
275 'launch_presentation_height' => [
277 'group' => 'launch_presentation',
281 'launch_presentation_locale' => [
283 'group' => 'launch_presentation',
287 'launch_presentation_return_url' => [
289 'group' => 'launch_presentation',
290 'claim' => 'return_url',
293 'launch_presentation_width' => [
295 'group' => 'launch_presentation',
299 'lis_person_contact_email_primary' => [
305 'lis_person_name_family' => [
308 'claim' => 'family_name',
311 'lis_person_name_full' => [
317 'lis_person_name_given' => [
320 'claim' => 'given_name',
323 'lis_person_sourcedid' => [
326 'claim' => 'person_sourcedid',
338 'claim' => 'picture',
347 'role_scope_mentor' => [
350 'claim' => 'role_scope_mentor',
356 'claim' => 'deployment_id',
359 'lti_message_type' => [
362 'claim' => 'message_type',
368 'claim' => 'version',
371 'resource_link_description' => [
373 'group' => 'resource_link',
374 'claim' => 'description',
377 'resource_link_id' => [
379 'group' => 'resource_link',
383 'resource_link_title' => [
385 'group' => 'resource_link',
389 'tool_consumer_info_product_family_code' => [
391 'group' => 'tool_platform',
392 'claim' => 'family_code',
395 'tool_consumer_info_version' => [
397 'group' => 'tool_platform',
398 'claim' => 'version',
401 'tool_consumer_instance_contact_email' => [
403 'group' => 'tool_platform',
404 'claim' => 'contact_email',
407 'tool_consumer_instance_description' => [
409 'group' => 'tool_platform',
410 'claim' => 'description',
413 'tool_consumer_instance_guid' => [
415 'group' => 'tool_platform',
419 'tool_consumer_instance_name' => [
421 'group' => 'tool_platform',
425 'tool_consumer_instance_url' => [
427 'group' => 'tool_platform',
431 'custom_context_memberships_url' => [
433 'group' => 'namesroleservice',
434 'claim' => 'context_memberships_url',
437 'custom_context_memberships_versions' => [
439 'group' => 'namesroleservice',
440 'claim' => 'service_versions',
443 'custom_gradebookservices_scope' => [
445 'group' => 'endpoint',
449 'custom_lineitems_url' => [
451 'group' => 'endpoint',
452 'claim' => 'lineitems',
455 'custom_lineitem_url' => [
457 'group' => 'endpoint',
458 'claim' => 'lineitem',
461 'custom_results_url' => [
463 'group' => 'endpoint',
464 'claim' => 'results',
467 'custom_result_url' => [
469 'group' => 'endpoint',
473 'custom_scores_url' => [
475 'group' => 'endpoint',
479 'custom_score_url' => [
481 'group' => 'endpoint',
485 'lis_outcome_service_url' => [
487 'group' => 'basicoutcomesservice',
488 'claim' => 'lis_outcome_service_url',
491 'lis_result_sourcedid' => [
493 'group' => 'basicoutcomesservice',
494 'claim' => 'lis_result_sourcedid',
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
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
);
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)
525 function lti_get_launch_data($instance, $nonce = '') {
526 global $PAGE, $CFG, $USER;
528 $tool = lti_get_instance_type($instance);
531 $ltiversion = $tool->ltiversion
;
534 $ltiversion = LTI_VERSION_1
;
538 $typeconfig = lti_get_type_config($typeid);
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
;
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'];
566 if (!empty($instance->password
)) {
567 $secret = $instance->password
;
568 } else if (!empty($typeconfig['password'])) {
569 $secret = $typeconfig['password'];
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);
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);
602 $requestparams = lti_build_request_lti2($tool, $allparams);
604 $requestparams = $allparams;
606 $requestparams = array_merge($requestparams, lti_build_standard_message($instance, $orgid, $ltiversion));
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);
629 switch($launchcontainer) {
630 case LTI_LAUNCH_CONTAINER_EMBED
:
631 case LTI_LAUNCH_CONTAINER_EMBED_NO_BLOCKS
:
634 case LTI_LAUNCH_CONTAINER_REPLACE_MOODLE_WINDOW
:
637 case LTI_LAUNCH_CONTAINER_WINDOW
:
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,
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);
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]);
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);
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);
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
772 function lti_get_organizationid($typeconfig) {
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'];
780 return md5(get_site_identifier());
783 return $typeconfig['organizationid'];
789 * @param int $instanceid
791 * @param string $servicesalt
792 * @param null|int $typeid
793 * @param null|int $launchid
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;
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;
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) {
833 if (empty($instance->cmid
)) {
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
,
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';
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();
885 if (!empty($CFG->mod_lti_forcessl
)) {
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') {
960 $ltiversion = LTI_VERSION_1
;
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') {
980 $requestparams = array();
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;
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));
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.
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);
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));
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 = '') {
1090 $tool = lti_get_type($id);
1091 // Validate parameters.
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);
1112 if (isset($tool->toolproxyid
)) {
1114 $toolproxy = lti_get_tool_proxy($tool->toolproxyid
);
1115 $key = $toolproxy->guid
;
1116 $secret = $toolproxy->secret
;
1118 $islti13 = $tool->ltiversion
=== LTI_VERSION_1P3
;
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']);
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.
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.
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,
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);
1207 // Media types. Set to ltilink by default if empty.
1208 if (empty($mediatypes)) {
1210 'application/vnd.ims.lti.v1.ltilink',
1213 $requestparams['accept_media_types'] = implode(',', $mediatypes);
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 = [
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;
1239 $signedparams = lti_sign_parameters($requestparams, $toolurlout, 'POST', $key, $secret);
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.
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;
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.
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
;
1299 if (!empty($typeconfig['resourcekey'])) {
1300 $key = $typeconfig['resourcekey'];
1304 if (!empty($typeconfig['password'])) {
1305 $secret = $typeconfig['password'];
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();
1322 $server->verify_request($request);
1323 } catch (lti\OAuthException
$e) {
1324 throw new lti\
OAuthException("OAuth signature failed: " . $e->getMessage());
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);
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);
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.
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
);
1418 throw new moodle_exception('Invalid public key type');
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.
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'.",
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');
1468 if (!empty($items->{'@graph'})) {
1469 $item = $items->{'@graph'}[0];
1470 $typeconfig = lti_get_type_type_config($tool->id
);
1472 $config = new stdClass();
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);
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;
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
;
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);
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) {
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';
1578 $newitem->{'@type'} = 'ContentItem';
1579 $newitem->mediaType
= 'text/html';
1582 $newitem->{'@type'} = 'FileItem';
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) {
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)) {
1658 <div id=\"{$id}_tools_container\" style=\"margin-top:.5em;margin-bottom:.5em\">
1659 <table id=\"{$id}_tools\">
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',
1680 'sesskey' => sesskey(),
1683 $ref = $type->baseurl
;
1685 $baseurl = new \
moodle_url('/mod/lti/toolssettings.php', array(
1686 'action' => 'accept',
1688 'sesskey' => sesskey(),
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
) {
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'));
1735 <td align=\"center\">
1736 {$accepthtml}{$updatehtml}{$deletehtml}
1741 $html .= '</table></div>';
1743 $html .= get_string('no_' . $id, 'lti');
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) {
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');
1767 <div id
="{$id}_tool_proxies_container" style
="margin-top: 0.5em; margin-bottom: 0.5em">
1768 <table id
="{$id}_tool_proxies">
1771 <th
>{$typename}</th
>
1773 <th
>{$createdon}</th
>
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(),
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
) {
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'));
1828 {$toolproxy->regurl
}
1834 {$accepthtml}{$updatehtml}{$deletehtml}
1839 $html .= '</table></div>';
1841 $html = get_string('no_' . $id, 'lti');
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)) {
1858 if (!empty($tool->enabledcapability
)) {
1859 $enabledcapabilities = explode("\n", $tool->enabledcapability
);
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)) {
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;]/"?
1902 foreach ($lines as $line) {
1903 $pos = strpos($line, '=');
1904 if ( $pos === false ||
$pos < 1 ) {
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;
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) {
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;
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;
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];
1968 if (substr($val, 0, 1) != '$') {
1969 $value = $params[$val];
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);
1978 $value = lti_calculate_custom_parameter($value1);
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) {
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;
2009 case 'Moodle.Person.userGroupIds':
2010 return implode(",", groups_get_user_groups($COURSE->id
, $USER->id
)[0]);
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) {
2025 $key = core_text
::strtolower(trim($key));
2026 foreach (str_split($key) as $ch) {
2027 if ( ($ch >= 'a' && $ch <= 'z') ||
($ch >= '0' && $ch <= '9') ) {
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) {
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');
2061 array_push($roles, 'Learner');
2064 $context = context_module
::instance($cmid);
2066 if (has_capability('mod/lti:manage', $context)) {
2067 array_push($roles, 'Instructor');
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'));
2077 array_push($roles, 'urn:lti:sysrole:ims/lis/Administrator', 'urn:lti:instrole:ims/lis/Administrator');
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) {
2096 $query = "SELECT name, value
2097 FROM {lti_types_config}
2098 WHERE typeid = :typeid1
2100 SELECT 'toolurl' AS name, baseurl AS value
2104 SELECT 'icon' AS name, icon AS value
2108 SELECT 'secureicon' AS name, secureicon AS value
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
;
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) {
2138 $statefilter = 'AND state = :state';
2141 if ($courseid && $courseid != $SITE->id
) {
2142 $coursefilter = 'OR course = :courseid';
2147 WHERE tooldomain = :tooldomain
2148 AND (course = :siteid $coursefilter)
2151 return $DB->get_records_sql($query, array(
2152 'courseid' => $courseid,
2153 'siteid' => $SITE->id
,
2154 'tooldomain' => $domain,
2160 * Returns all basicLTI tools configured by the administrator
2162 * @param int $course
2166 function lti_filter_get_types($course) {
2169 if (!empty($course)) {
2170 $where = "WHERE t.course = :course";
2171 $params = array('course' => $course);
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
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
2189 function lti_filter_tool_types(array $tools, $state) {
2191 foreach ($tools as $key => $tool) {
2192 if ($tool->state
== $state) {
2193 $return[$key] = $tool;
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) {
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');
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) {
2225 $coursecond = implode(" OR ", $courseconds);
2228 WHERE coursevisible $coursevisiblesql
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() {
2243 $admintypes = lti_get_lti_types_by_course($COURSE->id
);
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;
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) {
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'));
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
));
2294 function lti_get_domain_from_url($url) {
2297 if (preg_match(LTI_URL_DOMAIN_REGEX
, $url, $matches)) {
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'];
2339 function lti_get_best_tool_by_url($url, $tools, $courseid = null) {
2340 if (count($tools) === 0) {
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
) {
2375 }, (object)array('_matchscore' => -1));
2377 // None of the tools are suitable for this URL.
2378 if ($bestmatch->_matchscore
<= 0) {
2385 function lti_get_shared_secrets_by_key($key) {
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
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
2407 SELECT password AS value
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
;
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.
2424 * Delete a Basic LTI configuration
2426 * @param int $id Configuration id
2428 function lti_delete_type($id) {
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) {
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);
2465 * Generates some of the tool configuration based on the instance details
2469 * @return object configuration
2472 function lti_get_type_config_from_instance($id) {
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'];
2503 * Generates some of the tool configuration based on the admin configuration details
2507 * @return stdClass Configuration details
2509 function lti_get_type_type_config($id) {
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'];
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];
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
;
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) {
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
);
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');
2732 $sql = "SELECT DISTINCT course
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);
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_') {
2788 $record = new \
StdClass();
2789 $record->typeid
= $id;
2790 $record->name
= $fieldname;
2791 $record->value
= $value;
2793 lti_add_config($record);
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
2809 function lti_filter_tool_proxy_types(array $toolproxies, $state) {
2811 foreach ($toolproxies as $key => $toolproxy) {
2812 if ($toolproxy->state
== $state) {
2813 $return[$key] = $toolproxy;
2820 * Get the tool proxy instance given its GUID
2822 * @param string $toolproxyguid Tool proxy GUID value
2826 function lti_get_tool_proxy_from_guid($toolproxyguid) {
2829 $toolproxy = $DB->get_record('lti_tool_proxies', array('guid' => $toolproxyguid));
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) {
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
2856 * @return mixed Tool Proxy details
2858 function lti_get_tool_proxy($id) {
2861 $toolproxy = $DB->get_record('lti_tool_proxies', array('id' => $id));
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) {
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]);
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
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
);
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) {
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
);
2929 $toolproxy->capabilityoffered
= implode("\n", array_keys(lti_get_capabilities()));
2931 if (isset($config->lti_services
)) {
2932 $toolproxy->serviceoffered
= implode("\n", $config->lti_services
);
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);
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);
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) {
2973 $toolproxy->timemodified
= time();
2974 $id = $DB->update_record('lti_tool_proxies', $toolproxy);
2980 * Delete a Tool Proxy
2982 * @param int $id Tool Proxy id
2984 function lti_delete_tool_proxy($id) {
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) {
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) {
3017 $old = $DB->get_record('lti_types_config', array('typeid' => $config->typeid
, 'name' => $config->name
));
3020 $config->id
= $old->id
;
3021 $return = $DB->update_record('lti_types_config', $config);
3023 $return = $DB->insert_record('lti_types_config', $config);
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) {
3040 $settings = array();
3041 if ($toolproxyid > 0) {
3042 $settingsstr = $DB->get_field('lti_tool_settings', 'settings', array('toolproxyid' => $toolproxyid,
3043 'course' => $courseid, 'coursemoduleid' => $instanceid));
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);
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) {
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));
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()));
3076 $record = new \
stdClass();
3077 if ($toolproxyid > 0) {
3078 $record->toolproxyid
= $toolproxyid;
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) {
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();
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 = '') {
3131 if (empty($typeid)) {
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);
3157 if (empty($nonce)) {
3158 $nonce = bin2hex(openssl_random_pseudo_bytes(10));
3160 $claimmapping = lti_get_jwt_claim_mapping();
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);
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;
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;
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) {
3220 $parts = explode('.', $jwtparam);
3221 $ok = (count($parts) === 3);
3223 $payload = JWT
::urlsafeB64Decode($parts[1]);
3224 $claims = json_decode($payload, true);
3225 $ok = !is_null($claims) && !empty($claims['iss']);
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'];
3241 $claim .= $mapping['group'];
3243 if (isset($claims[$claim])) {
3245 if (empty($mapping['group'])) {
3246 $value = $claims[$claim];
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);
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']];
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)
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\"";
3315 $r .= "<input type=\"hidden\" name=\"{$key}\"";
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";
3331 $r .= " ele.style.display = \"block\";\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 <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 .= " <br/>\n";
3353 $r .= " <script type=\"text/javascript\"> \n" .
3355 " document.ltiLaunchForm.submit(); \n" .
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
3374 function lti_initiate_login($courseid, $id, $instance, $config, $messagetype = 'basic-lti-launch-request', $title = '',
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";
3393 $r .= "<script type=\"text/javascript\">\n" .
3395 "document.ltiInitiateLoginForm.submit();\n" .
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) {
3415 if (!empty($instance)) {
3416 $endpoint = !empty($instance->toolurl
) ?
$instance->toolurl
: $config->lti_toolurl
;
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;
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
;
3442 function lti_get_type($typeid) {
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'];
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() {
3479 return (stripos($CFG->wwwroot
, 'https://') === 0);
3482 function lti_ensure_url_is_https($url) {
3483 if (!strstr($url, '://')) {
3484 $url = 'https://' . $url;
3486 // If the URL starts with http, replace with https.
3487 if (stripos($url, 'http://') === 0) {
3488 $url = 'https://' . substr($url, 7);
3496 * Determines if we should try to log the request
3498 * @param string $rawbody
3501 function lti_should_log_request($rawbody) {
3504 if (empty($CFG->mod_lti_log_users
)) {
3508 $logusers = explode(',', $CFG->mod_lti_log_users
);
3509 if (empty($logusers)) {
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')) {
3521 $requestuserid = clean_param((string) $node, PARAM_INT
);
3522 } else if ($node = $xml->xpath('//lti:sourcedId')) {
3524 $resultjson = json_decode((string) $node);
3525 $requestuserid = clean_param($resultjson->data
->userid
, PARAM_INT
);
3527 } catch (Exception
$e) {
3531 if (empty($requestuserid) or !in_array($requestuserid, $logusers)) {
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'))) {
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);
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) {
3596 if (empty($instance->typeid
)) {
3597 $tool = lti_get_tool_by_url_match($instance->toolurl
, $instance->course
);
3599 $typeid = $tool->id
;
3602 $typeid = $instance->typeid
;
3604 if (!empty($typeid)) {
3605 return lti_get_type_config($typeid);
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) {
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();
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) {
3709 $classname = "\\ltiservice_{$servicename}\\local\\service\\{$servicename}";
3710 if (class_exists($classname)) {
3711 $service = new $classname();
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) {
3729 foreach ($services as $aservice) {
3730 foreach ($aservice->get_resources() as $resource) {
3731 if ($resource->get_id() === $resourceid) {
3732 $service = $aservice;
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();
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);
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);
3790 * Converts an ID to a fully-qualified ID
3792 * @param array $contexts
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];
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) {
3820 $iconurl = $type->secureicon
;
3822 if (empty($iconurl)) {
3823 $iconurl = $type->icon
;
3826 if (empty($iconurl)) {
3827 $iconurl = $OUTPUT->image_url('icon', 'lti')->out();
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'));
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'));
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
));
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);
3885 'icon' => get_tool_type_icon_url($type),
3886 'edit' => get_tool_type_edit_url($type),
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();
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) {
3914 'icon' => $OUTPUT->image_url('icon', 'lti')->out(),
3915 'edit' => get_tool_proxy_edit_url($proxy),
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;
3932 $isrejected = false;
3934 switch ($type->state
) {
3935 case LTI_TOOL_STATE_CONFIGURED
:
3936 $state = get_string('active', 'mod_lti');
3937 $isconfigured = true;
3939 case LTI_TOOL_STATE_PENDING
:
3940 $state = get_string('pending', 'mod_lti');
3943 case LTI_TOOL_STATE_REJECTED
:
3944 $state = get_string('rejected', 'mod_lti');
3948 $state = get_string('unknownstate', 'mod_lti');
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) {
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();
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);
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) {
4014 if (!$hascourse && preg_match('/^CourseSection/', $capability)) {
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');
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) {
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) {
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
);
4064 $description = get_string('editdescription', 'mod_lti');
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) {
4094 'name' => $proxy->name
,
4095 'description' => get_string('activatetoadddescription', 'mod_lti'),
4096 'urls' => get_tool_proxy_urls($proxy),
4098 'text' => get_string('pending', 'mod_lti'),
4100 'configured' => false,
4101 'rejected' => false,
4104 'hascapabilitygroups' => true,
4105 'capabilitygroups' => array(),
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
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
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
4143 function lti_is_cartridge($url) {
4144 // If it is empty, it's not a cartridge.
4148 // If it has xml at the end of the url, it's a cartridge.
4149 if (preg_match('/\.xml$/', $url)) {
4152 // Even if it doesn't have .xml, load the url to check if it's a cartridge..
4154 $toolinfo = lti_load_cartridge($url,
4156 "launch_url" => "launchurl"
4159 if (!empty($toolinfo['launchurl'])) {
4162 } catch (moodle_exception
$e) {
4163 return false; // Error loading the xml, so it's not a cartridge.
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
4176 function lti_load_type_from_cartridge($url, $type) {
4177 $toolinfo = lti_load_cartridge($url,
4179 "title" => "lti_typename",
4180 "launch_url" => "lti_toolurl",
4181 "description" => "lti_description",
4182 "icon" => "lti_icon",
4183 "secure_icon" => "lti_secureicon"
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
4228 function lti_load_tool_from_cartridge($url, $lti) {
4229 $toolinfo = lti_load_cartridge($url,
4232 "launch_url" => "toolurl",
4233 "secure_launch_url" => "securetoolurl",
4234 "description" => "intro",
4236 "secure_icon" => "secureicon"
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
4274 function lti_load_cartridge($url, $map, $propertiesmap = array()) {
4276 require_once($CFG->libdir
. "/filelib.php");
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);
4309 $toolinfo[$key] = $value;
4312 if (!empty($propertiesmap)) {
4313 foreach ($propertiesmap as $property => $key) {
4314 $value = get_tag("property", $cartridge, $property);
4316 $toolinfo[$key] = $value;
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
4333 function get_tag($tagname, $xpath, $attribute = null) {
4335 $result = $xpath->query('//*[local-name() = \'' . $tagname . '\'][@name="' . $attribute . '"]');
4337 $result = $xpath->query('//*[local-name() = \'' . $tagname . '\']');
4339 if ($result->length
> 0) {
4340 return $result->item(0)->nodeValue
;
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) {
4356 // Make sure the token doesn't exist (even if it should be almost impossible with the random generation).
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);