public function getMyTransactions($page = 1, $recordsPerPage = -1, $sortOption = 'dueDate') { global $configArray; global $user; $userId = $user->id; $checkedOutTitles = array(); //Get the session token for the user if (isset(HorizonAPI::$sessionIdsForUsers[$userId])) { $sessionToken = HorizonAPI::$sessionIdsForUsers[$userId]; } else { //Log the user in list($userValid, $sessionToken) = $this->loginViaWebService($user->cat_username, $user->cat_password); if (!$userValid) { echo "No session id found for user"; return $checkedOutTitles; } } //Now that we have the session token, get checkouts information $lookupMyAccountInfoResponse = $this->getWebServiceResponse($configArray['Catalog']['webServiceUrl'] . '/standard/lookupMyAccountInfo?clientID=' . $configArray['Catalog']['clientId'] . '&sessionToken=' . $sessionToken . '&includeItemsOutInfo=true'); if (isset($lookupMyAccountInfoResponse->ItemsOutInfo)) { $sCount = 0; foreach ($lookupMyAccountInfoResponse->ItemsOutInfo as $itemOut) { $sCount++; $bibId = (string) $itemOut->titleKey; $curTitle['checkoutSource'] = 'ILS'; $curTitle['recordId'] = $bibId; $curTitle['shortId'] = $bibId; $curTitle['id'] = $bibId; $curTitle['title'] = (string) $itemOut->title; $curTitle['author'] = (string) $itemOut->author; $curTitle['duedate'] = (string) $itemOut->dueDate; $curTitle['checkoutdate'] = (string) $itemOut->ckoDate; $dueTime = strtotime($curTitle['duedate']); $daysUntilDue = ceil(($dueTime - time()) / (24 * 60 * 60)); $overdue = $daysUntilDue < 0; $curTitle['overdue'] = $overdue; $curTitle['daysUntilDue'] = $daysUntilDue; $curTitle['renewCount'] = (string) $itemOut->renewals; $curTitle['canrenew'] = true; //TODO: Figure out if the user can renew the title or not $curTitle['renewIndicator'] = (string) $itemOut->itemBarcode; $curTitle['barcode'] = (string) $itemOut->itemBarcode; $curTitle['holdQueueLength'] = $this->getNumHolds($bibId); $curTitle['format'] = 'Unknown'; if ($curTitle['id'] && strlen($curTitle['id']) > 0) { require_once ROOT_DIR . '/RecordDrivers/MarcRecord.php'; $recordDriver = new MarcRecord($curTitle['id']); if ($recordDriver->isValid()) { $curTitle['coverUrl'] = $recordDriver->getBookcoverUrl('medium'); $curTitle['groupedWorkId'] = $recordDriver->getGroupedWorkId(); $formats = $recordDriver->getFormats(); $curTitle['format'] = reset($formats); } else { $curTitle['coverUrl'] = ""; } } $sortTitle = isset($curTitle['title_sort']) ? $curTitle['title_sort'] : $curTitle['title']; $sortKey = $sortTitle; if ($sortOption == 'title') { $sortKey = $sortTitle; } elseif ($sortOption == 'author') { $sortKey = (isset($curTitle['author']) ? $curTitle['author'] : "Unknown") . '-' . $sortTitle; } elseif ($sortOption == 'dueDate') { if (isset($curTitle['duedate'])) { if (preg_match('/.*?(\\d{1,2})[-\\/](\\d{1,2})[-\\/](\\d{2,4}).*/', $curTitle['duedate'], $matches)) { $sortKey = $matches[3] . '-' . $matches[1] . '-' . $matches[2] . '-' . $sortTitle; } else { $sortKey = $curTitle['duedate'] . '-' . $sortTitle; } } } elseif ($sortOption == 'format') { $sortKey = (isset($curTitle['format']) ? $curTitle['format'] : "Unknown") . '-' . $sortTitle; } elseif ($sortOption == 'renewed') { $sortKey = (isset($curTitle['renewCount']) ? $curTitle['renewCount'] : 0) . '-' . $sortTitle; } elseif ($sortOption == 'holdQueueLength') { $sortKey = (isset($curTitle['holdQueueLength']) ? $curTitle['holdQueueLength'] : 0) . '-' . $sortTitle; } $sortKey .= "_{$sCount}"; $checkedOutTitles[$sortKey] = $curTitle; } } return array('transactions' => $checkedOutTitles, 'numTransactions' => count($checkedOutTitles)); }
/** * Get Reading History * * This is responsible for retrieving a history of checked out items for the patron. * * @param array $patron The patron array * @param int $page * @param int $recordsPerPage * @param string $sortOption * * @return array Array of the patron's reading list * If an error occurs, return a PEAR_Error * @access public */ public function getReadingHistory($patron = null, $page = 1, $recordsPerPage = -1, $sortOption = "checkedOut") { global $user; $this->initDatabaseConnection(); //Figure out if the user is opted in to reading history $sql = "select disable_reading_history from borrowers where borrowernumber = {$user->username}"; $historyEnabledRS = mysqli_query($this->dbConnection, $sql); if ($historyEnabledRS) { $historyEnabledRow = $historyEnabledRS->fetch_assoc(); $historyEnabled = !$historyEnabledRow['disable_reading_history']; if (!$historyEnabled) { return array('historyActive' => false, 'titles' => array(), 'numTitles' => 0); } else { $historyActive = true; $readingHistoryTitles = array(); //Borrowed from C4:Members.pm $readingHistoryTitleSql = "SELECT *,issues.renewals AS renewals,items.renewals AS totalrenewals,items.timestamp AS itemstimestamp\n\t\t\t\t\tFROM issues\n\t\t\t\t\tLEFT JOIN items on items.itemnumber=issues.itemnumber\n\t\t\t\t\tLEFT JOIN biblio ON items.biblionumber=biblio.biblionumber\n\t\t\t\t\tLEFT JOIN biblioitems ON items.biblioitemnumber=biblioitems.biblioitemnumber\n\t\t\t\t\tWHERE borrowernumber=?\n\t\t\t\t\tUNION ALL\n\t\t\t\t\tSELECT *,old_issues.renewals AS renewals,items.renewals AS totalrenewals,items.timestamp AS itemstimestamp\n\t\t\t\t\tFROM old_issues\n\t\t\t\t\tLEFT JOIN items on items.itemnumber=old_issues.itemnumber\n\t\t\t\t\tLEFT JOIN biblio ON items.biblionumber=biblio.biblionumber\n\t\t\t\t\tLEFT JOIN biblioitems ON items.biblioitemnumber=biblioitems.biblioitemnumber\n\t\t\t\t\tWHERE borrowernumber=?"; $readingHistoryTitleStmt = mysqli_prepare($this->dbConnection, $readingHistoryTitleSql); $readingHistoryTitleStmt->bind_param('ii', $user->username, $user->username); if ($readingHistoryTitleStmt->execute()) { $readingHistoryTitleRS = $readingHistoryTitleStmt->get_result(); while ($readingHistoryTitleRow = $readingHistoryTitleRS->fetch_assoc()) { $curTitle = array(); $curTitle['id'] = $readingHistoryTitleRow['biblionumber']; $curTitle['shortId'] = $readingHistoryTitleRow['biblionumber']; $curTitle['recordId'] = $readingHistoryTitleRow['biblionumber']; $curTitle['title'] = $readingHistoryTitleRow['title']; $curTitle['checkout'] = $readingHistoryTitleRow['itemstimestamp']; $readingHistoryTitles[] = $curTitle; } } } $numTitles = count($readingHistoryTitles); //process pagination if ($recordsPerPage != -1) { $startRecord = ($page - 1) * $recordsPerPage; $readingHistoryTitles = array_slice($readingHistoryTitles, $startRecord, $recordsPerPage); } set_time_limit(20 * count($readingHistoryTitles)); foreach ($readingHistoryTitles as $key => $historyEntry) { //Get additional information from resources table $historyEntry['ratingData'] = null; $historyEntry['permanentId'] = null; $historyEntry['linkUrl'] = null; $historyEntry['coverUrl'] = null; $historyEntry['format'] = array(); if (isset($historyEntry['recordId']) && strlen($historyEntry['recordId']) > 0) { require_once ROOT_DIR . '/RecordDrivers/MarcRecord.php'; $recordDriver = new MarcRecord($historyEntry['recordId']); if ($recordDriver->isValid()) { $historyEntry['ratingData'] = $recordDriver->getRatingData(); $historyEntry['permanentId'] = $recordDriver->getPermanentId(); $historyEntry['linkUrl'] = $recordDriver->getLinkUrl(); $historyEntry['coverUrl'] = $recordDriver->getBookcoverUrl('medium'); $historyEntry['format'] = $recordDriver->getFormats(); $historyEntry['author'] = $recordDriver->getPrimaryAuthor(); } $recordDriver = null; } $readingHistoryTitles[$key] = $historyEntry; } return array('historyActive' => $historyActive, 'titles' => $readingHistoryTitles, 'numTitles' => $numTitles); } return array('historyActive' => false, 'titles' => array(), 'numTitles' => 0); }
public function getReadingHistory($patron, $page = 1, $recordsPerPage = -1, $sortOption = "checkedOut") { global $timer; $patronDump = $this->driver->_getPatronDump($this->driver->_getBarcode()); //Load the information from millennium using CURL $pageContents = $this->driver->_fetchPatronInfoPage($patronDump, 'readinghistory'); //Check to see if there are multiple pages of reading history $hasPagination = preg_match('/<td[^>]*class="browsePager"/', $pageContents); if ($hasPagination) { //Load a list of extra pages to load. The pagination links display multiple times, so load into an associative array to make them unique preg_match_all('/<a href="readinghistory&page=(\\d+)">/', $pageContents, $additionalPageMatches); $maxPageNum = 0; foreach ($additionalPageMatches[1] as $additionalPageMatch) { if ($additionalPageMatch > $maxPageNum) { $maxPageNum = $additionalPageMatch; } } } $recordsRead = 0; $readingHistoryTitles = $this->parseReadingHistoryPage($pageContents, $patron, $sortOption, $recordsRead); $recordsRead += count($readingHistoryTitles); if (isset($maxPageNum)) { for ($pageNum = 2; $pageNum <= $maxPageNum; $pageNum++) { $pageContents = $this->driver->_fetchPatronInfoPage($patronDump, 'readinghistory&page=' . $pageNum); $additionalTitles = $this->parseReadingHistoryPage($pageContents, $patron, $sortOption, $recordsRead); $recordsRead += count($additionalTitles); $readingHistoryTitles = array_merge($readingHistoryTitles, $additionalTitles); } } if ($sortOption == "checkedOut" || $sortOption == "returned") { krsort($readingHistoryTitles); } else { ksort($readingHistoryTitles); } $numTitles = count($readingHistoryTitles); //process pagination if ($recordsPerPage != -1) { $startRecord = ($page - 1) * $recordsPerPage; $readingHistoryTitles = array_slice($readingHistoryTitles, $startRecord, $recordsPerPage); } set_time_limit(20 * count($readingHistoryTitles)); foreach ($readingHistoryTitles as $key => $historyEntry) { //Get additional information from resources table $historyEntry['ratingData'] = null; $historyEntry['permanentId'] = null; $historyEntry['linkUrl'] = null; $historyEntry['coverUrl'] = null; $historyEntry['format'] = array(); if (isset($historyEntry['shortId']) && strlen($historyEntry['shortId']) > 0) { $historyEntry['recordId'] = "." . $historyEntry['shortId'] . $this->driver->getCheckDigit($historyEntry['shortId']); require_once ROOT_DIR . '/RecordDrivers/MarcRecord.php'; $recordDriver = new MarcRecord($historyEntry['recordId']); if ($recordDriver->isValid()) { $historyEntry['ratingData'] = $recordDriver->getRatingData(); $historyEntry['permanentId'] = $recordDriver->getPermanentId(); $historyEntry['linkUrl'] = $recordDriver->getLinkUrl(); $historyEntry['coverUrl'] = $recordDriver->getBookcoverUrl('medium'); $historyEntry['format'] = $recordDriver->getFormats(); } $recordDriver = null; } $readingHistoryTitles[$key] = $historyEntry; } //The history is active if there is an opt out link. $historyActive = strpos($pageContents, 'OptOut') > 0; $timer->logTime("Loaded Reading history for patron"); global $user; if ($historyActive && !$user->trackReadingHistory) { //The user does have reading history even though we hadn't detected it before. $user->trackReadingHistory = true; $user->update(); $_SESSION['userinfo'] = serialize($user); } if (!$historyActive && $user->trackReadingHistory) { //The user does have reading history even though we hadn't detected it before. $user->trackReadingHistory = false; $user->update(); $_SESSION['userinfo'] = serialize($user); } return array('historyActive' => $historyActive, 'titles' => $readingHistoryTitles, 'numTitles' => $numTitles); }
public function getMyTransactions($page = 1, $recordsPerPage = -1, $sortOption = 'dueDate') { global $timer; $patronDump = $this->driver->_getPatronDump($this->driver->_getBarcode()); $timer->logTime("Ready to load checked out titles from Millennium"); //Load the information from millennium using CURL $sResult = $this->driver->_fetchPatronInfoPage($patronDump, 'items'); $timer->logTime("Loaded checked out titles from Millennium"); $sResult = preg_replace("/<[^<]+?>\\W<[^<]+?>\\W\\d* ITEM.? CHECKED OUT<[^<]+?>\\W<[^<]+?>/i", "", $sResult); $s = substr($sResult, stripos($sResult, 'patFunc')); $s = substr($s, strpos($s, ">") + 1); $s = substr($s, 0, stripos($s, "</table")); $s = preg_replace("/<br \\/>/", "", $s); $sRows = preg_split("/<tr([^>]*)>/", $s); $sCount = 0; $sKeys = array_pad(array(), 10, ""); $checkedOutTitles = array(); //Get patron's location to determine if renewals are allowed. global $locationSingleton; /** @var Location $patronLocation */ $patronLocation = $locationSingleton->getUserHomeLocation(); if (isset($patronLocation)) { $patronPType = $this->driver->getPType(); $patronCanRenew = false; if ($patronLocation->ptypesToAllowRenewals == '*') { $patronCanRenew = true; } else { if (preg_match("/^({$patronLocation->ptypesToAllowRenewals})\$/", $patronPType)) { $patronCanRenew = true; } } } else { $patronCanRenew = true; } $timer->logTime("Determined if patron can renew"); foreach ($sRows as $srow) { $scols = preg_split("/<t(h|d)([^>]*)>/", $srow); $curTitle = array(); for ($i = 0; $i < sizeof($scols); $i++) { $scols[$i] = str_replace(" ", " ", $scols[$i]); $scols[$i] = preg_replace("/<br+?>/", " ", $scols[$i]); $scols[$i] = html_entity_decode(trim(substr($scols[$i], 0, stripos($scols[$i], "</t")))); //print_r($scols[$i]); if ($sCount == 1) { $sKeys[$i] = $scols[$i]; } else { if ($sCount > 1) { if (stripos($sKeys[$i], "TITLE") > -1) { if (preg_match('/.*?<a href=\\"\\/record=(.*?)(?:~S\\d{1,2})\\">(.*?)<\\/a>.*/', $scols[$i], $matches)) { //Standard Millennium WebPAC $shortId = $matches[1]; $bibid = '.' . $matches[1]; //Technically, this isn't correct since the check digit is missing $title = strip_tags($matches[2]); } elseif (preg_match('/.*<a href=".*?\\/record\\/C__R(.*?)\\?.*?">(.*?)<\\/a>.*/si', $scols[$i], $matches)) { //Encore $shortId = $matches[1]; $bibid = '.' . $matches[1]; //Technically, this isn't correct since the check digit is missing $title = strip_tags($matches[2]); } else { $title = strip_tags($scols[$i]); $shortId = ''; $bibid = ''; } $curTitle['checkoutSource'] = 'ILS'; $curTitle['shortId'] = $shortId; $curTitle['id'] = $bibid; $curTitle['title'] = utf8_encode($title); } if (stripos($sKeys[$i], "STATUS") > -1) { // $sret[$scount-2]['duedate'] = strip_tags($scols[$i]); $due = trim(str_replace("DUE", "", strip_tags($scols[$i]))); $renewCount = 0; if (preg_match('/FINE\\(up to now\\) (\\$\\d+\\.\\d+)/i', $due, $matches)) { $curTitle['fine'] = trim($matches[1]); } if (preg_match('/(.*)Renewed (\\d+) time(?:s)?/i', $due, $matches)) { $due = trim($matches[1]); $renewCount = $matches[2]; } else { if (preg_match('/(.*)\\+\\d+ HOLD.*/i', $due, $matches)) { $due = trim($matches[1]); } } if (preg_match('/(\\d{2}-\\d{2}-\\d{2})/', $due, $dueMatches)) { $dateDue = DateTime::createFromFormat('m-d-y', $dueMatches[1]); if ($dateDue) { $dueTime = $dateDue->getTimestamp(); } else { $dueTime = null; } } else { $dueTime = strtotime($due); } if ($dueTime != null) { $daysUntilDue = ceil(($dueTime - time()) / (24 * 60 * 60)); $overdue = $daysUntilDue < 0; $curTitle['duedate'] = $dueTime; $curTitle['overdue'] = $overdue; $curTitle['daysUntilDue'] = $daysUntilDue; } $curTitle['renewCount'] = $renewCount; } if (stripos($sKeys[$i], "BARCODE") > -1) { $curTitle['barcode'] = strip_tags($scols[$i]); } if (stripos($sKeys[$i], "RENEW") > -1) { $matches = array(); if (preg_match('/<input\\s*type="checkbox"\\s*name="renew(\\d+)"\\s*id="renew(\\d+)"\\s*value="(.*?)"\\s*\\/>/', $scols[$i], $matches)) { $curTitle['canrenew'] = $patronCanRenew; $curTitle['itemindex'] = $matches[1]; $curTitle['itemid'] = $matches[3]; $curTitle['renewIndicator'] = $curTitle['itemid'] . '|' . $curTitle['itemindex']; $curTitle['renewMessage'] = ''; } else { $curTitle['canrenew'] = false; } } if (stripos($sKeys[$i], "CALL NUMBER") > -1) { $curTitle['request'] = "null"; } } } } if ($sCount > 1) { //Get additional information from resources table if ($curTitle['shortId'] && strlen($curTitle['shortId']) > 0) { $checkDigit = $this->driver->getCheckDigit($curTitle['shortId']); $curTitle['recordId'] = '.' . $curTitle['shortId'] . $checkDigit; $curTitle['id'] = '.' . $curTitle['shortId'] . $checkDigit; require_once ROOT_DIR . '/RecordDrivers/MarcRecord.php'; $recordDriver = new MarcRecord($curTitle['recordId']); if ($recordDriver->isValid()) { $curTitle['coverUrl'] = $recordDriver->getBookcoverUrl('medium'); $curTitle['groupedWorkId'] = $recordDriver->getGroupedWorkId(); $formats = $recordDriver->getFormats(); $curTitle['format'] = reset($formats); $curTitle['author'] = $recordDriver->getPrimaryAuthor(); if (!isset($curTitle['title']) || empty($curTitle['title'])) { $curTitle['title'] = $recordDriver->getTitle(); } } else { $curTitle['coverUrl'] = ""; $curTitle['groupedWorkId'] = ""; $curTitle['format'] = "Unknown"; $curTitle['author'] = ""; } } $sortTitle = isset($curTitle['title_sort']) ? $curTitle['title_sort'] : $curTitle['title']; $sortKey = $sortTitle; if ($sortOption == 'title') { $sortKey = $sortTitle; } elseif ($sortOption == 'author') { $sortKey = (isset($curTitle['author']) ? $curTitle['author'] : "Unknown") . '-' . $sortTitle; } elseif ($sortOption == 'dueDate') { if (isset($curTitle['duedate'])) { if (preg_match('/.*?(\\d{1,2})[-\\/](\\d{1,2})[-\\/](\\d{2,4}).*/', $curTitle['duedate'], $matches)) { $sortKey = $matches[3] . '-' . $matches[1] . '-' . $matches[2] . '-' . $sortTitle; } else { $sortKey = $curTitle['duedate'] . '-' . $sortTitle; } } } elseif ($sortOption == 'format') { $sortKey = (isset($curTitle['format']) ? $curTitle['format'] : "Unknown") . '-' . $sortTitle; } elseif ($sortOption == 'renewed') { $sortKey = (isset($curTitle['renewCount']) ? $curTitle['renewCount'] : 0) . '-' . $sortTitle; } elseif ($sortOption == 'holdQueueLength') { $sortKey = (isset($curTitle['holdQueueLength']) ? $curTitle['holdQueueLength'] : 0) . '-' . $sortTitle; } $sortKey .= "_{$sCount}"; $checkedOutTitles[utf8_encode($sortKey)] = $curTitle; } $sCount++; } ksort($checkedOutTitles); $timer->logTime("Parsed checkout information"); $numTransactions = count($checkedOutTitles); //Process pagination if ($recordsPerPage != -1) { $startRecord = ($page - 1) * $recordsPerPage; if ($startRecord > $numTransactions) { $startRecord = 0; } $checkedOutTitles = array_slice($checkedOutTitles, $startRecord, $recordsPerPage); } return array('transactions' => $checkedOutTitles, 'numTransactions' => $numTransactions); }