Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are 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   * This file contains a class definition for the System Settings resource
  19   *
  20   * @package    ltiservice_toolsettings
  21   * @copyright  2014 Vital Source Technologies http://vitalsource.com
  22   * @author     Stephen Vickers
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  
  27  namespace ltiservice_toolsettings\local\resources;
  28  
  29  use ltiservice_toolsettings\local\service\toolsettings;
  30  use mod_lti\local\ltiservice\resource_base;
  31  
  32  defined('MOODLE_INTERNAL') || die();
  33  
  34  /**
  35   * A resource implementing the System-level (ToolProxy) Settings.
  36   *
  37   * @package    ltiservice_toolsettings
  38   * @since      Moodle 2.8
  39   * @copyright  2014 Vital Source Technologies http://vitalsource.com
  40   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  class systemsettings extends resource_base {
  43  
  44      /**
  45       * Class constructor.
  46       *
  47       * @param \mod_lti\local\ltiservice\service_base $service Service instance
  48       */
  49      public function __construct($service) {
  50  
  51          parent::__construct($service);
  52          $this->id = 'ToolProxySettings';
  53          $this->template = '/{config_type}/{tool_proxy_id}(/custom)';
  54          $this->variables[] = 'ToolProxy.custom.url';
  55          $this->formats[] = 'application/vnd.ims.lti.v2.toolsettings+json';
  56          $this->formats[] = 'application/vnd.ims.lti.v2.toolsettings.simple+json';
  57          $this->methods[] = self::HTTP_GET;
  58          $this->methods[] = self::HTTP_PUT;
  59  
  60      }
  61  
  62      /**
  63       * Execute the request for this resource.
  64       *
  65       * @param \mod_lti\local\ltiservice\response $response  Response object for this request.
  66       */
  67      public function execute($response) {
  68  
  69          $params = $this->parse_template();
  70          $tpid = $params['tool_proxy_id'];
  71          $configtype = $params['config_type'];
  72          $ok = (in_array($configtype, array('toolproxy', 'tool')));
  73          if ($ok) {
  74              $typeid = null;
  75              if (($configtype === 'tool') && is_numeric($tpid)) {
  76                  $typeid = $tpid;
  77              }
  78              $bubble = optional_param('bubble', '', PARAM_ALPHA);
  79              $ok = !empty($tpid) && $this->check_tool($typeid, $response->get_request_data(),
  80                  array(toolsettings::SCOPE_TOOL_SETTINGS));
  81          }
  82          if (!$ok) {
  83              $response->set_code(401);
  84          } else if (!empty($this->get_service()->get_tool_proxy())) {
  85              $ok = ($this->get_service()->get_tool_proxy()->guid === $tpid);
  86              $id = $this->get_service()->get_tool_proxy()->id;
  87          } else if (!empty($typeid)) {
  88              $id = -$typeid;
  89          } else {
  90              $ok = false;
  91              $response->set_code(404);
  92          }
  93          $contenttype = $response->get_accept();
  94          $simpleformat = !empty($contenttype) && ($contenttype == $this->formats[1]);
  95          if ($ok) {
  96              $ok = (empty($bubble) || ((($bubble == 'distinct') || ($bubble == 'all')))) &&
  97                 (!$simpleformat || empty($bubble) || ($bubble != 'all')) &&
  98                 (empty($bubble) || ($response->get_request_method() == 'GET'));
  99              if (!$ok) {
 100                  $response->set_code(406);
 101              }
 102          }
 103  
 104          if ($ok) {
 105              $systemsettings = lti_get_tool_settings($id);
 106              if ($response->get_request_method() == 'GET') {
 107                  $json = '';
 108                  if ($simpleformat) {
 109                      $response->set_content_type($this->formats[1]);
 110                      $json .= "{";
 111                  } else {
 112                      $response->set_content_type($this->formats[0]);
 113                      $json .= "{\n  \"@context\":\"http://purl.imsglobal.org/ctx/lti/v2/ToolSettings\",\n  \"@graph\":[\n";
 114                  }
 115                  $json .= toolsettings::settings_to_json($systemsettings, $simpleformat,
 116                      'ToolProxy', $this);
 117                  if ($simpleformat) {
 118                      $json .= "\n}";
 119                  } else {
 120                      $json .= "\n  ]\n}";
 121                  }
 122                  $response->set_body($json);
 123              } else { // PUT.
 124                  $settings = null;
 125                  if ($response->get_content_type() == $this->formats[0]) {
 126                      $json = json_decode($response->get_request_data());
 127                      $ok = !empty($json);
 128                      if ($ok) {
 129                          $ok = isset($json->{"@graph"}) && is_array($json->{"@graph"}) && (count($json->{"@graph"}) == 1) &&
 130                                ($json->{"@graph"}[0]->{"@type"} == 'ToolProxy');
 131                      }
 132                      if ($ok) {
 133                          $settings = $json->{"@graph"}[0]->custom;
 134                          unset($settings->{'@id'});
 135                      }
 136                  } else {  // Simple JSON.
 137                      $json = json_decode($response->get_request_data(), true);
 138                      $ok = !empty($json);
 139                      if ($ok) {
 140                          $ok = is_array($json);
 141                      }
 142                      if ($ok) {
 143                          $settings = $json;
 144                      }
 145                  }
 146                  if ($ok) {
 147                      lti_set_tool_settings($settings, $id);
 148                  } else {
 149                      $response->set_code(406);
 150                  }
 151              }
 152          }
 153  
 154      }
 155  
 156      /**
 157       * Parse a value for custom parameter substitution variables.
 158       *
 159       * @param string $value String to be parsed
 160       *
 161       * @return string
 162       */
 163      public function parse_value($value) {
 164          if (strpos($value, '$ToolProxy.custom.url') !== false) {
 165              $value = str_replace('$ToolProxy.custom.url', parent::get_endpoint(), $value);
 166          }
 167          return $value;
 168  
 169      }
 170  
 171  }