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 Context 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  namespace ltiservice_toolsettings\local\resources;
  27  
  28  use ltiservice_toolsettings\local\service\toolsettings;
  29  
  30  defined('MOODLE_INTERNAL') || die();
  31  
  32  /**
  33   * A resource implementing the Context-level (ToolProxyBinding) Settings.
  34   *
  35   * @package    ltiservice_toolsettings
  36   * @since      Moodle 2.8
  37   * @copyright  2014 Vital Source Technologies http://vitalsource.com
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class linksettings extends \mod_lti\local\ltiservice\resource_base {
  41  
  42      /**
  43       * Class constructor.
  44       *
  45       * @param \mod_lti\local\ltiservice\service_base $service Service instance
  46       */
  47      public function __construct($service) {
  48  
  49          parent::__construct($service);
  50          $this->id = 'LtiLinkSettings';
  51          $this->template = '/links/{link_id}(/custom)';
  52          $this->variables[] = 'LtiLink.custom.url';
  53          $this->formats[] = 'application/vnd.ims.lti.v2.toolsettings+json';
  54          $this->formats[] = 'application/vnd.ims.lti.v2.toolsettings.simple+json';
  55          $this->methods[] = 'GET';
  56          $this->methods[] = 'PUT';
  57  
  58      }
  59  
  60      /**
  61       * Execute the request for this resource.
  62       *
  63       * @param \mod_lti\local\ltiservice\response $response  Response object for this request.
  64       */
  65      public function execute($response) {
  66          global $DB, $COURSE;
  67  
  68          $params = $this->parse_template();
  69          $linkid = $params['link_id'];
  70          $bubble = optional_param('bubble', '', PARAM_ALPHA);
  71          $contenttype = $response->get_accept();
  72          $simpleformat = !empty($contenttype) && ($contenttype == $this->formats[1]);
  73          $ok = (empty($bubble) || ((($bubble == 'distinct') || ($bubble == 'all')))) &&
  74               (!$simpleformat || empty($bubble) || ($bubble != 'all')) &&
  75               (empty($bubble) || ($response->get_request_method() == self::HTTP_GET));
  76          if (!$ok) {
  77              $response->set_code(406);
  78          }
  79  
  80          $systemsetting = null;
  81          $contextsetting = null;
  82          $lti = null;
  83          if ($ok) {
  84              $ok = !empty($linkid);
  85              if ($ok) {
  86                  $lti = $DB->get_record('lti', array('id' => $linkid), 'course,typeid', MUST_EXIST);
  87                  $ok = $this->check_tool($lti->typeid, $response->get_request_data(),
  88                      array(toolsettings::SCOPE_TOOL_SETTINGS));
  89              }
  90              if (!$ok) {
  91                  $response->set_code(401);
  92              }
  93          }
  94          if ($ok) {
  95              if (!empty($this->get_service()->get_tool_proxy())) {
  96                  $id = $this->get_service()->get_tool_proxy()->id;
  97              } else {
  98                  $id = -$this->get_service()->get_type()->id;
  99              }
 100              if ($response->get_request_method() == 'GET') {
 101                  $linksettings = lti_get_tool_settings($id, $lti->course, $linkid);
 102                  if (!empty($bubble)) {
 103                      $contextsetting = new contextsettings($this->get_service());
 104                      if ($COURSE == 'site') {
 105                          $contextsetting->params['context_type'] = 'Group';
 106                      } else {
 107                          $contextsetting->params['context_type'] = 'CourseSection';
 108                      }
 109                      $contextsetting->params['context_id'] = $lti->course;
 110                      if ($id >= 0) {
 111                          $contextsetting->params['vendor_code'] = $this->get_service()->get_tool_proxy()->vendorcode;
 112                      } else {
 113                          $contextsetting->params['vendor_code'] = 'tool';
 114                      }
 115                      $contextsetting->params['product_code'] = abs($id);
 116                      $contextsettings = lti_get_tool_settings($id, $lti->course);
 117                      $systemsetting = new systemsettings($this->get_service());
 118                      if ($id >= 0) {
 119                          $systemsetting->params['config_type'] = 'toolproxy';
 120                      } else {
 121                          $systemsetting->params['config_type'] = 'tool';
 122                      }
 123                      $systemsetting->params['tool_proxy_id'] = abs($id);
 124                      $systemsettings = lti_get_tool_settings($id);
 125                      if ($bubble == 'distinct') {
 126                          toolsettings::distinct_settings($systemsettings, $contextsettings, $linksettings);
 127                      }
 128                  } else {
 129                      $contextsettings = null;
 130                      $systemsettings = null;
 131                  }
 132                  $json = '';
 133                  if ($simpleformat) {
 134                      $response->set_content_type($this->formats[1]);
 135                      $json .= "{";
 136                  } else {
 137                      $response->set_content_type($this->formats[0]);
 138                      $json .= "{\n  \"@context\":\"http://purl.imsglobal.org/ctx/lti/v2/ToolSettings\",\n  \"@graph\":[\n";
 139                  }
 140                  $settings = toolsettings::settings_to_json($systemsettings, $simpleformat, 'ToolProxy', $systemsetting);
 141                  $json .= $settings;
 142                  $isfirst = strlen($settings) <= 0;
 143                  $settings = toolsettings::settings_to_json($contextsettings, $simpleformat, 'ToolProxyBinding', $contextsetting);
 144                  if (strlen($settings) > 0) {
 145                      if (!$isfirst) {
 146                          $json .= ",";
 147                          if (!$simpleformat) {
 148                              $json .= "\n";
 149                          }
 150                      }
 151                      $isfirst = false;
 152                  }
 153                  $json .= $settings;
 154                  $settings = toolsettings::settings_to_json($linksettings, $simpleformat, 'LtiLink', $this);
 155                  if ((strlen($settings) > 0) && !$isfirst) {
 156                      $json .= ",";
 157                      if (!$simpleformat) {
 158                          $json .= "\n";
 159                      }
 160                  }
 161                  $json .= $settings;
 162                  if ($simpleformat) {
 163                      $json .= "\n}";
 164                  } else {
 165                      $json .= "\n  ]\n}";
 166                  }
 167                  $response->set_body($json);
 168              } else { // PUT.
 169                  $settings = null;
 170                  if ($response->get_content_type() == $this->formats[0]) {
 171                      $json = json_decode($response->get_request_data());
 172                      $ok = !empty($json);
 173                      if ($ok) {
 174                          $ok = isset($json->{"@graph"}) && is_array($json->{"@graph"}) && (count($json->{"@graph"}) == 1) &&
 175                                ($json->{"@graph"}[0]->{"@type"} == 'LtiLink');
 176                      }
 177                      if ($ok) {
 178                          $settings = $json->{"@graph"}[0]->custom;
 179                          unset($settings->{'@id'});
 180                      }
 181                  } else {  // Simple JSON.
 182                      $json = json_decode($response->get_request_data(), true);
 183                      $ok = !empty($json);
 184                      if ($ok) {
 185                          $ok = is_array($json);
 186                      }
 187                      if ($ok) {
 188                          $settings = $json;
 189                      }
 190                  }
 191                  if ($ok) {
 192                      lti_set_tool_settings($settings, $id, $lti->course, $linkid);
 193                  } else {
 194                      $response->set_code(406);
 195                  }
 196              }
 197          }
 198      }
 199  
 200      /**
 201       * Parse a value for custom parameter substitution variables.
 202       *
 203       * @param string $value String to be parsed
 204       *
 205       * @return string
 206       */
 207      public function parse_value($value) {
 208  
 209          if (strpos($value, '$LtiLink.custom.url') !== false) {
 210              $id = optional_param('id', 0, PARAM_INT); // Course Module ID.
 211              if (!empty($id)) {
 212                  $cm = get_coursemodule_from_id('lti', $id, 0, false, MUST_EXIST);
 213                  $this->params['link_id'] = $cm->instance;
 214              }
 215              $value = str_replace('$LtiLink.custom.url', parent::get_endpoint(), $value);
 216          }
 217          return $value;
 218  
 219      }
 220  
 221  }