Skip to content
Snippets Groups Projects
Commit 52e7d24f authored by Ian Hardy's avatar Ian Hardy Committed by Robert Lange
Browse files

FOLIO paged results (#1636)

parent 8c81cd74
No related merge requests found
...@@ -559,6 +559,48 @@ class Folio extends AbstractAPI implements ...@@ -559,6 +559,48 @@ class Folio extends AbstractAPI implements
return count($json->users) === 1 ? $json->users[0] : null; return count($json->users) === 1 ? $json->users[0] : null;
} }
/**
* Helper function to retrieve paged results from FOLIO API
*
* @param string $responseKey Key containing values to collect in response
* @param string $interface FOLIO api interface to call
* @param array $query CQL query
*
* @return array
*/
protected function getPagedResults($responseKey, $interface, $query = [])
{
$count = 0;
$limit = 1000;
$offset = 0;
do {
$combinedQuery = array_merge($query, compact('offset', 'limit'));
$response = $this->makeRequest(
'GET',
$interface,
$combinedQuery
);
$json = json_decode($response->getBody());
if (!$response->isSuccess() || !$json) {
$msg = $json->errors[0]->message ?? json_last_error_msg();
throw new ILSException($msg);
}
$total = $json->totalRecords ?? 0;
$previousCount = $count;
foreach ($json->$responseKey ?? [] as $item) {
$count++;
if ($count % $limit == 0) {
$offset += $limit;
}
yield $item ?? '';
}
// Continue until the count reaches the total records
// found, if count does not increase, something has gone
// wrong. Stop so we don't loop forever.
} while ($count < $total && $previousCount != $count);
}
/** /**
* Patron Login * Patron Login
* *
...@@ -697,13 +739,10 @@ class Folio extends AbstractAPI implements ...@@ -697,13 +739,10 @@ class Folio extends AbstractAPI implements
public function getMyTransactions($patron) public function getMyTransactions($patron)
{ {
$query = ['query' => 'userId==' . $patron['id'] . ' and status.name==Open']; $query = ['query' => 'userId==' . $patron['id'] . ' and status.name==Open'];
$response = $this->makeRequest("GET", '/circulation/loans', $query);
$json = json_decode($response->getBody());
if (count($json->loans) == 0) {
return [];
}
$transactions = []; $transactions = [];
foreach ($json->loans as $trans) { foreach ($this->getPagedResults(
'loans', '/circulation/loans', $query
) as $trans) {
$date = date_create($trans->dueDate); $date = date_create($trans->dueDate);
$transactions[] = [ $transactions[] = [
'duedate' => date_format($date, "j M Y"), 'duedate' => date_format($date, "j M Y"),
...@@ -797,10 +836,10 @@ class Folio extends AbstractAPI implements ...@@ -797,10 +836,10 @@ class Folio extends AbstractAPI implements
public function getPickupLocations($patron) public function getPickupLocations($patron)
{ {
$query = ['query' => 'pickupLocation=true']; $query = ['query' => 'pickupLocation=true'];
$response = $this->makeRequest('GET', '/service-points', $query);
$json = json_decode($response->getBody());
$locations = []; $locations = [];
foreach ($json->servicepoints as $servicepoint) { foreach ($this->getPagedResults(
'servicepoints', '/service-points', $query
) as $servicepoint) {
$locations[] = [ $locations[] = [
'locationID' => $servicepoint->id, 'locationID' => $servicepoint->id,
'locationDisplay' => $servicepoint->discoveryDisplayName 'locationDisplay' => $servicepoint->discoveryDisplayName
...@@ -855,10 +894,10 @@ class Folio extends AbstractAPI implements ...@@ -855,10 +894,10 @@ class Folio extends AbstractAPI implements
'query' => '(requesterId == "' . $patron['id'] . '" ' . 'query' => '(requesterId == "' . $patron['id'] . '" ' .
'and status == Open*)' 'and status == Open*)'
]; ];
$response = $this->makeRequest('GET', '/request-storage/requests', $query);
$json = json_decode($response->getBody());
$holds = []; $holds = [];
foreach ($json->requests as $hold) { foreach ($this->getPagedResults(
'requests', '/request-storage/requests', $query
) as $hold) {
$requestDate = date_create($hold->requestDate); $requestDate = date_create($hold->requestDate);
// Set expire date if it was included in the response // Set expire date if it was included in the response
$expireDate = isset($hold->requestExpirationDate) $expireDate = isset($hold->requestExpirationDate)
...@@ -1015,28 +1054,14 @@ class Folio extends AbstractAPI implements ...@@ -1015,28 +1054,14 @@ class Folio extends AbstractAPI implements
$valueKey = 'name' $valueKey = 'name'
) { ) {
$retVal = []; $retVal = [];
$limit = 1000; // how many records to retrieve at once
$offset = 0;
// Results can be paginated, so let's loop until we've gotten everything: // Results can be paginated, so let's loop until we've gotten everything:
do { foreach ($this->getPagedResults(
$response = $this->makeRequest( $responseKey ?? $type, '/coursereserves/' . $type
'GET', ) as $item) {
'/coursereserves/' . $type, $retVal[$item->id] = $item->$valueKey ?? '';
compact('offset', 'limit') }
);
$json = json_decode($response->getBody());
$total = $json->totalRecords ?? 0;
$preCount = count($retVal);
foreach ($json->{$responseKey ?? $type} ?? [] as $item) {
$retVal[$item->id] = $item->$valueKey ?? '';
}
$postCount = count($retVal);
$offset += $limit;
// Loop has a safety valve: if the count of records doesn't change
// in a full iteration, something has gone wrong, and we should stop
// so we don't loop forever!
} while ($total && $postCount < $total && $preCount != $postCount);
return $retVal; return $retVal;
} }
...@@ -1139,51 +1164,36 @@ class Folio extends AbstractAPI implements ...@@ -1139,51 +1164,36 @@ class Folio extends AbstractAPI implements
public function findReserves($course, $inst, $dept) public function findReserves($course, $inst, $dept)
{ {
$retVal = []; $retVal = [];
$limit = 1000; // how many records to retrieve at once
$offset = 0;
// Results can be paginated, so let's loop until we've gotten everything: // Results can be paginated, so let's loop until we've gotten everything:
do { foreach ($this->getPagedResults(
$response = $this->makeRequest( 'reserves', '/coursereserves/reserves'
'GET', ) as $item) {
'/coursereserves/reserves', try {
compact('offset', 'limit') $bibId = $this->getBibId(null, null, $item->itemId);
); } catch (\Exception $e) {
$json = json_decode($response->getBody()); $bibId = null;
$total = $json->totalRecords ?? 0; }
$preCount = count($retVal); if ($bibId !== null) {
foreach ($json->reserves ?? [] as $item) { $courseData = $this->getCourseDetails(
try { $item->courseListingId ?? null
$bibId = $this->getBibId(null, null, $item->itemId); );
} catch (\Exception $e) { $instructorIds = $this->getInstructorIds(
$bibId = null; $item->courseListingId ?? null
} );
if ($bibId !== null) { foreach ($courseData as $courseId => $departmentId) {
$courseData = $this->getCourseDetails( foreach ($instructorIds as $instructorId) {
$item->courseListingId ?? null $retVal[] = [
); 'BIB_ID' => $bibId,
$instructorIds = $this->getInstructorIds( 'COURSE_ID' => $courseId == '' ? null : $courseId,
$item->courseListingId ?? null 'DEPARTMENT_ID' => $departmentId == ''
); ? null : $departmentId,
foreach ($courseData as $courseId => $departmentId) { 'INSTRUCTOR_ID' => $instructorId,
foreach ($instructorIds as $instructorId) { ];
$retVal[] = [
'BIB_ID' => $bibId,
'COURSE_ID' => $courseId == '' ? null : $courseId,
'DEPARTMENT_ID' => $departmentId == ''
? null : $departmentId,
'INSTRUCTOR_ID' => $instructorId,
];
}
} }
} }
} }
$postCount = count($retVal); }
$offset += $limit;
// Loop has a safety valve: if the count of records doesn't change
// in a full iteration, something has gone wrong, and we should stop
// so we don't loop forever!
} while ($total && $postCount < $total && $preCount != $postCount);
// If the user has requested a filter, apply it now: // If the user has requested a filter, apply it now:
if (!empty($course) || !empty($inst) || !empty($dept)) { if (!empty($course) || !empty($inst) || !empty($dept)) {
...@@ -1255,14 +1265,11 @@ class Folio extends AbstractAPI implements ...@@ -1255,14 +1265,11 @@ class Folio extends AbstractAPI implements
*/ */
public function getMyFines($patron) public function getMyFines($patron)
{ {
$query = ['query' => 'userId==' . $patron['id'] . ' and status.name<>Closed']; $query = ['query' => 'userId==' . $patron['id'] . ' and status.name==Closed'];
$response = $this->makeRequest("GET", '/accounts', $query);
$json = json_decode($response->getBody());
if (count($json->accounts) == 0) {
return [];
}
$fines = []; $fines = [];
foreach ($json->accounts as $fine) { foreach ($this->getPagedResults(
'accounts', '/accounts', $query
) as $fine) {
$date = date_create($fine->metadata->createdDate); $date = date_create($fine->metadata->createdDate);
$title = (isset($fine->title) ? $fine->title : null); $title = (isset($fine->title) ? $fine->title : null);
$fines[] = [ $fines[] = [
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment