MDL-63185 mod_quiz: replace existing tests to use new step
[moodle.git] / mod / lti / locallib.php
blob295581b901bb26a0713a7cba309b0540c1269805
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 global $CFG;
57 require_once($CFG->dirroot.'/mod/lti/OAuth.php');
58 require_once($CFG->libdir.'/weblib.php');
59 require_once($CFG->dirroot . '/course/modlib.php');
60 require_once($CFG->dirroot . '/mod/lti/TrivialStore.php');
62 define('LTI_URL_DOMAIN_REGEX', '/(?:https?:\/\/)?(?:www\.)?([^\/]+)(?:\/|$)/i');
64 define('LTI_LAUNCH_CONTAINER_DEFAULT', 1);
65 define('LTI_LAUNCH_CONTAINER_EMBED', 2);
66 define('LTI_LAUNCH_CONTAINER_EMBED_NO_BLOCKS', 3);
67 define('LTI_LAUNCH_CONTAINER_WINDOW', 4);
68 define('LTI_LAUNCH_CONTAINER_REPLACE_MOODLE_WINDOW', 5);
70 define('LTI_TOOL_STATE_ANY', 0);
71 define('LTI_TOOL_STATE_CONFIGURED', 1);
72 define('LTI_TOOL_STATE_PENDING', 2);
73 define('LTI_TOOL_STATE_REJECTED', 3);
74 define('LTI_TOOL_PROXY_TAB', 4);
76 define('LTI_TOOL_PROXY_STATE_CONFIGURED', 1);
77 define('LTI_TOOL_PROXY_STATE_PENDING', 2);
78 define('LTI_TOOL_PROXY_STATE_ACCEPTED', 3);
79 define('LTI_TOOL_PROXY_STATE_REJECTED', 4);
81 define('LTI_SETTING_NEVER', 0);
82 define('LTI_SETTING_ALWAYS', 1);
83 define('LTI_SETTING_DELEGATE', 2);
85 define('LTI_COURSEVISIBLE_NO', 0);
86 define('LTI_COURSEVISIBLE_PRECONFIGURED', 1);
87 define('LTI_COURSEVISIBLE_ACTIVITYCHOOSER', 2);
89 define('LTI_VERSION_1', 'LTI-1p0');
90 define('LTI_VERSION_2', 'LTI-2p0');
92 /**
93 * Return the launch data required for opening the external tool.
95 * @param stdClass $instance the external tool activity settings
96 * @return array the endpoint URL and parameters (including the signature)
97 * @since Moodle 3.0
99 function lti_get_launch_data($instance) {
100 global $PAGE, $CFG, $USER;
102 if (empty($instance->typeid)) {
103 $tool = lti_get_tool_by_url_match($instance->toolurl, $instance->course);
104 if ($tool) {
105 $typeid = $tool->id;
106 } else {
107 $tool = lti_get_tool_by_url_match($instance->securetoolurl, $instance->course);
108 if ($tool) {
109 $typeid = $tool->id;
110 } else {
111 $typeid = null;
114 } else {
115 $typeid = $instance->typeid;
116 $tool = lti_get_type($typeid);
119 if ($typeid) {
120 $typeconfig = lti_get_type_config($typeid);
121 } else {
122 // There is no admin configuration for this tool. Use configuration in the lti instance record plus some defaults.
123 $typeconfig = (array)$instance;
125 $typeconfig['sendname'] = $instance->instructorchoicesendname;
126 $typeconfig['sendemailaddr'] = $instance->instructorchoicesendemailaddr;
127 $typeconfig['customparameters'] = $instance->instructorcustomparameters;
128 $typeconfig['acceptgrades'] = $instance->instructorchoiceacceptgrades;
129 $typeconfig['allowroster'] = $instance->instructorchoiceallowroster;
130 $typeconfig['forcessl'] = '0';
133 // Default the organizationid if not specified.
134 if (empty($typeconfig['organizationid'])) {
135 $urlparts = parse_url($CFG->wwwroot);
137 $typeconfig['organizationid'] = $urlparts['host'];
140 if (isset($tool->toolproxyid)) {
141 $toolproxy = lti_get_tool_proxy($tool->toolproxyid);
142 $key = $toolproxy->guid;
143 $secret = $toolproxy->secret;
144 } else {
145 $toolproxy = null;
146 if (!empty($instance->resourcekey)) {
147 $key = $instance->resourcekey;
148 } else if (!empty($typeconfig['resourcekey'])) {
149 $key = $typeconfig['resourcekey'];
150 } else {
151 $key = '';
153 if (!empty($instance->password)) {
154 $secret = $instance->password;
155 } else if (!empty($typeconfig['password'])) {
156 $secret = $typeconfig['password'];
157 } else {
158 $secret = '';
162 $endpoint = !empty($instance->toolurl) ? $instance->toolurl : $typeconfig['toolurl'];
163 $endpoint = trim($endpoint);
165 // If the current request is using SSL and a secure tool URL is specified, use it.
166 if (lti_request_is_using_ssl() && !empty($instance->securetoolurl)) {
167 $endpoint = trim($instance->securetoolurl);
170 // If SSL is forced, use the secure tool url if specified. Otherwise, make sure https is on the normal launch URL.
171 if (isset($typeconfig['forcessl']) && ($typeconfig['forcessl'] == '1')) {
172 if (!empty($instance->securetoolurl)) {
173 $endpoint = trim($instance->securetoolurl);
176 $endpoint = lti_ensure_url_is_https($endpoint);
177 } else {
178 if (!strstr($endpoint, '://')) {
179 $endpoint = 'http://' . $endpoint;
183 $orgid = $typeconfig['organizationid'];
185 $course = $PAGE->course;
186 $islti2 = isset($tool->toolproxyid);
187 $allparams = lti_build_request($instance, $typeconfig, $course, $typeid, $islti2);
188 if ($islti2) {
189 $requestparams = lti_build_request_lti2($tool, $allparams);
190 } else {
191 $requestparams = $allparams;
193 $requestparams = array_merge($requestparams, lti_build_standard_request($instance, $orgid, $islti2));
194 $customstr = '';
195 if (isset($typeconfig['customparameters'])) {
196 $customstr = $typeconfig['customparameters'];
198 $requestparams = array_merge($requestparams, lti_build_custom_parameters($toolproxy, $tool, $instance, $allparams, $customstr,
199 $instance->instructorcustomparameters, $islti2));
201 $launchcontainer = lti_get_launch_container($instance, $typeconfig);
202 $returnurlparams = array('course' => $course->id,
203 'launch_container' => $launchcontainer,
204 'instanceid' => $instance->id,
205 'sesskey' => sesskey());
207 // Add the return URL. We send the launch container along to help us avoid frames-within-frames when the user returns.
208 $url = new \moodle_url('/mod/lti/return.php', $returnurlparams);
209 $returnurl = $url->out(false);
211 if (isset($typeconfig['forcessl']) && ($typeconfig['forcessl'] == '1')) {
212 $returnurl = lti_ensure_url_is_https($returnurl);
215 $target = '';
216 switch($launchcontainer) {
217 case LTI_LAUNCH_CONTAINER_EMBED:
218 case LTI_LAUNCH_CONTAINER_EMBED_NO_BLOCKS:
219 $target = 'iframe';
220 break;
221 case LTI_LAUNCH_CONTAINER_REPLACE_MOODLE_WINDOW:
222 $target = 'frame';
223 break;
224 case LTI_LAUNCH_CONTAINER_WINDOW:
225 $target = 'window';
226 break;
228 if (!empty($target)) {
229 $requestparams['launch_presentation_document_target'] = $target;
232 $requestparams['launch_presentation_return_url'] = $returnurl;
234 // Add the parameters configured by the LTI advantage services.
235 if ($typeid && !$islti2) {
236 $services = lti_get_services();
237 foreach ($services as $service) {
238 $ltiadvantageparameters = $service->get_launch_parameters('basic-lti-launch-request',
239 $course->id, $USER->id , $typeid, $instance->id);
240 foreach ($ltiadvantageparameters as $ltiadvantagekey => $ltiadvantagevalue) {
241 $requestparams[$ltiadvantagekey] = $ltiadvantagevalue;
246 // Allow request params to be updated by sub-plugins.
247 $plugins = core_component::get_plugin_list('ltisource');
248 foreach (array_keys($plugins) as $plugin) {
249 $pluginparams = component_callback('ltisource_'.$plugin, 'before_launch',
250 array($instance, $endpoint, $requestparams), array());
252 if (!empty($pluginparams) && is_array($pluginparams)) {
253 $requestparams = array_merge($requestparams, $pluginparams);
257 if (!empty($key) && !empty($secret)) {
258 $parms = lti_sign_parameters($requestparams, $endpoint, "POST", $key, $secret);
260 $endpointurl = new \moodle_url($endpoint);
261 $endpointparams = $endpointurl->params();
263 // Strip querystring params in endpoint url from $parms to avoid duplication.
264 if (!empty($endpointparams) && !empty($parms)) {
265 foreach (array_keys($endpointparams) as $paramname) {
266 if (isset($parms[$paramname])) {
267 unset($parms[$paramname]);
272 } else {
273 // If no key and secret, do the launch unsigned.
274 $returnurlparams['unsigned'] = '1';
275 $parms = $requestparams;
278 return array($endpoint, $parms);
282 * Launch an external tool activity.
284 * @param stdClass $instance the external tool activity settings
285 * @return string The HTML code containing the javascript code for the launch
287 function lti_launch_tool($instance) {
289 list($endpoint, $parms) = lti_get_launch_data($instance);
290 $debuglaunch = ( $instance->debuglaunch == 1 );
292 $content = lti_post_launch_html($parms, $endpoint, $debuglaunch);
294 echo $content;
298 * Prepares an LTI registration request message
300 * @param object $toolproxy Tool Proxy instance object
302 function lti_register($toolproxy) {
303 $endpoint = $toolproxy->regurl;
305 // Change the status to pending.
306 $toolproxy->state = LTI_TOOL_PROXY_STATE_PENDING;
307 lti_update_tool_proxy($toolproxy);
309 $requestparams = lti_build_registration_request($toolproxy);
311 $content = lti_post_launch_html($requestparams, $endpoint, false);
313 echo $content;
318 * Gets the parameters for the regirstration request
320 * @param object $toolproxy Tool Proxy instance object
321 * @return array Registration request parameters
323 function lti_build_registration_request($toolproxy) {
324 $key = $toolproxy->guid;
325 $secret = $toolproxy->secret;
327 $requestparams = array();
328 $requestparams['lti_message_type'] = 'ToolProxyRegistrationRequest';
329 $requestparams['lti_version'] = 'LTI-2p0';
330 $requestparams['reg_key'] = $key;
331 $requestparams['reg_password'] = $secret;
332 $requestparams['reg_url'] = $toolproxy->regurl;
334 // Add the profile URL.
335 $profileservice = lti_get_service_by_name('profile');
336 $profileservice->set_tool_proxy($toolproxy);
337 $requestparams['tc_profile_url'] = $profileservice->parse_value('$ToolConsumerProfile.url');
339 // Add the return URL.
340 $returnurlparams = array('id' => $toolproxy->id, 'sesskey' => sesskey());
341 $url = new \moodle_url('/mod/lti/externalregistrationreturn.php', $returnurlparams);
342 $returnurl = $url->out(false);
344 $requestparams['launch_presentation_return_url'] = $returnurl;
346 return $requestparams;
350 * Build source ID
352 * @param int $instanceid
353 * @param int $userid
354 * @param string $servicesalt
355 * @param null|int $typeid
356 * @param null|int $launchid
357 * @return stdClass
359 function lti_build_sourcedid($instanceid, $userid, $servicesalt, $typeid = null, $launchid = null) {
360 $data = new \stdClass();
362 $data->instanceid = $instanceid;
363 $data->userid = $userid;
364 $data->typeid = $typeid;
365 if (!empty($launchid)) {
366 $data->launchid = $launchid;
367 } else {
368 $data->launchid = mt_rand();
371 $json = json_encode($data);
373 $hash = hash('sha256', $json . $servicesalt, false);
375 $container = new \stdClass();
376 $container->data = $data;
377 $container->hash = $hash;
379 return $container;
383 * This function builds the request that must be sent to the tool producer
385 * @param object $instance Basic LTI instance object
386 * @param array $typeconfig Basic LTI tool configuration
387 * @param object $course Course object
388 * @param int|null $typeid Basic LTI tool ID
389 * @param boolean $islti2 True if an LTI 2 tool is being launched
391 * @return array Request details
393 function lti_build_request($instance, $typeconfig, $course, $typeid = null, $islti2 = false) {
394 global $USER, $CFG;
396 if (empty($instance->cmid)) {
397 $instance->cmid = 0;
400 $role = lti_get_ims_role($USER, $instance->cmid, $instance->course, $islti2);
402 $requestparams = array(
403 'user_id' => $USER->id,
404 'lis_person_sourcedid' => $USER->idnumber,
405 'roles' => $role,
406 'context_id' => $course->id,
407 'context_label' => trim(html_to_text($course->shortname, 0)),
408 'context_title' => trim(html_to_text($course->fullname, 0)),
410 if (!empty($instance->name)) {
411 $requestparams['resource_link_title'] = trim(html_to_text($instance->name, 0));
413 if (!empty($instance->cmid)) {
414 $intro = format_module_intro('lti', $instance, $instance->cmid);
415 $intro = trim(html_to_text($intro, 0, false));
417 // This may look weird, but this is required for new lines
418 // so we generate the same OAuth signature as the tool provider.
419 $intro = str_replace("\n", "\r\n", $intro);
420 $requestparams['resource_link_description'] = $intro;
422 if (!empty($instance->id)) {
423 $requestparams['resource_link_id'] = $instance->id;
425 if (!empty($instance->resource_link_id)) {
426 $requestparams['resource_link_id'] = $instance->resource_link_id;
428 if ($course->format == 'site') {
429 $requestparams['context_type'] = 'Group';
430 } else {
431 $requestparams['context_type'] = 'CourseSection';
432 $requestparams['lis_course_section_sourcedid'] = $course->idnumber;
435 if (!empty($instance->id) && !empty($instance->servicesalt) && ($islti2 ||
436 $typeconfig['acceptgrades'] == LTI_SETTING_ALWAYS ||
437 ($typeconfig['acceptgrades'] == LTI_SETTING_DELEGATE && $instance->instructorchoiceacceptgrades == LTI_SETTING_ALWAYS))
439 $placementsecret = $instance->servicesalt;
440 $sourcedid = json_encode(lti_build_sourcedid($instance->id, $USER->id, $placementsecret, $typeid));
441 $requestparams['lis_result_sourcedid'] = $sourcedid;
443 // Add outcome service URL.
444 $serviceurl = new \moodle_url('/mod/lti/service.php');
445 $serviceurl = $serviceurl->out();
447 $forcessl = false;
448 if (!empty($CFG->mod_lti_forcessl)) {
449 $forcessl = true;
452 if ((isset($typeconfig['forcessl']) && ($typeconfig['forcessl'] == '1')) or $forcessl) {
453 $serviceurl = lti_ensure_url_is_https($serviceurl);
456 $requestparams['lis_outcome_service_url'] = $serviceurl;
459 // Send user's name and email data if appropriate.
460 if ($islti2 || $typeconfig['sendname'] == LTI_SETTING_ALWAYS ||
461 ($typeconfig['sendname'] == LTI_SETTING_DELEGATE && isset($instance->instructorchoicesendname)
462 && $instance->instructorchoicesendname == LTI_SETTING_ALWAYS)
464 $requestparams['lis_person_name_given'] = $USER->firstname;
465 $requestparams['lis_person_name_family'] = $USER->lastname;
466 $requestparams['lis_person_name_full'] = fullname($USER);
467 $requestparams['ext_user_username'] = $USER->username;
470 if ($islti2 || $typeconfig['sendemailaddr'] == LTI_SETTING_ALWAYS ||
471 ($typeconfig['sendemailaddr'] == LTI_SETTING_DELEGATE && isset($instance->instructorchoicesendemailaddr)
472 && $instance->instructorchoicesendemailaddr == LTI_SETTING_ALWAYS)
474 $requestparams['lis_person_contact_email_primary'] = $USER->email;
477 return $requestparams;
481 * This function builds the request that must be sent to an LTI 2 tool provider
483 * @param object $tool Basic LTI tool object
484 * @param array $params Custom launch parameters
486 * @return array Request details
488 function lti_build_request_lti2($tool, $params) {
490 $requestparams = array();
492 $capabilities = lti_get_capabilities();
493 $enabledcapabilities = explode("\n", $tool->enabledcapability);
494 foreach ($enabledcapabilities as $capability) {
495 if (array_key_exists($capability, $capabilities)) {
496 $val = $capabilities[$capability];
497 if ($val && (substr($val, 0, 1) != '$')) {
498 if (isset($params[$val])) {
499 $requestparams[$capabilities[$capability]] = $params[$capabilities[$capability]];
505 return $requestparams;
510 * This function builds the standard parameters for an LTI 1 or 2 request that must be sent to the tool producer
512 * @param stdClass $instance Basic LTI instance object
513 * @param string $orgid Organisation ID
514 * @param boolean $islti2 True if an LTI 2 tool is being launched
515 * @param string $messagetype The request message type. Defaults to basic-lti-launch-request if empty.
517 * @return array Request details
519 function lti_build_standard_request($instance, $orgid, $islti2, $messagetype = 'basic-lti-launch-request') {
520 global $CFG;
522 $requestparams = array();
524 if ($instance) {
525 $requestparams['resource_link_id'] = $instance->id;
526 if (property_exists($instance, 'resource_link_id') and !empty($instance->resource_link_id)) {
527 $requestparams['resource_link_id'] = $instance->resource_link_id;
531 $requestparams['launch_presentation_locale'] = current_language();
533 // Make sure we let the tool know what LMS they are being called from.
534 $requestparams['ext_lms'] = 'moodle-2';
535 $requestparams['tool_consumer_info_product_family_code'] = 'moodle';
536 $requestparams['tool_consumer_info_version'] = strval($CFG->version);
538 // Add oauth_callback to be compliant with the 1.0A spec.
539 $requestparams['oauth_callback'] = 'about:blank';
541 if (!$islti2) {
542 $requestparams['lti_version'] = 'LTI-1p0';
543 } else {
544 $requestparams['lti_version'] = 'LTI-2p0';
546 $requestparams['lti_message_type'] = $messagetype;
548 if ($orgid) {
549 $requestparams["tool_consumer_instance_guid"] = $orgid;
551 if (!empty($CFG->mod_lti_institution_name)) {
552 $requestparams['tool_consumer_instance_name'] = trim(html_to_text($CFG->mod_lti_institution_name, 0));
553 } else {
554 $requestparams['tool_consumer_instance_name'] = get_site()->shortname;
556 $requestparams['tool_consumer_instance_description'] = trim(html_to_text(get_site()->fullname, 0));
558 return $requestparams;
562 * This function builds the custom parameters
564 * @param object $toolproxy Tool proxy instance object
565 * @param object $tool Tool instance object
566 * @param object $instance Tool placement instance object
567 * @param array $params LTI launch parameters
568 * @param string $customstr Custom parameters defined for tool
569 * @param string $instructorcustomstr Custom parameters defined for this placement
570 * @param boolean $islti2 True if an LTI 2 tool is being launched
572 * @return array Custom parameters
574 function lti_build_custom_parameters($toolproxy, $tool, $instance, $params, $customstr, $instructorcustomstr, $islti2) {
576 // Concatenate the custom parameters from the administrator and the instructor
577 // Instructor parameters are only taken into consideration if the administrator
578 // has given permission.
579 $custom = array();
580 if ($customstr) {
581 $custom = lti_split_custom_parameters($toolproxy, $tool, $params, $customstr, $islti2);
583 if ($instructorcustomstr) {
584 $custom = array_merge(lti_split_custom_parameters($toolproxy, $tool, $params,
585 $instructorcustomstr, $islti2), $custom);
587 if ($islti2) {
588 $custom = array_merge(lti_split_custom_parameters($toolproxy, $tool, $params,
589 $tool->parameter, true), $custom);
590 $settings = lti_get_tool_settings($tool->toolproxyid);
591 $custom = array_merge($custom, lti_get_custom_parameters($toolproxy, $tool, $params, $settings));
592 if (!empty($instance->course)) {
593 $settings = lti_get_tool_settings($tool->toolproxyid, $instance->course);
594 $custom = array_merge($custom, lti_get_custom_parameters($toolproxy, $tool, $params, $settings));
595 if (!empty($instance->id)) {
596 $settings = lti_get_tool_settings($tool->toolproxyid, $instance->course, $instance->id);
597 $custom = array_merge($custom, lti_get_custom_parameters($toolproxy, $tool, $params, $settings));
602 return $custom;
606 * Builds a standard LTI Content-Item selection request.
608 * @param int $id The tool type ID.
609 * @param stdClass $course The course object.
610 * @param moodle_url $returnurl The return URL in the tool consumer (TC) that the tool provider (TP)
611 * will use to return the Content-Item message.
612 * @param string $title The tool's title, if available.
613 * @param string $text The text to display to represent the content item. This value may be a long description of the content item.
614 * @param array $mediatypes Array of MIME types types supported by the TC. If empty, the TC will support ltilink by default.
615 * @param array $presentationtargets Array of ways in which the selected content item(s) can be requested to be opened
616 * (via the presentationDocumentTarget element for a returned content item).
617 * If empty, "frame", "iframe", and "window" will be supported by default.
618 * @param bool $autocreate Indicates whether any content items returned by the TP would be automatically persisted without
619 * @param bool $multiple Indicates whether the user should be permitted to select more than one item. False by default.
620 * any option for the user to cancel the operation. False by default.
621 * @param bool $unsigned Indicates whether the TC is willing to accept an unsigned return message, or not.
622 * A signed message should always be required when the content item is being created automatically in the
623 * TC without further interaction from the user. False by default.
624 * @param bool $canconfirm Flag for can_confirm parameter. False by default.
625 * @param bool $copyadvice Indicates whether the TC is able and willing to make a local copy of a content item. False by default.
626 * @return stdClass The object containing the signed request parameters and the URL to the TP's Content-Item selection interface.
627 * @throws moodle_exception When the LTI tool type does not exist.`
628 * @throws coding_exception For invalid media type and presentation target parameters.
630 function lti_build_content_item_selection_request($id, $course, moodle_url $returnurl, $title = '', $text = '', $mediatypes = [],
631 $presentationtargets = [], $autocreate = false, $multiple = false,
632 $unsigned = false, $canconfirm = false, $copyadvice = false) {
633 global $USER;
635 $tool = lti_get_type($id);
636 // Validate parameters.
637 if (!$tool) {
638 throw new moodle_exception('errortooltypenotfound', 'mod_lti');
640 if (!is_array($mediatypes)) {
641 throw new coding_exception('The list of accepted media types should be in an array');
643 if (!is_array($presentationtargets)) {
644 throw new coding_exception('The list of accepted presentation targets should be in an array');
647 // Check title. If empty, use the tool's name.
648 if (empty($title)) {
649 $title = $tool->name;
652 $typeconfig = lti_get_type_config($id);
653 $key = '';
654 $secret = '';
655 $islti2 = false;
656 if (isset($tool->toolproxyid)) {
657 $islti2 = true;
658 $toolproxy = lti_get_tool_proxy($tool->toolproxyid);
659 $key = $toolproxy->guid;
660 $secret = $toolproxy->secret;
661 } else {
662 $toolproxy = null;
663 if (!empty($typeconfig['resourcekey'])) {
664 $key = $typeconfig['resourcekey'];
666 if (!empty($typeconfig['password'])) {
667 $secret = $typeconfig['password'];
670 $tool->enabledcapability = '';
671 if (!empty($typeconfig['enabledcapability_ContentItemSelectionRequest'])) {
672 $tool->enabledcapability = $typeconfig['enabledcapability_ContentItemSelectionRequest'];
675 $tool->parameter = '';
676 if (!empty($typeconfig['parameter_ContentItemSelectionRequest'])) {
677 $tool->parameter = $typeconfig['parameter_ContentItemSelectionRequest'];
680 // Set the tool URL.
681 if (!empty($typeconfig['toolurl_ContentItemSelectionRequest'])) {
682 $toolurl = new moodle_url($typeconfig['toolurl_ContentItemSelectionRequest']);
683 } else {
684 $toolurl = new moodle_url($typeconfig['toolurl']);
687 // Check if SSL is forced.
688 if (!empty($typeconfig['forcessl'])) {
689 // Make sure the tool URL is set to https.
690 if (strtolower($toolurl->get_scheme()) === 'http') {
691 $toolurl->set_scheme('https');
693 // Make sure the return URL is set to https.
694 if (strtolower($returnurl->get_scheme()) === 'http') {
695 $returnurl->set_scheme('https');
698 $toolurlout = $toolurl->out(false);
700 // Get base request parameters.
701 $instance = new stdClass();
702 $instance->course = $course->id;
703 $requestparams = lti_build_request($instance, $typeconfig, $course, $id, $islti2);
705 // Get LTI2-specific request parameters and merge to the request parameters if applicable.
706 if ($islti2) {
707 $lti2params = lti_build_request_lti2($tool, $requestparams);
708 $requestparams = array_merge($requestparams, $lti2params);
711 // Add the parameters configured by the LTI advantage services.
712 if ($id && !$islti2) {
713 $services = lti_get_services();
714 foreach ($services as $service) {
715 $ltiadvantageparameters = $service->get_launch_parameters('ContentItemSelectionRequest',
716 $course->id, $USER->id , $id);
717 foreach ($ltiadvantageparameters as $ltiadvantagekey => $ltiadvantagevalue) {
718 $requestparams[$ltiadvantagekey] = $ltiadvantagevalue;
723 // Get standard request parameters and merge to the request parameters.
724 $orgid = !empty($typeconfig['organizationid']) ? $typeconfig['organizationid'] : '';
725 $standardparams = lti_build_standard_request(null, $orgid, $islti2, 'ContentItemSelectionRequest');
726 $requestparams = array_merge($requestparams, $standardparams);
728 // Get custom request parameters and merge to the request parameters.
729 $customstr = '';
730 if (!empty($typeconfig['customparameters'])) {
731 $customstr = $typeconfig['customparameters'];
733 $customparams = lti_build_custom_parameters($toolproxy, $tool, $instance, $requestparams, $customstr, '', $islti2);
734 $requestparams = array_merge($requestparams, $customparams);
736 // Allow request params to be updated by sub-plugins.
737 $plugins = core_component::get_plugin_list('ltisource');
738 foreach (array_keys($plugins) as $plugin) {
739 $pluginparams = component_callback('ltisource_' . $plugin, 'before_launch', [$instance, $toolurlout, $requestparams], []);
741 if (!empty($pluginparams) && is_array($pluginparams)) {
742 $requestparams = array_merge($requestparams, $pluginparams);
746 // Media types. Set to ltilink by default if empty.
747 if (empty($mediatypes)) {
748 $mediatypes = [
749 'application/vnd.ims.lti.v1.ltilink',
752 $requestparams['accept_media_types'] = implode(',', $mediatypes);
754 // Presentation targets. Supports frame, iframe, window by default if empty.
755 if (empty($presentationtargets)) {
756 $presentationtargets = [
757 'frame',
758 'iframe',
759 'window',
762 $requestparams['accept_presentation_document_targets'] = implode(',', $presentationtargets);
764 // Other request parameters.
765 $requestparams['accept_copy_advice'] = $copyadvice === true ? 'true' : 'false';
766 $requestparams['accept_multiple'] = $multiple === true ? 'true' : 'false';
767 $requestparams['accept_unsigned'] = $unsigned === true ? 'true' : 'false';
768 $requestparams['auto_create'] = $autocreate === true ? 'true' : 'false';
769 $requestparams['can_confirm'] = $canconfirm === true ? 'true' : 'false';
770 $requestparams['content_item_return_url'] = $returnurl->out(false);
771 $requestparams['title'] = $title;
772 $requestparams['text'] = $text;
773 $signedparams = lti_sign_parameters($requestparams, $toolurlout, 'POST', $key, $secret);
774 $toolurlparams = $toolurl->params();
776 // Strip querystring params in endpoint url from $signedparams to avoid duplication.
777 if (!empty($toolurlparams) && !empty($signedparams)) {
778 foreach (array_keys($toolurlparams) as $paramname) {
779 if (isset($signedparams[$paramname])) {
780 unset($signedparams[$paramname]);
785 // Check for params that should not be passed. Unset if they are set.
786 $unwantedparams = [
787 'resource_link_id',
788 'resource_link_title',
789 'resource_link_description',
790 'launch_presentation_return_url',
791 'lis_result_sourcedid',
793 foreach ($unwantedparams as $param) {
794 if (isset($signedparams[$param])) {
795 unset($signedparams[$param]);
799 // Prepare result object.
800 $result = new stdClass();
801 $result->params = $signedparams;
802 $result->url = $toolurlout;
804 return $result;
808 * Processes the tool provider's response to the ContentItemSelectionRequest and builds the configuration data from the
809 * selected content item. This configuration data can be then used when adding a tool into the course.
811 * @param int $typeid The tool type ID.
812 * @param string $messagetype The value for the lti_message_type parameter.
813 * @param string $ltiversion The value for the lti_version parameter.
814 * @param string $consumerkey The consumer key.
815 * @param string $contentitemsjson The JSON string for the content_items parameter.
816 * @return stdClass The array of module information objects.
817 * @throws moodle_exception
818 * @throws lti\OAuthException
820 function lti_tool_configuration_from_content_item($typeid, $messagetype, $ltiversion, $consumerkey, $contentitemsjson) {
821 $tool = lti_get_type($typeid);
822 // Validate parameters.
823 if (!$tool) {
824 throw new moodle_exception('errortooltypenotfound', 'mod_lti');
826 // Check lti_message_type. Show debugging if it's not set to ContentItemSelection.
827 // No need to throw exceptions for now since lti_message_type does not seem to be used in this processing at the moment.
828 if ($messagetype !== 'ContentItemSelection') {
829 debugging("lti_message_type is invalid: {$messagetype}. It should be set to 'ContentItemSelection'.",
830 DEBUG_DEVELOPER);
833 $typeconfig = lti_get_type_config($typeid);
835 if (isset($tool->toolproxyid)) {
836 $islti2 = true;
837 $toolproxy = lti_get_tool_proxy($tool->toolproxyid);
838 $key = $toolproxy->guid;
839 $secret = $toolproxy->secret;
840 } else {
841 $islti2 = false;
842 $toolproxy = null;
843 if (!empty($typeconfig['resourcekey'])) {
844 $key = $typeconfig['resourcekey'];
845 } else {
846 $key = '';
848 if (!empty($typeconfig['password'])) {
849 $secret = $typeconfig['password'];
850 } else {
851 $secret = '';
855 // Check LTI versions from our side and the response's side. Show debugging if they don't match.
856 // No need to throw exceptions for now since LTI version does not seem to be used in this processing at the moment.
857 $expectedversion = LTI_VERSION_1;
858 if ($islti2) {
859 $expectedversion = LTI_VERSION_2;
861 if ($ltiversion !== $expectedversion) {
862 debugging("lti_version from response does not match the tool's configuration. Tool: {$expectedversion}," .
863 " Response: {$ltiversion}", DEBUG_DEVELOPER);
866 if ($consumerkey !== $key) {
867 throw new moodle_exception('errorincorrectconsumerkey', 'mod_lti');
870 $store = new lti\TrivialOAuthDataStore();
871 $store->add_consumer($key, $secret);
872 $server = new lti\OAuthServer($store);
873 $method = new lti\OAuthSignatureMethod_HMAC_SHA1();
874 $server->add_signature_method($method);
875 $request = lti\OAuthRequest::from_request();
876 try {
877 $server->verify_request($request);
878 } catch (lti\OAuthException $e) {
879 throw new lti\OAuthException("OAuth signature failed: " . $e->getMessage());
882 $items = json_decode($contentitemsjson);
883 if (empty($items)) {
884 throw new moodle_exception('errorinvaliddata', 'mod_lti', '', $contentitemsjson);
886 if (!isset($items->{'@graph'}) || !is_array($items->{'@graph'}) || (count($items->{'@graph'}) > 1)) {
887 throw new moodle_exception('errorinvalidresponseformat', 'mod_lti');
890 $config = null;
891 if (!empty($items->{'@graph'})) {
892 $item = $items->{'@graph'}[0];
894 $config = new stdClass();
895 $config->name = '';
896 if (isset($item->title)) {
897 $config->name = $item->title;
899 if (empty($config->name)) {
900 $config->name = $tool->name;
902 if (isset($item->text)) {
903 $config->introeditor = [
904 'text' => $item->text,
905 'format' => FORMAT_PLAIN
908 if (isset($item->icon->{'@id'})) {
909 $iconurl = new moodle_url($item->icon->{'@id'});
910 // Assign item's icon URL to secureicon or icon depending on its scheme.
911 if (strtolower($iconurl->get_scheme()) === 'https') {
912 $config->secureicon = $iconurl->out(false);
913 } else {
914 $config->icon = $iconurl->out(false);
917 if (isset($item->url)) {
918 $url = new moodle_url($item->url);
919 $config->toolurl = $url->out(false);
920 $config->typeid = 0;
921 } else {
922 $config->typeid = $typeid;
924 $config->instructorchoiceacceptgrades = LTI_SETTING_NEVER;
925 if (!$islti2 && isset($typeconfig['acceptgrades'])) {
926 $acceptgrades = $typeconfig['acceptgrades'];
927 if ($acceptgrades == LTI_SETTING_ALWAYS) {
928 // We create a line item regardless if the definition contains one or not.
929 $config->instructorchoiceacceptgrades = LTI_SETTING_ALWAYS;
931 if ($acceptgrades == LTI_SETTING_DELEGATE || $acceptgrades == LTI_SETTING_ALWAYS) {
932 if (isset($item->lineItem)) {
933 $lineitem = $item->lineItem;
934 $config->instructorchoiceacceptgrades = LTI_SETTING_ALWAYS;
935 $maxscore = 100;
936 if (isset($lineitem->scoreConstraints)) {
937 $sc = $lineitem->scoreConstraints;
938 if (isset($sc->totalMaximum)) {
939 $maxscore = $sc->totalMaximum;
940 } else if (isset($sc->normalMaximum)) {
941 $maxscore = $sc->normalMaximum;
944 $config->grade_modgrade_point = $maxscore;
945 if (isset($lineitem->assignedActivity) && isset($lineitem->assignedActivity->activityId)) {
946 $config->cmidnumber = $lineitem->assignedActivity->activityId;
951 $config->instructorchoicesendname = LTI_SETTING_NEVER;
952 $config->instructorchoicesendemailaddr = LTI_SETTING_NEVER;
953 $config->launchcontainer = LTI_LAUNCH_CONTAINER_DEFAULT;
954 if (isset($item->placementAdvice->presentationDocumentTarget)) {
955 if ($item->placementAdvice->presentationDocumentTarget === 'window') {
956 $config->launchcontainer = LTI_LAUNCH_CONTAINER_WINDOW;
957 } else if ($item->placementAdvice->presentationDocumentTarget === 'frame') {
958 $config->launchcontainer = LTI_LAUNCH_CONTAINER_EMBED_NO_BLOCKS;
959 } else if ($item->placementAdvice->presentationDocumentTarget === 'iframe') {
960 $config->launchcontainer = LTI_LAUNCH_CONTAINER_EMBED;
963 if (isset($item->custom)) {
964 $customparameters = [];
965 foreach ($item->custom as $key => $value) {
966 $customparameters[] = "{$key}={$value}";
968 $config->instructorcustomparameters = implode("\n", $customparameters);
970 $config->contentitemjson = json_encode($item);
972 return $config;
975 function lti_get_tool_table($tools, $id) {
976 global $OUTPUT;
977 $html = '';
979 $typename = get_string('typename', 'lti');
980 $baseurl = get_string('baseurl', 'lti');
981 $action = get_string('action', 'lti');
982 $createdon = get_string('createdon', 'lti');
984 if (!empty($tools)) {
985 $html .= "
986 <div id=\"{$id}_tools_container\" style=\"margin-top:.5em;margin-bottom:.5em\">
987 <table id=\"{$id}_tools\">
988 <thead>
989 <tr>
990 <th>$typename</th>
991 <th>$baseurl</th>
992 <th>$createdon</th>
993 <th>$action</th>
994 </tr>
995 </thead>
998 foreach ($tools as $type) {
999 $date = userdate($type->timecreated, get_string('strftimedatefullshort', 'core_langconfig'));
1000 $accept = get_string('accept', 'lti');
1001 $update = get_string('update', 'lti');
1002 $delete = get_string('delete', 'lti');
1004 if (empty($type->toolproxyid)) {
1005 $baseurl = new \moodle_url('/mod/lti/typessettings.php', array(
1006 'action' => 'accept',
1007 'id' => $type->id,
1008 'sesskey' => sesskey(),
1009 'tab' => $id
1011 $ref = $type->baseurl;
1012 } else {
1013 $baseurl = new \moodle_url('/mod/lti/toolssettings.php', array(
1014 'action' => 'accept',
1015 'id' => $type->id,
1016 'sesskey' => sesskey(),
1017 'tab' => $id
1019 $ref = $type->tpname;
1022 $accepthtml = $OUTPUT->action_icon($baseurl,
1023 new \pix_icon('t/check', $accept, '', array('class' => 'iconsmall')), null,
1024 array('title' => $accept, 'class' => 'editing_accept'));
1026 $deleteaction = 'delete';
1028 if ($type->state == LTI_TOOL_STATE_CONFIGURED) {
1029 $accepthtml = '';
1032 if ($type->state != LTI_TOOL_STATE_REJECTED) {
1033 $deleteaction = 'reject';
1034 $delete = get_string('reject', 'lti');
1037 $updateurl = clone($baseurl);
1038 $updateurl->param('action', 'update');
1039 $updatehtml = $OUTPUT->action_icon($updateurl,
1040 new \pix_icon('t/edit', $update, '', array('class' => 'iconsmall')), null,
1041 array('title' => $update, 'class' => 'editing_update'));
1043 if (($type->state != LTI_TOOL_STATE_REJECTED) || empty($type->toolproxyid)) {
1044 $deleteurl = clone($baseurl);
1045 $deleteurl->param('action', $deleteaction);
1046 $deletehtml = $OUTPUT->action_icon($deleteurl,
1047 new \pix_icon('t/delete', $delete, '', array('class' => 'iconsmall')), null,
1048 array('title' => $delete, 'class' => 'editing_delete'));
1049 } else {
1050 $deletehtml = '';
1052 $html .= "
1053 <tr>
1054 <td>
1055 {$type->name}
1056 </td>
1057 <td>
1058 {$ref}
1059 </td>
1060 <td>
1061 {$date}
1062 </td>
1063 <td align=\"center\">
1064 {$accepthtml}{$updatehtml}{$deletehtml}
1065 </td>
1066 </tr>
1069 $html .= '</table></div>';
1070 } else {
1071 $html .= get_string('no_' . $id, 'lti');
1074 return $html;
1078 * This function builds the tab for a category of tool proxies
1080 * @param object $toolproxies Tool proxy instance objects
1081 * @param string $id Category ID
1083 * @return string HTML for tab
1085 function lti_get_tool_proxy_table($toolproxies, $id) {
1086 global $OUTPUT;
1088 if (!empty($toolproxies)) {
1089 $typename = get_string('typename', 'lti');
1090 $url = get_string('registrationurl', 'lti');
1091 $action = get_string('action', 'lti');
1092 $createdon = get_string('createdon', 'lti');
1094 $html = <<< EOD
1095 <div id="{$id}_tool_proxies_container" style="margin-top: 0.5em; margin-bottom: 0.5em">
1096 <table id="{$id}_tool_proxies">
1097 <thead>
1098 <tr>
1099 <th>{$typename}</th>
1100 <th>{$url}</th>
1101 <th>{$createdon}</th>
1102 <th>{$action}</th>
1103 </tr>
1104 </thead>
1105 EOD;
1106 foreach ($toolproxies as $toolproxy) {
1107 $date = userdate($toolproxy->timecreated, get_string('strftimedatefullshort', 'core_langconfig'));
1108 $accept = get_string('register', 'lti');
1109 $update = get_string('update', 'lti');
1110 $delete = get_string('delete', 'lti');
1112 $baseurl = new \moodle_url('/mod/lti/registersettings.php', array(
1113 'action' => 'accept',
1114 'id' => $toolproxy->id,
1115 'sesskey' => sesskey(),
1116 'tab' => $id
1119 $registerurl = new \moodle_url('/mod/lti/register.php', array(
1120 'id' => $toolproxy->id,
1121 'sesskey' => sesskey(),
1122 'tab' => 'tool_proxy'
1125 $accepthtml = $OUTPUT->action_icon($registerurl,
1126 new \pix_icon('t/check', $accept, '', array('class' => 'iconsmall')), null,
1127 array('title' => $accept, 'class' => 'editing_accept'));
1129 $deleteaction = 'delete';
1131 if ($toolproxy->state != LTI_TOOL_PROXY_STATE_CONFIGURED) {
1132 $accepthtml = '';
1135 if (($toolproxy->state == LTI_TOOL_PROXY_STATE_CONFIGURED) || ($toolproxy->state == LTI_TOOL_PROXY_STATE_PENDING)) {
1136 $delete = get_string('cancel', 'lti');
1139 $updateurl = clone($baseurl);
1140 $updateurl->param('action', 'update');
1141 $updatehtml = $OUTPUT->action_icon($updateurl,
1142 new \pix_icon('t/edit', $update, '', array('class' => 'iconsmall')), null,
1143 array('title' => $update, 'class' => 'editing_update'));
1145 $deleteurl = clone($baseurl);
1146 $deleteurl->param('action', $deleteaction);
1147 $deletehtml = $OUTPUT->action_icon($deleteurl,
1148 new \pix_icon('t/delete', $delete, '', array('class' => 'iconsmall')), null,
1149 array('title' => $delete, 'class' => 'editing_delete'));
1150 $html .= <<< EOD
1151 <tr>
1152 <td>
1153 {$toolproxy->name}
1154 </td>
1155 <td>
1156 {$toolproxy->regurl}
1157 </td>
1158 <td>
1159 {$date}
1160 </td>
1161 <td align="center">
1162 {$accepthtml}{$updatehtml}{$deletehtml}
1163 </td>
1164 </tr>
1165 EOD;
1167 $html .= '</table></div>';
1168 } else {
1169 $html = get_string('no_' . $id, 'lti');
1172 return $html;
1176 * Extracts the enabled capabilities into an array, including those implicitly declared in a parameter
1178 * @param object $tool Tool instance object
1180 * @return array List of enabled capabilities
1182 function lti_get_enabled_capabilities($tool) {
1183 if (!isset($tool)) {
1184 return array();
1186 if (!empty($tool->enabledcapability)) {
1187 $enabledcapabilities = explode("\n", $tool->enabledcapability);
1188 } else {
1189 $enabledcapabilities = array();
1191 $paramstr = str_replace("\r\n", "\n", $tool->parameter);
1192 $paramstr = str_replace("\n\r", "\n", $paramstr);
1193 $paramstr = str_replace("\r", "\n", $paramstr);
1194 $params = explode("\n", $paramstr);
1195 foreach ($params as $param) {
1196 $pos = strpos($param, '=');
1197 if (($pos === false) || ($pos < 1)) {
1198 continue;
1200 $value = trim(core_text::substr($param, $pos + 1, strlen($param)));
1201 if (substr($value, 0, 1) == '$') {
1202 $value = substr($value, 1);
1203 if (!in_array($value, $enabledcapabilities)) {
1204 $enabledcapabilities[] = $value;
1208 return $enabledcapabilities;
1212 * Splits the custom parameters field to the various parameters
1214 * @param object $toolproxy Tool proxy instance object
1215 * @param object $tool Tool instance object
1216 * @param array $params LTI launch parameters
1217 * @param string $customstr String containing the parameters
1218 * @param boolean $islti2 True if an LTI 2 tool is being launched
1220 * @return array of custom parameters
1222 function lti_split_custom_parameters($toolproxy, $tool, $params, $customstr, $islti2 = false) {
1223 $customstr = str_replace("\r\n", "\n", $customstr);
1224 $customstr = str_replace("\n\r", "\n", $customstr);
1225 $customstr = str_replace("\r", "\n", $customstr);
1226 $lines = explode("\n", $customstr); // Or should this split on "/[\n;]/"?
1227 $retval = array();
1228 foreach ($lines as $line) {
1229 $pos = strpos($line, '=');
1230 if ( $pos === false || $pos < 1 ) {
1231 continue;
1233 $key = trim(core_text::substr($line, 0, $pos));
1234 $key = lti_map_keyname($key, false);
1235 $val = trim(core_text::substr($line, $pos + 1, strlen($line)));
1236 $val = lti_parse_custom_parameter($toolproxy, $tool, $params, $val, $islti2);
1237 $key2 = lti_map_keyname($key);
1238 $retval['custom_'.$key2] = $val;
1239 if ($key != $key2) {
1240 $retval['custom_'.$key] = $val;
1243 return $retval;
1247 * Adds the custom parameters to an array
1249 * @param object $toolproxy Tool proxy instance object
1250 * @param object $tool Tool instance object
1251 * @param array $params LTI launch parameters
1252 * @param array $parameters Array containing the parameters
1254 * @return array Array of custom parameters
1256 function lti_get_custom_parameters($toolproxy, $tool, $params, $parameters) {
1257 $retval = array();
1258 foreach ($parameters as $key => $val) {
1259 $key2 = lti_map_keyname($key);
1260 $val = lti_parse_custom_parameter($toolproxy, $tool, $params, $val, true);
1261 $retval['custom_'.$key2] = $val;
1262 if ($key != $key2) {
1263 $retval['custom_'.$key] = $val;
1266 return $retval;
1270 * Parse a custom parameter to replace any substitution variables
1272 * @param object $toolproxy Tool proxy instance object
1273 * @param object $tool Tool instance object
1274 * @param array $params LTI launch parameters
1275 * @param string $value Custom parameter value
1276 * @param boolean $islti2 True if an LTI 2 tool is being launched
1278 * @return string Parsed value of custom parameter
1280 function lti_parse_custom_parameter($toolproxy, $tool, $params, $value, $islti2) {
1281 // This is required as {${$valarr[0]}->{$valarr[1]}}" may be using the USER or COURSE var.
1282 global $USER, $COURSE;
1284 if ($value) {
1285 if (substr($value, 0, 1) == '\\') {
1286 $value = substr($value, 1);
1287 } else if (substr($value, 0, 1) == '$') {
1288 $value1 = substr($value, 1);
1289 $enabledcapabilities = lti_get_enabled_capabilities($tool);
1290 if (!$islti2 || in_array($value1, $enabledcapabilities)) {
1291 $capabilities = lti_get_capabilities();
1292 if (array_key_exists($value1, $capabilities)) {
1293 $val = $capabilities[$value1];
1294 if ($val) {
1295 if (substr($val, 0, 1) != '$') {
1296 $value = $params[$val];
1297 } else {
1298 $valarr = explode('->', substr($val, 1), 2);
1299 $value = "{${$valarr[0]}->{$valarr[1]}}";
1300 $value = str_replace('<br />' , ' ', $value);
1301 $value = str_replace('<br>' , ' ', $value);
1302 $value = format_string($value);
1304 } else {
1305 $value = lti_calculate_custom_parameter($value1);
1307 } else if ($islti2) {
1308 $val = $value;
1309 $services = lti_get_services();
1310 foreach ($services as $service) {
1311 $service->set_tool_proxy($toolproxy);
1312 $value = $service->parse_value($val);
1313 if ($val != $value) {
1314 break;
1321 return $value;
1325 * Calculates the value of a custom parameter that has not been specified earlier
1327 * @param string $value Custom parameter value
1329 * @return string Calculated value of custom parameter
1331 function lti_calculate_custom_parameter($value) {
1332 global $USER, $COURSE;
1334 switch ($value) {
1335 case 'Moodle.Person.userGroupIds':
1336 return implode(",", groups_get_user_groups($COURSE->id, $USER->id)[0]);
1338 return null;
1342 * Used for building the names of the different custom parameters
1344 * @param string $key Parameter name
1345 * @param bool $tolower Do we want to convert the key into lower case?
1346 * @return string Processed name
1348 function lti_map_keyname($key, $tolower = true) {
1349 $newkey = "";
1350 if ($tolower) {
1351 $key = core_text::strtolower(trim($key));
1353 foreach (str_split($key) as $ch) {
1354 if ( ($ch >= 'a' && $ch <= 'z') || ($ch >= '0' && $ch <= '9') || (!$tolower && ($ch >= 'A' && $ch <= 'Z'))) {
1355 $newkey .= $ch;
1356 } else {
1357 $newkey .= '_';
1360 return $newkey;
1364 * Gets the IMS role string for the specified user and LTI course module.
1366 * @param mixed $user User object or user id
1367 * @param int $cmid The course module id of the LTI activity
1368 * @param int $courseid The course id of the LTI activity
1369 * @param boolean $islti2 True if an LTI 2 tool is being launched
1371 * @return string A role string suitable for passing with an LTI launch
1373 function lti_get_ims_role($user, $cmid, $courseid, $islti2) {
1374 $roles = array();
1376 if (empty($cmid)) {
1377 // If no cmid is passed, check if the user is a teacher in the course
1378 // This allows other modules to programmatically "fake" a launch without
1379 // a real LTI instance.
1380 $context = context_course::instance($courseid);
1382 if (has_capability('moodle/course:manageactivities', $context, $user)) {
1383 array_push($roles, 'Instructor');
1384 } else {
1385 array_push($roles, 'Learner');
1387 } else {
1388 $context = context_module::instance($cmid);
1390 if (has_capability('mod/lti:manage', $context)) {
1391 array_push($roles, 'Instructor');
1392 } else {
1393 array_push($roles, 'Learner');
1397 if (is_siteadmin($user) || has_capability('mod/lti:admin', $context)) {
1398 // Make sure admins do not have the Learner role, then set admin role.
1399 $roles = array_diff($roles, array('Learner'));
1400 if (!$islti2) {
1401 array_push($roles, 'urn:lti:sysrole:ims/lis/Administrator', 'urn:lti:instrole:ims/lis/Administrator');
1402 } else {
1403 array_push($roles, 'http://purl.imsglobal.org/vocab/lis/v2/person#Administrator');
1407 return join(',', $roles);
1411 * Returns configuration details for the tool
1413 * @param int $typeid Basic LTI tool typeid
1415 * @return array Tool Configuration
1417 function lti_get_type_config($typeid) {
1418 global $DB;
1420 $query = "SELECT name, value
1421 FROM {lti_types_config}
1422 WHERE typeid = :typeid1
1423 UNION ALL
1424 SELECT 'toolurl' AS name, baseurl AS value
1425 FROM {lti_types}
1426 WHERE id = :typeid2
1427 UNION ALL
1428 SELECT 'icon' AS name, icon AS value
1429 FROM {lti_types}
1430 WHERE id = :typeid3
1431 UNION ALL
1432 SELECT 'secureicon' AS name, secureicon AS value
1433 FROM {lti_types}
1434 WHERE id = :typeid4";
1436 $typeconfig = array();
1437 $configs = $DB->get_records_sql($query,
1438 array('typeid1' => $typeid, 'typeid2' => $typeid, 'typeid3' => $typeid, 'typeid4' => $typeid));
1440 if (!empty($configs)) {
1441 foreach ($configs as $config) {
1442 $typeconfig[$config->name] = $config->value;
1446 return $typeconfig;
1449 function lti_get_tools_by_url($url, $state, $courseid = null) {
1450 $domain = lti_get_domain_from_url($url);
1452 return lti_get_tools_by_domain($domain, $state, $courseid);
1455 function lti_get_tools_by_domain($domain, $state = null, $courseid = null) {
1456 global $DB, $SITE;
1458 $statefilter = '';
1459 $coursefilter = '';
1461 if ($state) {
1462 $statefilter = 'AND state = :state';
1465 if ($courseid && $courseid != $SITE->id) {
1466 $coursefilter = 'OR course = :courseid';
1469 $query = "SELECT *
1470 FROM {lti_types}
1471 WHERE tooldomain = :tooldomain
1472 AND (course = :siteid $coursefilter)
1473 $statefilter";
1475 return $DB->get_records_sql($query, array(
1476 'courseid' => $courseid,
1477 'siteid' => $SITE->id,
1478 'tooldomain' => $domain,
1479 'state' => $state
1484 * Returns all basicLTI tools configured by the administrator
1486 * @param int $course
1488 * @return array
1490 function lti_filter_get_types($course) {
1491 global $DB;
1493 if (!empty($course)) {
1494 $where = "WHERE t.course = :course";
1495 $params = array('course' => $course);
1496 } else {
1497 $where = '';
1498 $params = array();
1500 $query = "SELECT t.id, t.name, t.baseurl, t.state, t.toolproxyid, t.timecreated, tp.name tpname
1501 FROM {lti_types} t LEFT OUTER JOIN {lti_tool_proxies} tp ON t.toolproxyid = tp.id
1502 {$where}";
1503 return $DB->get_records_sql($query, $params);
1507 * Given an array of tools, filter them based on their state
1509 * @param array $tools An array of lti_types records
1510 * @param int $state One of the LTI_TOOL_STATE_* constants
1511 * @return array
1513 function lti_filter_tool_types(array $tools, $state) {
1514 $return = array();
1515 foreach ($tools as $key => $tool) {
1516 if ($tool->state == $state) {
1517 $return[$key] = $tool;
1520 return $return;
1524 * Returns all lti types visible in this course
1526 * @param int $courseid The id of the course to retieve types for
1527 * @param array $coursevisible options for 'coursevisible' field,
1528 * default [LTI_COURSEVISIBLE_PRECONFIGURED, LTI_COURSEVISIBLE_ACTIVITYCHOOSER]
1529 * @return stdClass[] All the lti types visible in the given course
1531 function lti_get_lti_types_by_course($courseid, $coursevisible = null) {
1532 global $DB, $SITE;
1534 if ($coursevisible === null) {
1535 $coursevisible = [LTI_COURSEVISIBLE_PRECONFIGURED, LTI_COURSEVISIBLE_ACTIVITYCHOOSER];
1538 list($coursevisiblesql, $coursevisparams) = $DB->get_in_or_equal($coursevisible, SQL_PARAMS_NAMED, 'coursevisible');
1539 $query = "SELECT *
1540 FROM {lti_types}
1541 WHERE coursevisible $coursevisiblesql
1542 AND (course = :siteid OR course = :courseid)
1543 AND state = :active";
1545 return $DB->get_records_sql($query,
1546 array('siteid' => $SITE->id, 'courseid' => $courseid, 'active' => LTI_TOOL_STATE_CONFIGURED) + $coursevisparams);
1550 * Returns tool types for lti add instance and edit page
1552 * @return array Array of lti types
1554 function lti_get_types_for_add_instance() {
1555 global $COURSE;
1556 $admintypes = lti_get_lti_types_by_course($COURSE->id);
1558 $types = array();
1559 $types[0] = (object)array('name' => get_string('automatic', 'lti'), 'course' => 0, 'toolproxyid' => null);
1561 foreach ($admintypes as $type) {
1562 $types[$type->id] = $type;
1565 return $types;
1569 * Returns a list of configured types in the given course
1571 * @param int $courseid The id of the course to retieve types for
1572 * @param int $sectionreturn section to return to for forming the URLs
1573 * @return array Array of lti types. Each element is object with properties: name, title, icon, help, helplink, link
1575 function lti_get_configured_types($courseid, $sectionreturn = 0) {
1576 global $OUTPUT;
1577 $types = array();
1578 $admintypes = lti_get_lti_types_by_course($courseid, [LTI_COURSEVISIBLE_ACTIVITYCHOOSER]);
1580 foreach ($admintypes as $ltitype) {
1581 $type = new stdClass();
1582 $type->modclass = MOD_CLASS_ACTIVITY;
1583 $type->name = 'lti_type_' . $ltitype->id;
1584 // Clean the name. We don't want tags here.
1585 $type->title = clean_param($ltitype->name, PARAM_NOTAGS);
1586 $trimmeddescription = trim($ltitype->description);
1587 if ($trimmeddescription != '') {
1588 // Clean the description. We don't want tags here.
1589 $type->help = clean_param($trimmeddescription, PARAM_NOTAGS);
1590 $type->helplink = get_string('modulename_shortcut_link', 'lti');
1592 if (empty($ltitype->icon)) {
1593 $type->icon = $OUTPUT->pix_icon('icon', '', 'lti', array('class' => 'icon'));
1594 } else {
1595 $type->icon = html_writer::empty_tag('img', array('src' => $ltitype->icon, 'alt' => $ltitype->name, 'class' => 'icon'));
1597 $type->link = new moodle_url('/course/modedit.php', array('add' => 'lti', 'return' => 0, 'course' => $courseid,
1598 'sr' => $sectionreturn, 'typeid' => $ltitype->id));
1599 $types[] = $type;
1601 return $types;
1604 function lti_get_domain_from_url($url) {
1605 $matches = array();
1607 if (preg_match(LTI_URL_DOMAIN_REGEX, $url, $matches)) {
1608 return $matches[1];
1612 function lti_get_tool_by_url_match($url, $courseid = null, $state = LTI_TOOL_STATE_CONFIGURED) {
1613 $possibletools = lti_get_tools_by_url($url, $state, $courseid);
1615 return lti_get_best_tool_by_url($url, $possibletools, $courseid);
1618 function lti_get_url_thumbprint($url) {
1619 // Parse URL requires a schema otherwise everything goes into 'path'. Fixed 5.4.7 or later.
1620 if (preg_match('/https?:\/\//', $url) !== 1) {
1621 $url = 'http://'.$url;
1623 $urlparts = parse_url(strtolower($url));
1624 if (!isset($urlparts['path'])) {
1625 $urlparts['path'] = '';
1628 if (!isset($urlparts['query'])) {
1629 $urlparts['query'] = '';
1632 if (!isset($urlparts['host'])) {
1633 $urlparts['host'] = '';
1636 if (substr($urlparts['host'], 0, 4) === 'www.') {
1637 $urlparts['host'] = substr($urlparts['host'], 4);
1640 $urllower = $urlparts['host'] . '/' . $urlparts['path'];
1642 if ($urlparts['query'] != '') {
1643 $urllower .= '?' . $urlparts['query'];
1646 return $urllower;
1649 function lti_get_best_tool_by_url($url, $tools, $courseid = null) {
1650 if (count($tools) === 0) {
1651 return null;
1654 $urllower = lti_get_url_thumbprint($url);
1656 foreach ($tools as $tool) {
1657 $tool->_matchscore = 0;
1659 $toolbaseurllower = lti_get_url_thumbprint($tool->baseurl);
1661 if ($urllower === $toolbaseurllower) {
1662 // 100 points for exact thumbprint match.
1663 $tool->_matchscore += 100;
1664 } else if (substr($urllower, 0, strlen($toolbaseurllower)) === $toolbaseurllower) {
1665 // 50 points if tool thumbprint starts with the base URL thumbprint.
1666 $tool->_matchscore += 50;
1669 // Prefer course tools over site tools.
1670 if (!empty($courseid)) {
1671 // Minus 10 points for not matching the course id (global tools).
1672 if ($tool->course != $courseid) {
1673 $tool->_matchscore -= 10;
1678 $bestmatch = array_reduce($tools, function($value, $tool) {
1679 if ($tool->_matchscore > $value->_matchscore) {
1680 return $tool;
1681 } else {
1682 return $value;
1685 }, (object)array('_matchscore' => -1));
1687 // None of the tools are suitable for this URL.
1688 if ($bestmatch->_matchscore <= 0) {
1689 return null;
1692 return $bestmatch;
1695 function lti_get_shared_secrets_by_key($key) {
1696 global $DB;
1698 // Look up the shared secret for the specified key in both the types_config table (for configured tools)
1699 // And in the lti resource table for ad-hoc tools.
1700 $query = "SELECT t2.value
1701 FROM {lti_types_config} t1
1702 JOIN {lti_types_config} t2 ON t1.typeid = t2.typeid
1703 JOIN {lti_types} type ON t2.typeid = type.id
1704 WHERE t1.name = 'resourcekey'
1705 AND t1.value = :key1
1706 AND t2.name = 'password'
1707 AND type.state = :configured1
1708 UNION
1709 SELECT tp.secret AS value
1710 FROM {lti_tool_proxies} tp
1711 JOIN {lti_types} t ON tp.id = t.toolproxyid
1712 WHERE tp.guid = :key2
1713 AND t.state = :configured2
1714 UNION
1715 SELECT password AS value
1716 FROM {lti}
1717 WHERE resourcekey = :key3";
1719 $sharedsecrets = $DB->get_records_sql($query, array('configured1' => LTI_TOOL_STATE_CONFIGURED,
1720 'configured2' => LTI_TOOL_STATE_CONFIGURED, 'key1' => $key, 'key2' => $key, 'key3' => $key));
1722 $values = array_map(function($item) {
1723 return $item->value;
1724 }, $sharedsecrets);
1726 // There should really only be one shared secret per key. But, we can't prevent
1727 // more than one getting entered. For instance, if the same key is used for two tool providers.
1728 return $values;
1732 * Delete a Basic LTI configuration
1734 * @param int $id Configuration id
1736 function lti_delete_type($id) {
1737 global $DB;
1739 // We should probably just copy the launch URL to the tool instances in this case... using a single query.
1741 $instances = $DB->get_records('lti', array('typeid' => $id));
1742 foreach ($instances as $instance) {
1743 $instance->typeid = 0;
1744 $DB->update_record('lti', $instance);
1747 $DB->delete_records('lti_types', array('id' => $id));
1748 $DB->delete_records('lti_types_config', array('typeid' => $id));
1751 function lti_set_state_for_type($id, $state) {
1752 global $DB;
1754 $DB->update_record('lti_types', (object)array('id' => $id, 'state' => $state));
1758 * Transforms a basic LTI object to an array
1760 * @param object $ltiobject Basic LTI object
1762 * @return array Basic LTI configuration details
1764 function lti_get_config($ltiobject) {
1765 $typeconfig = (array)$ltiobject;
1766 $additionalconfig = lti_get_type_config($ltiobject->typeid);
1767 $typeconfig = array_merge($typeconfig, $additionalconfig);
1768 return $typeconfig;
1773 * Generates some of the tool configuration based on the instance details
1775 * @param int $id
1777 * @return object configuration
1780 function lti_get_type_config_from_instance($id) {
1781 global $DB;
1783 $instance = $DB->get_record('lti', array('id' => $id));
1784 $config = lti_get_config($instance);
1786 $type = new \stdClass();
1787 $type->lti_fix = $id;
1788 if (isset($config['toolurl'])) {
1789 $type->lti_toolurl = $config['toolurl'];
1791 if (isset($config['instructorchoicesendname'])) {
1792 $type->lti_sendname = $config['instructorchoicesendname'];
1794 if (isset($config['instructorchoicesendemailaddr'])) {
1795 $type->lti_sendemailaddr = $config['instructorchoicesendemailaddr'];
1797 if (isset($config['instructorchoiceacceptgrades'])) {
1798 $type->lti_acceptgrades = $config['instructorchoiceacceptgrades'];
1800 if (isset($config['instructorchoiceallowroster'])) {
1801 $type->lti_allowroster = $config['instructorchoiceallowroster'];
1804 if (isset($config['instructorcustomparameters'])) {
1805 $type->lti_allowsetting = $config['instructorcustomparameters'];
1807 return $type;
1811 * Generates some of the tool configuration based on the admin configuration details
1813 * @param int $id
1815 * @return stdClass Configuration details
1817 function lti_get_type_type_config($id) {
1818 global $DB;
1820 $basicltitype = $DB->get_record('lti_types', array('id' => $id));
1821 $config = lti_get_type_config($id);
1823 $type = new \stdClass();
1825 $type->lti_typename = $basicltitype->name;
1827 $type->typeid = $basicltitype->id;
1829 $type->toolproxyid = $basicltitype->toolproxyid;
1831 $type->lti_toolurl = $basicltitype->baseurl;
1833 $type->lti_description = $basicltitype->description;
1835 $type->lti_parameters = $basicltitype->parameter;
1837 $type->lti_icon = $basicltitype->icon;
1839 $type->lti_secureicon = $basicltitype->secureicon;
1841 if (isset($config['resourcekey'])) {
1842 $type->lti_resourcekey = $config['resourcekey'];
1844 if (isset($config['password'])) {
1845 $type->lti_password = $config['password'];
1848 if (isset($config['sendname'])) {
1849 $type->lti_sendname = $config['sendname'];
1851 if (isset($config['instructorchoicesendname'])) {
1852 $type->lti_instructorchoicesendname = $config['instructorchoicesendname'];
1854 if (isset($config['sendemailaddr'])) {
1855 $type->lti_sendemailaddr = $config['sendemailaddr'];
1857 if (isset($config['instructorchoicesendemailaddr'])) {
1858 $type->lti_instructorchoicesendemailaddr = $config['instructorchoicesendemailaddr'];
1860 if (isset($config['acceptgrades'])) {
1861 $type->lti_acceptgrades = $config['acceptgrades'];
1863 if (isset($config['instructorchoiceacceptgrades'])) {
1864 $type->lti_instructorchoiceacceptgrades = $config['instructorchoiceacceptgrades'];
1866 if (isset($config['allowroster'])) {
1867 $type->lti_allowroster = $config['allowroster'];
1869 if (isset($config['instructorchoiceallowroster'])) {
1870 $type->lti_instructorchoiceallowroster = $config['instructorchoiceallowroster'];
1873 if (isset($config['customparameters'])) {
1874 $type->lti_customparameters = $config['customparameters'];
1877 if (isset($config['forcessl'])) {
1878 $type->lti_forcessl = $config['forcessl'];
1881 if (isset($config['organizationid'])) {
1882 $type->lti_organizationid = $config['organizationid'];
1884 if (isset($config['organizationurl'])) {
1885 $type->lti_organizationurl = $config['organizationurl'];
1887 if (isset($config['organizationdescr'])) {
1888 $type->lti_organizationdescr = $config['organizationdescr'];
1890 if (isset($config['launchcontainer'])) {
1891 $type->lti_launchcontainer = $config['launchcontainer'];
1894 if (isset($config['coursevisible'])) {
1895 $type->lti_coursevisible = $config['coursevisible'];
1898 if (isset($config['contentitem'])) {
1899 $type->lti_contentitem = $config['contentitem'];
1902 if (isset($config['toolurl_ContentItemSelectionRequest'])) {
1903 $type->lti_toolurl_ContentItemSelectionRequest = $config['toolurl_ContentItemSelectionRequest'];
1906 if (isset($config['debuglaunch'])) {
1907 $type->lti_debuglaunch = $config['debuglaunch'];
1910 if (isset($config['module_class_type'])) {
1911 $type->lti_module_class_type = $config['module_class_type'];
1914 // Get the parameters from the LTI services.
1915 $services = lti_get_services();
1916 $ltiserviceprefixlength = 11;
1917 foreach ($services as $service) {
1918 $configurationparameters = $service->get_configuration_parameter_names();
1919 foreach ($configurationparameters as $ltiserviceparameter) {
1920 $shortltiserviceparameter = substr($ltiserviceparameter, $ltiserviceprefixlength);
1921 if (isset($config[$shortltiserviceparameter])) {
1922 $type->$ltiserviceparameter = $config[$shortltiserviceparameter];
1927 return $type;
1930 function lti_prepare_type_for_save($type, $config) {
1931 if (isset($config->lti_toolurl)) {
1932 $type->baseurl = $config->lti_toolurl;
1933 $type->tooldomain = lti_get_domain_from_url($config->lti_toolurl);
1935 if (isset($config->lti_description)) {
1936 $type->description = $config->lti_description;
1938 if (isset($config->lti_typename)) {
1939 $type->name = $config->lti_typename;
1941 if (isset($config->lti_coursevisible)) {
1942 $type->coursevisible = $config->lti_coursevisible;
1945 if (isset($config->lti_icon)) {
1946 $type->icon = $config->lti_icon;
1948 if (isset($config->lti_secureicon)) {
1949 $type->secureicon = $config->lti_secureicon;
1952 $type->forcessl = !empty($config->lti_forcessl) ? $config->lti_forcessl : 0;
1953 $config->lti_forcessl = $type->forcessl;
1954 if (isset($config->lti_contentitem)) {
1955 $type->contentitem = !empty($config->lti_contentitem) ? $config->lti_contentitem : 0;
1956 $config->lti_contentitem = $type->contentitem;
1958 if (isset($config->lti_toolurl_ContentItemSelectionRequest)) {
1959 if (!empty($config->lti_toolurl_ContentItemSelectionRequest)) {
1960 $type->toolurl_ContentItemSelectionRequest = $config->lti_toolurl_ContentItemSelectionRequest;
1961 } else {
1962 $type->toolurl_ContentItemSelectionRequest = '';
1964 $config->lti_toolurl_ContentItemSelectionRequest = $type->toolurl_ContentItemSelectionRequest;
1967 $type->timemodified = time();
1969 unset ($config->lti_typename);
1970 unset ($config->lti_toolurl);
1971 unset ($config->lti_description);
1972 unset ($config->lti_icon);
1973 unset ($config->lti_secureicon);
1976 function lti_update_type($type, $config) {
1977 global $DB, $CFG;
1979 lti_prepare_type_for_save($type, $config);
1981 if (lti_request_is_using_ssl() && !empty($type->secureicon)) {
1982 $clearcache = !isset($config->oldicon) || ($config->oldicon !== $type->secureicon);
1983 } else {
1984 $clearcache = isset($type->icon) && (!isset($config->oldicon) || ($config->oldicon !== $type->icon));
1986 unset($config->oldicon);
1988 if ($DB->update_record('lti_types', $type)) {
1989 foreach ($config as $key => $value) {
1990 if (substr($key, 0, 4) == 'lti_' && !is_null($value)) {
1991 $record = new \StdClass();
1992 $record->typeid = $type->id;
1993 $record->name = substr($key, 4);
1994 $record->value = $value;
1995 lti_update_config($record);
1997 if (substr($key, 0, 11) == 'ltiservice_' && !is_null($value)) {
1998 $record = new \StdClass();
1999 $record->typeid = $type->id;
2000 $record->name = substr($key, 11);
2001 $record->value = $value;
2002 lti_update_config($record);
2005 require_once($CFG->libdir.'/modinfolib.php');
2006 if ($clearcache) {
2007 $sql = "SELECT DISTINCT course
2008 FROM {lti}
2009 WHERE typeid = ?";
2011 $courses = $DB->get_fieldset_sql($sql, array($type->id));
2013 foreach ($courses as $courseid) {
2014 rebuild_course_cache($courseid, true);
2020 function lti_add_type($type, $config) {
2021 global $USER, $SITE, $DB;
2023 lti_prepare_type_for_save($type, $config);
2025 if (!isset($type->state)) {
2026 $type->state = LTI_TOOL_STATE_PENDING;
2029 if (!isset($type->timecreated)) {
2030 $type->timecreated = time();
2033 if (!isset($type->createdby)) {
2034 $type->createdby = $USER->id;
2037 if (!isset($type->course)) {
2038 $type->course = $SITE->id;
2041 // Create a salt value to be used for signing passed data to extension services
2042 // The outcome service uses the service salt on the instance. This can be used
2043 // for communication with services not related to a specific LTI instance.
2044 $config->lti_servicesalt = uniqid('', true);
2046 $id = $DB->insert_record('lti_types', $type);
2048 if ($id) {
2049 foreach ($config as $key => $value) {
2050 if (!is_null($value)) {
2051 $fieldparts = preg_split("/(lti|ltiservice)_/i", $key);
2052 // If array has only one element, it did not start with the pattern.
2053 if (count($fieldparts) < 2) {
2054 continue;
2056 $fieldname = $fieldparts[1];
2058 $record = new \StdClass();
2059 $record->typeid = $id;
2060 $record->name = $fieldname;
2061 $record->value = $value;
2063 lti_add_config($record);
2068 return $id;
2072 * Given an array of tool proxies, filter them based on their state
2074 * @param array $toolproxies An array of lti_tool_proxies records
2075 * @param int $state One of the LTI_TOOL_PROXY_STATE_* constants
2077 * @return array
2079 function lti_filter_tool_proxy_types(array $toolproxies, $state) {
2080 $return = array();
2081 foreach ($toolproxies as $key => $toolproxy) {
2082 if ($toolproxy->state == $state) {
2083 $return[$key] = $toolproxy;
2086 return $return;
2090 * Get the tool proxy instance given its GUID
2092 * @param string $toolproxyguid Tool proxy GUID value
2094 * @return object
2096 function lti_get_tool_proxy_from_guid($toolproxyguid) {
2097 global $DB;
2099 $toolproxy = $DB->get_record('lti_tool_proxies', array('guid' => $toolproxyguid));
2101 return $toolproxy;
2105 * Get the tool proxy instance given its registration URL
2107 * @param string $regurl Tool proxy registration URL
2109 * @return array The record of the tool proxy with this url
2111 function lti_get_tool_proxies_from_registration_url($regurl) {
2112 global $DB;
2114 return $DB->get_records_sql(
2115 'SELECT * FROM {lti_tool_proxies}
2116 WHERE '.$DB->sql_compare_text('regurl', 256).' = :regurl',
2117 array('regurl' => $regurl)
2122 * Generates some of the tool proxy configuration based on the admin configuration details
2124 * @param int $id
2126 * @return mixed Tool Proxy details
2128 function lti_get_tool_proxy($id) {
2129 global $DB;
2131 $toolproxy = $DB->get_record('lti_tool_proxies', array('id' => $id));
2132 return $toolproxy;
2136 * Returns lti tool proxies.
2138 * @param bool $orphanedonly Only retrieves tool proxies that have no type associated with them
2139 * @return array of basicLTI types
2141 function lti_get_tool_proxies($orphanedonly) {
2142 global $DB;
2144 if ($orphanedonly) {
2145 $usedproxyids = array_values($DB->get_fieldset_select('lti_types', 'toolproxyid', 'toolproxyid IS NOT NULL'));
2146 $proxies = $DB->get_records('lti_tool_proxies', null, 'state DESC, timemodified DESC');
2147 foreach ($proxies as $key => $value) {
2148 if (in_array($value->id, $usedproxyids)) {
2149 unset($proxies[$key]);
2152 return $proxies;
2153 } else {
2154 return $DB->get_records('lti_tool_proxies', null, 'state DESC, timemodified DESC');
2159 * Generates some of the tool proxy configuration based on the admin configuration details
2161 * @param int $id
2163 * @return mixed Tool Proxy details
2165 function lti_get_tool_proxy_config($id) {
2166 $toolproxy = lti_get_tool_proxy($id);
2168 $tp = new \stdClass();
2169 $tp->lti_registrationname = $toolproxy->name;
2170 $tp->toolproxyid = $toolproxy->id;
2171 $tp->state = $toolproxy->state;
2172 $tp->lti_registrationurl = $toolproxy->regurl;
2173 $tp->lti_capabilities = explode("\n", $toolproxy->capabilityoffered);
2174 $tp->lti_services = explode("\n", $toolproxy->serviceoffered);
2176 return $tp;
2180 * Update the database with a tool proxy instance
2182 * @param object $config Tool proxy definition
2184 * @return int Record id number
2186 function lti_add_tool_proxy($config) {
2187 global $USER, $DB;
2189 $toolproxy = new \stdClass();
2190 if (isset($config->lti_registrationname)) {
2191 $toolproxy->name = trim($config->lti_registrationname);
2193 if (isset($config->lti_registrationurl)) {
2194 $toolproxy->regurl = trim($config->lti_registrationurl);
2196 if (isset($config->lti_capabilities)) {
2197 $toolproxy->capabilityoffered = implode("\n", $config->lti_capabilities);
2198 } else {
2199 $toolproxy->capabilityoffered = implode("\n", array_keys(lti_get_capabilities()));
2201 if (isset($config->lti_services)) {
2202 $toolproxy->serviceoffered = implode("\n", $config->lti_services);
2203 } else {
2204 $func = function($s) {
2205 return $s->get_id();
2207 $servicenames = array_map($func, lti_get_services());
2208 $toolproxy->serviceoffered = implode("\n", $servicenames);
2210 if (isset($config->toolproxyid) && !empty($config->toolproxyid)) {
2211 $toolproxy->id = $config->toolproxyid;
2212 if (!isset($toolproxy->state) || ($toolproxy->state != LTI_TOOL_PROXY_STATE_ACCEPTED)) {
2213 $toolproxy->state = LTI_TOOL_PROXY_STATE_CONFIGURED;
2214 $toolproxy->guid = random_string();
2215 $toolproxy->secret = random_string();
2217 $id = lti_update_tool_proxy($toolproxy);
2218 } else {
2219 $toolproxy->state = LTI_TOOL_PROXY_STATE_CONFIGURED;
2220 $toolproxy->timemodified = time();
2221 $toolproxy->timecreated = $toolproxy->timemodified;
2222 if (!isset($toolproxy->createdby)) {
2223 $toolproxy->createdby = $USER->id;
2225 $toolproxy->guid = random_string();
2226 $toolproxy->secret = random_string();
2227 $id = $DB->insert_record('lti_tool_proxies', $toolproxy);
2230 return $id;
2234 * Updates a tool proxy in the database
2236 * @param object $toolproxy Tool proxy
2238 * @return int Record id number
2240 function lti_update_tool_proxy($toolproxy) {
2241 global $DB;
2243 $toolproxy->timemodified = time();
2244 $id = $DB->update_record('lti_tool_proxies', $toolproxy);
2246 return $id;
2250 * Delete a Tool Proxy
2252 * @param int $id Tool Proxy id
2254 function lti_delete_tool_proxy($id) {
2255 global $DB;
2256 $DB->delete_records('lti_tool_settings', array('toolproxyid' => $id));
2257 $tools = $DB->get_records('lti_types', array('toolproxyid' => $id));
2258 foreach ($tools as $tool) {
2259 lti_delete_type($tool->id);
2261 $DB->delete_records('lti_tool_proxies', array('id' => $id));
2265 * Add a tool configuration in the database
2267 * @param object $config Tool configuration
2269 * @return int Record id number
2271 function lti_add_config($config) {
2272 global $DB;
2274 return $DB->insert_record('lti_types_config', $config);
2278 * Updates a tool configuration in the database
2280 * @param object $config Tool configuration
2282 * @return mixed Record id number
2284 function lti_update_config($config) {
2285 global $DB;
2287 $old = $DB->get_record('lti_types_config', array('typeid' => $config->typeid, 'name' => $config->name));
2289 if ($old) {
2290 $config->id = $old->id;
2291 $return = $DB->update_record('lti_types_config', $config);
2292 } else {
2293 $return = $DB->insert_record('lti_types_config', $config);
2295 return $return;
2299 * Gets the tool settings
2301 * @param int $toolproxyid Id of tool proxy record
2302 * @param int $courseid Id of course (null if system settings)
2303 * @param int $instanceid Id of course module (null if system or context settings)
2305 * @return array Array settings
2307 function lti_get_tool_settings($toolproxyid, $courseid = null, $instanceid = null) {
2308 global $DB;
2310 $settings = array();
2311 $settingsstr = $DB->get_field('lti_tool_settings', 'settings', array('toolproxyid' => $toolproxyid,
2312 'course' => $courseid, 'coursemoduleid' => $instanceid));
2313 if ($settingsstr !== false) {
2314 $settings = json_decode($settingsstr, true);
2316 return $settings;
2320 * Sets the tool settings (
2322 * @param array $settings Array of settings
2323 * @param int $toolproxyid Id of tool proxy record
2324 * @param int $courseid Id of course (null if system settings)
2325 * @param int $instanceid Id of course module (null if system or context settings)
2327 function lti_set_tool_settings($settings, $toolproxyid, $courseid = null, $instanceid = null) {
2328 global $DB;
2330 $json = json_encode($settings);
2331 $record = $DB->get_record('lti_tool_settings', array('toolproxyid' => $toolproxyid,
2332 'course' => $courseid, 'coursemoduleid' => $instanceid));
2333 if ($record !== false) {
2334 $DB->update_record('lti_tool_settings', (object)array('id' => $record->id, 'settings' => $json, 'timemodified' => time()));
2335 } else {
2336 $record = new \stdClass();
2337 $record->toolproxyid = $toolproxyid;
2338 $record->course = $courseid;
2339 $record->coursemoduleid = $instanceid;
2340 $record->settings = $json;
2341 $record->timecreated = time();
2342 $record->timemodified = $record->timecreated;
2343 $DB->insert_record('lti_tool_settings', $record);
2348 * Signs the petition to launch the external tool using OAuth
2350 * @param array $oldparms Parameters to be passed for signing
2351 * @param string $endpoint url of the external tool
2352 * @param string $method Method for sending the parameters (e.g. POST)
2353 * @param string $oauthconsumerkey
2354 * @param string $oauthconsumersecret
2355 * @return array|null
2357 function lti_sign_parameters($oldparms, $endpoint, $method, $oauthconsumerkey, $oauthconsumersecret) {
2359 $parms = $oldparms;
2361 $testtoken = '';
2363 // TODO: Switch to core oauthlib once implemented - MDL-30149.
2364 $hmacmethod = new lti\OAuthSignatureMethod_HMAC_SHA1();
2365 $testconsumer = new lti\OAuthConsumer($oauthconsumerkey, $oauthconsumersecret, null);
2366 $accreq = lti\OAuthRequest::from_consumer_and_token($testconsumer, $testtoken, $method, $endpoint, $parms);
2367 $accreq->sign_request($hmacmethod, $testconsumer, $testtoken);
2369 $newparms = $accreq->get_parameters();
2371 return $newparms;
2375 * Posts the launch petition HTML
2377 * @param array $newparms Signed parameters
2378 * @param string $endpoint URL of the external tool
2379 * @param bool $debug Debug (true/false)
2380 * @return string
2382 function lti_post_launch_html($newparms, $endpoint, $debug=false) {
2383 $r = "<form action=\"" . $endpoint .
2384 "\" name=\"ltiLaunchForm\" id=\"ltiLaunchForm\" method=\"post\" encType=\"application/x-www-form-urlencoded\">\n";
2386 // Contruct html for the launch parameters.
2387 foreach ($newparms as $key => $value) {
2388 $key = htmlspecialchars($key);
2389 $value = htmlspecialchars($value);
2390 if ( $key == "ext_submit" ) {
2391 $r .= "<input type=\"submit\"";
2392 } else {
2393 $r .= "<input type=\"hidden\" name=\"{$key}\"";
2395 $r .= " value=\"";
2396 $r .= $value;
2397 $r .= "\"/>\n";
2400 if ( $debug ) {
2401 $r .= "<script language=\"javascript\"> \n";
2402 $r .= " //<![CDATA[ \n";
2403 $r .= "function basicltiDebugToggle() {\n";
2404 $r .= " var ele = document.getElementById(\"basicltiDebug\");\n";
2405 $r .= " if (ele.style.display == \"block\") {\n";
2406 $r .= " ele.style.display = \"none\";\n";
2407 $r .= " }\n";
2408 $r .= " else {\n";
2409 $r .= " ele.style.display = \"block\";\n";
2410 $r .= " }\n";
2411 $r .= "} \n";
2412 $r .= " //]]> \n";
2413 $r .= "</script>\n";
2414 $r .= "<a id=\"displayText\" href=\"javascript:basicltiDebugToggle();\">";
2415 $r .= get_string("toggle_debug_data", "lti")."</a>\n";
2416 $r .= "<div id=\"basicltiDebug\" style=\"display:none\">\n";
2417 $r .= "<b>".get_string("basiclti_endpoint", "lti")."</b><br/>\n";
2418 $r .= $endpoint . "<br/>\n&nbsp;<br/>\n";
2419 $r .= "<b>".get_string("basiclti_parameters", "lti")."</b><br/>\n";
2420 foreach ($newparms as $key => $value) {
2421 $key = htmlspecialchars($key);
2422 $value = htmlspecialchars($value);
2423 $r .= "$key = $value<br/>\n";
2425 $r .= "&nbsp;<br/>\n";
2426 $r .= "</div>\n";
2428 $r .= "</form>\n";
2430 if ( ! $debug ) {
2431 $r .= " <script type=\"text/javascript\"> \n" .
2432 " //<![CDATA[ \n" .
2433 " document.ltiLaunchForm.submit(); \n" .
2434 " //]]> \n" .
2435 " </script> \n";
2437 return $r;
2440 function lti_get_type($typeid) {
2441 global $DB;
2443 return $DB->get_record('lti_types', array('id' => $typeid));
2446 function lti_get_launch_container($lti, $toolconfig) {
2447 if (empty($lti->launchcontainer)) {
2448 $lti->launchcontainer = LTI_LAUNCH_CONTAINER_DEFAULT;
2451 if ($lti->launchcontainer == LTI_LAUNCH_CONTAINER_DEFAULT) {
2452 if (isset($toolconfig['launchcontainer'])) {
2453 $launchcontainer = $toolconfig['launchcontainer'];
2455 } else {
2456 $launchcontainer = $lti->launchcontainer;
2459 if (empty($launchcontainer) || $launchcontainer == LTI_LAUNCH_CONTAINER_DEFAULT) {
2460 $launchcontainer = LTI_LAUNCH_CONTAINER_EMBED_NO_BLOCKS;
2463 $devicetype = core_useragent::get_device_type();
2465 // Scrolling within the object element doesn't work on iOS or Android
2466 // Opening the popup window also had some issues in testing
2467 // For mobile devices, always take up the entire screen to ensure the best experience.
2468 if ($devicetype === core_useragent::DEVICETYPE_MOBILE || $devicetype === core_useragent::DEVICETYPE_TABLET ) {
2469 $launchcontainer = LTI_LAUNCH_CONTAINER_REPLACE_MOODLE_WINDOW;
2472 return $launchcontainer;
2475 function lti_request_is_using_ssl() {
2476 global $CFG;
2477 return (stripos($CFG->wwwroot, 'https://') === 0);
2480 function lti_ensure_url_is_https($url) {
2481 if (!strstr($url, '://')) {
2482 $url = 'https://' . $url;
2483 } else {
2484 // If the URL starts with http, replace with https.
2485 if (stripos($url, 'http://') === 0) {
2486 $url = 'https://' . substr($url, 7);
2490 return $url;
2494 * Determines if we should try to log the request
2496 * @param string $rawbody
2497 * @return bool
2499 function lti_should_log_request($rawbody) {
2500 global $CFG;
2502 if (empty($CFG->mod_lti_log_users)) {
2503 return false;
2506 $logusers = explode(',', $CFG->mod_lti_log_users);
2507 if (empty($logusers)) {
2508 return false;
2511 try {
2512 $xml = new \SimpleXMLElement($rawbody);
2513 $ns = $xml->getNamespaces();
2514 $ns = array_shift($ns);
2515 $xml->registerXPathNamespace('lti', $ns);
2516 $requestuserid = '';
2517 if ($node = $xml->xpath('//lti:userId')) {
2518 $node = $node[0];
2519 $requestuserid = clean_param((string) $node, PARAM_INT);
2520 } else if ($node = $xml->xpath('//lti:sourcedId')) {
2521 $node = $node[0];
2522 $resultjson = json_decode((string) $node);
2523 $requestuserid = clean_param($resultjson->data->userid, PARAM_INT);
2525 } catch (Exception $e) {
2526 return false;
2529 if (empty($requestuserid) or !in_array($requestuserid, $logusers)) {
2530 return false;
2533 return true;
2537 * Logs the request to a file in temp dir.
2539 * @param string $rawbody
2541 function lti_log_request($rawbody) {
2542 if ($tempdir = make_temp_directory('mod_lti', false)) {
2543 if ($tempfile = tempnam($tempdir, 'mod_lti_request'.date('YmdHis'))) {
2544 $content = "Request Headers:\n";
2545 foreach (moodle\mod\lti\OAuthUtil::get_headers() as $header => $value) {
2546 $content .= "$header: $value\n";
2548 $content .= "Request Body:\n";
2549 $content .= $rawbody;
2551 file_put_contents($tempfile, $content);
2552 chmod($tempfile, 0644);
2558 * Log an LTI response.
2560 * @param string $responsexml The response XML
2561 * @param Exception $e If there was an exception, pass that too
2563 function lti_log_response($responsexml, $e = null) {
2564 if ($tempdir = make_temp_directory('mod_lti', false)) {
2565 if ($tempfile = tempnam($tempdir, 'mod_lti_response'.date('YmdHis'))) {
2566 $content = '';
2567 if ($e instanceof Exception) {
2568 $info = get_exception_info($e);
2570 $content .= "Exception:\n";
2571 $content .= "Message: $info->message\n";
2572 $content .= "Debug info: $info->debuginfo\n";
2573 $content .= "Backtrace:\n";
2574 $content .= format_backtrace($info->backtrace, true);
2575 $content .= "\n";
2577 $content .= "Response XML:\n";
2578 $content .= $responsexml;
2580 file_put_contents($tempfile, $content);
2581 chmod($tempfile, 0644);
2587 * Fetches LTI type configuration for an LTI instance
2589 * @param stdClass $instance
2590 * @return array Can be empty if no type is found
2592 function lti_get_type_config_by_instance($instance) {
2593 $typeid = null;
2594 if (empty($instance->typeid)) {
2595 $tool = lti_get_tool_by_url_match($instance->toolurl, $instance->course);
2596 if ($tool) {
2597 $typeid = $tool->id;
2599 } else {
2600 $typeid = $instance->typeid;
2602 if (!empty($typeid)) {
2603 return lti_get_type_config($typeid);
2605 return array();
2609 * Enforce type config settings onto the LTI instance
2611 * @param stdClass $instance
2612 * @param array $typeconfig
2614 function lti_force_type_config_settings($instance, array $typeconfig) {
2615 $forced = array(
2616 'instructorchoicesendname' => 'sendname',
2617 'instructorchoicesendemailaddr' => 'sendemailaddr',
2618 'instructorchoiceacceptgrades' => 'acceptgrades',
2621 foreach ($forced as $instanceparam => $typeconfigparam) {
2622 if (array_key_exists($typeconfigparam, $typeconfig) && $typeconfig[$typeconfigparam] != LTI_SETTING_DELEGATE) {
2623 $instance->$instanceparam = $typeconfig[$typeconfigparam];
2629 * Initializes an array with the capabilities supported by the LTI module
2631 * @return array List of capability names (without a dollar sign prefix)
2633 function lti_get_capabilities() {
2635 $capabilities = array(
2636 'basic-lti-launch-request' => '',
2637 'ContentItemSelectionRequest' => '',
2638 'ToolProxyRegistrationRequest' => '',
2639 'Context.id' => 'context_id',
2640 'Context.title' => 'context_title',
2641 'Context.label' => 'context_label',
2642 'Context.sourcedId' => 'lis_course_section_sourcedid',
2643 'Context.longDescription' => '$COURSE->summary',
2644 'Context.timeFrame.begin' => '$COURSE->startdate',
2645 'CourseSection.title' => 'context_title',
2646 'CourseSection.label' => 'context_label',
2647 'CourseSection.sourcedId' => 'lis_course_section_sourcedid',
2648 'CourseSection.longDescription' => '$COURSE->summary',
2649 'CourseSection.timeFrame.begin' => '$COURSE->startdate',
2650 'ResourceLink.id' => 'resource_link_id',
2651 'ResourceLink.title' => 'resource_link_title',
2652 'ResourceLink.description' => 'resource_link_description',
2653 'User.id' => 'user_id',
2654 'User.username' => '$USER->username',
2655 'Person.name.full' => 'lis_person_name_full',
2656 'Person.name.given' => 'lis_person_name_given',
2657 'Person.name.family' => 'lis_person_name_family',
2658 'Person.email.primary' => 'lis_person_contact_email_primary',
2659 'Person.sourcedId' => 'lis_person_sourcedid',
2660 'Person.name.middle' => '$USER->middlename',
2661 'Person.address.street1' => '$USER->address',
2662 'Person.address.locality' => '$USER->city',
2663 'Person.address.country' => '$USER->country',
2664 'Person.address.timezone' => '$USER->timezone',
2665 'Person.phone.primary' => '$USER->phone1',
2666 'Person.phone.mobile' => '$USER->phone2',
2667 'Person.webaddress' => '$USER->url',
2668 'Membership.role' => 'roles',
2669 'Result.sourcedId' => 'lis_result_sourcedid',
2670 'Result.autocreate' => 'lis_outcome_service_url',
2671 'Moodle.Person.userGroupIds' => null);
2673 return $capabilities;
2678 * Initializes an array with the services supported by the LTI module
2680 * @return array List of services
2682 function lti_get_services() {
2684 $services = array();
2685 $definedservices = core_component::get_plugin_list('ltiservice');
2686 foreach ($definedservices as $name => $location) {
2687 $classname = "\\ltiservice_{$name}\\local\\service\\{$name}";
2688 $services[] = new $classname();
2691 return $services;
2696 * Initializes an instance of the named service
2698 * @param string $servicename Name of service
2700 * @return bool|\mod_lti\local\ltiservice\service_base Service
2702 function lti_get_service_by_name($servicename) {
2704 $service = false;
2705 $classname = "\\ltiservice_{$servicename}\\local\\service\\{$servicename}";
2706 if (class_exists($classname)) {
2707 $service = new $classname();
2710 return $service;
2715 * Finds a service by id
2717 * @param \mod_lti\local\ltiservice\service_base[] $services Array of services
2718 * @param string $resourceid ID of resource
2720 * @return mod_lti\local\ltiservice\service_base Service
2722 function lti_get_service_by_resource_id($services, $resourceid) {
2724 $service = false;
2725 foreach ($services as $aservice) {
2726 foreach ($aservice->get_resources() as $resource) {
2727 if ($resource->get_id() === $resourceid) {
2728 $service = $aservice;
2729 break 2;
2734 return $service;
2739 * Extracts the named contexts from a tool proxy
2741 * @param object $json
2743 * @return array Contexts
2745 function lti_get_contexts($json) {
2747 $contexts = array();
2748 if (isset($json->{'@context'})) {
2749 foreach ($json->{'@context'} as $context) {
2750 if (is_object($context)) {
2751 $contexts = array_merge(get_object_vars($context), $contexts);
2756 return $contexts;
2761 * Converts an ID to a fully-qualified ID
2763 * @param array $contexts
2764 * @param string $id
2766 * @return string Fully-qualified ID
2768 function lti_get_fqid($contexts, $id) {
2770 $parts = explode(':', $id, 2);
2771 if (count($parts) > 1) {
2772 if (array_key_exists($parts[0], $contexts)) {
2773 $id = $contexts[$parts[0]] . $parts[1];
2777 return $id;
2782 * Returns the icon for the given tool type
2784 * @param stdClass $type The tool type
2786 * @return string The url to the tool type's corresponding icon
2788 function get_tool_type_icon_url(stdClass $type) {
2789 global $OUTPUT;
2791 $iconurl = $type->secureicon;
2793 if (empty($iconurl)) {
2794 $iconurl = $type->icon;
2797 if (empty($iconurl)) {
2798 $iconurl = $OUTPUT->image_url('icon', 'lti')->out();
2801 return $iconurl;
2805 * Returns the edit url for the given tool type
2807 * @param stdClass $type The tool type
2809 * @return string The url to edit the tool type
2811 function get_tool_type_edit_url(stdClass $type) {
2812 $url = new moodle_url('/mod/lti/typessettings.php',
2813 array('action' => 'update', 'id' => $type->id, 'sesskey' => sesskey(), 'returnto' => 'toolconfigure'));
2814 return $url->out();
2818 * Returns the edit url for the given tool proxy.
2820 * @param stdClass $proxy The tool proxy
2822 * @return string The url to edit the tool type
2824 function get_tool_proxy_edit_url(stdClass $proxy) {
2825 $url = new moodle_url('/mod/lti/registersettings.php',
2826 array('action' => 'update', 'id' => $proxy->id, 'sesskey' => sesskey(), 'returnto' => 'toolconfigure'));
2827 return $url->out();
2831 * Returns the course url for the given tool type
2833 * @param stdClass $type The tool type
2835 * @return string The url to the course of the tool type, void if it is a site wide type
2837 function get_tool_type_course_url(stdClass $type) {
2838 if ($type->course != 1) {
2839 $url = new moodle_url('/course/view.php', array('id' => $type->course));
2840 return $url->out();
2842 return null;
2846 * Returns the icon and edit urls for the tool type and the course url if it is a course type.
2848 * @param stdClass $type The tool type
2850 * @return array The urls of the tool type
2852 function get_tool_type_urls(stdClass $type) {
2853 $courseurl = get_tool_type_course_url($type);
2855 $urls = array(
2856 'icon' => get_tool_type_icon_url($type),
2857 'edit' => get_tool_type_edit_url($type),
2860 if ($courseurl) {
2861 $urls['course'] = $courseurl;
2864 return $urls;
2868 * Returns the icon and edit urls for the tool proxy.
2870 * @param stdClass $proxy The tool proxy
2872 * @return array The urls of the tool proxy
2874 function get_tool_proxy_urls(stdClass $proxy) {
2875 global $OUTPUT;
2877 $urls = array(
2878 'icon' => $OUTPUT->image_url('icon', 'lti')->out(),
2879 'edit' => get_tool_proxy_edit_url($proxy),
2882 return $urls;
2886 * Returns information on the current state of the tool type
2888 * @param stdClass $type The tool type
2890 * @return array An array with a text description of the state, and boolean for whether it is in each state:
2891 * pending, configured, rejected, unknown
2893 function get_tool_type_state_info(stdClass $type) {
2894 $isconfigured = false;
2895 $ispending = false;
2896 $isrejected = false;
2897 $isunknown = false;
2898 switch ($type->state) {
2899 case LTI_TOOL_STATE_CONFIGURED:
2900 $state = get_string('active', 'mod_lti');
2901 $isconfigured = true;
2902 break;
2903 case LTI_TOOL_STATE_PENDING:
2904 $state = get_string('pending', 'mod_lti');
2905 $ispending = true;
2906 break;
2907 case LTI_TOOL_STATE_REJECTED:
2908 $state = get_string('rejected', 'mod_lti');
2909 $isrejected = true;
2910 break;
2911 default:
2912 $state = get_string('unknownstate', 'mod_lti');
2913 $isunknown = true;
2914 break;
2917 return array(
2918 'text' => $state,
2919 'pending' => $ispending,
2920 'configured' => $isconfigured,
2921 'rejected' => $isrejected,
2922 'unknown' => $isunknown
2927 * Returns a summary of each LTI capability this tool type requires in plain language
2929 * @param stdClass $type The tool type
2931 * @return array An array of text descriptions of each of the capabilities this tool type requires
2933 function get_tool_type_capability_groups($type) {
2934 $capabilities = lti_get_enabled_capabilities($type);
2935 $groups = array();
2936 $hascourse = false;
2937 $hasactivities = false;
2938 $hasuseraccount = false;
2939 $hasuserpersonal = false;
2941 foreach ($capabilities as $capability) {
2942 // Bail out early if we've already found all groups.
2943 if (count($groups) >= 4) {
2944 continue;
2947 if (!$hascourse && preg_match('/^CourseSection/', $capability)) {
2948 $hascourse = true;
2949 $groups[] = get_string('courseinformation', 'mod_lti');
2950 } else if (!$hasactivities && preg_match('/^ResourceLink/', $capability)) {
2951 $hasactivities = true;
2952 $groups[] = get_string('courseactivitiesorresources', 'mod_lti');
2953 } else if (!$hasuseraccount && preg_match('/^User/', $capability) || preg_match('/^Membership/', $capability)) {
2954 $hasuseraccount = true;
2955 $groups[] = get_string('useraccountinformation', 'mod_lti');
2956 } else if (!$hasuserpersonal && preg_match('/^Person/', $capability)) {
2957 $hasuserpersonal = true;
2958 $groups[] = get_string('userpersonalinformation', 'mod_lti');
2962 return $groups;
2967 * Returns the ids of each instance of this tool type
2969 * @param stdClass $type The tool type
2971 * @return array An array of ids of the instances of this tool type
2973 function get_tool_type_instance_ids($type) {
2974 global $DB;
2976 return array_keys($DB->get_fieldset_select('lti', 'id', 'typeid = ?', array($type->id)));
2980 * Serialises this tool type
2982 * @param stdClass $type The tool type
2984 * @return array An array of values representing this type
2986 function serialise_tool_type(stdClass $type) {
2987 $capabilitygroups = get_tool_type_capability_groups($type);
2988 $instanceids = get_tool_type_instance_ids($type);
2989 // Clean the name. We don't want tags here.
2990 $name = clean_param($type->name, PARAM_NOTAGS);
2991 if (!empty($type->description)) {
2992 // Clean the description. We don't want tags here.
2993 $description = clean_param($type->description, PARAM_NOTAGS);
2994 } else {
2995 $description = get_string('editdescription', 'mod_lti');
2997 return array(
2998 'id' => $type->id,
2999 'name' => $name,
3000 'description' => $description,
3001 'urls' => get_tool_type_urls($type),
3002 'state' => get_tool_type_state_info($type),
3003 'hascapabilitygroups' => !empty($capabilitygroups),
3004 'capabilitygroups' => $capabilitygroups,
3005 // Course ID of 1 means it's not linked to a course.
3006 'courseid' => $type->course == 1 ? 0 : $type->course,
3007 'instanceids' => $instanceids,
3008 'instancecount' => count($instanceids)
3013 * Serialises this tool proxy.
3015 * @param stdClass $proxy The tool proxy
3017 * @return array An array of values representing this type
3019 function serialise_tool_proxy(stdClass $proxy) {
3020 return array(
3021 'id' => $proxy->id,
3022 'name' => $proxy->name,
3023 'description' => get_string('activatetoadddescription', 'mod_lti'),
3024 'urls' => get_tool_proxy_urls($proxy),
3025 'state' => array(
3026 'text' => get_string('pending', 'mod_lti'),
3027 'pending' => true,
3028 'configured' => false,
3029 'rejected' => false,
3030 'unknown' => false
3032 'hascapabilitygroups' => true,
3033 'capabilitygroups' => array(),
3034 'courseid' => 0,
3035 'instanceids' => array(),
3036 'instancecount' => 0
3041 * Loads the cartridge information into the tool type, if the launch url is for a cartridge file
3043 * @param stdClass $type The tool type object to be filled in
3044 * @since Moodle 3.1
3046 function lti_load_type_if_cartridge($type) {
3047 if (!empty($type->lti_toolurl) && lti_is_cartridge($type->lti_toolurl)) {
3048 lti_load_type_from_cartridge($type->lti_toolurl, $type);
3053 * Loads the cartridge information into the new tool, if the launch url is for a cartridge file
3055 * @param stdClass $lti The tools config
3056 * @since Moodle 3.1
3058 function lti_load_tool_if_cartridge($lti) {
3059 if (!empty($lti->toolurl) && lti_is_cartridge($lti->toolurl)) {
3060 lti_load_tool_from_cartridge($lti->toolurl, $lti);
3065 * Determines if the given url is for a IMS basic cartridge
3067 * @param string $url The url to be checked
3068 * @return True if the url is for a cartridge
3069 * @since Moodle 3.1
3071 function lti_is_cartridge($url) {
3072 // If it is empty, it's not a cartridge.
3073 if (empty($url)) {
3074 return false;
3076 // If it has xml at the end of the url, it's a cartridge.
3077 if (preg_match('/\.xml$/', $url)) {
3078 return true;
3080 // Even if it doesn't have .xml, load the url to check if it's a cartridge..
3081 try {
3082 $toolinfo = lti_load_cartridge($url,
3083 array(
3084 "launch_url" => "launchurl"
3087 if (!empty($toolinfo['launchurl'])) {
3088 return true;
3090 } catch (moodle_exception $e) {
3091 return false; // Error loading the xml, so it's not a cartridge.
3093 return false;
3097 * Allows you to load settings for an external tool type from an IMS cartridge.
3099 * @param string $url The URL to the cartridge
3100 * @param stdClass $type The tool type object to be filled in
3101 * @throws moodle_exception if the cartridge could not be loaded correctly
3102 * @since Moodle 3.1
3104 function lti_load_type_from_cartridge($url, $type) {
3105 $toolinfo = lti_load_cartridge($url,
3106 array(
3107 "title" => "lti_typename",
3108 "launch_url" => "lti_toolurl",
3109 "description" => "lti_description",
3110 "icon" => "lti_icon",
3111 "secure_icon" => "lti_secureicon"
3113 array(
3114 "icon_url" => "lti_extension_icon",
3115 "secure_icon_url" => "lti_extension_secureicon"
3118 // If an activity name exists, unset the cartridge name so we don't override it.
3119 if (isset($type->lti_typename)) {
3120 unset($toolinfo['lti_typename']);
3123 // Always prefer cartridge core icons first, then, if none are found, look at the extension icons.
3124 if (empty($toolinfo['lti_icon']) && !empty($toolinfo['lti_extension_icon'])) {
3125 $toolinfo['lti_icon'] = $toolinfo['lti_extension_icon'];
3127 unset($toolinfo['lti_extension_icon']);
3129 if (empty($toolinfo['lti_secureicon']) && !empty($toolinfo['lti_extension_secureicon'])) {
3130 $toolinfo['lti_secureicon'] = $toolinfo['lti_extension_secureicon'];
3132 unset($toolinfo['lti_extension_secureicon']);
3134 // Ensure Custom icons aren't overridden by cartridge params.
3135 if (!empty($type->lti_icon)) {
3136 unset($toolinfo['lti_icon']);
3139 if (!empty($type->lti_secureicon)) {
3140 unset($toolinfo['lti_secureicon']);
3143 foreach ($toolinfo as $property => $value) {
3144 $type->$property = $value;
3149 * Allows you to load in the configuration for an external tool from an IMS cartridge.
3151 * @param string $url The URL to the cartridge
3152 * @param stdClass $lti LTI object
3153 * @throws moodle_exception if the cartridge could not be loaded correctly
3154 * @since Moodle 3.1
3156 function lti_load_tool_from_cartridge($url, $lti) {
3157 $toolinfo = lti_load_cartridge($url,
3158 array(
3159 "title" => "name",
3160 "launch_url" => "toolurl",
3161 "secure_launch_url" => "securetoolurl",
3162 "description" => "intro",
3163 "icon" => "icon",
3164 "secure_icon" => "secureicon"
3166 array(
3167 "icon_url" => "extension_icon",
3168 "secure_icon_url" => "extension_secureicon"
3171 // If an activity name exists, unset the cartridge name so we don't override it.
3172 if (isset($lti->name)) {
3173 unset($toolinfo['name']);
3176 // Always prefer cartridge core icons first, then, if none are found, look at the extension icons.
3177 if (empty($toolinfo['icon']) && !empty($toolinfo['extension_icon'])) {
3178 $toolinfo['icon'] = $toolinfo['extension_icon'];
3180 unset($toolinfo['extension_icon']);
3182 if (empty($toolinfo['secureicon']) && !empty($toolinfo['extension_secureicon'])) {
3183 $toolinfo['secureicon'] = $toolinfo['extension_secureicon'];
3185 unset($toolinfo['extension_secureicon']);
3187 foreach ($toolinfo as $property => $value) {
3188 $lti->$property = $value;
3193 * Search for a tag within an XML DOMDocument
3195 * @param string $url The url of the cartridge to be loaded
3196 * @param array $map The map of tags to keys in the return array
3197 * @param array $propertiesmap The map of properties to keys in the return array
3198 * @return array An associative array with the given keys and their values from the cartridge
3199 * @throws moodle_exception if the cartridge could not be loaded correctly
3200 * @since Moodle 3.1
3202 function lti_load_cartridge($url, $map, $propertiesmap = array()) {
3203 global $CFG;
3204 require_once($CFG->libdir. "/filelib.php");
3206 $curl = new curl();
3207 $response = $curl->get($url);
3209 // TODO MDL-46023 Replace this code with a call to the new library.
3210 $origerrors = libxml_use_internal_errors(true);
3211 $origentity = libxml_disable_entity_loader(true);
3212 libxml_clear_errors();
3214 $document = new DOMDocument();
3215 @$document->loadXML($response, LIBXML_DTDLOAD | LIBXML_DTDATTR);
3217 $cartridge = new DomXpath($document);
3219 $errors = libxml_get_errors();
3221 libxml_clear_errors();
3222 libxml_use_internal_errors($origerrors);
3223 libxml_disable_entity_loader($origentity);
3225 if (count($errors) > 0) {
3226 $message = 'Failed to load cartridge.';
3227 foreach ($errors as $error) {
3228 $message .= "\n" . trim($error->message, "\n\r\t .") . " at line " . $error->line;
3230 throw new moodle_exception('errorreadingfile', '', '', $url, $message);
3233 $toolinfo = array();
3234 foreach ($map as $tag => $key) {
3235 $value = get_tag($tag, $cartridge);
3236 if ($value) {
3237 $toolinfo[$key] = $value;
3240 if (!empty($propertiesmap)) {
3241 foreach ($propertiesmap as $property => $key) {
3242 $value = get_tag("property", $cartridge, $property);
3243 if ($value) {
3244 $toolinfo[$key] = $value;
3249 return $toolinfo;
3253 * Search for a tag within an XML DOMDocument
3255 * @param stdClass $tagname The name of the tag to search for
3256 * @param XPath $xpath The XML to find the tag in
3257 * @param XPath $attribute The attribute to search for (if we should search for a child node with the given
3258 * value for the name attribute
3259 * @since Moodle 3.1
3261 function get_tag($tagname, $xpath, $attribute = null) {
3262 if ($attribute) {
3263 $result = $xpath->query('//*[local-name() = \'' . $tagname . '\'][@name="' . $attribute . '"]');
3264 } else {
3265 $result = $xpath->query('//*[local-name() = \'' . $tagname . '\']');
3267 if ($result->length > 0) {
3268 return $result->item(0)->nodeValue;
3270 return null;