Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 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 310 and 402] [Versions 310 and 403]

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