LTI Integration Library 4.10.3
PHP class library for building LTI integrations
 
Loading...
Searching...
No Matches
Context.php
1<?php
2
3namespace ceLTIc\LTI;
4
9
18{
19 use ApiHook;
20
26 public $ltiContextId = null;
27
33 public $title = null;
34
40 public $type = null;
41
55 public $groupSets = null;
56
67 public $groups = null;
68
74 public $lastServiceRequest = null;
75
81 public $created = null;
82
88 public $updated = null;
89
95 private $platform = null;
96
102 private $platformId = null;
103
109 private $id = null;
110
116 private $settings = null;
117
123 private $settingsChanged = false;
124
130 private $dataConnector = null;
131
135 public function __construct()
136 {
137 $this->initialize();
138 }
139
143 public function initialize()
144 {
145 $this->title = '';
146 $this->settings = array();
147 $this->groupSets = null;
148 $this->groups = null;
149 $this->created = null;
150 $this->updated = null;
151 }
152
158 public function initialise()
159 {
160 $this->initialize();
161 }
162
168 public function save()
169 {
170 $ok = $this->getDataConnector()->saveContext($this);
171 if ($ok) {
172 $this->settingsChanged = false;
173 }
174
175 return $ok;
176 }
177
183 public function delete()
184 {
185 return $this->getDataConnector()->deleteContext($this);
186 }
187
196 public function getConsumer()
197 {
198 Util::logDebug('Method ceLTIc\LTI\Context::getConsumer() has been deprecated; please use ceLTIc\LTI\Context::getPlatform() instead.',
199 true);
200 return $this->getPlatform();
201 }
202
211 public function setConsumerId($consumerId)
212 {
213 Util::logDebug('Method ceLTIc\LTI\Context::setConsumerId() has been deprecated; please use ceLTIc\LTI\Context::setPlatformId() instead.',
214 true);
215 $this->setPlatformId($consumerId);
216 }
217
223 public function getPlatform()
224 {
225 if (is_null($this->platform)) {
226 $this->platform = Platform::fromRecordId($this->platformId, $this->getDataConnector());
227 }
228
229 return $this->platform;
230 }
231
237 public function setPlatformId($platformId)
238 {
239 $this->platform = null;
240 $this->platformId = $platformId;
241 }
242
248 public function getKey()
249 {
250 return $this->getPlatform()->getKey();
251 }
252
258 public function getId()
259 {
260 return $this->ltiContextId;
261 }
262
268 public function getRecordId()
269 {
270 return $this->id;
271 }
272
278 public function setRecordId($id)
279 {
280 $this->id = $id;
281 }
282
288 public function getDataConnector()
289 {
290 return $this->dataConnector;
291 }
292
301 public function getSetting($name, $default = '')
302 {
303 if (array_key_exists($name, $this->settings)) {
304 $value = $this->settings[$name];
305 } else {
306 $value = $default;
307 }
308
309 return $value;
310 }
311
318 public function setSetting($name, $value = null)
319 {
320 $old_value = $this->getSetting($name);
321 if ($value !== $old_value) {
322 if (!empty($value)) {
323 $this->settings[$name] = $value;
324 } else {
325 unset($this->settings[$name]);
326 }
327 $this->settingsChanged = true;
328 }
329 }
330
336 public function getSettings()
337 {
338 return $this->settings;
339 }
340
346 public function setSettings($settings)
347 {
348 $this->settings = $settings;
349 }
350
356 public function saveSettings()
357 {
358 if ($this->settingsChanged) {
359 $ok = $this->save();
360 } else {
361 $ok = true;
362 }
363
364 return $ok;
365 }
366
372 public function hasToolSettingsService()
373 {
374 $has = !empty($this->getSetting('custom_context_setting_url'));
375 if (!$has) {
376 $has = self::hasConfiguredApiHook(self::$TOOL_SETTINGS_SERVICE_HOOK, $this->getPlatform()->getFamilyCode(), $this);
377 }
378 return $has;
379 }
380
389 public function getToolSettings($mode = Service\ToolSettings::MODE_CURRENT_LEVEL, $simple = true)
390 {
391 $ok = false;
392 $settings = array();
393 if (!empty($this->getSetting('custom_context_setting_url'))) {
394 $url = $this->getSetting('custom_context_setting_url');
395 $service = new Service\ToolSettings($this, $url, $simple);
396 $settings = $service->get($mode);
397 $this->lastServiceRequest = $service->getHttpMessage();
398 $ok = $settings !== false;
399 }
400 if (!$ok && $this->hasConfiguredApiHook(self::$TOOL_SETTINGS_SERVICE_HOOK, $this->getPlatform()->getFamilyCode(), $this)) {
401 $className = $this->getApiHook(self::$TOOL_SETTINGS_SERVICE_HOOK, $this->getPlatform()->getFamilyCode());
402 $hook = new $className($this);
403 $settings = $hook->getToolSettings($mode, $simple);
404 }
405
406 return $settings;
407 }
408
416 public function setToolSettings($settings = array())
417 {
418 $ok = false;
419 if (!empty($this->getSetting('custom_context_setting_url'))) {
420 $url = $this->getSetting('custom_context_setting_url');
421 $service = new Service\ToolSettings($this, $url);
422 $ok = $service->set($settings);
423 $this->lastServiceRequest = $service->getHttpMessage();
424 }
425 if (!$ok && $this->hasConfiguredApiHook(self::$TOOL_SETTINGS_SERVICE_HOOK, $this->getPlatform()->getFamilyCode(), $this)) {
426 $className = $this->getApiHook(self::$TOOL_SETTINGS_SERVICE_HOOK, $this->getPlatform()->getFamilyCode());
427 $hook = new $className($this);
428 $ok = $hook->setToolSettings($settings);
429 }
430
431 return $ok;
432 }
433
439 public function hasGroupService()
440 {
441 $has = !empty($this->getSetting('custom_context_groups_url'));
442 if (!$has) {
443 $has = self::hasConfiguredApiHook(self::$MEMBERSHIPS_SERVICE_HOOK, $this->getPlatform()->getFamilyCode(), $this);
444 }
445 return $has;
446 }
447
453 public function getGroups()
454 {
455 $groupsUrl = $this->getSetting('custom_context_groups_url');
456 $groupsetsUrl = $this->getSetting('custom_context_group_sets_url');
457 $service = new Service\Groups($this, $groupsUrl, $groupsetsUrl);
458 $ok = $service->get();
459 if (!empty($service->getHttpMessage())) {
460 $this->lastServiceRequest = $service->getHttpMessage();
461 }
462 if (!$ok && $this->hasConfiguredApiHook(self::$GROUPS_SERVICE_HOOK, $this->getPlatform()->getFamilyCode(), $this)) {
463 $className = $this->getApiHook(self::$GROUPS_SERVICE_HOOK, $this->getPlatform()->getFamilyCode());
464 $hook = new $className($this);
465 $ok = $hook->getGroups();
466 }
467
468 return $ok;
469 }
470
479 public function hasMembershipService()
480 {
481 Util::logDebug('Method ceLTIc\LTI\Context::hasMembershipService() has been deprecated; please use ceLTIc\LTI\Context::hasMembershipsService() instead.',
482 true);
483 return $this->hasMembershipsService();
484 }
485
491 public function hasMembershipsService()
492 {
493 $has = !empty($this->getSetting('custom_context_memberships_url')) || !empty($this->getSetting('custom_context_memberships_v2_url'));
494 if (!$has) {
495 $has = self::hasConfiguredApiHook(self::$MEMBERSHIPS_SERVICE_HOOK, $this->getPlatform()->getFamilyCode(), $this);
496 }
497 return $has;
498 }
499
508 public function getMembership()
509 {
510 Util::logDebug('Method ceLTIc\LTI\Context::getMembership() has been deprecated; please use ceLTIc\LTI\Context::getMemberships() instead.',
511 true);
512 return $this->getMemberships();
513 }
514
522 public function getMemberships($withGroups = false)
523 {
524 $ok = false;
525 $userResults = array();
526 $hasMembershipsService = !empty($this->getSetting('custom_context_memberships_url'));
527 $hasNRPService = !empty($this->getSetting('custom_context_memberships_v2_url'));
528 $hasGroupsService = !empty($this->getSetting('custom_context_groups_url')) ||
529 $this->hasConfiguredApiHook(self::$GROUPS_SERVICE_HOOK, $this->getPlatform()->getFamilyCode(), $this);
530 $hasApiHook = $this->hasConfiguredApiHook(self::$MEMBERSHIPS_SERVICE_HOOK, $this->getPlatform()->getFamilyCode(), $this);
531 if (($hasMembershipsService || $hasNRPService) && (!$withGroups || ($hasNRPService && $hasGroupsService) || !$hasApiHook)) {
532 if ($hasNRPService) {
533 $url = $this->getSetting('custom_context_memberships_v2_url');
534 $format = Service\Membership::MEDIA_TYPE_MEMBERSHIPS_NRPS;
535 } else {
536 $url = $this->getSetting('custom_context_memberships_url');
537 $format = Service\Membership::MEDIA_TYPE_MEMBERSHIPS_V1;
538 }
539 $service = new Service\Membership($this, $url, $format);
540 if (!$withGroups || !$hasNRPService) {
541 $userResults = $service->get();
542 } else {
543 $userResults = $service->getWithGroups();
544 }
545 if (!empty($service->getHttpMessage())) {
546 $this->lastServiceRequest = $service->getHttpMessage();
547 }
548 $ok = $userResults !== false;
549 }
550 if (!$ok && $hasApiHook) {
551 $className = $this->getApiHook(self::$MEMBERSHIPS_SERVICE_HOOK, $this->getPlatform()->getFamilyCode());
552 $hook = new $className($this);
553 $userResults = $hook->getMemberships($withGroups);
554 }
555
556 return $userResults;
557 }
558
564 public function hasLineItemService()
565 {
566 $has = false;
567 if (!empty($this->getSetting('custom_ags_scopes'))) {
568 $scopes = explode(',', $this->getSetting('custom_ags_scopes'));
569 if (in_array(Service\LineItem::$SCOPE, $scopes) || in_array(Service\LineItem::$SCOPE_READONLY, $scopes)) {
570 $has = !empty($this->getSetting('custom_lineitems_url'));
571 }
572 }
573
574 return $has;
575 }
576
582 public function hasScoreService()
583 {
584 $has = false;
585 if (!empty($this->getSetting('custom_ags_scopes'))) {
586 $scopes = explode(',', $this->getSetting('custom_ags_scopes'));
587 if (in_array(Service\Score::$SCOPE, $scopes)) {
588 $has = !empty($this->getSetting('custom_lineitems_url'));
589 }
590 }
591
592 return $has;
593 }
594
600 public function hasResultService()
601 {
602 $has = false;
603 if (!empty($this->getSetting('custom_ags_scopes'))) {
604 $scopes = explode(',', $this->getSetting('custom_ags_scopes'));
605 if (in_array(Service\Result::$SCOPE, $scopes)) {
606 $has = !empty($this->getSetting('custom_lineitems_url'));
607 }
608 }
609
610 return $has;
611 }
612
622 public function getLineItems($resourceId = null, $tag = null, $limit = null)
623 {
624 $lineItems = false;
625 $this->lastServiceRequest = null;
626 $lineItemService = $this->getLineItemService();
627 if (!empty($lineItemService)) {
628 $lineItems = $lineItemService->getAll(null, $resourceId, $tag);
629 $http = $lineItemService->getHttpMessage();
630 $this->lastServiceRequest = $http;
631 }
632
633 return $lineItems;
634 }
635
643 public function createLineItem($lineItem)
644 {
645 $ok = false;
646 $lineItemService = $this->getLineItemService();
647 if (!empty($lineItemService)) {
648 $ok = $lineItemService->createLineItem($lineItem);
649 }
650
651 return $ok;
652 }
653
662 public static function fromRecordId($id, $dataConnector)
663 {
664 $context = new Context();
665 $context->dataConnector = $dataConnector;
666 $context->load($id);
667
668 return $context;
669 }
670
682 public static function fromConsumer($consumer, $ltiContextId)
683 {
684 return self::fromPlatform($consumer, $ltiContextId);
685 }
686
695 public static function fromPlatform($platform, $ltiContextId)
696 {
697 $context = new Context();
698 $context->platform = $platform;
699 $context->dataConnector = $platform->getDataConnector();
700 $context->ltiContextId = $ltiContextId;
701 if (!empty($ltiContextId)) {
702 $context->load();
703 }
704
705 return $context;
706 }
707
708###
709### PRIVATE METHODS
710###
711
719 private function load($id = null)
720 {
721 $this->initialize();
722 $this->id = $id;
723 return $this->getDataConnector()->loadContext($this);
724 }
725
731 private function getLineItemService()
732 {
733 $url = $this->getSetting('custom_lineitems_url');
734 if (!empty($url)) {
735 $lineItemService = new Service\LineItem($this->getPlatform(), $url);
736 } else {
737 $lineItemService = false;
738 }
739
740 return $lineItemService;
741 }
742
743}
Trait to handle API hook registrations.
Definition ApiHook.php:13
Class to represent a platform context.
Definition Context.php:18
$groups
User groups (null if the platform does not support the groups enhancement)
Definition Context.php:66
hasMembershipsService()
Check if a Membership service is available.
Definition Context.php:490
__construct()
Class constructor.
Definition Context.php:134
getId()
Get context ID.
Definition Context.php:257
$ltiContextId
Context ID as supplied in the last connection request.
Definition Context.php:25
$type
Context type.
Definition Context.php:39
getMemberships($withGroups=false)
Get Memberships.
Definition Context.php:521
hasToolSettingsService()
Check if the Tool Settings service is available.
Definition Context.php:371
$lastServiceRequest
HttpMessage object for last service request.
Definition Context.php:73
hasScoreService()
Check if the Score service is available.
Definition Context.php:581
$updated
Timestamp for when the object was last updated.
Definition Context.php:87
getGroups()
Get course group sets and groups.
Definition Context.php:452
getMembership()
Get Membership.
Definition Context.php:507
setSetting($name, $value=null)
Set a setting value.
Definition Context.php:317
getDataConnector()
Get the data connector.
Definition Context.php:287
getConsumer()
Get tool consumer.
Definition Context.php:195
hasLineItemService()
Check if the Line-item service is available.
Definition Context.php:563
getToolSettings($mode=Service\ToolSettings::MODE_CURRENT_LEVEL, $simple=true)
Get Tool Settings.
Definition Context.php:388
setPlatformId($platformId)
Set platform ID.
Definition Context.php:236
hasMembershipService()
Check if the Membership service is supported.
Definition Context.php:478
setToolSettings($settings=array())
Set Tool Settings.
Definition Context.php:415
setConsumerId($consumerId)
Set tool consumer ID.
Definition Context.php:210
static fromPlatform($platform, $ltiContextId)
Class constructor from platform.
Definition Context.php:694
$groupSets
User group sets (null if the platform does not support the groups enhancement)
Definition Context.php:54
initialize()
Initialise the context.
Definition Context.php:142
saveSettings()
Save setting values.
Definition Context.php:355
$created
Timestamp for when the object was created.
Definition Context.php:80
hasResultService()
Check if the Result service is available.
Definition Context.php:599
setSettings($settings)
Set an array of all setting values.
Definition Context.php:345
static fromConsumer($consumer, $ltiContextId)
Class constructor from consumer.
Definition Context.php:681
$title
Context title.
Definition Context.php:32
hasGroupService()
Check if a Course Group service is available.
Definition Context.php:438
getPlatform()
Get platform.
Definition Context.php:222
getRecordId()
Get the context record ID.
Definition Context.php:267
getKey()
Get consumer key.
Definition Context.php:247
getSetting($name, $default='')
Get a setting value.
Definition Context.php:300
getLineItems($resourceId=null, $tag=null, $limit=null)
Get line-items.
Definition Context.php:621
setRecordId($id)
Sets the context record ID.
Definition Context.php:277
initialise()
Initialise the context.
Definition Context.php:157
getSettings()
Get an array of all setting values.
Definition Context.php:335
save()
Save the context to the database.
Definition Context.php:167
createLineItem($lineItem)
Create a new line-item.
Definition Context.php:642
static fromRecordId($id, $dataConnector)
Load the context from the database.
Definition Context.php:661
Class to represent an HTTP message request.
Class to represent a line-item.
Definition LineItem.php:15
static fromRecordId($id, $dataConnector)
Load the platform from the database by its record ID.
Definition Platform.php:534
Class to implement the Course Groups service.
Definition Groups.php:16
Class to implement the Membership service.
static $SCOPE
Access scope.
Definition Result.php:24
static $SCOPE
Access scope.
Definition Score.php:24
Class to implement a service.
Definition Service.php:19
Class to implement the Tool Settings service.
const MODE_CURRENT_LEVEL
Settings at current level mode.
Class to implement utility methods.
Definition Util.php:15
static logDebug($message, $showSource=false)
Log a debug message.
Definition Util.php:274