2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 // This file is part of BasicLTI4Moodle
19 // BasicLTI4Moodle is an IMS BasicLTI (Basic Learning Tools for Interoperability)
20 // consumer for Moodle 1.9 and Moodle 2.0. BasicLTI is a IMS Standard that allows web
21 // based learning tools to be easily integrated in LMS as native ones. The IMS BasicLTI
22 // specification is part of the IMS standard Common Cartridge 1.1 Sakai and other main LMS
23 // are already supporting or going to support BasicLTI. This project Implements the consumer
24 // for Moodle. Moodle is a Free Open source Learning Management System by Martin Dougiamas.
25 // BasicLTI4Moodle is a project iniciated and leaded by Ludo(Marc Alier) and Jordi Piguillem
26 // at the GESSI research group at UPC.
27 // SimpleLTI consumer for Moodle is an implementation of the early specification of LTI
28 // by Charles Severance (Dr Chuck) htp://dr-chuck.com , developed by Jordi Piguillem in a
29 // Google Summer of Code 2008 project co-mentored by Charles Severance and Marc Alier.
31 // BasicLTI4Moodle is copyright 2009 by Marc Alier Forment, Jordi Piguillem and Nikolas Galanis
32 // of the Universitat Politecnica de Catalunya http://www.upc.edu
33 // Contact info: Marc Alier Forment granludo @ gmail.com or marc.alier @ upc.edu.
36 * This file contains the library of functions and constants for the lti module
39 * @copyright 2009 Marc Alier, Jordi Piguillem, Nikolas Galanis
41 * @copyright 2009 Universitat Politecnica de Catalunya http://www.upc.edu
43 * @author Jordi Piguillem
44 * @author Nikolas Galanis
45 * @author Chris Scribner
46 * @copyright 2015 Vital Source Technologies http://vitalsource.com
47 * @author Stephen Vickers
48 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
51 defined('MOODLE_INTERNAL') ||
die;
53 // TODO: Switch to core oauthlib once implemented - MDL-30149.
54 use moodle\mod\lti
as lti
;
56 require_once($CFG->dirroot
.'/mod/lti/OAuth.php');
57 require_once($CFG->libdir
.'/weblib.php');
58 require_once($CFG->dirroot
. '/course/modlib.php');
59 require_once($CFG->dirroot
. '/mod/lti/TrivialStore.php');
61 define('LTI_URL_DOMAIN_REGEX', '/(?:https?:\/\/)?(?:www\.)?([^\/]+)(?:\/|$)/i');
63 define('LTI_LAUNCH_CONTAINER_DEFAULT', 1);
64 define('LTI_LAUNCH_CONTAINER_EMBED', 2);
65 define('LTI_LAUNCH_CONTAINER_EMBED_NO_BLOCKS', 3);
66 define('LTI_LAUNCH_CONTAINER_WINDOW', 4);
67 define('LTI_LAUNCH_CONTAINER_REPLACE_MOODLE_WINDOW', 5);
69 define('LTI_TOOL_STATE_ANY', 0);
70 define('LTI_TOOL_STATE_CONFIGURED', 1);
71 define('LTI_TOOL_STATE_PENDING', 2);
72 define('LTI_TOOL_STATE_REJECTED', 3);
73 define('LTI_TOOL_PROXY_TAB', 4);
75 define('LTI_TOOL_PROXY_STATE_CONFIGURED', 1);
76 define('LTI_TOOL_PROXY_STATE_PENDING', 2);
77 define('LTI_TOOL_PROXY_STATE_ACCEPTED', 3);
78 define('LTI_TOOL_PROXY_STATE_REJECTED', 4);
80 define('LTI_SETTING_NEVER', 0);
81 define('LTI_SETTING_ALWAYS', 1);
82 define('LTI_SETTING_DELEGATE', 2);
84 define('LTI_COURSEVISIBLE_NO', 0);
85 define('LTI_COURSEVISIBLE_PRECONFIGURED', 1);
86 define('LTI_COURSEVISIBLE_ACTIVITYCHOOSER', 2);
88 define('LTI_VERSION_1', 'LTI-1p0');
89 define('LTI_VERSION_2', 'LTI-2p0');
92 * Return the launch data required for opening the external tool.
94 * @param stdClass $instance the external tool activity settings
95 * @return array the endpoint URL and parameters (including the signature)
98 function lti_get_launch_data($instance) {
101 if (empty($instance->typeid
)) {
102 $tool = lti_get_tool_by_url_match($instance->toolurl
, $instance->course
);
106 $tool = lti_get_tool_by_url_match($instance->securetoolurl
, $instance->course
);
114 $typeid = $instance->typeid
;
115 $tool = lti_get_type($typeid);
119 $typeconfig = lti_get_type_config($typeid);
121 // There is no admin configuration for this tool. Use configuration in the lti instance record plus some defaults.
122 $typeconfig = (array)$instance;
124 $typeconfig['sendname'] = $instance->instructorchoicesendname
;
125 $typeconfig['sendemailaddr'] = $instance->instructorchoicesendemailaddr
;
126 $typeconfig['customparameters'] = $instance->instructorcustomparameters
;
127 $typeconfig['acceptgrades'] = $instance->instructorchoiceacceptgrades
;
128 $typeconfig['allowroster'] = $instance->instructorchoiceallowroster
;
129 $typeconfig['forcessl'] = '0';
132 // Default the organizationid if not specified.
133 if (empty($typeconfig['organizationid'])) {
134 $urlparts = parse_url($CFG->wwwroot
);
136 $typeconfig['organizationid'] = $urlparts['host'];
139 if (isset($tool->toolproxyid
)) {
140 $toolproxy = lti_get_tool_proxy($tool->toolproxyid
);
141 $key = $toolproxy->guid
;
142 $secret = $toolproxy->secret
;
145 if (!empty($instance->resourcekey
)) {
146 $key = $instance->resourcekey
;
147 } else if (!empty($typeconfig['resourcekey'])) {
148 $key = $typeconfig['resourcekey'];
152 if (!empty($instance->password
)) {
153 $secret = $instance->password
;
154 } else if (!empty($typeconfig['password'])) {
155 $secret = $typeconfig['password'];
161 $endpoint = !empty($instance->toolurl
) ?
$instance->toolurl
: $typeconfig['toolurl'];
162 $endpoint = trim($endpoint);
164 // If the current request is using SSL and a secure tool URL is specified, use it.
165 if (lti_request_is_using_ssl() && !empty($instance->securetoolurl
)) {
166 $endpoint = trim($instance->securetoolurl
);
169 // If SSL is forced, use the secure tool url if specified. Otherwise, make sure https is on the normal launch URL.
170 if (isset($typeconfig['forcessl']) && ($typeconfig['forcessl'] == '1')) {
171 if (!empty($instance->securetoolurl
)) {
172 $endpoint = trim($instance->securetoolurl
);
175 $endpoint = lti_ensure_url_is_https($endpoint);
177 if (!strstr($endpoint, '://')) {
178 $endpoint = 'http://' . $endpoint;
182 $orgid = $typeconfig['organizationid'];
184 $course = $PAGE->course
;
185 $islti2 = isset($tool->toolproxyid
);
186 $allparams = lti_build_request($instance, $typeconfig, $course, $typeid, $islti2);
188 $requestparams = lti_build_request_lti2($tool, $allparams);
190 $requestparams = $allparams;
192 $requestparams = array_merge($requestparams, lti_build_standard_request($instance, $orgid, $islti2));
194 if (isset($typeconfig['customparameters'])) {
195 $customstr = $typeconfig['customparameters'];
197 $requestparams = array_merge($requestparams, lti_build_custom_parameters($toolproxy, $tool, $instance, $allparams, $customstr,
198 $instance->instructorcustomparameters
, $islti2));
200 $launchcontainer = lti_get_launch_container($instance, $typeconfig);
201 $returnurlparams = array('course' => $course->id
,
202 'launch_container' => $launchcontainer,
203 'instanceid' => $instance->id
,
204 'sesskey' => sesskey());
206 // Add the return URL. We send the launch container along to help us avoid frames-within-frames when the user returns.
207 $url = new \
moodle_url('/mod/lti/return.php', $returnurlparams);
208 $returnurl = $url->out(false);
210 if (isset($typeconfig['forcessl']) && ($typeconfig['forcessl'] == '1')) {
211 $returnurl = lti_ensure_url_is_https($returnurl);
215 switch($launchcontainer) {
216 case LTI_LAUNCH_CONTAINER_EMBED
:
217 case LTI_LAUNCH_CONTAINER_EMBED_NO_BLOCKS
:
220 case LTI_LAUNCH_CONTAINER_REPLACE_MOODLE_WINDOW
:
223 case LTI_LAUNCH_CONTAINER_WINDOW
:
227 if (!empty($target)) {
228 $requestparams['launch_presentation_document_target'] = $target;
231 $requestparams['launch_presentation_return_url'] = $returnurl;
233 // Allow request params to be updated by sub-plugins.
234 $plugins = core_component
::get_plugin_list('ltisource');
235 foreach (array_keys($plugins) as $plugin) {
236 $pluginparams = component_callback('ltisource_'.$plugin, 'before_launch',
237 array($instance, $endpoint, $requestparams), array());
239 if (!empty($pluginparams) && is_array($pluginparams)) {
240 $requestparams = array_merge($requestparams, $pluginparams);
244 if (!empty($key) && !empty($secret)) {
245 $parms = lti_sign_parameters($requestparams, $endpoint, "POST", $key, $secret);
247 $endpointurl = new \
moodle_url($endpoint);
248 $endpointparams = $endpointurl->params();
250 // Strip querystring params in endpoint url from $parms to avoid duplication.
251 if (!empty($endpointparams) && !empty($parms)) {
252 foreach (array_keys($endpointparams) as $paramname) {
253 if (isset($parms[$paramname])) {
254 unset($parms[$paramname]);
260 // If no key and secret, do the launch unsigned.
261 $returnurlparams['unsigned'] = '1';
262 $parms = $requestparams;
265 return array($endpoint, $parms);
269 * Launch an external tool activity.
271 * @param stdClass $instance the external tool activity settings
272 * @return string The HTML code containing the javascript code for the launch
274 function lti_launch_tool($instance) {
276 list($endpoint, $parms) = lti_get_launch_data($instance);
277 $debuglaunch = ( $instance->debuglaunch
== 1 );
279 $content = lti_post_launch_html($parms, $endpoint, $debuglaunch);
285 * Prepares an LTI registration request message
287 * $param object $instance Tool Proxy instance object
289 function lti_register($toolproxy) {
290 $endpoint = $toolproxy->regurl
;
292 // Change the status to pending.
293 $toolproxy->state
= LTI_TOOL_PROXY_STATE_PENDING
;
294 lti_update_tool_proxy($toolproxy);
296 $requestparams = lti_build_registration_request($toolproxy);
298 $content = lti_post_launch_html($requestparams, $endpoint, false);
305 * Gets the parameters for the regirstration request
307 * @param object $toolproxy Tool Proxy instance object
308 * @return array Registration request parameters
310 function lti_build_registration_request($toolproxy) {
311 $key = $toolproxy->guid
;
312 $secret = $toolproxy->secret
;
314 $requestparams = array();
315 $requestparams['lti_message_type'] = 'ToolProxyRegistrationRequest';
316 $requestparams['lti_version'] = 'LTI-2p0';
317 $requestparams['reg_key'] = $key;
318 $requestparams['reg_password'] = $secret;
319 $requestparams['reg_url'] = $toolproxy->regurl
;
321 // Add the profile URL.
322 $profileservice = lti_get_service_by_name('profile');
323 $profileservice->set_tool_proxy($toolproxy);
324 $requestparams['tc_profile_url'] = $profileservice->parse_value('$ToolConsumerProfile.url');
326 // Add the return URL.
327 $returnurlparams = array('id' => $toolproxy->id
, 'sesskey' => sesskey());
328 $url = new \
moodle_url('/mod/lti/externalregistrationreturn.php', $returnurlparams);
329 $returnurl = $url->out(false);
331 $requestparams['launch_presentation_return_url'] = $returnurl;
333 return $requestparams;
339 * @param int $instanceid
341 * @param string $servicesalt
342 * @param null|int $typeid
343 * @param null|int $launchid
346 function lti_build_sourcedid($instanceid, $userid, $servicesalt, $typeid = null, $launchid = null) {
347 $data = new \
stdClass();
349 $data->instanceid
= $instanceid;
350 $data->userid
= $userid;
351 $data->typeid
= $typeid;
352 if (!empty($launchid)) {
353 $data->launchid
= $launchid;
355 $data->launchid
= mt_rand();
358 $json = json_encode($data);
360 $hash = hash('sha256', $json . $servicesalt, false);
362 $container = new \
stdClass();
363 $container->data
= $data;
364 $container->hash
= $hash;
370 * This function builds the request that must be sent to the tool producer
372 * @param object $instance Basic LTI instance object
373 * @param array $typeconfig Basic LTI tool configuration
374 * @param object $course Course object
375 * @param int|null $typeid Basic LTI tool ID
376 * @param boolean $islti2 True if an LTI 2 tool is being launched
378 * @return array Request details
380 function lti_build_request($instance, $typeconfig, $course, $typeid = null, $islti2 = false) {
383 if (empty($instance->cmid
)) {
387 $role = lti_get_ims_role($USER, $instance->cmid
, $instance->course
, $islti2);
389 $requestparams = array(
390 'user_id' => $USER->id
,
391 'lis_person_sourcedid' => $USER->idnumber
,
393 'context_id' => $course->id
,
394 'context_label' => trim(html_to_text($course->shortname
, 0)),
395 'context_title' => trim(html_to_text($course->fullname
, 0)),
397 if (!empty($instance->name
)) {
398 $requestparams['resource_link_title'] = trim(html_to_text($instance->name
, 0));
400 if (!empty($instance->cmid
)) {
401 $intro = format_module_intro('lti', $instance, $instance->cmid
);
402 $intro = trim(html_to_text($intro, 0, false));
404 // This may look weird, but this is required for new lines
405 // so we generate the same OAuth signature as the tool provider.
406 $intro = str_replace("\n", "\r\n", $intro);
407 $requestparams['resource_link_description'] = $intro;
409 if (!empty($instance->id
)) {
410 $requestparams['resource_link_id'] = $instance->id
;
412 if (!empty($instance->resource_link_id
)) {
413 $requestparams['resource_link_id'] = $instance->resource_link_id
;
415 if ($course->format
== 'site') {
416 $requestparams['context_type'] = 'Group';
418 $requestparams['context_type'] = 'CourseSection';
419 $requestparams['lis_course_section_sourcedid'] = $course->idnumber
;
422 if (!empty($instance->id
) && !empty($instance->servicesalt
) && ($islti2 ||
423 $typeconfig['acceptgrades'] == LTI_SETTING_ALWAYS ||
424 ($typeconfig['acceptgrades'] == LTI_SETTING_DELEGATE
&& $instance->instructorchoiceacceptgrades
== LTI_SETTING_ALWAYS
))
426 $placementsecret = $instance->servicesalt
;
427 $sourcedid = json_encode(lti_build_sourcedid($instance->id
, $USER->id
, $placementsecret, $typeid));
428 $requestparams['lis_result_sourcedid'] = $sourcedid;
430 // Add outcome service URL.
431 $serviceurl = new \
moodle_url('/mod/lti/service.php');
432 $serviceurl = $serviceurl->out();
435 if (!empty($CFG->mod_lti_forcessl
)) {
439 if ((isset($typeconfig['forcessl']) && ($typeconfig['forcessl'] == '1')) or $forcessl) {
440 $serviceurl = lti_ensure_url_is_https($serviceurl);
443 $requestparams['lis_outcome_service_url'] = $serviceurl;
446 // Send user's name and email data if appropriate.
447 if ($islti2 ||
$typeconfig['sendname'] == LTI_SETTING_ALWAYS ||
448 ($typeconfig['sendname'] == LTI_SETTING_DELEGATE
&& isset($instance->instructorchoicesendname
)
449 && $instance->instructorchoicesendname
== LTI_SETTING_ALWAYS
)
451 $requestparams['lis_person_name_given'] = $USER->firstname
;
452 $requestparams['lis_person_name_family'] = $USER->lastname
;
453 $requestparams['lis_person_name_full'] = $USER->firstname
. ' ' . $USER->lastname
;
454 $requestparams['ext_user_username'] = $USER->username
;
457 if ($islti2 ||
$typeconfig['sendemailaddr'] == LTI_SETTING_ALWAYS ||
458 ($typeconfig['sendemailaddr'] == LTI_SETTING_DELEGATE
&& isset($instance->instructorchoicesendemailaddr
)
459 && $instance->instructorchoicesendemailaddr
== LTI_SETTING_ALWAYS
)
461 $requestparams['lis_person_contact_email_primary'] = $USER->email
;
464 return $requestparams;
468 * This function builds the request that must be sent to an LTI 2 tool provider
470 * @param object $tool Basic LTI tool object
471 * @param array $params Custom launch parameters
473 * @return array Request details
475 function lti_build_request_lti2($tool, $params) {
477 $requestparams = array();
479 $capabilities = lti_get_capabilities();
480 $enabledcapabilities = explode("\n", $tool->enabledcapability
);
481 foreach ($enabledcapabilities as $capability) {
482 if (array_key_exists($capability, $capabilities)) {
483 $val = $capabilities[$capability];
484 if ($val && (substr($val, 0, 1) != '$')) {
485 if (isset($params[$val])) {
486 $requestparams[$capabilities[$capability]] = $params[$capabilities[$capability]];
492 return $requestparams;
497 * This function builds the standard parameters for an LTI 1 or 2 request that must be sent to the tool producer
499 * @param stdClass $instance Basic LTI instance object
500 * @param string $orgid Organisation ID
501 * @param boolean $islti2 True if an LTI 2 tool is being launched
502 * @param string $messagetype The request message type. Defaults to basic-lti-launch-request if empty.
504 * @return array Request details
506 function lti_build_standard_request($instance, $orgid, $islti2, $messagetype = 'basic-lti-launch-request') {
509 $requestparams = array();
512 $requestparams['resource_link_id'] = $instance->id
;
513 if (property_exists($instance, 'resource_link_id') and !empty($instance->resource_link_id
)) {
514 $requestparams['resource_link_id'] = $instance->resource_link_id
;
518 $requestparams['launch_presentation_locale'] = current_language();
520 // Make sure we let the tool know what LMS they are being called from.
521 $requestparams['ext_lms'] = 'moodle-2';
522 $requestparams['tool_consumer_info_product_family_code'] = 'moodle';
523 $requestparams['tool_consumer_info_version'] = strval($CFG->version
);
525 // Add oauth_callback to be compliant with the 1.0A spec.
526 $requestparams['oauth_callback'] = 'about:blank';
529 $requestparams['lti_version'] = 'LTI-1p0';
531 $requestparams['lti_version'] = 'LTI-2p0';
533 $requestparams['lti_message_type'] = $messagetype;
536 $requestparams["tool_consumer_instance_guid"] = $orgid;
538 if (!empty($CFG->mod_lti_institution_name
)) {
539 $requestparams['tool_consumer_instance_name'] = trim(html_to_text($CFG->mod_lti_institution_name
, 0));
541 $requestparams['tool_consumer_instance_name'] = get_site()->shortname
;
543 $requestparams['tool_consumer_instance_description'] = trim(html_to_text(get_site()->fullname
, 0));
545 return $requestparams;
549 * This function builds the custom parameters
551 * @param object $toolproxy Tool proxy instance object
552 * @param object $tool Tool instance object
553 * @param object $instance Tool placement instance object
554 * @param array $params LTI launch parameters
555 * @param string $customstr Custom parameters defined for tool
556 * @param string $instructorcustomstr Custom parameters defined for this placement
557 * @param boolean $islti2 True if an LTI 2 tool is being launched
559 * @return array Custom parameters
561 function lti_build_custom_parameters($toolproxy, $tool, $instance, $params, $customstr, $instructorcustomstr, $islti2) {
563 // Concatenate the custom parameters from the administrator and the instructor
564 // Instructor parameters are only taken into consideration if the administrator
565 // has given permission.
568 $custom = lti_split_custom_parameters($toolproxy, $tool, $params, $customstr, $islti2);
570 if (!isset($typeconfig['allowinstructorcustom']) ||
$typeconfig['allowinstructorcustom'] != LTI_SETTING_NEVER
) {
571 if ($instructorcustomstr) {
572 $custom = array_merge(lti_split_custom_parameters($toolproxy, $tool, $params,
573 $instructorcustomstr, $islti2), $custom);
577 $custom = array_merge(lti_split_custom_parameters($toolproxy, $tool, $params,
578 $tool->parameter
, true), $custom);
579 $settings = lti_get_tool_settings($tool->toolproxyid
);
580 $custom = array_merge($custom, lti_get_custom_parameters($toolproxy, $tool, $params, $settings));
581 if (!empty($instance->course
)) {
582 $settings = lti_get_tool_settings($tool->toolproxyid
, $instance->course
);
583 $custom = array_merge($custom, lti_get_custom_parameters($toolproxy, $tool, $params, $settings));
584 if (!empty($instance->id
)) {
585 $settings = lti_get_tool_settings($tool->toolproxyid
, $instance->course
, $instance->id
);
586 $custom = array_merge($custom, lti_get_custom_parameters($toolproxy, $tool, $params, $settings));
595 * Builds a standard LTI Content-Item selection request.
597 * @param int $id The tool type ID.
598 * @param stdClass $course The course object.
599 * @param moodle_url $returnurl The return URL in the tool consumer (TC) that the tool provider (TP)
600 * will use to return the Content-Item message.
601 * @param string $title The tool's title, if available.
602 * @param string $text The text to display to represent the content item. This value may be a long description of the content item.
603 * @param array $mediatypes Array of MIME types types supported by the TC. If empty, the TC will support ltilink by default.
604 * @param array $presentationtargets Array of ways in which the selected content item(s) can be requested to be opened
605 * (via the presentationDocumentTarget element for a returned content item).
606 * If empty, "frame", "iframe", and "window" will be supported by default.
607 * @param bool $autocreate Indicates whether any content items returned by the TP would be automatically persisted without
608 * @param bool $multiple Indicates whether the user should be permitted to select more than one item. False by default.
609 * any option for the user to cancel the operation. False by default.
610 * @param bool $unsigned Indicates whether the TC is willing to accept an unsigned return message, or not.
611 * A signed message should always be required when the content item is being created automatically in the
612 * TC without further interaction from the user. False by default.
613 * @param bool $canconfirm Flag for can_confirm parameter. False by default.
614 * @param bool $copyadvice Indicates whether the TC is able and willing to make a local copy of a content item. False by default.
615 * @return stdClass The object containing the signed request parameters and the URL to the TP's Content-Item selection interface.
616 * @throws moodle_exception When the LTI tool type does not exist.`
617 * @throws coding_exception For invalid media type and presentation target parameters.
619 function lti_build_content_item_selection_request($id, $course, moodle_url
$returnurl, $title = '', $text = '', $mediatypes = [],
620 $presentationtargets = [], $autocreate = false, $multiple = false,
621 $unsigned = false, $canconfirm = false, $copyadvice = false) {
622 $tool = lti_get_type($id);
623 // Validate parameters.
625 throw new moodle_exception('errortooltypenotfound', 'mod_lti');
627 if (!is_array($mediatypes)) {
628 throw new coding_exception('The list of accepted media types should be in an array');
630 if (!is_array($presentationtargets)) {
631 throw new coding_exception('The list of accepted presentation targets should be in an array');
634 // Check title. If empty, use the tool's name.
636 $title = $tool->name
;
639 $typeconfig = lti_get_type_config($id);
643 if (isset($tool->toolproxyid
)) {
645 $toolproxy = lti_get_tool_proxy($tool->toolproxyid
);
646 $key = $toolproxy->guid
;
647 $secret = $toolproxy->secret
;
650 if (!empty($typeconfig['resourcekey'])) {
651 $key = $typeconfig['resourcekey'];
653 if (!empty($typeconfig['password'])) {
654 $secret = $typeconfig['password'];
657 $tool->enabledcapability
= '';
658 if (!empty($typeconfig['enabledcapability_ContentItemSelectionRequest'])) {
659 $tool->enabledcapability
= $typeconfig['enabledcapability_ContentItemSelectionRequest'];
662 $tool->parameter
= '';
663 if (!empty($typeconfig['parameter_ContentItemSelectionRequest'])) {
664 $tool->parameter
= $typeconfig['parameter_ContentItemSelectionRequest'];
668 if (!empty($typeconfig['toolurl_ContentItemSelectionRequest'])) {
669 $toolurl = new moodle_url($typeconfig['toolurl_ContentItemSelectionRequest']);
671 $toolurl = new moodle_url($typeconfig['toolurl']);
674 // Check if SSL is forced.
675 if (!empty($typeconfig['forcessl'])) {
676 // Make sure the tool URL is set to https.
677 if (strtolower($toolurl->get_scheme()) === 'http') {
678 $toolurl->set_scheme('https');
680 // Make sure the return URL is set to https.
681 if (strtolower($returnurl->get_scheme()) === 'http') {
682 $returnurl->set_scheme('https');
685 $toolurlout = $toolurl->out(false);
687 // Get base request parameters.
688 $instance = new stdClass();
689 $instance->course
= $course->id
;
690 $requestparams = lti_build_request($instance, $typeconfig, $course, $id, $islti2);
692 // Get LTI2-specific request parameters and merge to the request parameters if applicable.
694 $lti2params = lti_build_request_lti2($tool, $requestparams);
695 $requestparams = array_merge($requestparams, $lti2params);
698 // Get standard request parameters and merge to the request parameters.
699 $orgid = !empty($typeconfig['organizationid']) ?
$typeconfig['organizationid'] : '';
700 $standardparams = lti_build_standard_request(null, $orgid, $islti2, 'ContentItemSelectionRequest');
701 $requestparams = array_merge($requestparams, $standardparams);
703 // Get custom request parameters and merge to the request parameters.
705 if (!empty($typeconfig['customparameters'])) {
706 $customstr = $typeconfig['customparameters'];
708 $customparams = lti_build_custom_parameters($toolproxy, $tool, $instance, $requestparams, $customstr, '', $islti2);
709 $requestparams = array_merge($requestparams, $customparams);
711 // Allow request params to be updated by sub-plugins.
712 $plugins = core_component
::get_plugin_list('ltisource');
713 foreach (array_keys($plugins) as $plugin) {
714 $pluginparams = component_callback('ltisource_' . $plugin, 'before_launch', [$instance, $toolurlout, $requestparams], []);
716 if (!empty($pluginparams) && is_array($pluginparams)) {
717 $requestparams = array_merge($requestparams, $pluginparams);
721 // Media types. Set to ltilink by default if empty.
722 if (empty($mediatypes)) {
724 'application/vnd.ims.lti.v1.ltilink',
727 $requestparams['accept_media_types'] = implode(',', $mediatypes);
729 // Presentation targets. Supports frame, iframe, window by default if empty.
730 if (empty($presentationtargets)) {
731 $presentationtargets = [
737 $requestparams['accept_presentation_document_targets'] = implode(',', $presentationtargets);
739 // Other request parameters.
740 $requestparams['accept_copy_advice'] = $copyadvice === true ?
'true' : 'false';
741 $requestparams['accept_multiple'] = $multiple === true ?
'true' : 'false';
742 $requestparams['accept_unsigned'] = $unsigned === true ?
'true' : 'false';
743 $requestparams['auto_create'] = $autocreate === true ?
'true' : 'false';
744 $requestparams['can_confirm'] = $canconfirm === true ?
'true' : 'false';
745 $requestparams['content_item_return_url'] = $returnurl->out(false);
746 $requestparams['title'] = $title;
747 $requestparams['text'] = $text;
748 $signedparams = lti_sign_parameters($requestparams, $toolurlout, 'POST', $key, $secret);
749 $toolurlparams = $toolurl->params();
751 // Strip querystring params in endpoint url from $signedparams to avoid duplication.
752 if (!empty($toolurlparams) && !empty($signedparams)) {
753 foreach (array_keys($toolurlparams) as $paramname) {
754 if (isset($signedparams[$paramname])) {
755 unset($signedparams[$paramname]);
760 // Check for params that should not be passed. Unset if they are set.
763 'resource_link_title',
764 'resource_link_description',
765 'launch_presentation_return_url',
766 'lis_result_sourcedid',
768 foreach ($unwantedparams as $param) {
769 if (isset($signedparams[$param])) {
770 unset($signedparams[$param]);
774 // Prepare result object.
775 $result = new stdClass();
776 $result->params
= $signedparams;
777 $result->url
= $toolurlout;
783 * Processes the tool provider's response to the ContentItemSelectionRequest and builds the configuration data from the
784 * selected content item. This configuration data can be then used when adding a tool into the course.
786 * @param int $typeid The tool type ID.
787 * @param string $messagetype The value for the lti_message_type parameter.
788 * @param string $ltiversion The value for the lti_version parameter.
789 * @param string $consumerkey The consumer key.
790 * @param string $contentitemsjson The JSON string for the content_items parameter.
791 * @return stdClass The array of module information objects.
792 * @throws moodle_exception
793 * @throws lti\OAuthException
795 function lti_tool_configuration_from_content_item($typeid, $messagetype, $ltiversion, $consumerkey, $contentitemsjson) {
796 $tool = lti_get_type($typeid);
797 // Validate parameters.
799 throw new moodle_exception('errortooltypenotfound', 'mod_lti');
801 // Check lti_message_type. Show debugging if it's not set to ContentItemSelection.
802 // No need to throw exceptions for now since lti_message_type does not seem to be used in this processing at the moment.
803 if ($messagetype !== 'ContentItemSelection') {
804 debugging("lti_message_type is invalid: {$messagetype}. It should be set to 'ContentItemSelection'.",
808 $typeconfig = lti_get_type_config($typeid);
810 if (isset($tool->toolproxyid
)) {
812 $toolproxy = lti_get_tool_proxy($tool->toolproxyid
);
813 $key = $toolproxy->guid
;
814 $secret = $toolproxy->secret
;
818 if (!empty($typeconfig['resourcekey'])) {
819 $key = $typeconfig['resourcekey'];
823 if (!empty($typeconfig['password'])) {
824 $secret = $typeconfig['password'];
830 // Check LTI versions from our side and the response's side. Show debugging if they don't match.
831 // No need to throw exceptions for now since LTI version does not seem to be used in this processing at the moment.
832 $expectedversion = LTI_VERSION_1
;
834 $expectedversion = LTI_VERSION_2
;
836 if ($ltiversion !== $expectedversion) {
837 debugging("lti_version from response does not match the tool's configuration. Tool: {$expectedversion}," .
838 " Response: {$ltiversion}", DEBUG_DEVELOPER
);
841 if ($consumerkey !== $key) {
842 throw new moodle_exception('errorincorrectconsumerkey', 'mod_lti');
845 $store = new lti\
TrivialOAuthDataStore();
846 $store->add_consumer($key, $secret);
847 $server = new lti\
OAuthServer($store);
848 $method = new lti\
OAuthSignatureMethod_HMAC_SHA1();
849 $server->add_signature_method($method);
850 $request = lti\OAuthRequest
::from_request();
852 $server->verify_request($request);
853 } catch (lti\OAuthException
$e) {
854 throw new lti\
OAuthException("OAuth signature failed: " . $e->getMessage());
857 $items = json_decode($contentitemsjson);
859 throw new moodle_exception('errorinvaliddata', 'mod_lti', '', $contentitemsjson);
861 if ($items->{'@context'} !== 'http://purl.imsglobal.org/ctx/lti/v1/ContentItem') {
862 throw new moodle_exception('errorinvalidmediatype', 'mod_lti', '', $items->{'@context'});
864 if (!isset($items->{'@graph'}) ||
!is_array($items->{'@graph'}) ||
(count($items->{'@graph'}) > 1)) {
865 throw new moodle_exception('errorinvalidresponseformat', 'mod_lti');
869 if (!empty($items->{'@graph'})) {
870 $item = $items->{'@graph'}[0];
872 $config = new stdClass();
874 if (isset($item->title
)) {
875 $config->name
= $item->title
;
877 if (empty($config->name
)) {
878 $config->name
= $tool->name
;
880 if (isset($item->text
)) {
881 $config->introeditor
= [
882 'text' => $item->text
,
883 'format' => FORMAT_PLAIN
886 if (isset($item->icon
->{'@id'})) {
887 $iconurl = new moodle_url($item->icon
->{'@id'});
888 // Assign item's icon URL to secureicon or icon depending on its scheme.
889 if (strtolower($iconurl->get_scheme()) === 'https') {
890 $config->secureicon
= $iconurl->out(false);
892 $config->icon
= $iconurl->out(false);
895 if (isset($item->url
)) {
896 $url = new moodle_url($item->url
);
897 $config->toolurl
= $url->out(false);
900 $config->typeid
= $typeid;
902 $config->instructorchoicesendname
= LTI_SETTING_NEVER
;
903 $config->instructorchoicesendemailaddr
= LTI_SETTING_NEVER
;
904 $config->instructorchoiceacceptgrades
= LTI_SETTING_NEVER
;
905 $config->launchcontainer
= LTI_LAUNCH_CONTAINER_DEFAULT
;
906 if (isset($item->placementAdvice
->presentationDocumentTarget
)) {
907 if ($item->placementAdvice
->presentationDocumentTarget
=== 'window') {
908 $config->launchcontainer
= LTI_LAUNCH_CONTAINER_WINDOW
;
909 } else if ($item->placementAdvice
->presentationDocumentTarget
=== 'frame') {
910 $config->launchcontainer
= LTI_LAUNCH_CONTAINER_EMBED_NO_BLOCKS
;
911 } else if ($item->placementAdvice
->presentationDocumentTarget
=== 'iframe') {
912 $config->launchcontainer
= LTI_LAUNCH_CONTAINER_EMBED
;
915 if (isset($item->custom
)) {
916 $customparameters = [];
917 foreach ($item->custom
as $key => $value) {
918 $customparameters[] = "{$key}={$value}";
920 $config->instructorcustomparameters
= implode("\n", $customparameters);
926 function lti_get_tool_table($tools, $id) {
927 global $CFG, $OUTPUT, $USER;
930 $typename = get_string('typename', 'lti');
931 $baseurl = get_string('baseurl', 'lti');
932 $action = get_string('action', 'lti');
933 $createdon = get_string('createdon', 'lti');
935 if (!empty($tools)) {
937 <div id=\"{$id}_tools_container\" style=\"margin-top:.5em;margin-bottom:.5em\">
938 <table id=\"{$id}_tools\">
949 foreach ($tools as $type) {
950 $date = userdate($type->timecreated
, get_string('strftimedatefullshort', 'core_langconfig'));
951 $accept = get_string('accept', 'lti');
952 $update = get_string('update', 'lti');
953 $delete = get_string('delete', 'lti');
955 if (empty($type->toolproxyid
)) {
956 $baseurl = new \
moodle_url('/mod/lti/typessettings.php', array(
957 'action' => 'accept',
959 'sesskey' => sesskey(),
962 $ref = $type->baseurl
;
964 $baseurl = new \
moodle_url('/mod/lti/toolssettings.php', array(
965 'action' => 'accept',
967 'sesskey' => sesskey(),
970 $ref = $type->tpname
;
973 $accepthtml = $OUTPUT->action_icon($baseurl,
974 new \
pix_icon('t/check', $accept, '', array('class' => 'iconsmall')), null,
975 array('title' => $accept, 'class' => 'editing_accept'));
977 $deleteaction = 'delete';
979 if ($type->state
== LTI_TOOL_STATE_CONFIGURED
) {
983 if ($type->state
!= LTI_TOOL_STATE_REJECTED
) {
984 $deleteaction = 'reject';
985 $delete = get_string('reject', 'lti');
988 $updateurl = clone($baseurl);
989 $updateurl->param('action', 'update');
990 $updatehtml = $OUTPUT->action_icon($updateurl,
991 new \
pix_icon('t/edit', $update, '', array('class' => 'iconsmall')), null,
992 array('title' => $update, 'class' => 'editing_update'));
994 if (($type->state
!= LTI_TOOL_STATE_REJECTED
) ||
empty($type->toolproxyid
)) {
995 $deleteurl = clone($baseurl);
996 $deleteurl->param('action', $deleteaction);
997 $deletehtml = $OUTPUT->action_icon($deleteurl,
998 new \
pix_icon('t/delete', $delete, '', array('class' => 'iconsmall')), null,
999 array('title' => $delete, 'class' => 'editing_delete'));
1014 <td align=\"center\">
1015 {$accepthtml}{$updatehtml}{$deletehtml}
1020 $html .= '</table></div>';
1022 $html .= get_string('no_' . $id, 'lti');
1029 * This function builds the tab for a category of tool proxies
1031 * @param object $toolproxies Tool proxy instance objects
1032 * @param string $id Category ID
1034 * @return string HTML for tab
1036 function lti_get_tool_proxy_table($toolproxies, $id) {
1039 if (!empty($toolproxies)) {
1040 $typename = get_string('typename', 'lti');
1041 $url = get_string('registrationurl', 'lti');
1042 $action = get_string('action', 'lti');
1043 $createdon = get_string('createdon', 'lti');
1046 <div id
="{$id}_tool_proxies_container" style
="margin-top: 0.5em; margin-bottom: 0.5em">
1047 <table id
="{$id}_tool_proxies">
1050 <th
>{$typename}</th
>
1052 <th
>{$createdon}</th
>
1057 foreach ($toolproxies as $toolproxy) {
1058 $date = userdate($toolproxy->timecreated
, get_string('strftimedatefullshort', 'core_langconfig'));
1059 $accept = get_string('register', 'lti');
1060 $update = get_string('update', 'lti');
1061 $delete = get_string('delete', 'lti');
1063 $baseurl = new \
moodle_url('/mod/lti/registersettings.php', array(
1064 'action' => 'accept',
1065 'id' => $toolproxy->id
,
1066 'sesskey' => sesskey(),
1070 $registerurl = new \
moodle_url('/mod/lti/register.php', array(
1071 'id' => $toolproxy->id
,
1072 'sesskey' => sesskey(),
1073 'tab' => 'tool_proxy'
1076 $accepthtml = $OUTPUT->action_icon($registerurl,
1077 new \
pix_icon('t/check', $accept, '', array('class' => 'iconsmall')), null,
1078 array('title' => $accept, 'class' => 'editing_accept'));
1080 $deleteaction = 'delete';
1082 if ($toolproxy->state
!= LTI_TOOL_PROXY_STATE_CONFIGURED
) {
1086 if (($toolproxy->state
== LTI_TOOL_PROXY_STATE_CONFIGURED
) ||
($toolproxy->state
== LTI_TOOL_PROXY_STATE_PENDING
)) {
1087 $delete = get_string('cancel', 'lti');
1090 $updateurl = clone($baseurl);
1091 $updateurl->param('action', 'update');
1092 $updatehtml = $OUTPUT->action_icon($updateurl,
1093 new \
pix_icon('t/edit', $update, '', array('class' => 'iconsmall')), null,
1094 array('title' => $update, 'class' => 'editing_update'));
1096 $deleteurl = clone($baseurl);
1097 $deleteurl->param('action', $deleteaction);
1098 $deletehtml = $OUTPUT->action_icon($deleteurl,
1099 new \
pix_icon('t/delete', $delete, '', array('class' => 'iconsmall')), null,
1100 array('title' => $delete, 'class' => 'editing_delete'));
1107 {$toolproxy->regurl
}
1113 {$accepthtml}{$updatehtml}{$deletehtml}
1118 $html .= '</table></div>';
1120 $html = get_string('no_' . $id, 'lti');
1127 * Extracts the enabled capabilities into an array, including those implicitly declared in a parameter
1129 * @param object $tool Tool instance object
1131 * @return Array of enabled capabilities
1133 function lti_get_enabled_capabilities($tool) {
1134 if (!isset($tool)) {
1137 if (!empty($tool->enabledcapability
)) {
1138 $enabledcapabilities = explode("\n", $tool->enabledcapability
);
1140 $enabledcapabilities = array();
1142 $paramstr = str_replace("\r\n", "\n", $tool->parameter
);
1143 $paramstr = str_replace("\n\r", "\n", $paramstr);
1144 $paramstr = str_replace("\r", "\n", $paramstr);
1145 $params = explode("\n", $paramstr);
1146 foreach ($params as $param) {
1147 $pos = strpos($param, '=');
1148 if (($pos === false) ||
($pos < 1)) {
1151 $value = trim(core_text
::substr($param, $pos +
1, strlen($param)));
1152 if (substr($value, 0, 1) == '$') {
1153 $value = substr($value, 1);
1154 if (!in_array($value, $enabledcapabilities)) {
1155 $enabledcapabilities[] = $value;
1159 return $enabledcapabilities;
1163 * Splits the custom parameters field to the various parameters
1165 * @param object $toolproxy Tool proxy instance object
1166 * @param object $tool Tool instance object
1167 * @param array $params LTI launch parameters
1168 * @param string $customstr String containing the parameters
1169 * @param boolean $islti2 True if an LTI 2 tool is being launched
1171 * @return array of custom parameters
1173 function lti_split_custom_parameters($toolproxy, $tool, $params, $customstr, $islti2 = false) {
1174 $customstr = str_replace("\r\n", "\n", $customstr);
1175 $customstr = str_replace("\n\r", "\n", $customstr);
1176 $customstr = str_replace("\r", "\n", $customstr);
1177 $lines = explode("\n", $customstr); // Or should this split on "/[\n;]/"?
1179 foreach ($lines as $line) {
1180 $pos = strpos($line, '=');
1181 if ( $pos === false ||
$pos < 1 ) {
1184 $key = trim(core_text
::substr($line, 0, $pos));
1185 $key = lti_map_keyname($key, false);
1186 $val = trim(core_text
::substr($line, $pos +
1, strlen($line)));
1187 $val = lti_parse_custom_parameter($toolproxy, $tool, $params, $val, $islti2);
1188 $key2 = lti_map_keyname($key);
1189 $retval['custom_'.$key2] = $val;
1190 if ($key != $key2) {
1191 $retval['custom_'.$key] = $val;
1198 * Adds the custom parameters to an array
1200 * @param object $toolproxy Tool proxy instance object
1201 * @param object $tool Tool instance object
1202 * @param array $params LTI launch parameters
1203 * @param array $parameters Array containing the parameters
1205 * @return array Array of custom parameters
1207 function lti_get_custom_parameters($toolproxy, $tool, $params, $parameters) {
1209 foreach ($parameters as $key => $val) {
1210 $key2 = lti_map_keyname($key);
1211 $val = lti_parse_custom_parameter($toolproxy, $tool, $params, $val, true);
1212 $retval['custom_'.$key2] = $val;
1213 if ($key != $key2) {
1214 $retval['custom_'.$key] = $val;
1221 * Parse a custom parameter to replace any substitution variables
1223 * @param object $toolproxy Tool proxy instance object
1224 * @param object $tool Tool instance object
1225 * @param array $params LTI launch parameters
1226 * @param string $value Custom parameter value
1227 * @param boolean $islti2 True if an LTI 2 tool is being launched
1229 * @return Parsed value of custom parameter
1231 function lti_parse_custom_parameter($toolproxy, $tool, $params, $value, $islti2) {
1232 global $USER, $COURSE;
1235 if (substr($value, 0, 1) == '\\') {
1236 $value = substr($value, 1);
1237 } else if (substr($value, 0, 1) == '$') {
1238 $value1 = substr($value, 1);
1239 $enabledcapabilities = lti_get_enabled_capabilities($tool);
1240 if (!$islti2 ||
in_array($value1, $enabledcapabilities)) {
1241 $capabilities = lti_get_capabilities();
1242 if (array_key_exists($value1, $capabilities)) {
1243 $val = $capabilities[$value1];
1245 if (substr($val, 0, 1) != '$') {
1246 $value = $params[$val];
1248 $valarr = explode('->', substr($val, 1), 2);
1249 $value = "{${$valarr[0]}->{$valarr[1]}}";
1250 $value = str_replace('<br />' , ' ', $value);
1251 $value = str_replace('<br>' , ' ', $value);
1252 $value = format_string($value);
1255 $value = lti_calculate_custom_parameter($value1);
1257 } else if ($islti2) {
1259 $services = lti_get_services();
1260 foreach ($services as $service) {
1261 $service->set_tool_proxy($toolproxy);
1262 $value = $service->parse_value($val);
1263 if ($val != $value) {
1275 * Calculates the value of a custom parameter that has not been specified earlier
1277 * @param string $value Custom parameter value
1279 * @return string Calculated value of custom parameter
1281 function lti_calculate_custom_parameter($value) {
1282 global $USER, $COURSE;
1285 case 'Moodle.Person.userGroupIds':
1286 return implode(",", groups_get_user_groups($COURSE->id
, $USER->id
)[0]);
1292 * Used for building the names of the different custom parameters
1294 * @param string $key Parameter name
1295 * @param bool $tolower Do we want to convert the key into lower case?
1296 * @return string Processed name
1298 function lti_map_keyname($key, $tolower = true) {
1301 $key = core_text
::strtolower(trim($key));
1303 foreach (str_split($key) as $ch) {
1304 if ( ($ch >= 'a' && $ch <= 'z') ||
($ch >= '0' && $ch <= '9') ||
(!$tolower && ($ch >= 'A' && $ch <= 'Z'))) {
1314 * Gets the IMS role string for the specified user and LTI course module.
1316 * @param mixed $user User object or user id
1317 * @param int $cmid The course module id of the LTI activity
1318 * @param int $courseid The course id of the LTI activity
1319 * @param boolean $islti2 True if an LTI 2 tool is being launched
1321 * @return string A role string suitable for passing with an LTI launch
1323 function lti_get_ims_role($user, $cmid, $courseid, $islti2) {
1327 // If no cmid is passed, check if the user is a teacher in the course
1328 // This allows other modules to programmatically "fake" a launch without
1329 // a real LTI instance.
1330 $context = context_course
::instance($courseid);
1332 if (has_capability('moodle/course:manageactivities', $context, $user)) {
1333 array_push($roles, 'Instructor');
1335 array_push($roles, 'Learner');
1338 $context = context_module
::instance($cmid);
1340 if (has_capability('mod/lti:manage', $context)) {
1341 array_push($roles, 'Instructor');
1343 array_push($roles, 'Learner');
1347 if (is_siteadmin($user) ||
has_capability('mod/lti:admin', $context)) {
1348 // Make sure admins do not have the Learner role, then set admin role.
1349 $roles = array_diff($roles, array('Learner'));
1351 array_push($roles, 'urn:lti:sysrole:ims/lis/Administrator', 'urn:lti:instrole:ims/lis/Administrator');
1353 array_push($roles, 'http://purl.imsglobal.org/vocab/lis/v2/person#Administrator');
1357 return join(',', $roles);
1361 * Returns configuration details for the tool
1363 * @param int $typeid Basic LTI tool typeid
1365 * @return array Tool Configuration
1367 function lti_get_type_config($typeid) {
1370 $query = "SELECT name, value
1371 FROM {lti_types_config}
1372 WHERE typeid = :typeid1
1374 SELECT 'toolurl' AS name, baseurl AS value
1378 SELECT 'icon' AS name, icon AS value
1382 SELECT 'secureicon' AS name, secureicon AS value
1384 WHERE id = :typeid4";
1386 $typeconfig = array();
1387 $configs = $DB->get_records_sql($query,
1388 array('typeid1' => $typeid, 'typeid2' => $typeid, 'typeid3' => $typeid, 'typeid4' => $typeid));
1390 if (!empty($configs)) {
1391 foreach ($configs as $config) {
1392 $typeconfig[$config->name
] = $config->value
;
1399 function lti_get_tools_by_url($url, $state, $courseid = null) {
1400 $domain = lti_get_domain_from_url($url);
1402 return lti_get_tools_by_domain($domain, $state, $courseid);
1405 function lti_get_tools_by_domain($domain, $state = null, $courseid = null) {
1408 $filters = array('tooldomain' => $domain);
1414 $statefilter = 'AND state = :state';
1417 if ($courseid && $courseid != $SITE->id
) {
1418 $coursefilter = 'OR course = :courseid';
1423 WHERE tooldomain = :tooldomain
1424 AND (course = :siteid $coursefilter)
1427 return $DB->get_records_sql($query, array(
1428 'courseid' => $courseid,
1429 'siteid' => $SITE->id
,
1430 'tooldomain' => $domain,
1436 * Returns all basicLTI tools configured by the administrator
1439 function lti_filter_get_types($course) {
1442 if (!empty($course)) {
1443 $where = "WHERE t.course = :course";
1444 $params = array('course' => $course);
1449 $query = "SELECT t.id, t.name, t.baseurl, t.state, t.toolproxyid, t.timecreated, tp.name tpname
1450 FROM {lti_types} t LEFT OUTER JOIN {lti_tool_proxies} tp ON t.toolproxyid = tp.id
1452 return $DB->get_records_sql($query, $params);
1456 * Given an array of tools, filter them based on their state
1458 * @param array $tools An array of lti_types records
1459 * @param int $state One of the LTI_TOOL_STATE_* constants
1462 function lti_filter_tool_types(array $tools, $state) {
1464 foreach ($tools as $key => $tool) {
1465 if ($tool->state
== $state) {
1466 $return[$key] = $tool;
1473 * Returns all lti types visible in this course
1475 * @param int $courseid The id of the course to retieve types for
1476 * @param array $coursevisible options for 'coursevisible' field,
1477 * default [LTI_COURSEVISIBLE_PRECONFIGURED, LTI_COURSEVISIBLE_ACTIVITYCHOOSER]
1478 * @return stdClass[] All the lti types visible in the given course
1480 function lti_get_lti_types_by_course($courseid, $coursevisible = null) {
1483 if ($coursevisible === null) {
1484 $coursevisible = [LTI_COURSEVISIBLE_PRECONFIGURED
, LTI_COURSEVISIBLE_ACTIVITYCHOOSER
];
1487 list($coursevisiblesql, $coursevisparams) = $DB->get_in_or_equal($coursevisible, SQL_PARAMS_NAMED
, 'coursevisible');
1490 WHERE coursevisible $coursevisiblesql
1491 AND (course = :siteid OR course = :courseid)
1492 AND state = :active";
1494 return $DB->get_records_sql($query,
1495 array('siteid' => $SITE->id
, 'courseid' => $courseid, 'active' => LTI_TOOL_STATE_CONFIGURED
) +
$coursevisparams);
1499 * Returns tool types for lti add instance and edit page
1501 * @return array Array of lti types
1503 function lti_get_types_for_add_instance() {
1505 $admintypes = lti_get_lti_types_by_course($COURSE->id
);
1508 $types[0] = (object)array('name' => get_string('automatic', 'lti'), 'course' => 0, 'toolproxyid' => null);
1510 foreach ($admintypes as $type) {
1511 $types[$type->id
] = $type;
1518 * Returns a list of configured types in the given course
1520 * @param int $courseid The id of the course to retieve types for
1521 * @param int $sectionreturn section to return to for forming the URLs
1522 * @return array Array of lti types. Each element is object with properties: name, title, icon, help, helplink, link
1524 function lti_get_configured_types($courseid, $sectionreturn = 0) {
1527 $admintypes = lti_get_lti_types_by_course($courseid, [LTI_COURSEVISIBLE_ACTIVITYCHOOSER
]);
1529 foreach ($admintypes as $ltitype) {
1530 $type = new stdClass();
1531 $type->modclass
= MOD_CLASS_ACTIVITY
;
1532 $type->name
= 'lti_type_' . $ltitype->id
;
1533 // Clean the name. We don't want tags here.
1534 $type->title
= clean_param($ltitype->name
, PARAM_NOTAGS
);
1535 $trimmeddescription = trim($ltitype->description
);
1536 if ($trimmeddescription != '') {
1537 // Clean the description. We don't want tags here.
1538 $type->help
= clean_param($trimmeddescription, PARAM_NOTAGS
);
1539 $type->helplink
= get_string('modulename_shortcut_link', 'lti');
1541 if (empty($ltitype->icon
)) {
1542 $type->icon
= $OUTPUT->pix_icon('icon', '', 'lti', array('class' => 'icon'));
1544 $type->icon
= html_writer
::empty_tag('img', array('src' => $ltitype->icon
, 'alt' => $ltitype->name
, 'class' => 'icon'));
1546 $type->link
= new moodle_url('/course/modedit.php', array('add' => 'lti', 'return' => 0, 'course' => $courseid,
1547 'sr' => $sectionreturn, 'typeid' => $ltitype->id
));
1553 function lti_get_domain_from_url($url) {
1556 if (preg_match(LTI_URL_DOMAIN_REGEX
, $url, $matches)) {
1561 function lti_get_tool_by_url_match($url, $courseid = null, $state = LTI_TOOL_STATE_CONFIGURED
) {
1562 $possibletools = lti_get_tools_by_url($url, $state, $courseid);
1564 return lti_get_best_tool_by_url($url, $possibletools, $courseid);
1567 function lti_get_url_thumbprint($url) {
1568 // Parse URL requires a schema otherwise everything goes into 'path'. Fixed 5.4.7 or later.
1569 if (preg_match('/https?:\/\//', $url) !== 1) {
1570 $url = 'http://'.$url;
1572 $urlparts = parse_url(strtolower($url));
1573 if (!isset($urlparts['path'])) {
1574 $urlparts['path'] = '';
1577 if (!isset($urlparts['query'])) {
1578 $urlparts['query'] = '';
1581 if (!isset($urlparts['host'])) {
1582 $urlparts['host'] = '';
1585 if (substr($urlparts['host'], 0, 4) === 'www.') {
1586 $urlparts['host'] = substr($urlparts['host'], 4);
1589 $urllower = $urlparts['host'] . '/' . $urlparts['path'];
1591 if ($urlparts['query'] != '') {
1592 $urllower .= '?' . $urlparts['query'];
1598 function lti_get_best_tool_by_url($url, $tools, $courseid = null) {
1599 if (count($tools) === 0) {
1603 $urllower = lti_get_url_thumbprint($url);
1605 foreach ($tools as $tool) {
1606 $tool->_matchscore
= 0;
1608 $toolbaseurllower = lti_get_url_thumbprint($tool->baseurl
);
1610 if ($urllower === $toolbaseurllower) {
1611 // 100 points for exact thumbprint match.
1612 $tool->_matchscore +
= 100;
1613 } else if (substr($urllower, 0, strlen($toolbaseurllower)) === $toolbaseurllower) {
1614 // 50 points if tool thumbprint starts with the base URL thumbprint.
1615 $tool->_matchscore +
= 50;
1618 // Prefer course tools over site tools.
1619 if (!empty($courseid)) {
1620 // Minus 10 points for not matching the course id (global tools).
1621 if ($tool->course
!= $courseid) {
1622 $tool->_matchscore
-= 10;
1627 $bestmatch = array_reduce($tools, function($value, $tool) {
1628 if ($tool->_matchscore
> $value->_matchscore
) {
1634 }, (object)array('_matchscore' => -1));
1636 // None of the tools are suitable for this URL.
1637 if ($bestmatch->_matchscore
<= 0) {
1644 function lti_get_shared_secrets_by_key($key) {
1647 // Look up the shared secret for the specified key in both the types_config table (for configured tools)
1648 // And in the lti resource table for ad-hoc tools.
1649 $query = "SELECT t2.value
1650 FROM {lti_types_config} t1
1651 JOIN {lti_types_config} t2 ON t1.typeid = t2.typeid
1652 JOIN {lti_types} type ON t2.typeid = type.id
1653 WHERE t1.name = 'resourcekey'
1654 AND t1.value = :key1
1655 AND t2.name = 'password'
1656 AND type.state = :configured1
1658 SELECT tp.secret AS value
1659 FROM {lti_tool_proxies} tp
1660 JOIN {lti_types} t ON tp.id = t.toolproxyid
1661 WHERE tp.guid = :key2
1662 AND t.state = :configured2
1664 SELECT password AS value
1666 WHERE resourcekey = :key3";
1668 $sharedsecrets = $DB->get_records_sql($query, array('configured1' => LTI_TOOL_STATE_CONFIGURED
,
1669 'configured2' => LTI_TOOL_STATE_CONFIGURED
, 'key1' => $key, 'key2' => $key, 'key3' => $key));
1671 $values = array_map(function($item) {
1672 return $item->value
;
1675 // There should really only be one shared secret per key. But, we can't prevent
1676 // more than one getting entered. For instance, if the same key is used for two tool providers.
1681 * Delete a Basic LTI configuration
1683 * @param int $id Configuration id
1685 function lti_delete_type($id) {
1688 // We should probably just copy the launch URL to the tool instances in this case... using a single query.
1690 $instances = $DB->get_records('lti', array('typeid' => $id));
1691 foreach ($instances as $instance) {
1692 $instance->typeid = 0;
1693 $DB->update_record('lti', $instance);
1696 $DB->delete_records('lti_types', array('id' => $id));
1697 $DB->delete_records('lti_types_config', array('typeid' => $id));
1700 function lti_set_state_for_type($id, $state) {
1703 $DB->update_record('lti_types', array('id' => $id, 'state' => $state));
1707 * Transforms a basic LTI object to an array
1709 * @param object $ltiobject Basic LTI object
1711 * @return array Basic LTI configuration details
1713 function lti_get_config($ltiobject) {
1714 $typeconfig = array();
1715 $typeconfig = (array)$ltiobject;
1716 $additionalconfig = lti_get_type_config($ltiobject->typeid
);
1717 $typeconfig = array_merge($typeconfig, $additionalconfig);
1723 * Generates some of the tool configuration based on the instance details
1727 * @return Instance configuration
1730 function lti_get_type_config_from_instance($id) {
1733 $instance = $DB->get_record('lti', array('id' => $id));
1734 $config = lti_get_config($instance);
1736 $type = new \
stdClass();
1737 $type->lti_fix
= $id;
1738 if (isset($config['toolurl'])) {
1739 $type->lti_toolurl
= $config['toolurl'];
1741 if (isset($config['instructorchoicesendname'])) {
1742 $type->lti_sendname
= $config['instructorchoicesendname'];
1744 if (isset($config['instructorchoicesendemailaddr'])) {
1745 $type->lti_sendemailaddr
= $config['instructorchoicesendemailaddr'];
1747 if (isset($config['instructorchoiceacceptgrades'])) {
1748 $type->lti_acceptgrades
= $config['instructorchoiceacceptgrades'];
1750 if (isset($config['instructorchoiceallowroster'])) {
1751 $type->lti_allowroster
= $config['instructorchoiceallowroster'];
1754 if (isset($config['instructorcustomparameters'])) {
1755 $type->lti_allowsetting
= $config['instructorcustomparameters'];
1761 * Generates some of the tool configuration based on the admin configuration details
1765 * @return Configuration details
1767 function lti_get_type_type_config($id) {
1770 $basicltitype = $DB->get_record('lti_types', array('id' => $id));
1771 $config = lti_get_type_config($id);
1773 $type = new \
stdClass();
1775 $type->lti_typename
= $basicltitype->name
;
1777 $type->typeid
= $basicltitype->id
;
1779 $type->toolproxyid
= $basicltitype->toolproxyid
;
1781 $type->lti_toolurl
= $basicltitype->baseurl
;
1783 $type->lti_description
= $basicltitype->description
;
1785 $type->lti_parameters
= $basicltitype->parameter
;
1787 $type->lti_icon
= $basicltitype->icon
;
1789 $type->lti_secureicon
= $basicltitype->secureicon
;
1791 if (isset($config['resourcekey'])) {
1792 $type->lti_resourcekey
= $config['resourcekey'];
1794 if (isset($config['password'])) {
1795 $type->lti_password
= $config['password'];
1798 if (isset($config['sendname'])) {
1799 $type->lti_sendname
= $config['sendname'];
1801 if (isset($config['instructorchoicesendname'])) {
1802 $type->lti_instructorchoicesendname
= $config['instructorchoicesendname'];
1804 if (isset($config['sendemailaddr'])) {
1805 $type->lti_sendemailaddr
= $config['sendemailaddr'];
1807 if (isset($config['instructorchoicesendemailaddr'])) {
1808 $type->lti_instructorchoicesendemailaddr
= $config['instructorchoicesendemailaddr'];
1810 if (isset($config['acceptgrades'])) {
1811 $type->lti_acceptgrades
= $config['acceptgrades'];
1813 if (isset($config['instructorchoiceacceptgrades'])) {
1814 $type->lti_instructorchoiceacceptgrades
= $config['instructorchoiceacceptgrades'];
1816 if (isset($config['allowroster'])) {
1817 $type->lti_allowroster
= $config['allowroster'];
1819 if (isset($config['instructorchoiceallowroster'])) {
1820 $type->lti_instructorchoiceallowroster
= $config['instructorchoiceallowroster'];
1823 if (isset($config['customparameters'])) {
1824 $type->lti_customparameters
= $config['customparameters'];
1827 if (isset($config['forcessl'])) {
1828 $type->lti_forcessl
= $config['forcessl'];
1831 if (isset($config['organizationid'])) {
1832 $type->lti_organizationid
= $config['organizationid'];
1834 if (isset($config['organizationurl'])) {
1835 $type->lti_organizationurl
= $config['organizationurl'];
1837 if (isset($config['organizationdescr'])) {
1838 $type->lti_organizationdescr
= $config['organizationdescr'];
1840 if (isset($config['launchcontainer'])) {
1841 $type->lti_launchcontainer
= $config['launchcontainer'];
1844 if (isset($config['coursevisible'])) {
1845 $type->lti_coursevisible
= $config['coursevisible'];
1848 if (isset($config['contentitem'])) {
1849 $type->lti_contentitem
= $config['contentitem'];
1852 if (isset($config['debuglaunch'])) {
1853 $type->lti_debuglaunch
= $config['debuglaunch'];
1856 if (isset($config['module_class_type'])) {
1857 $type->lti_module_class_type
= $config['module_class_type'];
1863 function lti_prepare_type_for_save($type, $config) {
1864 if (isset($config->lti_toolurl
)) {
1865 $type->baseurl
= $config->lti_toolurl
;
1866 $type->tooldomain
= lti_get_domain_from_url($config->lti_toolurl
);
1868 if (isset($config->lti_description
)) {
1869 $type->description
= $config->lti_description
;
1871 if (isset($config->lti_typename
)) {
1872 $type->name
= $config->lti_typename
;
1874 if (isset($config->lti_coursevisible
)) {
1875 $type->coursevisible
= $config->lti_coursevisible
;
1878 if (isset($config->lti_icon
)) {
1879 $type->icon
= $config->lti_icon
;
1881 if (isset($config->lti_secureicon
)) {
1882 $type->secureicon
= $config->lti_secureicon
;
1885 $type->forcessl
= !empty($config->lti_forcessl
) ?
$config->lti_forcessl
: 0;
1886 $config->lti_forcessl
= $type->forcessl
;
1887 if (isset($config->lti_contentitem
)) {
1888 $type->contentitem
= !empty($config->lti_contentitem
) ?
$config->lti_contentitem
: 0;
1889 $config->lti_contentitem
= $type->contentitem
;
1892 $type->timemodified
= time();
1894 unset ($config->lti_typename
);
1895 unset ($config->lti_toolurl
);
1896 unset ($config->lti_description
);
1897 unset ($config->lti_icon
);
1898 unset ($config->lti_secureicon
);
1901 function lti_update_type($type, $config) {
1904 lti_prepare_type_for_save($type, $config);
1906 $clearcache = false;
1907 if (lti_request_is_using_ssl() && !empty($type->secureicon
)) {
1908 $clearcache = !isset($config->oldicon
) ||
($config->oldicon
!== $type->secureicon
);
1910 $clearcache = isset($type->icon
) && (!isset($config->oldicon
) ||
($config->oldicon
!== $type->icon
));
1912 unset($config->oldicon
);
1914 if ($DB->update_record('lti_types', $type)) {
1915 foreach ($config as $key => $value) {
1916 if (substr($key, 0, 4) == 'lti_' && !is_null($value)) {
1917 $record = new \
StdClass();
1918 $record->typeid
= $type->id
;
1919 $record->name
= substr($key, 4);
1920 $record->value
= $value;
1921 lti_update_config($record);
1924 require_once($CFG->libdir
.'/modinfolib.php');
1926 $sql = "SELECT DISTINCT course
1930 $courses = $DB->get_fieldset_sql($sql, array($type->id
));
1932 foreach ($courses as $courseid) {
1933 rebuild_course_cache($courseid, true);
1939 function lti_add_type($type, $config) {
1940 global $USER, $SITE, $DB;
1942 lti_prepare_type_for_save($type, $config);
1944 if (!isset($type->state
)) {
1945 $type->state
= LTI_TOOL_STATE_PENDING
;
1948 if (!isset($type->timecreated
)) {
1949 $type->timecreated
= time();
1952 if (!isset($type->createdby
)) {
1953 $type->createdby
= $USER->id
;
1956 if (!isset($type->course
)) {
1957 $type->course
= $SITE->id
;
1960 // Create a salt value to be used for signing passed data to extension services
1961 // The outcome service uses the service salt on the instance. This can be used
1962 // for communication with services not related to a specific LTI instance.
1963 $config->lti_servicesalt
= uniqid('', true);
1965 $id = $DB->insert_record('lti_types', $type);
1968 foreach ($config as $key => $value) {
1969 if (substr($key, 0, 4) == 'lti_' && !is_null($value)) {
1970 $record = new \
StdClass();
1971 $record->typeid
= $id;
1972 $record->name
= substr($key, 4);
1973 $record->value
= $value;
1975 lti_add_config($record);
1984 * Given an array of tool proxies, filter them based on their state
1986 * @param array $toolproxies An array of lti_tool_proxies records
1987 * @param int $state One of the LTI_TOOL_PROXY_STATE_* constants
1991 function lti_filter_tool_proxy_types(array $toolproxies, $state) {
1993 foreach ($toolproxies as $key => $toolproxy) {
1994 if ($toolproxy->state
== $state) {
1995 $return[$key] = $toolproxy;
2002 * Get the tool proxy instance given its GUID
2004 * @param string $toolproxyguid Tool proxy GUID value
2008 function lti_get_tool_proxy_from_guid($toolproxyguid) {
2011 $toolproxy = $DB->get_record('lti_tool_proxies', array('guid' => $toolproxyguid));
2017 * Get the tool proxy instance given its registration URL
2019 * @param string $regurl Tool proxy registration URL
2021 * @return array The record of the tool proxy with this url
2023 function lti_get_tool_proxies_from_registration_url($regurl) {
2026 return $DB->get_records_sql(
2027 'SELECT * FROM {lti_tool_proxies}
2028 WHERE '.$DB->sql_compare_text('regurl', 256).' = :regurl',
2029 array('regurl' => $regurl)
2034 * Generates some of the tool proxy configuration based on the admin configuration details
2038 * @return Tool Proxy details
2040 function lti_get_tool_proxy($id) {
2043 $toolproxy = $DB->get_record('lti_tool_proxies', array('id' => $id));
2048 * Returns lti tool proxies.
2050 * @param bool $orphanedonly Only retrieves tool proxies that have no type associated with them
2051 * @return array of basicLTI types
2053 function lti_get_tool_proxies($orphanedonly) {
2056 if ($orphanedonly) {
2057 $tools = $DB->get_records('lti_types');
2058 $usedproxyids = array_values($DB->get_fieldset_select('lti_types', 'toolproxyid', 'toolproxyid IS NOT NULL'));
2059 $proxies = $DB->get_records('lti_tool_proxies', null, 'state DESC, timemodified DESC');
2060 foreach ($proxies as $key => $value) {
2061 if (in_array($value->id
, $usedproxyids)) {
2062 unset($proxies[$key]);
2067 return $DB->get_records('lti_tool_proxies', null, 'state DESC, timemodified DESC');
2072 * Generates some of the tool proxy configuration based on the admin configuration details
2076 * @return Tool Proxy details
2078 function lti_get_tool_proxy_config($id) {
2079 $toolproxy = lti_get_tool_proxy($id);
2081 $tp = new \
stdClass();
2082 $tp->lti_registrationname
= $toolproxy->name
;
2083 $tp->toolproxyid
= $toolproxy->id
;
2084 $tp->state
= $toolproxy->state
;
2085 $tp->lti_registrationurl
= $toolproxy->regurl
;
2086 $tp->lti_capabilities
= explode("\n", $toolproxy->capabilityoffered
);
2087 $tp->lti_services
= explode("\n", $toolproxy->serviceoffered
);
2093 * Update the database with a tool proxy instance
2095 * @param object $config Tool proxy definition
2097 * @return int Record id number
2099 function lti_add_tool_proxy($config) {
2102 $toolproxy = new \
stdClass();
2103 if (isset($config->lti_registrationname
)) {
2104 $toolproxy->name
= trim($config->lti_registrationname
);
2106 if (isset($config->lti_registrationurl
)) {
2107 $toolproxy->regurl
= trim($config->lti_registrationurl
);
2109 if (isset($config->lti_capabilities
)) {
2110 $toolproxy->capabilityoffered
= implode("\n", $config->lti_capabilities
);
2112 $toolproxy->capabilityoffered
= implode("\n", array_keys(lti_get_capabilities()));
2114 if (isset($config->lti_services
)) {
2115 $toolproxy->serviceoffered
= implode("\n", $config->lti_services
);
2117 $func = function($s) {
2118 return $s->get_id();
2120 $servicenames = array_map($func, lti_get_services());
2121 $toolproxy->serviceoffered
= implode("\n", $servicenames);
2123 if (isset($config->toolproxyid
) && !empty($config->toolproxyid
)) {
2124 $toolproxy->id
= $config->toolproxyid
;
2125 if (!isset($toolproxy->state
) ||
($toolproxy->state
!= LTI_TOOL_PROXY_STATE_ACCEPTED
)) {
2126 $toolproxy->state
= LTI_TOOL_PROXY_STATE_CONFIGURED
;
2127 $toolproxy->guid
= random_string();
2128 $toolproxy->secret
= random_string();
2130 $id = lti_update_tool_proxy($toolproxy);
2132 $toolproxy->state
= LTI_TOOL_PROXY_STATE_CONFIGURED
;
2133 $toolproxy->timemodified
= time();
2134 $toolproxy->timecreated
= $toolproxy->timemodified
;
2135 if (!isset($toolproxy->createdby
)) {
2136 $toolproxy->createdby
= $USER->id
;
2138 $toolproxy->guid
= random_string();
2139 $toolproxy->secret
= random_string();
2140 $id = $DB->insert_record('lti_tool_proxies', $toolproxy);
2147 * Updates a tool proxy in the database
2149 * @param object $toolproxy Tool proxy
2151 * @return int Record id number
2153 function lti_update_tool_proxy($toolproxy) {
2156 $toolproxy->timemodified
= time();
2157 $id = $DB->update_record('lti_tool_proxies', $toolproxy);
2163 * Delete a Tool Proxy
2165 * @param int $id Tool Proxy id
2167 function lti_delete_tool_proxy($id) {
2169 $DB->delete_records('lti_tool_settings', array('toolproxyid' => $id));
2170 $tools = $DB->get_records('lti_types', array('toolproxyid' => $id));
2171 foreach ($tools as $tool) {
2172 lti_delete_type($tool->id
);
2174 $DB->delete_records('lti_tool_proxies', array('id' => $id));
2178 * Add a tool configuration in the database
2180 * @param object $config Tool configuration
2182 * @return int Record id number
2184 function lti_add_config($config) {
2187 return $DB->insert_record('lti_types_config', $config);
2191 * Updates a tool configuration in the database
2193 * @param object $config Tool configuration
2195 * @return Record id number
2197 function lti_update_config($config) {
2201 $old = $DB->get_record('lti_types_config', array('typeid' => $config->typeid
, 'name' => $config->name
));
2204 $config->id
= $old->id
;
2205 $return = $DB->update_record('lti_types_config', $config);
2207 $return = $DB->insert_record('lti_types_config', $config);
2213 * Gets the tool settings
2215 * @param int $toolproxyid Id of tool proxy record
2216 * @param int $courseid Id of course (null if system settings)
2217 * @param int $instanceid Id of course module (null if system or context settings)
2219 * @return array Array settings
2221 function lti_get_tool_settings($toolproxyid, $courseid = null, $instanceid = null) {
2224 $settings = array();
2225 $settingsstr = $DB->get_field('lti_tool_settings', 'settings', array('toolproxyid' => $toolproxyid,
2226 'course' => $courseid, 'coursemoduleid' => $instanceid));
2227 if ($settingsstr !== false) {
2228 $settings = json_decode($settingsstr, true);
2234 * Sets the tool settings (
2236 * @param array $settings Array of settings
2237 * @param int $toolproxyid Id of tool proxy record
2238 * @param int $courseid Id of course (null if system settings)
2239 * @param int $instanceid Id of course module (null if system or context settings)
2241 function lti_set_tool_settings($settings, $toolproxyid, $courseid = null, $instanceid = null) {
2244 $json = json_encode($settings);
2245 $record = $DB->get_record('lti_tool_settings', array('toolproxyid' => $toolproxyid,
2246 'course' => $courseid, 'coursemoduleid' => $instanceid));
2247 if ($record !== false) {
2248 $DB->update_record('lti_tool_settings', array('id' => $record->id
, 'settings' => $json, 'timemodified' => time()));
2250 $record = new \
stdClass();
2251 $record->toolproxyid
= $toolproxyid;
2252 $record->course
= $courseid;
2253 $record->coursemoduleid
= $instanceid;
2254 $record->settings
= $json;
2255 $record->timecreated
= time();
2256 $record->timemodified
= $record->timecreated
;
2257 $DB->insert_record('lti_tool_settings', $record);
2262 * Signs the petition to launch the external tool using OAuth
2264 * @param $oldparms Parameters to be passed for signing
2265 * @param $endpoint url of the external tool
2266 * @param $method Method for sending the parameters (e.g. POST)
2267 * @param $oauth_consumoer_key Key
2268 * @param $oauth_consumoer_secret Secret
2270 function lti_sign_parameters($oldparms, $endpoint, $method, $oauthconsumerkey, $oauthconsumersecret) {
2276 // TODO: Switch to core oauthlib once implemented - MDL-30149.
2277 $hmacmethod = new lti\
OAuthSignatureMethod_HMAC_SHA1();
2278 $testconsumer = new lti\
OAuthConsumer($oauthconsumerkey, $oauthconsumersecret, null);
2279 $accreq = lti\OAuthRequest
::from_consumer_and_token($testconsumer, $testtoken, $method, $endpoint, $parms);
2280 $accreq->sign_request($hmacmethod, $testconsumer, $testtoken);
2282 $newparms = $accreq->get_parameters();
2288 * Posts the launch petition HTML
2290 * @param $newparms Signed parameters
2291 * @param $endpoint URL of the external tool
2292 * @param $debug Debug (true/false)
2294 function lti_post_launch_html($newparms, $endpoint, $debug=false) {
2295 $r = "<form action=\"" . $endpoint .
2296 "\" name=\"ltiLaunchForm\" id=\"ltiLaunchForm\" method=\"post\" encType=\"application/x-www-form-urlencoded\">\n";
2298 // Contruct html for the launch parameters.
2299 foreach ($newparms as $key => $value) {
2300 $key = htmlspecialchars($key);
2301 $value = htmlspecialchars($value);
2302 if ( $key == "ext_submit" ) {
2303 $r .= "<input type=\"submit\"";
2305 $r .= "<input type=\"hidden\" name=\"{$key}\"";
2313 $r .= "<script language=\"javascript\"> \n";
2314 $r .= " //<![CDATA[ \n";
2315 $r .= "function basicltiDebugToggle() {\n";
2316 $r .= " var ele = document.getElementById(\"basicltiDebug\");\n";
2317 $r .= " if (ele.style.display == \"block\") {\n";
2318 $r .= " ele.style.display = \"none\";\n";
2321 $r .= " ele.style.display = \"block\";\n";
2325 $r .= "</script>\n";
2326 $r .= "<a id=\"displayText\" href=\"javascript:basicltiDebugToggle();\">";
2327 $r .= get_string("toggle_debug_data", "lti")."</a>\n";
2328 $r .= "<div id=\"basicltiDebug\" style=\"display:none\">\n";
2329 $r .= "<b>".get_string("basiclti_endpoint", "lti")."</b><br/>\n";
2330 $r .= $endpoint . "<br/>\n <br/>\n";
2331 $r .= "<b>".get_string("basiclti_parameters", "lti")."</b><br/>\n";
2332 foreach ($newparms as $key => $value) {
2333 $key = htmlspecialchars($key);
2334 $value = htmlspecialchars($value);
2335 $r .= "$key = $value<br/>\n";
2337 $r .= " <br/>\n";
2343 $r .= " <script type=\"text/javascript\"> \n" .
2345 " document.ltiLaunchForm.submit(); \n" .
2352 function lti_get_type($typeid) {
2355 return $DB->get_record('lti_types', array('id' => $typeid));
2358 function lti_get_launch_container($lti, $toolconfig) {
2359 if (empty($lti->launchcontainer
)) {
2360 $lti->launchcontainer
= LTI_LAUNCH_CONTAINER_DEFAULT
;
2363 if ($lti->launchcontainer
== LTI_LAUNCH_CONTAINER_DEFAULT
) {
2364 if (isset($toolconfig['launchcontainer'])) {
2365 $launchcontainer = $toolconfig['launchcontainer'];
2368 $launchcontainer = $lti->launchcontainer
;
2371 if (empty($launchcontainer) ||
$launchcontainer == LTI_LAUNCH_CONTAINER_DEFAULT
) {
2372 $launchcontainer = LTI_LAUNCH_CONTAINER_EMBED_NO_BLOCKS
;
2375 $devicetype = core_useragent
::get_device_type();
2377 // Scrolling within the object element doesn't work on iOS or Android
2378 // Opening the popup window also had some issues in testing
2379 // For mobile devices, always take up the entire screen to ensure the best experience.
2380 if ($devicetype === core_useragent
::DEVICETYPE_MOBILE ||
$devicetype === core_useragent
::DEVICETYPE_TABLET
) {
2381 $launchcontainer = LTI_LAUNCH_CONTAINER_REPLACE_MOODLE_WINDOW
;
2384 return $launchcontainer;
2387 function lti_request_is_using_ssl() {
2389 return (stripos($CFG->wwwroot
, 'https://') === 0);
2392 function lti_ensure_url_is_https($url) {
2393 if (!strstr($url, '://')) {
2394 $url = 'https://' . $url;
2396 // If the URL starts with http, replace with https.
2397 if (stripos($url, 'http://') === 0) {
2398 $url = 'https://' . substr($url, 7);
2406 * Determines if we should try to log the request
2408 * @param string $rawbody
2411 function lti_should_log_request($rawbody) {
2414 if (empty($CFG->mod_lti_log_users
)) {
2418 $logusers = explode(',', $CFG->mod_lti_log_users
);
2419 if (empty($logusers)) {
2424 $xml = new \
SimpleXMLElement($rawbody);
2425 $ns = $xml->getNamespaces();
2426 $ns = array_shift($ns);
2427 $xml->registerXPathNamespace('lti', $ns);
2428 $requestuserid = '';
2429 if ($node = $xml->xpath('//lti:userId')) {
2431 $requestuserid = clean_param((string) $node, PARAM_INT
);
2432 } else if ($node = $xml->xpath('//lti:sourcedId')) {
2434 $resultjson = json_decode((string) $node);
2435 $requestuserid = clean_param($resultjson->data
->userid
, PARAM_INT
);
2437 } catch (Exception
$e) {
2441 if (empty($requestuserid) or !in_array($requestuserid, $logusers)) {
2449 * Logs the request to a file in temp dir.
2451 * @param string $rawbody
2453 function lti_log_request($rawbody) {
2454 if ($tempdir = make_temp_directory('mod_lti', false)) {
2455 if ($tempfile = tempnam($tempdir, 'mod_lti_request'.date('YmdHis'))) {
2456 $content = "Request Headers:\n";
2457 foreach (moodle\mod\lti\OAuthUtil
::get_headers() as $header => $value) {
2458 $content .= "$header: $value\n";
2460 $content .= "Request Body:\n";
2461 $content .= $rawbody;
2463 file_put_contents($tempfile, $content);
2464 chmod($tempfile, 0644);
2470 * Log an LTI response.
2472 * @param string $responsexml The response XML
2473 * @param Exception $e If there was an exception, pass that too
2475 function lti_log_response($responsexml, $e = null) {
2476 if ($tempdir = make_temp_directory('mod_lti', false)) {
2477 if ($tempfile = tempnam($tempdir, 'mod_lti_response'.date('YmdHis'))) {
2479 if ($e instanceof Exception
) {
2480 $info = get_exception_info($e);
2482 $content .= "Exception:\n";
2483 $content .= "Message: $info->message\n";
2484 $content .= "Debug info: $info->debuginfo\n";
2485 $content .= "Backtrace:\n";
2486 $content .= format_backtrace($info->backtrace
, true);
2489 $content .= "Response XML:\n";
2490 $content .= $responsexml;
2492 file_put_contents($tempfile, $content);
2493 chmod($tempfile, 0644);
2499 * Fetches LTI type configuration for an LTI instance
2501 * @param stdClass $instance
2502 * @return array Can be empty if no type is found
2504 function lti_get_type_config_by_instance($instance) {
2506 if (empty($instance->typeid
)) {
2507 $tool = lti_get_tool_by_url_match($instance->toolurl
, $instance->course
);
2509 $typeid = $tool->id
;
2512 $typeid = $instance->typeid
;
2514 if (!empty($typeid)) {
2515 return lti_get_type_config($typeid);
2521 * Enforce type config settings onto the LTI instance
2523 * @param stdClass $instance
2524 * @param array $typeconfig
2526 function lti_force_type_config_settings($instance, array $typeconfig) {
2528 'instructorchoicesendname' => 'sendname',
2529 'instructorchoicesendemailaddr' => 'sendemailaddr',
2530 'instructorchoiceacceptgrades' => 'acceptgrades',
2533 foreach ($forced as $instanceparam => $typeconfigparam) {
2534 if (array_key_exists($typeconfigparam, $typeconfig) && $typeconfig[$typeconfigparam] != LTI_SETTING_DELEGATE
) {
2535 $instance->$instanceparam = $typeconfig[$typeconfigparam];
2541 * Initializes an array with the capabilities supported by the LTI module
2543 * @return array List of capability names (without a dollar sign prefix)
2545 function lti_get_capabilities() {
2547 $capabilities = array(
2548 'basic-lti-launch-request' => '',
2549 'ContentItemSelectionRequest' => '',
2550 'ToolProxyRegistrationRequest' => '',
2551 'Context.id' => 'context_id',
2552 'Context.title' => 'context_title',
2553 'Context.label' => 'context_label',
2554 'Context.sourcedId' => 'lis_course_section_sourcedid',
2555 'Context.longDescription' => '$COURSE->summary',
2556 'Context.timeFrame.begin' => '$COURSE->startdate',
2557 'CourseSection.title' => 'context_title',
2558 'CourseSection.label' => 'context_label',
2559 'CourseSection.sourcedId' => 'lis_course_section_sourcedid',
2560 'CourseSection.longDescription' => '$COURSE->summary',
2561 'CourseSection.timeFrame.begin' => '$COURSE->startdate',
2562 'ResourceLink.id' => 'resource_link_id',
2563 'ResourceLink.title' => 'resource_link_title',
2564 'ResourceLink.description' => 'resource_link_description',
2565 'User.id' => 'user_id',
2566 'User.username' => '$USER->username',
2567 'Person.name.full' => 'lis_person_name_full',
2568 'Person.name.given' => 'lis_person_name_given',
2569 'Person.name.family' => 'lis_person_name_family',
2570 'Person.email.primary' => 'lis_person_contact_email_primary',
2571 'Person.sourcedId' => 'lis_person_sourcedid',
2572 'Person.name.middle' => '$USER->middlename',
2573 'Person.address.street1' => '$USER->address',
2574 'Person.address.locality' => '$USER->city',
2575 'Person.address.country' => '$USER->country',
2576 'Person.address.timezone' => '$USER->timezone',
2577 'Person.phone.primary' => '$USER->phone1',
2578 'Person.phone.mobile' => '$USER->phone2',
2579 'Person.webaddress' => '$USER->url',
2580 'Membership.role' => 'roles',
2581 'Result.sourcedId' => 'lis_result_sourcedid',
2582 'Result.autocreate' => 'lis_outcome_service_url',
2583 'Moodle.Person.userGroupIds' => null);
2585 return $capabilities;
2590 * Initializes an array with the services supported by the LTI module
2592 * @return array List of services
2594 function lti_get_services() {
2596 $services = array();
2597 $definedservices = core_component
::get_plugin_list('ltiservice');
2598 foreach ($definedservices as $name => $location) {
2599 $classname = "\\ltiservice_{$name}\\local\\service\\{$name}";
2600 $services[] = new $classname();
2608 * Initializes an instance of the named service
2610 * @param string $servicename Name of service
2612 * @return mod_lti\local\ltiservice\service_base Service
2614 function lti_get_service_by_name($servicename) {
2617 $classname = "\\ltiservice_{$servicename}\\local\\service\\{$servicename}";
2618 if (class_exists($classname)) {
2619 $service = new $classname();
2627 * Finds a service by id
2629 * @param array $services Array of services
2630 * @param string $resourceid ID of resource
2632 * @return mod_lti\local\ltiservice\service_base Service
2634 function lti_get_service_by_resource_id($services, $resourceid) {
2637 foreach ($services as $aservice) {
2638 foreach ($aservice->get_resources() as $resource) {
2639 if ($resource->get_id() === $resourceid) {
2640 $service = $aservice;
2651 * Extracts the named contexts from a tool proxy
2653 * @param object $json
2655 * @return array Contexts
2657 function lti_get_contexts($json) {
2659 $contexts = array();
2660 if (isset($json->{'@context'})) {
2661 foreach ($json->{'@context'} as $context) {
2662 if (is_object($context)) {
2663 $contexts = array_merge(get_object_vars($context), $contexts);
2673 * Converts an ID to a fully-qualified ID
2675 * @param array $contexts
2678 * @return string Fully-qualified ID
2680 function lti_get_fqid($contexts, $id) {
2682 $parts = explode(':', $id, 2);
2683 if (count($parts) > 1) {
2684 if (array_key_exists($parts[0], $contexts)) {
2685 $id = $contexts[$parts[0]] . $parts[1];
2694 * Returns the icon for the given tool type
2696 * @param stdClass $type The tool type
2698 * @return string The url to the tool type's corresponding icon
2700 function get_tool_type_icon_url(stdClass
$type) {
2703 $iconurl = $type->secureicon
;
2705 if (empty($iconurl)) {
2706 $iconurl = $type->icon
;
2709 if (empty($iconurl)) {
2710 $iconurl = $OUTPUT->image_url('icon', 'lti')->out();
2717 * Returns the edit url for the given tool type
2719 * @param stdClass $type The tool type
2721 * @return string The url to edit the tool type
2723 function get_tool_type_edit_url(stdClass
$type) {
2724 $url = new moodle_url('/mod/lti/typessettings.php',
2725 array('action' => 'update', 'id' => $type->id
, 'sesskey' => sesskey(), 'returnto' => 'toolconfigure'));
2730 * Returns the edit url for the given tool proxy.
2732 * @param stdClass $proxy The tool proxy
2734 * @return string The url to edit the tool type
2736 function get_tool_proxy_edit_url(stdClass
$proxy) {
2737 $url = new moodle_url('/mod/lti/registersettings.php',
2738 array('action' => 'update', 'id' => $proxy->id
, 'sesskey' => sesskey(), 'returnto' => 'toolconfigure'));
2743 * Returns the course url for the given tool type
2745 * @param stdClass $type The tool type
2747 * @return string|void The url to the course of the tool type, void if it is a site wide type
2749 function get_tool_type_course_url(stdClass
$type) {
2750 if ($type->course
== 1) {
2753 $url = new moodle_url('/course/view.php', array('id' => $type->course
));
2759 * Returns the icon and edit urls for the tool type and the course url if it is a course type.
2761 * @param stdClass $type The tool type
2763 * @return string The urls of the tool type
2765 function get_tool_type_urls(stdClass
$type) {
2766 $courseurl = get_tool_type_course_url($type);
2769 'icon' => get_tool_type_icon_url($type),
2770 'edit' => get_tool_type_edit_url($type),
2774 $urls['course'] = $courseurl;
2781 * Returns the icon and edit urls for the tool proxy.
2783 * @param stdClass $proxy The tool proxy
2785 * @return string The urls of the tool proxy
2787 function get_tool_proxy_urls(stdClass
$proxy) {
2791 'icon' => $OUTPUT->image_url('icon', 'lti')->out(),
2792 'edit' => get_tool_proxy_edit_url($proxy),
2799 * Returns information on the current state of the tool type
2801 * @param stdClass $type The tool type
2803 * @return array An array with a text description of the state, and boolean for whether it is in each state:
2804 * pending, configured, rejected, unknown
2806 function get_tool_type_state_info(stdClass
$type) {
2808 $isconfigured = false;
2810 $isrejected = false;
2812 switch ($type->state
) {
2813 case LTI_TOOL_STATE_CONFIGURED
:
2814 $state = get_string('active', 'mod_lti');
2815 $isconfigured = true;
2817 case LTI_TOOL_STATE_PENDING
:
2818 $state = get_string('pending', 'mod_lti');
2821 case LTI_TOOL_STATE_REJECTED
:
2822 $state = get_string('rejected', 'mod_lti');
2826 $state = get_string('unknownstate', 'mod_lti');
2833 'pending' => $ispending,
2834 'configured' => $isconfigured,
2835 'rejected' => $isrejected,
2836 'unknown' => $isunknown
2841 * Returns a summary of each LTI capability this tool type requires in plain language
2843 * @param stdClass $type The tool type
2845 * @return array An array of text descriptions of each of the capabilities this tool type requires
2847 function get_tool_type_capability_groups($type) {
2848 $capabilities = lti_get_enabled_capabilities($type);
2851 $hasactivities = false;
2852 $hasuseraccount = false;
2853 $hasuserpersonal = false;
2855 foreach ($capabilities as $capability) {
2856 // Bail out early if we've already found all groups.
2857 if (count($groups) >= 4) {
2861 if (!$hascourse && preg_match('/^CourseSection/', $capability)) {
2863 $groups[] = get_string('courseinformation', 'mod_lti');
2864 } else if (!$hasactivities && preg_match('/^ResourceLink/', $capability)) {
2865 $hasactivities = true;
2866 $groups[] = get_string('courseactivitiesorresources', 'mod_lti');
2867 } else if (!$hasuseraccount && preg_match('/^User/', $capability) ||
preg_match('/^Membership/', $capability)) {
2868 $hasuseraccount = true;
2869 $groups[] = get_string('useraccountinformation', 'mod_lti');
2870 } else if (!$hasuserpersonal && preg_match('/^Person/', $capability)) {
2871 $hasuserpersonal = true;
2872 $groups[] = get_string('userpersonalinformation', 'mod_lti');
2881 * Returns the ids of each instance of this tool type
2883 * @param stdClass $type The tool type
2885 * @return array An array of ids of the instances of this tool type
2887 function get_tool_type_instance_ids($type) {
2890 return array_keys($DB->get_fieldset_select('lti', 'id', 'typeid = ?', array($type->id
)));
2894 * Serialises this tool type
2896 * @param stdClass $type The tool type
2898 * @return array An array of values representing this type
2900 function serialise_tool_type(stdClass
$type) {
2901 $capabilitygroups = get_tool_type_capability_groups($type);
2902 $instanceids = get_tool_type_instance_ids($type);
2903 // Clean the name. We don't want tags here.
2904 $name = clean_param($type->name
, PARAM_NOTAGS
);
2905 if (!empty($type->description
)) {
2906 // Clean the description. We don't want tags here.
2907 $description = clean_param($type->description
, PARAM_NOTAGS
);
2909 $description = get_string('editdescription', 'mod_lti');
2914 'description' => $description,
2915 'urls' => get_tool_type_urls($type),
2916 'state' => get_tool_type_state_info($type),
2917 'hascapabilitygroups' => !empty($capabilitygroups),
2918 'capabilitygroups' => $capabilitygroups,
2919 // Course ID of 1 means it's not linked to a course.
2920 'courseid' => $type->course
== 1 ?
0 : $type->course
,
2921 'instanceids' => $instanceids,
2922 'instancecount' => count($instanceids)
2927 * Serialises this tool proxy.
2929 * @param stdClass $proxy The tool proxy
2931 * @return array An array of values representing this type
2933 function serialise_tool_proxy(stdClass
$proxy) {
2936 'name' => $proxy->name
,
2937 'description' => get_string('activatetoadddescription', 'mod_lti'),
2938 'urls' => get_tool_proxy_urls($proxy),
2940 'text' => get_string('pending', 'mod_lti'),
2942 'configured' => false,
2943 'rejected' => false,
2946 'hascapabilitygroups' => true,
2947 'capabilitygroups' => array(),
2949 'instanceids' => array(),
2950 'instancecount' => 0
2955 * Loads the cartridge information into the tool type, if the launch url is for a cartridge file
2957 * @param stdClass $type The tool type object to be filled in
2960 function lti_load_type_if_cartridge($type) {
2961 if (!empty($type->lti_toolurl
) && lti_is_cartridge($type->lti_toolurl
)) {
2962 lti_load_type_from_cartridge($type->lti_toolurl
, $type);
2967 * Loads the cartridge information into the new tool, if the launch url is for a cartridge file
2969 * @param stdClass $lti The tools config
2972 function lti_load_tool_if_cartridge($lti) {
2973 if (!empty($lti->toolurl
) && lti_is_cartridge($lti->toolurl
)) {
2974 lti_load_tool_from_cartridge($lti->toolurl
, $lti);
2979 * Determines if the given url is for a IMS basic cartridge
2981 * @param string $url The url to be checked
2982 * @return True if the url is for a cartridge
2985 function lti_is_cartridge($url) {
2986 // If it is empty, it's not a cartridge.
2990 // If it has xml at the end of the url, it's a cartridge.
2991 if (preg_match('/\.xml$/', $url)) {
2994 // Even if it doesn't have .xml, load the url to check if it's a cartridge..
2996 $toolinfo = lti_load_cartridge($url,
2998 "launch_url" => "launchurl"
3001 if (!empty($toolinfo['launchurl'])) {
3004 } catch (moodle_exception
$e) {
3005 return false; // Error loading the xml, so it's not a cartridge.
3011 * Allows you to load settings for an external tool type from an IMS cartridge.
3013 * @param string $url The URL to the cartridge
3014 * @param stdClass $type The tool type object to be filled in
3015 * @throws moodle_exception if the cartridge could not be loaded correctly
3018 function lti_load_type_from_cartridge($url, $type) {
3019 $toolinfo = lti_load_cartridge($url,
3021 "title" => "lti_typename",
3022 "launch_url" => "lti_toolurl",
3023 "description" => "lti_description",
3024 "icon" => "lti_icon",
3025 "secure_icon" => "lti_secureicon"
3028 "icon_url" => "lti_extension_icon",
3029 "secure_icon_url" => "lti_extension_secureicon"
3032 // If an activity name exists, unset the cartridge name so we don't override it.
3033 if (isset($type->lti_typename
)) {
3034 unset($toolinfo['lti_typename']);
3037 // Always prefer cartridge core icons first, then, if none are found, look at the extension icons.
3038 if (empty($toolinfo['lti_icon']) && !empty($toolinfo['lti_extension_icon'])) {
3039 $toolinfo['lti_icon'] = $toolinfo['lti_extension_icon'];
3041 unset($toolinfo['lti_extension_icon']);
3043 if (empty($toolinfo['lti_secureicon']) && !empty($toolinfo['lti_extension_secureicon'])) {
3044 $toolinfo['lti_secureicon'] = $toolinfo['lti_extension_secureicon'];
3046 unset($toolinfo['lti_extension_secureicon']);
3048 // Ensure Custom icons aren't overridden by cartridge params.
3049 if (!empty($type->lti_icon
)) {
3050 unset($toolinfo['lti_icon']);
3053 if (!empty($type->lti_secureicon
)) {
3054 unset($toolinfo['lti_secureicon']);
3057 foreach ($toolinfo as $property => $value) {
3058 $type->$property = $value;
3063 * Allows you to load in the configuration for an external tool from an IMS cartridge.
3065 * @param string $url The URL to the cartridge
3066 * @param stdClass $lti LTI object
3067 * @throws moodle_exception if the cartridge could not be loaded correctly
3070 function lti_load_tool_from_cartridge($url, $lti) {
3071 $toolinfo = lti_load_cartridge($url,
3074 "launch_url" => "toolurl",
3075 "secure_launch_url" => "securetoolurl",
3076 "description" => "intro",
3078 "secure_icon" => "secureicon"
3081 "icon_url" => "extension_icon",
3082 "secure_icon_url" => "extension_secureicon"
3085 // If an activity name exists, unset the cartridge name so we don't override it.
3086 if (isset($lti->name
)) {
3087 unset($toolinfo['name']);
3090 // Always prefer cartridge core icons first, then, if none are found, look at the extension icons.
3091 if (empty($toolinfo['icon']) && !empty($toolinfo['extension_icon'])) {
3092 $toolinfo['icon'] = $toolinfo['extension_icon'];
3094 unset($toolinfo['extension_icon']);
3096 if (empty($toolinfo['secureicon']) && !empty($toolinfo['extension_secureicon'])) {
3097 $toolinfo['secureicon'] = $toolinfo['extension_secureicon'];
3099 unset($toolinfo['extension_secureicon']);
3101 foreach ($toolinfo as $property => $value) {
3102 $lti->$property = $value;
3107 * Search for a tag within an XML DOMDocument
3109 * @param string $url The url of the cartridge to be loaded
3110 * @param array $map The map of tags to keys in the return array
3111 * @param array $propertiesmap The map of properties to keys in the return array
3112 * @return array An associative array with the given keys and their values from the cartridge
3113 * @throws moodle_exception if the cartridge could not be loaded correctly
3116 function lti_load_cartridge($url, $map, $propertiesmap = array()) {
3118 require_once($CFG->libdir
. "/filelib.php");
3121 $response = $curl->get($url);
3123 // TODO MDL-46023 Replace this code with a call to the new library.
3124 $origerrors = libxml_use_internal_errors(true);
3125 $origentity = libxml_disable_entity_loader(true);
3126 libxml_clear_errors();
3128 $document = new DOMDocument();
3129 @$document->loadXML($response, LIBXML_DTDLOAD | LIBXML_DTDATTR
);
3131 $cartridge = new DomXpath($document);
3133 $errors = libxml_get_errors();
3135 libxml_clear_errors();
3136 libxml_use_internal_errors($origerrors);
3137 libxml_disable_entity_loader($origentity);
3139 if (count($errors) > 0) {
3140 $message = 'Failed to load cartridge.';
3141 foreach ($errors as $error) {
3142 $message .= "\n" . trim($error->message
, "\n\r\t .") . " at line " . $error->line
;
3144 throw new moodle_exception('errorreadingfile', '', '', $url, $message);
3147 $toolinfo = array();
3148 foreach ($map as $tag => $key) {
3149 $value = get_tag($tag, $cartridge);
3151 $toolinfo[$key] = $value;
3154 if (!empty($propertiesmap)) {
3155 foreach ($propertiesmap as $property => $key) {
3156 $value = get_tag("property", $cartridge, $property);
3158 $toolinfo[$key] = $value;
3167 * Search for a tag within an XML DOMDocument
3169 * @param stdClass $tagname The name of the tag to search for
3170 * @param XPath $xpath The XML to find the tag in
3171 * @param XPath $attribute The attribute to search for (if we should search for a child node with the given
3172 * value for the name attribute
3175 function get_tag($tagname, $xpath, $attribute = null) {
3177 $result = $xpath->query('//*[local-name() = \'' . $tagname . '\'][@name="' . $attribute . '"]');
3179 $result = $xpath->query('//*[local-name() = \'' . $tagname . '\']');
3181 if ($result->length
> 0) {
3182 return $result->item(0)->nodeValue
;