Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
/enrol/lti/ -> tool.php (source)

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]

   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   * The main entry point for the external system.
  19   *
  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
  23   */
  24  
  25  require_once(__DIR__ . '/../../config.php');
  26  
  27  $toolid = required_param('id', PARAM_INT);
  28  
  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'));
  34  
  35  // Get the tool.
  36  $tool = \enrol_lti\helper::get_lti_tool($toolid);
  37  
  38  // Check if the authentication plugin is disabled.
  39  if (!is_enabled_auth('lti')) {
  40      print_error('pluginnotenabled', 'auth', '', get_string('pluginname', 'auth_lti'));
  41      exit();
  42  }
  43  
  44  // Check if the enrolment plugin is disabled.
  45  if (!enrol_is_enabled('lti')) {
  46      print_error('enrolisdisabled', 'enrol_lti');
  47      exit();
  48  }
  49  
  50  // Check if the enrolment instance is disabled.
  51  if ($tool->status != ENROL_INSTANCE_ENABLED) {
  52      print_error('enrolisdisabled', 'enrol_lti');
  53      exit();
  54  }
  55  
  56  $consumerkey = required_param('oauth_consumer_key', PARAM_TEXT);
  57  $ltiversion = optional_param('lti_version', null, PARAM_TEXT);
  58  $messagetype = required_param('lti_message_type', PARAM_TEXT);
  59  
  60  // Only accept launch requests from this endpoint.
  61  if ($messagetype != "basic-lti-launch-request") {
  62      print_error('invalidrequest', 'enrol_lti');
  63      exit();
  64  }
  65  
  66  // Initialise tool provider.
  67  $toolprovider = new \enrol_lti\tool_provider($toolid);
  68  
  69  // Special handling for LTIv1 launch requests.
  70  if ($ltiversion === \IMSGlobal\LTI\ToolProvider\ToolProvider::LTI_VERSION1) {
  71      $dataconnector = new \enrol_lti\data_connector();
  72      $consumer = new \IMSGlobal\LTI\ToolProvider\ToolConsumer($consumerkey, $dataconnector);
  73      // Check if the consumer has already been registered to the enrol_lti_lti2_consumer table. Register if necessary.
  74      $consumer->ltiVersion = \IMSGlobal\LTI\ToolProvider\ToolProvider::LTI_VERSION1;
  75      // For LTIv1, set the tool secret as the consumer secret.
  76      $consumer->secret = $tool->secret;
  77      $consumer->name = optional_param('tool_consumer_instance_name', '', PARAM_TEXT);
  78      $consumer->consumerName = $consumer->name;
  79      $consumer->consumerGuid = optional_param('tool_consumer_instance_guid', null, PARAM_TEXT);
  80      $consumer->consumerVersion = optional_param('tool_consumer_info_version', null, PARAM_TEXT);
  81      $consumer->enabled = true;
  82      $consumer->protected = true;
  83      $consumer->save();
  84  
  85      // Set consumer to tool provider.
  86      $toolprovider->consumer = $consumer;
  87      // Map tool consumer and published tool, if necessary.
  88      $toolprovider->map_tool_to_consumer();
  89  }
  90  
  91  // Handle the request.
  92  $toolprovider->handleRequest();
  93  
  94  echo $OUTPUT->header();
  95  echo $OUTPUT->footer();