LTI Integration Library  3.1.0
PHP class library for building LTI integrations
DataConnector.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use ceLTIc\LTI;
13 
25 {
26 
30  const CONSUMER_TABLE_NAME = 'lti2_consumer';
31 
35  const CONTEXT_TABLE_NAME = 'lti2_context';
36 
40  const RESOURCE_LINK_TABLE_NAME = 'lti2_resource_link';
41 
45  const USER_RESULT_TABLE_NAME = 'lti2_user_result';
46 
50  const RESOURCE_LINK_SHARE_KEY_TABLE_NAME = 'lti2_share_key';
51 
55  const NONCE_TABLE_NAME = 'lti2_nonce';
56 
62  protected $db = null;
63 
69  protected $dbTableNamePrefix = '';
70 
76  protected $dateFormat = 'Y-m-d';
77 
83  protected $timeFormat = 'H:i:s';
84 
91  public function __construct($db, $dbTableNamePrefix = '')
92  {
93  $this->db = $db;
94  $this->dbTableNamePrefix = $dbTableNamePrefix;
95  }
96 
97 ###
98 ### ToolConsumer methods
99 ###
100 
108  public function loadToolConsumer($consumer)
109  {
110  $consumer->secret = 'secret';
111  $consumer->enabled = true;
112  $now = time();
113  $consumer->created = $now;
114  $consumer->updated = $now;
115 
116  return true;
117  }
118 
126  public function saveToolConsumer($consumer)
127  {
128  $consumer->updated = time();
129 
130  return true;
131  }
132 
140  public function deleteToolConsumer($consumer)
141  {
142  $consumer->initialize();
143 
144  return true;
145  }
146 
152  public function getToolConsumers()
153  {
154  return array();
155  }
156 
157 ###
158 ### Context methods
159 ###
160 
168  public function loadContext($context)
169  {
170  $now = time();
171  $context->created = $now;
172  $context->updated = $now;
173 
174  return true;
175  }
176 
184  public function saveContext($context)
185  {
186  $context->updated = time();
187 
188  return true;
189  }
190 
198  public function deleteContext($context)
199  {
200  $context->initialize();
201 
202  return true;
203  }
204 
205 ###
206 ### ResourceLink methods
207 ###
208 
216  public function loadResourceLink($resourceLink)
217  {
218  $now = time();
219  $resourceLink->created = $now;
220  $resourceLink->updated = $now;
221 
222  return true;
223  }
224 
232  public function saveResourceLink($resourceLink)
233  {
234  $resourceLink->updated = time();
235 
236  return true;
237  }
238 
246  public function deleteResourceLink($resourceLink)
247  {
248  $resourceLink->initialize();
249 
250  return true;
251  }
252 
265  public function getUserResultSourcedIDsResourceLink($resourceLink, $localOnly, $idScope)
266  {
267  return array();
268  }
269 
277  public function getSharesResourceLink($resourceLink)
278  {
279  return array();
280  }
281 
282 ###
283 ### ConsumerNonce methods
284 ###
285 
293  public function loadConsumerNonce($nonce)
294  {
295  return false; // assume the nonce does not already exist
296  }
297 
305  public function saveConsumerNonce($nonce)
306  {
307  return true;
308  }
309 
310 ###
311 ### ResourceLinkShareKey methods
312 ###
313 
321  public function loadResourceLinkShareKey($shareKey)
322  {
323  return true;
324  }
325 
333  public function saveResourceLinkShareKey($shareKey)
334  {
335  return true;
336  }
337 
345  public function deleteResourceLinkShareKey($shareKey)
346  {
347  return true;
348  }
349 
350 ###
351 ### UserResult methods
352 ###
353 
361  public function loadUserResult($userresult)
362  {
363  $now = time();
364  $userresult->created = $now;
365  $userresult->updated = $now;
366 
367  return true;
368  }
369 
377  public function saveUserResult($userresult)
378  {
379  $userresult->updated = time();
380 
381  return true;
382  }
383 
391  public function deleteUserResult($userresult)
392  {
393  $userresult->initialize();
394 
395  return true;
396  }
397 
398 ###
399 ### Other methods
400 ###
401 
418  public static function getDataConnector($db = null, $dbTableNamePrefix = '', $type = '')
419  {
420  if (is_null($dbTableNamePrefix)) {
421  $dbTableNamePrefix = '';
422  }
423  if (!is_null($db) && empty($type)) {
424  if (is_object($db)) {
425  $type = get_class($db);
426  } elseif (is_resource($db)) {
427  $type = strtok(get_resource_type($db), ' ');
428  }
429  }
430  $type = strtolower($type);
431  if ($type === 'pdo') {
432  if ($db->getAttribute(\PDO::ATTR_DRIVER_NAME) === 'pgsql') {
433  $type .= '_pgsql';
434  } elseif ($db->getAttribute(\PDO::ATTR_DRIVER_NAME) === 'oci') {
435  $type .= '_oci';
436  }
437  }
438  if (!empty($type)) {
439  $type = "DataConnector_{$type}";
440  } else {
441  $type = 'DataConnector';
442  }
443  $type = "\\ceLTIc\\LTI\\DataConnector\\{$type}";
444  $dataConnector = new $type($db, $dbTableNamePrefix);
445 
446  return $dataConnector;
447  }
448 
458  public static function getRandomString($length = 8)
459  {
460  $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
461 
462  $value = '';
463  $charsLength = strlen($chars) - 1;
464 
465  for ($i = 1; $i <= $length; $i++) {
466  $value .= $chars[rand(0, $charsLength)];
467  }
468 
469  return $value;
470  }
471 
483  public function escape($value, $addQuotes = true)
484  {
485  return static::quoted($value, $addQuotes);
486  }
487 
499  public static function quoted($value, $addQuotes = true)
500  {
501  if (is_null($value)) {
502  $value = 'null';
503  } else {
504  $value = str_replace('\'', '\'\'', $value);
505  if ($addQuotes) {
506  $value = "'{$value}'";
507  }
508  }
509 
510  return $value;
511  }
512 
519  protected static function getConsumerKey($key)
520  {
521  $len = strlen($key);
522  if ($len > 255) {
523  $key = 'sha512:' . hash('sha512', $key);
524  }
525 
526  return $key;
527  }
528 
529 }
$dbTableNamePrefix
Prefix for database table names.
const NONCE_TABLE_NAME
Default name for database table used to store nonce values.
Class to provide a connection to a persistent store for LTI objects.
getUserResultSourcedIDsResourceLink($resourceLink, $localOnly, $idScope)
Get array of user objects.
escape($value, $addQuotes=true)
Escape a string for use in a database query.
$timeFormat
SQL time format (default = 'H:i:s')
const CONTEXT_TABLE_NAME
Default name for database table used to store contexts.
Class to represent a tool consumer.
static getConsumerKey($key)
Return a hash of a consumer key for values longer than 255 characters.
__construct($db, $dbTableNamePrefix='')
Class constructor.
Class to represent a tool consumer context.
Definition: Context.php:17
static getDataConnector($db=null, $dbTableNamePrefix='', $type='')
Create data connector object.
loadConsumerNonce($nonce)
Load nonce object.
getSharesResourceLink($resourceLink)
Get array of shares defined for this resource link.
deleteContext($context)
Delete context object.
loadContext($context)
Load context object.
const RESOURCE_LINK_SHARE_KEY_TABLE_NAME
Default name for database table used to store resource link share keys.
deleteResourceLink($resourceLink)
Delete resource link object.
Class to represent a tool consumer nonce.
Class to represent a tool consumer resource link share.
getToolConsumers()
Load tool consumer objects.
static quoted($value, $addQuotes=true)
Quote a string for use in a database query.
loadResourceLinkShareKey($shareKey)
Load resource link share key object.
deleteResourceLinkShareKey($shareKey)
Delete resource link share key object.
const RESOURCE_LINK_TABLE_NAME
Default name for database table used to store resource links.
const CONSUMER_TABLE_NAME
Default name for database table used to store tool consumers.
Class to represent a tool consumer user.
Definition: UserResult.php:15
saveUserResult($userresult)
Save user object.
deleteUserResult($userresult)
Delete user object.
loadToolConsumer($consumer)
Load tool consumer object.
deleteToolConsumer($consumer)
Delete tool consumer object.
saveContext($context)
Save context object.
loadResourceLink($resourceLink)
Load resource link object.
Class to represent a tool consumer resource link share key.
loadUserResult($userresult)
Load user object.
static getRandomString($length=8)
Generate a random string.
$dateFormat
SQL date format (default = 'Y-m-d')
saveResourceLink($resourceLink)
Save resource link object.
saveResourceLinkShareKey($shareKey)
Save resource link share key object.
saveToolConsumer($consumer)
Save tool consumer object.
saveConsumerNonce($nonce)
Save nonce object.
const USER_RESULT_TABLE_NAME
Default name for database table used to store users.