public function getReadingHistory($patron, $page = 1, $recordsPerPage = -1, $sortOption = "dueDate") { require_once ROOT_DIR . '/sys/ReadingHistoryEntry.php'; require_once ROOT_DIR . '/services/MyResearch/lib/Resource.php'; //Reading History is stored within VuFind for each patron. global $user; $historyActive = $user->trackReadingHistory == 1; //Get a list of titles for the user. $titles = array(); $readingHistory = new ReadingHistoryEntry(); $readingHistorySql = "SELECT * FROM user_reading_history INNER JOIN resource ON user_reading_history.resourceId = resource.id where userId = {$user->id}"; if ($sortOption == "title") { $readingHistorySql .= " order by title_sort ASC, title ASC"; } elseif ($sortOption == "author") { $readingHistorySql .= " order by author ASC, title ASC"; } elseif ($sortOption == "checkedOut") { $readingHistorySql .= " order by firstCheckoutDate DESC, title ASC"; } elseif ($sortOption == "returned") { $readingHistorySql .= " order by lastCheckoutDate DESC, title ASC"; } elseif ($sortOption == "format") { $readingHistorySql .= " order by format DESC, title ASC"; } //Get count of reading history $readingHistoryCount = new ReadingHistoryEntry(); $readingHistoryCount->query($readingHistorySql); $numTitles = $readingHistoryCount->N; //Get individual titles to display if ($recordsPerPage > 0) { $startRecord = ($page - 1) * $recordsPerPage; $readingHistorySql .= " LIMIT {$startRecord}, {$recordsPerPage}"; } $readingHistory->query($readingHistorySql); if ($readingHistory->N > 0) { //Load additional details for each title global $configArray; // Setup Search Engine Connection $i = 0; $titles = array(); while ($readingHistory->fetch()) { $firstCheckoutDate = $readingHistory->firstCheckoutDate; $firstCheckoutTime = strtotime($firstCheckoutDate); $lastCheckoutDate = $readingHistory->lastCheckoutDate; $lastCheckoutTime = strtotime($lastCheckoutDate); $titles[] = array('recordId' => $readingHistory->record_id, 'source' => $readingHistory->source, 'checkout' => $firstCheckoutDate, 'checkoutTime' => $firstCheckoutTime, 'lastCheckout' => $lastCheckoutDate, 'lastCheckoutTime' => $lastCheckoutTime, 'title' => $readingHistory->title, 'title_sort' => $readingHistory->title_sort, 'author' => $readingHistory->author, 'format' => $readingHistory->format, 'format_category' => $readingHistory->format_category, 'isbn' => $readingHistory->isbn, 'upc' => $readingHistory->upc); } } return array('historyActive' => $historyActive, 'titles' => array_values($titles), 'numTitles' => $numTitles); }
private function updateReadingHistoryBasedOnCurrentCheckouts() { global $user; require_once ROOT_DIR . '/sys/ReadingHistoryEntry.php'; //Note, include deleted titles here so they are not added multiple times. $readingHistoryDB = new ReadingHistoryEntry(); $readingHistoryDB->userId = $user->id; $readingHistoryDB->whereAdd('checkInDate IS NULL'); $readingHistoryDB->find(); $activeHistoryTitles = array(); while ($readingHistoryDB->fetch()) { $historyEntry = $this->getHistoryEntryForDatabaseEntry($readingHistoryDB); $key = $historyEntry['source'] . ':' . $historyEntry['id']; $activeHistoryTitles[$key] = $historyEntry; } //Update reading history based on current checkouts. That way it never looks out of date require_once ROOT_DIR . '/services/API/UserAPI.php'; $userAPI = new UserAPI(); $checkouts = $userAPI->getPatronCheckedOutItems(); foreach ($checkouts['checkedOutItems'] as $checkout) { $sourceId = '?'; $source = $checkout['checkoutSource']; if ($source == 'OverDrive') { $sourceId = $checkout['overDriveId']; } elseif ($source == 'ILS') { $sourceId = $checkout['id']; } elseif ($source == 'eContent') { $source = $checkout['recordType']; $sourceId = $checkout['id']; } $key = $source . ':' . $sourceId; if (array_key_exists($key, $activeHistoryTitles)) { unset($activeHistoryTitles[$key]); } else { $historyEntryDB = new ReadingHistoryEntry(); $historyEntryDB->userId = $user->id; if (isset($checkout['groupedWorkId'])) { $historyEntryDB->groupedWorkPermanentId = $checkout['groupedWorkId'] == null ? '' : $checkout['groupedWorkId']; } else { $historyEntryDB->groupedWorkPermanentId = ""; } $historyEntryDB->source = $source; $historyEntryDB->sourceId = $sourceId; $historyEntryDB->title = substr($checkout['title'], 0, 150); $historyEntryDB->author = substr($checkout['author'], 0, 75); $historyEntryDB->format = substr($checkout['format'], 0, 50); $historyEntryDB->checkOutDate = time(); if (!$historyEntryDB->insert()) { global $logger; $logger->log("Could not insert new reading history entry", PEAR_LOG_ERR); } } } //Anything that was still active is now checked in foreach ($activeHistoryTitles as $historyEntry) { //Update even if deleted to make sure code is cleaned up correctly $historyEntryDB = new ReadingHistoryEntry(); $historyEntryDB->source = $historyEntry['source']; $historyEntryDB->sourceId = $historyEntry['id']; $historyEntryDB->checkInDate = null; if ($historyEntryDB->find(true)) { $historyEntryDB->checkInDate = time(); $numUpdates = $historyEntryDB->update(); if ($numUpdates != 1) { global $logger; $key = $historyEntry['source'] . ':' . $historyEntry['id']; $logger->log("Could not update reading history entry {$key}", PEAR_LOG_ERR); } } } }