LTI Integration Library 4.10.3
PHP class library for building LTI integrations
 
Loading...
Searching...
No Matches
DataConnector_pdo.php
1<?php
2
4
5use ceLTIc\LTI;
16
25{
26###
27### Platform methods
28###
29
37 public function loadPlatform($platform)
38 {
39 $allowMultiple = false;
40 if (!is_null($platform->getRecordId())) {
41 $sql = 'SELECT consumer_pk, name, consumer_key, secret, ' .
42 'platform_id, client_id, deployment_id, public_key, ' .
43 'lti_version, signature_method, consumer_name, consumer_version, consumer_guid, ' .
44 'profile, tool_proxy, settings, protected, enabled, ' .
45 'enable_from, enable_until, last_access, created, updated ' .
46 "FROM {$this->dbTableNamePrefix}" . static::PLATFORM_TABLE_NAME . ' ' .
47 'WHERE consumer_pk = :id';
48 $query = $this->db->prepare($sql);
49 $id = $platform->getRecordId();
50 $query->bindValue('id', $id, \PDO::PARAM_INT);
51 } elseif (!empty($platform->platformId)) {
52 if (empty($platform->clientId)) {
53 $allowMultiple = true;
54 $sql = 'SELECT consumer_pk, name, consumer_key, secret, ' .
55 'platform_id, client_id, deployment_id, public_key, ' .
56 'lti_version, signature_method, consumer_name, consumer_version, consumer_guid, ' .
57 'profile, tool_proxy, settings, protected, enabled, ' .
58 'enable_from, enable_until, last_access, created, updated ' .
59 "FROM {$this->dbTableNamePrefix}" . static::PLATFORM_TABLE_NAME . ' ' .
60 'WHERE (platform_id = :platform_id) ';
61 $query = $this->db->prepare($sql);
62 $query->bindValue('platform_id', $platform->platformId, \PDO::PARAM_STR);
63 } elseif (empty($platform->deploymentId)) {
64 $allowMultiple = true;
65 $sql = 'SELECT consumer_pk, name, consumer_key, secret, ' .
66 'platform_id, client_id, deployment_id, public_key, ' .
67 'lti_version, signature_method, consumer_name, consumer_version, consumer_guid, ' .
68 'profile, tool_proxy, settings, protected, enabled, ' .
69 'enable_from, enable_until, last_access, created, updated ' .
70 "FROM {$this->dbTableNamePrefix}" . static::PLATFORM_TABLE_NAME . ' ' .
71 'WHERE (platform_id = :platform_id) AND (client_id = :client_id)';
72 $query = $this->db->prepare($sql);
73 $query->bindValue('platform_id', $platform->platformId, \PDO::PARAM_STR);
74 $query->bindValue('client_id', $platform->clientId, \PDO::PARAM_STR);
75 } else {
76 $sql = 'SELECT consumer_pk, name, consumer_key, secret, ' .
77 'platform_id, client_id, deployment_id, public_key, ' .
78 'lti_version, signature_method, consumer_name, consumer_version, consumer_guid, ' .
79 'profile, tool_proxy, settings, protected, enabled, ' .
80 'enable_from, enable_until, last_access, created, updated ' .
81 "FROM {$this->dbTableNamePrefix}" . static::PLATFORM_TABLE_NAME . ' ' .
82 'WHERE (platform_id = :platform_id) AND (client_id = :client_id) AND (deployment_id = :deployment_id)';
83 $query = $this->db->prepare($sql);
84 $query->bindValue('platform_id', $platform->platformId, \PDO::PARAM_STR);
85 $query->bindValue('client_id', $platform->clientId, \PDO::PARAM_STR);
86 $query->bindValue('deployment_id', $platform->deploymentId, \PDO::PARAM_STR);
87 }
88 } else {
89 $sql = 'SELECT consumer_pk, name, consumer_key, secret, ' .
90 'platform_id, client_id, deployment_id, public_key, ' .
91 'lti_version, signature_method, consumer_name, consumer_version, consumer_guid, ' .
92 'profile, tool_proxy, settings, protected, enabled, ' .
93 'enable_from, enable_until, last_access, created, updated ' .
94 "FROM {$this->dbTableNamePrefix}" . static::PLATFORM_TABLE_NAME . ' ' .
95 'WHERE (consumer_key = :key)';
96 $query = $this->db->prepare($sql);
97 $consumer_key = $platform->getKey();
98 $query->bindValue('key', $consumer_key, \PDO::PARAM_STR);
99 }
100 $ok = $this->executeQuery($sql, $query);
101 if ($ok) {
102 $row = $query->fetch(\PDO::FETCH_ASSOC);
103 $ok = ($row !== false) && ($allowMultiple || !$query->fetch(\PDO::FETCH_ASSOC));
104 }
105 if ($ok) {
106 $row = array_change_key_case($row);
107 $platform->setRecordId(intval($row['consumer_pk']));
108 $platform->name = $row['name'];
109 $platform->setkey($row['consumer_key']);
110 $platform->secret = $row['secret'];
111 $platform->platformId = $row['platform_id'];
112 $platform->clientId = $row['client_id'];
113 $platform->deploymentId = $row['deployment_id'];
114 $platform->rsaKey = $row['public_key'];
115 $platform->ltiVersion = $row['lti_version'];
116 $platform->signatureMethod = $row['signature_method'];
117 $platform->consumerName = $row['consumer_name'];
118 $platform->consumerVersion = $row['consumer_version'];
119 $platform->consumerGuid = $row['consumer_guid'];
120 $platform->profile = Util::jsonDecode($row['profile']);
121 $platform->toolProxy = $row['tool_proxy'];
122 $settings = Util::jsonDecode($row['settings'], true);
123 if (!is_array($settings)) {
124 $settings = @unserialize($row['settings']); // check for old serialized setting
125 }
126 if (!is_array($settings)) {
127 $settings = array();
128 }
129 $platform->setSettings($settings);
130 $platform->protected = (intval($row['protected']) === 1);
131 $platform->enabled = (intval($row['enabled']) === 1);
132 $platform->enableFrom = null;
133 if (!is_null($row['enable_from'])) {
134 $platform->enableFrom = strtotime($row['enable_from']);
135 }
136 $platform->enableUntil = null;
137 if (!is_null($row['enable_until'])) {
138 $platform->enableUntil = strtotime($row['enable_until']);
139 }
140 $platform->lastAccess = null;
141 if (!is_null($row['last_access'])) {
142 $platform->lastAccess = strtotime($row['last_access']);
143 }
144 $platform->created = strtotime($row['created']);
145 $platform->updated = strtotime($row['updated']);
146 $this->fixPlatformSettings($platform, false);
147 }
148
149 return $ok;
150 }
151
159 public function savePlatform($platform)
160 {
161 $id = $platform->getRecordId();
162 $protected = ($platform->protected) ? 1 : 0;
163 $enabled = ($platform->enabled) ? 1 : 0;
164 $profile = (!empty($platform->profile)) ? json_encode($platform->profile) : null;
165 $this->fixPlatformSettings($platform, true);
166 $settingsValue = json_encode($platform->getSettings());
167 $this->fixPlatformSettings($platform, false);
168 $time = time();
169 $now = date("{$this->dateFormat} {$this->timeFormat}", $time);
170 $from = null;
171 if (!is_null($platform->enableFrom)) {
172 $from = date("{$this->dateFormat} {$this->timeFormat}", $platform->enableFrom);
173 }
174 $until = null;
175 if (!is_null($platform->enableUntil)) {
176 $until = date("{$this->dateFormat} {$this->timeFormat}", $platform->enableUntil);
177 }
178 $last = null;
179 if (!is_null($platform->lastAccess)) {
180 $last = date($this->dateFormat, $platform->lastAccess);
181 }
182 if (empty($id)) {
183 $sql = "INSERT INTO {$this->dbTableNamePrefix}" . static::PLATFORM_TABLE_NAME . ' (consumer_key, name, secret, ' .
184 'platform_id, client_id, deployment_id, public_key, ' .
185 'lti_version, signature_method, consumer_name, consumer_version, consumer_guid, ' .
186 'profile, tool_proxy, settings, protected, enabled, ' .
187 'enable_from, enable_until, last_access, created, updated) ' .
188 'VALUES (:key, :name, :secret, ' .
189 ':platform_id, :client_id, :deployment_id, :public_key, ' .
190 ':lti_version, :signature_method, ' .
191 ':consumer_name, :consumer_version, :consumer_guid, :profile, :tool_proxy, :settings, ' .
192 ':protected, :enabled, :enable_from, :enable_until, :last_access, :created, :updated)';
193 $query = $this->db->prepare($sql);
194 $query->bindValue('key', $platform->getKey(), \PDO::PARAM_STR);
195 $query->bindValue('name', $platform->name, \PDO::PARAM_STR);
196 $query->bindValue('secret', $platform->secret, \PDO::PARAM_STR);
197 $query->bindValue('platform_id', $platform->platformId, \PDO::PARAM_STR);
198 $query->bindValue('client_id', $platform->clientId, \PDO::PARAM_STR);
199 $query->bindValue('deployment_id', $platform->deploymentId, \PDO::PARAM_STR);
200 $query->bindValue('public_key', $platform->rsaKey, \PDO::PARAM_STR);
201 $query->bindValue('lti_version', $platform->ltiVersion, \PDO::PARAM_STR);
202 $query->bindValue('signature_method', $platform->signatureMethod, \PDO::PARAM_STR);
203 $query->bindValue('consumer_name', $platform->consumerName, \PDO::PARAM_STR);
204 $query->bindValue('consumer_version', $platform->consumerVersion, \PDO::PARAM_STR);
205 $query->bindValue('consumer_guid', $platform->consumerGuid, \PDO::PARAM_STR);
206 $query->bindValue('profile', $profile, \PDO::PARAM_STR);
207 $query->bindValue('tool_proxy', $platform->toolProxy, \PDO::PARAM_STR);
208 $query->bindValue('settings', $settingsValue, \PDO::PARAM_STR);
209 $query->bindValue('protected', $protected, \PDO::PARAM_INT);
210 $query->bindValue('enabled', $enabled, \PDO::PARAM_INT);
211 $query->bindValue('enable_from', $from, \PDO::PARAM_STR);
212 $query->bindValue('enable_until', $until, \PDO::PARAM_STR);
213 $query->bindValue('last_access', $last, \PDO::PARAM_STR);
214 $query->bindValue('created', $now, \PDO::PARAM_STR);
215 $query->bindValue('updated', $now, \PDO::PARAM_STR);
216 } else {
217 $sql = 'UPDATE ' . $this->dbTableNamePrefix . static::PLATFORM_TABLE_NAME . ' ' .
218 'SET consumer_key = :key, name = :name, secret = :secret, ' .
219 'platform_id = :platform_id, client_id = :client_id, deployment_id = :deployment_id, ' .
220 'public_key = :public_key, lti_version = :lti_version, signature_method = :signature_method, ' .
221 'consumer_name = :consumer_name, consumer_version = :consumer_version, consumer_guid = :consumer_guid, ' .
222 'profile = :profile, tool_proxy = :tool_proxy, settings = :settings, ' .
223 'protected = :protected, enabled = :enabled, enable_from = :enable_from, enable_until = :enable_until, ' .
224 'last_access = :last_access, updated = :updated ' .
225 'WHERE consumer_pk = :id';
226 $query = $this->db->prepare($sql);
227 $query->bindValue('key', $platform->getKey(), \PDO::PARAM_STR);
228 $query->bindValue('name', $platform->name, \PDO::PARAM_STR);
229 $query->bindValue('secret', $platform->secret, \PDO::PARAM_STR);
230 $query->bindValue('platform_id', $platform->platformId, \PDO::PARAM_STR);
231 $query->bindValue('client_id', $platform->clientId, \PDO::PARAM_STR);
232 $query->bindValue('deployment_id', $platform->deploymentId, \PDO::PARAM_STR);
233 $query->bindValue('public_key', $platform->rsaKey, \PDO::PARAM_STR);
234 $query->bindValue('lti_version', $platform->ltiVersion, \PDO::PARAM_STR);
235 $query->bindValue('signature_method', $platform->signatureMethod, \PDO::PARAM_STR);
236 $query->bindValue('consumer_name', $platform->consumerName, \PDO::PARAM_STR);
237 $query->bindValue('consumer_version', $platform->consumerVersion, \PDO::PARAM_STR);
238 $query->bindValue('consumer_guid', $platform->consumerGuid, \PDO::PARAM_STR);
239 $query->bindValue('profile', $profile, \PDO::PARAM_STR);
240 $query->bindValue('tool_proxy', $platform->toolProxy, \PDO::PARAM_STR);
241 $query->bindValue('settings', $settingsValue, \PDO::PARAM_STR);
242 $query->bindValue('protected', $protected, \PDO::PARAM_INT);
243 $query->bindValue('enabled', $enabled, \PDO::PARAM_INT);
244 $query->bindValue('enable_from', $from, \PDO::PARAM_STR);
245 $query->bindValue('enable_until', $until, \PDO::PARAM_STR);
246 $query->bindValue('last_access', $last, \PDO::PARAM_STR);
247 $query->bindValue('updated', $now, \PDO::PARAM_STR);
248 $query->bindValue('id', $id, \PDO::PARAM_INT);
249 }
250 $ok = $this->executeQuery($sql, $query);
251 if ($ok) {
252 if (empty($id)) {
253 $platform->setRecordId($this->getLastInsertId(static::PLATFORM_TABLE_NAME));
254 $platform->created = $time;
255 }
256 $platform->updated = $time;
257 }
258
259 return $ok;
260 }
261
269 public function deletePlatform($platform)
270 {
271 $id = $platform->getRecordId();
272
273// Delete any access token for this consumer
274 $sql = "DELETE FROM {$this->dbTableNamePrefix}" . static::ACCESS_TOKEN_TABLE_NAME . ' WHERE consumer_pk = :id';
275 $query = $this->db->prepare($sql);
276 $query->bindValue('id', $id, \PDO::PARAM_INT);
277 $this->executeQuery($sql, $query);
278
279// Delete any nonce values for this consumer
280 $sql = "DELETE FROM {$this->dbTableNamePrefix}" . static::NONCE_TABLE_NAME . ' WHERE consumer_pk = :id';
281 $query = $this->db->prepare($sql);
282 $query->bindValue('id', $id, \PDO::PARAM_INT);
283 $this->executeQuery($sql, $query);
284
285// Delete any outstanding share keys for resource links for this consumer
286 $sql = "DELETE FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_SHARE_KEY_TABLE_NAME . ' ' .
287 "WHERE resource_link_pk IN (SELECT resource_link_pk FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' ' .
288 'WHERE consumer_pk = :id)';
289 $query = $this->db->prepare($sql);
290 $query->bindValue('id', $id, \PDO::PARAM_INT);
291 $this->executeQuery($sql, $query);
292
293// Delete any outstanding share keys for resource links for contexts in this consumer
294 $sql = "DELETE FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_SHARE_KEY_TABLE_NAME . ' ' .
295 "WHERE resource_link_pk IN (SELECT resource_link_pk FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' rl ' .
296 "INNER JOIN {$this->dbTableNamePrefix}" . static::CONTEXT_TABLE_NAME . ' c ON rl.context_pk = c.context_pk WHERE c.consumer_pk = :id)';
297 $query = $this->db->prepare($sql);
298 $query->bindValue('id', $id, \PDO::PARAM_INT);
299 $this->executeQuery($sql, $query);
300
301// Delete any users in resource links for this consumer
302 $sql = "DELETE FROM {$this->dbTableNamePrefix}" . static::USER_RESULT_TABLE_NAME . ' ' .
303 "WHERE resource_link_pk IN (SELECT resource_link_pk FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' ' .
304 'WHERE consumer_pk = :id)';
305 $query = $this->db->prepare($sql);
306 $query->bindValue('id', $id, \PDO::PARAM_INT);
307 $this->executeQuery($sql, $query);
308
309// Delete any users in resource links for contexts in this consumer
310 $sql = "DELETE FROM {$this->dbTableNamePrefix}" . static::USER_RESULT_TABLE_NAME . ' ' .
311 "WHERE resource_link_pk IN (SELECT resource_link_pk FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' rl ' .
312 "INNER JOIN {$this->dbTableNamePrefix}" . static::CONTEXT_TABLE_NAME . ' c ON rl.context_pk = c.context_pk WHERE c.consumer_pk = :id)';
313 $query = $this->db->prepare($sql);
314 $query->bindValue('id', $id, \PDO::PARAM_INT);
315 $this->executeQuery($sql, $query);
316
317// Update any resource links for which this consumer is acting as a primary resource link
318 $sql = "UPDATE {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' ' .
319 'SET primary_resource_link_pk = NULL, share_approved = NULL ' .
320 'WHERE primary_resource_link_pk IN ' .
321 "(SELECT resource_link_pk FROM (SELECT resource_link_pk FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' ' .
322 'WHERE consumer_pk = :id) t)';
323 $query = $this->db->prepare($sql);
324 $query->bindValue('id', $id, \PDO::PARAM_INT);
325 $this->executeQuery($sql, $query);
326
327// Update any resource links for contexts in which this consumer is acting as a primary resource link
328 $sql = "UPDATE {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' ' .
329 'SET primary_resource_link_pk = NULL, share_approved = NULL ' .
330 'WHERE primary_resource_link_pk IN ' .
331 "(SELECT resource_link_pk FROM (SELECT rl.resource_link_pk FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' rl ' .
332 "INNER JOIN {$this->dbTableNamePrefix}" . static::CONTEXT_TABLE_NAME . ' c ON rl.context_pk = c.context_pk ' .
333 'WHERE c.consumer_pk = :id) t)';
334 $query = $this->db->prepare($sql);
335 $query->bindValue('id', $id, \PDO::PARAM_INT);
336 $this->executeQuery($sql, $query);
337
338// Delete any resource links for this consumer
339 $sql = "DELETE FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' ' .
340 'WHERE consumer_pk = :id';
341 $query = $this->db->prepare($sql);
342 $query->bindValue('id', $id, \PDO::PARAM_INT);
343 $this->executeQuery($sql, $query);
344
345// Delete any resource links for contexts in this consumer
346 $sql = "DELETE FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' ' .
347 'WHERE context_pk IN (' .
348 "SELECT context_pk FROM {$this->dbTableNamePrefix}" . static::CONTEXT_TABLE_NAME . ' ' . 'WHERE consumer_pk = :id)';
349 $query = $this->db->prepare($sql);
350 $query->bindValue('id', $id, \PDO::PARAM_INT);
351 $this->executeQuery($sql, $query);
352
353// Delete any contexts for this consumer
354 $sql = "DELETE FROM {$this->dbTableNamePrefix}" . static::CONTEXT_TABLE_NAME . ' ' .
355 'WHERE consumer_pk = :id';
356 $query = $this->db->prepare($sql);
357 $query->bindValue('id', $id, \PDO::PARAM_INT);
358 $this->executeQuery($sql, $query);
359
360// Delete consumer
361 $sql = "DELETE FROM {$this->dbTableNamePrefix}" . static::PLATFORM_TABLE_NAME . ' ' .
362 'WHERE consumer_pk = :id';
363 $query = $this->db->prepare($sql);
364 $query->bindValue('id', $id, \PDO::PARAM_INT);
365 $ok = $this->executeQuery($sql, $query);
366
367 if ($ok) {
368 $platform->initialize();
369 }
370
371 return $ok;
372 }
373
379 public function getPlatforms()
380 {
381 $platforms = array();
382
383 $sql = 'SELECT consumer_pk, name, consumer_key, secret, ' .
384 'platform_id, client_id, deployment_id, public_key, ' .
385 'lti_version, signature_method, consumer_name, consumer_version, consumer_guid, ' .
386 'profile, tool_proxy, settings, protected, enabled, ' .
387 'enable_from, enable_until, last_access, created, updated ' .
388 "FROM {$this->dbTableNamePrefix}" . static::PLATFORM_TABLE_NAME . ' ' .
389 'ORDER BY name';
390 $query = $this->db->prepare($sql);
391 $ok = ($query !== false);
392
393 if ($ok) {
394 $ok = $this->executeQuery($sql, $query);
395 }
396
397 if ($ok) {
398 while ($row = $query->fetch(\PDO::FETCH_ASSOC)) {
399 $row = array_change_key_case($row);
400 $platform = Platform::fromConsumerKey($row['consumer_key'], $this);
401 $platform->setRecordId(intval($row['consumer_pk']));
402 $platform->name = $row['name'];
403 $platform->secret = $row['secret'];
404 $platform->platformId = $row['platform_id'];
405 $platform->clientId = $row['client_id'];
406 $platform->deploymentId = $row['deployment_id'];
407 $platform->rsaKey = $row['public_key'];
408 $platform->ltiVersion = $row['lti_version'];
409 $platform->signatureMethod = $row['signature_method'];
410 $platform->consumerName = $row['consumer_name'];
411 $platform->consumerVersion = $row['consumer_version'];
412 $platform->consumerGuid = $row['consumer_guid'];
413 $platform->profile = Util::jsonDecode($row['profile']);
414 $platform->toolProxy = $row['tool_proxy'];
415 $settings = Util::jsonDecode($row['settings'], true);
416 if (!is_array($settings)) {
417 $settings = @unserialize($row['settings']); // check for old serialized setting
418 }
419 if (!is_array($settings)) {
420 $settings = array();
421 }
422 $platform->setSettings($settings);
423 $platform->protected = (intval($row['protected']) === 1);
424 $platform->enabled = (intval($row['enabled']) === 1);
425 $platform->enableFrom = null;
426 if (!is_null($row['enable_from'])) {
427 $platform->enableFrom = strtotime($row['enable_from']);
428 }
429 $platform->enableUntil = null;
430 if (!is_null($row['enable_until'])) {
431 $platform->enableUntil = strtotime($row['enable_until']);
432 }
433 $platform->lastAccess = null;
434 if (!is_null($row['last_access'])) {
435 $platform->lastAccess = strtotime($row['last_access']);
436 }
437 $platform->created = strtotime($row['created']);
438 $platform->updated = strtotime($row['updated']);
439 $this->fixPlatformSettings($platform, false);
440 $platforms[] = $platform;
441 }
442 }
443
444 return $platforms;
445 }
446
447###
448### Context methods
449###
450
458 public function loadContext($context)
459 {
460 $ok = false;
461 if (!is_null($context->getRecordId())) {
462 $sql = 'SELECT context_pk, consumer_pk, title, lti_context_id, type, settings, created, updated ' .
463 "FROM {$this->dbTableNamePrefix}" . static::CONTEXT_TABLE_NAME . ' ' .
464 'WHERE (context_pk = :id)';
465 $query = $this->db->prepare($sql);
466 $query->bindValue('id', $context->getRecordId(), \PDO::PARAM_INT);
467 } else {
468 $sql = 'SELECT context_pk, consumer_pk, title, lti_context_id, type, settings, created, updated ' .
469 "FROM {$this->dbTableNamePrefix}" . static::CONTEXT_TABLE_NAME . ' ' .
470 'WHERE (consumer_pk = :cid) AND (lti_context_id = :ctx)';
471 $query = $this->db->prepare($sql);
472 $query->bindValue('cid', $context->getPlatform()->getRecordId(), \PDO::PARAM_INT);
473 $query->bindValue('ctx', $context->ltiContextId, \PDO::PARAM_STR);
474 }
475 $ok = $this->executeQuery($sql, $query);
476 if ($ok) {
477 $row = $query->fetch(\PDO::FETCH_ASSOC);
478 $ok = ($row !== false);
479 }
480 if ($ok) {
481 $row = array_change_key_case($row);
482 $context->setRecordId(intval($row['context_pk']));
483 $context->setPlatformId(intval($row['consumer_pk']));
484 $context->title = $row['title'];
485 $context->ltiContextId = $row['lti_context_id'];
486 $context->type = $row['type'];
487 $settings = Util::jsonDecode($row['settings'], true);
488 if (!is_array($settings)) {
489 $settings = @unserialize($row['settings']); // check for old serialized setting
490 }
491 if (!is_array($settings)) {
492 $settings = array();
493 }
494 $context->setSettings($settings);
495 $context->created = strtotime($row['created']);
496 $context->updated = strtotime($row['updated']);
497 }
498
499 return $ok;
500 }
501
509 public function saveContext($context)
510 {
511 $time = time();
512 $now = date("{$this->dateFormat} {$this->timeFormat}", $time);
513 $settingsValue = json_encode($context->getSettings());
514 $id = $context->getRecordId();
515 $consumer_pk = $context->getPlatform()->getRecordId();
516 if (empty($id)) {
517 $sql = "INSERT INTO {$this->dbTableNamePrefix}" . static::CONTEXT_TABLE_NAME . ' (consumer_pk, title, ' .
518 'lti_context_id, type, settings, created, updated) ' .
519 'VALUES (:cid, :title, :ctx, :type, :settings, :created, :updated)';
520 $query = $this->db->prepare($sql);
521 $query->bindValue('cid', $consumer_pk, \PDO::PARAM_INT);
522 $query->bindValue('title', $context->title, \PDO::PARAM_STR);
523 $query->bindValue('ctx', $context->ltiContextId, \PDO::PARAM_STR);
524 $query->bindValue('type', $context->type, \PDO::PARAM_STR);
525 $query->bindValue('settings', $settingsValue, \PDO::PARAM_STR);
526 $query->bindValue('created', $now, \PDO::PARAM_STR);
527 $query->bindValue('updated', $now, \PDO::PARAM_STR);
528 } else {
529 $sql = "UPDATE {$this->dbTableNamePrefix}" . static::CONTEXT_TABLE_NAME . ' SET ' .
530 'title = :title, lti_context_id = :ctx, type = :type, settings = :settings, ' .
531 'updated = :updated ' .
532 'WHERE (consumer_pk = :cid) AND (context_pk = :ctxid)';
533 $query = $this->db->prepare($sql);
534 $query->bindValue('title', $context->title, \PDO::PARAM_STR);
535 $query->bindValue('ctx', $context->ltiContextId, \PDO::PARAM_STR);
536 $query->bindValue('type', $context->type, \PDO::PARAM_STR);
537 $query->bindValue('settings', $settingsValue, \PDO::PARAM_STR);
538 $query->bindValue('updated', $now, \PDO::PARAM_STR);
539 $query->bindValue('cid', $consumer_pk, \PDO::PARAM_INT);
540 $query->bindValue('ctxid', $id, \PDO::PARAM_INT);
541 }
542 $ok = $this->executeQuery($sql, $query);
543 if ($ok) {
544 if (empty($id)) {
545 $context->setRecordId($this->getLastInsertId(static::CONTEXT_TABLE_NAME));
546 $context->created = $time;
547 }
548 $context->updated = $time;
549 }
550
551 return $ok;
552 }
553
561 public function deleteContext($context)
562 {
563 $id = $context->getRecordId();
564
565// Delete any outstanding share keys for resource links for this context
566 $sql = "DELETE FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_SHARE_KEY_TABLE_NAME . ' ' .
567 "WHERE resource_link_pk IN (SELECT resource_link_pk FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' ' .
568 'WHERE context_pk = :id)';
569 $query = $this->db->prepare($sql);
570 $query->bindValue('id', $id, \PDO::PARAM_INT);
571 $this->executeQuery($sql, $query);
572
573// Delete any users in resource links for this context
574 $sql = "DELETE FROM {$this->dbTableNamePrefix}" . static::USER_RESULT_TABLE_NAME . ' ' .
575 "WHERE resource_link_pk IN (SELECT resource_link_pk FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' ' .
576 'WHERE context_pk = :id)';
577 $query = $this->db->prepare($sql);
578 $query->bindValue('id', $id, \PDO::PARAM_INT);
579 $this->executeQuery($sql, $query);
580
581// Update any resource links for which this consumer is acting as a primary resource link
582 $sql = "UPDATE {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' ' .
583 'SET primary_resource_link_pk = null, share_approved = null ' .
584 'WHERE primary_resource_link_pk IN ' .
585 "(SELECT resource_link_pk FROM (SELECT resource_link_pk FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' WHERE context_pk = :id) t)';
586 $query = $this->db->prepare($sql);
587 $query->bindValue('id', $id, \PDO::PARAM_INT);
588 $this->executeQuery($sql, $query);
589
590// Delete any resource links for this consumer
591 $sql = "DELETE FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' ' .
592 'WHERE context_pk = :id';
593 $query = $this->db->prepare($sql);
594 $query->bindValue('id', $id, \PDO::PARAM_INT);
595 $this->executeQuery($sql, $query);
596
597// Delete context
598 $sql = "DELETE FROM {$this->dbTableNamePrefix}" . static::CONTEXT_TABLE_NAME . ' ' .
599 'WHERE context_pk = :id';
600 $query = $this->db->prepare($sql);
601 $query->bindValue('id', $id, \PDO::PARAM_INT);
602 $ok = $this->executeQuery($sql, $query);
603
604 if ($ok) {
605 $context->initialize();
606 }
607
608 return $ok;
609 }
610
611###
612### ResourceLink methods
613###
614
622 public function loadResourceLink($resourceLink)
623 {
624 if (!is_null($resourceLink->getRecordId())) {
625 $sql = 'SELECT resource_link_pk, context_pk, consumer_pk, title, lti_resource_link_id, settings, primary_resource_link_pk, share_approved, created, updated ' .
626 "FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' ' .
627 'WHERE (resource_link_pk = :id)';
628 $query = $this->db->prepare($sql);
629 $query->bindValue('id', $resourceLink->getRecordId(), \PDO::PARAM_INT);
630 } elseif (!is_null($resourceLink->getContext())) {
631 $sql = '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 ' .
632 "FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' r ' .
633 'WHERE (r.lti_resource_link_id = :rlid) AND ((r.context_pk = :id1) OR (r.consumer_pk IN (' .
634 'SELECT c.consumer_pk ' .
635 "FROM {$this->dbTableNamePrefix}" . static::CONTEXT_TABLE_NAME . ' c ' .
636 'WHERE (c.context_pk = :id2)' .
637 ')))';
638 $query = $this->db->prepare($sql);
639 $query->bindValue('rlid', $resourceLink->getId(), \PDO::PARAM_STR);
640 $query->bindValue('id1', $resourceLink->getContext()->getRecordId(), \PDO::PARAM_INT);
641 $query->bindValue('id2', $resourceLink->getContext()->getRecordId(), \PDO::PARAM_INT);
642 } else {
643 $sql = '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 ' .
644 "FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' r LEFT OUTER JOIN ' .
645 $this->dbTableNamePrefix . static::CONTEXT_TABLE_NAME . ' c ON r.context_pk = c.context_pk ' .
646 ' WHERE ((r.consumer_pk = :id1) OR (c.consumer_pk = :id2)) AND (lti_resource_link_id = :rlid)';
647 $query = $this->db->prepare($sql);
648 $query->bindValue('id1', $resourceLink->getPlatform()->getRecordId(), \PDO::PARAM_INT);
649 $query->bindValue('id2', $resourceLink->getPlatform()->getRecordId(), \PDO::PARAM_INT);
650 $query->bindValue('rlid', $resourceLink->getId(), \PDO::PARAM_STR);
651 }
652 $ok = $this->executeQuery($sql, $query);
653 if ($ok) {
654 $row = $query->fetch(\PDO::FETCH_ASSOC);
655 $ok = ($row !== false);
656 }
657
658 if ($ok) {
659 $row = array_change_key_case($row);
660 $resourceLink->setRecordId(intval($row['resource_link_pk']));
661 if (!is_null($row['context_pk'])) {
662 $resourceLink->setContextId(intval($row['context_pk']));
663 } else {
664 $resourceLink->setContextId(null);
665 }
666 if (!is_null($row['consumer_pk'])) {
667 $resourceLink->setPlatformId(intval($row['consumer_pk']));
668 } else {
669 $resourceLink->setPlatformId(null);
670 }
671 $resourceLink->title = $row['title'];
672 $resourceLink->ltiResourceLinkId = $row['lti_resource_link_id'];
673 $settings = Util::jsonDecode($row['settings'], true);
674 if (!is_array($settings)) {
675 $settings = @unserialize($row['settings']); // check for old serialized setting
676 }
677 if (!is_array($settings)) {
678 $settings = array();
679 }
680 $resourceLink->setSettings($settings);
681 if (!is_null($row['primary_resource_link_pk'])) {
682 $resourceLink->primaryResourceLinkId = intval($row['primary_resource_link_pk']);
683 } else {
684 $resourceLink->primaryResourceLinkId = null;
685 }
686 $resourceLink->shareApproved = (is_null($row['share_approved'])) ? null : (intval($row['share_approved']) === 1);
687 $resourceLink->created = strtotime($row['created']);
688 $resourceLink->updated = strtotime($row['updated']);
689 }
690
691 return $ok;
692 }
693
701 public function saveResourceLink($resourceLink)
702 {
703 if (is_null($resourceLink->shareApproved)) {
704 $approved = null;
705 } elseif ($resourceLink->shareApproved) {
706 $approved = 1;
707 } else {
708 $approved = 0;
709 }
710 $time = time();
711 $now = date("{$this->dateFormat} {$this->timeFormat}", $time);
712 $settingsValue = json_encode($resourceLink->getSettings());
713 if (!is_null($resourceLink->getContext())) {
714 $consumerId = null;
715 $contextId = $resourceLink->getContext()->getRecordId();
716 } elseif (!is_null($resourceLink->getContextId())) {
717 $consumerId = null;
718 $contextId = $resourceLink->getContextId();
719 } else {
720 $consumerId = $resourceLink->getPlatform()->getRecordId();
721 $contextId = null;
722 }
723 if (empty($resourceLink->primaryResourceLinkId)) {
724 $primaryResourceLinkId = null;
725 } else {
726 $primaryResourceLinkId = $resourceLink->primaryResourceLinkId;
727 }
728 $id = $resourceLink->getRecordId();
729 if (empty($id)) {
730 $sql = "INSERT INTO {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' (consumer_pk, context_pk, ' .
731 'title, lti_resource_link_id, settings, primary_resource_link_pk, share_approved, created, updated) ' .
732 'VALUES (:cid, :ctx, :title, :rlid, :settings, :prlid, :share_approved, :created, :updated)';
733 $query = $this->db->prepare($sql);
734 $query->bindValue('cid', $consumerId, \PDO::PARAM_INT);
735 $query->bindValue('ctx', $contextId, \PDO::PARAM_INT);
736 $query->bindValue('title', $resourceLink->title, \PDO::PARAM_STR);
737 $query->bindValue('rlid', $resourceLink->getId(), \PDO::PARAM_STR);
738 $query->bindValue('settings', $settingsValue, \PDO::PARAM_STR);
739 $query->bindValue('prlid', $primaryResourceLinkId, \PDO::PARAM_INT);
740 $query->bindValue('share_approved', $approved, \PDO::PARAM_INT);
741 $query->bindValue('created', $now, \PDO::PARAM_STR);
742 $query->bindValue('updated', $now, \PDO::PARAM_STR);
743 } elseif (!is_null($contextId)) {
744 $sql = "UPDATE {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' SET ' .
745 'consumer_pk = NULL, context_pk = :ctx, title = :title, lti_resource_link_id = :rlid, settings = :settings, ' .
746 'primary_resource_link_pk = :prlid, share_approved = :share_approved, updated = :updated ' .
747 'WHERE (resource_link_pk = :id)';
748 $query = $this->db->prepare($sql);
749 $query->bindValue('ctx', $contextId, \PDO::PARAM_INT);
750 $query->bindValue('title', $resourceLink->title, \PDO::PARAM_STR);
751 $query->bindValue('rlid', $resourceLink->getId(), \PDO::PARAM_STR);
752 $query->bindValue('settings', $settingsValue, \PDO::PARAM_STR);
753 $query->bindValue('prlid', $primaryResourceLinkId, \PDO::PARAM_INT);
754 $query->bindValue('share_approved', $approved, \PDO::PARAM_INT);
755 $query->bindValue('updated', $now, \PDO::PARAM_STR);
756 $query->bindValue('id', $id, \PDO::PARAM_INT);
757 } else {
758 $sql = "UPDATE {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' SET ' .
759 'context_pk = NULL, title = :title, lti_resource_link_id = :rlid, settings = :settings, ' .
760 'primary_resource_link_pk = :prlid, share_approved = :share_approved, updated = :updated ' .
761 'WHERE (consumer_pk = :cid) AND (resource_link_pk = :id)';
762 $query = $this->db->prepare($sql);
763 $query->bindValue('title', $resourceLink->title, \PDO::PARAM_STR);
764 $query->bindValue('rlid', $resourceLink->getId(), \PDO::PARAM_STR);
765 $query->bindValue('settings', $settingsValue, \PDO::PARAM_STR);
766 $query->bindValue('prlid', $primaryResourceLinkId, \PDO::PARAM_INT);
767 $query->bindValue('share_approved', $approved, \PDO::PARAM_INT);
768 $query->bindValue('updated', $now, \PDO::PARAM_STR);
769 $query->bindValue('cid', $consumerId, \PDO::PARAM_INT);
770 $query->bindValue('id', $id, \PDO::PARAM_INT);
771 }
772 $ok = $this->executeQuery($sql, $query);
773 if ($ok) {
774 if (empty($id)) {
775 $resourceLink->setRecordId($this->getLastInsertId(static::RESOURCE_LINK_TABLE_NAME));
776 $resourceLink->created = $time;
777 }
778 $resourceLink->updated = $time;
779 }
780
781 return $ok;
782 }
783
791 public function deleteResourceLink($resourceLink)
792 {
793 $id = $resourceLink->getRecordId();
794
795// Delete any outstanding share keys for resource links for this consumer
796 $sql = "DELETE FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_SHARE_KEY_TABLE_NAME . ' ' .
797 'WHERE (resource_link_pk = :id)';
798 $query = $this->db->prepare($sql);
799 $query->bindValue('id', $id, \PDO::PARAM_INT);
800 $ok = $this->executeQuery($sql, $query);
801
802// Delete users
803 if ($ok) {
804 $sql = "DELETE FROM {$this->dbTableNamePrefix}" . static::USER_RESULT_TABLE_NAME . ' ' .
805 'WHERE (resource_link_pk = :id)';
806 $query = $this->db->prepare($sql);
807 $query->bindValue('id', $id, \PDO::PARAM_INT);
808 $ok = $this->executeQuery($sql, $query);
809 }
810
811// Update any resource links for which this is the primary resource link
812 if ($ok) {
813 $sql = "UPDATE {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' ' .
814 'SET primary_resource_link_pk = NULL ' .
815 'WHERE (primary_resource_link_pk = :id)';
816 $query = $this->db->prepare($sql);
817 $query->bindValue('id', $id, \PDO::PARAM_INT);
818 $ok = $this->executeQuery($sql, $query);
819 }
820
821// Delete resource link
822 if ($ok) {
823 $sql = "DELETE FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' ' .
824 'WHERE (resource_link_pk = :id)';
825 $query = $this->db->prepare($sql);
826 $query->bindValue('id', $id, \PDO::PARAM_INT);
827 $ok = $this->executeQuery($sql, $query);
828 }
829
830 if ($ok) {
831 $resourceLink->initialize();
832 }
833
834 return $ok;
835 }
836
849 public function getUserResultSourcedIDsResourceLink($resourceLink, $localOnly, $idScope)
850 {
851 $id = $resourceLink->getRecordId();
852 $userResults = array();
853
854 if ($localOnly) {
855 $sql = 'SELECT u.user_result_pk, u.lti_result_sourcedid, u.lti_user_id, u.created, u.updated ' .
856 "FROM {$this->dbTableNamePrefix}" . static::USER_RESULT_TABLE_NAME . ' u ' .
857 "INNER JOIN {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' rl ' .
858 'ON u.resource_link_pk = rl.resource_link_pk ' .
859 'WHERE (rl.resource_link_pk = :id) AND (rl.primary_resource_link_pk IS NULL)';
860 $query = $this->db->prepare($sql);
861 $query->bindValue('id', $id, \PDO::PARAM_INT);
862 } else {
863 $sql = 'SELECT u.user_result_pk, u.lti_result_sourcedid, u.lti_user_id, u.created, u.updated ' .
864 "FROM {$this->dbTableNamePrefix}" . static::USER_RESULT_TABLE_NAME . ' u ' .
865 "INNER JOIN {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' rl ' .
866 'ON u.resource_link_pk = rl.resource_link_pk ' .
867 'WHERE ((rl.resource_link_pk = :id) AND (rl.primary_resource_link_pk IS NULL)) OR ' .
868 '((rl.primary_resource_link_pk = :pid) AND (share_approved = 1))';
869 $query = $this->db->prepare($sql);
870 $query->bindValue('id', $id, \PDO::PARAM_INT);
871 $query->bindValue('pid', $id, \PDO::PARAM_INT);
872 }
873 if ($this->executeQuery($sql, $query)) {
874 while ($row = $query->fetch(\PDO::FETCH_ASSOC)) {
875 $row = array_change_key_case($row);
876 $userresult = LTI\UserResult::fromRecordId($row['user_result_pk'], $resourceLink->getDataConnector());
877 $userresult->setRecordId(intval($row['user_result_pk']));
878 $userresult->ltiResultSourcedId = $row['lti_result_sourcedid'];
879 $userresult->created = strtotime($row['created']);
880 $userresult->updated = strtotime($row['updated']);
881 if (is_null($idScope)) {
882 $userResults[] = $userresult;
883 } else {
884 $userResults[$userresult->getId($idScope)] = $userresult;
885 }
886 }
887 }
888
889 return $userResults;
890 }
891
899 public function getSharesResourceLink($resourceLink)
900 {
901 $id = $resourceLink->getRecordId();
902
903 $shares = array();
904
905 $sql = 'SELECT c.consumer_name consumer_name, r.resource_link_pk resource_link_pk, r.title title, r.share_approved share_approved ' .
906 "FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' r ' .
907 "INNER JOIN {$this->dbTableNamePrefix}" . static::PLATFORM_TABLE_NAME . ' c ON r.consumer_pk = c.consumer_pk ' .
908 'WHERE (r.primary_resource_link_pk = :id1) ' .
909 'UNION ' .
910 'SELECT c2.consumer_name consumer_name, r2.resource_link_pk resource_link_pk, r2.title title, r2.share_approved share_approved ' .
911 "FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' r2 ' .
912 "INNER JOIN {$this->dbTableNamePrefix}" . static::CONTEXT_TABLE_NAME . ' x ON r2.context_pk = x.context_pk ' .
913 "INNER JOIN {$this->dbTableNamePrefix}" . static::PLATFORM_TABLE_NAME . ' c2 ON x.consumer_pk = c2.consumer_pk ' .
914 'WHERE (r2.primary_resource_link_pk = :id2) ' .
915 'ORDER BY consumer_name, title';
916 $query = $this->db->prepare($sql);
917 $query->bindValue('id1', $id, \PDO::PARAM_INT);
918 $query->bindValue('id2', $id, \PDO::PARAM_INT);
919 if ($this->executeQuery($sql, $query)) {
920 while ($row = $query->fetch(\PDO::FETCH_ASSOC)) {
921 $row = array_change_key_case($row);
922 $share = new LTI\ResourceLinkShare();
923 $share->consumerName = $row['consumer_name'];
924 $share->resourceLinkId = intval($row['resource_link_pk']);
925 $share->title = $row['title'];
926 $share->approved = (intval($row['share_approved']) === 1);
927 $shares[] = $share;
928 }
929 }
930
931 return $shares;
932 }
933
934###
935### PlatformNonce methods
936###
937
945 public function loadPlatformNonce($nonce)
946 {
947 if (parent::useMemcache()) {
948 $ok = parent::loadPlatformNonce($nonce);
949 } else {
950// Delete any expired nonce values
951 $now = date("{$this->dateFormat} {$this->timeFormat}", time());
952 $sql = "DELETE FROM {$this->dbTableNamePrefix}" . static::NONCE_TABLE_NAME . ' WHERE expires <= :now';
953 $query = $this->db->prepare($sql);
954 $query->bindValue('now', $now, \PDO::PARAM_STR);
955 $this->executeQuery($sql, $query);
956
957// Load the nonce
958 $id = $nonce->getPlatform()->getRecordId();
959 $value = $nonce->getValue();
960 $sql = "SELECT value T FROM {$this->dbTableNamePrefix}" . static::NONCE_TABLE_NAME . ' WHERE (consumer_pk = :id) AND (value = :value)';
961 $query = $this->db->prepare($sql);
962 $query->bindValue('id', $id, \PDO::PARAM_INT);
963 $query->bindValue('value', $value, \PDO::PARAM_STR);
964 $ok = $this->executeQuery($sql, $query, false);
965 if ($ok) {
966 $row = $query->fetch(\PDO::FETCH_ASSOC);
967 if ($row === false) {
968 $ok = false;
969 }
970 }
971 }
972
973 return $ok;
974 }
975
983 public function savePlatformNonce($nonce)
984 {
985 if (parent::useMemcache()) {
986 $ok = parent::savePlatformNonce($nonce);
987 } else {
988 $id = $nonce->getPlatform()->getRecordId();
989 $value = $nonce->getValue();
990 $expires = date("{$this->dateFormat} {$this->timeFormat}", $nonce->expires);
991 $sql = "INSERT INTO {$this->dbTableNamePrefix}" . static::NONCE_TABLE_NAME . ' (consumer_pk, value, expires) VALUES (:id, :value, :expires)';
992 $query = $this->db->prepare($sql);
993 $query->bindValue('id', $id, \PDO::PARAM_INT);
994 $query->bindValue('value', $value, \PDO::PARAM_STR);
995 $query->bindValue('expires', $expires, \PDO::PARAM_STR);
996 $ok = $this->executeQuery($sql, $query);
997 }
998
999 return $ok;
1000 }
1001
1009 public function deletePlatformNonce($nonce)
1010 {
1011 if (parent::useMemcache()) {
1012 $ok = parent::deletePlatformNonce($nonce);
1013 } else {
1014 $sql = "DELETE FROM {$this->dbTableNamePrefix}" . static::NONCE_TABLE_NAME . ' ' .
1015 'WHERE (consumer_pk = :id) AND (value = :value)';
1016 $query = $this->db->prepare($sql);
1017 $id = $nonce->getPlatform()->getRecordId();
1018 $query->bindValue('id', $id, \PDO::PARAM_STR);
1019 $value = $nonce->getValue();
1020 $query->bindValue('value', $value, \PDO::PARAM_STR);
1021 $ok = $this->executeQuery($sql, $query);
1022 }
1023
1024 return $ok;
1025 }
1026
1027###
1028### AccessToken methods
1029###
1030
1038 public function loadAccessToken($accessToken)
1039 {
1040 if (parent::useMemcache()) {
1041 $ok = parent::loadAccessToken($accessToken);
1042 } else {
1043 $ok = false;
1044 $consumer_pk = $accessToken->getPlatform()->getRecordId();
1045 $sql = "SELECT scopes, token, expires, created, updated FROM {$this->dbTableNamePrefix}" . static::ACCESS_TOKEN_TABLE_NAME . ' ' .
1046 'WHERE (consumer_pk = :consumer_pk)';
1047 $query = $this->db->prepare($sql);
1048 $query->bindValue('consumer_pk', $consumer_pk, \PDO::PARAM_INT);
1049 if ($this->executeQuery($sql, $query, false)) {
1050 $row = $query->fetch(\PDO::FETCH_ASSOC);
1051 if ($row !== false) {
1052 $row = array_change_key_case($row);
1053 $scopes = Util::jsonDecode($row['scopes'], true);
1054 if (!is_array($scopes)) {
1055 $scopes = array();
1056 }
1057 $accessToken->scopes = $scopes;
1058 $accessToken->token = $row['token'];
1059 $accessToken->expires = strtotime($row['expires']);
1060 $accessToken->created = strtotime($row['created']);
1061 $accessToken->updated = strtotime($row['updated']);
1062 $ok = true;
1063 }
1064 }
1065 }
1066
1067 return $ok;
1068 }
1069
1077 public function saveAccessToken($accessToken)
1078 {
1079 if (parent::useMemcache()) {
1080 $ok = parent::saveAccessToken($accessToken);
1081 } else {
1082 $consumer_pk = $accessToken->getPlatform()->getRecordId();
1083 $scopes = json_encode($accessToken->scopes, JSON_UNESCAPED_SLASHES);
1084 $token = $accessToken->token;
1085 $expires = date("{$this->dateFormat} {$this->timeFormat}", $accessToken->expires);
1086 $time = time();
1087 $now = date("{$this->dateFormat} {$this->timeFormat}", $time);
1088 if (empty($accessToken->created)) {
1089 $sql = "INSERT INTO {$this->dbTableNamePrefix}" . static::ACCESS_TOKEN_TABLE_NAME . ' ' .
1090 '(consumer_pk, scopes, token, expires, created, updated) ' .
1091 'VALUES (:consumer_pk, :scopes, :token, :expires, :created, :updated)';
1092 $query = $this->db->prepare($sql);
1093 $query->bindValue('consumer_pk', $consumer_pk, \PDO::PARAM_INT);
1094 $query->bindValue('scopes', $scopes, \PDO::PARAM_STR);
1095 $query->bindValue('token', $token, \PDO::PARAM_STR);
1096 $query->bindValue('expires', $expires, \PDO::PARAM_STR);
1097 $query->bindValue('created', $now, \PDO::PARAM_STR);
1098 $query->bindValue('updated', $now, \PDO::PARAM_STR);
1099 } else {
1100 $sql = 'UPDATE ' . $this->dbTableNamePrefix . static::ACCESS_TOKEN_TABLE_NAME . ' ' .
1101 'SET scopes = :scopes, token = :token, expires = :expires, updated = :updated ' .
1102 'WHERE consumer_pk = :consumer_pk';
1103 $query = $this->db->prepare($sql);
1104 $query->bindValue('scopes', $scopes, \PDO::PARAM_STR);
1105 $query->bindValue('token', $token, \PDO::PARAM_STR);
1106 $query->bindValue('expires', $expires, \PDO::PARAM_STR);
1107 $query->bindValue('updated', $now, \PDO::PARAM_STR);
1108 $query->bindValue('consumer_pk', $consumer_pk, \PDO::PARAM_INT);
1109 }
1110 $ok = $this->executeQuery($sql, $query);
1111 }
1112
1113 return $ok;
1114 }
1115
1116###
1117### ResourceLinkShareKey methods
1118###
1119
1127 public function loadResourceLinkShareKey($shareKey)
1128 {
1129 $ok = false;
1130
1131// Clear expired share keys
1132 $now = date("{$this->dateFormat} {$this->timeFormat}", time());
1133 $sql = "DELETE FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_SHARE_KEY_TABLE_NAME . ' WHERE expires <= :now';
1134 $query = $this->db->prepare($sql);
1135 $query->bindValue('now', $now, \PDO::PARAM_STR);
1136 $this->executeQuery($sql, $query);
1137
1138// Load share key
1139 $id = $shareKey->getId();
1140 $sql = 'SELECT resource_link_pk, auto_approve, expires ' .
1141 "FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_SHARE_KEY_TABLE_NAME . ' ' .
1142 'WHERE share_key_id = :id';
1143 $query = $this->db->prepare($sql);
1144 $query->bindValue('id', $id, \PDO::PARAM_STR);
1145 if ($this->executeQuery($sql, $query)) {
1146 $row = $query->fetch(\PDO::FETCH_ASSOC);
1147 if ($row !== false) {
1148 $row = array_change_key_case($row);
1149 $shareKey->resourceLinkId = intval($row['resource_link_pk']);
1150 $shareKey->autoApprove = (intval($row['auto_approve']) === 1);
1151 $shareKey->expires = strtotime($row['expires']);
1152 $ok = true;
1153 }
1154 }
1155
1156 return $ok;
1157 }
1158
1166 public function saveResourceLinkShareKey($shareKey)
1167 {
1168 $id = $shareKey->getId();
1169 $autoApprove = ($shareKey->autoApprove) ? 1 : 0;
1170 $expires = date("{$this->dateFormat} {$this->timeFormat}", $shareKey->expires);
1171 $sql = "INSERT INTO {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_SHARE_KEY_TABLE_NAME . ' ' .
1172 '(share_key_id, resource_link_pk, auto_approve, expires) ' .
1173 'VALUES (:id, :prlid, :approve, :expires)';
1174 $query = $this->db->prepare($sql);
1175 $query->bindValue('id', $id, \PDO::PARAM_STR);
1176 $query->bindValue('prlid', $shareKey->resourceLinkId, \PDO::PARAM_INT);
1177 $query->bindValue('approve', $autoApprove, \PDO::PARAM_INT);
1178 $query->bindValue('expires', $expires, \PDO::PARAM_STR);
1179 $ok = $this->executeQuery($sql, $query);
1180
1181 return $ok;
1182 }
1183
1191 public function deleteResourceLinkShareKey($shareKey)
1192 {
1193 $id = $shareKey->getId();
1194 $sql = "DELETE FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_SHARE_KEY_TABLE_NAME . ' WHERE share_key_id = :id';
1195 $query = $this->db->prepare($sql);
1196 $query->bindValue('id', $id, \PDO::PARAM_STR);
1197 $ok = $this->executeQuery($sql, $query);
1198
1199 if ($ok) {
1200 $shareKey->initialize();
1201 }
1202
1203 return $ok;
1204 }
1205
1206###
1207### UserResult Result methods
1208###
1209
1217 public function loadUserResult($userresult)
1218 {
1219 $ok = false;
1220 if (!is_null($userresult->getRecordId())) {
1221 $id = $userresult->getRecordId();
1222 $sql = 'SELECT user_result_pk, resource_link_pk, lti_user_id, lti_result_sourcedid, created, updated ' .
1223 "FROM {$this->dbTableNamePrefix}" . static::USER_RESULT_TABLE_NAME . ' ' .
1224 'WHERE (user_result_pk = :id)';
1225 $query = $this->db->prepare($sql);
1226 $query->bindValue('id', $id, \PDO::PARAM_INT);
1227 } else {
1228 $id = $userresult->getResourceLink()->getRecordId();
1229 $uid = $userresult->getId(LTI\Tool::ID_SCOPE_ID_ONLY);
1230 $sql = 'SELECT user_result_pk, resource_link_pk, lti_user_id, lti_result_sourcedid, created, updated ' .
1231 "FROM {$this->dbTableNamePrefix}" . static::USER_RESULT_TABLE_NAME . ' ' .
1232 'WHERE (resource_link_pk = :id) AND (lti_user_id = :u_id)';
1233 $query = $this->db->prepare($sql);
1234 $query->bindValue('id', $id, \PDO::PARAM_INT);
1235 $query->bindValue('u_id', $uid, \PDO::PARAM_STR);
1236 }
1237 if ($this->executeQuery($sql, $query)) {
1238 $row = $query->fetch(\PDO::FETCH_ASSOC);
1239 if ($row !== false) {
1240 $row = array_change_key_case($row);
1241 $userresult->setRecordId(intval($row['user_result_pk']));
1242 $userresult->setResourceLinkId(intval($row['resource_link_pk']));
1243 $userresult->ltiUserId = $row['lti_user_id'];
1244 $userresult->ltiResultSourcedId = $row['lti_result_sourcedid'];
1245 $userresult->created = strtotime($row['created']);
1246 $userresult->updated = strtotime($row['updated']);
1247 $ok = true;
1248 }
1249 }
1250
1251 return $ok;
1252 }
1253
1261 public function saveUserResult($userresult)
1262 {
1263 $time = time();
1264 $now = date("{$this->dateFormat} {$this->timeFormat}", $time);
1265 if (is_null($userresult->created)) {
1266 $sql = "INSERT INTO {$this->dbTableNamePrefix}" . static::USER_RESULT_TABLE_NAME . ' (resource_link_pk, ' .
1267 'lti_user_id, lti_result_sourcedid, created, updated) ' .
1268 'VALUES (:rlid, :u_id, :sourcedid, :created, :updated)';
1269 $query = $this->db->prepare($sql);
1270 $query->bindValue('rlid', $userresult->getResourceLink()->getRecordId(), \PDO::PARAM_INT);
1271 $query->bindValue('u_id', $userresult->getId(LTI\Tool::ID_SCOPE_ID_ONLY), \PDO::PARAM_STR);
1272 $query->bindValue('sourcedid', $userresult->ltiResultSourcedId, \PDO::PARAM_STR);
1273 $query->bindValue('created', $now, \PDO::PARAM_STR);
1274 $query->bindValue('updated', $now, \PDO::PARAM_STR);
1275 } else {
1276 $sql = "UPDATE {$this->dbTableNamePrefix}" . static::USER_RESULT_TABLE_NAME . ' ' .
1277 'SET lti_result_sourcedid = :sourcedid, updated = :updated ' .
1278 'WHERE (user_result_pk = :id)';
1279 $query = $this->db->prepare($sql);
1280 $query->bindValue('sourcedid', $userresult->ltiResultSourcedId, \PDO::PARAM_STR);
1281 $query->bindValue('updated', $now, \PDO::PARAM_STR);
1282 $query->bindValue('id', $userresult->getRecordId(), \PDO::PARAM_INT);
1283 }
1284 $ok = $this->executeQuery($sql, $query);
1285 if ($ok) {
1286 if (is_null($userresult->created)) {
1287 $userresult->setRecordId($this->getLastInsertId(static::USER_RESULT_TABLE_NAME));
1288 $userresult->created = $time;
1289 }
1290 $userresult->updated = $time;
1291 }
1292
1293 return $ok;
1294 }
1295
1303 public function deleteUserResult($userresult)
1304 {
1305 $sql = "DELETE FROM {$this->dbTableNamePrefix}" . static::USER_RESULT_TABLE_NAME . ' ' .
1306 'WHERE (user_result_pk = :id)';
1307 $query = $this->db->prepare($sql);
1308 $query->bindValue('id', $userresult->getRecordId(), \PDO::PARAM_INT);
1309 $ok = $this->executeQuery($sql, $query);
1310
1311 if ($ok) {
1312 $userresult->initialize();
1313 }
1314
1315 return $ok;
1316 }
1317
1318###
1319### Tool methods
1320###
1321
1329 public function loadTool($tool)
1330 {
1331 $ok = false;
1332 if (!is_null($tool->getRecordId())) {
1333 $sql = 'SELECT tool_pk, name, consumer_key, secret, ' .
1334 'message_url, initiate_login_url, redirection_uris, public_key, ' .
1335 'lti_version, signature_method, settings, enabled, ' .
1336 'enable_from, enable_until, last_access, created, updated ' .
1337 "FROM {$this->dbTableNamePrefix}" . static::TOOL_TABLE_NAME . ' ' .
1338 'WHERE tool_pk = :id';
1339 $query = $this->db->prepare($sql);
1340 $id = $tool->getRecordId();
1341 $query->bindValue('id', $id, \PDO::PARAM_INT);
1342 } elseif (!empty($tool->initiateLoginUrl)) {
1343 $sql = 'SELECT tool_pk, name, consumer_key, secret, ' .
1344 'message_url, initiate_login_url, redirection_uris, public_key, ' .
1345 'lti_version, signature_method, settings, enabled, ' .
1346 'enable_from, enable_until, last_access, created, updated ' .
1347 "FROM {$this->dbTableNamePrefix}" . static::TOOL_TABLE_NAME . ' ' .
1348 'WHERE initiate_login_url = :initiate_login_url';
1349 $query = $this->db->prepare($sql);
1350 $query->bindValue('initiate_login_url', $tool->initiateLoginUrl, \PDO::PARAM_STR);
1351 } else {
1352 $sql = 'SELECT tool_pk, name, consumer_key, secret, ' .
1353 'message_url, initiate_login_url, redirection_uris, public_key, ' .
1354 'lti_version, signature_method, settings, enabled, ' .
1355 'enable_from, enable_until, last_access, created, updated ' .
1356 "FROM {$this->dbTableNamePrefix}" . static::TOOL_TABLE_NAME . ' ' .
1357 'WHERE consumer_key = :key';
1358 $query = $this->db->prepare($sql);
1359 $consumer_key = $tool->getKey();
1360 $query->bindValue('key', $consumer_key, \PDO::PARAM_STR);
1361 }
1362 $ok = $this->executeQuery($sql, $query);
1363 if ($ok) {
1364 $row = $query->fetch(\PDO::FETCH_ASSOC);
1365 $ok = ($row !== false);
1366 }
1367 if ($ok) {
1368 $row = array_change_key_case($row);
1369 $tool->setRecordId(intval($row['tool_pk']));
1370 $tool->name = $row['name'];
1371 $tool->setkey($row['consumer_key']);
1372 $tool->secret = $row['secret'];
1373 $tool->messageUrl = $row['message_url'];
1374 $tool->initiateLoginUrl = $row['initiate_login_url'];
1375 $tool->redirectionUris = Util::jsonDecode($row['redirection_uris'], true);
1376 if (!is_array($tool->redirectionUris)) {
1377 $tool->redirectionUris = array();
1378 }
1379 $tool->rsaKey = $row['public_key'];
1380 $tool->ltiVersion = $row['lti_version'];
1381 $tool->signatureMethod = $row['signature_method'];
1382 $settings = Util::jsonDecode($row['settings'], true);
1383 if (!is_array($settings)) {
1384 $settings = array();
1385 }
1386 $tool->setSettings($settings);
1387 $tool->enabled = (intval($row['enabled']) === 1);
1388 $tool->enableFrom = null;
1389 if (!is_null($row['enable_from'])) {
1390 $tool->enableFrom = strtotime($row['enable_from']);
1391 }
1392 $tool->enableUntil = null;
1393 if (!is_null($row['enable_until'])) {
1394 $tool->enableUntil = strtotime($row['enable_until']);
1395 }
1396 $tool->lastAccess = null;
1397 if (!is_null($row['last_access'])) {
1398 $tool->lastAccess = strtotime($row['last_access']);
1399 }
1400 $tool->created = strtotime($row['created']);
1401 $tool->updated = strtotime($row['updated']);
1402 $this->fixToolSettings($tool, false);
1403 $ok = true;
1404 }
1405
1406 return $ok;
1407 }
1408
1416 public function saveTool($tool)
1417 {
1418 $id = $tool->getRecordId();
1419 $consumer_key = $tool->getKey();
1420 $enabled = ($tool->enabled) ? 1 : 0;
1421 $redirectionUrisValue = json_encode($tool->redirectionUris);
1422 $this->fixToolSettings($tool, true);
1423 $settingsValue = json_encode($tool->getSettings());
1424 $this->fixToolSettings($tool, false);
1425 $time = time();
1426 $now = date("{$this->dateFormat} {$this->timeFormat}", $time);
1427 $from = null;
1428 if (!is_null($tool->enableFrom)) {
1429 $from = date("{$this->dateFormat} {$this->timeFormat}", $tool->enableFrom);
1430 }
1431 $until = null;
1432 if (!is_null($tool->enableUntil)) {
1433 $until = date("{$this->dateFormat} {$this->timeFormat}", $tool->enableUntil);
1434 }
1435 $last = null;
1436 if (!is_null($tool->lastAccess)) {
1437 $last = date($this->dateFormat, $tool->lastAccess);
1438 }
1439 if (empty($id)) {
1440 $sql = "INSERT INTO {$this->dbTableNamePrefix}" . static::TOOL_TABLE_NAME . ' (name, consumer_key, secret, ' .
1441 'message_url, initiate_login_url, redirection_uris, public_key, ' .
1442 'lti_version, signature_method, settings, enabled, enable_from, enable_until, ' .
1443 'last_access, created, updated) ' .
1444 'VALUES (:name, :key, :secret, ' .
1445 ':message_url, :initiate_login_url, :redirection_uris, :public_key, ' .
1446 ':lti_version, :signature_method, :settings, :enabled, :enable_from, :enable_until, ' .
1447 ':last_access, :created, :updated)';
1448 $query = $this->db->prepare($sql);
1449 $query->bindValue('name', $tool->name, \PDO::PARAM_STR);
1450 $query->bindValue('key', $consumer_key, \PDO::PARAM_STR);
1451 $query->bindValue('secret', $tool->secret, \PDO::PARAM_STR);
1452 $query->bindValue('message_url', $tool->messageUrl, \PDO::PARAM_STR);
1453 $query->bindValue('initiate_login_url', $tool->initiateLoginUrl, \PDO::PARAM_STR);
1454 $query->bindValue('redirection_uris', $redirectionUrisValue, \PDO::PARAM_STR);
1455 $query->bindValue('public_key', $tool->rsaKey, \PDO::PARAM_STR);
1456 $query->bindValue('lti_version', $tool->ltiVersion, \PDO::PARAM_STR);
1457 $query->bindValue('signature_method', $tool->signatureMethod, \PDO::PARAM_STR);
1458 $query->bindValue('settings', $settingsValue, \PDO::PARAM_STR);
1459 $query->bindValue('enabled', $enabled, \PDO::PARAM_INT);
1460 $query->bindValue('enable_from', $from, \PDO::PARAM_STR);
1461 $query->bindValue('enable_until', $until, \PDO::PARAM_STR);
1462 $query->bindValue('last_access', $last, \PDO::PARAM_STR);
1463 $query->bindValue('created', $now, \PDO::PARAM_STR);
1464 $query->bindValue('updated', $now, \PDO::PARAM_STR);
1465 } else {
1466 $sql = "UPDATE {$this->dbTableNamePrefix}" . static::TOOL_TABLE_NAME . ' SET ' .
1467 'name = :name, consumer_key = :key, secret= :secret, ' .
1468 'message_url = :message_url, initiate_login_url = :initiate_login_url, redirection_uris = :redirection_uris, public_key = :public_key, ' .
1469 'lti_version = :lti_version, signature_method = :signature_method, settings = :settings, enabled = :enabled, enable_from = :enable_from, enable_until = :enable_until, ' .
1470 'last_access = :last_access, updated = :updated ' .
1471 'WHERE tool_pk = :id';
1472 $query = $this->db->prepare($sql);
1473 $query->bindValue('name', $tool->name, \PDO::PARAM_STR);
1474 $query->bindValue('key', $consumer_key, \PDO::PARAM_STR);
1475 $query->bindValue('secret', $tool->secret, \PDO::PARAM_STR);
1476 $query->bindValue('message_url', $tool->messageUrl, \PDO::PARAM_STR);
1477 $query->bindValue('initiate_login_url', $tool->initiateLoginUrl, \PDO::PARAM_STR);
1478 $query->bindValue('redirection_uris', $redirectionUrisValue, \PDO::PARAM_STR);
1479 $query->bindValue('public_key', $tool->rsaKey, \PDO::PARAM_STR);
1480 $query->bindValue('lti_version', $tool->ltiVersion, \PDO::PARAM_STR);
1481 $query->bindValue('signature_method', $tool->signatureMethod, \PDO::PARAM_STR);
1482 $query->bindValue('settings', $settingsValue, \PDO::PARAM_STR);
1483 $query->bindValue('enabled', $enabled, \PDO::PARAM_INT);
1484 $query->bindValue('enable_from', $from, \PDO::PARAM_STR);
1485 $query->bindValue('enable_until', $until, \PDO::PARAM_STR);
1486 $query->bindValue('last_access', $last, \PDO::PARAM_STR);
1487 $query->bindValue('updated', $now, \PDO::PARAM_STR);
1488 $query->bindValue('id', $id, \PDO::PARAM_INT);
1489 }
1490 $ok = $this->executeQuery($sql, $query);
1491 if ($ok) {
1492 if (empty($id)) {
1493 $tool->setRecordId($this->getLastInsertId(static::TOOL_TABLE_NAME));
1494 $tool->created = $time;
1495 }
1496 $tool->updated = $time;
1497 }
1498
1499 return $ok;
1500 }
1501
1509 public function deleteTool($tool)
1510 {
1511 $id = $tool->getRecordId();
1512
1513 $sql = "DELETE FROM {$this->dbTableNamePrefix}" . static::TOOL_TABLE_NAME . ' ' .
1514 'WHERE tool_pk = :id';
1515 $query = $this->db->prepare($sql);
1516 $query->bindValue('id', $id, \PDO::PARAM_INT);
1517 $ok = $this->executeQuery($sql, $query);
1518
1519 if ($ok) {
1520 $tool->initialize();
1521 }
1522
1523 return $ok;
1524 }
1525
1531 public function getTools()
1532 {
1533 $tools = array();
1534
1535 $sql = 'SELECT tool_pk, name, consumer_key, secret, ' .
1536 'message_url, initiate_login_url, redirection_uris, public_key, ' .
1537 'lti_version, signature_method, settings, enabled, ' .
1538 'enable_from, enable_until, last_access, created, updated ' .
1539 "FROM {$this->dbTableNamePrefix}" . static::TOOL_TABLE_NAME . ' ' .
1540 'ORDER BY name';
1541 $query = $this->db->prepare($sql);
1542 $ok = ($query !== false);
1543
1544 if ($ok) {
1545 $ok = $this->executeQuery($sql, $query);
1546 }
1547
1548 if ($ok) {
1549 while ($row = $query->fetch(\PDO::FETCH_ASSOC)) {
1550 $row = array_change_key_case($row);
1551 $tool = new Tool($this);
1552 $tool->setRecordId(intval($row['tool_pk']));
1553 $tool->name = $row['name'];
1554 $tool->setkey($row['consumer_key']);
1555 $tool->secret = $row['secret'];
1556 $tool->messageUrl = $row['message_url'];
1557 $tool->initiateLoginUrl = $row['initiate_login_url'];
1558 $tool->redirectionUris = Util::jsonDecode($row['redirection_uris'], true);
1559 if (!is_array($tool->redirectionUris)) {
1560 $tool->redirectionUris = array();
1561 }
1562 $tool->rsaKey = $row['public_key'];
1563 $tool->ltiVersion = $row['lti_version'];
1564 $tool->signatureMethod = $row['signature_method'];
1565 $settings = Util::jsonDecode($row['settings'], true);
1566 if (!is_array($settings)) {
1567 $settings = array();
1568 }
1569 $tool->setSettings($settings);
1570 $tool->enabled = (intval($row['enabled']) === 1);
1571 $tool->enableFrom = null;
1572 if (!is_null($row['enable_from'])) {
1573 $tool->enableFrom = strtotime($row['enable_from']);
1574 }
1575 $tool->enableUntil = null;
1576 if (!is_null($row['enable_until'])) {
1577 $tool->enableUntil = strtotime($row['enable_until']);
1578 }
1579 $tool->lastAccess = null;
1580 if (!is_null($row['last_access'])) {
1581 $tool->lastAccess = strtotime($row['last_access']);
1582 }
1583 $tool->created = strtotime($row['created']);
1584 $tool->updated = strtotime($row['updated']);
1585 $this->fixToolSettings($tool, false);
1586 $tools[] = $tool;
1587 }
1588 }
1589
1590 return $tools;
1591 }
1592
1593###
1594### Other methods
1595###
1596
1604 protected function getLastInsertId($tableName)
1605 {
1606 return intval($this->db->lastInsertId());
1607 }
1608
1620 protected function executeQuery($sql, $query, $reportError = true)
1621 {
1622 try {
1623 $ok = $query->execute();
1624 } catch (\PDOException $e) {
1625 $ok = false;
1626 }
1628 ob_start();
1629 $query->debugDumpParams();
1630 $debug = ob_get_contents();
1631 ob_end_clean();
1632 $pos = strpos($debug, 'Sent SQL: [');
1633 if ($pos !== false) {
1634 $debug = substr($debug, $pos + 11);
1635 $pos = strpos($debug, '] ');
1636 if ($pos !== false) {
1637 $n = substr($debug, 0, $pos);
1638 if (is_numeric($n)) {
1639 $sql = substr($debug, $pos + 2, intval($n));
1640 }
1641 }
1642 }
1643 if (!$ok && $reportError) {
1644 Util::logError($sql . $this->errorInfoToString($query->errorInfo()));
1645 } else {
1646 Util::logDebug("{$sql} (row count = {$query->rowCount()})");
1647 }
1648 }
1649
1650 return $ok;
1651 }
1652
1660 private function errorInfoToString($errorInfo)
1661 {
1662 if (is_array($errorInfo) && (count($errorInfo) === 3)) {
1663 $errors = PHP_EOL . "Error {$errorInfo[0]}/{$errorInfo[1]}: {$errorInfo[2]}";
1664 } else {
1665 $errors = '';
1666 }
1667
1668 return $errors;
1669 }
1670
1671}
Class to represent an HTTP message.
Class to represent a platform context.
Definition Context.php:18
Class to represent an LTI Data Connector for PDO connections.
loadResourceLinkShareKey($shareKey)
Load resource link share key object.
getLastInsertId($tableName)
Get the ID for the last record inserted into a table.
savePlatform($platform)
Save platform object.
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.
loadPlatform($platform)
Load platform object.
getUserResultSourcedIDsResourceLink($resourceLink, $localOnly, $idScope)
Get array of user objects.
getSharesResourceLink($resourceLink)
Get array of shares defined for this resource link.
loadResourceLink($resourceLink)
Load resource link object.
saveResourceLink($resourceLink)
Save resource link object.
deleteContext($context)
Delete context object.
executeQuery($sql, $query, $reportError=true)
Execute a database query.
deleteUserResult($userresult)
Delete user 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.
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
static fromConsumerKey($key=null, $dataConnector=null, $autoEnable=false)
Load the platform from the database by its consumer key.
Definition Platform.php:486
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 $logLevel
Current logging level.
Definition Util.php:202
static logError($message, $showSource=true)
Log an error message.
Definition Util.php:248
const LOGLEVEL_DEBUG
Log all messages.
Definition Util.php:159
static jsonDecode($str, $associative=false)
Decode a JSON string.
Definition Util.php:560
const LOGLEVEL_ERROR
Log errors only.
Definition Util.php:149
static logDebug($message, $showSource=false)
Log a debug message.
Definition Util.php:274