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