Skip to content

Commit 30ace61

Browse files
committed
Add item paging support to search availability.
1 parent df1cedc commit 30ace61

File tree

7 files changed

+71
-13
lines changed

7 files changed

+71
-13
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
66

77
Anything marked with [**BC**] is known to affect backward compatibility with previous versions.
88

9+
## [23.11.07] - 2024-08-07
10+
11+
### Added
12+
13+
- Added support for paging items in biblio search availability (limit and offset query parameters). Also the total item count (items_total) and checked item count (items_checked) is now returned.
14+
915
## [23.11.06] - 2024-06-20
1016

1117
### Added

Koha/Plugin/Fi/KohaSuomi/DI/AvailabilityController.pm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ sub biblio_search {
152152
};
153153
$params->{'include_found_in_hold_queue'} = 1 if $include_found_in_hold_queue;
154154
$params->{'include_suspended_in_hold_queue'} = 1 if $include_suspended_in_hold_queue;
155+
$params->{'limit'} = $c->validation->param('limit');
156+
$params->{'offset'} = $c->validation->param('offset');
155157
$availability = Koha::Plugin::Fi::KohaSuomi::DI::Koha::Availability::Search->biblio($params);
156158
return $c->render(status => 200, openapi => $availability->in_opac->to_api);
157159
}

Koha/Plugin/Fi/KohaSuomi/DI/Koha/Biblio/Availability.pm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,12 @@ sub to_api {
183183
if (defined $self->{'hold_queue_length'}) {
184184
$hash->{'hold_queue_length'} = 0+$self->{'hold_queue_length'};
185185
}
186+
if (defined $self->{'items_total'}) {
187+
$hash->{'items_total'} = 0+$self->{'items_total'};
188+
}
189+
if (defined $self->{'items_checked'}) {
190+
$hash->{'items_checked'} = 0+$self->{'items_checked'};
191+
}
186192
return $hash;
187193
}
188194

Koha/Plugin/Fi/KohaSuomi/DI/Koha/Biblio/Availability/Search.pm

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ sub new {
6666

6767
my $self = $class->SUPER::new($params);
6868

69+
# Check only first n items.
70+
$self->{'limit'} = $params->{'limit'};
71+
72+
# Check items starting at offset of n.
73+
$self->{'offset'} = $params->{'offset'};
74+
6975
return $self;
7076
}
7177

@@ -95,36 +101,40 @@ sub _item_looper {
95101

96102
if (scalar(@items) == 0) {
97103
$self->unavailable(Koha::Plugin::Fi::KohaSuomi::DI::Koha::Exceptions::Biblio::NoAvailableItems->new);
104+
$self->{'items_total'} = 0;
98105
return;
99106
}
100107

108+
# Sort items for paging
109+
sort { $a->itemnumber <=> $b->itemnumber } @items;
110+
101111
my $opachiddenitems_rules = C4::Context->yaml_preference('OpacHiddenItems');
102112

103-
# Stop calculating item availabilities after $limit available items are found.
104-
# E.g. parameter 'limit' with value 1 will find only one available item and
105-
# return biblio as available if no other unavailabilities are found. If you
106-
# want to calculate availability of every item in this biblio, do not give this
107-
# parameter.
108-
my $limit = $params->{'limit'};
109113
my $avoid_queries_after = $params->{'MaxSearchResultsItemsPerRecordStatusCheck'}
110114
? C4::Context->preference('MaxSearchResultsItemsPerRecordStatusCheck') : undef;
111115
my $count = 0;
116+
my $checked = 0;
112117

113118
$self->{'hold_queue_length'} = $self->get_hold_queue_length();
114119

115120
foreach my $item (@items) {
116-
# Break out of loop after $limit items are found available
117-
if (defined $limit && @{$self->{'item_availabilities'}} >= $limit) {
118-
last;
119-
}
120-
121121
next if ($item->hidden_in_opac({ rules => $opachiddenitems_rules }));
122122

123+
$count++;
124+
# Skip this item if we haven't reached the offset yet
125+
next if (defined $self->offset && $count <= $self->offset);
126+
127+
# Skip this item if we have reached the limit. We still need to
128+
# loop all items for total count of items visible in opac.
129+
next if (defined $self->limit && $checked >= $self->limit);
130+
131+
$checked++;
132+
123133
my $item_availability = Koha::Plugin::Fi::KohaSuomi::DI::Koha::Item::Availability::Search->new({
124134
item => $item,
125135
});
126136
if ($params->{'MaxSearchResultsItemsPerRecordStatusCheck'} &&
127-
$count >= $avoid_queries_after) {
137+
$count > $avoid_queries_after) {
128138
# A couple heuristics to limit how many times
129139
# we query the database for item transfer information, sacrificing
130140
# accuracy in some cases for speed;
@@ -144,7 +154,6 @@ sub _item_looper {
144154
} else {
145155
push @{$self->{'item_unavailabilities'}}, $item_availability;
146156
}
147-
$count++;
148157
}
149158

150159
# After going through items, if none are found available, set the biblio
@@ -153,6 +162,10 @@ sub _item_looper {
153162
$self->unavailable(Koha::Plugin::Fi::KohaSuomi::DI::Koha::Exceptions::Biblio::NoAvailableItems->new);
154163
}
155164

165+
# Add total and checked item count
166+
$self->{'items_total'} = $count;
167+
$self->{'items_checked'} = $checked;
168+
156169
return $self;
157170
}
158171

Koha/Plugin/Fi/KohaSuomi/DI/openapi.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,16 @@ paths:
227227
name: include_suspended_in_hold_queue
228228
required: false
229229
type: integer
230+
- description: Check availability of first n items only. Note that this may affect the returned biblio availability as well.
231+
in: query
232+
name: limit
233+
required: false
234+
type: integer
235+
- description: Offset to start checking item availability. Note that this may affect the returned biblio availability as well.
236+
in: query
237+
name: offset
238+
required: false
239+
type: integer
230240
produces:
231241
- application/json
232242
responses:

Koha/Plugin/Fi/KohaSuomi/DI/openapi/definitions/availability/biblio.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,10 @@ properties:
1212
type:
1313
- array
1414
- "null"
15+
items_total:
16+
description: Total item count
17+
type: integer
18+
items_checked:
19+
description: Checked item count
20+
type: integer
1521
type: object

Koha/Plugin/Fi/KohaSuomi/sample_requests.http

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,21 @@ grant_type=client_credentials&client_id={{clientId}}&client_secret={{clientSecre
3636
GET {{baseUrl}}/contrib/kohasuomi/availability/biblios/2/search HTTP/1.1
3737
Authorization: Bearer {{token}}
3838

39+
###
40+
# @name item_statuses_limit
41+
GET {{baseUrl}}/contrib/kohasuomi/availability/biblios/2/search?limit=1 HTTP/1.1
42+
Authorization: Bearer {{token}}
43+
44+
###
45+
# @name item_statuses_offset
46+
GET {{baseUrl}}/contrib/kohasuomi/availability/biblios/2/search?offset=1 HTTP/1.1
47+
Authorization: Bearer {{token}}
48+
49+
###
50+
# @name item_statuses_limit_offset
51+
GET {{baseUrl}}/contrib/kohasuomi/availability/biblios/2/search?limit=3&offset=1 HTTP/1.1
52+
Authorization: Bearer {{token}}
53+
3954
###
4055
# @name hold_availability
4156
GET {{baseUrl}}/contrib/kohasuomi/availability/biblios/2/hold?patron_id=57&include_found_in_hold_queue=0 HTTP/1.1

0 commit comments

Comments
 (0)