MDL-63101 cache: Improve accuracy of cache event invalidation
[moodle.git] / lib / ltiprovider / src / ToolProvider / Context.php
blob5a0c784f4406b928905fe0f71f64645d91315f1b
1 <?php
3 namespace IMSGlobal\LTI\ToolProvider;
5 use IMSGlobal\LTI\ToolProvider\DataConnector\DataConnector;
6 use IMSGlobal\LTI\ToolProvider\Service;
8 /**
9 * Class to represent a tool consumer context
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
17 class Context
20 /**
21 * Context ID as supplied in the last connection request.
23 * @var string $ltiContextId
25 public $ltiContextId = null;
26 /**
27 * Context title.
29 * @var string $title
31 public $title = null;
32 /**
33 * Setting values (LTI parameters, custom parameters and local parameters).
35 * @var array $settings
37 public $settings = null;
38 /**
39 * Context type.
41 * @var string $type
43 public $type = null;
44 /**
45 * Date/time when the object was created.
47 * @var int $created
49 public $created = null;
50 /**
51 * Date/time when the object was last updated.
53 * @var int $updated
55 public $updated = null;
57 /**
58 * Tool Consumer for this context.
60 * @var ToolConsumer $consumer
62 private $consumer = null;
63 /**
64 * Tool Consumer ID for this context.
66 * @var int $consumerId
68 private $consumerId = null;
69 /**
70 * ID for this context.
72 * @var int $id
74 private $id = null;
75 /**
76 * Whether the settings value have changed since last saved.
78 * @var boolean $settingsChanged
80 private $settingsChanged = false;
81 /**
82 * Data connector object or string.
84 * @var mixed $dataConnector
86 private $dataConnector = null;
88 /**
89 * Class constructor.
91 public function __construct()
94 $this->initialize();
98 /**
99 * Initialise the context.
101 public function initialize()
104 $this->title = '';
105 $this->settings = array();
106 $this->created = null;
107 $this->updated = null;
112 * Initialise the context.
114 * Pseudonym for initialize().
116 public function initialise()
119 $this->initialize();
124 * Save the context to the database.
126 * @return boolean True if the context was successfully saved.
128 public function save()
131 $ok = $this->getDataConnector()->saveContext($this);
132 if ($ok) {
133 $this->settingsChanged = false;
136 return $ok;
141 * Delete the context from the database.
143 * @return boolean True if the context was successfully deleted.
145 public function delete()
148 return $this->getDataConnector()->deleteContext($this);
153 * Get tool consumer.
155 * @return ToolConsumer Tool consumer object for this context.
157 public function getConsumer()
160 if (is_null($this->consumer)) {
161 $this->consumer = ToolConsumer::fromRecordId($this->consumerId, $this->getDataConnector());
164 return $this->consumer;
168 * Set tool consumer ID.
170 * @param int $consumerId Tool Consumer ID for this resource link.
172 public function setConsumerId($consumerId)
175 $this->consumer = null;
176 $this->consumerId = $consumerId;
181 * Get tool consumer key.
183 * @return string Consumer key value for this context.
185 public function getKey()
188 return $this->getConsumer()->getKey();
193 * Get context ID.
195 * @return string ID for this context.
197 public function getId()
200 return $this->ltiContextId;
205 * Get the context record ID.
207 * @return int Context record ID value
209 public function getRecordId()
212 return $this->id;
217 * Sets the context record ID.
219 * @return int $id Context record ID value
221 public function setRecordId($id)
224 $this->id = $id;
229 * Get the data connector.
231 * @return mixed Data connector object or string
233 public function getDataConnector()
236 return $this->dataConnector;
241 * Get a setting value.
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)
246 * @return string Setting value
248 public function getSetting($name, $default = '')
251 if (array_key_exists($name, $this->settings)) {
252 $value = $this->settings[$name];
253 } else {
254 $value = $default;
257 return $value;
262 * Set a setting value.
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)
267 public function setSetting($name, $value = null)
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]);
277 $this->settingsChanged = true;
283 * Get an array of all setting values.
285 * @return array Associative array of setting values
287 public function getSettings()
290 return $this->settings;
295 * Set an array of all setting values.
297 * @param array $settings Associative array of setting values
299 public function setSettings($settings)
302 $this->settings = $settings;
307 * Save setting values.
309 * @return boolean True if the settings were successfully saved
311 public function saveSettings()
314 if ($this->settingsChanged) {
315 $ok = $this->save();
316 } else {
317 $ok = true;
320 return $ok;
325 * Check if the Tool Settings service is supported.
327 * @return boolean True if this context supports the Tool Settings service
329 public function hasToolSettingsService()
332 $url = $this->getSetting('custom_context_setting_url');
334 return !empty($url);
339 * Get Tool Settings.
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)
344 * @return mixed The array of settings if successful, otherwise false
346 public function getToolSettings($mode = Service\ToolSettings::MODE_CURRENT_LEVEL, $simple = true)
349 $url = $this->getSetting('custom_context_setting_url');
350 $service = new Service\ToolSettings($this, $url, $simple);
351 $response = $service->get($mode);
353 return $response;
358 * Perform a Tool Settings service request.
360 * @param array $settings An associative array of settings (optional, default is none)
362 * @return boolean True if action was successful, otherwise false
364 public function setToolSettings($settings = array())
367 $url = $this->getSetting('custom_context_setting_url');
368 $service = new Service\ToolSettings($this, $url);
369 $response = $service->set($settings);
371 return $response;
376 * Check if the Membership service is supported.
378 * @return boolean True if this context supports the Membership service
380 public function hasMembershipService()
383 $url = $this->getSetting('custom_context_memberships_url');
385 return !empty($url);
390 * Get Memberships.
392 * @return mixed The array of User objects if successful, otherwise false
394 public function getMembership()
397 $url = $this->getSetting('custom_context_memberships_url');
398 $service = new Service\Membership($this, $url);
399 $response = $service->get();
401 return $response;
406 * Load the context from the database.
408 * @param int $id Record ID of context
409 * @param DataConnector $dataConnector Database connection object
411 * @return Context Context object
413 public static function fromRecordId($id, $dataConnector)
416 $context = new Context();
417 $context->dataConnector = $dataConnector;
418 $context->load($id);
420 return $context;
425 * Class constructor from consumer.
427 * @param ToolConsumer $consumer Consumer instance
428 * @param string $ltiContextId LTI Context ID value
429 * @return Context
431 public static function fromConsumer($consumer, $ltiContextId)
434 $context = new Context();
435 $context->consumer = $consumer;
436 $context->dataConnector = $consumer->getDataConnector();
437 $context->ltiContextId = $ltiContextId;
438 if (!empty($ltiContextId)) {
439 $context->load();
442 return $context;
447 ### PRIVATE METHODS
451 * Load the context from the database.
453 * @param int $id Record ID of context (optional, default is null)
455 * @return boolean True if context was successfully loaded
457 private function load($id = null)
460 $this->initialize();
461 $this->id = $id;
462 return $this->getDataConnector()->loadContext($this);