Merge branch 'MDL-69688' of https://github.com/stronk7/moodle
[moodle.git] / enrol / lti / tool.php
blob5aad2b3af5530554e19aac15c1ce5f84bfb161b0
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 * The main entry point for the external system.
20 * @package enrol_lti
21 * @copyright 2016 Mark Nelson <markn@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once(__DIR__ . '/../../config.php');
27 $toolid = required_param('id', PARAM_INT);
29 $PAGE->set_context(context_system::instance());
30 $url = new moodle_url('/enrol/lti/tool.php');
31 $PAGE->set_url($url);
32 $PAGE->set_pagelayout('popup');
33 $PAGE->set_title(get_string('opentool', 'enrol_lti'));
35 // Get the tool.
36 $tool = \enrol_lti\helper::get_lti_tool($toolid);
38 // Check if the authentication plugin is disabled.
39 if (!is_enabled_auth('lti')) {
40 throw new \moodle_exception('pluginnotenabled', 'auth', '', get_string('pluginname', 'auth_lti'));
41 exit();
44 // Check if the enrolment plugin is disabled.
45 if (!enrol_is_enabled('lti')) {
46 throw new \moodle_exception('enrolisdisabled', 'enrol_lti');
47 exit();
50 // Check if the enrolment instance is disabled.
51 if ($tool->status != ENROL_INSTANCE_ENABLED) {
52 throw new \moodle_exception('enrolisdisabled', 'enrol_lti');
53 exit();
56 // Check if the enrolment instance has been upgraded to a newer LTI version.
57 if ($tool->ltiversion != 'LTI-1p0/LTI-2p0') {
58 throw new \moodle_exception('enrolltiversionincorrect', 'enrol_lti');
59 exit();
62 $consumerkey = required_param('oauth_consumer_key', PARAM_TEXT);
63 $ltiversion = optional_param('lti_version', null, PARAM_TEXT);
64 $messagetype = required_param('lti_message_type', PARAM_TEXT);
66 // Only accept launch requests from this endpoint.
67 if ($messagetype != "basic-lti-launch-request") {
68 throw new \moodle_exception('invalidrequest', 'enrol_lti');
69 exit();
72 // Initialise tool provider.
73 $toolprovider = new \enrol_lti\tool_provider($toolid);
75 // Special handling for LTIv1 launch requests.
76 if ($ltiversion === \IMSGlobal\LTI\ToolProvider\ToolProvider::LTI_VERSION1) {
77 $dataconnector = new \enrol_lti\data_connector();
78 $consumer = new \IMSGlobal\LTI\ToolProvider\ToolConsumer($consumerkey, $dataconnector);
79 // Check if the consumer has already been registered to the enrol_lti_lti2_consumer table. Register if necessary.
80 $consumer->ltiVersion = \IMSGlobal\LTI\ToolProvider\ToolProvider::LTI_VERSION1;
81 // For LTIv1, set the tool secret as the consumer secret.
82 $consumer->secret = $tool->secret;
83 $consumer->name = optional_param('tool_consumer_instance_name', '', PARAM_TEXT);
84 $consumer->consumerName = $consumer->name;
85 $consumer->consumerGuid = optional_param('tool_consumer_instance_guid', null, PARAM_TEXT);
86 $consumer->consumerVersion = optional_param('tool_consumer_info_version', null, PARAM_TEXT);
87 $consumer->enabled = true;
88 $consumer->protected = true;
89 $consumer->save();
91 // Set consumer to tool provider.
92 $toolprovider->consumer = $consumer;
93 // Map tool consumer and published tool, if necessary.
94 $toolprovider->map_tool_to_consumer();
97 // Handle the request.
98 $toolprovider->handleRequest();
100 echo $OUTPUT->header();
101 echo $OUTPUT->footer();