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\MediaType;
   7  
   8  /**
   9   * Class to represent an LTI Tool Proxy
  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  GNU Lesser General Public License, version 3 (<http://www.gnu.org/licenses/lgpl.html>)
  16   */
  17  #[\AllowDynamicProperties]
  18  class ToolProxy
  19  {
  20  
  21  /**
  22   * Local id of tool consumer.
  23   *
  24   * @var string $id
  25   */
  26      public $id = null;
  27  
  28  /**
  29   * Tool Consumer for this tool proxy.
  30   *
  31   * @var ToolConsumer $consumer
  32   */
  33      private $consumer = null;
  34  /**
  35   * Tool Consumer ID for this tool proxy.
  36   *
  37   * @var int $consumerId
  38   */
  39      private $consumerId = null;
  40  /**
  41   * Consumer ID value.
  42   *
  43   * @var int $id
  44   */
  45      private $recordId = null;
  46  /**
  47   * Data connector object.
  48   *
  49   * @var DataConnector $dataConnector
  50   */
  51      private $dataConnector = null;
  52  /**
  53   * Tool Proxy document.
  54   *
  55   * @var MediaType\ToolProxy $toolProxy
  56   */
  57      private $toolProxy = null;
  58  
  59  /**
  60   * Class constructor.
  61   *
  62   * @param DataConnector   $dataConnector   Data connector
  63   * @param string                        $id              Tool Proxy ID (optional, default is null)
  64   */
  65      public function __construct($dataConnector, $id = null)
  66      {
  67  
  68          $this->initialize();
  69          $this->dataConnector = $dataConnector;
  70          if (!empty($id)) {
  71              $this->load($id);
  72          } else {
  73              $this->recordId = DataConnector::getRandomString(32);
  74          }
  75  
  76      }
  77  
  78  /**
  79   * Initialise the tool proxy.
  80   */
  81      public function initialize()
  82      {
  83  
  84          $this->id = null;
  85          $this->recordId = null;
  86          $this->toolProxy = null;
  87          $this->created = null;
  88          $this->updated = null;
  89  
  90      }
  91  
  92  /**
  93   * Initialise the tool proxy.
  94   *
  95   * Pseudonym for initialize().
  96   */
  97      public function initialise()
  98      {
  99  
 100          $this->initialize();
 101  
 102      }
 103  
 104  /**
 105   * Get the tool proxy record ID.
 106   *
 107   * @return int Tool Proxy record ID value
 108   */
 109      public function getRecordId()
 110      {
 111  
 112          return $this->recordId;
 113  
 114      }
 115  
 116  /**
 117   * Sets the tool proxy record ID.
 118   *
 119   * @param int $recordId  Tool Proxy record ID value
 120   */
 121      public function setRecordId($recordId)
 122      {
 123  
 124          $this->recordId = $recordId;
 125  
 126      }
 127  
 128  /**
 129   * Get tool consumer.
 130   *
 131   * @return ToolConsumer Tool consumer object for this context.
 132   */
 133      public function getConsumer()
 134      {
 135  
 136          if (is_null($this->consumer)) {
 137              $this->consumer = ToolConsumer::fromRecordId($this->consumerId, $this->getDataConnector());
 138          }
 139  
 140          return $this->consumer;
 141  
 142      }
 143  
 144  /**
 145   * Set tool consumer ID.
 146   *
 147   * @param int $consumerId  Tool Consumer ID for this resource link.
 148   */
 149      public function setConsumerId($consumerId)
 150      {
 151  
 152          $this->consumer = null;
 153          $this->consumerId = $consumerId;
 154  
 155      }
 156  
 157  /**
 158   * Get the data connector.
 159   *
 160   * @return DataConnector  Data connector object
 161   */
 162      public function getDataConnector()
 163      {
 164  
 165          return $this->dataConnector;
 166  
 167      }
 168  
 169  
 170  ###
 171  ###  PRIVATE METHOD
 172  ###
 173  
 174  /**
 175   * Load the tool proxy from the database.
 176   *
 177   * @param string  $id        The tool proxy id value
 178   *
 179   * @return boolean True if the tool proxy was successfully loaded
 180   */
 181      private function load($id)
 182      {
 183  
 184          $this->initialize();
 185          $this->id = $id;
 186          $ok = $this->dataConnector->loadToolProxy($this);
 187          if (!$ok) {
 188              $this->enabled = $autoEnable;
 189          }
 190  
 191          return $ok;
 192  
 193      }
 194  
 195  }