[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/lti/service/toolsettings/classes/local/resource/ -> systemsettings.php (source)

   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\resource;
  28  
  29  use ltiservice_toolsettings\local\service\toolsettings;
  30  
  31  defined('MOODLE_INTERNAL') || die();
  32  
  33  /**
  34   * A resource implementing the System-level (ToolProxy) Settings.
  35   *
  36   * @package    ltiservice_toolsettings
  37   * @since      Moodle 2.8
  38   * @copyright  2014 Vital Source Technologies http://vitalsource.com
  39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class systemsettings extends \mod_lti\local\ltiservice\resource_base {
  42  
  43      /**
  44       * Class constructor.
  45       *
  46       * @param ltiservice_toolsettings\local\service\toolsettings $service Service instance
  47       */
  48      public function __construct($service) {
  49  
  50          parent::__construct($service);
  51          $this->id = 'ToolProxySettings';
  52          $this->template = '/toolproxy/{tool_proxy_id}';
  53          $this->variables[] = 'ToolProxy.custom.url';
  54          $this->formats[] = 'application/vnd.ims.lti.v2.toolsettings+json';
  55          $this->formats[] = 'application/vnd.ims.lti.v2.toolsettings.simple+json';
  56          $this->methods[] = 'GET';
  57          $this->methods[] = 'PUT';
  58  
  59      }
  60  
  61      /**
  62       * Execute the request for this resource.
  63       *
  64       * @param mod_lti\local\ltiservice\response $response  Response object for this request.
  65       */
  66      public function execute($response) {
  67  
  68          $params = $this->parse_template();
  69          $tpid = $params['tool_proxy_id'];
  70          $bubble = optional_param('bubble', '', PARAM_ALPHA);
  71          $ok = !empty($tpid) && $this->check_tool_proxy($tpid, $response->get_request_data());
  72          if (!$ok) {
  73              $response->set_code(401);
  74          }
  75          $contenttype = $response->get_accept();
  76          $simpleformat = !empty($contenttype) && ($contenttype == $this->formats[1]);
  77          if ($ok) {
  78              $ok = (empty($bubble) || ((($bubble == 'distinct') || ($bubble == 'all')))) &&
  79                 (!$simpleformat || empty($bubble) || ($bubble != 'all')) &&
  80                 (empty($bubble) || ($response->get_request_method() == 'GET'));
  81              if (!$ok) {
  82                  $response->set_code(406);
  83              }
  84          }
  85  
  86          if ($ok) {
  87              $systemsettings = lti_get_tool_settings($this->get_service()->get_tool_proxy()->id);
  88              if ($response->get_request_method() == 'GET') {
  89                  $json = '';
  90                  if ($simpleformat) {
  91                      $response->set_content_type($this->formats[1]);
  92                      $json .= "{";
  93                  } else {
  94                      $response->set_content_type($this->formats[0]);
  95                      $json .= "{\n  \"@context\":\"http://purl.imsglobal.org/ctx/lti/v2/ToolSettings\",\n  \"@graph\":[\n";
  96                  }
  97                  $json .= toolsettings::settings_to_json($systemsettings, $simpleformat,
  98                      'ToolProxy', $this);
  99                  if ($simpleformat) {
 100                      $json .= "\n}";
 101                  } else {
 102                      $json .= "\n  ]\n}";
 103                  }
 104                  $response->set_body($json);
 105              } else { // PUT.
 106                  $settings = null;
 107                  if ($response->get_content_type() == $this->formats[0]) {
 108                      $json = json_decode($response->get_request_data());
 109                      $ok = !empty($json);
 110                      if ($ok) {
 111                          $ok = isset($json->{"@graph"}) && is_array($json->{"@graph"}) && (count($json->{"@graph"}) == 1) &&
 112                                ($json->{"@graph"}[0]->{"@type"} == 'ToolProxy');
 113                      }
 114                      if ($ok) {
 115                          $settings = $json->{"@graph"}[0]->custom;
 116                          unset($settings->{'@id'});
 117                      }
 118                  } else {  // Simple JSON.
 119                      $json = json_decode($response->get_request_data(), true);
 120                      $ok = !empty($json);
 121                      if ($ok) {
 122                          $ok = is_array($json);
 123                      }
 124                      if ($ok) {
 125                          $settings = $json;
 126                      }
 127                  }
 128                  if ($ok) {
 129                      lti_set_tool_settings($settings, $this->get_service()->get_tool_proxy()->id);
 130                  } else {
 131                      $response->set_code(406);
 132                  }
 133              }
 134          }
 135  
 136      }
 137  
 138      /**
 139       * Parse a value for custom parameter substitution variables.
 140       *
 141       * @param string $value String to be parsed
 142       *
 143       * @return string
 144       */
 145      public function parse_value($value) {
 146  
 147          $value = str_replace('$ToolProxy.custom.url', parent::get_endpoint(), $value);
 148  
 149          return $value;
 150  
 151      }
 152  
 153  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1