LTI Integration Library 4.10.3
PHP class library for building LTI integrations
 
Loading...
Searching...
No Matches
DataConnector_mysql.php
1<?php
2
4
5use ceLTIc\LTI;
16
27###
28# NB This class assumes that a MySQL connection has already been opened to the appropriate schema
29###
30
31
33{
34
41 public function __construct($db, $dbTableNamePrefix = '')
42 {
43 parent::__construct($db, $dbTableNamePrefix);
44 Util::logDebug('Class ceLTIc\LTI\DataConnector\DataConnector_mysql has been deprecated; please use ceLTIc\LTI\DataConnector\DataConnector_mysqli instead.',
45 true);
46 }
47
48###
49### Platform methods
50###
51
59 public function loadPlatform($platform)
60 {
61 $ok = false;
62 $allowMultiple = false;
63 if (!is_null($platform->getRecordId())) {
64 $sql = sprintf('SELECT consumer_pk, name, consumer_key, secret, ' .
65 'platform_id, client_id, deployment_id, public_key, ' .
66 'lti_version, signature_method, consumer_name, consumer_version, consumer_guid, ' .
67 'profile, tool_proxy, settings, protected, enabled, ' .
68 'enable_from, enable_until, last_access, created, updated ' .
69 "FROM {$this->dbTableNamePrefix}" . static::PLATFORM_TABLE_NAME . ' ' .
70 'WHERE consumer_pk = %d', $platform->getRecordId());
71 } elseif (!empty($platform->platformId)) {
72 if (empty($platform->clientId)) {
73 $allowMultiple = true;
74 $sql = sprintf('SELECT consumer_pk, name, consumer_key, secret, ' .
75 'platform_id, client_id, deployment_id, public_key, ' .
76 'lti_version, signature_method, consumer_name, consumer_version, consumer_guid, ' .
77 'profile, tool_proxy, settings, protected, enabled, ' .
78 'enable_from, enable_until, last_access, created, updated ' .
79 "FROM {$this->dbTableNamePrefix}" . static::PLATFORM_TABLE_NAME . ' ' .
80 'WHERE (platform_id = %s) ', $this->escape($platform->platformId));
81 } elseif (empty($platform->deploymentId)) {
82 $allowMultiple = true;
83 $sql = sprintf('SELECT consumer_pk, name, consumer_key, secret, ' .
84 'platform_id, client_id, deployment_id, public_key, ' .
85 'lti_version, signature_method, consumer_name, consumer_version, consumer_guid, ' .
86 'profile, tool_proxy, settings, protected, enabled, ' .
87 'enable_from, enable_until, last_access, created, updated ' .
88 "FROM {$this->dbTableNamePrefix}" . static::PLATFORM_TABLE_NAME . ' ' .
89 'WHERE (platform_id = %s) AND (client_id = %s)', $this->escape($platform->platformId),
90 $this->escape($platform->clientId));
91 } else {
92 $sql = sprintf('SELECT consumer_pk, name, consumer_key, secret, ' .
93 'platform_id, client_id, deployment_id, public_key, ' .
94 'lti_version, signature_method, consumer_name, consumer_version, consumer_guid, ' .
95 'profile, tool_proxy, settings, protected, enabled, ' .
96 'enable_from, enable_until, last_access, created, updated ' .
97 "FROM {$this->dbTableNamePrefix}" . static::PLATFORM_TABLE_NAME . ' ' .
98 'WHERE (platform_id = %s) AND (client_id = %s) AND (deployment_id = %s)', $this->escape($platform->platformId),
99 $this->escape($platform->clientId), $this->escape($platform->deploymentId));
100 }
101 } else {
102 $sql = sprintf('SELECT consumer_pk, name, consumer_key, secret, ' .
103 'platform_id, client_id, deployment_id, public_key, ' .
104 'lti_version, signature_method, consumer_name, consumer_version, consumer_guid, ' .
105 'profile, tool_proxy, settings, protected, enabled, ' .
106 'enable_from, enable_until, last_access, created, updated ' .
107 "FROM {$this->dbTableNamePrefix}" . static::PLATFORM_TABLE_NAME . ' ' .
108 'WHERE consumer_key = %s', $this->escape($platform->getKey()));
109 }
110 $rsConsumer = $this->executeQuery($sql);
111 if ($rsConsumer) {
112 $row = mysql_fetch_object($rsConsumer);
113 if ($row && ($allowMultiple || !mysql_fetch_object($rsConsumer))) {
114 $platform->setRecordId(intval($row->consumer_pk));
115 $platform->name = $row->name;
116 $platform->setkey($row->consumer_key);
117 $platform->secret = $row->secret;
118 $platform->platformId = $row->platform_id;
119 $platform->clientId = $row->client_id;
120 $platform->deploymentId = $row->deployment_id;
121 $platform->rsaKey = $row->public_key;
122 $platform->ltiVersion = $row->lti_version;
123 $platform->signatureMethod = $row->signature_method;
124 $platform->consumerName = $row->consumer_name;
125 $platform->consumerVersion = $row->consumer_version;
126 $platform->consumerGuid = $row->consumer_guid;
127 $platform->profile = Util::jsonDecode($row->profile);
128 $platform->toolProxy = $row->tool_proxy;
129 $settings = Util::jsonDecode($row->settings, true);
130 if (!is_array($settings)) {
131 $settings = @unserialize($row->settings); // check for old serialized setting
132 }
133 if (!is_array($settings)) {
134 $settings = array();
135 }
136 $platform->setSettings($settings);
137 $platform->protected = (intval($row->protected) === 1);
138 $platform->enabled = (intval($row->enabled) === 1);
139 $platform->enableFrom = null;
140 if (!is_null($row->enable_from)) {
141 $platform->enableFrom = strtotime($row->enable_from);
142 }
143 $platform->enableUntil = null;
144 if (!is_null($row->enable_until)) {
145 $platform->enableUntil = strtotime($row->enable_until);
146 }
147 $platform->lastAccess = null;
148 if (!is_null($row->last_access)) {
149 $platform->lastAccess = strtotime($row->last_access);
150 }
151 $platform->created = strtotime($row->created);
152 $platform->updated = strtotime($row->updated);
153 $this->fixPlatformSettings($platform, false);
154 $ok = true;
155 }
156 }
157
158 return $ok;
159 }
160
168 public function savePlatform($platform)
169 {
170 $id = $platform->getRecordId();
171 $protected = ($platform->protected) ? 1 : 0;
172 $enabled = ($platform->enabled) ? 1 : 0;
173 $profile = (!empty($platform->profile)) ? json_encode($platform->profile) : null;
174 $this->fixPlatformSettings($platform, true);
175 $settingsValue = json_encode($platform->getSettings());
176 $this->fixPlatformSettings($platform, false);
177 $time = time();
178 $now = date("{$this->dateFormat} {$this->timeFormat}", $time);
179 $from = null;
180 if (!is_null($platform->enableFrom)) {
181 $from = date("{$this->dateFormat} {$this->timeFormat}", $platform->enableFrom);
182 }
183 $until = null;
184 if (!is_null($platform->enableUntil)) {
185 $until = date("{$this->dateFormat} {$this->timeFormat}", $platform->enableUntil);
186 }
187 $last = null;
188 if (!is_null($platform->lastAccess)) {
189 $last = date($this->dateFormat, $platform->lastAccess);
190 }
191 if (empty($id)) {
192 $sql = sprintf("INSERT INTO {$this->dbTableNamePrefix}" . static::PLATFORM_TABLE_NAME . ' (consumer_key, name, secret, ' .
193 'platform_id, client_id, deployment_id, public_key, ' .
194 'lti_version, signature_method, consumer_name, consumer_version, consumer_guid, ' .
195 'profile, tool_proxy, settings, protected, enabled, ' .
196 'enable_from, enable_until, last_access, created, updated) ' .
197 'VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %d, %d, %s, %s, %s, %s, %s)',
198 $this->escape($platform->getKey()), $this->escape($platform->name), $this->escape($platform->secret),
199 $this->escape($platform->platformId), $this->escape($platform->clientId), $this->escape($platform->deploymentId),
200 $this->escape($platform->rsaKey), $this->escape($platform->ltiVersion), $this->escape($platform->signatureMethod),
201 $this->escape($platform->consumerName), $this->escape($platform->consumerVersion),
202 $this->escape($platform->consumerGuid), $this->escape($profile), $this->escape($platform->toolProxy),
203 $this->escape($settingsValue), $protected, $enabled, $this->escape($from), $this->escape($until),
204 $this->escape($last), $this->escape($now), $this->escape($now));
205 } else {
206 $sql = sprintf("UPDATE {$this->dbTableNamePrefix}" . static::PLATFORM_TABLE_NAME . ' SET ' .
207 'consumer_key = %s, name = %s, secret= %s, ' .
208 'platform_id = %s, client_id = %s, deployment_id = %s, public_key = %s, ' .
209 'lti_version = %s, signature_method = %s, ' .
210 'consumer_name = %s, consumer_version = %s, consumer_guid = %s, ' .
211 'profile = %s, tool_proxy = %s, settings = %s, ' .
212 'protected = %d, enabled = %d, enable_from = %s, enable_until = %s, last_access = %s, updated = %s ' .
213 'WHERE consumer_pk = %d', $this->escape($platform->getKey()), $this->escape($platform->name),
214 $this->escape($platform->secret), $this->escape($platform->platformId), $this->escape($platform->clientId),
215 $this->escape($platform->deploymentId), $this->escape($platform->rsaKey), $this->escape($platform->ltiVersion),
216 $this->escape($platform->signatureMethod), $this->escape($platform->consumerName),
217 $this->escape($platform->consumerVersion), $this->escape($platform->consumerGuid), $this->escape($profile),
218 $this->escape($platform->toolProxy), $this->escape($settingsValue), $protected, $enabled, $this->escape($from),
219 $this->escape($until), $this->escape($last), $this->escape($now), $platform->getRecordId());
220 }
221 $ok = $this->executeQuery($sql);
222 if ($ok) {
223 if (empty($id)) {
224 $platform->setRecordId(mysql_insert_id());
225 $platform->created = $time;
226 }
227 $platform->updated = $time;
228 }
229
230 return $ok;
231 }
232
240 public function deletePlatform($platform)
241 {
242// Delete any access token value for this consumer
243 $sql = sprintf("DELETE FROM {$this->dbTableNamePrefix}" . static::ACCESS_TOKEN_TABLE_NAME . ' WHERE consumer_pk = %d',
244 $platform->getRecordId());
245 $this->executeQuery($sql);
246
247// Delete any nonce values for this consumer
248 $sql = sprintf("DELETE FROM {$this->dbTableNamePrefix}" . static::NONCE_TABLE_NAME . ' WHERE consumer_pk = %d',
249 $platform->getRecordId());
250 $this->executeQuery($sql);
251
252// Delete any outstanding share keys for resource links for this consumer
253 $sql = sprintf('DELETE sk ' .
254 "FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_SHARE_KEY_TABLE_NAME . ' sk ' .
255 "INNER JOIN {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' rl ON sk.resource_link_pk = rl.resource_link_pk ' .
256 'WHERE rl.consumer_pk = %d', $platform->getRecordId());
257 $this->executeQuery($sql);
258
259// Delete any outstanding share keys for resource links for contexts in this consumer
260 $sql = sprintf('DELETE sk ' .
261 "FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_SHARE_KEY_TABLE_NAME . ' sk ' .
262 "INNER JOIN {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' rl ON sk.resource_link_pk = rl.resource_link_pk ' .
263 "INNER JOIN {$this->dbTableNamePrefix}" . static::CONTEXT_TABLE_NAME . ' c ON rl.context_pk = c.context_pk ' .
264 'WHERE c.consumer_pk = %d', $platform->getRecordId());
265 $this->executeQuery($sql);
266
267// Delete any users in resource links for this consumer
268 $sql = sprintf('DELETE u ' .
269 "FROM {$this->dbTableNamePrefix}" . static::USER_RESULT_TABLE_NAME . ' u ' .
270 "INNER JOIN {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' rl ON u.resource_link_pk = rl.resource_link_pk ' .
271 'WHERE rl.consumer_pk = %d', $platform->getRecordId());
272 $this->executeQuery($sql);
273
274// Delete any users in resource links for contexts in this consumer
275 $sql = sprintf('DELETE u ' .
276 "FROM {$this->dbTableNamePrefix}" . static::USER_RESULT_TABLE_NAME . ' u ' .
277 "INNER JOIN {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' rl ON u.resource_link_pk = rl.resource_link_pk ' .
278 "INNER JOIN {$this->dbTableNamePrefix}" . static::CONTEXT_TABLE_NAME . ' c ON rl.context_pk = c.context_pk ' .
279 'WHERE c.consumer_pk = %d', $platform->getRecordId());
280 $this->executeQuery($sql);
281
282// Update any resource links for which this consumer is acting as a primary resource link
283 $sql = sprintf("UPDATE {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' prl ' .
284 "INNER JOIN {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' rl ON prl.primary_resource_link_pk = rl.resource_link_pk ' .
285 'SET prl.primary_resource_link_pk = NULL, prl.share_approved = NULL ' .
286 'WHERE rl.consumer_pk = %d', $platform->getRecordId());
287 $ok = $this->executeQuery($sql);
288
289// Update any resource links for contexts in which this consumer is acting as a primary resource link
290 $sql = sprintf("UPDATE {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' prl ' .
291 "INNER JOIN {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' rl ON prl.primary_resource_link_pk = rl.resource_link_pk ' .
292 "INNER JOIN {$this->dbTableNamePrefix}" . static::CONTEXT_TABLE_NAME . ' c ON rl.context_pk = c.context_pk ' .
293 'SET prl.primary_resource_link_pk = NULL, prl.share_approved = NULL ' .
294 'WHERE c.consumer_pk = %d', $platform->getRecordId());
295 $ok = $this->executeQuery($sql);
296
297// Delete any resource links for this consumer
298 $sql = sprintf('DELETE rl ' .
299 "FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' rl ' .
300 'WHERE rl.consumer_pk = %d', $platform->getRecordId());
301 $this->executeQuery($sql);
302
303// Delete any resource links for contexts in this consumer
304 $sql = sprintf('DELETE rl ' .
305 "FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' rl ' .
306 "INNER JOIN {$this->dbTableNamePrefix}" . static::CONTEXT_TABLE_NAME . ' c ON rl.context_pk = c.context_pk ' .
307 'WHERE c.consumer_pk = %d', $platform->getRecordId());
308 $this->executeQuery($sql);
309
310// Delete any contexts for this consumer
311 $sql = sprintf('DELETE c ' .
312 "FROM {$this->dbTableNamePrefix}" . static::CONTEXT_TABLE_NAME . ' c ' .
313 'WHERE c.consumer_pk = %d', $platform->getRecordId());
314 $this->executeQuery($sql);
315
316// Delete consumer
317 $sql = sprintf('DELETE c ' .
318 "FROM {$this->dbTableNamePrefix}" . static::PLATFORM_TABLE_NAME . ' c ' .
319 'WHERE c.consumer_pk = %d', $platform->getRecordId());
320 $ok = $this->executeQuery($sql);
321
322 if ($ok) {
323 $platform->initialize();
324 }
325
326 return $ok;
327 }
328
334 public function getPlatforms()
335 {
336 $platforms = array();
337
338 $sql = 'SELECT consumer_pk, consumer_key, name, secret, ' .
339 'platform_id, client_id, deployment_id, public_key, ' .
340 'lti_version, signature_method, consumer_name, consumer_version, consumer_guid, ' .
341 'profile, tool_proxy, settings, ' .
342 'protected, enabled, enable_from, enable_until, last_access, created, updated ' .
343 "FROM {$this->dbTableNamePrefix}" . static::PLATFORM_TABLE_NAME . ' ' .
344 'ORDER BY name';
345 $rsConsumers = $this->executeQuery($sql);
346 if ($rsConsumers) {
347 while ($row = mysql_fetch_object($rsConsumers)) {
348 $platform = new Platform($this);
349 $platform->setRecordId(intval($row->consumer_pk));
350 $platform->name = $row->name;
351 $platform->setKey($row->consumer_key);
352 $platform->secret = $row->secret;
353 $platform->platformId = $row->platform_id;
354 $platform->clientId = $row->client_id;
355 $platform->deploymentId = $row->deployment_id;
356 $platform->rsaKey = $row->public_key;
357 $platform->ltiVersion = $row->lti_version;
358 $platform->signatureMethod = $row->signature_method;
359 $platform->consumerName = $row->consumer_name;
360 $platform->consumerVersion = $row->consumer_version;
361 $platform->consumerGuid = $row->consumer_guid;
362 $platform->profile = Util::jsonDecode($row->profile);
363 $platform->toolProxy = $row->tool_proxy;
364 $settings = Util::jsonDecode($row->settings, true);
365 if (!is_array($settings)) {
366 $settings = @unserialize($row->settings); // check for old serialized setting
367 }
368 if (!is_array($settings)) {
369 $settings = array();
370 }
371 $platform->setSettings($settings);
372 $platform->protected = (intval($row->protected) === 1);
373 $platform->enabled = (intval($row->enabled) === 1);
374 $platform->enableFrom = null;
375 if (!is_null($row->enable_from)) {
376 $platform->enableFrom = strtotime($row->enable_from);
377 }
378 $platform->enableUntil = null;
379 if (!is_null($row->enable_until)) {
380 $platform->enableUntil = strtotime($row->enable_until);
381 }
382 $platform->lastAccess = null;
383 if (!is_null($row->last_access)) {
384 $platform->lastAccess = strtotime($row->last_access);
385 }
386 $platform->created = strtotime($row->created);
387 $platform->updated = strtotime($row->updated);
388 $this->fixPlatformSettings($platform, false);
389 $platforms[] = $platform;
390 }
391 mysql_free_result($rsConsumers);
392 }
393
394 return $platforms;
395 }
396
397###
398### Context methods
399###
400
408 public function loadContext($context)
409 {
410 $ok = false;
411 if (!is_null($context->getRecordId())) {
412 $sql = sprintf('SELECT context_pk, consumer_pk, title, lti_context_id, type, settings, created, updated ' .
413 "FROM {$this->dbTableNamePrefix}" . static::CONTEXT_TABLE_NAME . ' ' .
414 'WHERE (context_pk = %d)', $context->getRecordId());
415 } else {
416 $sql = sprintf('SELECT context_pk, consumer_pk, title, lti_context_id, type, settings, created, updated ' .
417 "FROM {$this->dbTableNamePrefix}" . static::CONTEXT_TABLE_NAME . ' ' .
418 'WHERE (consumer_pk = %d) AND (lti_context_id = %s)', $context->getPlatform()->getRecordId(),
419 $this->escape($context->ltiContextId));
420 }
421 $rsContext = $this->executeQuery($sql);
422 if ($rsContext) {
423 $row = mysql_fetch_object($rsContext);
424 if ($row) {
425 $context->setRecordId(intval($row->context_pk));
426 $context->setPlatformId(intval($row->consumer_pk));
427 $context->title = $row->title;
428 $context->ltiContextId = $row->lti_context_id;
429 $context->type = $row->type;
430 $settings = Util::jsonDecode($row->settings, true);
431 if (!is_array($settings)) {
432 $settings = @unserialize($row->settings); // check for old serialized setting
433 }
434 if (!is_array($settings)) {
435 $settings = array();
436 }
437 $context->setSettings($settings);
438 $context->created = strtotime($row->created);
439 $context->updated = strtotime($row->updated);
440 $ok = true;
441 }
442 }
443
444 return $ok;
445 }
446
454 public function saveContext($context)
455 {
456 $time = time();
457 $now = date("{$this->dateFormat} {$this->timeFormat}", $time);
458 $settingsValue = json_encode($context->getSettings());
459 $id = $context->getRecordId();
460 $consumer_pk = $context->getPlatform()->getRecordId();
461 if (empty($id)) {
462 $sql = sprintf("INSERT INTO {$this->dbTableNamePrefix}" . static::CONTEXT_TABLE_NAME . ' (consumer_pk, title, ' .
463 'lti_context_id, type, settings, created, updated) ' .
464 'VALUES (%d, %s, %s, %s, %s, %s, %s)', $consumer_pk, $this->escape($context->title),
465 $this->escape($context->ltiContextId), $this->escape($context->type), $this->escape($settingsValue),
466 $this->escape($now), $this->escape($now));
467 } else {
468 $sql = sprintf("UPDATE {$this->dbTableNamePrefix}" . static::CONTEXT_TABLE_NAME . ' SET ' .
469 'title = %s, lti_context_id = %s, type = %s, settings = %s, ' .
470 'updated = %s' .
471 'WHERE (consumer_pk = %d) AND (context_pk = %d)', $this->escape($context->title),
472 $this->escape($context->ltiContextId), $this->escape($context->type), $this->escape($settingsValue),
473 $this->escape($now), $consumer_pk, $id);
474 }
475 $ok = $this->executeQuery($sql);
476 if ($ok) {
477 if (empty($id)) {
478 $context->setRecordId(mysql_insert_id());
479 $context->created = $time;
480 }
481 $context->updated = $time;
482 }
483
484 return $ok;
485 }
486
494 public function deleteContext($context)
495 {
496// Delete any outstanding share keys for resource links for this context
497 $sql = sprintf('DELETE sk ' .
498 "FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_SHARE_KEY_TABLE_NAME . ' sk ' .
499 "INNER JOIN {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' rl ON sk.resource_link_pk = rl.resource_link_pk ' .
500 'WHERE rl.context_pk = %d', $context->getRecordId());
501 $this->executeQuery($sql);
502
503// Delete any users in resource links for this context
504 $sql = sprintf('DELETE u ' .
505 "FROM {$this->dbTableNamePrefix}" . static::USER_RESULT_TABLE_NAME . ' u ' .
506 "INNER JOIN {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' rl ON u.resource_link_pk = rl.resource_link_pk ' .
507 'WHERE rl.context_pk = %d', $context->getRecordId());
508 $this->executeQuery($sql);
509
510// Update any resource links for which this consumer is acting as a primary resource link
511 $sql = sprintf("UPDATE {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' prl ' .
512 "INNER JOIN {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' rl ON prl.primary_resource_link_pk = rl.resource_link_pk ' .
513 'SET prl.primary_resource_link_pk = null, prl.share_approved = null ' .
514 'WHERE rl.context_pk = %d', $context->getRecordId());
515 $ok = $this->executeQuery($sql);
516
517// Delete any resource links for this consumer
518 $sql = sprintf('DELETE rl ' .
519 "FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' rl ' .
520 'WHERE rl.context_pk = %d', $context->getRecordId());
521 $this->executeQuery($sql);
522
523// Delete context
524 $sql = sprintf('DELETE c ' .
525 "FROM {$this->dbTableNamePrefix}" . static::CONTEXT_TABLE_NAME . ' c ', 'WHERE c.context_pk = %d',
526 $context->getRecordId());
527 $ok = $this->executeQuery($sql);
528 if ($ok) {
529 $context->initialize();
530 }
531
532 return $ok;
533 }
534
535###
536### ResourceLink methods
537###
538
546 public function loadResourceLink($resourceLink)
547 {
548 $ok = false;
549 if (!is_null($resourceLink->getRecordId())) {
550 $sql = sprintf('SELECT resource_link_pk, context_pk, consumer_pk, title, lti_resource_link_id, settings, primary_resource_link_pk, share_approved, created, updated ' .
551 "FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' ' .
552 'WHERE (resource_link_pk = %d)', $resourceLink->getRecordId());
553 } elseif (!is_null($resourceLink->getContext())) {
554 $sql = sprintf('SELECT r.resource_link_pk, r.context_pk, r.consumer_pk, r.title, r.lti_resource_link_id, r.settings, r.primary_resource_link_pk, r.share_approved, r.created, r.updated ' .
555 "FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' r ' .
556 'WHERE (r.lti_resource_link_id = %s) AND ((r.context_pk = %d) OR (r.consumer_pk IN (' .
557 'SELECT c.consumer_pk ' .
558 "FROM {$this->dbTableNamePrefix}" . static::CONTEXT_TABLE_NAME . ' c ' .
559 'WHERE (c.context_pk = %d))))', $this->escape($resourceLink->getId()), $resourceLink->getContext()->getRecordId(),
560 $resourceLink->getContext()->getRecordId());
561 } else {
562 $sql = sprintf('SELECT r.resource_link_pk, r.context_pk, r.consumer_pk, r.title, r.lti_resource_link_id, r.settings, r.primary_resource_link_pk, r.share_approved, r.created, r.updated ' .
563 "FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' r LEFT OUTER JOIN ' .
564 $this->dbTableNamePrefix . static::CONTEXT_TABLE_NAME . ' c ON r.context_pk = c.context_pk ' .
565 ' WHERE ((r.consumer_pk = %d) OR (c.consumer_pk = %d)) AND (lti_resource_link_id = %s)',
566 $resourceLink->getPlatform()->getRecordId(), $resourceLink->getPlatform()->getRecordId(),
567 $this->escape($resourceLink->getId()));
568 }
569 $rsResourceLink = $this->executeQuery($sql);
570 if ($rsResourceLink) {
571 $row = mysql_fetch_object($rsResourceLink);
572 if ($row) {
573 $resourceLink->setRecordId(intval($row->resource_link_pk));
574 if (!is_null($row->context_pk)) {
575 $resourceLink->setContextId(intval($row->context_pk));
576 } else {
577 $resourceLink->setContextId(null);
578 }
579 if (!is_null($row->consumer_pk)) {
580 $resourceLink->setPlatformId(intval($row->consumer_pk));
581 } else {
582 $resourceLink->setPlatformId(null);
583 }
584 $resourceLink->title = $row->title;
585 $resourceLink->ltiResourceLinkId = $row->lti_resource_link_id;
586 $settings = Util::jsonDecode($row->settings, true);
587 if (!is_array($settings)) {
588 $settings = @unserialize($row->settings); // check for old serialized setting
589 }
590 if (!is_array($settings)) {
591 $settings = array();
592 }
593 $resourceLink->setSettings($settings);
594 if (!is_null($row->primary_resource_link_pk)) {
595 $resourceLink->primaryResourceLinkId = intval($row->primary_resource_link_pk);
596 } else {
597 $resourceLink->primaryResourceLinkId = null;
598 }
599 $resourceLink->shareApproved = (is_null($row->share_approved)) ? null : (intval($row->share_approved) === 1);
600 $resourceLink->created = strtotime($row->created);
601 $resourceLink->updated = strtotime($row->updated);
602 $ok = true;
603 }
604 }
605
606 return $ok;
607 }
608
616 public function saveResourceLink($resourceLink)
617 {
618 if (is_null($resourceLink->shareApproved)) {
619 $approved = 'NULL';
620 } elseif ($resourceLink->shareApproved) {
621 $approved = '1';
622 } else {
623 $approved = '0';
624 }
625 if (empty($resourceLink->primaryResourceLinkId)) {
626 $primaryResourceLinkId = 'NULL';
627 } else {
628 $primaryResourceLinkId = strval($resourceLink->primaryResourceLinkId);
629 }
630 $time = time();
631 $now = date("{$this->dateFormat} {$this->timeFormat}", $time);
632 $settingsValue = json_encode($resourceLink->getSettings());
633 if (!is_null($resourceLink->getContext())) {
634 $consumerId = 'NULL';
635 $contextId = strval($resourceLink->getContext()->getRecordId());
636 } elseif (!is_null($resourceLink->getContextId())) {
637 $consumerId = 'NULL';
638 $contextId = strval($resourceLink->getContextId());
639 } else {
640 $consumerId = strval($resourceLink->getPlatform()->getRecordId());
641 $contextId = 'NULL';
642 }
643 $id = $resourceLink->getRecordId();
644 if (empty($id)) {
645 $sql = sprintf("INSERT INTO {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' (consumer_pk, context_pk, ' .
646 'title, lti_resource_link_id, settings, primary_resource_link_pk, share_approved, created, updated) ' .
647 'VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)', $consumerId, $contextId, $this->escape($resourceLink->title),
648 $this->escape($resourceLink->getId()), $this->escape($settingsValue), $primaryResourceLinkId, $approved,
649 $this->escape($now), $this->escape($now));
650 } elseif ($contextId !== 'NULL') {
651 $sql = sprintf("UPDATE {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' SET ' .
652 'consumer_pk = %s, title = %s, lti_resource_link_id = %s, settings = %s, ' .
653 'primary_resource_link_pk = %s, share_approved = %s, updated = %s ' .
654 'WHERE (context_pk = %s) AND (resource_link_pk = %d)', $consumerId, $this->escape($resourceLink->title),
655 $this->escape($resourceLink->getId()), $this->escape($settingsValue), $primaryResourceLinkId, $approved,
656 $this->escape($now), $contextId, $id);
657 } else {
658 $sql = sprintf("UPDATE {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' SET ' .
659 'context_pk = NULL, title = %s, lti_resource_link_id = %s, settings = %s, ' .
660 'primary_resource_link_pk = %s, share_approved = %s, updated = %s ' .
661 'WHERE (consumer_pk = %s) AND (resource_link_pk = %d)', $this->escape($resourceLink->title),
662 $this->escape($resourceLink->getId()), $this->escape($settingsValue), $primaryResourceLinkId, $approved,
663 $this->escape($now), $consumerId, $id);
664 }
665 $ok = $this->executeQuery($sql);
666 if ($ok) {
667 if (empty($id)) {
668 $resourceLink->setRecordId(mysql_insert_id());
669 $resourceLink->created = $time;
670 }
671 $resourceLink->updated = $time;
672 }
673
674 return $ok;
675 }
676
684 public function deleteResourceLink($resourceLink)
685 {
686// Delete any outstanding share keys for resource links for this consumer
687 $sql = sprintf("DELETE FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_SHARE_KEY_TABLE_NAME . ' ' .
688 'WHERE (resource_link_pk = %d)', $resourceLink->getRecordId());
689 $ok = $this->executeQuery($sql);
690
691// Delete users
692 if ($ok) {
693 $sql = sprintf("DELETE FROM {$this->dbTableNamePrefix}" . static::USER_RESULT_TABLE_NAME . ' ' .
694 'WHERE (resource_link_pk = %d)', $resourceLink->getRecordId());
695 $ok = $this->executeQuery($sql);
696 }
697
698// Update any resource links for which this is the primary resource link
699 if ($ok) {
700 $sql = sprintf("UPDATE {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' ' .
701 'SET primary_resource_link_pk = NULL ' .
702 'WHERE (primary_resource_link_pk = %d)', $resourceLink->getRecordId());
703 $ok = $this->executeQuery($sql);
704 }
705
706// Delete resource link
707 if ($ok) {
708 $sql = sprintf("DELETE FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' ' .
709 'WHERE (resource_link_pk = %s)', $resourceLink->getRecordId());
710 $ok = $this->executeQuery($sql);
711 }
712
713 if ($ok) {
714 $resourceLink->initialize();
715 }
716
717 return $ok;
718 }
719
732 public function getUserResultSourcedIDsResourceLink($resourceLink, $localOnly, $idScope)
733 {
734 $userResults = array();
735
736 if ($localOnly) {
737 $sql = sprintf('SELECT u.user_result_pk, u.lti_result_sourcedid, u.lti_user_id, u.created, u.updated ' .
738 "FROM {$this->dbTableNamePrefix}" . static::USER_RESULT_TABLE_NAME . ' AS u ' .
739 "INNER JOIN {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' AS rl ' .
740 'ON u.resource_link_pk = rl.resource_link_pk ' .
741 "WHERE (rl.resource_link_pk = %d) AND (rl.primary_resource_link_pk IS NULL)", $resourceLink->getRecordId());
742 } else {
743 $sql = sprintf('SELECT u.user_result_pk, u.lti_result_sourcedid, u.lti_user_id, u.created, u.updated ' .
744 "FROM {$this->dbTableNamePrefix}" . static::USER_RESULT_TABLE_NAME . ' AS u ' .
745 "INNER JOIN {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' AS rl ' .
746 'ON u.resource_link_pk = rl.resource_link_pk ' .
747 'WHERE ((rl.resource_link_pk = %d) AND (rl.primary_resource_link_pk IS NULL)) OR ' .
748 '((rl.primary_resource_link_pk = %d) AND (share_approved = 1))', $resourceLink->getRecordId(),
749 $resourceLink->getRecordId());
750 }
751 $rsUser = $this->executeQuery($sql);
752 if ($rsUser) {
753 while ($row = mysql_fetch_object($rsUser)) {
754 $userresult = LTI\UserResult::fromResourceLink($resourceLink, $row->lti_user_id);
755 if (is_null($idScope)) {
756 $userResults[] = $userresult;
757 } else {
758 $userResults[$userresult->getId($idScope)] = $userresult;
759 }
760 }
761 }
762
763 return $userResults;
764 }
765
773 public function getSharesResourceLink($resourceLink)
774 {
775 $shares = array();
776
777 $sql = sprintf('SELECT c.consumer_name, r.resource_link_pk, r.title, r.share_approved ' .
778 "FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' AS r ' .
779 "INNER JOIN {$this->dbTableNamePrefix}" . static::PLATFORM_TABLE_NAME . ' AS c ON r.consumer_pk = c.consumer_pk ' .
780 'WHERE (r.primary_resource_link_pk = %d) ' .
781 'UNION ' .
782 'SELECT c2.consumer_name, r2.resource_link_pk, r2.title, r2.share_approved ' .
783 "FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' AS r2 ' .
784 "INNER JOIN {$this->dbTableNamePrefix}" . static::CONTEXT_TABLE_NAME . ' AS x ON r2.context_pk = x.context_pk ' .
785 "INNER JOIN {$this->dbTableNamePrefix}" . static::PLATFORM_TABLE_NAME . ' AS c2 ON x.consumer_pk = c2.consumer_pk ' .
786 'WHERE (r2.primary_resource_link_pk = %d) ' .
787 'ORDER BY consumer_name, title', $resourceLink->getRecordId(), $resourceLink->getRecordId());
788 $rsShare = $this->executeQuery($sql);
789 if ($rsShare) {
790 while ($row = mysql_fetch_object($rsShare)) {
791 $share = new LTI\ResourceLinkShare();
792 $share->consumerName = $row->consumer_name;
793 $share->resourceLinkId = intval($row->resource_link_pk);
794 $share->title = $row->title;
795 $share->approved = (intval($row->share_approved) === 1);
796 $shares[] = $share;
797 }
798 }
799
800 return $shares;
801 }
802
803###
804### PlatformNonce methods
805###
806
814 public function loadPlatformNonce($nonce)
815 {
816 if (parent::useMemcache()) {
817 $ok = parent::loadPlatformNonce($nonce);
818 } else {
819 $ok = false;
820
821// Delete any expired nonce values
822 $now = date("{$this->dateFormat} {$this->timeFormat}", time());
823 $sql = "DELETE FROM {$this->dbTableNamePrefix}" . static::NONCE_TABLE_NAME . " WHERE expires <= '{$now}'";
824 $this->executeQuery($sql);
825
826// Load the nonce
827 $sql = sprintf("SELECT value AS T FROM {$this->dbTableNamePrefix}" . static::NONCE_TABLE_NAME . ' WHERE (consumer_pk = %d) AND (value = %s)',
828 $nonce->getPlatform()->getRecordId(), $this->escape($nonce->getValue()));
829 $rsNonce = $this->executeQuery($sql, false);
830 if ($rsNonce) {
831 $row = mysql_fetch_object($rsNonce);
832 if ($row !== false) {
833 $ok = true;
834 }
835 }
836 }
837
838 return $ok;
839 }
840
848 public function savePlatformNonce($nonce)
849 {
850 if (parent::useMemcache()) {
851 $ok = parent::savePlatformNonce($nonce);
852 } else {
853 $expires = date("{$this->dateFormat} {$this->timeFormat}", $nonce->expires);
854 $sql = sprintf("INSERT INTO {$this->dbTableNamePrefix}" . static::NONCE_TABLE_NAME . " (consumer_pk, value, expires) VALUES (%d, %s, %s)",
855 $nonce->getPlatform()->getRecordId(), $this->escape($nonce->getValue()), $this->escape($expires));
856 $ok = $this->executeQuery($sql);
857 }
858
859 return $ok;
860 }
861
869 public function deletePlatformNonce($nonce)
870 {
871 if (parent::useMemcache()) {
872 $ok = parent::deletePlatformNonce($nonce);
873 } else {
874 $sql = sprintf("DELETE FROM {$this->dbTableNamePrefix}" . static::NONCE_TABLE_NAME . ' ' .
875 'WHERE (consumer_pk = %d) AND (value = %s)', $nonce->getPlatform()->getRecordId(), $this->escape($nonce->getValue()));
876 $ok = $this->executeQuery($sql);
877 }
878
879 return $ok;
880 }
881
882###
883### AccessToken methods
884###
885
893 public function loadAccessToken($accessToken)
894 {
895 if (parent::useMemcache()) {
896 $ok = parent::loadAccessToken($accessToken);
897 } else {
898 $ok = false;
899 $consumer_pk = $accessToken->getPlatform()->getRecordId();
900 $sql = sprintf('SELECT scopes, token, expires, created, updated ' .
901 "FROM {$this->dbTableNamePrefix}" . static::ACCESS_TOKEN_TABLE_NAME . ' ' .
902 'WHERE (consumer_pk = %d)', $consumer_pk);
903 $rsAccessToken = $this->executeQuery($sql, false);
904 if ($rsAccessToken) {
905 $row = mysql_fetch_object($rsAccessToken);
906 if ($row) {
907 $scopes = Util::jsonDecode($row->scopes, true);
908 if (!is_array($scopes)) {
909 $scopes = array();
910 }
911 $accessToken->scopes = $scopes;
912 $accessToken->token = $row->token;
913 $accessToken->expires = strtotime($row->expires);
914 $accessToken->created = strtotime($row->created);
915 $accessToken->updated = strtotime($row->updated);
916 $ok = true;
917 }
918 }
919 }
920
921 return $ok;
922 }
923
931 public function saveAccessToken($accessToken)
932 {
933 if (parent::useMemcache()) {
934 $ok = parent::saveAccessToken($accessToken);
935 } else {
936 $consumer_pk = $accessToken->getPlatform()->getRecordId();
937 $scopes = json_encode($accessToken->scopes, JSON_UNESCAPED_SLASHES);
938 $token = $accessToken->token;
939 $expires = date("{$this->dateFormat} {$this->timeFormat}", $accessToken->expires);
940 $time = time();
941 $now = date("{$this->dateFormat} {$this->timeFormat}", $time);
942 if (empty($accessToken->created)) {
943 $sql = sprintf("INSERT INTO {$this->dbTableNamePrefix}" . static::ACCESS_TOKEN_TABLE_NAME . ' ' .
944 '(consumer_pk, scopes, token, expires, created, updated) ' .
945 'VALUES (%d, %s, %s, %s, %s, %s)', $consumer_pk, $this->escape($scopes), $this->escape($token),
946 $this->escape($expires), $this->escape($now), $this->escape($now));
947 } else {
948 $sql = sprintf('UPDATE ' . $this->dbTableNamePrefix . static::ACCESS_TOKEN_TABLE_NAME . ' ' .
949 'SET scopes = %s, token = %s, expires = %s, updated = %s WHERE consumer_pk = %d', $this->escape($scopes),
950 $this->escape($token), $this->escape($expires), $this->escape($now), $consumer_pk);
951 }
952 $ok = $this->executeQuery($sql);
953 }
954
955 return $ok;
956 }
957
958###
959### ResourceLinkShareKey methods
960###
961
969 public function loadResourceLinkShareKey($shareKey)
970 {
971 $ok = false;
972
973// Clear expired share keys
974 $now = date("{$this->dateFormat} {$this->timeFormat}", time());
975 $sql = "DELETE FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_SHARE_KEY_TABLE_NAME . " WHERE expires <= '{$now}'";
976 $this->executeQuery($sql);
977
978// Load share key
979 $id = mysql_real_escape_string($shareKey->getId());
980 $sql = 'SELECT resource_link_pk, auto_approve, expires ' .
981 "FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_SHARE_KEY_TABLE_NAME . ' ' .
982 "WHERE share_key_id = '{$id}'";
983 $rsShareKey = $this->executeQuery($sql);
984 if ($rsShareKey) {
985 $row = mysql_fetch_object($rsShareKey);
986 if ($row) {
987 $shareKey->resourceLinkId = intval($row->resource_link_pk);
988 $shareKey->autoApprove = (intval($row->auto_approve) === 1);
989 $shareKey->expires = strtotime($row->expires);
990 $ok = true;
991 }
992 }
993
994 return $ok;
995 }
996
1004 public function saveResourceLinkShareKey($shareKey)
1005 {
1006 if ($shareKey->autoApprove) {
1007 $approve = 1;
1008 } else {
1009 $approve = 0;
1010 }
1011 $expires = date("{$this->dateFormat} {$this->timeFormat}", $shareKey->expires);
1012 $sql = sprintf("INSERT INTO {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_SHARE_KEY_TABLE_NAME . ' ' .
1013 '(share_key_id, resource_link_pk, auto_approve, expires) ' .
1014 "VALUES (%s, %d, {$approve}, '{$expires}')", $this->escape($shareKey->getId()), $shareKey->resourceLinkId);
1015 $ok = $this->executeQuery($sql);
1016
1017 return $ok;
1018 }
1019
1027 public function deleteResourceLinkShareKey($shareKey)
1028 {
1029 $sql = "DELETE FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_SHARE_KEY_TABLE_NAME . " WHERE share_key_id = '{$shareKey->getId()}'";
1030
1031 $ok = $this->executeQuery($sql);
1032
1033 if ($ok) {
1034 $shareKey->initialize();
1035 }
1036
1037 return $ok;
1038 }
1039
1040###
1041### UserResult methods
1042###
1043
1051 public function loadUserResult($userresult)
1052 {
1053 $ok = false;
1054 if (!is_null($userresult->getRecordId())) {
1055 $sql = sprintf('SELECT user_result_pk, resource_link_pk, lti_user_id, lti_result_sourcedid, created, updated ' .
1056 "FROM {$this->dbTableNamePrefix}" . static::USER_RESULT_TABLE_NAME . ' ' .
1057 'WHERE (user_result_pk = %d)', $userresult->getRecordId());
1058 } else {
1059 $sql = sprintf('SELECT user_result_pk, resource_link_pk, lti_user_id, lti_result_sourcedid, created, updated ' .
1060 "FROM {$this->dbTableNamePrefix}" . static::USER_RESULT_TABLE_NAME . ' ' .
1061 'WHERE (resource_link_pk = %d) AND (lti_user_id = %s)', $userresult->getResourceLink()->getRecordId(),
1062 $this->escape($userresult->getId(LTI\Tool::ID_SCOPE_ID_ONLY)));
1063 }
1064 $rsUserResult = $this->executeQuery($sql);
1065 if ($rsUserResult) {
1066 $row = mysql_fetch_object($rsUserResult);
1067 if ($row) {
1068 $userresult->setRecordId(intval($row->user_result_pk));
1069 $userresult->setResourceLinkId(intval($row->resource_link_pk));
1070 $userresult->ltiUserId = $row->lti_user_id;
1071 $userresult->ltiResultSourcedId = $row->lti_result_sourcedid;
1072 $userresult->created = strtotime($row->created);
1073 $userresult->updated = strtotime($row->updated);
1074 $ok = true;
1075 }
1076 }
1077
1078 return $ok;
1079 }
1080
1088 public function saveUserResult($userresult)
1089 {
1090 $time = time();
1091 $now = date("{$this->dateFormat} {$this->timeFormat}", $time);
1092 if (is_null($userresult->created)) {
1093 $sql = sprintf("INSERT INTO {$this->dbTableNamePrefix}" . static::USER_RESULT_TABLE_NAME . ' (resource_link_pk, ' .
1094 'lti_user_id, lti_result_sourcedid, created, updated) ' .
1095 'VALUES (%d, %s, %s, %s, %s)', $userresult->getResourceLink()->getRecordId(),
1096 $this->escape($userresult->getId(LTI\Tool::ID_SCOPE_ID_ONLY)), $this->escape($userresult->ltiResultSourcedId),
1097 $this->escape($now), $this->escape($now));
1098 } else {
1099 $sql = sprintf("UPDATE {$this->dbTableNamePrefix}" . static::USER_RESULT_TABLE_NAME . ' ' .
1100 'SET lti_user_id = %s, lti_result_sourcedid = %s, updated = %s ' .
1101 'WHERE (user_result_pk = %d)', $this->escape($userresult->getId(LTI\Tool::ID_SCOPE_ID_ONLY)),
1102 $this->escape($userresult->ltiResultSourcedId), $this->escape($now), $userresult->getRecordId());
1103 }
1104 $ok = $this->executeQuery($sql);
1105 if ($ok) {
1106 if (is_null($userresult->created)) {
1107 $userresult->setRecordId(mysql_insert_id());
1108 $userresult->created = $time;
1109 }
1110 $userresult->updated = $time;
1111 }
1112
1113 return $ok;
1114 }
1115
1123 public function deleteUserResult($userresult)
1124 {
1125 $sql = sprintf("DELETE FROM {$this->dbTableNamePrefix}" . static::USER_RESULT_TABLE_NAME . ' ' .
1126 'WHERE (user_result_pk = %d)', $userresult->getRecordId());
1127 $ok = $this->executeQuery($sql);
1128
1129 if ($ok) {
1130 $userresult->initialize();
1131 }
1132
1133 return $ok;
1134 }
1135
1136###
1137### Tool methods
1138###
1139
1147 public function loadTool($tool)
1148 {
1149 $ok = false;
1150 if (!is_null($tool->getRecordId())) {
1151 $sql = sprintf('SELECT tool_pk, name, consumer_key, secret, ' .
1152 'message_url, initiate_login_url, redirection_uris, public_key, ' .
1153 'lti_version, signature_method, settings, enabled, ' .
1154 'enable_from, enable_until, last_access, created, updated ' .
1155 "FROM {$this->dbTableNamePrefix}" . static::TOOL_TABLE_NAME . ' ' .
1156 'WHERE tool_pk = %d', $tool->getRecordId());
1157 } elseif (!empty($tool->initiateLoginUrl)) {
1158 $sql = sprintf('SELECT tool_pk, name, consumer_key, secret, ' .
1159 'message_url, initiate_login_url, redirection_uris, public_key, ' .
1160 'lti_version, signature_method, settings, enabled, ' .
1161 'enable_from, enable_until, last_access, created, updated ' .
1162 "FROM {$this->dbTableNamePrefix}" . static::TOOL_TABLE_NAME . ' ' .
1163 'WHERE initiate_login_url = %s', $this->escape($tool->initiateLoginUrl));
1164 } else {
1165 $sql = sprintf('SELECT tool_pk, name, consumer_key, secret, ' .
1166 'message_url, initiate_login_url, redirection_uris, public_key, ' .
1167 'lti_version, signature_method, settings, enabled, ' .
1168 'enable_from, enable_until, last_access, created, updated ' .
1169 "FROM {$this->dbTableNamePrefix}" . static::TOOL_TABLE_NAME . ' ' .
1170 'WHERE consumer_key = %s', $this->escape($tool->getKey()));
1171 }
1172 $rsTool = $this->executeQuery($sql);
1173 if ($rsTool) {
1174 $row = mysql_fetch_object($rsTool);
1175 if ($row) {
1176 $tool->setRecordId(intval($row->tool_pk));
1177 $tool->name = $row->name;
1178 $tool->setkey($row->consumer_key);
1179 $tool->secret = $row->secret;
1180 $tool->messageUrl = $row->message_url;
1181 $tool->initiateLoginUrl = $row->initiate_login_url;
1182 $tool->redirectionUris = Util::jsonDecode($row->redirection_uris, true);
1183 if (!is_array($tool->redirectionUris)) {
1184 $tool->redirectionUris = array();
1185 }
1186 $tool->rsaKey = $row->public_key;
1187 $tool->ltiVersion = $row->lti_version;
1188 $tool->signatureMethod = $row->signature_method;
1189 $settings = Util::jsonDecode($row->settings, true);
1190 if (!is_array($settings)) {
1191 $settings = array();
1192 }
1193 $tool->setSettings($settings);
1194 $tool->enabled = (intval($row->enabled) === 1);
1195 $tool->enableFrom = null;
1196 if (!is_null($row->enable_from)) {
1197 $tool->enableFrom = strtotime($row->enable_from);
1198 }
1199 $tool->enableUntil = null;
1200 if (!is_null($row->enable_until)) {
1201 $tool->enableUntil = strtotime($row->enable_until);
1202 }
1203 $tool->lastAccess = null;
1204 if (!is_null($row->last_access)) {
1205 $tool->lastAccess = strtotime($row->last_access);
1206 }
1207 $tool->created = strtotime($row->created);
1208 $tool->updated = strtotime($row->updated);
1209 $this->fixToolSettings($tool, false);
1210 $ok = true;
1211 }
1212 }
1213
1214 return $ok;
1215 }
1216
1224 public function saveTool($tool)
1225 {
1226 $id = $tool->getRecordId();
1227 $enabled = ($tool->enabled) ? 1 : 0;
1228 $redirectionUrisValue = json_encode($tool->redirectionUris);
1229 $this->fixToolSettings($tool, true);
1230 $settingsValue = json_encode($tool->getSettings());
1231 $this->fixToolSettings($tool, false);
1232 $time = time();
1233 $now = date("{$this->dateFormat} {$this->timeFormat}", $time);
1234 $from = null;
1235 if (!is_null($tool->enableFrom)) {
1236 $from = date("{$this->dateFormat} {$this->timeFormat}", $tool->enableFrom);
1237 }
1238 $until = null;
1239 if (!is_null($tool->enableUntil)) {
1240 $until = date("{$this->dateFormat} {$this->timeFormat}", $tool->enableUntil);
1241 }
1242 $last = null;
1243 if (!is_null($tool->lastAccess)) {
1244 $last = date($this->dateFormat, $tool->lastAccess);
1245 }
1246 if (empty($id)) {
1247 $sql = sprintf("INSERT INTO {$this->dbTableNamePrefix}" . static::TOOL_TABLE_NAME . ' (name, consumer_key, secret, ' .
1248 'message_url, initiate_login_url, redirection_uris, public_key, ' .
1249 'lti_version, signature_method, settings, enabled, enable_from, enable_until, ' .
1250 'last_access, created, updated) ' .
1251 'VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %d, %s, %s, %s, %s, %s)', $this->escape($tool->name),
1252 $this->escape($tool->getKey()), $this->escape($tool->secret), $this->escape($tool->messageUrl),
1253 $this->escape($tool->initiateLoginUrl), $this->escape($redirectionUrisValue), $this->escape($tool->rsaKey),
1254 $this->escape($tool->ltiVersion), $this->escape($tool->signatureMethod), $this->escape($settingsValue), $enabled,
1255 $this->escape($from), $this->escape($until), $this->escape($last), $this->escape($now), $this->escape($now));
1256 } else {
1257 $sql = sprintf("UPDATE {$this->dbTableNamePrefix}" . static::TOOL_TABLE_NAME . ' SET ' .
1258 'name = %s, consumer_key = %s, secret= %s, ' .
1259 'message_url = %s, initiate_login_url = %s, redirection_uris = %s, public_key = %s, ' .
1260 'lti_version = %s, signature_method = %s, settings = %s, enabled = %d, enable_from = %s, enable_until = %s, ' .
1261 'last_access = %s, updated = %s ' .
1262 'WHERE tool_pk = %d', $this->escape($tool->name), $this->escape($tool->getKey()), $this->escape($tool->secret),
1263 $this->escape($tool->messageUrl), $this->escape($tool->initiateLoginUrl), $this->escape($redirectionUrisValue),
1264 $this->escape($tool->rsaKey), $this->escape($tool->ltiVersion), $this->escape($tool->signatureMethod),
1265 $this->escape($settingsValue), $enabled, $this->escape($from), $this->escape($until), $this->escape($last),
1266 $this->escape($now), $tool->getRecordId());
1267 }
1268 $ok = $this->executeQuery($sql);
1269 if ($ok) {
1270 if (empty($id)) {
1271 $tool->setRecordId(mysql_insert_id($this->db));
1272 $tool->created = $time;
1273 }
1274 $tool->updated = $time;
1275 }
1276
1277 return $ok;
1278 }
1279
1287 public function deleteTool($tool)
1288 {
1289 $sql = sprintf('DELETE t ' .
1290 "FROM {$this->dbTableNamePrefix}" . static::TOOL_TABLE_NAME . ' t ' .
1291 'WHERE t.tool_pk = %d', $tool->getRecordId());
1292 $ok = $this->executeQuery($sql);
1293
1294 if ($ok) {
1295 $tool->initialize();
1296 }
1297
1298 return $ok;
1299 }
1300
1306 public function getTools()
1307 {
1308 $tools = array();
1309
1310 $sql = 'SELECT tool_pk, name, consumer_key, secret, ' .
1311 'message_url, initiate_login_url, redirection_uris, public_key, ' .
1312 'lti_version, signature_method, settings, enabled, ' .
1313 'enable_from, enable_until, last_access, created, updated ' .
1314 "FROM {$this->dbTableNamePrefix}" . static::TOOL_TABLE_NAME . ' ' .
1315 'ORDER BY name';
1316 $rsTools = $this->executeQuery($sql);
1317 if ($rsTools) {
1318 while ($row = mysql_fetch_object($rsTools)) {
1319 $tool = new Tool($this);
1320 $tool->setRecordId(intval($row->tool_pk));
1321 $tool->name = $row->name;
1322 $tool->setkey($row->consumer_key);
1323 $tool->secret = $row->secret;
1324 $tool->messageUrl = $row->message_url;
1325 $tool->initiateLoginUrl = $row->initiate_login_url;
1326 $tool->redirectionUris = Util::jsonDecode($row->redirection_uris, true);
1327 if (!is_array($tool->redirectionUris)) {
1328 $tool->redirectionUris = array();
1329 }
1330 $tool->rsaKey = $row->public_key;
1331 $tool->ltiVersion = $row->lti_version;
1332 $tool->signatureMethod = $row->signature_method;
1333 $settings = Util::jsonDecode($row->settings, true);
1334 if (!is_array($settings)) {
1335 $settings = array();
1336 }
1337 $tool->setSettings($settings);
1338 $tool->enabled = (intval($row->enabled) === 1);
1339 $tool->enableFrom = null;
1340 if (!is_null($row->enable_from)) {
1341 $tool->enableFrom = strtotime($row->enable_from);
1342 }
1343 $tool->enableUntil = null;
1344 if (!is_null($row->enable_until)) {
1345 $tool->enableUntil = strtotime($row->enable_until);
1346 }
1347 $tool->lastAccess = null;
1348 if (!is_null($row->last_access)) {
1349 $tool->lastAccess = strtotime($row->last_access);
1350 }
1351 $tool->created = strtotime($row->created);
1352 $tool->updated = strtotime($row->updated);
1353 $this->fixToolSettings($tool, false);
1354 $tools[] = $tool;
1355 }
1356 mysql_free_result($rsTools);
1357 }
1358
1359 return $tools;
1360 }
1361
1362###
1363### Other methods
1364###
1365
1377 public function escape($value, $addQuotes = true)
1378 {
1379 if (is_null($value)) {
1380 $value = 'null';
1381 } else {
1382 $value = mysql_real_escape_string($value);
1383 if ($addQuotes) {
1384 $value = "'{$value}'";
1385 }
1386 }
1387
1388 return $value;
1389 }
1390
1401 private function executeQuery($sql, $reportError = true)
1402 {
1403 $res = mysql_query($sql);
1404 $info = mysql_info();
1405 if ($info) {
1406 $info = PHP_EOL . $info;
1407 } else {
1408 $info = '';
1409 }
1410 if (($res === false) && $reportError) {
1411 Util::logError($sql . $info . PHP_EOL . 'Error ' . mysql_errno() . ': ' . mysql_error());
1412 } else {
1413 Util::logDebug($sql . $info);
1414 }
1415
1416 return $res;
1417 }
1418
1419}
Class to represent an HTTP message.
Class to represent a platform context.
Definition Context.php:18
Class to represent an LTI Data Connector for MySQL.
loadResourceLinkShareKey($shareKey)
Load resource link share key object.
escape($value, $addQuotes=true)
Escape a string for use in a database query.
deletePlatform($platform)
Delete platform object.
saveAccessToken($accessToken)
Save access token object.
saveResourceLinkShareKey($shareKey)
Save resource link share key object.
loadAccessToken($accessToken)
Load access token object.
getUserResultSourcedIDsResourceLink($resourceLink, $localOnly, $idScope)
Get array of user objects.
__construct($db, $dbTableNamePrefix='')
Class constructor.
getPlatforms()
Load all platforms from the database.
getSharesResourceLink($resourceLink)
Get array of shares defined for this resource link.
loadResourceLink($resourceLink)
Load resource link object.
saveResourceLink($resourceLink)
Save resource link object.
deleteResourceLinkShareKey($shareKey)
Delete resource link share key object.
deleteResourceLink($resourceLink)
Delete resource link object.
Class to provide a connection to a persistent store for LTI objects.
$dbTableNamePrefix
Prefix for database table names.
fixToolSettings($tool, $isSave)
Adjust the settings for any tool properties being stored as a setting value.
fixPlatformSettings($platform, $isSave)
Adjust the settings for any platform properties being stored as a setting value.
Class to represent a platform nonce.
Class to represent a platform.
Definition Platform.php:18
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:24
const ID_SCOPE_ID_ONLY
Use ID value only.
Definition Tool.php:34
Class to represent a platform user association with a resource link.
Class to implement utility methods.
Definition Util.php:15
static logError($message, $showSource=true)
Log an error message.
Definition Util.php:248
static jsonDecode($str, $associative=false)
Decode a JSON string.
Definition Util.php:560
static logDebug($message, $showSource=false)
Log a debug message.
Definition Util.php:274