Search moodle.org's
Developer Documentation

See Release Notes

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

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

   1  <?php
   2  
   3  namespace IMSGlobal\LTI\ToolProvider;
   4  
   5  use IMSGlobal\LTI\ToolProvider\DataConnector\DataConnector;
   6  
   7  /**
   8   * Class to represent a tool consumer resource link share key
   9   *
  10   * @author  Stephen P Vickers <svickers@imsglobal.org>
  11   * @copyright  IMS Global Learning Consortium Inc
  12   * @date  2016
  13   * @version 3.0.2
  14   * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  15   */
  16  #[\AllowDynamicProperties]
  17  class ResourceLinkShareKey
  18  {
  19  
  20  /**
  21   * Maximum permitted life for a share key value.
  22   */
  23      const MAX_SHARE_KEY_LIFE = 168;  // in hours (1 week)
  24  /**
  25   * Default life for a share key value.
  26   */
  27      const DEFAULT_SHARE_KEY_LIFE = 24;  // in hours
  28  /**
  29   * Minimum length for a share key value.
  30   */
  31      const MIN_SHARE_KEY_LENGTH = 5;
  32  /**
  33   * Maximum length for a share key value.
  34   */
  35      const MAX_SHARE_KEY_LENGTH = 32;
  36  
  37  /**
  38   * ID for resource link being shared.
  39   *
  40   * @var string $resourceLinkId
  41   */
  42      public $resourceLinkId = null;
  43  /**
  44   * Length of share key.
  45   *
  46   * @var int $length
  47   */
  48      public $length = null;
  49  /**
  50   * Life of share key.
  51   *
  52   * @var int $life
  53   */
  54      public $life = null;  // in hours
  55  /**
  56   * Whether the sharing arrangement should be automatically approved when first used.
  57   *
  58   * @var boolean $autoApprove
  59   */
  60      public $autoApprove = false;
  61  /**
  62   * Date/time when the share key expires.
  63   *
  64   * @var int $expires
  65   */
  66      public $expires = null;
  67  
  68  /**
  69   * Share key value.
  70   *
  71   * @var string $id
  72   */
  73      private $id = null;
  74  /**
  75   * Data connector.
  76   *
  77   * @var DataConnector $dataConnector
  78   */
  79      private $dataConnector = null;
  80  
  81  /**
  82   * Class constructor.
  83   *
  84   * @param ResourceLink $resourceLink  Resource_Link object
  85   * @param string       $id      Value of share key (optional, default is null)
  86   */
  87      public function __construct($resourceLink, $id = null)
  88      {
  89  
  90          $this->initialize();
  91          $this->dataConnector = $resourceLink->getDataConnector();
  92          $this->resourceLinkId = $resourceLink->getRecordId();
  93          $this->id = $id;
  94          if (!empty($id)) {
  95              $this->load();
  96          }
  97  
  98      }
  99  
 100  /**
 101   * Initialise the resource link share key.
 102   */
 103      public function initialize()
 104      {
 105  
 106          $this->length = null;
 107          $this->life = null;
 108          $this->autoApprove = false;
 109          $this->expires = null;
 110  
 111      }
 112  
 113  /**
 114   * Initialise the resource link share key.
 115   *
 116   * Pseudonym for initialize().
 117   */
 118      public function initialise()
 119      {
 120  
 121          $this->initialize();
 122  
 123      }
 124  
 125  /**
 126   * Save the resource link share key to the database.
 127   *
 128   * @return boolean True if the share key was successfully saved
 129   */
 130      public function save()
 131      {
 132  
 133          if (empty($this->life)) {
 134              $this->life = self::DEFAULT_SHARE_KEY_LIFE;
 135          } else {
 136              $this->life = max(min($this->life, self::MAX_SHARE_KEY_LIFE), 0);
 137          }
 138          $this->expires = time() + ($this->life * 60 * 60);
 139          if (empty($this->id)) {
 140              if (empty($this->length) || !is_numeric($this->length)) {
 141                  $this->length = self::MAX_SHARE_KEY_LENGTH;
 142              } else {
 143                  $this->length = max(min($this->length, self::MAX_SHARE_KEY_LENGTH), self::MIN_SHARE_KEY_LENGTH);
 144              }
 145              $this->id = DataConnector::getRandomString($this->length);
 146          }
 147  
 148          return $this->dataConnector->saveResourceLinkShareKey($this);
 149  
 150      }
 151  
 152  /**
 153   * Delete the resource link share key from the database.
 154   *
 155   * @return boolean True if the share key was successfully deleted
 156   */
 157      public function delete()
 158      {
 159  
 160          return $this->dataConnector->deleteResourceLinkShareKey($this);
 161  
 162      }
 163  
 164  /**
 165   * Get share key value.
 166   *
 167   * @return string Share key value
 168   */
 169      public function getId()
 170      {
 171  
 172          return $this->id;
 173  
 174      }
 175  
 176  ###
 177  ###  PRIVATE METHOD
 178  ###
 179  
 180  /**
 181   * Load the resource link share key from the database.
 182   */
 183      private function load()
 184      {
 185  
 186          $this->initialize();
 187          $this->dataConnector->loadResourceLinkShareKey($this);
 188          if (!is_null($this->id)) {
 189              $this->length = strlen($this->id);
 190          }
 191          if (!is_null($this->expires)) {
 192              $this->life = ($this->expires - time()) / 60 / 60;
 193          }
 194  
 195      }
 196  
 197  }