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.
   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   * Exception handler for LTI services
  19   *
  20   * @package   mod_lti
  21   * @copyright Copyright (c) 2015 Moodlerooms Inc. (http://www.moodlerooms.com)
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace mod_lti;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  require_once (__DIR__.'/../locallib.php');
  30  require_once (__DIR__.'/../servicelib.php');
  31  
  32  /**
  33   * Handles exceptions when handling incoming LTI messages.
  34   *
  35   * Ensures that LTI always returns a XML message that can be consumed by the caller.
  36   *
  37   * @package   mod_lti
  38   * @copyright Copyright (c) 2015 Moodlerooms Inc. (http://www.moodlerooms.com)
  39   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class service_exception_handler {
  42      /**
  43       * Enable error response logging.
  44       *
  45       * @var bool
  46       */
  47      protected $log = false;
  48  
  49      /**
  50       * The LTI service message ID, if known.
  51       *
  52       * @var string
  53       */
  54      protected $id = '';
  55  
  56      /**
  57       * The LTI service message type, if known.
  58       *
  59       * @var string
  60       */
  61      protected $type = 'unknownRequest';
  62  
  63      /**
  64       * Constructor.
  65       *
  66       * @param boolean $log Enable error response logging.
  67       */
  68      public function __construct($log) {
  69          $this->log = $log;
  70      }
  71  
  72      /**
  73       * Set the LTI message ID being handled.
  74       *
  75       * @param string $id
  76       */
  77      public function set_message_id($id) {
  78          if (!empty($id)) {
  79              $this->id = $id;
  80          }
  81      }
  82  
  83      /**
  84       * Set the LTI message type being handled.
  85       *
  86       * @param string $type
  87       */
  88      public function set_message_type($type) {
  89          if (!empty($type)) {
  90              $this->type = $type;
  91          }
  92      }
  93  
  94      /**
  95       * Echo an exception message encapsulated in XML.
  96       *
  97       * @param \Exception|\Throwable $exception The exception that was thrown
  98       */
  99      public function handle($exception) {
 100          $message = $exception->getMessage();
 101  
 102          // Add the exception backtrace for developers.
 103          if (debugging('', DEBUG_DEVELOPER)) {
 104              $message .= "\n".format_backtrace(get_exception_info($exception)->backtrace, true);
 105          }
 106  
 107          // Switch to response.
 108          $type = str_replace('Request', 'Response', $this->type);
 109  
 110          // Build the appropriate xml.
 111          $response = lti_get_response_xml('failure', $message, $this->id, $type);
 112  
 113          $xml = $response->asXML();
 114  
 115          // Log the request if necessary.
 116          if ($this->log) {
 117              lti_log_response($xml, $exception);
 118          }
 119  
 120          echo $xml;
 121      }
 122  }