1 |
efrain |
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.
|
|
|
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 |
|
|
|
17 |
/**
|
|
|
18 |
* Returns the deep link resource via a POST to the platform.
|
|
|
19 |
*
|
|
|
20 |
* @package enrol_lti
|
|
|
21 |
* @copyright 2021 Jake Dallimore <jrhdallimore@gmail.com>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
use core\http_client;
|
|
|
26 |
use enrol_lti\local\ltiadvantage\lib\lti_cookie;
|
|
|
27 |
use enrol_lti\local\ltiadvantage\lib\launch_cache_session;
|
|
|
28 |
use enrol_lti\local\ltiadvantage\lib\issuer_database;
|
|
|
29 |
use enrol_lti\local\ltiadvantage\repository\application_registration_repository;
|
|
|
30 |
use enrol_lti\local\ltiadvantage\repository\deployment_repository;
|
|
|
31 |
use enrol_lti\local\ltiadvantage\repository\published_resource_repository;
|
|
|
32 |
use Packback\Lti1p3\DeepLinkResources\Resource;
|
|
|
33 |
use Packback\Lti1p3\LtiConstants;
|
|
|
34 |
use Packback\Lti1p3\LtiLineitem;
|
|
|
35 |
use Packback\Lti1p3\LtiMessageLaunch;
|
|
|
36 |
use Packback\Lti1p3\LtiServiceConnector;
|
|
|
37 |
|
|
|
38 |
require(__DIR__.'/../../config.php');
|
|
|
39 |
require_once(__DIR__.'/lib.php');
|
|
|
40 |
global $CFG, $DB, $PAGE, $USER;
|
|
|
41 |
require_once($CFG->libdir . '/filelib.php');
|
|
|
42 |
require_login(null, false);
|
|
|
43 |
require_sesskey();
|
|
|
44 |
$launchid = required_param('launchid', PARAM_TEXT);
|
|
|
45 |
$modules = optional_param_array('modules', [], PARAM_INT);
|
|
|
46 |
$grades = optional_param_array('grades', [], PARAM_INT);
|
|
|
47 |
|
|
|
48 |
$sesscache = new launch_cache_session();
|
|
|
49 |
$issdb = new issuer_database(new application_registration_repository(), new deployment_repository());
|
|
|
50 |
$cookie = new lti_cookie();
|
|
|
51 |
$serviceconnector = new LtiServiceConnector($sesscache, new http_client());
|
|
|
52 |
$messagelaunch = LtiMessageLaunch::fromCache($launchid, $issdb, $sesscache, $cookie, $serviceconnector);
|
|
|
53 |
|
|
|
54 |
if (!$messagelaunch->isDeepLinkLaunch()) {
|
|
|
55 |
throw new coding_exception('Configuration can only be accessed as part of a content item selection deep link '.
|
|
|
56 |
'launch.');
|
|
|
57 |
}
|
|
|
58 |
$sesscache->purge();
|
|
|
59 |
|
|
|
60 |
// Get the selected resources and create the resource link content items to post back.
|
|
|
61 |
$resourcerepo = new published_resource_repository();
|
|
|
62 |
$resources = $resourcerepo->find_all_by_ids_for_user($modules, $USER->id);
|
|
|
63 |
|
|
|
64 |
$contentitems = [];
|
|
|
65 |
foreach ($resources as $resource) {
|
|
|
66 |
|
|
|
67 |
$contentitem = Resource::new()
|
|
|
68 |
->setUrl($CFG->wwwroot . '/enrol/lti/launch.php')
|
|
|
69 |
->setCustomParams(['id' => $resource->get_uuid()])
|
|
|
70 |
->setTitle($resource->get_name());
|
|
|
71 |
|
|
|
72 |
// If the activity supports grading, and the user has selected it, then include line item information.
|
|
|
73 |
if ($resource->supports_grades() && in_array($resource->get_id(), $grades)) {
|
|
|
74 |
require_once($CFG->libdir . '/gradelib.php');
|
|
|
75 |
|
|
|
76 |
$lineitem = LtiLineitem::new()
|
|
|
77 |
->setScoreMaximum($resource->get_grademax())
|
|
|
78 |
->setResourceId($resource->get_uuid());
|
|
|
79 |
|
|
|
80 |
$contentitem->setLineitem($lineitem);
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
$contentitems[] = $contentitem;
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
|
|
|
87 |
global $USER, $CFG, $OUTPUT;
|
|
|
88 |
$PAGE->set_context(context_system::instance());
|
|
|
89 |
$url = new moodle_url('/enrol/lti/configure.php');
|
|
|
90 |
$PAGE->set_url($url);
|
|
|
91 |
$PAGE->set_pagelayout('popup');
|
|
|
92 |
echo $OUTPUT->header();
|
|
|
93 |
$dl = $messagelaunch->getDeepLink();
|
|
|
94 |
|
|
|
95 |
$formactionurl = $messagelaunch->getLaunchData()[LtiConstants::DL_DEEP_LINK_SETTINGS]['deep_link_return_url'];
|
|
|
96 |
echo <<<HTML
|
|
|
97 |
<form id="auto_submit" action="{$formactionurl}" method="POST">
|
|
|
98 |
<input type="hidden" name="JWT" value="{$messagelaunch->getDeepLink()->getResponseJwt($contentitems)}" />
|
|
|
99 |
<input type="submit" name="Go" />
|
|
|
100 |
</form>
|
|
|
101 |
<script>document.getElementById('auto_submit').submit();</script>
|
|
|
102 |
HTML;
|
|
|
103 |
|