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