ILIAS  release_8 Revision v8.24
DataConnector.php
Go to the documentation of this file.
1<?php
2
20
30//UK: added
32
43{
47 public const PLATFORM_TABLE_NAME = 'lti2_consumer';
48
56
60 public const CONTEXT_TABLE_NAME = 'lti2_context';
61
65 public const RESOURCE_LINK_TABLE_NAME = 'lti2_resource_link';
66
70 public const USER_RESULT_TABLE_NAME = 'lti2_user_result';
71
75 public const RESOURCE_LINK_SHARE_KEY_TABLE_NAME = 'lti2_share_key';
76
80 public const NONCE_TABLE_NAME = 'lti2_nonce';
81
85 public const ACCESS_TOKEN_TABLE_NAME = 'lti2_access_token';
86
90 public const TOOL_TABLE_NAME = 'lti2_tool';
91
97 protected $db = null;
98
104 protected string $dbTableNamePrefix = '';
105
111 protected string $dateFormat = 'Y-m-d';
112
118 protected string $timeFormat = 'H:i:s';
119
125 protected function __construct(object $db, string $dbTableNamePrefix = '')
126 {
127 $this->db = $db;
128 $this->dbTableNamePrefix = $dbTableNamePrefix;
129 }
130
131 ###
132 ### Platform methods
133 ###
134
135// /**
136// * Load tool consumer object.
137// *
138// * @deprecated Use loadPlatform() instead
139// * @see DataConnector::loadPlatform()
140// *
141// * @param ToolConsumer $consumer Tool consumer object
142// *
143// * @return bool True if the tool consumer object was successfully loaded
144// */
145// public function loadToolConsumer($consumer)
146// {
147// Util::logDebug('Method ceLTIc\LTI\DataConnector\DataConnector::loadToolConsumer() has been deprecated; please use ceLTIc\LTI\DataConnector\DataConnector::loadPlatform() instead.',
148// true);
149// return $this->loadPlatform($consumer);
150// }
151
152// /**
153// * Save tool consumer object.
154// *
155// * @deprecated Use savePlatform() instead
156// * @see DataConnector::savePlatform()
157// *
158// * @param ToolConsumer $consumer Tool consumer object
159// *
160// * @return bool True if the tool consumer object was successfully saved
161// */
162// public function saveToolConsumer($consumer)
163// {
164// Util::logDebug('Method ceLTIc\LTI\DataConnector\DataConnector::saveToolConsumer() has been deprecated; please use ceLTIc\LTI\DataConnector\DataConnector::savePlatform() instead.',
165// true);
166// return $this->savePlatform($consumer);
167// }
168
169// /**
170// * Delete tool consumer object.
171// *
172// * @deprecated Use deletePlatform() instead
173// * @see DataConnector::deletePlatform()
174// *
175// * @param ToolConsumer $consumer Tool consumer object
176// *
177// * @return bool True if the tool consumer object was successfully deleted
178// */
179// public function deleteToolConsumer($consumer)
180// {
181// Util::logDebug('Method ceLTIc\LTI\DataConnector\DataConnector::deleteToolConsumer() has been deprecated; please use ceLTIc\LTI\DataConnector\DataConnector::deletePlatform() instead.',
182// true);
183// return $this->deletePlatform($consumer);
184// }
185
186// /**
187// * Load tool consumer objects.
188// *
189// * @deprecated Use getPlatforms() instead
190// * @see DataConnector::getPlatforms()
191// *
192// * @return ToolConsumer[] Array of all defined tool consumer objects
193// */
194// public function getToolConsumers()
195// {
196// Util::logDebug('Method ceLTIc\LTI\DataConnector\DataConnector::getToolConsumers() has been deprecated; please use ceLTIc\LTI\DataConnector\DataConnector::getPlatforms() instead.',
197// true);
198// return $this->getPlatforms();
199// }
200
206 public function loadPlatform(Platform $platform): bool
207 {
208 $platform->secret = 'secret';
209 $platform->enabled = true;
210 $now = time();
211 $platform->created = $now;
212 $platform->updated = $now;
213
214 return true;
215 }
216
222 public function savePlatform(Platform $platform): bool
223 {
224 $platform->updated = time();
225
226 return true;
227 }
228
234 public function deletePlatform(Platform $platform): bool
235 {
236 $platform->initialize();
237
238 return true;
239 }
240
246 public function getPlatforms(): array
247 {
248 return array();
249 }
250
251 ###
252 ### Context methods
253 ###
254
260 public function loadContext(Context $context): bool
261 {
262 $now = time();
263 $context->created = $now;
264 $context->updated = $now;
265
266 return true;
267 }
268
274 public function saveContext(Context $context): bool
275 {
276 $context->updated = time();
277
278 return true;
279 }
280
286 public function deleteContext(Context $context): bool
287 {
288 $context->initialize();
289
290 return true;
291 }
292
293 ###
294 ### ResourceLink methods
295 ###
296
302 public function loadResourceLink(ResourceLink $resourceLink): bool
303 {
304 $now = time();
305 $resourceLink->created = $now;
306 $resourceLink->updated = $now;
307
308 return true;
309 }
310
316 public function saveResourceLink(ResourceLink $resourceLink): bool
317 {
318 $resourceLink->updated = time();
319
320 return true;
321 }
322
328 public function deleteResourceLink(ResourceLink $resourceLink): bool
329 {
330 $resourceLink->initialize();
331
332 return true;
333 }
334
344 public function getUserResultSourcedIDsResourceLink(ResourceLink $resourceLink, bool $localOnly, int $idScope): array
345 {
346 return array();
347 }
348
354 public function getSharesResourceLink(ResourceLink $resourceLink): array
355 {
356 return array();
357 }
358
359 ###
360 ### PlatformNonce methods
361 ###
362
363// /**
364// * Load nonce object.
365// *
366// * @deprecated Use loadPlatformNonce() instead
367// * @see DataConnector::loadPlatformNonce()
368// *
369// * @param ConsumerNonce $nonce Nonce object
370// *
371// * @return bool True if the nonce object was successfully loaded
372// */
373// public function loadConsumerNonce($nonce)
374// {
375// Util::logDebug('Method ceLTIc\LTI\DataConnector\DataConnector::loadConsumerNonce() has been deprecated; please use ceLTIc\LTI\DataConnector\DataConnector::loadPlatformNonce() instead.',
376// true);
377// return $this->loadPlatformNonce($nonce);
378// }
379
380// /**
381// * Save nonce object.
382// *
383// * @deprecated Use savePlatformNonce() instead
384// * @see DataConnector::savePlatformNonce()
385// *
386// * @param ConsumerNonce $nonce Nonce object
387// *
388// * @return bool True if the nonce object was successfully saved
389// */
390// public function saveConsumerNonce($nonce)
391// {
392// Util::logDebug('Method ceLTIc\LTI\DataConnector\DataConnector::saveConsumerNonce() has been deprecated; please use ceLTIc\LTI\DataConnector\DataConnector::savePlatformNonce() instead.',
393// true);
394// return $this->savePlatformNonce($nonce);
395// }
396
397// /**
398// * Delete nonce object.
399// *
400// * @deprecated Use deletePlatformNonce() instead
401// * @see DataConnector::deletePlatformNonce()
402// *
403// * @param ConsumerNonce $nonce Nonce object
404// *
405// * @return bool True if the nonce object was successfully deleted
406// */
407// public function deleteConsumerNonce($nonce)
408// {
409// Util::logDebug('Method ceLTIc\LTI\DataConnector\DataConnector::deleteConsumerNonce() has been deprecated; please use ceLTIc\LTI\DataConnector\DataConnector::deletePlatformNonce() instead.',
410// true);
411// return $this->deletePlatformNonce($nonce);
412// }
413
419 public function loadPlatformNonce(PlatformNonce $nonce): bool
420 {
421 return false; // assume the nonce does not already exist
422 }
423
429 public function savePlatformNonce(PlatformNonce $nonce): bool
430 {
431 return true;
432 }
433
439 public function deletePlatformNonce(PlatformNonce $nonce): bool
440 {
441 return true;
442 }
443
444 ###
445 ### AccessToken methods
446 ###
447
453 public function loadAccessToken(AccessToken $accessToken): bool
454 {
455 return false; // assume the access token does not already exist
456 }
457
463 public function saveAccessToken(AccessToken $accessToken): bool
464 {
465 return true;
466 }
467
468 ###
469 ### ResourceLinkShareKey methods
470 ###
471
477 public function loadResourceLinkShareKey(ResourceLinkShareKey $shareKey): bool
478 {
479 return true;
480 }
481
487 public function saveResourceLinkShareKey(ResourceLinkShareKey $shareKey): bool
488 {
489 return true;
490 }
491
497 public function deleteResourceLinkShareKey(ResourceLinkShareKey $shareKey): bool
498 {
499 return true;
500 }
501
502 ###
503 ### UserResult methods
504 ###
505
511 public function loadUserResult(UserResult $userresult): bool
512 {
513 $now = time();
514 $userresult->created = $now;
515 $userresult->updated = $now;
516
517 return true;
518 }
519
525 public function saveUserResult(UserResult $userresult): bool
526 {
527 $userresult->updated = time();
528
529 return true;
530 }
531
537 public function deleteUserResult(UserResult $userresult): bool
538 {
539 $userresult->initialize();
540
541 return true;
542 }
543
544 ###
545 ### Tool methods
546 ###
547
553 public function loadTool(Tool $tool): bool
554 {
555 $tool->secret = 'secret';
556 $tool->enabled = true;
557 $now = time();
558 $tool->created = $now;
559 $tool->updated = $now;
560
561 return true;
562 }
563
569 public function saveTool(Tool $tool): bool
570 {
571 $tool->updated = time();
572
573 return true;
574 }
575
581 public function deleteTool(Tool $tool): bool
582 {
583 $tool->initialize();
584
585 return true;
586 }
587
593 public function getTools(): array
594 {
595 return array();
596 }
597
598 ###
599 ### Other methods
600 ###
601
614 public static function getDataConnector(object $db = null, string $dbTableNamePrefix = '', string $type = ''): DataConnector
615 {
616 if (is_null($dbTableNamePrefix)) {
618 }
619 if (!is_null($db) && empty($type)) {
620 if (is_object($db)) {
621 $type = get_class($db);
622 } elseif (is_resource($db)) {
623 $type = strtok(get_resource_type($db), ' ');
624 }
625 }
626 $type = strtolower($type);
627 if ($type === 'pdo') {
628 if ($db->getAttribute(\PDO::ATTR_DRIVER_NAME) === 'pgsql') {
629 $type .= '_pgsql';
630 } elseif ($db->getAttribute(\PDO::ATTR_DRIVER_NAME) === 'oci') {
631 $type .= '_oci';
632 }
633 }
634 if (!empty($type)) {
635 $type = "DataConnector_{$type}";
636 } else {
637 $type = 'DataConnector';
638 }
639 $type = "\\ceLTIc\\LTI\\DataConnector\\{$type}";
641
642 return $dataConnector;
643 }
644
645// /**
646// * Generate a random string.
647// *
648// * The generated string will only comprise letters (upper- and lower-case) and digits.
649// *
650// * @deprecated Use Util::getRandomString() instead
651// * @see Util::getRandomString()
652// *
653// * @param int $length Length of string to be generated (optional, default is 8 characters)
654// *
655// * @return string Random string
656// */
657// public static function getRandomString($length = 8)
658// {
659// Util::logDebug('Method ceLTIc\LTI\DataConnector::getRandomString() has been deprecated; please use ceLTIc\LTI\Util::getRandomString() instead.',
660// true);
661// return Util::getRandomString($length);
662// }
663
672 public function escape(string $value, bool $addQuotes = true): string
673 {
674 return static::quoted($value, $addQuotes);
675 }
676
685 public static function quoted(string $value, bool $addQuotes = true): string
686 {
687 if (is_null($value)) {
688 $value = 'null';
689 } else {
690 $value = str_replace('\'', '\'\'', $value);
691 if ($addQuotes) {
692 $value = "'{$value}'";
693 }
694 }
695
696 return $value;
697 }
698
704 protected function fixPlatformSettings(Platform $platform, bool $isSave)
705 {
706 if (!$isSave) {
707 $platform->authorizationServerId = $platform->getSetting('_authorization_server_id', $platform->authorizationServerId);
708 $platform->setSetting('_authorization_server_id');
709 $platform->authenticationUrl = $platform->getSetting('_authentication_request_url', $platform->authenticationUrl);
710 $platform->setSetting('_authentication_request_url');
711 $platform->accessTokenUrl = $platform->getSetting('_oauth2_access_token_url', $platform->accessTokenUrl);
712 $platform->setSetting('_oauth2_access_token_url');
713 $platform->jku = $platform->getSetting('_jku', $platform->jku);
714 $platform->setSetting('_jku');
715 $platform->encryptionMethod = $platform->getSetting('_encryption_method', $platform->encryptionMethod);
716 $platform->setSetting('_encryption_method');
717 $platform->debugMode = $platform->getSetting('_debug', $platform->debugMode ? 'true' : 'false') === 'true';
718 $platform->setSetting('_debug');
719 if ($platform->debugMode) {
721 }
722 } else {
723 $platform->setSetting(
724 '_authorization_server_id',
725 !empty($platform->authorizationServerId) ? $platform->authorizationServerId : null
726 );
727 $platform->setSetting(
728 '_authentication_request_url',
729 !empty($platform->authenticationUrl) ? $platform->authenticationUrl : null
730 );
731 $platform->setSetting('_oauth2_access_token_url', !empty($platform->accessTokenUrl) ? $platform->accessTokenUrl : null);
732 $platform->setSetting('_jku', !empty($platform->jku) ? $platform->jku : null);
733 $platform->setSetting('_encryption_method', !empty($platform->encryptionMethod) ? $platform->encryptionMethod : null);
734 $platform->setSetting('_debug', $platform->debugMode ? 'true' : null);
735 }
736 }
737
743 protected function fixToolSettings(Tool $tool, bool $isSave)
744 {
745 if (!$isSave) {
746 $tool->encryptionMethod = $tool->getSetting('_encryption_method', $tool->encryptionMethod);
747 $tool->setSetting('_encryption_method');
748 $tool->debugMode = $tool->getSetting('_debug', $tool->debugMode ? 'true' : 'false') === 'true';
749 $tool->setSetting('_debug');
750 if ($tool->debugMode) {
752 }
753 } else {
754 $tool->setSetting('_encryption_method', !empty($tool->encryptionMethod) ? $tool->encryptionMethod : null);
755 $tool->setSetting('_debug', $tool->debugMode ? 'true' : null);
756 }
757 }
758}
Class to represent an HTTP message.
Definition: AccessToken.php:33
Class to represent a platform context.
Definition: Context.php:34
Class to provide a connection to a persistent store for LTI objects.
loadResourceLinkShareKey(ResourceLinkShareKey $shareKey)
Load resource link share key object.
loadResourceLink(ResourceLink $resourceLink)
Load resource link object.
static quoted(string $value, bool $addQuotes=true)
Quote a string for use in a database query.
deletePlatformNonce(PlatformNonce $nonce)
Delete nonce object.
getSharesResourceLink(ResourceLink $resourceLink)
Get array of shares defined for this resource link.
saveAccessToken(AccessToken $accessToken)
Save access token object.
const CONSUMER_TABLE_NAME
Default name for database table used to store platforms.
savePlatformNonce(PlatformNonce $nonce)
Save nonce object.
const USER_RESULT_TABLE_NAME
Default name for database table used to store users.
static getDataConnector(object $db=null, string $dbTableNamePrefix='', string $type='')
Create data connector object.
loadAccessToken(AccessToken $accessToken)
Load access token object.
getUserResultSourcedIDsResourceLink(ResourceLink $resourceLink, bool $localOnly, int $idScope)
Get array of user objects.
saveResourceLink(ResourceLink $resourceLink)
Save resource link object.
saveContext(Context $context)
Save context object.
const CONTEXT_TABLE_NAME
Default name for database table used to store contexts.
fixPlatformSettings(Platform $platform, bool $isSave)
Adjust the settings for any platform properties being stored as a setting value.
const ACCESS_TOKEN_TABLE_NAME
Default name for database table used to store access token values.
fixToolSettings(Tool $tool, bool $isSave)
Adjust the settings for any tool properties being stored as a setting value.
loadContext(Context $context)
Load context object.
deletePlatform(Platform $platform)
Delete platform object.
deleteResourceLinkShareKey(ResourceLinkShareKey $shareKey)
Delete resource link share key object.
deleteContext(Context $context)
Delete context object.
savePlatform(Platform $platform)
Save platform object.
loadPlatform(Platform $platform)
Load platform object.
const NONCE_TABLE_NAME
Default name for database table used to store nonce values.
const RESOURCE_LINK_SHARE_KEY_TABLE_NAME
Default name for database table used to store resource link share keys.
loadPlatformNonce(PlatformNonce $nonce)
Load nonce object.
loadUserResult(UserResult $userresult)
Load user object.
escape(string $value, bool $addQuotes=true)
Escape a string for use in a database query.
string $dbTableNamePrefix
Prefix for database table names.
string $dateFormat
SQL date format (default = 'Y-m-d')
const TOOL_TABLE_NAME
Default name for database table used to store tools.
string $timeFormat
SQL time format (default = 'H:i:s')
deleteUserResult(UserResult $userresult)
Delete user object.
deleteResourceLink(ResourceLink $resourceLink)
Delete resource link object.
saveResourceLinkShareKey(ResourceLinkShareKey $shareKey)
Save resource link share key object.
saveUserResult(UserResult $userresult)
Save user object.
const RESOURCE_LINK_TABLE_NAME
Default name for database table used to store resource links.
const PLATFORM_TABLE_NAME
Default name for database table used to store platforms.
__construct(object $db, string $dbTableNamePrefix='')
Class constructor.
Class to represent a platform nonce.
Class to represent a platform.
Definition: Platform.php:36
initialize()
Initialise the platform.
Definition: Platform.php:216
Class to represent a platform resource link share key.
Class to represent a platform resource link share.
Class to represent an LTI Tool.
Definition: Tool.php:39
initialize()
Initialise the tool.
Definition: Tool.php:381
Class to represent a platform user.
Definition: UserResult.php:31
initialize()
Initialise the user.
Definition: UserResult.php:92
const LOGLEVEL_DEBUG
Log all messages.
Definition: Util.php:156
static int $logLevel
Current logging level.
Definition: Util.php:189
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
ilLTIDataConnector $dataConnector
Data connector object.
Definition: System.php:64
$type
$context
Definition: webdav.php:29