ILIAS  release_8 Revision v8.24
Context.php
Go to the documentation of this file.
1<?php
2
20
22use ILIAS\LTI\ToolProvider\Http\HTTPMessage;
25
34{
35 use ApiHook;
36
42 public ?string $ltiContextId = null;
43
49 public ?string $title = null;
50
56 public ?string $type = null;
57
71 public ?array $groupSets = null;
72
83 public ?array $groups = null;
84
90 public ?HTTPMessage $lastServiceRequest = null;
91
97 public ?int $created = null;
98
104 public ?int $updated = null;
105
111 private ?Platform $platform = null;
112
118 private ?int $platformId = null;
119
125 private ?int $id = null;
126
132 private ?array $settings = null;
133
139 private bool $settingsChanged = false;
140
147
151 public function __construct()
152 {
153 $this->initialize();
154 }
155
159 public function initialize()
160 {
161 $this->title = '';
162 $this->settings = array();
163 $this->groupSets = null;
164 $this->groups = null;
165 $this->created = null;
166 $this->updated = null;
167 }
168
174 public function initialise()
175 {
176 $this->initialize();
177 }
178
184 public function save(): bool
185 {
186 $ok = $this->getDataConnector()->saveContext($this);
187 if ($ok) {
188 $this->settingsChanged = false;
189 }
190
191 return $ok;
192 }
193
199 public function delete(): bool
200 {
201 return $this->getDataConnector()->deleteContext($this);
202 }
203
204// /**
205// * Get tool consumer.
206// *
207// * @deprecated Use getPlatform() instead
208// * @see Context::getPlatform()
209// *
210// * @return ToolConsumer Tool consumer object for this context.
211// */
212// public function getConsumer()
213// {
214// Util::logDebug('Method ceLTIc\LTI\Context::getConsumer() has been deprecated; please use ceLTIc\LTI\Context::getPlatform() instead.',
215// true);
216// return $this->getPlatform();
217// }
218
219// /**
220// * Set tool consumer ID.
221// *
222// * @deprecated Use setPlatformId() instead
223// * @see Context::setPlatformId()
224// *
225// * @param int $consumerId Tool Consumer ID for this context.
226// */
227// public function setConsumerId($consumerId)
228// {
229// Util::logDebug('Method ceLTIc\LTI\Context::setConsumerId() has been deprecated; please use ceLTIc\LTI\Context::setPlatformId() instead.',
230// true);
231// $this->setPlatformId($consumerId);
232// }
233
239 public function getPlatform(): ?\ilLTIPlatform
240 {
241 if (is_null($this->platform)) {
242 $this->platform = \ilLTIPlatform::fromRecordId($this->platformId, $this->getDataConnector());
243 }
244
245 return $this->platform;
246 }
247
252 public function setPlatformId(int $platformId)
253 {
254 $this->platform = null;
255 $this->platformId = $platformId;
256 }
257
263 public function getKey(): string
264 {
265 return $this->getPlatform()->getKey();
266 }
267
273 public function getId(): ?string
274 {
275 return $this->ltiContextId;
276 }
277
283 public function getRecordId(): ?int
284 {
285 return $this->id;
286 }
287
292 public function setRecordId(int $id)
293 {
294 $this->id = $id;
295 }
296
302 public function getDataConnector()
303 {
305 }
306
313 public function getSetting(string $name, string $default = ''): string
314 {
315 if (array_key_exists($name, $this->settings)) {
316 $value = $this->settings[$name];
317 } else {
318 $value = $default;
319 }
320
321 return $value;
322 }
323
329 public function setSetting(string $name, string $value = null)
330 {
331 $old_value = $this->getSetting($name);
332 if ($value !== $old_value) {
333 if (!empty($value)) {
334 $this->settings[$name] = $value;
335 } else {
336 unset($this->settings[$name]);
337 }
338 $this->settingsChanged = true;
339 }
340 }
341
347 public function getSettings(): ?array
348 {
349 return $this->settings;
350 }
351
356 public function setSettings(array $settings)
357 {
358 $this->settings = $settings;
359 }
360
366 public function saveSettings(): bool
367 {
368 if ($this->settingsChanged) {
369 $ok = $this->save();
370 } else {
371 $ok = true;
372 }
373
374 return $ok;
375 }
376
382 public function hasToolSettingsService(): bool
383 {
384 $has = !empty($this->getSetting('custom_context_setting_url'));
385 if (!$has) {
386 $has = self::hasConfiguredApiHook(self::$TOOL_SETTINGS_SERVICE_HOOK, $this->getPlatform()->getFamilyCode(), $this);
387 }
388 return $has;
389 }
390
397 public function getToolSettings(int $mode = Service\ToolSettings::MODE_CURRENT_LEVEL, bool $simple = true)
398 {
399 $ok = false;
400 $settings = array();
401 if (!empty($this->getSetting('custom_context_setting_url'))) {
402 $url = $this->getSetting('custom_context_setting_url');
403 $service = new Service\ToolSettings($this, $url, $simple);
404 $settings = $service->get($mode);
405 $this->lastServiceRequest = $service->getHttpMessage();
406 $ok = $settings !== false;
407 }
408 if (!$ok && $this->hasConfiguredApiHook(self::$TOOL_SETTINGS_SERVICE_HOOK, $this->getPlatform()->getFamilyCode(), $this)) {
409 $className = $this->getApiHook(self::$TOOL_SETTINGS_SERVICE_HOOK, $this->getPlatform()->getFamilyCode());
410 $hook = new $className($this);
411 $settings = $hook->getToolSettings($mode, $simple);
412 }
413
414 return $settings;
415 }
416
422 public function setToolSettings(array $settings = array()): bool
423 {
424 $ok = false;
425 if (!empty($this->getSetting('custom_context_setting_url'))) {
426 $url = $this->getSetting('custom_context_setting_url');
427 $service = new Service\ToolSettings($this, $url);
428 $ok = $service->set($settings);
429 $this->lastServiceRequest = $service->getHttpMessage();
430 }
431 if (!$ok && $this->hasConfiguredApiHook(self::$TOOL_SETTINGS_SERVICE_HOOK, $this->getPlatform()->getFamilyCode(), $this)) {
432 $className = $this->getApiHook(self::$TOOL_SETTINGS_SERVICE_HOOK, $this->getPlatform()->getFamilyCode());
433 $hook = new $className($this);
434 $ok = $hook->setToolSettings($settings);
435 }
436
437 return $ok;
438 }
439
445 public function hasGroupService(): bool
446 {
447 $has = !empty($this->getSetting('custom_context_groups_url'));
448 if (!$has) {
449 $has = self::hasConfiguredApiHook(self::$MEMBERSHIPS_SERVICE_HOOK, $this->getPlatform()->getFamilyCode(), $this);
450 }
451 return $has;
452 }
453
459 public function getGroups(): bool
460 {
461 $groupsUrl = $this->getSetting('custom_context_groups_url');
462 $groupsetsUrl = $this->getSetting('custom_context_group_sets_url');
463 $service = new Service\Groups($this, $groupsUrl, $groupsetsUrl);
464 $ok = $service->get();
465 if (!empty($service->getHttpMessage())) {
466 $this->lastServiceRequest = $service->getHttpMessage();
467 }
468 if (!$ok && $this->hasConfiguredApiHook(self::$GROUPS_SERVICE_HOOK, $this->getPlatform()->getFamilyCode(), $this)) {
469 $className = $this->getApiHook(self::$GROUPS_SERVICE_HOOK, $this->getPlatform()->getFamilyCode());
470 $hook = new $className($this);
471 $ok = $hook->getGroups();
472 }
473
474 return $ok;
475 }
476
485 public function hasMembershipService(): bool
486 {
488 'Method ceLTIc\LTI\Context::hasMembershipService() has been deprecated; please use ceLTIc\LTI\Context::hasMembershipsService() instead.',
489 true
490 );
491 return $this->hasMembershipsService();
492 }
493
499 public function hasMembershipsService(): bool
500 {
501 $has = !empty($this->getSetting('custom_context_memberships_url')) || !empty($this->getSetting('custom_context_memberships_v2_url'));
502 if (!$has) {
503 $has = self::hasConfiguredApiHook(self::$MEMBERSHIPS_SERVICE_HOOK, $this->getPlatform()->getFamilyCode(), $this);
504 }
505 return $has;
506 }
507
516 public function getMembership()
517 {
519 'Method ceLTIc\LTI\Context::getMembership() has been deprecated; please use ceLTIc\LTI\Context::getMemberships() instead.',
520 true
521 );
522 return $this->getMemberships();
523 }
524
530 public function getMemberships(bool $withGroups = false)
531 {
532 $ok = false;
533 $userResults = array();
534 $hasMembershipsService = !empty($this->getSetting('custom_context_memberships_url'));
535 $hasNRPService = !empty($this->getSetting('custom_context_memberships_v2_url'));
536 $hasGroupsService = !empty($this->getSetting('custom_context_groups_url')) ||
537 $this->hasConfiguredApiHook(self::$GROUPS_SERVICE_HOOK, $this->getPlatform()->getFamilyCode(), $this);
538 $hasApiHook = $this->hasConfiguredApiHook(self::$MEMBERSHIPS_SERVICE_HOOK, $this->getPlatform()->getFamilyCode(), $this);
539 if (($hasMembershipsService || $hasNRPService) && (!$withGroups || ($hasNRPService && $hasGroupsService) || !$hasApiHook)) {
540 if ($hasNRPService) {
541 $url = $this->getSetting('custom_context_memberships_v2_url');
543 } else {
544 $url = $this->getSetting('custom_context_memberships_url');
546 }
548 if (!$withGroups || !$hasNRPService) {
549 $userResults = $service->get();
550 } else {
551 $userResults = $service->getWithGroups();
552 }
553 if (!empty($service->getHttpMessage())) {
554 $this->lastServiceRequest = $service->getHttpMessage();
555 }
556 $ok = $userResults !== false;
557 }
558 if (!$ok && $hasApiHook) {
559 $className = $this->getApiHook(self::$MEMBERSHIPS_SERVICE_HOOK, $this->getPlatform()->getFamilyCode());
560 $hook = new $className($this);
561 $userResults = $hook->getMemberships($withGroups);
562 }
563
564 return $userResults;
565 }
566
572 public function hasLineItemService(): bool
573 {
574 $has = false;
575 if (!empty($this->getSetting('custom_ags_scopes'))) {
576 $scopes = explode(',', $this->getSetting('custom_ags_scopes'));
577 if (in_array(Service\LineItem::$SCOPE, $scopes) || in_array(Service\LineItem::$SCOPE_READONLY, $scopes)) {
578 $has = !empty($this->getSetting('custom_lineitems_url'));
579 }
580 }
581
582 return $has;
583 }
584
590 public function hasScoreService(): bool
591 {
592 $has = false;
593 if (!empty($this->getSetting('custom_ags_scopes'))) {
594 $scopes = explode(',', $this->getSetting('custom_ags_scopes'));
595 if (in_array(Service\Score::$SCOPE, $scopes)) {
596 $has = !empty($this->getSetting('custom_lineitems_url'));
597 }
598 }
599
600 return $has;
601 }
602
608 public function hasResultService(): bool
609 {
610 $has = false;
611 if (!empty($this->getSetting('custom_ags_scopes'))) {
612 $scopes = explode(',', $this->getSetting('custom_ags_scopes'));
613 if (in_array(Service\Result::$SCOPE, $scopes)) {
614 $has = !empty($this->getSetting('custom_lineitems_url'));
615 }
616 }
617
618 return $has;
619 }
620
628 public function getLineItems(string $resourceId = null, string $tag = null, int $limit = null)
629 {
630 $lineItems = false;
631 $this->lastServiceRequest = null;
632 $lineItemService = $this->getLineItemService();
633 if (!empty($lineItemService)) {
634 $lineItems = $lineItemService->getAll(null, $resourceId, $tag);
635 $http = $lineItemService->getHttpMessage();
636 $this->lastServiceRequest = $http;
637 }
638
639 return $lineItems;
640 }
641
647 public function createLineItem(LineItem $lineItem): bool
648 {
649 $ok = false;
650 $lineItemService = $this->getLineItemService();
651 if (!empty($lineItemService)) {
652 $ok = $lineItemService->createLineItem($lineItem);
653 }
654
655 return $ok;
656 }
657
664 public static function fromRecordId(int $id, \ILIAS\LTI\ToolProvider\DataConnector\DataConnector $dataConnector): Context
665 {
666 $context = new Context();
667 $context->dataConnector = $dataConnector;
668 $context->load($id);
669
670 return $context;
671 }
672
673// /**
674// * Class constructor from consumer.
675// *
676// * @deprecated Use fromPlatform() instead
677// * @see Context::fromPlatform()
678// *
679// * @param ToolConsumer $consumer Consumer instance
680// * @param string $ltiContextId LTI Context ID value
681// *
682// * @return Context
683// */
684// public static function fromConsumer($consumer, $ltiContextId)
685// {
686// return self::fromPlatform($consumer, $ltiContextId);
687// }
688
695 public static function fromPlatform(Platform $platform, string $ltiContextId): Context
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
717 private function load(int $id = null): bool
718 {
719 $this->initialize();
720 $this->id = $id;
721 return $this->getDataConnector()->loadContext($this);
722 }
723
729 private function getLineItemService()
730 {
731 $url = $this->getSetting('custom_lineitems_url');
732 if (!empty($url)) {
733 $lineItemService = new Service\LineItem($this->getPlatform(), $url);
734 } else {
735 $lineItemService = false;
736 }
737
738 return $lineItemService;
739 }
740}
Class to represent a platform context.
Definition: Context.php:34
hasLineItemService()
Check if the Line Item service is available.
Definition: Context.php:572
__construct()
Class constructor.
Definition: Context.php:151
int $updated
Timestamp for when the object was last updated.
Definition: Context.php:104
initialize()
Initialise the context.
Definition: Context.php:159
string $title
Context title.
Definition: Context.php:49
static fromRecordId(int $id, \ILIAS\LTI\ToolProvider\DataConnector\DataConnector $dataConnector)
Load the context from the database.
Definition: Context.php:664
string $type
Context type.
Definition: Context.php:56
int $id
ID for this context.
Definition: Context.php:125
setPlatformId(int $platformId)
Set platform ID.
Definition: Context.php:252
bool $settingsChanged
Whether the settings value have changed since last saved.
Definition: Context.php:139
load(int $id=null)
Load the context from the database.
Definition: Context.php:717
createLineItem(LineItem $lineItem)
Create a new line item.
Definition: Context.php:647
save()
Save the context to the database.
Definition: Context.php:184
setSettings(array $settings)
Set an array of all setting values.
Definition: Context.php:356
ILIAS LTI ToolProvider DataConnector DataConnector $dataConnector
Data connector object or string.
Definition: Context.php:146
initialise()
Initialise the context.
Definition: Context.php:174
hasMembershipService()
Check if the Membership service is supported.
Definition: Context.php:485
setRecordId(int $id)
Sets the context record ID.
Definition: Context.php:292
getRecordId()
Get the context record ID.
Definition: Context.php:283
hasToolSettingsService()
Check if the Tool Settings service is available.
Definition: Context.php:382
getLineItems(string $resourceId=null, string $tag=null, int $limit=null)
Get line items.
Definition: Context.php:628
getMemberships(bool $withGroups=false)
Get Memberships.
Definition: Context.php:530
setSetting(string $name, string $value=null)
Set a setting value.
Definition: Context.php:329
hasResultService()
Check if the Result service is available.
Definition: Context.php:608
getId()
Get context ID.
Definition: Context.php:273
getGroups()
Get course group sets and groups.
Definition: Context.php:459
getMembership()
Get Membership.
Definition: Context.php:516
setToolSettings(array $settings=array())
Set Tool Settings.
Definition: Context.php:422
string $ltiContextId
Context ID as supplied in the last connection request.
Definition: Context.php:42
static fromPlatform(Platform $platform, string $ltiContextId)
Class constructor from platform.
Definition: Context.php:695
getSetting(string $name, string $default='')
Get a setting value.
Definition: Context.php:313
getSettings()
Get an array of all setting values.
Definition: Context.php:347
hasScoreService()
Check if the Score service is available.
Definition: Context.php:590
getKey()
Get consumer key.
Definition: Context.php:263
getToolSettings(int $mode=Service\ToolSettings::MODE_CURRENT_LEVEL, bool $simple=true)
Get Tool Settings.
Definition: Context.php:397
getDataConnector()
Get the data connector.
Definition: Context.php:302
HTTPMessage $lastServiceRequest
HttpMessage object for last service request.
Definition: Context.php:90
Platform $platform
Platform for this context.
Definition: Context.php:111
array $settings
Setting values (LTI parameters, custom parameters and local parameters).
Definition: Context.php:132
array $groupSets
User group sets (null if the platform does not support the groups enhancement)
Definition: Context.php:71
hasGroupService()
Check if a Course Group service is available.
Definition: Context.php:445
int $platformId
Platform ID for this context.
Definition: Context.php:118
getPlatform()
Get platform.
Definition: Context.php:239
hasMembershipsService()
Check if a Membership service is available.
Definition: Context.php:499
getLineItemService()
Get the Line Item service object.
Definition: Context.php:729
array $groups
User groups (null if the platform does not support the groups enhancement)
Definition: Context.php:83
int $created
Timestamp for when the object was created.
Definition: Context.php:97
saveSettings()
Save setting values.
Definition: Context.php:366
Class to provide a connection to a persistent store for LTI objects.
Class to represent a line item.
Definition: LineItem.php:31
Class to represent a platform.
Definition: Platform.php:36
getDataConnector()
Get the data connector.
Definition: Platform.php:328
Class to implement the Course Groups service.
Definition: Groups.php:31
Class to implement the Membership service.
Definition: Membership.php:33
const MEDIA_TYPE_MEMBERSHIPS_V1
Media type for version 1 of Memberships service.
Definition: Membership.php:37
const MEDIA_TYPE_MEMBERSHIPS_NRPS
Media type for Names and Role Provisioning service.
Definition: Membership.php:42
static string $SCOPE
Access scope.
Definition: Result.php:36
static string $SCOPE
Access scope.
Definition: Score.php:35
Class to implement a service.
Definition: Service.php:34
Class to implement the Tool Settings service.
const MODE_CURRENT_LEVEL
Settings at current level mode.
static logDebug(string $message, bool $showSource=false)
Log a debug message.
Definition: Util.php:363
LTI provider for LTI launch.
static fromRecordId(int $id, ilLTIDataConnector $dataConnector)
Load the platform from the database by its record ID.
$service
Definition: ltiservices.php:43
$scopes
Definition: ltitoken.php:99
if($format !==null) $name
Definition: metadata.php:247
$format
Definition: metadata.php:235
trait ApiHook
Trait to handle API hook registrations.
Definition: ApiHook.php:29
static string $GROUPS_SERVICE_HOOK
Course Groups service hook name.
Definition: ApiHook.php:43
static string $MEMBERSHIPS_SERVICE_HOOK
Memberships service hook name.
Definition: ApiHook.php:48
static getApiHook(string $hookName, string $familyCode)
Get the class name for an API hook.
Definition: ApiHook.php:88
static hasConfiguredApiHook(string $hookName, string $familyCode, $sourceObject)
Check if an API hook is registered and configured.
Definition: ApiHook.php:115
static string $TOOL_SETTINGS_SERVICE_HOOK
Tool Settings service hook name.
Definition: ApiHook.php:58
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: AccessToken.php:19
Class ChatMainBarProvider \MainMenu\Provider.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$url
$http
Definition: raiseError.php:7
$context
Definition: webdav.php:29