Skip to content

Commit

Permalink
Bug fixes
Browse files Browse the repository at this point in the history
Improve saving of resource links when context ID is not always passed in messages.
Fix processing of share keys.
  • Loading branch information
spvickers committed Jun 11, 2020
1 parent 75cf02c commit 4985077
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 109 deletions.
20 changes: 13 additions & 7 deletions src/DataConnector/DataConnector_mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -485,10 +485,13 @@ public function loadResourceLink($resourceLink)
"FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' ' .
'WHERE (resource_link_pk = %d)', $resourceLink->getRecordId());
} elseif (!is_null($resourceLink->getContext())) {
$sql = sprintf('SELECT resource_link_pk, context_pk, consumer_pk, title, lti_resource_link_id, settings, primary_resource_link_pk, share_approved, created, updated ' .
"FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' ' .
'WHERE (context_pk = %d) AND (lti_resource_link_id = %s)', $resourceLink->getContext()->getRecordId(),
$this->escape($resourceLink->getId()));
$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 ' .
"FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' r ' .
'WHERE (r.lti_resource_link_id = %s) AND ((r.context_pk = %d) OR (r.consumer_pk IN (' .
'SELECT c.consumer_pk ' .
"FROM {$this->dbTableNamePrefix}" . static::CONTEXT_TABLE_NAME . ' c ' .
'WHERE (c.context_pk = %d))))', $this->escape($resourceLink->getId()), $resourceLink->getContext()->getRecordId(),
$resourceLink->getContext()->getRecordId());
} else {
$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 ' .
"FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' r LEFT OUTER JOIN ' .
Expand Down Expand Up @@ -587,9 +590,9 @@ public function saveResourceLink($resourceLink)
$this->escape($now), $contextId, $id);
} else {
$sql = sprintf("UPDATE {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' SET ' .
'context_pk = %s, title = %s, lti_resource_link_id = %s, settings = %s, ' .
'context_pk = NULL, title = %s, lti_resource_link_id = %s, settings = %s, ' .
'primary_resource_link_pk = %s, share_approved = %s, updated = %s ' .
'WHERE (consumer_pk = %s) AND (resource_link_pk = %d)', $contextId, $this->escape($resourceLink->title),
'WHERE (consumer_pk = %s) AND (resource_link_pk = %d)', $this->escape($resourceLink->title),
$this->escape($resourceLink->getId()), $this->escape($settingsValue), $primaryResourceLinkId, $approved,
$this->escape($now), $consumerId, $id);
}
Expand Down Expand Up @@ -720,7 +723,9 @@ public function getSharesResourceLink($resourceLink)
if ($rsShare) {
while ($row = mysql_fetch_object($rsShare)) {
$share = new LTI\ResourceLinkShare();
$share->consumer_name = $row->consumer_name;
$share->resourceLinkId = intval($row->resource_link_pk);
$share->title = $row->title;
$share->approved = (intval($row->share_approved) === 1);
$shares[] = $share;
}
Expand Down Expand Up @@ -808,7 +813,8 @@ public function loadResourceLinkShareKey($shareKey)
$rsShareKey = $this->executeQuery($sql);
if ($rsShareKey) {
$row = mysql_fetch_object($rsShareKey);
if ($row && (intval($row->resource_link_pk) === $shareKey->resourceLinkId)) {
if ($row) {
$shareKey->resourceLinkId = intval($row->resource_link_pk);
$shareKey->autoApprove = (intval($row->auto_approve) === 1);
$shareKey->expires = strtotime($row->expires);
$ok = true;
Expand Down
18 changes: 11 additions & 7 deletions src/DataConnector/DataConnector_mysqli.php
Original file line number Diff line number Diff line change
Expand Up @@ -485,10 +485,13 @@ public function loadResourceLink($resourceLink)
"FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' ' .
'WHERE (resource_link_pk = %d)', $resourceLink->getRecordId());
} elseif (!is_null($resourceLink->getContext())) {
$sql = sprintf('SELECT resource_link_pk, context_pk, consumer_pk, title, lti_resource_link_id, settings, primary_resource_link_pk, share_approved, created, updated ' .
"FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' ' .
'WHERE (context_pk = %d) AND (lti_resource_link_id = %s)', $resourceLink->getContext()->getRecordId(),
$this->escape($resourceLink->getId()));
$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 ' .
"FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' r ' .
'WHERE (r.lti_resource_link_id = %s) AND ((r.context_pk = %d) OR (r.consumer_pk IN (' .
'SELECT c.consumer_pk ' .
"FROM {$this->dbTableNamePrefix}" . static::CONTEXT_TABLE_NAME . ' c ' .
'WHERE (c.context_pk = %d))))', $this->escape($resourceLink->getId()), $resourceLink->getContext()->getRecordId(),
$resourceLink->getContext()->getRecordId());
} else {
$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 ' .
"FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' r LEFT OUTER JOIN ' .
Expand Down Expand Up @@ -587,9 +590,9 @@ public function saveResourceLink($resourceLink)
$this->escape($now), $contextId, $id);
} else {
$sql = sprintf("UPDATE {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' SET ' .
'context_pk = %s, title = %s, lti_resource_link_id = %s, settings = %s, ' .
'context_pk = NULL, title = %s, lti_resource_link_id = %s, settings = %s, ' .
'primary_resource_link_pk = %s, share_approved = %s, updated = %s ' .
'WHERE (consumer_pk = %s) AND (resource_link_pk = %d)', $contextId, $this->escape($resourceLink->title),
'WHERE (consumer_pk = %s) AND (resource_link_pk = %d)', $this->escape($resourceLink->title),
$this->escape($resourceLink->getId()), $this->escape($settingsValue), $primaryResourceLinkId, $approved,
$this->escape($now), $consumerId, $id);
}
Expand Down Expand Up @@ -809,7 +812,8 @@ public function loadResourceLinkShareKey($shareKey)
$rsShareKey = $this->executeQuery($sql);
if ($rsShareKey) {
$row = mysqli_fetch_object($rsShareKey);
if ($row && (intval($row->resource_link_pk) === $shareKey->resourceLinkId)) {
if ($row) {
$shareKey->resourceLinkId = intval($row->resource_link_pk);
$shareKey->autoApprove = (intval($row->auto_approve) === 1);
$shareKey->expires = strtotime($row->expires);
$ok = true;
Expand Down
48 changes: 30 additions & 18 deletions src/DataConnector/DataConnector_oci.php
Original file line number Diff line number Diff line change
Expand Up @@ -600,14 +600,19 @@ public function loadResourceLink($resourceLink)
$id = $resourceLink->getRecordId();
oci_bind_by_name($query, 'id', $id);
} elseif (!is_null($resourceLink->getContext())) {
$sql = 'SELECT resource_link_pk, context_pk, consumer_pk, lti_resource_link_id, settings, primary_resource_link_pk, share_approved, created, updated ' .
"FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' ' .
'WHERE (context_pk = :id) AND (lti_resource_link_id = :rlid)';
$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 ' .
"FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' r ' .
'WHERE (r.lti_resource_link_id = :rlid) AND ((r.context_pk = :id1) OR (r.consumer_pk IN (' .
'SELECT c.consumer_pk ' .
"FROM {$this->dbTableNamePrefix}" . static::CONTEXT_TABLE_NAME . ' c ' .
'WHERE (c.context_pk = :id2)' .
')))';
$query = oci_parse($this->db, $sql);
$id = $resourceLink->getContext()->getRecordId();
oci_bind_by_name($query, 'id', $id);
$rlid = $resourceLink->getId();
oci_bind_by_name($query, 'rlid', $rlid);
$id = $resourceLink->getContext()->getRecordId();
oci_bind_by_name($query, 'id1', $id);
oci_bind_by_name($query, 'id2', $id);
} else {
$sql = '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 ' .
"FROM {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' r LEFT OUTER JOIN ' .
Expand Down Expand Up @@ -677,17 +682,24 @@ public function loadResourceLink($resourceLink)
*/
public function saveResourceLink($resourceLink)
{
if (is_null($resourceLink->shareApproved)) {
$approved = null;
} elseif ($resourceLink->shareApproved) {
$approved = 1;
} else {
$approved = 0;
}
$time = time();
$now = date("{$this->dateFormat} {$this->timeFormat}", $time);
$settingsValue = json_encode($resourceLink->getSettings());
if (!is_null($resourceLink->getContext())) {
$consumerId = null;
$contextId = strval($resourceLink->getContext()->getRecordId());
$contextId = $resourceLink->getContext()->getRecordId();
} elseif (!is_null($resourceLink->getContextId())) {
$consumerId = null;
$contextId = strval($resourceLink->getContextId());
$contextId = $resourceLink->getContextId();
} else {
$consumerId = strval($resourceLink->getConsumer()->getRecordId());
$consumerId = $resourceLink->getConsumer()->getRecordId();
$contextId = null;
}
if (empty($resourceLink->primaryResourceLinkId)) {
Expand All @@ -707,7 +719,7 @@ public function saveResourceLink($resourceLink)
oci_bind_by_name($query, 'rlid', $rlid);
oci_bind_by_name($query, 'settings', $settingsValue);
oci_bind_by_name($query, 'prlid', $primaryResourceLinkId);
oci_bind_by_name($query, 'share_approved', $resourceLink->shareApproved);
oci_bind_by_name($query, 'share_approved', $approved);
oci_bind_by_name($query, 'created', $now);
oci_bind_by_name($query, 'updated', $now);
oci_bind_by_name($query, 'pk', $pk);
Expand All @@ -722,21 +734,20 @@ public function saveResourceLink($resourceLink)
oci_bind_by_name($query, 'rlid', $rlid);
oci_bind_by_name($query, 'settings', $settingsValue);
oci_bind_by_name($query, 'prlid', $primaryResourceLinkId);
oci_bind_by_name($query, 'share_approved', $resourceLink->shareApproved);
oci_bind_by_name($query, 'share_approved', $approved);
oci_bind_by_name($query, 'updated', $now);
oci_bind_by_name($query, 'id', $id);
} else {
$sql = "UPDATE {$this->dbTableNamePrefix}" . static::RESOURCE_LINK_TABLE_NAME . ' SET ' .
'context_pk = :ctx, lti_resource_link_id = :rlid, settings = :settings, ' .
'context_pk = NULL, lti_resource_link_id = :rlid, settings = :settings, ' .
'primary_resource_link_pk = :prlid, share_approved = :share_approved, updated = :updated ' .
'WHERE (consumer_pk = :cid) AND (resource_link_pk = :id)';
$query = oci_parse($this->db, $sql);
oci_bind_by_name($query, 'ctx', $contextId);
$rlid = $resourceLink->getId();
oci_bind_by_name($query, 'rlid', $rlid);
oci_bind_by_name($query, 'settings', $settingsValue);
oci_bind_by_name($query, 'prlid', $primaryResourceLinkId);
oci_bind_by_name($query, 'share_approved', $resourceLink->shareApproved);
oci_bind_by_name($query, 'share_approved', $approved);
oci_bind_by_name($query, 'updated', $now);
oci_bind_by_name($query, 'cid', $consumerId);
oci_bind_by_name($query, 'id', $id);
Expand Down Expand Up @@ -892,7 +903,9 @@ public function getSharesResourceLink($resourceLink)
while ($row = oci_fetch_assoc($query)) {
$row = array_change_key_case($row);
$share = new LTI\ResourceLinkShare();
$share->consumer_name = $row['consumer_name'];
$share->resourceLinkId = intval($row['resource_link_pk']);
$share->title = $row['title'];
$share->approved = (intval($row['share_approved']) === 1);
$shares[] = $share;
}
Expand Down Expand Up @@ -994,11 +1007,10 @@ public function loadResourceLinkShareKey($shareKey)
$row = oci_fetch_assoc($query);
if ($row !== false) {
$row = array_change_key_case($row);
if (intval($row['resource_link_pk']) === $shareKey->resourceLinkId) {
$shareKey->autoApprove = ($row['auto_approve'] === 1);
$shareKey->expires = strtotime($row['expires']);
$ok = true;
}
$shareKey->resourceLinkId = intval($row['resource_link_pk']);
$shareKey->autoApprove = (intval($row['auto_approve']) === 1);
$shareKey->expires = strtotime($row['expires']);
$ok = true;
}
}

Expand Down
Loading

0 comments on commit 4985077

Please sign in to comment.