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);
 }
Ejemplo n.º 2
0
 /**
  * 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);
 }