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.

Differences Between: [Versions 39 and 311]

   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 scope(s) defined this service.
  93       *
  94       * @return array
  95       */
  96      public function get_scopes() {
  97          return [self::SCOPE_TOOL_SETTINGS];
  98      }
  99  
 100      /**
 101       * Get the distinct settings from each level by removing any duplicates from higher levels.
 102       *
 103       * @param array $systemsettings   System level settings
 104       * @param array $contextsettings  Context level settings
 105       * @param array $linksettings      Link level settings
 106       */
 107      public static function distinct_settings(&$systemsettings, &$contextsettings, $linksettings) {
 108  
 109          if (!empty($systemsettings)) {
 110              foreach ($systemsettings as $key => $value) {
 111                  if ((!empty($contextsettings) && array_key_exists($key, $contextsettings)) ||
 112                      (!empty($linksettings) && array_key_exists($key, $linksettings))) {
 113                      unset($systemsettings[$key]);
 114                  }
 115              }
 116          }
 117          if (!empty($contextsettings)) {
 118              foreach ($contextsettings as $key => $value) {
 119                  if (!empty($linksettings) && array_key_exists($key, $linksettings)) {
 120                      unset($contextsettings[$key]);
 121                  }
 122              }
 123          }
 124      }
 125  
 126      /**
 127       * Get the JSON representation of the settings.
 128       *
 129       * @param array $settings        Settings
 130       * @param boolean $simpleformat  <code>true</code> if simple JSON is to be returned
 131       * @param string $type           JSON-LD type
 132       * @param \mod_lti\local\ltiservice\resource_base $resource       Resource handling the request
 133       *
 134       * @return string
 135       */
 136      public static function settings_to_json($settings, $simpleformat, $type, $resource) {
 137  
 138          $json = '';
 139          if (!empty($resource)) {
 140              $indent = '';
 141              if (!$simpleformat) {
 142                  $json .= "    {\n      \"@type\":\"{$type}\",\n";
 143                  $json .= "      \"@id\":\"{$resource->get_endpoint()}\",\n";
 144                  $json .= "      \"custom\":{";
 145                  $indent = '      ';
 146              }
 147              $isfirst = true;
 148              if (!empty($settings)) {
 149                  foreach ($settings as $key => $value) {
 150                      if (!$isfirst) {
 151                          $json .= ',';
 152                      } else {
 153                          $isfirst = false;
 154                      }
 155                      $json .= "\n{$indent}  \"{$key}\":\"{$value}\"";
 156                  }
 157              }
 158              if (!$simpleformat) {
 159                  $json .= "\n{$indent}}\n    }";
 160              }
 161          }
 162  
 163          return $json;
 164  
 165      }
 166  
 167      /**
 168       * Adds form elements for membership add/edit page.
 169       *
 170       * @param \MoodleQuickForm $mform
 171       */
 172      public function get_configuration_options(&$mform) {
 173          $elementname = $this->get_component_id();
 174          $options = [
 175              get_string('notallow', $this->get_component_id()),
 176              get_string('allow', $this->get_component_id())
 177          ];
 178  
 179          $mform->addElement('select', $elementname, get_string($elementname, $this->get_component_id()), $options);
 180          $mform->setType($elementname, 'int');
 181          $mform->setDefault($elementname, 0);
 182          $mform->addHelpButton($elementname, $elementname, $this->get_component_id());
 183      }
 184  
 185      /**
 186       * Return an array of key/values to add to the launch parameters.
 187       *
 188       * @param string $messagetype 'basic-lti-launch-request' or 'ContentItemSelectionRequest'.
 189       * @param string $courseid The course id.
 190       * @param string $user The user id.
 191       * @param string $typeid The tool lti type id.
 192       * @param string $modlti The id of the lti activity.
 193       *
 194       * The type is passed to check the configuration
 195       * and not return parameters for services not used.
 196       *
 197       * @return array of key/value pairs to add as launch parameters.
 198       */
 199      public function get_launch_parameters($messagetype, $courseid, $user, $typeid, $modlti = null) {
 200          global $COURSE;
 201  
 202          $launchparameters = array();
 203          $tool = lti_get_type_type_config($typeid);
 204          if (isset($tool->{$this->get_component_id()})) {
 205              if ($tool->{$this->get_component_id()} == self::SERVICE_ENABLED && $this->is_used_in_context($typeid, $courseid)) {
 206                  $launchparameters['system_setting_url'] = '$ToolProxy.custom.url';
 207                  $launchparameters['context_setting_url'] = '$ToolProxyBinding.custom.url';
 208                  if ($messagetype === 'basic-lti-launch-request') {
 209                      $launchparameters['link_setting_url'] = '$LtiLink.custom.url';
 210                  }
 211              }
 212          }
 213          return $launchparameters;
 214      }
 215  
 216  }