Moodle release 4.0.8
[moodle.git] / enrol / lti / configure.php
blob59d17780029fb4fafd0d8cccb45e442ead544166
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Returns the deep link resource via a POST to the platform.
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
25 use enrol_lti\local\ltiadvantage\lib\http_client;
26 use enrol_lti\local\ltiadvantage\lib\launch_cache_session;
27 use enrol_lti\local\ltiadvantage\lib\issuer_database;
28 use enrol_lti\local\ltiadvantage\repository\application_registration_repository;
29 use enrol_lti\local\ltiadvantage\repository\deployment_repository;
30 use enrol_lti\local\ltiadvantage\repository\published_resource_repository;
31 use Packback\Lti1p3\ImsStorage\ImsCookie;
32 use Packback\Lti1p3\LtiDeepLinkResource;
33 use Packback\Lti1p3\LtiLineitem;
34 use Packback\Lti1p3\LtiMessageLaunch;
35 use Packback\Lti1p3\LtiServiceConnector;
37 require(__DIR__.'/../../config.php');
38 require_once(__DIR__.'/lib.php');
39 global $CFG, $DB, $PAGE, $USER;
40 require_once($CFG->libdir . '/filelib.php');
41 require_login(null, false);
43 confirm_sesskey();
44 $launchid = required_param('launchid', PARAM_TEXT);
45 $modules = optional_param_array('modules', [], PARAM_INT);
46 $grades = optional_param_array('grades', [], PARAM_INT);
48 $sesscache = new launch_cache_session();
49 $issdb = new issuer_database(new application_registration_repository(), new deployment_repository());
50 $cookie = new ImsCookie();
51 $serviceconnector = new LtiServiceConnector($sesscache, new http_client(new curl()));
52 $messagelaunch = LtiMessageLaunch::fromCache($launchid, $issdb, $sesscache, $serviceconnector);
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.');
58 $sesscache->purge();
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);
64 $contentitems = [];
65 foreach ($resources as $resource) {
67 $contentitem = LtiDeepLinkResource::new()
68 ->setUrl($CFG->wwwroot . '/enrol/lti/launch.php')
69 ->setCustomParams(['id' => $resource->get_uuid()])
70 ->setTitle($resource->get_name());
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');
76 $lineitem = LtiLineitem::new()
77 ->setScoreMaximum($resource->get_grademax())
78 ->setResourceId($resource->get_uuid());
80 $contentitem->setLineitem($lineitem);
83 $contentitems[] = $contentitem;
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 $dl->outputResponseForm($contentitems);