3 namespace IMSGlobal\LTI\ToolProvider
;
5 use IMSGlobal\LTI\ToolProvider\DataConnector\DataConnector
;
6 use IMSGlobal\LTI\ToolProvider\Service
;
9 * Class to represent a tool consumer context
11 * @author Stephen P Vickers <svickers@imsglobal.org>
12 * @copyright IMS Global Learning Consortium Inc
15 * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
21 * Context ID as supplied in the last connection request.
23 * @var string $ltiContextId
25 public $ltiContextId = null;
33 * Setting values (LTI parameters, custom parameters and local parameters).
35 * @var array $settings
37 public $settings = null;
45 * Date/time when the object was created.
49 public $created = null;
51 * Date/time when the object was last updated.
55 public $updated = null;
58 * Tool Consumer for this context.
60 * @var ToolConsumer $consumer
62 private $consumer = null;
64 * Tool Consumer ID for this context.
66 * @var int $consumerId
68 private $consumerId = null;
70 * ID for this context.
76 * Whether the settings value have changed since last saved.
78 * @var boolean $settingsChanged
80 private $settingsChanged = false;
82 * Data connector object or string.
84 * @var mixed $dataConnector
86 private $dataConnector = null;
91 public function __construct()
99 * Initialise the context.
101 public function initialize()
105 $this->settings
= array();
106 $this->created
= null;
107 $this->updated
= null;
112 * Initialise the context.
114 * Pseudonym for initialize().
116 public function initialise()
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);
133 $this->settingsChanged
= false;
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);
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();
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()
217 * Sets the context record ID.
219 * @return int $id Context record ID value
221 public function setRecordId($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];
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;
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
) {
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');
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);
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);
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');
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();
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;
425 * Class constructor from consumer.
427 * @param ToolConsumer $consumer Consumer instance
428 * @param string $ltiContextId LTI Context ID value
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)) {
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)
462 return $this->getDataConnector()->loadContext($this);