public function addRecordToReadingHistory($eContentRecord, $user) { //Get the resource for the record require_once ROOT_DIR . '/services/MyResearch/lib/Resource.php'; $resource = new Resource(); $resource->record_id = $eContentRecord->id; $resource->source = 'eContent'; if (!$resource->find(true)) { $resource->title = $eContentRecord->title; $resource->author = $eContentRecord->author; $resource->format = 'EMedia'; $resource->format_category = $eContentRecord->format_category(); $ret = $resource->insert(); } //Check to see if there is an existing entry require_once ROOT_DIR . '/sys/ReadingHistoryEntry.php'; $readingHistoryEntry = new ReadingHistoryEntry(); $readingHistoryEntry->userId = $user->id; $readingHistoryEntry->resourceId = $resource->id; if (!$readingHistoryEntry->find(true)) { $readingHistoryEntry->firstCheckoutDate = date('Y-m-d'); $readingHistoryEntry->lastCheckoutDate = date('Y-m-d'); $readingHistoryEntry->daysCheckedOut = 1; $ret = $readingHistoryEntry->insert(); } else { $readingHistoryEntry->lastCheckoutDate = date('Y-m-d'); $ret = $readingHistoryEntry->update(); } }
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); } } } }