/**
  * 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
  */
 function getReadingHistory($patron, $page = 1, $recordsPerPage = -1, $sortOption = "checkedOut")
 {
     //Get reading history from the database unless we specifically want to load from the driver.
     global $user;
     if ($user->trackReadingHistory && $user->initialReadingHistoryLoaded || !$this->driver->hasNativeReadingHistory()) {
         if ($user->trackReadingHistory) {
             //Make sure initial reading history loaded is set to true if we are here since
             //The only way it wouldn't be here is if the user has elected to start tracking reading history
             //And they don't have reading history currently specified.  We get what is checked out below though
             //So that takes care of the initial load
             if (!$user->initialReadingHistoryLoaded) {
                 //Load the initial reading history
                 $user->initialReadingHistoryLoaded = 1;
                 $user->update();
                 $_SESSION['userinfo'] = serialize($user);
             }
             $this->updateReadingHistoryBasedOnCurrentCheckouts();
             require_once ROOT_DIR . '/sys/ReadingHistoryEntry.php';
             $readingHistoryDB = new ReadingHistoryEntry();
             $readingHistoryDB->userId = $user->id;
             $readingHistoryDB->deleted = 0;
             //Only show titles that have not been deleted
             if ($sortOption == "checkedOut") {
                 $readingHistoryDB->orderBy('checkOutDate DESC, title ASC');
             } else {
                 if ($sortOption == "returned") {
                     $readingHistoryDB->orderBy('checkInDate DESC, title ASC');
                 } else {
                     if ($sortOption == "title") {
                         $readingHistoryDB->orderBy('title ASC, checkOutDate DESC');
                     } else {
                         if ($sortOption == "author") {
                             $readingHistoryDB->orderBy('author ASC, title ASC, checkOutDate DESC');
                         } else {
                             if ($sortOption == "format") {
                                 $readingHistoryDB->orderBy('format ASC, title ASC, checkOutDate DESC');
                             }
                         }
                     }
                 }
             }
             if ($recordsPerPage != -1) {
                 $readingHistoryDB->limit(($page - 1) * $recordsPerPage, $recordsPerPage);
             }
             $readingHistoryDB->find();
             $readingHistoryTitles = array();
             while ($readingHistoryDB->fetch()) {
                 $historyEntry = $this->getHistoryEntryForDatabaseEntry($readingHistoryDB);
                 $readingHistoryTitles[] = $historyEntry;
             }
             $readingHistoryDB = new ReadingHistoryEntry();
             $readingHistoryDB->userId = $user->id;
             $readingHistoryDB->deleted = 0;
             $numTitles = $readingHistoryDB->count();
             return array('historyActive' => $user->trackReadingHistory, 'titles' => $readingHistoryTitles, 'numTitles' => $numTitles);
         } else {
             //Reading history disabled
             return array('historyActive' => $user->trackReadingHistory, 'titles' => array(), 'numTitles' => 0);
         }
     } else {
         //Don't know enough to load internally, check the ILS.
         return $this->driver->getReadingHistory($patron, $page, $recordsPerPage, $sortOption);
     }
 }