MDL-57765 behat: Allow exact selectors to be used
[moodle.git] / mod / lti / locallib.php
bloba2e07ecfa3de189264e4a396eb9fe934ec8b4cbe
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 // This file is part of BasicLTI4Moodle
19 // BasicLTI4Moodle is an IMS BasicLTI (Basic Learning Tools for Interoperability)
20 // consumer for Moodle 1.9 and Moodle 2.0. BasicLTI is a IMS Standard that allows web
21 // based learning tools to be easily integrated in LMS as native ones. The IMS BasicLTI
22 // specification is part of the IMS standard Common Cartridge 1.1 Sakai and other main LMS
23 // are already supporting or going to support BasicLTI. This project Implements the consumer
24 // for Moodle. Moodle is a Free Open source Learning Management System by Martin Dougiamas.
25 // BasicLTI4Moodle is a project iniciated and leaded by Ludo(Marc Alier) and Jordi Piguillem
26 // at the GESSI research group at UPC.
27 // SimpleLTI consumer for Moodle is an implementation of the early specification of LTI
28 // by Charles Severance (Dr Chuck) htp://dr-chuck.com , developed by Jordi Piguillem in a
29 // Google Summer of Code 2008 project co-mentored by Charles Severance and Marc Alier.
31 // BasicLTI4Moodle is copyright 2009 by Marc Alier Forment, Jordi Piguillem and Nikolas Galanis
32 // of the Universitat Politecnica de Catalunya http://www.upc.edu
33 // Contact info: Marc Alier Forment granludo @ gmail.com or marc.alier @ upc.edu.
35 /**
36 * This file contains the library of functions and constants for the lti module
38 * @package mod_lti
39 * @copyright 2009 Marc Alier, Jordi Piguillem, Nikolas Galanis
40 * marc.alier@upc.edu
41 * @copyright 2009 Universitat Politecnica de Catalunya http://www.upc.edu
42 * @author Marc Alier
43 * @author Jordi Piguillem
44 * @author Nikolas Galanis
45 * @author Chris Scribner
46 * @copyright 2015 Vital Source Technologies http://vitalsource.com
47 * @author Stephen Vickers
48 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
51 defined('MOODLE_INTERNAL') || die;
53 // TODO: Switch to core oauthlib once implemented - MDL-30149.
54 use moodle\mod\lti as lti;
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');
91 /**
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)
96 * @since Moodle 3.0
98 function lti_get_launch_data($instance) {
99 global $PAGE, $CFG;
101 if (empty($instance->typeid)) {
102 $tool = lti_get_tool_by_url_match($instance->toolurl, $instance->course);
103 if ($tool) {
104 $typeid = $tool->id;
105 } else {
106 $typeid = null;
108 } else {
109 $typeid = $instance->typeid;
110 $tool = lti_get_type($typeid);
113 if ($typeid) {
114 $typeconfig = lti_get_type_config($typeid);
115 } else {
116 // There is no admin configuration for this tool. Use configuration in the lti instance record plus some defaults.
117 $typeconfig = (array)$instance;
119 $typeconfig['sendname'] = $instance->instructorchoicesendname;
120 $typeconfig['sendemailaddr'] = $instance->instructorchoicesendemailaddr;
121 $typeconfig['customparameters'] = $instance->instructorcustomparameters;
122 $typeconfig['acceptgrades'] = $instance->instructorchoiceacceptgrades;
123 $typeconfig['allowroster'] = $instance->instructorchoiceallowroster;
124 $typeconfig['forcessl'] = '0';
127 // Default the organizationid if not specified.
128 if (empty($typeconfig['organizationid'])) {
129 $urlparts = parse_url($CFG->wwwroot);
131 $typeconfig['organizationid'] = $urlparts['host'];
134 if (isset($tool->toolproxyid)) {
135 $toolproxy = lti_get_tool_proxy($tool->toolproxyid);
136 $key = $toolproxy->guid;
137 $secret = $toolproxy->secret;
138 } else {
139 $toolproxy = null;
140 if (!empty($instance->resourcekey)) {
141 $key = $instance->resourcekey;
142 } else if (!empty($typeconfig['resourcekey'])) {
143 $key = $typeconfig['resourcekey'];
144 } else {
145 $key = '';
147 if (!empty($instance->password)) {
148 $secret = $instance->password;
149 } else if (!empty($typeconfig['password'])) {
150 $secret = $typeconfig['password'];
151 } else {
152 $secret = '';
156 $endpoint = !empty($instance->toolurl) ? $instance->toolurl : $typeconfig['toolurl'];
157 $endpoint = trim($endpoint);
159 // If the current request is using SSL and a secure tool URL is specified, use it.
160 if (lti_request_is_using_ssl() && !empty($instance->securetoolurl)) {
161 $endpoint = trim($instance->securetoolurl);
164 // If SSL is forced, use the secure tool url if specified. Otherwise, make sure https is on the normal launch URL.
165 if (isset($typeconfig['forcessl']) && ($typeconfig['forcessl'] == '1')) {
166 if (!empty($instance->securetoolurl)) {
167 $endpoint = trim($instance->securetoolurl);
170 $endpoint = lti_ensure_url_is_https($endpoint);
171 } else {
172 if (!strstr($endpoint, '://')) {
173 $endpoint = 'http://' . $endpoint;
177 $orgid = $typeconfig['organizationid'];
179 $course = $PAGE->course;
180 $islti2 = isset($tool->toolproxyid);
181 $allparams = lti_build_request($instance, $typeconfig, $course, $typeid, $islti2);
182 if ($islti2) {
183 $requestparams = lti_build_request_lti2($tool, $allparams);
184 } else {
185 $requestparams = $allparams;
187 $requestparams = array_merge($requestparams, lti_build_standard_request($instance, $orgid, $islti2));
188 $customstr = '';
189 if (isset($typeconfig['customparameters'])) {
190 $customstr = $typeconfig['customparameters'];
192 $requestparams = array_merge($requestparams, lti_build_custom_parameters($toolproxy, $tool, $instance, $allparams, $customstr,
193 $instance->instructorcustomparameters, $islti2));
195 $launchcontainer = lti_get_launch_container($instance, $typeconfig);
196 $returnurlparams = array('course' => $course->id,
197 'launch_container' => $launchcontainer,
198 'instanceid' => $instance->id,
199 'sesskey' => sesskey());
201 // Add the return URL. We send the launch container along to help us avoid frames-within-frames when the user returns.
202 $url = new \moodle_url('/mod/lti/return.php', $returnurlparams);
203 $returnurl = $url->out(false);
205 if (isset($typeconfig['forcessl']) && ($typeconfig['forcessl'] == '1')) {
206 $returnurl = lti_ensure_url_is_https($returnurl);
209 $target = '';
210 switch($launchcontainer) {
211 case LTI_LAUNCH_CONTAINER_EMBED:
212 case LTI_LAUNCH_CONTAINER_EMBED_NO_BLOCKS:
213 $target = 'iframe';
214 break;
215 case LTI_LAUNCH_CONTAINER_REPLACE_MOODLE_WINDOW:
216 $target = 'frame';
217 break;
218 case LTI_LAUNCH_CONTAINER_WINDOW:
219 $target = 'window';
220 break;
222 if (!empty($target)) {
223 $requestparams['launch_presentation_document_target'] = $target;
226 $requestparams['launch_presentation_return_url'] = $returnurl;
228 // Allow request params to be updated by sub-plugins.
229 $plugins = core_component::get_plugin_list('ltisource');
230 foreach (array_keys($plugins) as $plugin) {
231 $pluginparams = component_callback('ltisource_'.$plugin, 'before_launch',
232 array($instance, $endpoint, $requestparams), array());
234 if (!empty($pluginparams) && is_array($pluginparams)) {
235 $requestparams = array_merge($requestparams, $pluginparams);
239 if (!empty($key) && !empty($secret)) {
240 $parms = lti_sign_parameters($requestparams, $endpoint, "POST", $key, $secret);
242 $endpointurl = new \moodle_url($endpoint);
243 $endpointparams = $endpointurl->params();
245 // Strip querystring params in endpoint url from $parms to avoid duplication.
246 if (!empty($endpointparams) && !empty($parms)) {
247 foreach (array_keys($endpointparams) as $paramname) {
248 if (isset($parms[$paramname])) {
249 unset($parms[$paramname]);
254 } else {
255 // If no key and secret, do the launch unsigned.
256 $returnurlparams['unsigned'] = '1';
257 $parms = $requestparams;
260 return array($endpoint, $parms);
264 * Launch an external tool activity.
266 * @param stdClass $instance the external tool activity settings
267 * @return string The HTML code containing the javascript code for the launch
269 function lti_launch_tool($instance) {
271 list($endpoint, $parms) = lti_get_launch_data($instance);
272 $debuglaunch = ( $instance->debuglaunch == 1 );
274 $content = lti_post_launch_html($parms, $endpoint, $debuglaunch);
276 echo $content;
280 * Prepares an LTI registration request message
282 * $param object $instance Tool Proxy instance object
284 function lti_register($toolproxy) {
285 $endpoint = $toolproxy->regurl;
287 // Change the status to pending.
288 $toolproxy->state = LTI_TOOL_PROXY_STATE_PENDING;
289 lti_update_tool_proxy($toolproxy);
291 $requestparams = lti_build_registration_request($toolproxy);
293 $content = lti_post_launch_html($requestparams, $endpoint, false);
295 echo $content;
300 * Gets the parameters for the regirstration request
302 * @param object $toolproxy Tool Proxy instance object
303 * @return array Registration request parameters
305 function lti_build_registration_request($toolproxy) {
306 $key = $toolproxy->guid;
307 $secret = $toolproxy->secret;
309 $requestparams = array();
310 $requestparams['lti_message_type'] = 'ToolProxyRegistrationRequest';
311 $requestparams['lti_version'] = 'LTI-2p0';
312 $requestparams['reg_key'] = $key;
313 $requestparams['reg_password'] = $secret;
314 $requestparams['reg_url'] = $toolproxy->regurl;
316 // Add the profile URL.
317 $profileservice = lti_get_service_by_name('profile');
318 $profileservice->set_tool_proxy($toolproxy);
319 $requestparams['tc_profile_url'] = $profileservice->parse_value('$ToolConsumerProfile.url');
321 // Add the return URL.
322 $returnurlparams = array('id' => $toolproxy->id, 'sesskey' => sesskey());
323 $url = new \moodle_url('/mod/lti/externalregistrationreturn.php', $returnurlparams);
324 $returnurl = $url->out(false);
326 $requestparams['launch_presentation_return_url'] = $returnurl;
328 return $requestparams;
332 * Build source ID
334 * @param int $instanceid
335 * @param int $userid
336 * @param string $servicesalt
337 * @param null|int $typeid
338 * @param null|int $launchid
339 * @return stdClass
341 function lti_build_sourcedid($instanceid, $userid, $servicesalt, $typeid = null, $launchid = null) {
342 $data = new \stdClass();
344 $data->instanceid = $instanceid;
345 $data->userid = $userid;
346 $data->typeid = $typeid;
347 if (!empty($launchid)) {
348 $data->launchid = $launchid;
349 } else {
350 $data->launchid = mt_rand();
353 $json = json_encode($data);
355 $hash = hash('sha256', $json . $servicesalt, false);
357 $container = new \stdClass();
358 $container->data = $data;
359 $container->hash = $hash;
361 return $container;
365 * This function builds the request that must be sent to the tool producer
367 * @param object $instance Basic LTI instance object
368 * @param array $typeconfig Basic LTI tool configuration
369 * @param object $course Course object
370 * @param int|null $typeid Basic LTI tool ID
371 * @param boolean $islti2 True if an LTI 2 tool is being launched
373 * @return array Request details
375 function lti_build_request($instance, $typeconfig, $course, $typeid = null, $islti2 = false) {
376 global $USER, $CFG;
378 if (empty($instance->cmid)) {
379 $instance->cmid = 0;
382 $role = lti_get_ims_role($USER, $instance->cmid, $instance->course, $islti2);
384 $requestparams = array(
385 'user_id' => $USER->id,
386 'lis_person_sourcedid' => $USER->idnumber,
387 'roles' => $role,
388 'context_id' => $course->id,
389 'context_label' => $course->shortname,
390 'context_title' => $course->fullname,
392 if (!empty($instance->name)) {
393 $requestparams['resource_link_title'] = $instance->name;
395 if (!empty($instance->cmid)) {
396 $intro = format_module_intro('lti', $instance, $instance->cmid);
397 $intro = html_to_text($intro, 0, false);
399 // This may look weird, but this is required for new lines
400 // so we generate the same OAuth signature as the tool provider.
401 $intro = str_replace("\n", "\r\n", $intro);
402 $requestparams['resource_link_description'] = $intro;
404 if (!empty($instance->id)) {
405 $requestparams['resource_link_id'] = $instance->id;
407 if (!empty($instance->resource_link_id)) {
408 $requestparams['resource_link_id'] = $instance->resource_link_id;
410 if ($course->format == 'site') {
411 $requestparams['context_type'] = 'Group';
412 } else {
413 $requestparams['context_type'] = 'CourseSection';
414 $requestparams['lis_course_section_sourcedid'] = $course->idnumber;
417 if (!empty($instance->id) && !empty($instance->servicesalt) && ($islti2 ||
418 $typeconfig['acceptgrades'] == LTI_SETTING_ALWAYS ||
419 ($typeconfig['acceptgrades'] == LTI_SETTING_DELEGATE && $instance->instructorchoiceacceptgrades == LTI_SETTING_ALWAYS))
421 $placementsecret = $instance->servicesalt;
422 $sourcedid = json_encode(lti_build_sourcedid($instance->id, $USER->id, $placementsecret, $typeid));
423 $requestparams['lis_result_sourcedid'] = $sourcedid;
425 // Add outcome service URL.
426 $serviceurl = new \moodle_url('/mod/lti/service.php');
427 $serviceurl = $serviceurl->out();
429 $forcessl = false;
430 if (!empty($CFG->mod_lti_forcessl)) {
431 $forcessl = true;
434 if ((isset($typeconfig['forcessl']) && ($typeconfig['forcessl'] == '1')) or $forcessl) {
435 $serviceurl = lti_ensure_url_is_https($serviceurl);
438 $requestparams['lis_outcome_service_url'] = $serviceurl;
441 // Send user's name and email data if appropriate.
442 if ($islti2 || $typeconfig['sendname'] == LTI_SETTING_ALWAYS ||
443 ($typeconfig['sendname'] == LTI_SETTING_DELEGATE && isset($instance->instructorchoicesendname)
444 && $instance->instructorchoicesendname == LTI_SETTING_ALWAYS)
446 $requestparams['lis_person_name_given'] = $USER->firstname;
447 $requestparams['lis_person_name_family'] = $USER->lastname;
448 $requestparams['lis_person_name_full'] = $USER->firstname . ' ' . $USER->lastname;
449 $requestparams['ext_user_username'] = $USER->username;
452 if ($islti2 || $typeconfig['sendemailaddr'] == LTI_SETTING_ALWAYS ||
453 ($typeconfig['sendemailaddr'] == LTI_SETTING_DELEGATE && isset($instance->instructorchoicesendemailaddr)
454 && $instance->instructorchoicesendemailaddr == LTI_SETTING_ALWAYS)
456 $requestparams['lis_person_contact_email_primary'] = $USER->email;
459 return $requestparams;
463 * This function builds the request that must be sent to an LTI 2 tool provider
465 * @param object $tool Basic LTI tool object
466 * @param array $params Custom launch parameters
468 * @return array Request details
470 function lti_build_request_lti2($tool, $params) {
472 $requestparams = array();
474 $capabilities = lti_get_capabilities();
475 $enabledcapabilities = explode("\n", $tool->enabledcapability);
476 foreach ($enabledcapabilities as $capability) {
477 if (array_key_exists($capability, $capabilities)) {
478 $val = $capabilities[$capability];
479 if ($val && (substr($val, 0, 1) != '$')) {
480 if (isset($params[$val])) {
481 $requestparams[$capabilities[$capability]] = $params[$capabilities[$capability]];
487 return $requestparams;
492 * This function builds the standard parameters for an LTI 1 or 2 request that must be sent to the tool producer
494 * @param stdClass $instance Basic LTI instance object
495 * @param string $orgid Organisation ID
496 * @param boolean $islti2 True if an LTI 2 tool is being launched
497 * @param string $messagetype The request message type. Defaults to basic-lti-launch-request if empty.
499 * @return array Request details
501 function lti_build_standard_request($instance, $orgid, $islti2, $messagetype = 'basic-lti-launch-request') {
502 global $CFG;
504 $requestparams = array();
506 if ($instance) {
507 $requestparams['resource_link_id'] = $instance->id;
508 if (property_exists($instance, 'resource_link_id') and !empty($instance->resource_link_id)) {
509 $requestparams['resource_link_id'] = $instance->resource_link_id;
513 $requestparams['launch_presentation_locale'] = current_language();
515 // Make sure we let the tool know what LMS they are being called from.
516 $requestparams['ext_lms'] = 'moodle-2';
517 $requestparams['tool_consumer_info_product_family_code'] = 'moodle';
518 $requestparams['tool_consumer_info_version'] = strval($CFG->version);
520 // Add oauth_callback to be compliant with the 1.0A spec.
521 $requestparams['oauth_callback'] = 'about:blank';
523 if (!$islti2) {
524 $requestparams['lti_version'] = 'LTI-1p0';
525 } else {
526 $requestparams['lti_version'] = 'LTI-2p0';
528 $requestparams['lti_message_type'] = $messagetype;
530 if ($orgid) {
531 $requestparams["tool_consumer_instance_guid"] = $orgid;
533 if (!empty($CFG->mod_lti_institution_name)) {
534 $requestparams['tool_consumer_instance_name'] = $CFG->mod_lti_institution_name;
535 } else {
536 $requestparams['tool_consumer_instance_name'] = get_site()->shortname;
538 $requestparams['tool_consumer_instance_description'] = get_site()->fullname;
540 return $requestparams;
544 * This function builds the custom parameters
546 * @param object $toolproxy Tool proxy instance object
547 * @param object $tool Tool instance object
548 * @param object $instance Tool placement instance object
549 * @param array $params LTI launch parameters
550 * @param string $customstr Custom parameters defined for tool
551 * @param string $instructorcustomstr Custom parameters defined for this placement
552 * @param boolean $islti2 True if an LTI 2 tool is being launched
554 * @return array Custom parameters
556 function lti_build_custom_parameters($toolproxy, $tool, $instance, $params, $customstr, $instructorcustomstr, $islti2) {
558 // Concatenate the custom parameters from the administrator and the instructor
559 // Instructor parameters are only taken into consideration if the administrator
560 // has given permission.
561 $custom = array();
562 if ($customstr) {
563 $custom = lti_split_custom_parameters($toolproxy, $tool, $params, $customstr, $islti2);
565 if (!isset($typeconfig['allowinstructorcustom']) || $typeconfig['allowinstructorcustom'] != LTI_SETTING_NEVER) {
566 if ($instructorcustomstr) {
567 $custom = array_merge(lti_split_custom_parameters($toolproxy, $tool, $params,
568 $instructorcustomstr, $islti2), $custom);
571 if ($islti2) {
572 $custom = array_merge(lti_split_custom_parameters($toolproxy, $tool, $params,
573 $tool->parameter, true), $custom);
574 $settings = lti_get_tool_settings($tool->toolproxyid);
575 $custom = array_merge($custom, lti_get_custom_parameters($toolproxy, $tool, $params, $settings));
576 if (!empty($instance->course)) {
577 $settings = lti_get_tool_settings($tool->toolproxyid, $instance->course);
578 $custom = array_merge($custom, lti_get_custom_parameters($toolproxy, $tool, $params, $settings));
579 if (!empty($instance->id)) {
580 $settings = lti_get_tool_settings($tool->toolproxyid, $instance->course, $instance->id);
581 $custom = array_merge($custom, lti_get_custom_parameters($toolproxy, $tool, $params, $settings));
586 return $custom;
590 * Builds a standard LTI Content-Item selection request.
592 * @param int $id The tool type ID.
593 * @param stdClass $course The course object.
594 * @param moodle_url $returnurl The return URL in the tool consumer (TC) that the tool provider (TP)
595 * will use to return the Content-Item message.
596 * @param string $title The tool's title, if available.
597 * @param string $text The text to display to represent the content item. This value may be a long description of the content item.
598 * @param array $mediatypes Array of MIME types types supported by the TC. If empty, the TC will support ltilink by default.
599 * @param array $presentationtargets Array of ways in which the selected content item(s) can be requested to be opened
600 * (via the presentationDocumentTarget element for a returned content item).
601 * If empty, "frame", "iframe", and "window" will be supported by default.
602 * @param bool $autocreate Indicates whether any content items returned by the TP would be automatically persisted without
603 * @param bool $multiple Indicates whether the user should be permitted to select more than one item. False by default.
604 * any option for the user to cancel the operation. False by default.
605 * @param bool $unsigned Indicates whether the TC is willing to accept an unsigned return message, or not.
606 * A signed message should always be required when the content item is being created automatically in the
607 * TC without further interaction from the user. False by default.
608 * @param bool $canconfirm Flag for can_confirm parameter. False by default.
609 * @param bool $copyadvice Indicates whether the TC is able and willing to make a local copy of a content item. False by default.
610 * @return stdClass The object containing the signed request parameters and the URL to the TP's Content-Item selection interface.
611 * @throws moodle_exception When the LTI tool type does not exist.`
612 * @throws coding_exception For invalid media type and presentation target parameters.
614 function lti_build_content_item_selection_request($id, $course, moodle_url $returnurl, $title = '', $text = '', $mediatypes = [],
615 $presentationtargets = [], $autocreate = false, $multiple = false,
616 $unsigned = false, $canconfirm = false, $copyadvice = false) {
617 $tool = lti_get_type($id);
618 // Validate parameters.
619 if (!$tool) {
620 throw new moodle_exception('errortooltypenotfound', 'mod_lti');
622 if (!is_array($mediatypes)) {
623 throw new coding_exception('The list of accepted media types should be in an array');
625 if (!is_array($presentationtargets)) {
626 throw new coding_exception('The list of accepted presentation targets should be in an array');
629 // Check title. If empty, use the tool's name.
630 if (empty($title)) {
631 $title = $tool->name;
634 $typeconfig = lti_get_type_config($id);
635 $key = '';
636 $secret = '';
637 $islti2 = false;
638 if (isset($tool->toolproxyid)) {
639 $islti2 = true;
640 $toolproxy = lti_get_tool_proxy($tool->toolproxyid);
641 $key = $toolproxy->guid;
642 $secret = $toolproxy->secret;
643 } else {
644 $toolproxy = null;
645 if (!empty($typeconfig['resourcekey'])) {
646 $key = $typeconfig['resourcekey'];
648 if (!empty($typeconfig['password'])) {
649 $secret = $typeconfig['password'];
652 $tool->enabledcapability = '';
653 if (!empty($typeconfig['enabledcapability_ContentItemSelectionRequest'])) {
654 $tool->enabledcapability = $typeconfig['enabledcapability_ContentItemSelectionRequest'];
657 $tool->parameter = '';
658 if (!empty($typeconfig['parameter_ContentItemSelectionRequest'])) {
659 $tool->parameter = $typeconfig['parameter_ContentItemSelectionRequest'];
662 // Set the tool URL.
663 if (!empty($typeconfig['toolurl_ContentItemSelectionRequest'])) {
664 $toolurl = new moodle_url($typeconfig['toolurl_ContentItemSelectionRequest']);
665 } else {
666 $toolurl = new moodle_url($typeconfig['toolurl']);
669 // Check if SSL is forced.
670 if (!empty($typeconfig['forcessl'])) {
671 // Make sure the tool URL is set to https.
672 if (strtolower($toolurl->get_scheme()) === 'http') {
673 $toolurl->set_scheme('https');
675 // Make sure the return URL is set to https.
676 if (strtolower($returnurl->get_scheme()) === 'http') {
677 $returnurl->set_scheme('https');
680 $toolurlout = $toolurl->out(false);
682 // Get base request parameters.
683 $instance = new stdClass();
684 $instance->course = $course->id;
685 $requestparams = lti_build_request($instance, $typeconfig, $course, $id, $islti2);
687 // Get LTI2-specific request parameters and merge to the request parameters if applicable.
688 if ($islti2) {
689 $lti2params = lti_build_request_lti2($tool, $requestparams);
690 $requestparams = array_merge($requestparams, $lti2params);
693 // Get standard request parameters and merge to the request parameters.
694 $orgid = !empty($typeconfig['organizationid']) ? $typeconfig['organizationid'] : '';
695 $standardparams = lti_build_standard_request(null, $orgid, $islti2, 'ContentItemSelectionRequest');
696 $requestparams = array_merge($requestparams, $standardparams);
698 // Get custom request parameters and merge to the request parameters.
699 $customstr = '';
700 if (!empty($typeconfig['customparameters'])) {
701 $customstr = $typeconfig['customparameters'];
703 $customparams = lti_build_custom_parameters($toolproxy, $tool, $instance, $requestparams, $customstr, '', $islti2);
704 $requestparams = array_merge($requestparams, $customparams);
706 // Allow request params to be updated by sub-plugins.
707 $plugins = core_component::get_plugin_list('ltisource');
708 foreach (array_keys($plugins) as $plugin) {
709 $pluginparams = component_callback('ltisource_' . $plugin, 'before_launch', [$instance, $toolurlout, $requestparams], []);
711 if (!empty($pluginparams) && is_array($pluginparams)) {
712 $requestparams = array_merge($requestparams, $pluginparams);
716 // Media types. Set to ltilink by default if empty.
717 if (empty($mediatypes)) {
718 $mediatypes = [
719 'application/vnd.ims.lti.v1.ltilink',
722 $requestparams['accept_media_types'] = implode(',', $mediatypes);
724 // Presentation targets. Supports frame, iframe, window by default if empty.
725 if (empty($presentationtargets)) {
726 $presentationtargets = [
727 'frame',
728 'iframe',
729 'window',
732 $requestparams['accept_presentation_document_targets'] = implode(',', $presentationtargets);
734 // Other request parameters.
735 $requestparams['accept_copy_advice'] = $copyadvice === true ? 'true' : 'false';
736 $requestparams['accept_multiple'] = $multiple === true ? 'true' : 'false';
737 $requestparams['accept_unsigned'] = $unsigned === true ? 'true' : 'false';
738 $requestparams['auto_create'] = $autocreate === true ? 'true' : 'false';
739 $requestparams['can_confirm'] = $canconfirm === true ? 'true' : 'false';
740 $requestparams['content_item_return_url'] = $returnurl->out(false);
741 $requestparams['title'] = $title;
742 $requestparams['text'] = $text;
743 $signedparams = lti_sign_parameters($requestparams, $toolurlout, 'POST', $key, $secret);
744 $toolurlparams = $toolurl->params();
746 // Strip querystring params in endpoint url from $signedparams to avoid duplication.
747 if (!empty($toolurlparams) && !empty($signedparams)) {
748 foreach (array_keys($toolurlparams) as $paramname) {
749 if (isset($signedparams[$paramname])) {
750 unset($signedparams[$paramname]);
755 // Check for params that should not be passed. Unset if they are set.
756 $unwantedparams = [
757 'resource_link_id',
758 'resource_link_title',
759 'resource_link_description',
760 'launch_presentation_return_url',
761 'lis_result_sourcedid',
763 foreach ($unwantedparams as $param) {
764 if (isset($signedparams[$param])) {
765 unset($signedparams[$param]);
769 // Prepare result object.
770 $result = new stdClass();
771 $result->params = $signedparams;
772 $result->url = $toolurlout;
774 return $result;
778 * Processes the tool provider's response to the ContentItemSelectionRequest and builds the configuration data from the
779 * selected content item. This configuration data can be then used when adding a tool into the course.
781 * @param int $typeid The tool type ID.
782 * @param string $messagetype The value for the lti_message_type parameter.
783 * @param string $ltiversion The value for the lti_version parameter.
784 * @param string $consumerkey The consumer key.
785 * @param string $contentitemsjson The JSON string for the content_items parameter.
786 * @return stdClass The array of module information objects.
787 * @throws moodle_exception
788 * @throws lti\OAuthException
790 function lti_tool_configuration_from_content_item($typeid, $messagetype, $ltiversion, $consumerkey, $contentitemsjson) {
791 $tool = lti_get_type($typeid);
792 // Validate parameters.
793 if (!$tool) {
794 throw new moodle_exception('errortooltypenotfound', 'mod_lti');
796 // Check lti_message_type. Show debugging if it's not set to ContentItemSelection.
797 // No need to throw exceptions for now since lti_message_type does not seem to be used in this processing at the moment.
798 if ($messagetype !== 'ContentItemSelection') {
799 debugging("lti_message_type is invalid: {$messagetype}. It should be set to 'ContentItemSelection'.",
800 DEBUG_DEVELOPER);
803 $typeconfig = lti_get_type_config($typeid);
805 if (isset($tool->toolproxyid)) {
806 $islti2 = true;
807 $toolproxy = lti_get_tool_proxy($tool->toolproxyid);
808 $key = $toolproxy->guid;
809 $secret = $toolproxy->secret;
810 } else {
811 $islti2 = false;
812 $toolproxy = null;
813 if (!empty($typeconfig['resourcekey'])) {
814 $key = $typeconfig['resourcekey'];
815 } else {
816 $key = '';
818 if (!empty($typeconfig['password'])) {
819 $secret = $typeconfig['password'];
820 } else {
821 $secret = '';
825 // Check LTI versions from our side and the response's side. Show debugging if they don't match.
826 // No need to throw exceptions for now since LTI version does not seem to be used in this processing at the moment.
827 $expectedversion = LTI_VERSION_1;
828 if ($islti2) {
829 $expectedversion = LTI_VERSION_2;
831 if ($ltiversion !== $expectedversion) {
832 debugging("lti_version from response does not match the tool's configuration. Tool: {$expectedversion}," .
833 " Response: {$ltiversion}", DEBUG_DEVELOPER);
836 if ($consumerkey !== $key) {
837 throw new moodle_exception('errorincorrectconsumerkey', 'mod_lti');
840 $store = new lti\TrivialOAuthDataStore();
841 $store->add_consumer($key, $secret);
842 $server = new lti\OAuthServer($store);
843 $method = new lti\OAuthSignatureMethod_HMAC_SHA1();
844 $server->add_signature_method($method);
845 $request = lti\OAuthRequest::from_request();
846 try {
847 $server->verify_request($request);
848 } catch (lti\OAuthException $e) {
849 throw new lti\OAuthException("OAuth signature failed: " . $e->getMessage());
852 $items = json_decode($contentitemsjson);
853 if (empty($items)) {
854 throw new moodle_exception('errorinvaliddata', 'mod_lti', '', $contentitemsjson);
856 if ($items->{'@context'} !== 'http://purl.imsglobal.org/ctx/lti/v1/ContentItem') {
857 throw new moodle_exception('errorinvalidmediatype', 'mod_lti', '', $items->{'@context'});
859 if (!isset($items->{'@graph'}) || !is_array($items->{'@graph'}) || (count($items->{'@graph'}) > 1)) {
860 throw new moodle_exception('errorinvalidresponseformat', 'mod_lti');
863 $config = null;
864 if (!empty($items->{'@graph'})) {
865 $item = $items->{'@graph'}[0];
867 $config = new stdClass();
868 $config->name = '';
869 if (isset($item->title)) {
870 $config->name = $item->title;
872 if (empty($config->name)) {
873 $config->name = $tool->name;
875 if (isset($item->text)) {
876 $config->introeditor = [
877 'text' => $item->text,
878 'format' => FORMAT_PLAIN
881 if (isset($item->icon->{'@id'})) {
882 $iconurl = new moodle_url($item->icon->{'@id'});
883 // Assign item's icon URL to secureicon or icon depending on its scheme.
884 if (strtolower($iconurl->get_scheme()) === 'https') {
885 $config->secureicon = $iconurl->out(false);
886 } else {
887 $config->icon = $iconurl->out(false);
890 if (isset($item->url)) {
891 $url = new moodle_url($item->url);
892 // Assign item URL to securetoolurl or toolurl depending on its scheme.
893 if (strtolower($url->get_scheme()) === 'https') {
894 $config->securetoolurl = $url->out(false);
895 } else {
896 $config->toolurl = $url->out(false);
898 $config->typeid = 0;
899 } else {
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);
923 return $config;
926 function lti_get_tool_table($tools, $id) {
927 global $CFG, $OUTPUT, $USER;
928 $html = '';
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)) {
936 $html .= "
937 <div id=\"{$id}_tools_container\" style=\"margin-top:.5em;margin-bottom:.5em\">
938 <table id=\"{$id}_tools\">
939 <thead>
940 <tr>
941 <th>$typename</th>
942 <th>$baseurl</th>
943 <th>$createdon</th>
944 <th>$action</th>
945 </tr>
946 </thead>
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',
958 'id' => $type->id,
959 'sesskey' => sesskey(),
960 'tab' => $id
962 $ref = $type->baseurl;
963 } else {
964 $baseurl = new \moodle_url('/mod/lti/toolssettings.php', array(
965 'action' => 'accept',
966 'id' => $type->id,
967 'sesskey' => sesskey(),
968 'tab' => $id
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) {
980 $accepthtml = '';
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'));
1000 } else {
1001 $deletehtml = '';
1003 $html .= "
1004 <tr>
1005 <td>
1006 {$type->name}
1007 </td>
1008 <td>
1009 {$ref}
1010 </td>
1011 <td>
1012 {$date}
1013 </td>
1014 <td align=\"center\">
1015 {$accepthtml}{$updatehtml}{$deletehtml}
1016 </td>
1017 </tr>
1020 $html .= '</table></div>';
1021 } else {
1022 $html .= get_string('no_' . $id, 'lti');
1025 return $html;
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) {
1037 global $OUTPUT;
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');
1045 $html = <<< EOD
1046 <div id="{$id}_tool_proxies_container" style="margin-top: 0.5em; margin-bottom: 0.5em">
1047 <table id="{$id}_tool_proxies">
1048 <thead>
1049 <tr>
1050 <th>{$typename}</th>
1051 <th>{$url}</th>
1052 <th>{$createdon}</th>
1053 <th>{$action}</th>
1054 </tr>
1055 </thead>
1056 EOD;
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(),
1067 'tab' => $id
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) {
1083 $accepthtml = '';
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'));
1101 $html .= <<< EOD
1102 <tr>
1103 <td>
1104 {$toolproxy->name}
1105 </td>
1106 <td>
1107 {$toolproxy->regurl}
1108 </td>
1109 <td>
1110 {$date}
1111 </td>
1112 <td align="center">
1113 {$accepthtml}{$updatehtml}{$deletehtml}
1114 </td>
1115 </tr>
1116 EOD;
1118 $html .= '</table></div>';
1119 } else {
1120 $html = get_string('no_' . $id, 'lti');
1123 return $html;
1127 * Extracts the enabled capabilities into an array, including those implicitly declared in a parameter
1129 * @param object $tool Tool instance object
1131 * @return Array of enabled capabilities
1133 function lti_get_enabled_capabilities($tool) {
1134 if (!empty($tool->enabledcapability)) {
1135 $enabledcapabilities = explode("\n", $tool->enabledcapability);
1136 } else {
1137 $enabledcapabilities = array();
1139 $paramstr = str_replace("\r\n", "\n", $tool->parameter);
1140 $paramstr = str_replace("\n\r", "\n", $paramstr);
1141 $paramstr = str_replace("\r", "\n", $paramstr);
1142 $params = explode("\n", $paramstr);
1143 foreach ($params as $param) {
1144 $pos = strpos($param, '=');
1145 if (($pos === false) || ($pos < 1)) {
1146 continue;
1148 $value = trim(core_text::substr($param, $pos + 1, strlen($param)));
1149 if (substr($value, 0, 1) == '$') {
1150 $value = substr($value, 1);
1151 if (!in_array($value, $enabledcapabilities)) {
1152 $enabledcapabilities[] = $value;
1156 return $enabledcapabilities;
1160 * Splits the custom parameters field to the various parameters
1162 * @param object $toolproxy Tool proxy instance object
1163 * @param object $tool Tool instance object
1164 * @param array $params LTI launch parameters
1165 * @param string $customstr String containing the parameters
1166 * @param boolean $islti2 True if an LTI 2 tool is being launched
1168 * @return Array of custom parameters
1170 function lti_split_custom_parameters($toolproxy, $tool, $params, $customstr, $islti2 = false) {
1171 $customstr = str_replace("\r\n", "\n", $customstr);
1172 $customstr = str_replace("\n\r", "\n", $customstr);
1173 $customstr = str_replace("\r", "\n", $customstr);
1174 $lines = explode("\n", $customstr); // Or should this split on "/[\n;]/"?
1175 $retval = array();
1176 foreach ($lines as $line) {
1177 $pos = strpos($line, '=');
1178 if ( $pos === false || $pos < 1 ) {
1179 continue;
1181 $key = trim(core_text::substr($line, 0, $pos));
1182 $val = trim(core_text::substr($line, $pos + 1, strlen($line)));
1183 $val = lti_parse_custom_parameter($toolproxy, $tool, $params, $val, $islti2);
1184 $key2 = lti_map_keyname($key);
1185 $retval['custom_'.$key2] = $val;
1186 if ($islti2 && ($key != $key2)) {
1187 $retval['custom_'.$key] = $val;
1190 return $retval;
1194 * Adds the custom parameters to an array
1196 * @param object $toolproxy Tool proxy instance object
1197 * @param object $tool Tool instance object
1198 * @param array $params LTI launch parameters
1199 * @param array $parameters Array containing the parameters
1201 * @return array Array of custom parameters
1203 function lti_get_custom_parameters($toolproxy, $tool, $params, $parameters) {
1204 $retval = array();
1205 foreach ($parameters as $key => $val) {
1206 $key2 = lti_map_keyname($key);
1207 $val = lti_parse_custom_parameter($toolproxy, $tool, $params, $val, true);
1208 $retval['custom_'.$key2] = $val;
1209 if ($key != $key2) {
1210 $retval['custom_'.$key] = $val;
1213 return $retval;
1217 * Parse a custom parameter to replace any substitution variables
1219 * @param object $toolproxy Tool proxy instance object
1220 * @param object $tool Tool instance object
1221 * @param array $params LTI launch parameters
1222 * @param string $value Custom parameter value
1223 * @param boolean $islti2 True if an LTI 2 tool is being launched
1225 * @return Parsed value of custom parameter
1227 function lti_parse_custom_parameter($toolproxy, $tool, $params, $value, $islti2) {
1228 global $USER, $COURSE;
1230 if ($value) {
1231 if (substr($value, 0, 1) == '\\') {
1232 $value = substr($value, 1);
1233 } else if (substr($value, 0, 1) == '$') {
1234 $value1 = substr($value, 1);
1235 $enabledcapabilities = lti_get_enabled_capabilities($tool);
1236 if (!$islti2 || in_array($value1, $enabledcapabilities)) {
1237 $capabilities = lti_get_capabilities();
1238 if (array_key_exists($value1, $capabilities)) {
1239 $val = $capabilities[$value1];
1240 if ($val) {
1241 if (substr($val, 0, 1) != '$') {
1242 $value = $params[$val];
1243 } else {
1244 $valarr = explode('->', substr($val, 1), 2);
1245 $value = "{${$valarr[0]}->{$valarr[1]}}";
1246 $value = str_replace('<br />' , ' ', $value);
1247 $value = str_replace('<br>' , ' ', $value);
1248 $value = format_string($value);
1251 } else if ($islti2) {
1252 $val = $value;
1253 $services = lti_get_services();
1254 foreach ($services as $service) {
1255 $service->set_tool_proxy($toolproxy);
1256 $value = $service->parse_value($val);
1257 if ($val != $value) {
1258 break;
1265 return $value;
1269 * Used for building the names of the different custom parameters
1271 * @param string $key Parameter name
1273 * @return string Processed name
1275 function lti_map_keyname($key) {
1276 $newkey = "";
1277 $key = core_text::strtolower(trim($key));
1278 foreach (str_split($key) as $ch) {
1279 if ( ($ch >= 'a' && $ch <= 'z') || ($ch >= '0' && $ch <= '9') ) {
1280 $newkey .= $ch;
1281 } else {
1282 $newkey .= '_';
1285 return $newkey;
1289 * Gets the IMS role string for the specified user and LTI course module.
1291 * @param mixed $user User object or user id
1292 * @param int $cmid The course module id of the LTI activity
1293 * @param int $courseid The course id of the LTI activity
1294 * @param boolean $islti2 True if an LTI 2 tool is being launched
1296 * @return string A role string suitable for passing with an LTI launch
1298 function lti_get_ims_role($user, $cmid, $courseid, $islti2) {
1299 $roles = array();
1301 if (empty($cmid)) {
1302 // If no cmid is passed, check if the user is a teacher in the course
1303 // This allows other modules to programmatically "fake" a launch without
1304 // a real LTI instance.
1305 $coursecontext = context_course::instance($courseid);
1307 if (has_capability('moodle/course:manageactivities', $coursecontext, $user)) {
1308 array_push($roles, 'Instructor');
1309 } else {
1310 array_push($roles, 'Learner');
1312 } else {
1313 $context = context_module::instance($cmid);
1315 if (has_capability('mod/lti:manage', $context)) {
1316 array_push($roles, 'Instructor');
1317 } else {
1318 array_push($roles, 'Learner');
1322 if (is_siteadmin($user)) {
1323 if (!$islti2) {
1324 array_push($roles, 'urn:lti:sysrole:ims/lis/Administrator', 'urn:lti:instrole:ims/lis/Administrator');
1325 } else {
1326 array_push($roles, 'http://purl.imsglobal.org/vocab/lis/v2/person#Administrator');
1330 return join(',', $roles);
1334 * Returns configuration details for the tool
1336 * @param int $typeid Basic LTI tool typeid
1338 * @return array Tool Configuration
1340 function lti_get_type_config($typeid) {
1341 global $DB;
1343 $query = "SELECT name, value
1344 FROM {lti_types_config}
1345 WHERE typeid = :typeid1
1346 UNION ALL
1347 SELECT 'toolurl' AS name, baseurl AS value
1348 FROM {lti_types}
1349 WHERE id = :typeid2
1350 UNION ALL
1351 SELECT 'icon' AS name, icon AS value
1352 FROM {lti_types}
1353 WHERE id = :typeid3
1354 UNION ALL
1355 SELECT 'secureicon' AS name, secureicon AS value
1356 FROM {lti_types}
1357 WHERE id = :typeid4";
1359 $typeconfig = array();
1360 $configs = $DB->get_records_sql($query,
1361 array('typeid1' => $typeid, 'typeid2' => $typeid, 'typeid3' => $typeid, 'typeid4' => $typeid));
1363 if (!empty($configs)) {
1364 foreach ($configs as $config) {
1365 $typeconfig[$config->name] = $config->value;
1369 return $typeconfig;
1372 function lti_get_tools_by_url($url, $state, $courseid = null) {
1373 $domain = lti_get_domain_from_url($url);
1375 return lti_get_tools_by_domain($domain, $state, $courseid);
1378 function lti_get_tools_by_domain($domain, $state = null, $courseid = null) {
1379 global $DB, $SITE;
1381 $filters = array('tooldomain' => $domain);
1383 $statefilter = '';
1384 $coursefilter = '';
1386 if ($state) {
1387 $statefilter = 'AND state = :state';
1390 if ($courseid && $courseid != $SITE->id) {
1391 $coursefilter = 'OR course = :courseid';
1394 $query = "SELECT *
1395 FROM {lti_types}
1396 WHERE tooldomain = :tooldomain
1397 AND (course = :siteid $coursefilter)
1398 $statefilter";
1400 return $DB->get_records_sql($query, array(
1401 'courseid' => $courseid,
1402 'siteid' => $SITE->id,
1403 'tooldomain' => $domain,
1404 'state' => $state
1409 * Returns all basicLTI tools configured by the administrator
1412 function lti_filter_get_types($course) {
1413 global $DB;
1415 if (!empty($course)) {
1416 $where = "WHERE t.course = :course";
1417 $params = array('course' => $course);
1418 } else {
1419 $where = '';
1420 $params = array();
1422 $query = "SELECT t.id, t.name, t.baseurl, t.state, t.toolproxyid, t.timecreated, tp.name tpname
1423 FROM {lti_types} t LEFT OUTER JOIN {lti_tool_proxies} tp ON t.toolproxyid = tp.id
1424 {$where}";
1425 return $DB->get_records_sql($query, $params);
1429 * Given an array of tools, filter them based on their state
1431 * @param array $tools An array of lti_types records
1432 * @param int $state One of the LTI_TOOL_STATE_* constants
1433 * @return array
1435 function lti_filter_tool_types(array $tools, $state) {
1436 $return = array();
1437 foreach ($tools as $key => $tool) {
1438 if ($tool->state == $state) {
1439 $return[$key] = $tool;
1442 return $return;
1446 * Returns all lti types visible in this course
1448 * @param int $courseid The id of the course to retieve types for
1449 * @param array $coursevisible options for 'coursevisible' field,
1450 * default [LTI_COURSEVISIBLE_PRECONFIGURED, LTI_COURSEVISIBLE_ACTIVITYCHOOSER]
1451 * @return stdClass[] All the lti types visible in the given course
1453 function lti_get_lti_types_by_course($courseid, $coursevisible = null) {
1454 global $DB, $SITE;
1456 if ($coursevisible === null) {
1457 $coursevisible = [LTI_COURSEVISIBLE_PRECONFIGURED, LTI_COURSEVISIBLE_ACTIVITYCHOOSER];
1460 list($coursevisiblesql, $coursevisparams) = $DB->get_in_or_equal($coursevisible, SQL_PARAMS_NAMED, 'coursevisible');
1461 $query = "SELECT *
1462 FROM {lti_types}
1463 WHERE coursevisible $coursevisiblesql
1464 AND (course = :siteid OR course = :courseid)
1465 AND state = :active";
1467 return $DB->get_records_sql($query,
1468 array('siteid' => $SITE->id, 'courseid' => $courseid, 'active' => LTI_TOOL_STATE_CONFIGURED) + $coursevisparams);
1472 * Returns tool types for lti add instance and edit page
1474 * @return array Array of lti types
1476 function lti_get_types_for_add_instance() {
1477 global $COURSE;
1478 $admintypes = lti_get_lti_types_by_course($COURSE->id);
1480 $types = array();
1481 $types[0] = (object)array('name' => get_string('automatic', 'lti'), 'course' => 0, 'toolproxyid' => null);
1483 foreach ($admintypes as $type) {
1484 $types[$type->id] = $type;
1487 return $types;
1491 * Returns a list of configured types in the given course
1493 * @param int $courseid The id of the course to retieve types for
1494 * @param int $sectionreturn section to return to for forming the URLs
1495 * @return array Array of lti types. Each element is object with properties: name, title, icon, help, helplink, link
1497 function lti_get_configured_types($courseid, $sectionreturn = 0) {
1498 global $OUTPUT;
1499 $types = array();
1500 $admintypes = lti_get_lti_types_by_course($courseid, [LTI_COURSEVISIBLE_ACTIVITYCHOOSER]);
1502 foreach ($admintypes as $ltitype) {
1503 $type = new stdClass();
1504 $type->modclass = MOD_CLASS_ACTIVITY;
1505 $type->name = 'lti_type_' . $ltitype->id;
1506 // Clean the name. We don't want tags here.
1507 $type->title = clean_param($ltitype->name, PARAM_NOTAGS);
1508 $trimmeddescription = trim($ltitype->description);
1509 if ($trimmeddescription != '') {
1510 // Clean the description. We don't want tags here.
1511 $type->help = clean_param($trimmeddescription, PARAM_NOTAGS);
1512 $type->helplink = get_string('modulename_shortcut_link', 'lti');
1514 if (empty($ltitype->icon)) {
1515 $type->icon = $OUTPUT->pix_icon('icon', '', 'lti', array('class' => 'icon'));
1516 } else {
1517 $type->icon = html_writer::empty_tag('img', array('src' => $ltitype->icon, 'alt' => $ltitype->name, 'class' => 'icon'));
1519 $type->link = new moodle_url('/course/modedit.php', array('add' => 'lti', 'return' => 0, 'course' => $courseid,
1520 'sr' => $sectionreturn, 'typeid' => $ltitype->id));
1521 $types[] = $type;
1523 return $types;
1526 function lti_get_domain_from_url($url) {
1527 $matches = array();
1529 if (preg_match(LTI_URL_DOMAIN_REGEX, $url, $matches)) {
1530 return $matches[1];
1534 function lti_get_tool_by_url_match($url, $courseid = null, $state = LTI_TOOL_STATE_CONFIGURED) {
1535 $possibletools = lti_get_tools_by_url($url, $state, $courseid);
1537 return lti_get_best_tool_by_url($url, $possibletools, $courseid);
1540 function lti_get_url_thumbprint($url) {
1541 // Parse URL requires a schema otherwise everything goes into 'path'. Fixed 5.4.7 or later.
1542 if (preg_match('/https?:\/\//', $url) !== 1) {
1543 $url = 'http://'.$url;
1545 $urlparts = parse_url(strtolower($url));
1546 if (!isset($urlparts['path'])) {
1547 $urlparts['path'] = '';
1550 if (!isset($urlparts['query'])) {
1551 $urlparts['query'] = '';
1554 if (!isset($urlparts['host'])) {
1555 $urlparts['host'] = '';
1558 if (substr($urlparts['host'], 0, 4) === 'www.') {
1559 $urlparts['host'] = substr($urlparts['host'], 4);
1562 $urllower = $urlparts['host'] . '/' . $urlparts['path'];
1564 if ($urlparts['query'] != '') {
1565 $urllower .= '?' . $urlparts['query'];
1568 return $urllower;
1571 function lti_get_best_tool_by_url($url, $tools, $courseid = null) {
1572 if (count($tools) === 0) {
1573 return null;
1576 $urllower = lti_get_url_thumbprint($url);
1578 foreach ($tools as $tool) {
1579 $tool->_matchscore = 0;
1581 $toolbaseurllower = lti_get_url_thumbprint($tool->baseurl);
1583 if ($urllower === $toolbaseurllower) {
1584 // 100 points for exact thumbprint match.
1585 $tool->_matchscore += 100;
1586 } else if (substr($urllower, 0, strlen($toolbaseurllower)) === $toolbaseurllower) {
1587 // 50 points if tool thumbprint starts with the base URL thumbprint.
1588 $tool->_matchscore += 50;
1591 // Prefer course tools over site tools.
1592 if (!empty($courseid)) {
1593 // Minus 10 points for not matching the course id (global tools).
1594 if ($tool->course != $courseid) {
1595 $tool->_matchscore -= 10;
1600 $bestmatch = array_reduce($tools, function($value, $tool) {
1601 if ($tool->_matchscore > $value->_matchscore) {
1602 return $tool;
1603 } else {
1604 return $value;
1607 }, (object)array('_matchscore' => -1));
1609 // None of the tools are suitable for this URL.
1610 if ($bestmatch->_matchscore <= 0) {
1611 return null;
1614 return $bestmatch;
1617 function lti_get_shared_secrets_by_key($key) {
1618 global $DB;
1620 // Look up the shared secret for the specified key in both the types_config table (for configured tools)
1621 // And in the lti resource table for ad-hoc tools.
1622 $query = "SELECT t2.value
1623 FROM {lti_types_config} t1
1624 JOIN {lti_types_config} t2 ON t1.typeid = t2.typeid
1625 JOIN {lti_types} type ON t2.typeid = type.id
1626 WHERE t1.name = 'resourcekey'
1627 AND t1.value = :key1
1628 AND t2.name = 'password'
1629 AND type.state = :configured1
1630 UNION
1631 SELECT tp.secret AS value
1632 FROM {lti_tool_proxies} tp
1633 JOIN {lti_types} t ON tp.id = t.toolproxyid
1634 WHERE tp.guid = :key2
1635 AND t.state = :configured2
1636 UNION
1637 SELECT password AS value
1638 FROM {lti}
1639 WHERE resourcekey = :key3";
1641 $sharedsecrets = $DB->get_records_sql($query, array('configured1' => LTI_TOOL_STATE_CONFIGURED,
1642 'configured2' => LTI_TOOL_STATE_CONFIGURED, 'key1' => $key, 'key2' => $key, 'key3' => $key));
1644 $values = array_map(function($item) {
1645 return $item->value;
1646 }, $sharedsecrets);
1648 // There should really only be one shared secret per key. But, we can't prevent
1649 // more than one getting entered. For instance, if the same key is used for two tool providers.
1650 return $values;
1654 * Delete a Basic LTI configuration
1656 * @param int $id Configuration id
1658 function lti_delete_type($id) {
1659 global $DB;
1661 // We should probably just copy the launch URL to the tool instances in this case... using a single query.
1663 $instances = $DB->get_records('lti', array('typeid' => $id));
1664 foreach ($instances as $instance) {
1665 $instance->typeid = 0;
1666 $DB->update_record('lti', $instance);
1669 $DB->delete_records('lti_types', array('id' => $id));
1670 $DB->delete_records('lti_types_config', array('typeid' => $id));
1673 function lti_set_state_for_type($id, $state) {
1674 global $DB;
1676 $DB->update_record('lti_types', array('id' => $id, 'state' => $state));
1680 * Transforms a basic LTI object to an array
1682 * @param object $ltiobject Basic LTI object
1684 * @return array Basic LTI configuration details
1686 function lti_get_config($ltiobject) {
1687 $typeconfig = array();
1688 $typeconfig = (array)$ltiobject;
1689 $additionalconfig = lti_get_type_config($ltiobject->typeid);
1690 $typeconfig = array_merge($typeconfig, $additionalconfig);
1691 return $typeconfig;
1696 * Generates some of the tool configuration based on the instance details
1698 * @param int $id
1700 * @return Instance configuration
1703 function lti_get_type_config_from_instance($id) {
1704 global $DB;
1706 $instance = $DB->get_record('lti', array('id' => $id));
1707 $config = lti_get_config($instance);
1709 $type = new \stdClass();
1710 $type->lti_fix = $id;
1711 if (isset($config['toolurl'])) {
1712 $type->lti_toolurl = $config['toolurl'];
1714 if (isset($config['instructorchoicesendname'])) {
1715 $type->lti_sendname = $config['instructorchoicesendname'];
1717 if (isset($config['instructorchoicesendemailaddr'])) {
1718 $type->lti_sendemailaddr = $config['instructorchoicesendemailaddr'];
1720 if (isset($config['instructorchoiceacceptgrades'])) {
1721 $type->lti_acceptgrades = $config['instructorchoiceacceptgrades'];
1723 if (isset($config['instructorchoiceallowroster'])) {
1724 $type->lti_allowroster = $config['instructorchoiceallowroster'];
1727 if (isset($config['instructorcustomparameters'])) {
1728 $type->lti_allowsetting = $config['instructorcustomparameters'];
1730 return $type;
1734 * Generates some of the tool configuration based on the admin configuration details
1736 * @param int $id
1738 * @return Configuration details
1740 function lti_get_type_type_config($id) {
1741 global $DB;
1743 $basicltitype = $DB->get_record('lti_types', array('id' => $id));
1744 $config = lti_get_type_config($id);
1746 $type = new \stdClass();
1748 $type->lti_typename = $basicltitype->name;
1750 $type->typeid = $basicltitype->id;
1752 $type->toolproxyid = $basicltitype->toolproxyid;
1754 $type->lti_toolurl = $basicltitype->baseurl;
1756 $type->lti_description = $basicltitype->description;
1758 $type->lti_parameters = $basicltitype->parameter;
1760 $type->lti_icon = $basicltitype->icon;
1762 $type->lti_secureicon = $basicltitype->secureicon;
1764 if (isset($config['resourcekey'])) {
1765 $type->lti_resourcekey = $config['resourcekey'];
1767 if (isset($config['password'])) {
1768 $type->lti_password = $config['password'];
1771 if (isset($config['sendname'])) {
1772 $type->lti_sendname = $config['sendname'];
1774 if (isset($config['instructorchoicesendname'])) {
1775 $type->lti_instructorchoicesendname = $config['instructorchoicesendname'];
1777 if (isset($config['sendemailaddr'])) {
1778 $type->lti_sendemailaddr = $config['sendemailaddr'];
1780 if (isset($config['instructorchoicesendemailaddr'])) {
1781 $type->lti_instructorchoicesendemailaddr = $config['instructorchoicesendemailaddr'];
1783 if (isset($config['acceptgrades'])) {
1784 $type->lti_acceptgrades = $config['acceptgrades'];
1786 if (isset($config['instructorchoiceacceptgrades'])) {
1787 $type->lti_instructorchoiceacceptgrades = $config['instructorchoiceacceptgrades'];
1789 if (isset($config['allowroster'])) {
1790 $type->lti_allowroster = $config['allowroster'];
1792 if (isset($config['instructorchoiceallowroster'])) {
1793 $type->lti_instructorchoiceallowroster = $config['instructorchoiceallowroster'];
1796 if (isset($config['customparameters'])) {
1797 $type->lti_customparameters = $config['customparameters'];
1800 if (isset($config['forcessl'])) {
1801 $type->lti_forcessl = $config['forcessl'];
1804 if (isset($config['organizationid'])) {
1805 $type->lti_organizationid = $config['organizationid'];
1807 if (isset($config['organizationurl'])) {
1808 $type->lti_organizationurl = $config['organizationurl'];
1810 if (isset($config['organizationdescr'])) {
1811 $type->lti_organizationdescr = $config['organizationdescr'];
1813 if (isset($config['launchcontainer'])) {
1814 $type->lti_launchcontainer = $config['launchcontainer'];
1817 if (isset($config['coursevisible'])) {
1818 $type->lti_coursevisible = $config['coursevisible'];
1821 if (isset($config['contentitem'])) {
1822 $type->lti_contentitem = $config['contentitem'];
1825 if (isset($config['debuglaunch'])) {
1826 $type->lti_debuglaunch = $config['debuglaunch'];
1829 if (isset($config['module_class_type'])) {
1830 $type->lti_module_class_type = $config['module_class_type'];
1833 return $type;
1836 function lti_prepare_type_for_save($type, $config) {
1837 if (isset($config->lti_toolurl)) {
1838 $type->baseurl = $config->lti_toolurl;
1839 $type->tooldomain = lti_get_domain_from_url($config->lti_toolurl);
1841 if (isset($config->lti_description)) {
1842 $type->description = $config->lti_description;
1844 if (isset($config->lti_typename)) {
1845 $type->name = $config->lti_typename;
1847 if (isset($config->lti_coursevisible)) {
1848 $type->coursevisible = $config->lti_coursevisible;
1851 if (isset($config->lti_icon)) {
1852 $type->icon = $config->lti_icon;
1854 if (isset($config->lti_secureicon)) {
1855 $type->secureicon = $config->lti_secureicon;
1858 $type->forcessl = !empty($config->lti_forcessl) ? $config->lti_forcessl : 0;
1859 $config->lti_forcessl = $type->forcessl;
1860 if (isset($config->lti_contentitem)) {
1861 $type->contentitem = !empty($config->lti_contentitem) ? $config->lti_contentitem : 0;
1862 $config->lti_contentitem = $type->contentitem;
1865 $type->timemodified = time();
1867 unset ($config->lti_typename);
1868 unset ($config->lti_toolurl);
1869 unset ($config->lti_description);
1870 unset ($config->lti_icon);
1871 unset ($config->lti_secureicon);
1874 function lti_update_type($type, $config) {
1875 global $DB, $CFG;
1877 lti_prepare_type_for_save($type, $config);
1879 $clearcache = false;
1880 if (lti_request_is_using_ssl() && !empty($type->secureicon)) {
1881 $clearcache = !isset($config->oldicon) || ($config->oldicon !== $type->secureicon);
1882 } else {
1883 $clearcache = isset($type->icon) && (!isset($config->oldicon) || ($config->oldicon !== $type->icon));
1885 unset($config->oldicon);
1887 if ($DB->update_record('lti_types', $type)) {
1888 foreach ($config as $key => $value) {
1889 if (substr($key, 0, 4) == 'lti_' && !is_null($value)) {
1890 $record = new \StdClass();
1891 $record->typeid = $type->id;
1892 $record->name = substr($key, 4);
1893 $record->value = $value;
1894 lti_update_config($record);
1897 require_once($CFG->libdir.'/modinfolib.php');
1898 if ($clearcache) {
1899 $sql = "SELECT DISTINCT course
1900 FROM {lti}
1901 WHERE typeid = ?";
1903 $courses = $DB->get_fieldset_sql($sql, array($type->id));
1905 foreach ($courses as $courseid) {
1906 rebuild_course_cache($courseid, true);
1912 function lti_add_type($type, $config) {
1913 global $USER, $SITE, $DB;
1915 lti_prepare_type_for_save($type, $config);
1917 if (!isset($type->state)) {
1918 $type->state = LTI_TOOL_STATE_PENDING;
1921 if (!isset($type->timecreated)) {
1922 $type->timecreated = time();
1925 if (!isset($type->createdby)) {
1926 $type->createdby = $USER->id;
1929 if (!isset($type->course)) {
1930 $type->course = $SITE->id;
1933 // Create a salt value to be used for signing passed data to extension services
1934 // The outcome service uses the service salt on the instance. This can be used
1935 // for communication with services not related to a specific LTI instance.
1936 $config->lti_servicesalt = uniqid('', true);
1938 $id = $DB->insert_record('lti_types', $type);
1940 if ($id) {
1941 foreach ($config as $key => $value) {
1942 if (substr($key, 0, 4) == 'lti_' && !is_null($value)) {
1943 $record = new \StdClass();
1944 $record->typeid = $id;
1945 $record->name = substr($key, 4);
1946 $record->value = $value;
1948 lti_add_config($record);
1953 return $id;
1957 * Given an array of tool proxies, filter them based on their state
1959 * @param array $toolproxies An array of lti_tool_proxies records
1960 * @param int $state One of the LTI_TOOL_PROXY_STATE_* constants
1962 * @return array
1964 function lti_filter_tool_proxy_types(array $toolproxies, $state) {
1965 $return = array();
1966 foreach ($toolproxies as $key => $toolproxy) {
1967 if ($toolproxy->state == $state) {
1968 $return[$key] = $toolproxy;
1971 return $return;
1975 * Get the tool proxy instance given its GUID
1977 * @param string $toolproxyguid Tool proxy GUID value
1979 * @return object
1981 function lti_get_tool_proxy_from_guid($toolproxyguid) {
1982 global $DB;
1984 $toolproxy = $DB->get_record('lti_tool_proxies', array('guid' => $toolproxyguid));
1986 return $toolproxy;
1990 * Get the tool proxy instance given its registration URL
1992 * @param string $regurl Tool proxy registration URL
1994 * @return array The record of the tool proxy with this url
1996 function lti_get_tool_proxies_from_registration_url($regurl) {
1997 global $DB;
1999 return $DB->get_records_sql(
2000 'SELECT * FROM {lti_tool_proxies}
2001 WHERE '.$DB->sql_compare_text('regurl', 256).' = :regurl',
2002 array('regurl' => $regurl)
2007 * Generates some of the tool proxy configuration based on the admin configuration details
2009 * @param int $id
2011 * @return Tool Proxy details
2013 function lti_get_tool_proxy($id) {
2014 global $DB;
2016 $toolproxy = $DB->get_record('lti_tool_proxies', array('id' => $id));
2017 return $toolproxy;
2021 * Returns lti tool proxies.
2023 * @param bool $orphanedonly Only retrieves tool proxies that have no type associated with them
2024 * @return array of basicLTI types
2026 function lti_get_tool_proxies($orphanedonly) {
2027 global $DB;
2029 if ($orphanedonly) {
2030 $tools = $DB->get_records('lti_types');
2031 $usedproxyids = array_values($DB->get_fieldset_select('lti_types', 'toolproxyid', 'toolproxyid IS NOT NULL'));
2032 $proxies = $DB->get_records('lti_tool_proxies', null, 'state DESC, timemodified DESC');
2033 foreach ($proxies as $key => $value) {
2034 if (in_array($value->id, $usedproxyids)) {
2035 unset($proxies[$key]);
2038 return $proxies;
2039 } else {
2040 return $DB->get_records('lti_tool_proxies', null, 'state DESC, timemodified DESC');
2045 * Generates some of the tool proxy configuration based on the admin configuration details
2047 * @param int $id
2049 * @return Tool Proxy details
2051 function lti_get_tool_proxy_config($id) {
2052 $toolproxy = lti_get_tool_proxy($id);
2054 $tp = new \stdClass();
2055 $tp->lti_registrationname = $toolproxy->name;
2056 $tp->toolproxyid = $toolproxy->id;
2057 $tp->state = $toolproxy->state;
2058 $tp->lti_registrationurl = $toolproxy->regurl;
2059 $tp->lti_capabilities = explode("\n", $toolproxy->capabilityoffered);
2060 $tp->lti_services = explode("\n", $toolproxy->serviceoffered);
2062 return $tp;
2066 * Update the database with a tool proxy instance
2068 * @param object $config Tool proxy definition
2070 * @return int Record id number
2072 function lti_add_tool_proxy($config) {
2073 global $USER, $DB;
2075 $toolproxy = new \stdClass();
2076 if (isset($config->lti_registrationname)) {
2077 $toolproxy->name = trim($config->lti_registrationname);
2079 if (isset($config->lti_registrationurl)) {
2080 $toolproxy->regurl = trim($config->lti_registrationurl);
2082 if (isset($config->lti_capabilities)) {
2083 $toolproxy->capabilityoffered = implode("\n", $config->lti_capabilities);
2084 } else {
2085 $toolproxy->capabilityoffered = implode("\n", array_keys(lti_get_capabilities()));
2087 if (isset($config->lti_services)) {
2088 $toolproxy->serviceoffered = implode("\n", $config->lti_services);
2089 } else {
2090 $func = function($s) {
2091 return $s->get_id();
2093 $servicenames = array_map($func, lti_get_services());
2094 $toolproxy->serviceoffered = implode("\n", $servicenames);
2096 if (isset($config->toolproxyid) && !empty($config->toolproxyid)) {
2097 $toolproxy->id = $config->toolproxyid;
2098 if (!isset($toolproxy->state) || ($toolproxy->state != LTI_TOOL_PROXY_STATE_ACCEPTED)) {
2099 $toolproxy->state = LTI_TOOL_PROXY_STATE_CONFIGURED;
2100 $toolproxy->guid = random_string();
2101 $toolproxy->secret = random_string();
2103 $id = lti_update_tool_proxy($toolproxy);
2104 } else {
2105 $toolproxy->state = LTI_TOOL_PROXY_STATE_CONFIGURED;
2106 $toolproxy->timemodified = time();
2107 $toolproxy->timecreated = $toolproxy->timemodified;
2108 if (!isset($toolproxy->createdby)) {
2109 $toolproxy->createdby = $USER->id;
2111 $toolproxy->guid = random_string();
2112 $toolproxy->secret = random_string();
2113 $id = $DB->insert_record('lti_tool_proxies', $toolproxy);
2116 return $id;
2120 * Updates a tool proxy in the database
2122 * @param object $toolproxy Tool proxy
2124 * @return int Record id number
2126 function lti_update_tool_proxy($toolproxy) {
2127 global $DB;
2129 $toolproxy->timemodified = time();
2130 $id = $DB->update_record('lti_tool_proxies', $toolproxy);
2132 return $id;
2136 * Delete a Tool Proxy
2138 * @param int $id Tool Proxy id
2140 function lti_delete_tool_proxy($id) {
2141 global $DB;
2142 $DB->delete_records('lti_tool_settings', array('toolproxyid' => $id));
2143 $tools = $DB->get_records('lti_types', array('toolproxyid' => $id));
2144 foreach ($tools as $tool) {
2145 lti_delete_type($tool->id);
2147 $DB->delete_records('lti_tool_proxies', array('id' => $id));
2151 * Add a tool configuration in the database
2153 * @param object $config Tool configuration
2155 * @return int Record id number
2157 function lti_add_config($config) {
2158 global $DB;
2160 return $DB->insert_record('lti_types_config', $config);
2164 * Updates a tool configuration in the database
2166 * @param object $config Tool configuration
2168 * @return Record id number
2170 function lti_update_config($config) {
2171 global $DB;
2173 $return = true;
2174 $old = $DB->get_record('lti_types_config', array('typeid' => $config->typeid, 'name' => $config->name));
2176 if ($old) {
2177 $config->id = $old->id;
2178 $return = $DB->update_record('lti_types_config', $config);
2179 } else {
2180 $return = $DB->insert_record('lti_types_config', $config);
2182 return $return;
2186 * Gets the tool settings
2188 * @param int $toolproxyid Id of tool proxy record
2189 * @param int $courseid Id of course (null if system settings)
2190 * @param int $instanceid Id of course module (null if system or context settings)
2192 * @return array Array settings
2194 function lti_get_tool_settings($toolproxyid, $courseid = null, $instanceid = null) {
2195 global $DB;
2197 $settings = array();
2198 $settingsstr = $DB->get_field('lti_tool_settings', 'settings', array('toolproxyid' => $toolproxyid,
2199 'course' => $courseid, 'coursemoduleid' => $instanceid));
2200 if ($settingsstr !== false) {
2201 $settings = json_decode($settingsstr, true);
2203 return $settings;
2207 * Sets the tool settings (
2209 * @param array $settings Array of settings
2210 * @param int $toolproxyid Id of tool proxy record
2211 * @param int $courseid Id of course (null if system settings)
2212 * @param int $instanceid Id of course module (null if system or context settings)
2214 function lti_set_tool_settings($settings, $toolproxyid, $courseid = null, $instanceid = null) {
2215 global $DB;
2217 $json = json_encode($settings);
2218 $record = $DB->get_record('lti_tool_settings', array('toolproxyid' => $toolproxyid,
2219 'course' => $courseid, 'coursemoduleid' => $instanceid));
2220 if ($record !== false) {
2221 $DB->update_record('lti_tool_settings', array('id' => $record->id, 'settings' => $json, 'timemodified' => time()));
2222 } else {
2223 $record = new \stdClass();
2224 $record->toolproxyid = $toolproxyid;
2225 $record->course = $courseid;
2226 $record->coursemoduleid = $instanceid;
2227 $record->settings = $json;
2228 $record->timecreated = time();
2229 $record->timemodified = $record->timecreated;
2230 $DB->insert_record('lti_tool_settings', $record);
2235 * Signs the petition to launch the external tool using OAuth
2237 * @param $oldparms Parameters to be passed for signing
2238 * @param $endpoint url of the external tool
2239 * @param $method Method for sending the parameters (e.g. POST)
2240 * @param $oauth_consumoer_key Key
2241 * @param $oauth_consumoer_secret Secret
2243 function lti_sign_parameters($oldparms, $endpoint, $method, $oauthconsumerkey, $oauthconsumersecret) {
2245 $parms = $oldparms;
2247 $testtoken = '';
2249 // TODO: Switch to core oauthlib once implemented - MDL-30149.
2250 $hmacmethod = new lti\OAuthSignatureMethod_HMAC_SHA1();
2251 $testconsumer = new lti\OAuthConsumer($oauthconsumerkey, $oauthconsumersecret, null);
2252 $accreq = lti\OAuthRequest::from_consumer_and_token($testconsumer, $testtoken, $method, $endpoint, $parms);
2253 $accreq->sign_request($hmacmethod, $testconsumer, $testtoken);
2255 $newparms = $accreq->get_parameters();
2257 return $newparms;
2261 * Posts the launch petition HTML
2263 * @param $newparms Signed parameters
2264 * @param $endpoint URL of the external tool
2265 * @param $debug Debug (true/false)
2267 function lti_post_launch_html($newparms, $endpoint, $debug=false) {
2268 $r = "<form action=\"" . $endpoint .
2269 "\" name=\"ltiLaunchForm\" id=\"ltiLaunchForm\" method=\"post\" encType=\"application/x-www-form-urlencoded\">\n";
2271 // Contruct html for the launch parameters.
2272 foreach ($newparms as $key => $value) {
2273 $key = htmlspecialchars($key);
2274 $value = htmlspecialchars($value);
2275 if ( $key == "ext_submit" ) {
2276 $r .= "<input type=\"submit\"";
2277 } else {
2278 $r .= "<input type=\"hidden\" name=\"{$key}\"";
2280 $r .= " value=\"";
2281 $r .= $value;
2282 $r .= "\"/>\n";
2285 if ( $debug ) {
2286 $r .= "<script language=\"javascript\"> \n";
2287 $r .= " //<![CDATA[ \n";
2288 $r .= "function basicltiDebugToggle() {\n";
2289 $r .= " var ele = document.getElementById(\"basicltiDebug\");\n";
2290 $r .= " if (ele.style.display == \"block\") {\n";
2291 $r .= " ele.style.display = \"none\";\n";
2292 $r .= " }\n";
2293 $r .= " else {\n";
2294 $r .= " ele.style.display = \"block\";\n";
2295 $r .= " }\n";
2296 $r .= "} \n";
2297 $r .= " //]]> \n";
2298 $r .= "</script>\n";
2299 $r .= "<a id=\"displayText\" href=\"javascript:basicltiDebugToggle();\">";
2300 $r .= get_string("toggle_debug_data", "lti")."</a>\n";
2301 $r .= "<div id=\"basicltiDebug\" style=\"display:none\">\n";
2302 $r .= "<b>".get_string("basiclti_endpoint", "lti")."</b><br/>\n";
2303 $r .= $endpoint . "<br/>\n&nbsp;<br/>\n";
2304 $r .= "<b>".get_string("basiclti_parameters", "lti")."</b><br/>\n";
2305 foreach ($newparms as $key => $value) {
2306 $key = htmlspecialchars($key);
2307 $value = htmlspecialchars($value);
2308 $r .= "$key = $value<br/>\n";
2310 $r .= "&nbsp;<br/>\n";
2311 $r .= "</div>\n";
2313 $r .= "</form>\n";
2315 if ( ! $debug ) {
2316 $r .= " <script type=\"text/javascript\"> \n" .
2317 " //<![CDATA[ \n" .
2318 " document.ltiLaunchForm.submit(); \n" .
2319 " //]]> \n" .
2320 " </script> \n";
2322 return $r;
2325 function lti_get_type($typeid) {
2326 global $DB;
2328 return $DB->get_record('lti_types', array('id' => $typeid));
2331 function lti_get_launch_container($lti, $toolconfig) {
2332 if (empty($lti->launchcontainer)) {
2333 $lti->launchcontainer = LTI_LAUNCH_CONTAINER_DEFAULT;
2336 if ($lti->launchcontainer == LTI_LAUNCH_CONTAINER_DEFAULT) {
2337 if (isset($toolconfig['launchcontainer'])) {
2338 $launchcontainer = $toolconfig['launchcontainer'];
2340 } else {
2341 $launchcontainer = $lti->launchcontainer;
2344 if (empty($launchcontainer) || $launchcontainer == LTI_LAUNCH_CONTAINER_DEFAULT) {
2345 $launchcontainer = LTI_LAUNCH_CONTAINER_EMBED_NO_BLOCKS;
2348 $devicetype = core_useragent::get_device_type();
2350 // Scrolling within the object element doesn't work on iOS or Android
2351 // Opening the popup window also had some issues in testing
2352 // For mobile devices, always take up the entire screen to ensure the best experience.
2353 if ($devicetype === core_useragent::DEVICETYPE_MOBILE || $devicetype === core_useragent::DEVICETYPE_TABLET ) {
2354 $launchcontainer = LTI_LAUNCH_CONTAINER_REPLACE_MOODLE_WINDOW;
2357 return $launchcontainer;
2360 function lti_request_is_using_ssl() {
2361 global $CFG;
2362 return (stripos($CFG->httpswwwroot, 'https://') === 0);
2365 function lti_ensure_url_is_https($url) {
2366 if (!strstr($url, '://')) {
2367 $url = 'https://' . $url;
2368 } else {
2369 // If the URL starts with http, replace with https.
2370 if (stripos($url, 'http://') === 0) {
2371 $url = 'https://' . substr($url, 7);
2375 return $url;
2379 * Determines if we should try to log the request
2381 * @param string $rawbody
2382 * @return bool
2384 function lti_should_log_request($rawbody) {
2385 global $CFG;
2387 if (empty($CFG->mod_lti_log_users)) {
2388 return false;
2391 $logusers = explode(',', $CFG->mod_lti_log_users);
2392 if (empty($logusers)) {
2393 return false;
2396 try {
2397 $xml = new \SimpleXMLElement($rawbody);
2398 $ns = $xml->getNamespaces();
2399 $ns = array_shift($ns);
2400 $xml->registerXPathNamespace('lti', $ns);
2401 $requestuserid = '';
2402 if ($node = $xml->xpath('//lti:userId')) {
2403 $node = $node[0];
2404 $requestuserid = clean_param((string) $node, PARAM_INT);
2405 } else if ($node = $xml->xpath('//lti:sourcedId')) {
2406 $node = $node[0];
2407 $resultjson = json_decode((string) $node);
2408 $requestuserid = clean_param($resultjson->data->userid, PARAM_INT);
2410 } catch (Exception $e) {
2411 return false;
2414 if (empty($requestuserid) or !in_array($requestuserid, $logusers)) {
2415 return false;
2418 return true;
2422 * Logs the request to a file in temp dir.
2424 * @param string $rawbody
2426 function lti_log_request($rawbody) {
2427 if ($tempdir = make_temp_directory('mod_lti', false)) {
2428 if ($tempfile = tempnam($tempdir, 'mod_lti_request'.date('YmdHis'))) {
2429 $content = "Request Headers:\n";
2430 foreach (moodle\mod\lti\OAuthUtil::get_headers() as $header => $value) {
2431 $content .= "$header: $value\n";
2433 $content .= "Request Body:\n";
2434 $content .= $rawbody;
2436 file_put_contents($tempfile, $content);
2437 chmod($tempfile, 0644);
2443 * Log an LTI response.
2445 * @param string $responsexml The response XML
2446 * @param Exception $e If there was an exception, pass that too
2448 function lti_log_response($responsexml, $e = null) {
2449 if ($tempdir = make_temp_directory('mod_lti', false)) {
2450 if ($tempfile = tempnam($tempdir, 'mod_lti_response'.date('YmdHis'))) {
2451 $content = '';
2452 if ($e instanceof Exception) {
2453 $info = get_exception_info($e);
2455 $content .= "Exception:\n";
2456 $content .= "Message: $info->message\n";
2457 $content .= "Debug info: $info->debuginfo\n";
2458 $content .= "Backtrace:\n";
2459 $content .= format_backtrace($info->backtrace, true);
2460 $content .= "\n";
2462 $content .= "Response XML:\n";
2463 $content .= $responsexml;
2465 file_put_contents($tempfile, $content);
2466 chmod($tempfile, 0644);
2472 * Fetches LTI type configuration for an LTI instance
2474 * @param stdClass $instance
2475 * @return array Can be empty if no type is found
2477 function lti_get_type_config_by_instance($instance) {
2478 $typeid = null;
2479 if (empty($instance->typeid)) {
2480 $tool = lti_get_tool_by_url_match($instance->toolurl, $instance->course);
2481 if ($tool) {
2482 $typeid = $tool->id;
2484 } else {
2485 $typeid = $instance->typeid;
2487 if (!empty($typeid)) {
2488 return lti_get_type_config($typeid);
2490 return array();
2494 * Enforce type config settings onto the LTI instance
2496 * @param stdClass $instance
2497 * @param array $typeconfig
2499 function lti_force_type_config_settings($instance, array $typeconfig) {
2500 $forced = array(
2501 'instructorchoicesendname' => 'sendname',
2502 'instructorchoicesendemailaddr' => 'sendemailaddr',
2503 'instructorchoiceacceptgrades' => 'acceptgrades',
2506 foreach ($forced as $instanceparam => $typeconfigparam) {
2507 if (array_key_exists($typeconfigparam, $typeconfig) && $typeconfig[$typeconfigparam] != LTI_SETTING_DELEGATE) {
2508 $instance->$instanceparam = $typeconfig[$typeconfigparam];
2514 * Initializes an array with the capabilities supported by the LTI module
2516 * @return array List of capability names (without a dollar sign prefix)
2518 function lti_get_capabilities() {
2520 $capabilities = array(
2521 'basic-lti-launch-request' => '',
2522 'ContentItemSelectionRequest' => '',
2523 'Context.id' => 'context_id',
2524 'CourseSection.title' => 'context_title',
2525 'CourseSection.label' => 'context_label',
2526 'CourseSection.sourcedId' => 'lis_course_section_sourcedid',
2527 'CourseSection.longDescription' => '$COURSE->summary',
2528 'CourseSection.timeFrame.begin' => '$COURSE->startdate',
2529 'ResourceLink.id' => 'resource_link_id',
2530 'ResourceLink.title' => 'resource_link_title',
2531 'ResourceLink.description' => 'resource_link_description',
2532 'User.id' => 'user_id',
2533 'User.username' => '$USER->username',
2534 'Person.name.full' => 'lis_person_name_full',
2535 'Person.name.given' => 'lis_person_name_given',
2536 'Person.name.family' => 'lis_person_name_family',
2537 'Person.email.primary' => 'lis_person_contact_email_primary',
2538 'Person.sourcedId' => 'lis_person_sourcedid',
2539 'Person.name.middle' => '$USER->middlename',
2540 'Person.address.street1' => '$USER->address',
2541 'Person.address.locality' => '$USER->city',
2542 'Person.address.country' => '$USER->country',
2543 'Person.address.timezone' => '$USER->timezone',
2544 'Person.phone.primary' => '$USER->phone1',
2545 'Person.phone.mobile' => '$USER->phone2',
2546 'Person.webaddress' => '$USER->url',
2547 'Membership.role' => 'roles',
2548 'Result.sourcedId' => 'lis_result_sourcedid',
2549 'Result.autocreate' => 'lis_outcome_service_url');
2551 return $capabilities;
2556 * Initializes an array with the services supported by the LTI module
2558 * @return array List of services
2560 function lti_get_services() {
2562 $services = array();
2563 $definedservices = core_component::get_plugin_list('ltiservice');
2564 foreach ($definedservices as $name => $location) {
2565 $classname = "\\ltiservice_{$name}\\local\\service\\{$name}";
2566 $services[] = new $classname();
2569 return $services;
2574 * Initializes an instance of the named service
2576 * @param string $servicename Name of service
2578 * @return mod_lti\local\ltiservice\service_base Service
2580 function lti_get_service_by_name($servicename) {
2582 $service = false;
2583 $classname = "\\ltiservice_{$servicename}\\local\\service\\{$servicename}";
2584 if (class_exists($classname)) {
2585 $service = new $classname();
2588 return $service;
2593 * Finds a service by id
2595 * @param array $services Array of services
2596 * @param string $resourceid ID of resource
2598 * @return mod_lti\local\ltiservice\service_base Service
2600 function lti_get_service_by_resource_id($services, $resourceid) {
2602 $service = false;
2603 foreach ($services as $aservice) {
2604 foreach ($aservice->get_resources() as $resource) {
2605 if ($resource->get_id() === $resourceid) {
2606 $service = $aservice;
2607 break 2;
2612 return $service;
2617 * Extracts the named contexts from a tool proxy
2619 * @param object $json
2621 * @return array Contexts
2623 function lti_get_contexts($json) {
2625 $contexts = array();
2626 if (isset($json->{'@context'})) {
2627 foreach ($json->{'@context'} as $context) {
2628 if (is_object($context)) {
2629 $contexts = array_merge(get_object_vars($context), $contexts);
2634 return $contexts;
2639 * Converts an ID to a fully-qualified ID
2641 * @param array $contexts
2642 * @param string $id
2644 * @return string Fully-qualified ID
2646 function lti_get_fqid($contexts, $id) {
2648 $parts = explode(':', $id, 2);
2649 if (count($parts) > 1) {
2650 if (array_key_exists($parts[0], $contexts)) {
2651 $id = $contexts[$parts[0]] . $parts[1];
2655 return $id;
2660 * Returns the icon for the given tool type
2662 * @param stdClass $type The tool type
2664 * @return string The url to the tool type's corresponding icon
2666 function get_tool_type_icon_url(stdClass $type) {
2667 global $OUTPUT;
2669 $iconurl = $type->secureicon;
2671 if (empty($iconurl)) {
2672 $iconurl = $type->icon;
2675 if (empty($iconurl)) {
2676 $iconurl = $OUTPUT->pix_url('icon', 'lti')->out();
2679 return $iconurl;
2683 * Returns the edit url for the given tool type
2685 * @param stdClass $type The tool type
2687 * @return string The url to edit the tool type
2689 function get_tool_type_edit_url(stdClass $type) {
2690 $url = new moodle_url('/mod/lti/typessettings.php',
2691 array('action' => 'update', 'id' => $type->id, 'sesskey' => sesskey(), 'returnto' => 'toolconfigure'));
2692 return $url->out();
2696 * Returns the edit url for the given tool proxy.
2698 * @param stdClass $proxy The tool proxy
2700 * @return string The url to edit the tool type
2702 function get_tool_proxy_edit_url(stdClass $proxy) {
2703 $url = new moodle_url('/mod/lti/registersettings.php',
2704 array('action' => 'update', 'id' => $proxy->id, 'sesskey' => sesskey(), 'returnto' => 'toolconfigure'));
2705 return $url->out();
2709 * Returns the course url for the given tool type
2711 * @param stdClass $type The tool type
2713 * @return string|void The url to the course of the tool type, void if it is a site wide type
2715 function get_tool_type_course_url(stdClass $type) {
2716 if ($type->course == 1) {
2717 return;
2718 } else {
2719 $url = new moodle_url('/course/view.php', array('id' => $type->course));
2720 return $url->out();
2725 * Returns the icon and edit urls for the tool type and the course url if it is a course type.
2727 * @param stdClass $type The tool type
2729 * @return string The urls of the tool type
2731 function get_tool_type_urls(stdClass $type) {
2732 $courseurl = get_tool_type_course_url($type);
2734 $urls = array(
2735 'icon' => get_tool_type_icon_url($type),
2736 'edit' => get_tool_type_edit_url($type),
2739 if ($courseurl) {
2740 $urls['course'] = $courseurl;
2743 return $urls;
2747 * Returns the icon and edit urls for the tool proxy.
2749 * @param stdClass $proxy The tool proxy
2751 * @return string The urls of the tool proxy
2753 function get_tool_proxy_urls(stdClass $proxy) {
2754 global $OUTPUT;
2756 $urls = array(
2757 'icon' => $OUTPUT->pix_url('icon', 'lti')->out(),
2758 'edit' => get_tool_proxy_edit_url($proxy),
2761 return $urls;
2765 * Returns information on the current state of the tool type
2767 * @param stdClass $type The tool type
2769 * @return array An array with a text description of the state, and boolean for whether it is in each state:
2770 * pending, configured, rejected, unknown
2772 function get_tool_type_state_info(stdClass $type) {
2773 $state = '';
2774 $isconfigured = false;
2775 $ispending = false;
2776 $isrejected = false;
2777 $isunknown = false;
2778 switch ($type->state) {
2779 case LTI_TOOL_STATE_CONFIGURED:
2780 $state = get_string('active', 'mod_lti');
2781 $isconfigured = true;
2782 break;
2783 case LTI_TOOL_STATE_PENDING:
2784 $state = get_string('pending', 'mod_lti');
2785 $ispending = true;
2786 break;
2787 case LTI_TOOL_STATE_REJECTED:
2788 $state = get_string('rejected', 'mod_lti');
2789 $isrejected = true;
2790 break;
2791 default:
2792 $state = get_string('unknownstate', 'mod_lti');
2793 $isunknown = true;
2794 break;
2797 return array(
2798 'text' => $state,
2799 'pending' => $ispending,
2800 'configured' => $isconfigured,
2801 'rejected' => $isrejected,
2802 'unknown' => $isunknown
2807 * Returns a summary of each LTI capability this tool type requires in plain language
2809 * @param stdClass $type The tool type
2811 * @return array An array of text descriptions of each of the capabilities this tool type requires
2813 function get_tool_type_capability_groups($type) {
2814 $capabilities = lti_get_enabled_capabilities($type);
2815 $groups = array();
2816 $hascourse = false;
2817 $hasactivities = false;
2818 $hasuseraccount = false;
2819 $hasuserpersonal = false;
2821 foreach ($capabilities as $capability) {
2822 // Bail out early if we've already found all groups.
2823 if (count($groups) >= 4) {
2824 continue;
2827 if (!$hascourse && preg_match('/^CourseSection/', $capability)) {
2828 $hascourse = true;
2829 $groups[] = get_string('courseinformation', 'mod_lti');
2830 } else if (!$hasactivities && preg_match('/^ResourceLink/', $capability)) {
2831 $hasactivities = true;
2832 $groups[] = get_string('courseactivitiesorresources', 'mod_lti');
2833 } else if (!$hasuseraccount && preg_match('/^User/', $capability) || preg_match('/^Membership/', $capability)) {
2834 $hasuseraccount = true;
2835 $groups[] = get_string('useraccountinformation', 'mod_lti');
2836 } else if (!$hasuserpersonal && preg_match('/^Person/', $capability)) {
2837 $hasuserpersonal = true;
2838 $groups[] = get_string('userpersonalinformation', 'mod_lti');
2842 return $groups;
2847 * Returns the ids of each instance of this tool type
2849 * @param stdClass $type The tool type
2851 * @return array An array of ids of the instances of this tool type
2853 function get_tool_type_instance_ids($type) {
2854 global $DB;
2856 return array_keys($DB->get_fieldset_select('lti', 'id', 'typeid = ?', array($type->id)));
2860 * Serialises this tool type
2862 * @param stdClass $type The tool type
2864 * @return array An array of values representing this type
2866 function serialise_tool_type(stdClass $type) {
2867 $capabilitygroups = get_tool_type_capability_groups($type);
2868 $instanceids = get_tool_type_instance_ids($type);
2869 // Clean the name. We don't want tags here.
2870 $name = clean_param($type->name, PARAM_NOTAGS);
2871 if (!empty($type->description)) {
2872 // Clean the description. We don't want tags here.
2873 $description = clean_param($type->description, PARAM_NOTAGS);
2874 } else {
2875 $description = get_string('editdescription', 'mod_lti');
2877 return array(
2878 'id' => $type->id,
2879 'name' => $name,
2880 'description' => $description,
2881 'urls' => get_tool_type_urls($type),
2882 'state' => get_tool_type_state_info($type),
2883 'hascapabilitygroups' => !empty($capabilitygroups),
2884 'capabilitygroups' => $capabilitygroups,
2885 // Course ID of 1 means it's not linked to a course.
2886 'courseid' => $type->course == 1 ? 0 : $type->course,
2887 'instanceids' => $instanceids,
2888 'instancecount' => count($instanceids)
2893 * Serialises this tool proxy.
2895 * @param stdClass $proxy The tool proxy
2897 * @return array An array of values representing this type
2899 function serialise_tool_proxy(stdClass $proxy) {
2900 return array(
2901 'id' => $proxy->id,
2902 'name' => $proxy->name,
2903 'description' => get_string('activatetoadddescription', 'mod_lti'),
2904 'urls' => get_tool_proxy_urls($proxy),
2905 'state' => array(
2906 'text' => get_string('pending', 'mod_lti'),
2907 'pending' => true,
2908 'configured' => false,
2909 'rejected' => false,
2910 'unknown' => false
2912 'hascapabilitygroups' => true,
2913 'capabilitygroups' => array(),
2914 'courseid' => 0,
2915 'instanceids' => array(),
2916 'instancecount' => 0
2921 * Loads the cartridge information into the tool type, if the launch url is for a cartridge file
2923 * @param stdClass $type The tool type object to be filled in
2924 * @since Moodle 3.1
2926 function lti_load_type_if_cartridge($type) {
2927 if (!empty($type->lti_toolurl) && lti_is_cartridge($type->lti_toolurl)) {
2928 lti_load_type_from_cartridge($type->lti_toolurl, $type);
2933 * Loads the cartridge information into the new tool, if the launch url is for a cartridge file
2935 * @param stdClass $lti The tools config
2936 * @since Moodle 3.1
2938 function lti_load_tool_if_cartridge($lti) {
2939 if (!empty($lti->toolurl) && lti_is_cartridge($lti->toolurl)) {
2940 lti_load_tool_from_cartridge($lti->toolurl, $lti);
2945 * Determines if the given url is for a IMS basic cartridge
2947 * @param string $url The url to be checked
2948 * @return True if the url is for a cartridge
2949 * @since Moodle 3.1
2951 function lti_is_cartridge($url) {
2952 // If it is empty, it's not a cartridge.
2953 if (empty($url)) {
2954 return false;
2956 // If it has xml at the end of the url, it's a cartridge.
2957 if (preg_match('/\.xml$/', $url)) {
2958 return true;
2960 // Even if it doesn't have .xml, load the url to check if it's a cartridge..
2961 try {
2962 $toolinfo = lti_load_cartridge($url,
2963 array(
2964 "launch_url" => "launchurl"
2967 if (!empty($toolinfo['launchurl'])) {
2968 return true;
2970 } catch (moodle_exception $e) {
2971 return false; // Error loading the xml, so it's not a cartridge.
2973 return false;
2977 * Allows you to load settings for an external tool type from an IMS cartridge.
2979 * @param string $url The URL to the cartridge
2980 * @param stdClass $type The tool type object to be filled in
2981 * @throws moodle_exception if the cartridge could not be loaded correctly
2982 * @since Moodle 3.1
2984 function lti_load_type_from_cartridge($url, $type) {
2985 $toolinfo = lti_load_cartridge($url,
2986 array(
2987 "title" => "lti_typename",
2988 "launch_url" => "lti_toolurl",
2989 "description" => "lti_description",
2990 "icon" => "lti_icon",
2991 "secure_icon" => "lti_secureicon"
2993 array(
2994 "icon_url" => "lti_extension_icon",
2995 "secure_icon_url" => "lti_extension_secureicon"
2998 // If an activity name exists, unset the cartridge name so we don't override it.
2999 if (isset($type->lti_typename)) {
3000 unset($toolinfo['lti_typename']);
3003 // Always prefer cartridge core icons first, then, if none are found, look at the extension icons.
3004 if (empty($toolinfo['lti_icon']) && !empty($toolinfo['lti_extension_icon'])) {
3005 $toolinfo['lti_icon'] = $toolinfo['lti_extension_icon'];
3007 unset($toolinfo['lti_extension_icon']);
3009 if (empty($toolinfo['lti_secureicon']) && !empty($toolinfo['lti_extension_secureicon'])) {
3010 $toolinfo['lti_secureicon'] = $toolinfo['lti_extension_secureicon'];
3012 unset($toolinfo['lti_extension_secureicon']);
3014 foreach ($toolinfo as $property => $value) {
3015 $type->$property = $value;
3020 * Allows you to load in the configuration for an external tool from an IMS cartridge.
3022 * @param string $url The URL to the cartridge
3023 * @param stdClass $lti LTI object
3024 * @throws moodle_exception if the cartridge could not be loaded correctly
3025 * @since Moodle 3.1
3027 function lti_load_tool_from_cartridge($url, $lti) {
3028 $toolinfo = lti_load_cartridge($url,
3029 array(
3030 "title" => "name",
3031 "launch_url" => "toolurl",
3032 "secure_launch_url" => "securetoolurl",
3033 "description" => "intro",
3034 "icon" => "icon",
3035 "secure_icon" => "secureicon"
3037 array(
3038 "icon_url" => "extension_icon",
3039 "secure_icon_url" => "extension_secureicon"
3042 // If an activity name exists, unset the cartridge name so we don't override it.
3043 if (isset($lti->name)) {
3044 unset($toolinfo['name']);
3047 // Always prefer cartridge core icons first, then, if none are found, look at the extension icons.
3048 if (empty($toolinfo['icon']) && !empty($toolinfo['extension_icon'])) {
3049 $toolinfo['icon'] = $toolinfo['extension_icon'];
3051 unset($toolinfo['extension_icon']);
3053 if (empty($toolinfo['secureicon']) && !empty($toolinfo['extension_secureicon'])) {
3054 $toolinfo['secureicon'] = $toolinfo['extension_secureicon'];
3056 unset($toolinfo['extension_secureicon']);
3058 foreach ($toolinfo as $property => $value) {
3059 $lti->$property = $value;
3064 * Search for a tag within an XML DOMDocument
3066 * @param string $url The url of the cartridge to be loaded
3067 * @param array $map The map of tags to keys in the return array
3068 * @param array $propertiesmap The map of properties to keys in the return array
3069 * @return array An associative array with the given keys and their values from the cartridge
3070 * @throws moodle_exception if the cartridge could not be loaded correctly
3071 * @since Moodle 3.1
3073 function lti_load_cartridge($url, $map, $propertiesmap = array()) {
3074 global $CFG;
3075 require_once($CFG->libdir. "/filelib.php");
3077 $curl = new curl();
3078 $response = $curl->get($url);
3080 // TODO MDL-46023 Replace this code with a call to the new library.
3081 $origerrors = libxml_use_internal_errors(true);
3082 $origentity = libxml_disable_entity_loader(true);
3083 libxml_clear_errors();
3085 $document = new DOMDocument();
3086 @$document->loadXML($response, LIBXML_DTDLOAD | LIBXML_DTDATTR);
3088 $cartridge = new DomXpath($document);
3090 $errors = libxml_get_errors();
3092 libxml_clear_errors();
3093 libxml_use_internal_errors($origerrors);
3094 libxml_disable_entity_loader($origentity);
3096 if (count($errors) > 0) {
3097 $message = 'Failed to load cartridge.';
3098 foreach ($errors as $error) {
3099 $message .= "\n" . trim($error->message, "\n\r\t .") . " at line " . $error->line;
3101 throw new moodle_exception('errorreadingfile', '', '', $url, $message);
3104 $toolinfo = array();
3105 foreach ($map as $tag => $key) {
3106 $value = get_tag($tag, $cartridge);
3107 if ($value) {
3108 $toolinfo[$key] = $value;
3111 if (!empty($propertiesmap)) {
3112 foreach ($propertiesmap as $property => $key) {
3113 $value = get_tag("property", $cartridge, $property);
3114 if ($value) {
3115 $toolinfo[$key] = $value;
3120 return $toolinfo;
3124 * Search for a tag within an XML DOMDocument
3126 * @param stdClass $tagname The name of the tag to search for
3127 * @param XPath $xpath The XML to find the tag in
3128 * @param XPath $attribute The attribute to search for (if we should search for a child node with the given
3129 * value for the name attribute
3130 * @since Moodle 3.1
3132 function get_tag($tagname, $xpath, $attribute = null) {
3133 if ($attribute) {
3134 $result = $xpath->query('//*[local-name() = \'' . $tagname . '\'][@name="' . $attribute . '"]');
3135 } else {
3136 $result = $xpath->query('//*[local-name() = \'' . $tagname . '\']');
3138 if ($result->length > 0) {
3139 return $result->item(0)->nodeValue;
3141 return null;