MDL-45221 Allow unit tests to pass under any OS
[moodle.git] / mod / lti / locallib.php
CommitLineData
b9b2e7bb 1<?php
61eb12d4
CS
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.
13//
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/>.
16//
b9b2e7bb
CS
17// This file is part of BasicLTI4Moodle
18//
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.
30//
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
b9b2e7bb
CS
34
35/**
61eb12d4 36 * This file contains the library of functions and constants for the lti module
b9b2e7bb 37 *
2b17ec3d 38 * @package mod_lti
61eb12d4 39 * @copyright 2009 Marc Alier, Jordi Piguillem, Nikolas Galanis
b9b2e7bb 40 * marc.alier@upc.edu
61eb12d4
CS
41 * @copyright 2009 Universitat Politecnica de Catalunya http://www.upc.edu
42 * @author Marc Alier
43 * @author Jordi Piguillem
44 * @author Nikolas Galanis
8f45215d 45 * @author Chris Scribner
61eb12d4 46 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
b9b2e7bb
CS
47 */
48
49defined('MOODLE_INTERNAL') || die;
50
fabd4fcf 51// TODO: Switch to core oauthlib once implemented - MDL-30149
795dff01
CS
52use moodle\mod\lti as lti;
53
b9b2e7bb
CS
54require_once($CFG->dirroot.'/mod/lti/OAuth.php');
55
56define('LTI_URL_DOMAIN_REGEX', '/(?:https?:\/\/)?(?:www\.)?([^\/]+)(?:\/|$)/i');
57
58define('LTI_LAUNCH_CONTAINER_DEFAULT', 1);
59define('LTI_LAUNCH_CONTAINER_EMBED', 2);
60define('LTI_LAUNCH_CONTAINER_EMBED_NO_BLOCKS', 3);
61define('LTI_LAUNCH_CONTAINER_WINDOW', 4);
cca9d3f7 62define('LTI_LAUNCH_CONTAINER_REPLACE_MOODLE_WINDOW', 5);
b9b2e7bb
CS
63
64define('LTI_TOOL_STATE_ANY', 0);
65define('LTI_TOOL_STATE_CONFIGURED', 1);
66define('LTI_TOOL_STATE_PENDING', 2);
67define('LTI_TOOL_STATE_REJECTED', 3);
68
69define('LTI_SETTING_NEVER', 0);
70define('LTI_SETTING_ALWAYS', 1);
5e078d62 71define('LTI_SETTING_DELEGATE', 2);
b9b2e7bb
CS
72
73/**
74 * Prints a Basic LTI activity
75 *
76 * $param int $basicltiid Basic LTI activity id
77 */
67ddf847 78function lti_view($instance) {
b9b2e7bb
CS
79 global $PAGE, $CFG;
80
ea04a9f9 81 if (empty($instance->typeid)) {
606ab1a1 82 $tool = lti_get_tool_by_url_match($instance->toolurl, $instance->course);
ea04a9f9 83 if ($tool) {
b9b2e7bb
CS
84 $typeid = $tool->id;
85 } else {
86 $typeid = null;
87 }
88 } else {
89 $typeid = $instance->typeid;
90 }
e27cb316 91
ea04a9f9 92 if ($typeid) {
b9b2e7bb
CS
93 $typeconfig = lti_get_type_config($typeid);
94 } else {
95 //There is no admin configuration for this tool. Use configuration in the lti instance record plus some defaults.
96 $typeconfig = (array)$instance;
e27cb316 97
b9b2e7bb
CS
98 $typeconfig['sendname'] = $instance->instructorchoicesendname;
99 $typeconfig['sendemailaddr'] = $instance->instructorchoicesendemailaddr;
100 $typeconfig['customparameters'] = $instance->instructorcustomparameters;
f4f711d7
CS
101 $typeconfig['acceptgrades'] = $instance->instructorchoiceacceptgrades;
102 $typeconfig['allowroster'] = $instance->instructorchoiceallowroster;
d8d04121 103 $typeconfig['forcessl'] = '0';
b9b2e7bb 104 }
e27cb316 105
b9b2e7bb 106 //Default the organizationid if not specified
ea04a9f9 107 if (empty($typeconfig['organizationid'])) {
b9b2e7bb 108 $urlparts = parse_url($CFG->wwwroot);
e27cb316 109
b9b2e7bb
CS
110 $typeconfig['organizationid'] = $urlparts['host'];
111 }
e27cb316 112
ea04a9f9 113 if (!empty($instance->resourcekey)) {
3dd9ca24 114 $key = $instance->resourcekey;
ea04a9f9 115 } else if (!empty($typeconfig['resourcekey'])) {
3dd9ca24
CS
116 $key = $typeconfig['resourcekey'];
117 } else {
118 $key = '';
119 }
120
ea04a9f9 121 if (!empty($instance->password)) {
3dd9ca24 122 $secret = $instance->password;
ea04a9f9 123 } else if (!empty($typeconfig['password'])) {
3dd9ca24
CS
124 $secret = $typeconfig['password'];
125 } else {
126 $secret = '';
127 }
e27cb316 128
b9b2e7bb 129 $endpoint = !empty($instance->toolurl) ? $instance->toolurl : $typeconfig['toolurl'];
d8d04121 130 $endpoint = trim($endpoint);
e27cb316 131
d8d04121 132 //If the current request is using SSL and a secure tool URL is specified, use it
ea04a9f9 133 if (lti_request_is_using_ssl() && !empty($instance->securetoolurl)) {
d8d04121
CS
134 $endpoint = trim($instance->securetoolurl);
135 }
e27cb316 136
d8d04121 137 //If SSL is forced, use the secure tool url if specified. Otherwise, make sure https is on the normal launch URL.
ea04a9f9
EL
138 if ($typeconfig['forcessl'] == '1') {
139 if (!empty($instance->securetoolurl)) {
d8d04121
CS
140 $endpoint = trim($instance->securetoolurl);
141 }
e27cb316 142
d8d04121
CS
143 $endpoint = lti_ensure_url_is_https($endpoint);
144 } else {
ea04a9f9 145 if (!strstr($endpoint, '://')) {
d8d04121
CS
146 $endpoint = 'http://' . $endpoint;
147 }
f17f4959 148 }
e27cb316 149
d8d04121
CS
150 $orgid = $typeconfig['organizationid'];
151
b9b2e7bb
CS
152 $course = $PAGE->course;
153 $requestparams = lti_build_request($instance, $typeconfig, $course);
154
3dd9ca24 155 $launchcontainer = lti_get_launch_container($instance, $typeconfig);
c4d80efe 156 $returnurlparams = array('course' => $course->id, 'launch_container' => $launchcontainer, 'instanceid' => $instance->id);
e27cb316 157
3dd9ca24
CS
158 if ( $orgid ) {
159 $requestparams["tool_consumer_instance_guid"] = $orgid;
160 }
e27cb316 161
ea04a9f9 162 if (empty($key) || empty($secret)) {
9d57ad17 163 $returnurlparams['unsigned'] = '1';
1706e83b 164 }
6d6bd5f1
EL
165
166 // Add the return URL. We send the launch container along to help us avoid frames-within-frames when the user returns.
1706e83b
CS
167 $url = new moodle_url('/mod/lti/return.php', $returnurlparams);
168 $returnurl = $url->out(false);
e27cb316 169
1706e83b
CS
170 if ($typeconfig['forcessl'] == '1') {
171 $returnurl = lti_ensure_url_is_https($returnurl);
9d57ad17 172 }
6d6bd5f1 173
1706e83b 174 $requestparams['launch_presentation_return_url'] = $returnurl;
e27cb316 175
ea04a9f9 176 if (!empty($key) && !empty($secret)) {
3dd9ca24 177 $parms = lti_sign_parameters($requestparams, $endpoint, "POST", $key, $secret);
533c42ea
MN
178
179 $endpointurl = new moodle_url($endpoint);
180 $endpointparams = $endpointurl->params();
181
182 // Strip querystring params in endpoint url from $parms to avoid duplication.
183 if (!empty($endpointparams) && !empty($parms)) {
184 foreach (array_keys($endpointparams) as $paramname) {
185 if (isset($parms[$paramname])) {
186 unset($parms[$paramname]);
187 }
188 }
189 }
190
3dd9ca24
CS
191 } else {
192 //If no key and secret, do the launch unsigned.
193 $parms = $requestparams;
3dd9ca24 194 }
e27cb316 195
b9b2e7bb 196 $debuglaunch = ( $instance->debuglaunch == 1 );
e27cb316 197
dbb0fec9 198 $content = lti_post_launch_html($parms, $endpoint, $debuglaunch);
e27cb316 199
b9b2e7bb
CS
200 echo $content;
201}
202
ea04a9f9 203function lti_build_sourcedid($instanceid, $userid, $launchid = null, $servicesalt) {
996b0fd9 204 $data = new stdClass();
e27cb316 205
996b0fd9
CS
206 $data->instanceid = $instanceid;
207 $data->userid = $userid;
ea04a9f9 208 if (!empty($launchid)) {
f4f711d7
CS
209 $data->launchid = $launchid;
210 } else {
211 $data->launchid = mt_rand();
212 }
996b0fd9
CS
213
214 $json = json_encode($data);
215
216 $hash = hash('sha256', $json . $servicesalt, false);
217
218 $container = new stdClass();
219 $container->data = $data;
220 $container->hash = $hash;
221
222 return $container;
223}
224
b9b2e7bb
CS
225/**
226 * This function builds the request that must be sent to the tool producer
227 *
228 * @param object $instance Basic LTI instance object
229 * @param object $typeconfig Basic LTI tool configuration
230 * @param object $course Course object
231 *
232 * @return array $request Request details
233 */
234function lti_build_request($instance, $typeconfig, $course) {
235 global $USER, $CFG;
236
ea04a9f9 237 if (empty($instance->cmid)) {
16cac566
CS
238 $instance->cmid = 0;
239 }
e27cb316 240
1d4f052e 241 $role = lti_get_ims_role($USER, $instance->cmid, $instance->course);
b9b2e7bb 242
b9b2e7bb 243 $requestparams = array(
34eb0501
CS
244 'resource_link_id' => $instance->id,
245 'resource_link_title' => $instance->name,
246 'resource_link_description' => $instance->intro,
247 'user_id' => $USER->id,
248 'roles' => $role,
249 'context_id' => $course->id,
250 'context_label' => $course->shortname,
251 'context_title' => $course->fullname,
2a457227 252 'launch_presentation_locale' => current_language()
b9b2e7bb
CS
253 );
254
b9b2e7bb 255 $placementsecret = $instance->servicesalt;
e27cb316 256
b9b2e7bb 257 if ( isset($placementsecret) ) {
f4f711d7 258 $sourcedid = json_encode(lti_build_sourcedid($instance->id, $USER->id, null, $placementsecret));
b9b2e7bb
CS
259 }
260
261 if ( isset($placementsecret) &&
5e078d62
CS
262 ( $typeconfig['acceptgrades'] == LTI_SETTING_ALWAYS ||
263 ( $typeconfig['acceptgrades'] == LTI_SETTING_DELEGATE && $instance->instructorchoiceacceptgrades == LTI_SETTING_ALWAYS ) ) ) {
34eb0501 264 $requestparams['lis_result_sourcedid'] = $sourcedid;
e27cb316 265
34eb0501
CS
266 //Add outcome service URL
267 $serviceurl = new moodle_url('/mod/lti/service.php');
268 $serviceurl = $serviceurl->out();
feeb5294 269
ea04a9f9 270 if ($typeconfig['forcessl'] == '1') {
d8d04121
CS
271 $serviceurl = lti_ensure_url_is_https($serviceurl);
272 }
feeb5294 273
34eb0501 274 $requestparams['lis_outcome_service_url'] = $serviceurl;
b9b2e7bb
CS
275 }
276
b9b2e7bb 277 // Send user's name and email data if appropriate
5e078d62
CS
278 if ( $typeconfig['sendname'] == LTI_SETTING_ALWAYS ||
279 ( $typeconfig['sendname'] == LTI_SETTING_DELEGATE && $instance->instructorchoicesendname == LTI_SETTING_ALWAYS ) ) {
34eb0501
CS
280 $requestparams['lis_person_name_given'] = $USER->firstname;
281 $requestparams['lis_person_name_family'] = $USER->lastname;
282 $requestparams['lis_person_name_full'] = $USER->firstname." ".$USER->lastname;
b9b2e7bb
CS
283 }
284
5e078d62
CS
285 if ( $typeconfig['sendemailaddr'] == LTI_SETTING_ALWAYS ||
286 ( $typeconfig['sendemailaddr'] == LTI_SETTING_DELEGATE && $instance->instructorchoicesendemailaddr == LTI_SETTING_ALWAYS ) ) {
34eb0501 287 $requestparams['lis_person_contact_email_primary'] = $USER->email;
b9b2e7bb
CS
288 }
289
290 // Concatenate the custom parameters from the administrator and the instructor
291 // Instructor parameters are only taken into consideration if the administrator
292 // has giver permission
293 $customstr = $typeconfig['customparameters'];
294 $instructorcustomstr = $instance->instructorcustomparameters;
295 $custom = array();
296 $instructorcustom = array();
297 if ($customstr) {
dbb0fec9 298 $custom = lti_split_custom_parameters($customstr);
b9b2e7bb 299 }
e91be4ff 300 if (isset($typeconfig['allowinstructorcustom']) && $typeconfig['allowinstructorcustom'] == LTI_SETTING_NEVER) {
b9b2e7bb
CS
301 $requestparams = array_merge($custom, $requestparams);
302 } else {
303 if ($instructorcustomstr) {
dbb0fec9 304 $instructorcustom = lti_split_custom_parameters($instructorcustomstr);
b9b2e7bb
CS
305 }
306 foreach ($instructorcustom as $key => $val) {
ea04a9f9
EL
307 // Ignore the instructor's parameter
308 if (!array_key_exists($key, $custom)) {
b9b2e7bb
CS
309 $custom[$key] = $val;
310 }
311 }
312 $requestparams = array_merge($custom, $requestparams);
313 }
314
3dd9ca24
CS
315 // Make sure we let the tool know what LMS they are being called from
316 $requestparams["ext_lms"] = "moodle-2";
34eb0501
CS
317 $requestparams['tool_consumer_info_product_family_code'] = 'moodle';
318 $requestparams['tool_consumer_info_version'] = strval($CFG->version);
3dd9ca24
CS
319
320 // Add oauth_callback to be compliant with the 1.0A spec
34eb0501 321 $requestparams['oauth_callback'] = 'about:blank';
3dd9ca24 322
558be8fc
CS
323 //The submit button needs to be part of the signature as it gets posted with the form.
324 //This needs to be here to support launching without javascript.
3dd9ca24 325 $submittext = get_string('press_to_submit', 'lti');
34eb0501 326 $requestparams['ext_submit'] = $submittext;
e27cb316 327
34eb0501
CS
328 $requestparams['lti_version'] = 'LTI-1p0';
329 $requestparams['lti_message_type'] = 'basic-lti-launch-request';
e27cb316 330
b9b2e7bb
CS
331 return $requestparams;
332}
333
ea04a9f9 334function lti_get_tool_table($tools, $id) {
99938034 335 global $CFG, $OUTPUT, $USER;
795dff01 336 $html = '';
e27cb316 337
795dff01
CS
338 $typename = get_string('typename', 'lti');
339 $baseurl = get_string('baseurl', 'lti');
340 $action = get_string('action', 'lti');
341 $createdon = get_string('createdon', 'lti');
e27cb316 342
ea04a9f9 343 if ($id == 'lti_configured') {
795dff01
CS
344 $html .= '<div><a style="margin-top:.25em" href="'.$CFG->wwwroot.'/mod/lti/typessettings.php?action=add&amp;sesskey='.$USER->sesskey.'">'.get_string('addtype', 'lti').'</a></div>';
345 }
e27cb316 346
795dff01 347 if (!empty($tools)) {
5de15b83
CS
348 $html .= "
349 <div id=\"{$id}_container\" style=\"margin-top:.5em;margin-bottom:.5em\">
350 <table id=\"{$id}_tools\">
795dff01
CS
351 <thead>
352 <tr>
353 <th>$typename</th>
354 <th>$baseurl</th>
355 <th>$createdon</th>
356 <th>$action</th>
357 </tr>
358 </thead>
5de15b83 359 ";
e27cb316 360
795dff01
CS
361 foreach ($tools as $type) {
362 $date = userdate($type->timecreated);
363 $accept = get_string('accept', 'lti');
364 $update = get_string('update', 'lti');
365 $delete = get_string('delete', 'lti');
e27cb316 366
baf41e4b
FM
367 $baseurl = new moodle_url('/mod/lti/typessettings.php', array(
368 'action' => 'accept',
369 'id' => $type->id,
370 'sesskey' => sesskey(),
371 'tab' => $id
372 ));
373
374 $accepthtml = $OUTPUT->action_icon($baseurl,
375 new pix_icon('t/check', $accept, '', array('class' => 'iconsmall')), null,
376 array('title' => $accept, 'class' => 'editing_accept'));
795dff01
CS
377
378 $deleteaction = 'delete';
e27cb316 379
ea04a9f9 380 if ($type->state == LTI_TOOL_STATE_CONFIGURED) {
795dff01
CS
381 $accepthtml = '';
382 }
e27cb316 383
ea04a9f9 384 if ($type->state != LTI_TOOL_STATE_REJECTED) {
795dff01
CS
385 $deleteaction = 'reject';
386 $delete = get_string('reject', 'lti');
387 }
e27cb316 388
baf41e4b
FM
389 $updateurl = clone($baseurl);
390 $updateurl->param('action', 'update');
391 $updatehtml = $OUTPUT->action_icon($updateurl,
73584712 392 new pix_icon('t/edit', $update, '', array('class' => 'iconsmall')), null,
baf41e4b
FM
393 array('title' => $update, 'class' => 'editing_update'));
394
395 $deleteurl = clone($baseurl);
396 $deleteurl->param('action', $deleteaction);
397 $deletehtml = $OUTPUT->action_icon($deleteurl,
73584712 398 new pix_icon('t/delete', $delete, '', array('class' => 'iconsmall')), null,
baf41e4b 399 array('title' => $delete, 'class' => 'editing_delete'));
5de15b83 400 $html .= "
795dff01
CS
401 <tr>
402 <td>
403 {$type->name}
404 </td>
405 <td>
406 {$type->baseurl}
407 </td>
408 <td>
409 {$date}
410 </td>
5de15b83 411 <td align=\"center\">
baf41e4b 412 {$accepthtml}{$updatehtml}{$deletehtml}
795dff01
CS
413 </td>
414 </tr>
5de15b83 415 ";
795dff01
CS
416 }
417 $html .= '</table></div>';
418 } else {
419 $html .= get_string('no_' . $id, 'lti');
420 }
e27cb316 421
795dff01
CS
422 return $html;
423}
424
b9b2e7bb
CS
425/**
426 * Splits the custom parameters field to the various parameters
427 *
428 * @param string $customstr String containing the parameters
429 *
430 * @return Array of custom parameters
431 */
dbb0fec9 432function lti_split_custom_parameters($customstr) {
b9b2e7bb
CS
433 $lines = preg_split("/[\n;]/", $customstr);
434 $retval = array();
435 foreach ($lines as $line) {
436 $pos = strpos($line, "=");
437 if ( $pos === false || $pos < 1 ) {
438 continue;
439 }
2f1e464a
PS
440 $key = trim(core_text::substr($line, 0, $pos));
441 $val = trim(core_text::substr($line, $pos+1, strlen($line)));
dbb0fec9 442 $key = lti_map_keyname($key);
b9b2e7bb
CS
443 $retval['custom_'.$key] = $val;
444 }
445 return $retval;
446}
447
448/**
449 * Used for building the names of the different custom parameters
450 *
451 * @param string $key Parameter name
452 *
453 * @return string Processed name
454 */
dbb0fec9 455function lti_map_keyname($key) {
b9b2e7bb 456 $newkey = "";
2f1e464a 457 $key = core_text::strtolower(trim($key));
b9b2e7bb
CS
458 foreach (str_split($key) as $ch) {
459 if ( ($ch >= 'a' && $ch <= 'z') || ($ch >= '0' && $ch <= '9') ) {
460 $newkey .= $ch;
461 } else {
462 $newkey .= '_';
463 }
464 }
465 return $newkey;
466}
467
468/**
16cac566 469 * Gets the IMS role string for the specified user and LTI course module.
e27cb316 470 *
16cac566
CS
471 * @param mixed $user User object or user id
472 * @param int $cmid The course module id of the LTI activity
473 * @return string A role string suitable for passing with an LTI launch
b9b2e7bb 474 */
1d4f052e 475function lti_get_ims_role($user, $cmid, $courseid) {
16cac566 476 $roles = array();
e27cb316 477
ea04a9f9 478 if (empty($cmid)) {
1d4f052e
CS
479 //If no cmid is passed, check if the user is a teacher in the course
480 //This allows other modules to programmatically "fake" a launch without
481 //a real LTI instance
c288a3db 482 $coursecontext = context_course::instance($courseid);
e27cb316 483
ea04a9f9 484 if (has_capability('moodle/course:manageactivities', $coursecontext)) {
1d4f052e
CS
485 array_push($roles, 'Instructor');
486 } else {
487 array_push($roles, 'Learner');
488 }
16cac566 489 } else {
c288a3db 490 $context = context_module::instance($cmid);
1d4f052e 491
ea04a9f9 492 if (has_capability('mod/lti:manage', $context)) {
1d4f052e
CS
493 array_push($roles, 'Instructor');
494 } else {
495 array_push($roles, 'Learner');
496 }
b9b2e7bb 497 }
1d4f052e 498
ea04a9f9 499 if (is_siteadmin($user)) {
16cac566 500 array_push($roles, 'urn:lti:sysrole:ims/lis/Administrator');
b9b2e7bb 501 }
e27cb316 502
16cac566 503 return join(',', $roles);
b9b2e7bb
CS
504}
505
506/**
507 * Returns configuration details for the tool
508 *
509 * @param int $typeid Basic LTI tool typeid
510 *
511 * @return array Tool Configuration
512 */
513function lti_get_type_config($typeid) {
514 global $DB;
515
5f136255
EL
516 $query = "SELECT name, value
517 FROM {lti_types_config}
518 WHERE typeid = :typeid1
519 UNION ALL
cc12ae6c 520 SELECT 'toolurl' AS name, " . $DB->sql_compare_text('baseurl', 1333) . " AS value
5f136255
EL
521 FROM {lti_types}
522 WHERE id = :typeid2";
e27cb316 523
b9b2e7bb 524 $typeconfig = array();
f17f4959 525 $configs = $DB->get_records_sql($query, array('typeid1' => $typeid, 'typeid2' => $typeid));
e27cb316 526
b9b2e7bb
CS
527 if (!empty($configs)) {
528 foreach ($configs as $config) {
529 $typeconfig[$config->name] = $config->value;
530 }
531 }
e27cb316 532
b9b2e7bb
CS
533 return $typeconfig;
534}
535
ea04a9f9 536function lti_get_tools_by_url($url, $state, $courseid = null) {
b9b2e7bb 537 $domain = lti_get_domain_from_url($url);
e27cb316 538
996b0fd9 539 return lti_get_tools_by_domain($domain, $state, $courseid);
b9b2e7bb
CS
540}
541
ea04a9f9 542function lti_get_tools_by_domain($domain, $state = null, $courseid = null) {
b9b2e7bb 543 global $DB, $SITE;
e27cb316 544
b9b2e7bb 545 $filters = array('tooldomain' => $domain);
e27cb316 546
b9b2e7bb
CS
547 $statefilter = '';
548 $coursefilter = '';
e27cb316 549
ea04a9f9 550 if ($state) {
b9b2e7bb
CS
551 $statefilter = 'AND state = :state';
552 }
e27cb316 553
ea04a9f9 554 if ($courseid && $courseid != $SITE->id) {
b9b2e7bb
CS
555 $coursefilter = 'OR course = :courseid';
556 }
e27cb316 557
5f136255
EL
558 $query = "SELECT *
559 FROM {lti_types}
560 WHERE tooldomain = :tooldomain
561 AND (course = :siteid $coursefilter)
562 $statefilter";
e27cb316 563
b9b2e7bb 564 return $DB->get_records_sql($query, array(
e27cb316
CS
565 'courseid' => $courseid,
566 'siteid' => $SITE->id,
567 'tooldomain' => $domain,
b9b2e7bb
CS
568 'state' => $state
569 ));
570}
571
572/**
573 * Returns all basicLTI tools configured by the administrator
574 *
575 */
6831c7cd 576function lti_filter_get_types($course) {
b9b2e7bb
CS
577 global $DB;
578
ea04a9f9 579 if (!empty($course)) {
6831c7cd
CS
580 $filter = array('course' => $course);
581 } else {
582 $filter = array();
583 }
e27cb316 584
6831c7cd 585 return $DB->get_records('lti_types', $filter);
b9b2e7bb
CS
586}
587
d81a603e
MN
588/**
589 * Given an array of tools, filter them based on their state
590 *
591 * @param array $tools An array of lti_types records
592 * @param int $state One of the LTI_TOOL_STATE_* constants
593 * @return array
594 */
595function lti_filter_tool_types(array $tools, $state) {
596 $return = array();
597 foreach ($tools as $key => $tool) {
598 if ($tool->state == $state) {
599 $return[$key] = $tool;
600 }
601 }
602 return $return;
603}
604
ea04a9f9 605function lti_get_types_for_add_instance() {
996b0fd9 606 global $DB, $SITE, $COURSE;
e27cb316 607
5f136255
EL
608 $query = "SELECT *
609 FROM {lti_types}
610 WHERE coursevisible = 1
611 AND (course = :siteid OR course = :courseid)
612 AND state = :active";
e27cb316 613
606ab1a1 614 $admintypes = $DB->get_records_sql($query, array('siteid' => $SITE->id, 'courseid' => $COURSE->id, 'active' => LTI_TOOL_STATE_CONFIGURED));
e27cb316 615
b9b2e7bb 616 $types = array();
996b0fd9 617 $types[0] = (object)array('name' => get_string('automatic', 'lti'), 'course' => $SITE->id);
e27cb316 618
ea04a9f9 619 foreach ($admintypes as $type) {
16e8f130 620 $types[$type->id] = $type;
b9b2e7bb 621 }
e27cb316 622
b9b2e7bb
CS
623 return $types;
624}
625
ea04a9f9 626function lti_get_domain_from_url($url) {
b9b2e7bb 627 $matches = array();
e27cb316 628
ea04a9f9 629 if (preg_match(LTI_URL_DOMAIN_REGEX, $url, $matches)) {
b9b2e7bb
CS
630 return $matches[1];
631 }
632}
633
ea04a9f9 634function lti_get_tool_by_url_match($url, $courseid = null, $state = LTI_TOOL_STATE_CONFIGURED) {
b69dc429 635 $possibletools = lti_get_tools_by_url($url, $state, $courseid);
e27cb316 636
7023b65e 637 return lti_get_best_tool_by_url($url, $possibletools, $courseid);
b9b2e7bb
CS
638}
639
ea04a9f9 640function lti_get_url_thumbprint($url) {
b9b2e7bb 641 $urlparts = parse_url(strtolower($url));
ea04a9f9 642 if (!isset($urlparts['path'])) {
b9b2e7bb
CS
643 $urlparts['path'] = '';
644 }
e27cb316 645
ea04a9f9 646 if (!isset($urlparts['host'])) {
d8d04121
CS
647 $urlparts['host'] = '';
648 }
e27cb316 649
ea04a9f9 650 if (substr($urlparts['host'], 0, 4) === 'www.') {
d8d04121 651 $urlparts['host'] = substr($urlparts['host'], 4);
b9b2e7bb 652 }
e27cb316 653
b9b2e7bb
CS
654 return $urllower = $urlparts['host'] . '/' . $urlparts['path'];
655}
656
ea04a9f9
EL
657function lti_get_best_tool_by_url($url, $tools, $courseid = null) {
658 if (count($tools) === 0) {
b9b2e7bb
CS
659 return null;
660 }
e27cb316 661
b9b2e7bb 662 $urllower = lti_get_url_thumbprint($url);
e27cb316 663
ea04a9f9 664 foreach ($tools as $tool) {
b9b2e7bb 665 $tool->_matchscore = 0;
e27cb316 666
b9b2e7bb 667 $toolbaseurllower = lti_get_url_thumbprint($tool->baseurl);
e27cb316 668
ea04a9f9 669 if ($urllower === $toolbaseurllower) {
7023b65e 670 //100 points for exact thumbprint match
b9b2e7bb 671 $tool->_matchscore += 100;
ea04a9f9 672 } else if (substr($urllower, 0, strlen($toolbaseurllower)) === $toolbaseurllower) {
7023b65e 673 //50 points if tool thumbprint starts with the base URL thumbprint
b9b2e7bb
CS
674 $tool->_matchscore += 50;
675 }
e27cb316 676
7023b65e 677 //Prefer course tools over site tools
ea04a9f9 678 if (!empty($courseid)) {
4c598b13 679 //Minus 10 points for not matching the course id (global tools)
ea04a9f9 680 if ($tool->course != $courseid) {
7023b65e
CS
681 $tool->_matchscore -= 10;
682 }
683 }
b9b2e7bb 684 }
e27cb316 685
ea04a9f9
EL
686 $bestmatch = array_reduce($tools, function($value, $tool) {
687 if ($tool->_matchscore > $value->_matchscore) {
b9b2e7bb
CS
688 return $tool;
689 } else {
690 return $value;
691 }
e27cb316 692
b9b2e7bb 693 }, (object)array('_matchscore' => -1));
e27cb316 694
b9b2e7bb 695 //None of the tools are suitable for this URL
ea04a9f9 696 if ($bestmatch->_matchscore <= 0) {
b9b2e7bb
CS
697 return null;
698 }
e27cb316 699
b9b2e7bb
CS
700 return $bestmatch;
701}
702
ea04a9f9 703function lti_get_shared_secrets_by_key($key) {
020eea1b 704 global $DB;
e27cb316 705
020eea1b
CS
706 //Look up the shared secret for the specified key in both the types_config table (for configured tools)
707 //And in the lti resource table for ad-hoc tools
5f136255
EL
708 $query = "SELECT t2.value
709 FROM {lti_types_config} t1
710 JOIN {lti_types_config} t2 ON t1.typeid = t2.typeid
711 JOIN {lti_types} type ON t2.typeid = type.id
712 WHERE t1.name = 'resourcekey'
713 AND t1.value = :key1
714 AND t2.name = 'password'
715 AND type.state = :configured
716 UNION
717 SELECT password AS value
718 FROM {lti}
719 WHERE resourcekey = :key2";
e27cb316 720
feb30fd8 721 $sharedsecrets = $DB->get_records_sql($query, array('configured' => LTI_TOOL_STATE_CONFIGURED, 'key1' => $key, 'key2' => $key));
e27cb316 722
ea04a9f9 723 $values = array_map(function($item) {
020eea1b
CS
724 return $item->value;
725 }, $sharedsecrets);
e27cb316 726
020eea1b
CS
727 //There should really only be one shared secret per key. But, we can't prevent
728 //more than one getting entered. For instance, if the same key is used for two tool providers.
729 return $values;
730}
731
b9b2e7bb
CS
732/**
733 * Delete a Basic LTI configuration
734 *
735 * @param int $id Configuration id
736 */
737function lti_delete_type($id) {
738 global $DB;
739
740 //We should probably just copy the launch URL to the tool instances in this case... using a single query
741 /*
742 $instances = $DB->get_records('lti', array('typeid' => $id));
743 foreach ($instances as $instance) {
744 $instance->typeid = 0;
745 $DB->update_record('lti', $instance);
746 }*/
747
748 $DB->delete_records('lti_types', array('id' => $id));
749 $DB->delete_records('lti_types_config', array('typeid' => $id));
750}
751
ea04a9f9 752function lti_set_state_for_type($id, $state) {
b9b2e7bb 753 global $DB;
e27cb316 754
b9b2e7bb
CS
755 $DB->update_record('lti_types', array('id' => $id, 'state' => $state));
756}
757
758/**
759 * Transforms a basic LTI object to an array
760 *
761 * @param object $ltiobject Basic LTI object
762 *
763 * @return array Basic LTI configuration details
764 */
765function lti_get_config($ltiobject) {
766 $typeconfig = array();
767 $typeconfig = (array)$ltiobject;
768 $additionalconfig = lti_get_type_config($ltiobject->typeid);
769 $typeconfig = array_merge($typeconfig, $additionalconfig);
770 return $typeconfig;
771}
772
773/**
774 *
775 * Generates some of the tool configuration based on the instance details
776 *
777 * @param int $id
778 *
779 * @return Instance configuration
780 *
781 */
782function lti_get_type_config_from_instance($id) {
783 global $DB;
784
785 $instance = $DB->get_record('lti', array('id' => $id));
786 $config = lti_get_config($instance);
787
788 $type = new stdClass();
789 $type->lti_fix = $id;
790 if (isset($config['toolurl'])) {
791 $type->lti_toolurl = $config['toolurl'];
792 }
793 if (isset($config['instructorchoicesendname'])) {
794 $type->lti_sendname = $config['instructorchoicesendname'];
795 }
796 if (isset($config['instructorchoicesendemailaddr'])) {
797 $type->lti_sendemailaddr = $config['instructorchoicesendemailaddr'];
798 }
799 if (isset($config['instructorchoiceacceptgrades'])) {
800 $type->lti_acceptgrades = $config['instructorchoiceacceptgrades'];
801 }
802 if (isset($config['instructorchoiceallowroster'])) {
803 $type->lti_allowroster = $config['instructorchoiceallowroster'];
804 }
805
806 if (isset($config['instructorcustomparameters'])) {
807 $type->lti_allowsetting = $config['instructorcustomparameters'];
808 }
809 return $type;
810}
811
812/**
813 * Generates some of the tool configuration based on the admin configuration details
814 *
815 * @param int $id
816 *
817 * @return Configuration details
818 */
819function lti_get_type_type_config($id) {
820 global $DB;
821
822 $basicltitype = $DB->get_record('lti_types', array('id' => $id));
823 $config = lti_get_type_config($id);
824
036e84c3 825 $type = new stdClass();
5f72d102 826
b9b2e7bb 827 $type->lti_typename = $basicltitype->name;
e27cb316 828
6831c7cd 829 $type->typeid = $basicltitype->id;
e27cb316 830
b9b2e7bb 831 $type->lti_toolurl = $basicltitype->baseurl;
e27cb316 832
b9b2e7bb
CS
833 if (isset($config['resourcekey'])) {
834 $type->lti_resourcekey = $config['resourcekey'];
835 }
836 if (isset($config['password'])) {
837 $type->lti_password = $config['password'];
838 }
839
840 if (isset($config['sendname'])) {
841 $type->lti_sendname = $config['sendname'];
842 }
ea04a9f9 843 if (isset($config['instructorchoicesendname'])) {
b9b2e7bb
CS
844 $type->lti_instructorchoicesendname = $config['instructorchoicesendname'];
845 }
ea04a9f9 846 if (isset($config['sendemailaddr'])) {
b9b2e7bb
CS
847 $type->lti_sendemailaddr = $config['sendemailaddr'];
848 }
ea04a9f9 849 if (isset($config['instructorchoicesendemailaddr'])) {
b9b2e7bb
CS
850 $type->lti_instructorchoicesendemailaddr = $config['instructorchoicesendemailaddr'];
851 }
ea04a9f9 852 if (isset($config['acceptgrades'])) {
b9b2e7bb
CS
853 $type->lti_acceptgrades = $config['acceptgrades'];
854 }
ea04a9f9 855 if (isset($config['instructorchoiceacceptgrades'])) {
b9b2e7bb
CS
856 $type->lti_instructorchoiceacceptgrades = $config['instructorchoiceacceptgrades'];
857 }
ea04a9f9 858 if (isset($config['allowroster'])) {
b9b2e7bb
CS
859 $type->lti_allowroster = $config['allowroster'];
860 }
ea04a9f9 861 if (isset($config['instructorchoiceallowroster'])) {
b9b2e7bb
CS
862 $type->lti_instructorchoiceallowroster = $config['instructorchoiceallowroster'];
863 }
864
865 if (isset($config['customparameters'])) {
866 $type->lti_customparameters = $config['customparameters'];
867 }
868
ea04a9f9 869 if (isset($config['forcessl'])) {
d8d04121
CS
870 $type->lti_forcessl = $config['forcessl'];
871 }
e27cb316 872
b9b2e7bb
CS
873 if (isset($config['organizationid'])) {
874 $type->lti_organizationid = $config['organizationid'];
875 }
876 if (isset($config['organizationurl'])) {
877 $type->lti_organizationurl = $config['organizationurl'];
878 }
879 if (isset($config['organizationdescr'])) {
880 $type->lti_organizationdescr = $config['organizationdescr'];
881 }
882 if (isset($config['launchcontainer'])) {
883 $type->lti_launchcontainer = $config['launchcontainer'];
884 }
e27cb316 885
b9b2e7bb
CS
886 if (isset($config['coursevisible'])) {
887 $type->lti_coursevisible = $config['coursevisible'];
888 }
e27cb316 889
b9b2e7bb
CS
890 if (isset($config['debuglaunch'])) {
891 $type->lti_debuglaunch = $config['debuglaunch'];
892 }
e27cb316 893
b9b2e7bb 894 if (isset($config['module_class_type'])) {
036e84c3 895 $type->lti_module_class_type = $config['module_class_type'];
b9b2e7bb
CS
896 }
897
898 return $type;
899}
900
ea04a9f9 901function lti_prepare_type_for_save($type, $config) {
b9b2e7bb
CS
902 $type->baseurl = $config->lti_toolurl;
903 $type->tooldomain = lti_get_domain_from_url($config->lti_toolurl);
904 $type->name = $config->lti_typename;
e27cb316 905
b9b2e7bb
CS
906 $type->coursevisible = !empty($config->lti_coursevisible) ? $config->lti_coursevisible : 0;
907 $config->lti_coursevisible = $type->coursevisible;
e27cb316 908
d8d04121
CS
909 $type->forcessl = !empty($config->lti_forcessl) ? $config->lti_forcessl : 0;
910 $config->lti_forcessl = $type->forcessl;
e27cb316 911
b9b2e7bb 912 $type->timemodified = time();
e27cb316 913
b9b2e7bb
CS
914 unset ($config->lti_typename);
915 unset ($config->lti_toolurl);
916}
917
ea04a9f9 918function lti_update_type($type, $config) {
b9b2e7bb 919 global $DB;
e27cb316 920
b9b2e7bb 921 lti_prepare_type_for_save($type, $config);
e27cb316 922
b9b2e7bb
CS
923 if ($DB->update_record('lti_types', $type)) {
924 foreach ($config as $key => $value) {
925 if (substr($key, 0, 4)=='lti_' && !is_null($value)) {
926 $record = new StdClass();
927 $record->typeid = $type->id;
928 $record->name = substr($key, 4);
929 $record->value = $value;
e27cb316 930
b9b2e7bb
CS
931 lti_update_config($record);
932 }
933 }
934 }
935}
936
ea04a9f9 937function lti_add_type($type, $config) {
b9b2e7bb 938 global $USER, $SITE, $DB;
e27cb316 939
b9b2e7bb 940 lti_prepare_type_for_save($type, $config);
e27cb316 941
ea04a9f9 942 if (!isset($type->state)) {
b9b2e7bb
CS
943 $type->state = LTI_TOOL_STATE_PENDING;
944 }
e27cb316 945
ea04a9f9 946 if (!isset($type->timecreated)) {
b9b2e7bb
CS
947 $type->timecreated = time();
948 }
e27cb316 949
ea04a9f9 950 if (!isset($type->createdby)) {
b9b2e7bb
CS
951 $type->createdby = $USER->id;
952 }
e27cb316 953
ea04a9f9 954 if (!isset($type->course)) {
b9b2e7bb
CS
955 $type->course = $SITE->id;
956 }
e27cb316 957
b9b2e7bb 958 //Create a salt value to be used for signing passed data to extension services
6831c7cd
CS
959 //The outcome service uses the service salt on the instance. This can be used
960 //for communication with services not related to a specific LTI instance.
b9b2e7bb
CS
961 $config->lti_servicesalt = uniqid('', true);
962
963 $id = $DB->insert_record('lti_types', $type);
964
965 if ($id) {
966 foreach ($config as $key => $value) {
967 if (substr($key, 0, 4)=='lti_' && !is_null($value)) {
968 $record = new StdClass();
969 $record->typeid = $id;
970 $record->name = substr($key, 4);
971 $record->value = $value;
972
973 lti_add_config($record);
974 }
975 }
976 }
e27cb316 977
996b0fd9 978 return $id;
b9b2e7bb
CS
979}
980
981/**
982 * Add a tool configuration in the database
983 *
984 * @param $config Tool configuration
985 *
986 * @return int Record id number
987 */
988function lti_add_config($config) {
989 global $DB;
990
991 return $DB->insert_record('lti_types_config', $config);
992}
993
994/**
995 * Updates a tool configuration in the database
996 *
997 * @param $config Tool configuration
998 *
999 * @return Record id number
1000 */
1001function lti_update_config($config) {
1002 global $DB;
1003
1004 $return = true;
1005 $old = $DB->get_record('lti_types_config', array('typeid' => $config->typeid, 'name' => $config->name));
e27cb316 1006
b9b2e7bb
CS
1007 if ($old) {
1008 $config->id = $old->id;
1009 $return = $DB->update_record('lti_types_config', $config);
1010 } else {
1011 $return = $DB->insert_record('lti_types_config', $config);
1012 }
1013 return $return;
1014}
1015
1016/**
1017 * Signs the petition to launch the external tool using OAuth
1018 *
1019 * @param $oldparms Parameters to be passed for signing
1020 * @param $endpoint url of the external tool
1021 * @param $method Method for sending the parameters (e.g. POST)
1022 * @param $oauth_consumoer_key Key
1023 * @param $oauth_consumoer_secret Secret
1024 * @param $submittext The text for the submit button
1025 * @param $orgid LMS name
1026 * @param $orgdesc LMS key
1027 */
3dd9ca24
CS
1028function lti_sign_parameters($oldparms, $endpoint, $method, $oauthconsumerkey, $oauthconsumersecret) {
1029 //global $lastbasestring;
b9b2e7bb 1030 $parms = $oldparms;
b9b2e7bb
CS
1031
1032 $testtoken = '';
e27cb316 1033
fabd4fcf 1034 // TODO: Switch to core oauthlib once implemented - MDL-30149
795dff01
CS
1035 $hmacmethod = new lti\OAuthSignatureMethod_HMAC_SHA1();
1036 $testconsumer = new lti\OAuthConsumer($oauthconsumerkey, $oauthconsumersecret, null);
795dff01 1037 $accreq = lti\OAuthRequest::from_consumer_and_token($testconsumer, $testtoken, $method, $endpoint, $parms);
b9b2e7bb
CS
1038 $accreq->sign_request($hmacmethod, $testconsumer, $testtoken);
1039
1040 // Pass this back up "out of band" for debugging
3dd9ca24 1041 //$lastbasestring = $accreq->get_signature_base_string();
b9b2e7bb
CS
1042
1043 $newparms = $accreq->get_parameters();
1044
1045 return $newparms;
1046}
1047
1048/**
1049 * Posts the launch petition HTML
1050 *
1051 * @param $newparms Signed parameters
1052 * @param $endpoint URL of the external tool
1053 * @param $debug Debug (true/false)
1054 */
dbb0fec9 1055function lti_post_launch_html($newparms, $endpoint, $debug=false) {
b9b2e7bb 1056 $r = "<form action=\"".$endpoint."\" name=\"ltiLaunchForm\" id=\"ltiLaunchForm\" method=\"post\" encType=\"application/x-www-form-urlencoded\">\n";
e27cb316 1057
b9b2e7bb
CS
1058 $submittext = $newparms['ext_submit'];
1059
1060 // Contruct html for the launch parameters
1061 foreach ($newparms as $key => $value) {
1062 $key = htmlspecialchars($key);
1063 $value = htmlspecialchars($value);
1064 if ( $key == "ext_submit" ) {
1065 $r .= "<input type=\"submit\" name=\"";
1066 } else {
1067 $r .= "<input type=\"hidden\" name=\"";
1068 }
1069 $r .= $key;
1070 $r .= "\" value=\"";
1071 $r .= $value;
1072 $r .= "\"/>\n";
1073 }
1074
1075 if ( $debug ) {
1076 $r .= "<script language=\"javascript\"> \n";
1077 $r .= " //<![CDATA[ \n";
1078 $r .= "function basicltiDebugToggle() {\n";
1079 $r .= " var ele = document.getElementById(\"basicltiDebug\");\n";
ea04a9f9 1080 $r .= " if (ele.style.display == \"block\") {\n";
b9b2e7bb
CS
1081 $r .= " ele.style.display = \"none\";\n";
1082 $r .= " }\n";
1083 $r .= " else {\n";
1084 $r .= " ele.style.display = \"block\";\n";
1085 $r .= " }\n";
1086 $r .= "} \n";
1087 $r .= " //]]> \n";
1088 $r .= "</script>\n";
1089 $r .= "<a id=\"displayText\" href=\"javascript:basicltiDebugToggle();\">";
1090 $r .= get_string("toggle_debug_data", "lti")."</a>\n";
1091 $r .= "<div id=\"basicltiDebug\" style=\"display:none\">\n";
1092 $r .= "<b>".get_string("basiclti_endpoint", "lti")."</b><br/>\n";
1093 $r .= $endpoint . "<br/>\n&nbsp;<br/>\n";
1094 $r .= "<b>".get_string("basiclti_parameters", "lti")."</b><br/>\n";
1095 foreach ($newparms as $key => $value) {
1096 $key = htmlspecialchars($key);
1097 $value = htmlspecialchars($value);
1098 $r .= "$key = $value<br/>\n";
1099 }
1100 $r .= "&nbsp;<br/>\n";
3dd9ca24 1101 //$r .= "<p><b>".get_string("basiclti_base_string", "lti")."</b><br/>\n".$lastbasestring."</p>\n";
b9b2e7bb
CS
1102 $r .= "</div>\n";
1103 }
1104 $r .= "</form>\n";
1105
1106 if ( ! $debug ) {
1107 $ext_submit = "ext_submit";
1108 $ext_submit_text = $submittext;
1109 $r .= " <script type=\"text/javascript\"> \n" .
1110 " //<![CDATA[ \n" .
1111 " document.getElementById(\"ltiLaunchForm\").style.display = \"none\";\n" .
1112 " nei = document.createElement('input');\n" .
1113 " nei.setAttribute('type', 'hidden');\n" .
1114 " nei.setAttribute('name', '".$ext_submit."');\n" .
1115 " nei.setAttribute('value', '".$ext_submit_text."');\n" .
1116 " document.getElementById(\"ltiLaunchForm\").appendChild(nei);\n" .
1117 " document.ltiLaunchForm.submit(); \n" .
1118 " //]]> \n" .
1119 " </script> \n";
1120 }
1121 return $r;
1122}
1123
ea04a9f9 1124function lti_get_type($typeid) {
996b0fd9 1125 global $DB;
e27cb316 1126
996b0fd9 1127 return $DB->get_record('lti_types', array('id' => $typeid));
57d1dffd
CS
1128}
1129
ea04a9f9
EL
1130function lti_get_launch_container($lti, $toolconfig) {
1131 if (empty($lti->launchcontainer)) {
e27cb316
CS
1132 $lti->launchcontainer = LTI_LAUNCH_CONTAINER_DEFAULT;
1133 }
1134
ea04a9f9
EL
1135 if ($lti->launchcontainer == LTI_LAUNCH_CONTAINER_DEFAULT) {
1136 if (isset($toolconfig['launchcontainer'])) {
c4d80efe
CS
1137 $launchcontainer = $toolconfig['launchcontainer'];
1138 }
1139 } else {
1140 $launchcontainer = $lti->launchcontainer;
1141 }
e27cb316 1142
ea04a9f9 1143 if (empty($launchcontainer) || $launchcontainer == LTI_LAUNCH_CONTAINER_DEFAULT) {
c4d80efe
CS
1144 $launchcontainer = LTI_LAUNCH_CONTAINER_EMBED_NO_BLOCKS;
1145 }
57d1dffd 1146
c3d2fbf9 1147 $devicetype = core_useragent::get_device_type();
57d1dffd
CS
1148
1149 //Scrolling within the object element doesn't work on iOS or Android
1150 //Opening the popup window also had some issues in testing
1151 //For mobile devices, always take up the entire screen to ensure the best experience
c3d2fbf9 1152 if ($devicetype === core_useragent::DEVICETYPE_MOBILE || $devicetype === core_useragent::DEVICETYPE_TABLET ) {
57d1dffd
CS
1153 $launchcontainer = LTI_LAUNCH_CONTAINER_REPLACE_MOODLE_WINDOW;
1154 }
e27cb316 1155
57d1dffd 1156 return $launchcontainer;
d8d04121
CS
1157}
1158
1159function lti_request_is_using_ssl() {
33dca156
PS
1160 global $CFG;
1161 return (stripos($CFG->httpswwwroot, 'https://') === 0);
d8d04121
CS
1162}
1163
ea04a9f9
EL
1164function lti_ensure_url_is_https($url) {
1165 if (!strstr($url, '://')) {
d8d04121
CS
1166 $url = 'https://' . $url;
1167 } else {
1168 //If the URL starts with http, replace with https
ea04a9f9 1169 if (stripos($url, 'http://') === 0) {
adc23cc9 1170 $url = 'https://' . substr($url, 7);
d8d04121
CS
1171 }
1172 }
e27cb316 1173
d8d04121 1174 return $url;
61eb12d4 1175}