-
Notifications
You must be signed in to change notification settings - Fork 393
Add vufind_sort option to [Loans] in Folio.ini #5109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
8a8ae3f
6092db7
0d1b0ae
a7db085
3c4749b
b45c4d2
70ebcc1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -40,6 +40,7 @@ | |||||
| use VuFindHttp\HttpServiceAwareInterface as HttpServiceAwareInterface; | ||||||
|
|
||||||
| use function array_key_exists; | ||||||
| use function array_slice; | ||||||
| use function count; | ||||||
| use function in_array; | ||||||
| use function is_callable; | ||||||
|
|
@@ -1144,19 +1145,32 @@ protected function formatHoldingItem( | |||||
| * @return array | ||||||
| */ | ||||||
| protected function sortHoldings(array $holdings, string $sortField): array | ||||||
| { | ||||||
| return $this->sortArray($holdings, $sortField); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Given an array and a sort field, sort the array. | ||||||
| * | ||||||
| * @param array $data Array to sort | ||||||
| * @param string $sortField Sort field | ||||||
| * | ||||||
| * @return array | ||||||
| */ | ||||||
| protected function sortArray(array $data, string $sortField): array | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we do this anywhere else? If so, I wonder if a trait would make sense. But I didn't check, so maybe that's a premature suggestion. :-)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There isn't anywhere else in the code that currently does a
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree, no sense in overengineering this prematurely. I just thought there might be another similar function somewhere else. Thanks for confirming that there is not! :-) |
||||||
| { | ||||||
| usort( | ||||||
| $holdings, | ||||||
| $data, | ||||||
| function ($a, $b) use ($sortField) { | ||||||
| return strnatcasecmp($a[$sortField], $b[$sortField]); | ||||||
| } | ||||||
| ); | ||||||
| // Renumber the re-sorted batch: | ||||||
| $nbCount = count($holdings); | ||||||
| $nbCount = count($data); | ||||||
| for ($nbIndex = 0; $nbIndex < $nbCount; $nbIndex++) { | ||||||
| $holdings[$nbIndex]['number'] = $nbIndex + 1; | ||||||
| $data[$nbIndex]['number'] = $nbIndex + 1; | ||||||
| } | ||||||
| return $holdings; | ||||||
| return $data; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
|
|
@@ -1829,53 +1843,90 @@ public function getMyTransactions($patron, $params = []) | |||||
| $limit = $params['limit'] ?? 1000; | ||||||
| $offset = isset($params['page']) ? ($params['page'] - 1) * $limit : 0; | ||||||
|
|
||||||
| $requestedSort = $params['sort'] ?? ''; | ||||||
| $vufindSortMap = $this->config['Loans']['vufind_sort'] ?? []; | ||||||
| $localSortField = $vufindSortMap[$requestedSort] ?? null; | ||||||
|
|
||||||
| $query = 'userId==' . $patron['id'] . ' and status.name==Open'; | ||||||
| if (isset($params['sort'])) { | ||||||
| $query .= ' sortby ' . $this->escapeCql($params['sort']); | ||||||
| } | ||||||
| $resultPage = $this->getResultPage('/circulation/loans', compact('query'), $offset, $limit); | ||||||
| $transactions = []; | ||||||
| foreach ($resultPage->loans ?? [] as $trans) { | ||||||
| $dueStatus = false; | ||||||
| $date = $this->getDateTimeFromString($trans->dueDate); | ||||||
| $dueDateTimestamp = $date->getTimestamp(); | ||||||
|
|
||||||
| $now = time(); | ||||||
| if ($now > $dueDateTimestamp) { | ||||||
| $dueStatus = 'overdue'; | ||||||
| } elseif ($now > $dueDateTimestamp - (1 * 24 * 60 * 60)) { | ||||||
| $dueStatus = 'due'; | ||||||
| $count = null; | ||||||
|
|
||||||
| // Fetch data from FOLIO, only pass sort to API if it is NOT handled locally by VuFind | ||||||
| if ($localSortField) { | ||||||
| $rawItems = $this->getPagedResults('loans', '/circulation/loans', compact('query')); | ||||||
| } else { | ||||||
| if (!empty($requestedSort)) { | ||||||
| $query .= ' sortby ' . $this->escapeCql($requestedSort); | ||||||
| } | ||||||
| $transactions[] = [ | ||||||
| 'duedate' => | ||||||
| $this->dateConverter->convertToDisplayDate( | ||||||
| 'U', | ||||||
| $dueDateTimestamp | ||||||
| ), | ||||||
| 'dueTime' => | ||||||
| $this->dateConverter->convertToDisplayTime( | ||||||
| 'U', | ||||||
| $dueDateTimestamp | ||||||
| ), | ||||||
| 'dueStatus' => $dueStatus, | ||||||
| 'id' => $this->getBibId($trans->item->instanceId), | ||||||
| 'item_id' => $trans->item->id, | ||||||
| 'barcode' => $trans->item->barcode, | ||||||
| 'renew' => $trans->renewalCount ?? 0, | ||||||
| 'renewable' => true, | ||||||
| 'title' => $trans->item->title, | ||||||
| ]; | ||||||
| $result = $this->getResultPage('/circulation/loans', compact('query'), $offset, $limit); | ||||||
| $rawItems = $result->loans ?? []; | ||||||
| } | ||||||
| // If we have a full page or have applied an offset, we need to look up the total count of transactions: | ||||||
|
|
||||||
| // Format the transaction results | ||||||
| foreach ($rawItems as $trans) { | ||||||
| $transactions[] = $this->formatTransactionItem($trans); | ||||||
| } | ||||||
|
|
||||||
| // If we have a full page, have applied an offset, or are not using a vufind_sort option, | ||||||
| // we need to look up the total count of transactions: | ||||||
| $count = count($transactions); | ||||||
| if ($offset > 0 || $count >= $limit) { | ||||||
| if (!$localSortField && ($offset > 0 || $count >= $limit)) { | ||||||
| // We could use the count in the result page, but that may be an estimate; | ||||||
| // safer to do a separate lookup to be sure we have the right number! | ||||||
| $count = $this->getResultCount('/circulation/loans', compact('query')); | ||||||
| } | ||||||
|
|
||||||
| // Apply local sorted if requested | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| if ($localSortField) { | ||||||
| $transactions = $this->sortArray($transactions, $localSortField); | ||||||
| $transactions = array_slice($transactions, $offset, $limit); | ||||||
| } | ||||||
|
|
||||||
| return ['count' => $count, 'records' => $transactions]; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Support method for getMyTransactions to parse a single | ||||||
| * transaction result. | ||||||
| * | ||||||
| * @param array $transaction An single transaction | ||||||
| * array from getMyTransactions | ||||||
| * | ||||||
| * @return string The FOLIO loan ID for this loan | ||||||
| */ | ||||||
| protected function formatTransactionItem($transaction) | ||||||
| { | ||||||
| $dueStatus = false; | ||||||
| $date = $this->getDateTimeFromString($transaction->dueDate); | ||||||
| $dueDateTimestamp = $date->getTimestamp(); | ||||||
|
|
||||||
| $now = time(); | ||||||
| if ($now > $dueDateTimestamp) { | ||||||
| $dueStatus = 'overdue'; | ||||||
| } elseif ($now > $dueDateTimestamp - (1 * 24 * 60 * 60)) { | ||||||
| $dueStatus = 'due'; | ||||||
| } | ||||||
| return [ | ||||||
| 'duedate' => | ||||||
| $this->dateConverter->convertToDisplayDate( | ||||||
| 'U', | ||||||
| $dueDateTimestamp | ||||||
| ), | ||||||
| 'dueTime' => | ||||||
| $this->dateConverter->convertToDisplayTime( | ||||||
| 'U', | ||||||
| $dueDateTimestamp | ||||||
| ), | ||||||
| 'dueStatus' => $dueStatus, | ||||||
| 'id' => $this->getBibId($transaction->item->instanceId), | ||||||
| 'item_id' => $transaction->item->id, | ||||||
| 'barcode' => $transaction->item->barcode, | ||||||
| 'renew' => $transaction->renewalCount ?? 0, | ||||||
| 'renewable' => true, | ||||||
| 'title' => $transaction->item->title, | ||||||
| ]; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Get FOLIO loan IDs for use in renewMyItems. | ||||||
| * | ||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.