Skip to content

Commit

Permalink
DIS-179: Reading history improvements for v25.03.00.
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoStoyanovByWater committed Feb 6, 2025
1 parent c786812 commit 1675a3c
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 49 deletions.
68 changes: 34 additions & 34 deletions code/web/CatalogConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -646,25 +646,26 @@ function getReadingHistory($patron, $page = 1, $recordsPerPage = 20, $sortOption
//if ($title['permanentId'] != null) {
$userReadingHistoryEntry = new ReadingHistoryEntry();
$userReadingHistoryEntry->userId = $patron->id;
$userReadingHistoryEntry->groupedWorkPermanentId = $title['permanentId'];
$userReadingHistoryEntry->source = $this->accountProfile->recordSource;
$userReadingHistoryEntry->sourceId = $title['recordId'];
$userReadingHistoryEntry->title = substr($title['title'], 0, 150);
$userReadingHistoryEntry->author = substr($title['author'], 0, 75);
$userReadingHistoryEntry->format = $title['format'];
$userReadingHistoryEntry->checkOutDate = $title['checkout'];
if (!empty($title['checkin'])) {
$userReadingHistoryEntry->checkInDate = $title['checkin'];
} else {
$userReadingHistoryEntry->checkInDate = null;
}
if (empty($title['isIll'])) {
$userReadingHistoryEntry->isIll = 0;
$userReadingHistoryEntry->source = $this->accountProfile->recordSource;
if ($userReadingHistoryEntry->find(true)) {
// Update existing entry
$userReadingHistoryEntry->checkOutDate = $title['checkout'];
$userReadingHistoryEntry->checkInDate = $title['checkin'] ?? null;
$userReadingHistoryEntry->isIll = $title['isIll'] ?? 0;
$userReadingHistoryEntry->deleted = 0;
$userReadingHistoryEntry->update();
} else {
$userReadingHistoryEntry->isIll = 1;
// Insert new entry
$userReadingHistoryEntry->groupedWorkPermanentId = $title['permanentId'];
$userReadingHistoryEntry->title = mb_substr($title['title'], 0, 150);
$userReadingHistoryEntry->author = mb_substr($title['author'], 0, 75);
$userReadingHistoryEntry->format = $title['format'];
$userReadingHistoryEntry->checkOutDate = $title['checkout'];
$userReadingHistoryEntry->checkInDate = $title['checkin'] ?? null;
$userReadingHistoryEntry->isIll = $title['isIll'] ?? 0;
$userReadingHistoryEntry->insert();
}
$userReadingHistoryEntry->deleted = 0;
$userReadingHistoryEntry->insert();
$userReadingHistoryEntry = null;
//}
}
Expand Down Expand Up @@ -1204,25 +1205,24 @@ public function updateReadingHistoryBasedOnCurrentCheckouts($patron, $isNightlyU
//TODO: This should check to see if the grouped work has been checked out rather than the record
$historyEntryDB = new ReadingHistoryEntry();
$historyEntryDB->userId = $patron->id;
if (!empty($checkout->groupedWorkId)) {
$historyEntryDB->groupedWorkPermanentId = $checkout->groupedWorkId;
$historyEntryDB->sourceId = $checkout->sourceId;
$existingEntry = $historyEntryDB->find(true); // Now only checks userId+sourceId
if ($existingEntry) {
$historyEntryDB->update();
} else {
$historyEntryDB->groupedWorkPermanentId = "";
}

$historyEntryDB->source = $source;
$historyEntryDB->sourceId = $sourceId;
$historyEntryDB->title = StringUtils::trimStringToLengthAtWordBoundary($checkout->title, 150, true);
$historyEntryDB->author = isset($checkout->author) ? StringUtils::trimStringToLengthAtWordBoundary($checkout->author, 75, true) : "";
$historyEntryDB->format = substr($checkout->format, 0, 50);
$historyEntryDB->checkOutDate = time();
$historyEntryDB->costSavings = $checkout->getReplacementCost();
if (!$historyEntryDB->insert()) {
global $logger;
$logger->log("Could not insert new reading history entry", Logger::LOG_ERROR);
} else {
if ($patron->enableCostSavings) {
$patron->__set('totalCostSavings', $patron->totalCostSavings + $historyEntryDB->costSavings);
$historyEntryDB->title = StringUtils::trimStringToLengthAtWordBoundary($checkout->title, 150, true);
$historyEntryDB->author = isset($checkout->author) ? StringUtils::trimStringToLengthAtWordBoundary($checkout->author, 75, true) : "";
$historyEntryDB->format = mb_substr($checkout->format, 0, 50);
$historyEntryDB->checkOutDate = time();
$historyEntryDB->costSavings = $checkout->getReplacementCost();
if (!$historyEntryDB->insert()) {
global $logger;
$logger->log("Could not insert new reading history entry", Logger::LOG_ERROR);
} else {
if ($patron->enableCostSavings) {
$patron->__set('totalCostSavings', $patron->totalCostSavings + $historyEntryDB->costSavings);
}
}
}
}
Expand Down Expand Up @@ -1964,4 +1964,4 @@ public function submitLocalIllRequest(User $patron, LocalIllForm $localIllForm):
public function hasIlsConsentSupport(): bool {
return $this->driver->hasIlsConsentSupport();
}
}
}
4 changes: 4 additions & 0 deletions code/web/release_notes/25.03.00.MD
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
// nick

// leo
### Reading History Updates
- Added a SQL query to delete old duplicates and to prevent future duplicates by creating a unique index. (DIS-179) (*LS*)
- Modified `CatalogConnection.php` to prevent duplicates during checkout-based history updates. (DIS-179) (*LS*)
- Updated `AJAX.php` to check `(userId, sourceId, source)` instead of `groupedWorkPermanentId` when adding manual entries. (DIS-179) (*LS*)

// imani

Expand Down
27 changes: 12 additions & 15 deletions code/web/services/MyAccount/AJAX.php
Original file line number Diff line number Diff line change
Expand Up @@ -7434,25 +7434,22 @@ function saveToReadingHistory(): array {
$groupedWork->permanent_id = $sourceId;
if ($groupedWork->find(true)) {
$groupedWorkDriver = new GroupedWorkDriver($sourceId);
$readingHistoryEntry->title = mb_substr($groupedWorkDriver->getTitle(), 0, 150);
$readingHistoryEntry->author = mb_substr($groupedWorkDriver->getPrimaryAuthor(), 0, 75);
//Leave the format blank
$readingHistoryEntry->format = '';
$checkoutDate = mktime(0, 0, 0, $month, 1, $year);
$readingHistoryEntry->checkOutDate = $checkoutDate;
$readingHistoryEntry->checkInDate = $checkoutDate;
$readingHistoryEntry->isIll = 0;
$readingHistoryEntry->isManuallyAdded = 1;
//No cost savings updates since this is outside the library
if ($readingHistoryEntry->find(true)) {
$existingEntry = true;
} else {
$existingEntry = false;
}
if ($existingEntry) {
$readingHistoryEntry->deleted = 0;
// Update existing entry
$readingHistoryEntry->checkOutDate = $checkoutDate;
$readingHistoryEntry->checkInDate = $checkoutDate;
$readingHistoryEntry->update();
} else {
// Insert new entry
$readingHistoryEntry->groupedWorkPermanentId = $sourceId;
$readingHistoryEntry->title = mb_substr($groupedWorkDriver->getTitle(), 0, 150);
$readingHistoryEntry->author = mb_substr($groupedWorkDriver->getPrimaryAuthor(), 0, 75);
$readingHistoryEntry->format = '';
$readingHistoryEntry->checkOutDate = $checkoutDate;
$readingHistoryEntry->checkInDate = $checkoutDate;
$readingHistoryEntry->isIll = 0;
$readingHistoryEntry->isManuallyAdded = 1;
$readingHistoryEntry->insert();
}

Expand Down
23 changes: 23 additions & 0 deletions code/web/sys/DBMaintenance/version_updates/25.03.00.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,29 @@ function getUpdates25_03_00(): array {
//kirstien - Grove

// Leo Stoyanov - BWS
'deduplicate_reading_history' => [
'title' => 'De-Duplicate Reading History & Add Unique Key',
'description' => 'Combine multiple rows per (userId, sourceId, source) into one row before adding unique key.',
'continueOnError' => true,
'sql' => [
// 1. Keep the latest entry of the duplicated titles.
"DELETE t1 FROM user_reading_history_work t1
INNER JOIN user_reading_history_work t2
WHERE
t1.id < t2.id AND
t1.userId = t2.userId AND
t1.sourceId = t2.sourceId AND
t1.source = t2.source;",

// 2. Remove existing index if present (usually if an Admin is re-running a DB maintenance).
"ALTER TABLE user_reading_history_work
DROP INDEX IF EXISTS user_source;",

// 3. Create the new unique index
"ALTER TABLE user_reading_history_work
ADD UNIQUE INDEX user_source (userId, sourceId, source);"
],
], //deduplicate_reading_history

//alexander - PTFS-Europe

Expand Down

0 comments on commit 1675a3c

Please sign in to comment.