24 # NB This class assumes that a MySQL connection has already been opened to the appropriate schema 32 ### ToolConsumer methods 46 if (!empty($consumer->getRecordId())) {
47 $sql = sprintf(
'SELECT consumer_pk, name, consumer_key256, consumer_key, secret, lti_version, ' .
48 'consumer_name, consumer_version, consumer_guid, ' .
49 'profile, tool_proxy, settings, protected, enabled, ' .
50 'enable_from, enable_until, last_access, created, updated ' .
52 "WHERE consumer_pk = %d",
53 $consumer->getRecordId());
56 $sql = sprintf(
'SELECT consumer_pk, name, consumer_key256, consumer_key, secret, lti_version, ' .
57 'consumer_name, consumer_version, consumer_guid, ' .
58 'profile, tool_proxy, settings, protected, enabled, ' .
59 'enable_from, enable_until, last_access, created, updated ' .
61 "WHERE consumer_key256 = %s",
64 $rsConsumer = mysql_query($sql);
66 while (
$row = mysql_fetch_object($rsConsumer)) {
67 if (empty($key256) || empty(
$row->consumer_key) || ($consumer->getKey() ===
$row->consumer_key)) {
68 $consumer->setRecordId(intval(
$row->consumer_pk));
69 $consumer->name =
$row->name;
70 $consumer->setkey(empty(
$row->consumer_key) ?
$row->consumer_key256 :
$row->consumer_key);
71 $consumer->secret =
$row->secret;
72 $consumer->ltiVersion =
$row->lti_version;
73 $consumer->consumerName =
$row->consumer_name;
74 $consumer->consumerVersion =
$row->consumer_version;
75 $consumer->consumerGuid =
$row->consumer_guid;
76 $consumer->profile = json_decode(
$row->profile);
77 $consumer->toolProxy =
$row->tool_proxy;
78 $settings = unserialize(
$row->settings);
79 if (!is_array($settings)) {
82 $consumer->setSettings($settings);
83 $consumer->protected = (intval(
$row->protected) === 1);
84 $consumer->enabled = (intval(
$row->enabled) === 1);
85 $consumer->enableFrom = null;
86 if (!is_null(
$row->enable_from)) {
87 $consumer->enableFrom = strtotime(
$row->enable_from);
89 $consumer->enableUntil = null;
90 if (!is_null(
$row->enable_until)) {
91 $consumer->enableUntil = strtotime(
$row->enable_until);
93 $consumer->lastAccess = null;
94 if (!is_null(
$row->last_access)) {
95 $consumer->lastAccess = strtotime(
$row->last_access);
97 $consumer->created = strtotime(
$row->created);
98 $consumer->updated = strtotime(
$row->updated);
103 mysql_free_result($rsConsumer);
120 $id = $consumer->getRecordId();
121 $key = $consumer->getKey();
123 if (
$key === $key256) {
126 $protected = ($consumer->protected) ? 1 : 0;
127 $enabled = ($consumer->enabled)? 1 : 0;
128 $profile = (!empty($consumer->profile)) ? json_encode($consumer->profile) : null;
129 $settingsValue = serialize($consumer->getSettings());
131 $now = date(
"{$this->dateFormat} {$this->timeFormat}", $time);
133 if (!is_null($consumer->enableFrom)) {
134 $from = date(
"{$this->dateFormat} {$this->timeFormat}", $consumer->enableFrom);
137 if (!is_null($consumer->enableUntil)) {
138 $until = date(
"{$this->dateFormat} {$this->timeFormat}", $consumer->enableUntil);
141 if (!is_null($consumer->lastAccess)) {
142 $last = date($this->dateFormat, $consumer->lastAccess);
146 'secret, lti_version, consumer_name, consumer_version, consumer_guid, profile, tool_proxy, settings, protected, enabled, ' .
147 'enable_from, enable_until, last_access, created, updated) ' .
148 'VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %d, %d, %s, %s, %s, %s, %s)',
157 'consumer_key256 = %s, consumer_key = %s, ' .
158 'name = %s, secret= %s, lti_version = %s, consumer_name = %s, consumer_version = %s, consumer_guid = %s, ' .
159 'profile = %s, tool_proxy = %s, settings = %s, ' .
160 'protected = %d, enabled = %d, enable_from = %s, enable_until = %s, last_access = %s, updated = %s ' .
161 'WHERE consumer_pk = %d',
167 $protected, $enabled,
171 $ok = mysql_query($sql);
174 $consumer->setRecordId(mysql_insert_id());
175 $consumer->created =
$time;
177 $consumer->updated =
$time;
196 $consumer->getRecordId());
200 $sql = sprintf(
'DELETE sk ' .
203 'WHERE rl.consumer_pk = %d',
204 $consumer->getRecordId());
208 $sql = sprintf(
'DELETE sk ' .
210 "INNER JOIN {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME .
' rl ON sk.resource_link_pk = rl.resource_link_pk ' .
212 'WHERE c.consumer_pk = %d',
213 $consumer->getRecordId());
217 $sql = sprintf(
'DELETE u ' .
219 "INNER JOIN {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME .
' rl ON u.resource_link_pk = rl.resource_link_pk ' .
220 'WHERE rl.consumer_pk = %d',
221 $consumer->getRecordId());
225 $sql = sprintf(
'DELETE u ' .
227 "INNER JOIN {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME .
' rl ON u.resource_link_pk = rl.resource_link_pk ' .
228 "INNER JOIN {$this->dbTableNamePrefix}" . DataConnector::CONTEXT_TABLE_NAME .
' c ON rl.context_pk = c.context_pk ' .
229 'WHERE c.consumer_pk = %d',
230 $consumer->getRecordId());
234 $sql = sprintf(
"UPDATE {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME .
' prl ' .
235 "INNER JOIN {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME .
' rl ON prl.primary_resource_link_pk = rl.resource_link_pk ' .
236 'SET prl.primary_resource_link_pk = NULL, prl.share_approved = NULL ' .
237 'WHERE rl.consumer_pk = %d',
238 $consumer->getRecordId());
239 $ok = mysql_query($sql);
242 $sql = sprintf(
"UPDATE {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME .
' prl ' .
243 "INNER JOIN {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME .
' rl ON prl.primary_resource_link_pk = rl.resource_link_pk ' .
244 "INNER JOIN {$this->dbTableNamePrefix}" . DataConnector::CONTEXT_TABLE_NAME .
' c ON rl.context_pk = c.context_pk ' .
245 'SET prl.primary_resource_link_pk = NULL, prl.share_approved = NULL ' .
246 'WHERE c.consumer_pk = %d',
247 $consumer->getRecordId());
248 $ok = mysql_query($sql);
251 $sql = sprintf(
'DELETE rl ' .
252 "FROM {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME .
' rl ' .
253 'WHERE rl.consumer_pk = %d',
254 $consumer->getRecordId());
258 $sql = sprintf(
'DELETE rl ' .
259 "FROM {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME .
' rl ' .
260 "INNER JOIN {$this->dbTableNamePrefix}" . DataConnector::CONTEXT_TABLE_NAME .
' c ON rl.context_pk = c.context_pk ' .
261 'WHERE c.consumer_pk = %d',
262 $consumer->getRecordId());
266 $sql = sprintf(
'DELETE c ' .
267 "FROM {$this->dbTableNamePrefix}" . DataConnector::CONTEXT_TABLE_NAME .
' c ' .
268 'WHERE c.consumer_pk = %d',
269 $consumer->getRecordId());
273 $sql = sprintf(
'DELETE c ' .
275 'WHERE c.consumer_pk = %d',
276 $consumer->getRecordId());
277 $ok = mysql_query($sql);
280 $consumer->initialize();
288 # Load all tool consumers from the database 293 $consumers = array();
295 $sql =
'SELECT consumer_pk, consumer_key, consumer_key, name, secret, lti_version, consumer_name, consumer_version, consumer_guid, ' .
296 'profile, tool_proxy, settings, ' .
297 'protected, enabled, enable_from, enable_until, last_access, created, updated ' .
300 $rsConsumers = mysql_query($sql);
302 while (
$row = mysql_fetch_object($rsConsumers)) {
304 $consumer->setRecordId(intval(
$row->consumer_pk));
305 $consumer->name =
$row->name;
306 $consumer->secret =
$row->secret;
307 $consumer->ltiVersion =
$row->lti_version;
308 $consumer->consumerName =
$row->consumer_name;
309 $consumer->consumerVersion =
$row->consumer_version;
310 $consumer->consumerGuid =
$row->consumer_guid;
311 $consumer->profile = json_decode(
$row->profile);
312 $consumer->toolProxy =
$row->tool_proxy;
313 $settings = unserialize(
$row->settings);
314 if (!is_array($settings)) {
317 $consumer->setSettings($settings);
318 $consumer->protected = (intval(
$row->protected) === 1);
319 $consumer->enabled = (intval(
$row->enabled) === 1);
320 $consumer->enableFrom = null;
321 if (!is_null(
$row->enable_from)) {
322 $consumer->enableFrom = strtotime(
$row->enable_from);
324 $consumer->enableUntil = null;
325 if (!is_null(
$row->enable_until)) {
326 $consumer->enableUntil = strtotime(
$row->enable_until);
328 $consumer->lastAccess = null;
329 if (!is_null(
$row->last_access)) {
330 $consumer->lastAccess = strtotime(
$row->last_access);
332 $consumer->created = strtotime(
$row->created);
333 $consumer->updated = strtotime(
$row->updated);
334 $consumers[] = $consumer;
336 mysql_free_result($rsConsumers);
344 ### ToolProxy methods 348 # Load the tool proxy from the database 358 # Save the tool proxy to the database 368 # Delete the tool proxy from the database 392 if (!empty(
$context->getRecordId())) {
393 $sql = sprintf(
'SELECT context_pk, consumer_pk, lti_context_id, settings, created, updated ' .
395 'WHERE (context_pk = %d)',
398 $sql = sprintf(
'SELECT context_pk, consumer_pk, lti_context_id, settings, created, updated ' .
400 'WHERE (consumer_pk = %d) AND (lti_context_id = %s)',
403 $rs_context = mysql_query($sql);
405 $row = mysql_fetch_object($rs_context);
410 $settings = unserialize(
$row->settings);
411 if (!is_array($settings)) {
436 $now = date(
"{$this->dateFormat} {$this->timeFormat}",
$time);
437 $settingsValue = serialize(
$context->getSettings());
439 $consumer_pk =
$context->getConsumer()->getRecordId();
442 'settings, created, updated) ' .
443 'VALUES (%d, %s, %s, %s, %s)',
449 'lti_context_id = %s, settings = %s, '.
451 'WHERE (consumer_pk = %d) AND (context_pk = %d)',
455 $ok = mysql_query($sql);
458 $context->setRecordId(mysql_insert_id());
479 $sql = sprintf(
'DELETE sk ' .
482 'WHERE rl.context_pk = %d',
487 $sql = sprintf(
'DELETE u ' .
489 "INNER JOIN {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME .
' rl ON u.resource_link_pk = rl.resource_link_pk ' .
490 'WHERE rl.context_pk = %d',
495 $sql = sprintf(
"UPDATE {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME .
' prl ' .
496 "INNER JOIN {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME .
' rl ON prl.primary_resource_link_pk = rl.resource_link_pk ' .
497 'SET prl.primary_resource_link_pk = null, prl.share_approved = null ' .
498 'WHERE rl.context_pk = %d',
500 $ok = mysql_query($sql);
503 $sql = sprintf(
'DELETE rl ' .
504 "FROM {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME .
' rl ' .
505 'WHERE rl.context_pk = %d',
510 $sql = sprintf(
'DELETE c ' .
512 'WHERE c.context_pk = %d',
514 $ok = mysql_query($sql);
524 ### ResourceLink methods 538 if (!empty($resourceLink->getRecordId())) {
539 $sql = sprintf(
'SELECT resource_link_pk, context_pk, consumer_pk, lti_resource_link_id, settings, primary_resource_link_pk, share_approved, created, updated ' .
541 'WHERE (resource_link_pk = %d)',
542 $resourceLink->getRecordId());
543 }
else if (!empty($resourceLink->getContext())) {
544 $sql = sprintf(
'SELECT resource_link_pk, context_pk, consumer_pk, lti_resource_link_id, settings, primary_resource_link_pk, share_approved, created, updated ' .
546 'WHERE (context_pk = %d) AND (lti_resource_link_id = %s)',
549 $sql = sprintf(
'SELECT r.resource_link_pk, r.context_pk, r.consumer_pk, r.lti_resource_link_id, r.settings, r.primary_resource_link_pk, r.share_approved, r.created, r.updated ' .
552 ' WHERE ((r.consumer_pk = %d) OR (c.consumer_pk = %d)) AND (lti_resource_link_id = %s)',
553 $resourceLink->getConsumer()->getRecordId(), $resourceLink->getConsumer()->getRecordId(),
DataConnector::quoted($resourceLink->getId()));
555 $rsContext = mysql_query($sql);
557 $row = mysql_fetch_object($rsContext);
559 $resourceLink->setRecordId(intval(
$row->resource_link_pk));
560 if (!is_null(
$row->context_pk)) {
561 $resourceLink->setContextId(intval(
$row->context_pk));
563 $resourceLink->setContextId(null);
565 if (!is_null(
$row->consumer_pk)) {
566 $resourceLink->setConsumerId(intval(
$row->consumer_pk));
568 $resourceLink->setConsumerId(null);
570 $resourceLink->ltiResourceLinkId =
$row->lti_resource_link_id;
571 $settings = unserialize(
$row->settings);
572 if (!is_array($settings)) {
575 $resourceLink->setSettings($settings);
576 if (!is_null(
$row->primary_resource_link_pk)) {
577 $resourceLink->primaryResourceLinkId = intval(
$row->primary_resource_link_pk);
579 $resourceLink->primaryResourceLinkId = null;
581 $resourceLink->shareApproved = (is_null(
$row->share_approved)) ? null : (intval(
$row->share_approved) === 1);
582 $resourceLink->created = strtotime(
$row->created);
583 $resourceLink->updated = strtotime(
$row->updated);
601 if (is_null($resourceLink->shareApproved)) {
603 }
else if ($resourceLink->shareApproved) {
608 if (empty($resourceLink->primaryResourceLinkId)) {
609 $primaryResourceLinkId =
'NULL';
611 $primaryResourceLinkId = strval($resourceLink->primaryResourceLinkId);
614 $now = date(
"{$this->dateFormat} {$this->timeFormat}",
$time);
615 $settingsValue = serialize($resourceLink->getSettings());
616 if (!empty($resourceLink->getContext())) {
617 $consumerId =
'NULL';
618 $contextId = strval($resourceLink->getContext()->getRecordId());
619 }
else if (!empty($resourceLink->getContextId())) {
620 $consumerId =
'NULL';
621 $contextId = strval($resourceLink->getContextId());
623 $consumerId = strval($resourceLink->getConsumer()->getRecordId());
626 $id = $resourceLink->getRecordId();
629 'lti_resource_link_id, settings, primary_resource_link_pk, share_approved, created, updated) ' .
630 'VALUES (%s, %s, %s, %s, %s, %s, %s, %s)',
634 }
else if ($contextId !==
'NULL') {
636 'consumer_pk = %s, lti_resource_link_id = %s, settings = %s, '.
637 'primary_resource_link_pk = %s, share_approved = %s, updated = %s ' .
638 'WHERE (context_pk = %s) AND (resource_link_pk = %d)',
644 'context_pk = %s, lti_resource_link_id = %s, settings = %s, '.
645 'primary_resource_link_pk = %s, share_approved = %s, updated = %s ' .
646 'WHERE (consumer_pk = %s) AND (resource_link_pk = %d)',
651 $ok = mysql_query($sql);
654 $resourceLink->setRecordId(mysql_insert_id());
655 $resourceLink->created =
$time;
657 $resourceLink->updated =
$time;
676 'WHERE (resource_link_pk = %d)',
677 $resourceLink->getRecordId());
678 $ok = mysql_query($sql);
683 'WHERE (resource_link_pk = %d)',
684 $resourceLink->getRecordId());
685 $ok = mysql_query($sql);
691 'SET primary_resource_link_pk = NULL ' .
692 'WHERE (primary_resource_link_pk = %d)',
693 $resourceLink->getRecordId());
694 $ok = mysql_query($sql);
700 'WHERE (resource_link_pk = %s)',
701 $resourceLink->getRecordId());
702 $ok = mysql_query($sql);
706 $resourceLink->initialize();
731 $sql = sprintf(
'SELECT u.user_pk, u.lti_result_sourcedid, u.lti_user_id, u.created, u.updated ' .
734 'ON u.resource_link_pk = rl.resource_link_pk ' .
735 "WHERE (rl.resource_link_pk = %d) AND (rl.primary_resource_link_pk IS NULL)",
736 $resourceLink->getRecordId());
738 $sql = sprintf(
'SELECT u.user_pk, u.lti_result_sourcedid, u.lti_user_id, u.created, u.updated ' .
741 'ON u.resource_link_pk = rl.resource_link_pk ' .
742 'WHERE ((rl.resource_link_pk = %d) AND (rl.primary_resource_link_pk IS NULL)) OR ' .
743 '((rl.primary_resource_link_pk = %d) AND (share_approved = 1))',
744 $resourceLink->getRecordId(), $resourceLink->getRecordId());
746 $rsUser = mysql_query($sql);
748 while (
$row = mysql_fetch_object($rsUser)) {
750 $user->setRecordId(intval(
$row->user_pk));
751 $user->ltiResultSourcedId =
$row->lti_result_sourcedid;
752 $user->created = strtotime(
$row->created);
753 $user->updated = strtotime(
$row->updated);
754 if (is_null($idScope)) {
778 $sql = sprintf(
'SELECT consumer_pk, resource_link_pk, share_approved ' .
780 'WHERE (primary_resource_link_pk = %d) ' .
781 'ORDER BY consumer_pk',
782 $resourceLink->getRecordId());
783 $rsShare = mysql_query($sql);
785 while (
$row = mysql_fetch_object($rsShare)) {
787 $share->resourceLinkId = intval(
$row->resource_link_pk);
788 $share->approved = (intval(
$row->share_approved) === 1);
799 ### ConsumerNonce methods 815 $now = date(
"{$this->dateFormat} {$this->timeFormat}", time());
822 $rs_nonce = mysql_query($sql);
824 $row = mysql_fetch_object($rs_nonce);
825 if (
$row ===
false) {
844 $expires = date(
"{$this->dateFormat} {$this->timeFormat}", $nonce->expires);
848 $ok = mysql_query($sql);
856 ### ResourceLinkShareKey methods 872 $now = date(
"{$this->dateFormat} {$this->timeFormat}", time());
877 $id = mysql_real_escape_string($shareKey->getId());
878 $sql =
'SELECT resource_link_pk, auto_approve, expires ' .
880 "WHERE share_key_id = '{$id}'";
881 $rsShareKey = mysql_query($sql);
883 $row = mysql_fetch_object($rsShareKey);
884 if (
$row && (intval(
$row->resource_link_pk) === $shareKey->resourceLinkId)) {
885 $shareKey->autoApprove = (intval(
$row->auto_approve) === 1);
886 $shareKey->expires = strtotime(
$row->expires);
905 if ($shareKey->autoApprove) {
910 $expires = date(
"{$this->dateFormat} {$this->timeFormat}", $shareKey->expires);
912 '(share_key_id, resource_link_pk, auto_approve, expires) ' .
913 "VALUES (%s, %d, {$approve}, '{$expires}')",
915 $ok = mysql_query($sql);
933 $ok = mysql_query($sql);
936 $shareKey->initialize();
959 if (!empty(
$user->getRecordId())) {
960 $sql = sprintf(
'SELECT user_pk, resource_link_pk, lti_user_id, lti_result_sourcedid, created, updated ' .
962 'WHERE (user_pk = %d)',
963 $user->getRecordId());
965 $sql = sprintf(
'SELECT user_pk, resource_link_pk, lti_user_id, lti_result_sourcedid, created, updated ' .
967 'WHERE (resource_link_pk = %d) AND (lti_user_id = %s)',
968 $user->getResourceLink()->getRecordId(),
971 $rsUser = mysql_query($sql);
973 $row = mysql_fetch_object($rsUser);
975 $user->setRecordId(intval(
$row->user_pk));
976 $user->setResourceLinkId(intval(
$row->resource_link_pk));
978 $user->ltiResultSourcedId =
$row->lti_result_sourcedid;
979 $user->created = strtotime(
$row->created);
980 $user->updated = strtotime(
$row->updated);
1000 $now = date(
"{$this->dateFormat} {$this->timeFormat}",
$time);
1001 if (is_null(
$user->created)) {
1003 'lti_user_id, lti_result_sourcedid, created, updated) ' .
1004 'VALUES (%d, %s, %s, %s, %s)',
1005 $user->getResourceLink()->getRecordId(),
1010 'SET lti_result_sourcedid = %s, updated = %s ' .
1011 'WHERE (user_pk = %d)',
1014 $user->getRecordId());
1016 $ok = mysql_query($sql);
1018 if (is_null(
$user->created)) {
1019 $user->setRecordId(mysql_insert_id());
1040 'WHERE (user_pk = %d)',
1041 $user->getRecordId());
1042 $ok = mysql_query($sql);
1045 $user->initialize();
if(!array_key_exists('StateId', $_REQUEST)) $id