Skip to content

Commit

Permalink
Adjusted to calculate history properly
Browse files Browse the repository at this point in the history
  • Loading branch information
LuomaJuha committed Jan 16, 2025
1 parent 6931dbf commit 1d742b5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
33 changes: 32 additions & 1 deletion module/Finna/src/Finna/AjaxHandler/GetCheckoutHistory.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ public function __construct(
protected int $batchLimit = 1000,
protected int $defaultPageSize = 50
) {
if ($this->batchLimit < $defaultPageSize) {
$this->batchLimit = $defaultPageSize;
}
parent::__construct($ss, $ils, $ilsAuthenticator, $user);
}

Expand All @@ -98,7 +101,35 @@ public function handleRequest(Params $params)
if ($result['success'] === false) {
return $this->formatResponse($result['message'], $result['status']);
}
return $this->formatResponse(['parts' => ceil(($result['function_result']['count'] ?? 1) / $this->batchLimit)]);
$calculatedResults = $this->calculateLimitsFromResult($result);
return $this->formatResponse(['parts' => $calculatedResults['parts']]);
}

/**
* Calculate limits used to fetch data from the results obtained from getCheckoutHistoryResult.
*
* @param array $result Checkout history result
*
* @return array
*/
public function calculateLimitsFromResult(array $result): array
{
$resultCount = $result['function_result']['count'] ?? 1;
$paginationHelper = new PaginationHelper();
$paginator = $paginationHelper->getPaginator(
$result['pageOptions'],
$result['function_result']['count'],
$result['function_result']['transactions']
);
$pageLimit = $paginator ? $paginator->getItemCountPerPage() : $this->defaultPageSize;
$parts = $pageLimit > $this->batchLimit
? floor($resultCount / $this->batchLimit)
: ceil($resultCount / $this->batchLimit);
return [
'pageLimit' => $paginator ? $paginator->getItemCountPerPage() : $this->defaultPageSize,
'pageCount' => $paginator ? $paginator->count() : 1,
'parts' => $parts,
];
}

/**
Expand Down
22 changes: 11 additions & 11 deletions module/Finna/src/Finna/AjaxHandler/GetCheckoutHistoryFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
use PhpOffice\PhpSpreadsheet\Writer\Csv;
use PhpOffice\PhpSpreadsheet\Writer\Ods;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use VuFind\ILS\PaginationHelper;

/**
* GetCheckoutHistoryFile AJAX handler
Expand Down Expand Up @@ -92,18 +91,16 @@ public function handleRequest(Params $params)
return $this->formatResponse($result['message'], $result['status']);
}
try {
$paginationHelper = new PaginationHelper();
$paginator = $paginationHelper->getPaginator(
$result['pageOptions'],
$result['function_result']['count'],
$result['function_result']['transactions']
);
// Get requested history part as a file to be downloaded
$part = $params->fromQuery('part', 1);
$fileFormat = $params->fromQuery('format', 'csv');
$pageLimit = $paginator ? $paginator->getItemCountPerPage() : $this->defaultPageSize;
$pagesCount = $paginator ? $paginator->count() : 1;
return $this->getHistoryAsFile($part, $pageLimit, $pagesCount, $fileFormat);
$calculatedResults = $this->calculateLimitsFromResult($result);
return $this->getHistoryAsFile(
$part,
$calculatedResults['pageLimit'],
$calculatedResults['pageCount'],
$fileFormat
);
} catch (Exception $e) {
return $this->formatResponse(
$this->translate('An error has occurred'),
Expand Down Expand Up @@ -213,7 +210,10 @@ private function getHistoryAsFile(
}
$writer = new $this->exportFormats[$fileFormat]['writer']($spreadsheet);
$writer->save($tmp);
$fileName = implode('-', ['finna-loan-history-pages', $firstPageToFetch, $lastPageToFetch]);
$fileName = 'finna-loan-history-parts-' . $firstPageToFetch;
if ($firstPageToFetch !== $lastPageToFetch) {
$fileName .= '-' . $lastPageToFetch;
}
$fileName .= ".$fileFormat";

rewind($tmp);
Expand Down

0 comments on commit 1d742b5

Please sign in to comment.