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.

Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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   * This file contains a class definition for the Tool Settings service
  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\service;
  28  
  29  defined('MOODLE_INTERNAL') || die();
  30  
  31  /**
  32   * A service implementing Tool Settings.
  33   *
  34   * @package    ltiservice_toolsettings
  35   * @since      Moodle 2.8
  36   * @copyright  2014 Vital Source Technologies http://vitalsource.com
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class toolsettings extends \mod_lti\local\ltiservice\service_base {
  40  
  41      /** Scope for managing tool settings */
  42      const SCOPE_TOOL_SETTINGS = 'https://purl.imsglobal.org/spec/lti-ts/scope/toolsetting';
  43  
  44      /**
  45       * Class constructor.
  46       */
  47      public function __construct() {
  48  
  49          parent::__construct();
  50          $this->id = 'toolsettings';
  51          $this->name = 'Tool Settings';
  52  
  53      }
  54  
  55      /**
  56       * Get the resources for this service.
  57       *
  58       * @return array
  59       */
  60      public function get_resources() {
  61  
  62          if (empty($this->resources)) {
  63              $this->resources = array();
  64              $this->resources[] = new \ltiservice_toolsettings\local\resources\systemsettings($this);
  65              $this->resources[] = new \ltiservice_toolsettings\local\resources\contextsettings($this);
  66              $this->resources[] = new \ltiservice_toolsettings\local\resources\linksettings($this);
  67          }
  68  
  69          return $this->resources;
  70  
  71      }
  72  
  73      /**
  74       * Get the scope(s) permitted for the tool relevant to this service.
  75       *
  76       * @return array
  77       */
  78      public function get_permitted_scopes() {
  79  
  80          $scopes = array();
  81          $ok = !empty($this->get_type());
  82          if ($ok && isset($this->get_typeconfig()[$this->get_component_id()]) &&
  83              ($this->get_typeconfig()[$this->get_component_id()] == parent::SERVICE_ENABLED)) {
  84              $scopes[] = self::SCOPE_TOOL_SETTINGS;
  85          }
  86  
  87          return $scopes;
  88  
  89      }
  90  
  91      /**
  92       * Get the distinct settings from each level by removing any duplicates from higher levels.
  93       *
  94       * @param array $systemsettings   System level settings
  95       * @param array $contextsettings  Context level settings
  96       * @param array $linksettings      Link level settings
  97       */
  98      public static function distinct_settings(&$systemsettings, &$contextsettings, $linksettings) {
  99  
 100          if (!empty($systemsettings)) {
 101              foreach ($systemsettings as $key => $value) {
 102                  if ((!empty($contextsettings) && array_key_exists($key, $contextsettings)) ||
 103                      (!empty($linksettings) && array_key_exists($key, $linksettings))) {
 104                      unset($systemsettings[$key]);
 105                  }
 106              }
 107          }
 108          if (!empty($contextsettings)) {
 109              foreach ($contextsettings as $key => $value) {
 110                  if (!empty($linksettings) && array_key_exists($key, $linksettings)) {
 111                      unset($contextsettings[$key]);
 112                  }
 113              }
 114          }
 115      }
 116  
 117      /**
 118       * Get the JSON representation of the settings.
 119       *
 120       * @param array $settings        Settings
 121       * @param boolean $simpleformat  <code>true</code> if simple JSON is to be returned
 122       * @param string $type           JSON-LD type
 123       * @param \mod_lti\local\ltiservice\resource_base $resource       Resource handling the request
 124       *
 125       * @return string
 126       */
 127      public static function settings_to_json($settings, $simpleformat, $type, $resource) {
 128  
 129          $json = '';
 130          if (!empty($resource)) {
 131              $indent = '';
 132              if (!$simpleformat) {
 133                  $json .= "    {\n      \"@type\":\"{$type}\",\n";
 134                  $json .= "      \"@id\":\"{$resource->get_endpoint()}\",\n";
 135                  $json .= "      \"custom\":{";
 136                  $indent = '      ';
 137              }
 138              $isfirst = true;
 139              if (!empty($settings)) {
 140                  foreach ($settings as $key => $value) {
 141                      if (!$isfirst) {
 142                          $json .= ',';
 143                      } else {
 144                          $isfirst = false;
 145                      }
 146                      $json .= "\n{$indent}  \"{$key}\":\"{$value}\"";
 147                  }
 148              }
 149              if (!$simpleformat) {
 150                  $json .= "\n{$indent}}\n    }";
 151              }
 152          }
 153  
 154          return $json;
 155  
 156      }
 157  
 158      /**
 159       * Adds form elements for membership add/edit page.
 160       *
 161       * @param \MoodleQuickForm $mform
 162       */
 163      public function get_configuration_options(&$mform) {
 164          $elementname = $this->get_component_id();
 165          $options = [
 166              get_string('notallow', $this->get_component_id()),
 167              get_string('allow', $this->get_component_id())
 168          ];
 169  
 170          $mform->addElement('select', $elementname, get_string($elementname, $this->get_component_id()), $options);
 171          $mform->setType($elementname, 'int');
 172          $mform->setDefault($elementname, 0);
 173          $mform->addHelpButton($elementname, $elementname, $this->get_component_id());
 174      }
 175  
 176      /**
 177       * Return an array of key/values to add to the launch parameters.
 178       *
 179       * @param string $messagetype 'basic-lti-launch-request' or 'ContentItemSelectionRequest'.
 180       * @param string $courseid The course id.
 181       * @param string $user The user id.
 182       * @param string $typeid The tool lti type id.
 183       * @param string $modlti The id of the lti activity.
 184       *
 185       * The type is passed to check the configuration
 186       * and not return parameters for services not used.
 187       *
 188       * @return array of key/value pairs to add as launch parameters.
 189       */
 190      public function get_launch_parameters($messagetype, $courseid, $user, $typeid, $modlti = null) {
 191          global $COURSE;
 192  
 193          $launchparameters = array();
 194          $tool = lti_get_type_type_config($typeid);
 195          if (isset($tool->{$this->get_component_id()})) {
 196              if ($tool->{$this->get_component_id()} == self::SERVICE_ENABLED && $this->is_used_in_context($typeid, $courseid)) {
 197                  $launchparameters['system_setting_url'] = '$ToolProxy.custom.url';
 198                  $launchparameters['context_setting_url'] = '$ToolProxyBinding.custom.url';
 199                  if ($messagetype === 'basic-lti-launch-request') {
 200                      $launchparameters['link_setting_url'] = '$LtiLink.custom.url';
 201                  }
 202              }
 203          }
 204          return $launchparameters;
 205      }
 206  
 207  }