LTI Integration Library  3.1.0
PHP class library for building LTI integrations
Context.php
Go to the documentation of this file.
1 <?php
2 
3 namespace ceLTIc\LTI;
4 
8 
17 class Context
18 {
19  use ApiHook;
20 
26  public $ltiContextId = null;
27 
33  public $title = null;
34 
40  public $type = null;
41 
47  public $groupSets = null;
48 
54  public $groups = null;
55 
61  public $lastServiceRequest = null;
62 
68  public $created = null;
69 
75  public $updated = null;
76 
82  private $consumer = null;
83 
89  private $consumerId = null;
90 
96  private $id = null;
97 
103  private $settings = null;
104 
110  private $settingsChanged = false;
111 
117  private $dataConnector = null;
118 
122  public function __construct()
123  {
124  $this->initialize();
125  }
126 
130  public function initialize()
131  {
132  $this->title = '';
133  $this->settings = array();
134  $this->groupSets = null;
135  $this->groups = null;
136  $this->created = null;
137  $this->updated = null;
138  }
139 
145  public function initialise()
146  {
147  $this->initialize();
148  }
149 
155  public function save()
156  {
157  $ok = $this->getDataConnector()->saveContext($this);
158  if ($ok) {
159  $this->settingsChanged = false;
160  }
161 
162  return $ok;
163  }
164 
170  public function delete()
171  {
172  return $this->getDataConnector()->deleteContext($this);
173  }
174 
180  public function getConsumer()
181  {
182  if (is_null($this->consumer)) {
183  $this->consumer = ToolConsumer::fromRecordId($this->consumerId, $this->getDataConnector());
184  }
185 
186  return $this->consumer;
187  }
188 
194  public function setConsumerId($consumerId)
195  {
196  $this->consumer = null;
197  $this->consumerId = $consumerId;
198  }
199 
205  public function getKey()
206  {
207  return $this->getConsumer()->getKey();
208  }
209 
215  public function getId()
216  {
217  return $this->ltiContextId;
218  }
219 
225  public function getRecordId()
226  {
227  return $this->id;
228  }
229 
235  public function setRecordId($id)
236  {
237  $this->id = $id;
238  }
239 
245  public function getDataConnector()
246  {
247  return $this->dataConnector;
248  }
249 
258  public function getSetting($name, $default = '')
259  {
260  if (array_key_exists($name, $this->settings)) {
261  $value = $this->settings[$name];
262  } else {
263  $value = $default;
264  }
265 
266  return $value;
267  }
268 
275  public function setSetting($name, $value = null)
276  {
277  $old_value = $this->getSetting($name);
278  if ($value !== $old_value) {
279  if (!empty($value)) {
280  $this->settings[$name] = $value;
281  } else {
282  unset($this->settings[$name]);
283  }
284  $this->settingsChanged = true;
285  }
286  }
287 
293  public function getSettings()
294  {
295  return $this->settings;
296  }
297 
303  public function setSettings($settings)
304  {
305  $this->settings = $settings;
306  }
307 
313  public function saveSettings()
314  {
315  if ($this->settingsChanged) {
316  $ok = $this->save();
317  } else {
318  $ok = true;
319  }
320 
321  return $ok;
322  }
323 
329  public function hasToolSettingsService()
330  {
331  $has = !empty($this->getSetting('custom_context_setting_url'));
332  if (!$has) {
333  $has = self::hasApiHook(self::$TOOL_SETTINGS_SERVICE_HOOK, $this->getConsumer()->getFamilyCode());
334  }
335  return $has;
336  }
337 
346  public function getToolSettings($mode = Service\ToolSettings::MODE_CURRENT_LEVEL, $simple = true)
347  {
348  $ok = false;
349  $settings = array();
350  if (!empty($this->getSetting('custom_context_setting_url'))) {
351  $url = $this->getSetting('custom_context_setting_url');
352  $service = new Service\ToolSettings($this, $url, $simple);
353  $settings = $service->get($mode);
354  $this->lastServiceRequest = $service->getHTTPMessage();
355  $ok = $settings !== false;
356  }
357  if (!$ok && $this->hasApiHook(self::$TOOL_SETTINGS_SERVICE_HOOK, $this->getConsumer()->getFamilyCode())) {
358  $className = $this->getApiHook(self::$TOOL_SETTINGS_SERVICE_HOOK, $this->getConsumer()->getFamilyCode());
359  $hook = new $className($this);
360  $settings = $hook->getToolSettings($mode, $simple);
361  }
362 
363  return $settings;
364  }
365 
373  public function setToolSettings($settings = array())
374  {
375  $ok = false;
376  if (!empty($this->getSetting('custom_context_setting_url'))) {
377  $url = $this->getSetting('custom_context_setting_url');
378  $service = new Service\ToolSettings($this, $url);
379  $ok = $service->set($settings);
380  $this->lastServiceRequest = $service->getHTTPMessage();
381  }
382  if (!$ok && $this->hasApiHook(self::$TOOL_SETTINGS_SERVICE_HOOK, $this->getConsumer()->getFamilyCode())) {
383  $className = $this->getApiHook(self::$TOOL_SETTINGS_SERVICE_HOOK, $this->getConsumer()->getFamilyCode());
384  $hook = new $className($this);
385  $ok = $hook->setToolSettings($settings);
386  }
387 
388  return $ok;
389  }
390 
399  public function hasMembershipService()
400  {
401  return $this->hasMembershipsService();
402  }
403 
409  public function hasMembershipsService()
410  {
411  $has = !empty($this->getSetting('custom_context_memberships_url'));
412  if (!$has) {
413  $has = self::hasApiHook(self::$MEMBERSHIPS_SERVICE_HOOK, $this->getConsumer()->getFamilyCode());
414  }
415  return $has;
416  }
417 
426  public function getMembership()
427  {
428  return $this->getMemberships();
429  }
430 
438  public function getMemberships($withGroups = false)
439  {
440  $ok = false;
441  $userResults = array();
442  $hasLtiService = !empty($this->getSetting('custom_context_memberships_url'));
443  $hasApiHook = $this->hasApiHook(self::$MEMBERSHIPS_SERVICE_HOOK, $this->getConsumer()->getFamilyCode());
444  if ($hasLtiService && (!$withGroups || !$hasApiHook)) {
445  $url = $this->getSetting('custom_context_memberships_url');
446  $service = new Service\Membership($this, $url);
447  $userResults = $service->get();
448  $this->lastServiceRequest = $service->getHTTPMessage();
449  $ok = $userResults !== false;
450  }
451  if (!$ok && $hasApiHook) {
452  $className = $this->getApiHook(self::$MEMBERSHIPS_SERVICE_HOOK, $this->getConsumer()->getFamilyCode());
453  $hook = new $className($this);
454  $userResults = $hook->getMemberships($withGroups);
455  }
456 
457  return $userResults;
458  }
459 
468  public static function fromRecordId($id, $dataConnector)
469  {
470  $context = new Context();
471  $context->dataConnector = $dataConnector;
472  $context->load($id);
473 
474  return $context;
475  }
476 
485  public static function fromConsumer($consumer, $ltiContextId)
486  {
487  $context = new Context();
488  $context->consumer = $consumer;
489  $context->dataConnector = $consumer->getDataConnector();
490  $context->ltiContextId = $ltiContextId;
491  if (!empty($ltiContextId)) {
492  $context->load();
493  }
494 
495  return $context;
496  }
497 
498 ###
499 ### PRIVATE METHODS
500 ###
501 
509  private function load($id = null)
510  {
511  $this->initialize();
512  $this->id = $id;
513  return $this->getDataConnector()->loadContext($this);
514  }
515 
516 }
getKey()
Get tool consumer key.
Definition: Context.php:204
Class to implement the Tool Settings service.
save()
Save the context to the database.
Definition: Context.php:154
Class to implement a service.
Definition: Service.php:17
$groupSets
UserResult group sets (null if the consumer does not support the groups enhancement)
Definition: Context.php:46
getMembership()
Get Membership.
Definition: Context.php:425
initialise()
Initialise the context.
Definition: Context.php:144
$ltiContextId
Context ID as supplied in the last connection request.
Definition: Context.php:25
Class to represent a tool consumer context.
Definition: Context.php:17
static fromRecordId($id, $dataConnector)
Load the context from the database.
Definition: Context.php:467
const MODE_CURRENT_LEVEL
Settings at current level mode.
getConsumer()
Get tool consumer.
Definition: Context.php:179
getMemberships($withGroups=false)
Get Memberships.
Definition: Context.php:437
$groups
UserResult groups (null if the consumer does not support the groups enhancement)
Definition: Context.php:53
getSettings()
Get an array of all setting values.
Definition: Context.php:292
setSettings($settings)
Set an array of all setting values.
Definition: Context.php:302
getDataConnector()
Get the data connector.
Definition: Context.php:244
setRecordId($id)
Sets the context record ID.
Definition: Context.php:234
getRecordId()
Get the context record ID.
Definition: Context.php:224
hasMembershipService()
Check if the Membership service is supported.
Definition: Context.php:398
Class to implement the Membership service.
Definition: Membership.php:17
__construct()
Class constructor.
Definition: Context.php:121
Class to represent an HTTP message request.
Definition: HTTPMessage.php:16
$created
Timestamp for when the object was created.
Definition: Context.php:67
hasMembershipsService()
Check if a Membership service is available.
Definition: Context.php:408
setConsumerId($consumerId)
Set tool consumer ID.
Definition: Context.php:193
$title
Context title.
Definition: Context.php:32
setSetting($name, $value=null)
Set a setting value.
Definition: Context.php:274
$updated
Timestamp for when the object was last updated.
Definition: Context.php:74
getSetting($name, $default='')
Get a setting value.
Definition: Context.php:257
saveSettings()
Save setting values.
Definition: Context.php:312
Trait to handle API hook registrations.
Definition: ApiHook.php:13
setToolSettings($settings=array())
Perform a Tool Settings service request.
Definition: Context.php:372
hasToolSettingsService()
Check if the Tool Settings service is available.
Definition: Context.php:328
$type
Context type.
Definition: Context.php:39
initialize()
Initialise the context.
Definition: Context.php:129
static fromConsumer($consumer, $ltiContextId)
Class constructor from consumer.
Definition: Context.php:484
getToolSettings($mode=Service\ToolSettings::MODE_CURRENT_LEVEL, $simple=true)
Get Tool Settings.
Definition: Context.php:345
getId()
Get context ID.
Definition: Context.php:214
$lastServiceRequest
HTTPMessage object for last service request.
Definition: Context.php:60
static fromRecordId($id, $dataConnector)
Load the tool consumer from the database by its record ID.