Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402]

   1  <?php
   2  
   3  namespace IMSGlobal\LTI\ToolProvider;
   4  
   5  use IMSGlobal\LTI\ToolProvider\DataConnector\DataConnector;
   6  use IMSGlobal\LTI\ToolProvider\Service;
   7  
   8  /**
   9   * Class to represent a tool consumer context
  10   *
  11   * @author  Stephen P Vickers <svickers@imsglobal.org>
  12   * @copyright  IMS Global Learning Consortium Inc
  13   * @date  2016
  14   * @version 3.0.2
  15   * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  16   */
  17  #[\AllowDynamicProperties]
  18  class Context
  19  {
  20  
  21  /**
  22   * Context ID as supplied in the last connection request.
  23   *
  24   * @var string $ltiContextId
  25   */
  26      public $ltiContextId = null;
  27  /**
  28   * Context title.
  29   *
  30   * @var string $title
  31   */
  32      public $title = null;
  33  /**
  34   * Setting values (LTI parameters, custom parameters and local parameters).
  35   *
  36   * @var array $settings
  37   */
  38      public $settings = null;
  39  /**
  40   * Context type.
  41   *
  42   * @var string $type
  43   */
  44      public $type = null;
  45  /**
  46   * Date/time when the object was created.
  47   *
  48   * @var int $created
  49   */
  50      public $created = null;
  51  /**
  52   * Date/time when the object was last updated.
  53   *
  54   * @var int $updated
  55   */
  56      public $updated = null;
  57  
  58  /**
  59   * Tool Consumer for this context.
  60   *
  61   * @var ToolConsumer $consumer
  62   */
  63      private $consumer = null;
  64  /**
  65   * Tool Consumer ID for this context.
  66   *
  67   * @var int $consumerId
  68   */
  69      private $consumerId = null;
  70  /**
  71   * ID for this context.
  72   *
  73   * @var int $id
  74   */
  75      private $id = null;
  76  /**
  77   * Whether the settings value have changed since last saved.
  78   *
  79   * @var boolean $settingsChanged
  80   */
  81      private $settingsChanged = false;
  82  /**
  83   * Data connector object or string.
  84   *
  85   * @var mixed $dataConnector
  86   */
  87      private $dataConnector = null;
  88  
  89  /**
  90   * Class constructor.
  91   */
  92      public function __construct()
  93      {
  94  
  95          $this->initialize();
  96  
  97      }
  98  
  99  /**
 100   * Initialise the context.
 101   */
 102      public function initialize()
 103      {
 104  
 105          $this->title = '';
 106          $this->settings = array();
 107          $this->created = null;
 108          $this->updated = null;
 109  
 110      }
 111  
 112  /**
 113   * Initialise the context.
 114   *
 115   * Pseudonym for initialize().
 116   */
 117      public function initialise()
 118      {
 119  
 120          $this->initialize();
 121  
 122      }
 123  
 124  /**
 125   * Save the context to the database.
 126   *
 127   * @return boolean True if the context was successfully saved.
 128   */
 129      public function save()
 130      {
 131  
 132          $ok = $this->getDataConnector()->saveContext($this);
 133          if ($ok) {
 134              $this->settingsChanged = false;
 135          }
 136  
 137          return $ok;
 138  
 139      }
 140  
 141  /**
 142   * Delete the context from the database.
 143   *
 144   * @return boolean True if the context was successfully deleted.
 145   */
 146      public function delete()
 147      {
 148  
 149          return $this->getDataConnector()->deleteContext($this);
 150  
 151      }
 152  
 153  /**
 154   * Get tool consumer.
 155   *
 156   * @return ToolConsumer Tool consumer object for this context.
 157   */
 158      public function getConsumer()
 159      {
 160  
 161          if (is_null($this->consumer)) {
 162              $this->consumer = ToolConsumer::fromRecordId($this->consumerId, $this->getDataConnector());
 163          }
 164  
 165          return $this->consumer;
 166  
 167      }
 168  /**
 169   * Set tool consumer ID.
 170   *
 171   * @param int $consumerId  Tool Consumer ID for this resource link.
 172   */
 173      public function setConsumerId($consumerId)
 174      {
 175  
 176          $this->consumer = null;
 177          $this->consumerId = $consumerId;
 178  
 179      }
 180  
 181  /**
 182   * Get tool consumer key.
 183   *
 184   * @return string Consumer key value for this context.
 185   */
 186      public function getKey()
 187      {
 188  
 189          return $this->getConsumer()->getKey();
 190  
 191      }
 192  
 193  /**
 194   * Get context ID.
 195   *
 196   * @return string ID for this context.
 197   */
 198      public function getId()
 199      {
 200  
 201          return $this->ltiContextId;
 202  
 203      }
 204  
 205  /**
 206   * Get the context record ID.
 207   *
 208   * @return int Context record ID value
 209   */
 210      public function getRecordId()
 211      {
 212  
 213          return $this->id;
 214  
 215      }
 216  
 217  /**
 218   * Sets the context record ID.
 219   *
 220   * @return int $id  Context record ID value
 221   */
 222      public function setRecordId($id)
 223      {
 224  
 225          $this->id = $id;
 226  
 227      }
 228  
 229  /**
 230   * Get the data connector.
 231   *
 232   * @return mixed Data connector object or string
 233   */
 234      public function getDataConnector()
 235      {
 236  
 237          return $this->dataConnector;
 238  
 239      }
 240  
 241  /**
 242   * Get a setting value.
 243   *
 244   * @param string $name    Name of setting
 245   * @param string $default Value to return if the setting does not exist (optional, default is an empty string)
 246   *
 247   * @return string Setting value
 248   */
 249      public function getSetting($name, $default = '')
 250      {
 251  
 252          if (array_key_exists($name, $this->settings)) {
 253              $value = $this->settings[$name];
 254          } else {
 255              $value = $default;
 256          }
 257  
 258          return $value;
 259  
 260      }
 261  
 262  /**
 263   * Set a setting value.
 264   *
 265   * @param string $name  Name of setting
 266   * @param string $value Value to set, use an empty value to delete a setting (optional, default is null)
 267   */
 268      public function setSetting($name, $value = null)
 269      {
 270  
 271          $old_value = $this->getSetting($name);
 272          if ($value !== $old_value) {
 273              if (!empty($value)) {
 274                  $this->settings[$name] = $value;
 275              } else {
 276                  unset($this->settings[$name]);
 277              }
 278              $this->settingsChanged = true;
 279          }
 280  
 281      }
 282  
 283  /**
 284   * Get an array of all setting values.
 285   *
 286   * @return array Associative array of setting values
 287   */
 288      public function getSettings()
 289      {
 290  
 291          return $this->settings;
 292  
 293      }
 294  
 295  /**
 296   * Set an array of all setting values.
 297   *
 298   * @param array $settings Associative array of setting values
 299   */
 300      public function setSettings($settings)
 301      {
 302  
 303          $this->settings = $settings;
 304  
 305      }
 306  
 307  /**
 308   * Save setting values.
 309   *
 310   * @return boolean True if the settings were successfully saved
 311   */
 312      public function saveSettings()
 313      {
 314  
 315          if ($this->settingsChanged) {
 316              $ok = $this->save();
 317          } else {
 318              $ok = true;
 319          }
 320  
 321          return $ok;
 322  
 323      }
 324  
 325  /**
 326   * Check if the Tool Settings service is supported.
 327   *
 328   * @return boolean True if this context supports the Tool Settings service
 329   */
 330      public function hasToolSettingsService()
 331      {
 332  
 333          $url = $this->getSetting('custom_context_setting_url');
 334  
 335          return !empty($url);
 336  
 337      }
 338  
 339  /**
 340   * Get Tool Settings.
 341   *
 342   * @param int      $mode       Mode for request (optional, default is current level only)
 343   * @param boolean  $simple     True if all the simple media type is to be used (optional, default is true)
 344   *
 345   * @return mixed The array of settings if successful, otherwise false
 346   */
 347      public function getToolSettings($mode = Service\ToolSettings::MODE_CURRENT_LEVEL, $simple = true)
 348      {
 349  
 350          $url = $this->getSetting('custom_context_setting_url');
 351          $service = new Service\ToolSettings($this, $url, $simple);
 352          $response = $service->get($mode);
 353  
 354          return $response;
 355  
 356      }
 357  
 358  /**
 359   * Perform a Tool Settings service request.
 360   *
 361   * @param array    $settings   An associative array of settings (optional, default is none)
 362   *
 363   * @return boolean True if action was successful, otherwise false
 364   */
 365      public function setToolSettings($settings = array())
 366      {
 367  
 368          $url = $this->getSetting('custom_context_setting_url');
 369          $service = new Service\ToolSettings($this, $url);
 370          $response = $service->set($settings);
 371  
 372          return $response;
 373  
 374      }
 375  
 376  /**
 377   * Check if the Membership service is supported.
 378   *
 379   * @return boolean True if this context supports the Membership service
 380   */
 381      public function hasMembershipService()
 382      {
 383  
 384          $url = $this->getSetting('custom_context_memberships_url');
 385  
 386          return !empty($url);
 387  
 388      }
 389  
 390  /**
 391   * Get Memberships.
 392   *
 393   * @return mixed The array of User objects if successful, otherwise false
 394   */
 395      public function getMembership()
 396      {
 397  
 398          $url = $this->getSetting('custom_context_memberships_url');
 399          $service = new Service\Membership($this, $url);
 400          $response = $service->get();
 401  
 402          return $response;
 403  
 404      }
 405  
 406  /**
 407   * Load the context from the database.
 408   *
 409   * @param int             $id               Record ID of context
 410   * @param DataConnector   $dataConnector    Database connection object
 411   *
 412   * @return Context    Context object
 413   */
 414      public static function fromRecordId($id, $dataConnector)
 415      {
 416  
 417          $context = new Context();
 418          $context->dataConnector = $dataConnector;
 419          $context->load($id);
 420  
 421          return $context;
 422  
 423      }
 424  
 425  /**
 426   * Class constructor from consumer.
 427   *
 428   * @param ToolConsumer $consumer Consumer instance
 429   * @param string $ltiContextId LTI Context ID value
 430   * @return Context
 431   */
 432      public static function fromConsumer($consumer, $ltiContextId)
 433      {
 434  
 435          $context = new Context();
 436          $context->consumer = $consumer;
 437          $context->dataConnector = $consumer->getDataConnector();
 438          $context->ltiContextId = $ltiContextId;
 439          if (!empty($ltiContextId)) {
 440              $context->load();
 441          }
 442  
 443          return $context;
 444  
 445      }
 446  
 447  ###
 448  ###  PRIVATE METHODS
 449  ###
 450  
 451  /**
 452   * Load the context from the database.
 453   *
 454   * @param int $id     Record ID of context (optional, default is null)
 455   *
 456   * @return boolean True if context was successfully loaded
 457   */
 458      private function load($id = null)
 459      {
 460  
 461          $this->initialize();
 462          $this->id = $id;
 463          return $this->getDataConnector()->loadContext($this);
 464  
 465      }
 466  
 467  }