ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Context.php
Go to the documentation of this file.
1 <?php
2 
4 
7 
17 class Context
18 {
19 
25  public $ltiContextId = null;
31  public $title = null;
37  public $settings = null;
43  public $created = null;
49  public $updated = null;
50 
56  private $consumer = null;
62  private $consumerId = null;
68  private $id = null;
74  private $settingsChanged = false;
80  private $dataConnector = null;
81 
85  public function __construct()
86  {
87 
88  $this->initialize();
89 
90  }
91 
95  public function initialize()
96  {
97 
98  $this->title = '';
99  $this->settings = array();
100  $this->created = null;
101  $this->updated = null;
102 
103  }
104 
110  public function initialise()
111  {
112 
113  $this->initialize();
114 
115  }
116 
122  public function save()
123  {
124 
125  $ok = $this->getDataConnector()->saveContext($this);
126  if ($ok) {
127  $this->settingsChanged = false;
128  }
129 
130  return $ok;
131 
132  }
133 
139  public function delete()
140  {
141 
142  return $this->getDataConnector()->deleteContext($this);
143 
144  }
145 
151  public function getConsumer()
152  {
153 
154  if (is_null($this->consumer)) {
155  $this->consumer = ToolConsumer::fromRecordId($this->consumerId, $this->getDataConnector());
156  }
157 
158  return $this->consumer;
159 
160  }
166  public function setConsumerId($consumerId)
167  {
168 
169  $this->consumer = null;
170  $this->consumerId = $consumerId;
171 
172  }
173 
179  public function getKey()
180  {
181 
182  return $this->getConsumer()->getKey();
183 
184  }
185 
191  public function getId()
192  {
193 
194  return $this->ltiContextId;
195 
196  }
197 
203  public function getRecordId()
204  {
205 
206  return $this->id;
207 
208  }
209 
215  public function setRecordId($id)
216  {
217 
218  $this->id = $id;
219 
220  }
221 
227  public function getDataConnector()
228  {
229 
230  return $this->dataConnector;
231 
232  }
233 
242  public function getSetting($name, $default = '')
243  {
244 
245  if (array_key_exists($name, $this->settings)) {
246  $value = $this->settings[$name];
247  } else {
248  $value = $default;
249  }
250 
251  return $value;
252 
253  }
254 
261  public function setSetting($name, $value = null)
262  {
263 
264  $old_value = $this->getSetting($name);
265  if ($value !== $old_value) {
266  if (!empty($value)) {
267  $this->settings[$name] = $value;
268  } else {
269  unset($this->settings[$name]);
270  }
271  $this->settingsChanged = true;
272  }
273 
274  }
275 
281  public function getSettings()
282  {
283 
284  return $this->settings;
285 
286  }
287 
293  public function setSettings($settings)
294  {
295 
296  $this->settings = $settings;
297 
298  }
299 
305  public function saveSettings()
306  {
307 
308  if ($this->settingsChanged) {
309  $ok = $this->save();
310  } else {
311  $ok = true;
312  }
313 
314  return $ok;
315 
316  }
317 
323  public function hasToolSettingsService()
324  {
325 
326  $url = $this->getSetting('custom_context_setting_url');
327 
328  return !empty($url);
329 
330  }
331 
340  public function getToolSettings($mode = Service\ToolSettings::MODE_CURRENT_LEVEL, $simple = true)
341  {
342 
343  $url = $this->getSetting('custom_context_setting_url');
344  $service = new Service\ToolSettings($this, $url, $simple);
345  $response = $service->get($mode);
346 
347  return $response;
348 
349  }
350 
358  public function setToolSettings($settings = array())
359  {
360 
361  $url = $this->getSetting('custom_context_setting_url');
362  $service = new Service\ToolSettings($this, $url);
363  $response = $service->set($settings);
364 
365  return $response;
366 
367  }
368 
374  public function hasMembershipService()
375  {
376 
377  $url = $this->getSetting('custom_context_memberships_url');
378 
379  return !empty($url);
380 
381  }
382 
388  public function getMembership()
389  {
390 
391  $url = $this->getSetting('custom_context_memberships_url');
392  $service = new Service\Membership($this, $url);
393  $response = $service->get();
394 
395  return $response;
396 
397  }
398 
407  public static function fromRecordId($id, $dataConnector)
408  {
409 
410  $context = new Context();
411  $context->dataConnector = $dataConnector;
412  $context->load($id);
413 
414  return $context;
415 
416  }
417 
425  public static function fromConsumer($consumer, $ltiContextId)
426  {
427 
428  $context = new Context();
429  $context->consumer = $consumer;
430  $context->dataConnector = $consumer->getDataConnector();
431  $context->ltiContextId = $ltiContextId;
432  if (!empty($ltiContextId)) {
433  $context->load();
434  }
435 
436  return $context;
437 
438  }
439 
440 ###
441 ### PRIVATE METHODS
442 ###
443 
451  private function load($id = null)
452  {
453 
454  $this->initialize();
455  $this->id = $id;
456  return $this->getDataConnector()->loadContext($this);
457 
458  }
459 
460 }
load($id=null)
Load the context from the database.
Definition: Context.php:451
getToolSettings($mode=Service\ToolSettings::MODE_CURRENT_LEVEL, $simple=true)
Get Tool Settings.
Definition: Context.php:340
$settingsChanged
Whether the settings value have changed since last saved.
Definition: Context.php:74
$consumerId
Tool Consumer ID for this context.
Definition: Context.php:62
setSetting($name, $value=null)
Set a setting value.
Definition: Context.php:261
hasMembershipService()
Check if the Membership service is supported.
Definition: Context.php:374
getKey()
Get tool consumer key.
Definition: Context.php:179
static fromRecordId($id, $dataConnector)
Load the context from the database.
Definition: Context.php:407
settings()
Definition: settings.php:2
$context
Definition: webdav.php:25
initialise()
Initialise the context.
Definition: Context.php:110
save()
Save the context to the database.
Definition: Context.php:122
getDataConnector()
Get the data connector.
Definition: Context.php:227
$dataConnector
Data connector object or string.
Definition: Context.php:80
$created
Date/time when the object was created.
Definition: Context.php:43
Class to implement the Membership service.
Definition: Membership.php:16
static fromRecordId($id, $dataConnector)
Load the tool consumer from the database by its record ID.
getSettings()
Get an array of all setting values.
Definition: Context.php:281
initialize()
Initialise the context.
Definition: Context.php:95
getRecordId()
Get the context record ID.
Definition: Context.php:203
setSettings($settings)
Set an array of all setting values.
Definition: Context.php:293
saveSettings()
Save setting values.
Definition: Context.php:305
const MODE_CURRENT_LEVEL
Settings at current level mode.
setConsumerId($consumerId)
Set tool consumer ID.
Definition: Context.php:166
$id
ID for this context.
Definition: Context.php:68
$default
Definition: build.php:20
getMembership()
Get Memberships.
Definition: Context.php:388
Class to implement a service.
Definition: Service.php:17
$consumer
Tool Consumer for this context.
Definition: Context.php:56
__construct()
Class constructor.
Definition: Context.php:85
setToolSettings($settings=array())
Perform a Tool Settings service request.
Definition: Context.php:358
$url
$updated
Date/time when the object was last updated.
Definition: Context.php:49
$ltiContextId
Context ID as supplied in the last connection request.
Definition: Context.php:25
$response
Class to represent a tool consumer context.
Definition: Context.php:17
Class to implement the Tool Settings service.
getConsumer()
Get tool consumer.
Definition: Context.php:151
static fromConsumer($consumer, $ltiContextId)
Class constructor from consumer.
Definition: Context.php:425
getSetting($name, $default='')
Get a setting value.
Definition: Context.php:242
$settings
Setting values (LTI parameters, custom parameters and local parameters).
Definition: Context.php:37
setRecordId($id)
Sets the context record ID.
Definition: Context.php:215
hasToolSettingsService()
Check if the Tool Settings service is supported.
Definition: Context.php:323