2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Handles LTI 1.3 deep linking launches.
20 * There are 2 pathways through this page:
21 * 1. When first making a deep linking launch from the platform. The launch data is cached at this point, pending user
22 * authentication, and the page is set such that the post-authentication redirect will return here.
23 * 2. The post-authentication redirect. The launch data is fetched from the session launch cache, and the resource
24 * selection view is rendered.
27 * @copyright 2021 Jake Dallimore <jrhdallimore@gmail.com>
28 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32 use enrol_lti\local\ltiadvantage\lib\lti_cookie
;
33 use enrol_lti\local\ltiadvantage\lib\issuer_database
;
34 use enrol_lti\local\ltiadvantage\lib\launch_cache_session
;
35 use enrol_lti\local\ltiadvantage\repository\application_registration_repository
;
36 use enrol_lti\local\ltiadvantage\repository\deployment_repository
;
37 use enrol_lti\local\ltiadvantage\repository\published_resource_repository
;
38 use Packback\Lti1p3\LtiMessageLaunch
;
39 use Packback\Lti1p3\LtiServiceConnector
;
41 require_once(__DIR__
. '/../../config.php');
42 global $OUTPUT, $PAGE, $CFG;
43 require_once($CFG->libdir
. '/filelib.php');
45 $idtoken = optional_param('id_token', null, PARAM_RAW
);
46 $launchid = optional_param('launchid', null, PARAM_RAW
);
48 if (!is_enabled_auth('lti')) {
49 throw new moodle_exception('pluginnotenabled', 'auth', '', get_string('pluginname', 'auth_lti'));
51 if (!enrol_is_enabled('lti')) {
52 throw new moodle_exception('enrolisdisabled', 'enrol_lti');
54 if (empty($idtoken) && empty($launchid)) {
55 throw new coding_exception('Error: launch requires id_token');
58 // First launch from the platform: get launch data and cache it in case the user's not authenticated.
59 $sesscache = new launch_cache_session();
60 $issdb = new issuer_database(new application_registration_repository(), new deployment_repository());
61 $cookie = new lti_cookie();
62 $serviceconnector = new LtiServiceConnector($sesscache, new http_client());
64 $messagelaunch = LtiMessageLaunch
::new($issdb, $sesscache, $cookie, $serviceconnector)
68 $messagelaunch = LtiMessageLaunch
::fromCache($launchid, $issdb, $sesscache, $cookie, $serviceconnector);
70 if (empty($messagelaunch)) {
71 throw new moodle_exception('Bad launch. Deep linking launch data could not be found');
74 // Authenticate the instructor.
75 // Deep linking cannot use resource-specific provisioning modes, so it just uses a sensible 'existing accounts only' mode.
76 $auth = get_auth_plugin('lti');
77 $auth->complete_login(
78 $messagelaunch->getLaunchData(),
79 new moodle_url('/enrol/lti/launch_deeplink.php', ['launchid' => $messagelaunch->getLaunchId()]),
80 auth_plugin_lti
::PROVISIONING_MODE_PROMPT_EXISTING_ONLY
83 require_login(null, false);
85 $PAGE->set_context(context_system
::instance());
86 $url = new moodle_url('/enrol/lti/launch_deeplink.php');
88 $PAGE->set_pagelayout('popup');
89 $PAGE->set_title(get_string('opentool', 'enrol_lti'));
91 // Get all the published_resource view objects and render them for selection.
93 $resourcerepo = new published_resource_repository();
94 $resources = $resourcerepo->find_all_for_user($USER->id
);
95 $renderer = $PAGE->get_renderer('enrol_lti');
97 echo $OUTPUT->header();
98 echo $renderer->render_published_resource_selection_view($messagelaunch, $resources);
99 echo $OUTPUT->footer();