Skip to content

Commit 800a6d3

Browse files
committed
Add support for course reserves.
1 parent 73908c3 commit 800a6d3

2 files changed

Lines changed: 397 additions & 0 deletions

File tree

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
package Koha::Plugin::Fi::KohaSuomi::DI::CourseReservesController;
2+
3+
# This program is free software: you can redistribute it and/or modify
4+
# it under the terms of the GNU General Public License as published by
5+
# the Free Software Foundation, either version 3 of the License, or
6+
# (at your option) any later version.
7+
#
8+
# This program is distributed in the hope that it will be useful,
9+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
# GNU General Public License for more details.
12+
#
13+
# You should have received a copy of the GNU General Public License
14+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
#
16+
# This program comes with ABSOLUTELY NO WARRANTY;
17+
18+
use Modern::Perl;
19+
20+
use Mojo::Base 'Mojolicious::Controller';
21+
22+
use Koha::AuthorisedValues;
23+
use Koha::Course::Reserves;
24+
use Koha::Courses;
25+
use Koha::Patrons;
26+
27+
=head1 Koha::Plugin::Fi::KohaSuomi::DI::CourseReservesController
28+
29+
A class implementing the controller methods for the course reserves API
30+
31+
=head2 Class Methods
32+
33+
=head3 getCourses
34+
35+
Get courses
36+
37+
=cut
38+
39+
sub getCourses {
40+
my $c = shift->openapi->valid_input or return;
41+
42+
my $attributes = {
43+
where => {
44+
enabled => 'yes'
45+
}
46+
};
47+
return _get_paged_results($c, Koha::Courses->new, $attributes);
48+
}
49+
50+
sub getDepartments {
51+
my $c = shift->openapi->valid_input or return;
52+
53+
my $courses = Koha::Database->new()->schema()->resultset('Course')->search(
54+
{ department => { '!=', undef }, enabled => 'yes' },
55+
{ select => 'department' }
56+
);
57+
my $attributes = {
58+
where => {
59+
category => 'DEPARTMENT',
60+
authorised_value => { '-in' => $courses->as_query }
61+
}
62+
};
63+
return _get_paged_results($c, Koha::AuthorisedValues->new, $attributes);
64+
}
65+
66+
sub getInstructors {
67+
my $c = shift->openapi->valid_input or return;
68+
69+
my $attributes = {
70+
join => {
71+
course_instructors => 'course',
72+
},
73+
where => {
74+
'course_instructors.course_id' => { '!=' => undef },
75+
'course.enabled' => 'yes'
76+
}
77+
};
78+
return _get_paged_results($c, Koha::Patrons->new, $attributes);
79+
}
80+
81+
sub getCourseReserves {
82+
my $c = shift->openapi->valid_input or return;
83+
84+
my $attributes = {
85+
join => [ { course => 'course_instructors' }, { ci => 'itemnumber' } ],
86+
'+select' => [ qw/ course.course_name course.department course_instructors.borrowernumber itemnumber.biblionumber / ],
87+
'+as' => [ qw/ course_name course_department patron_id biblio_id / ],
88+
where => {
89+
'course.enabled' => 'yes'
90+
}
91+
};
92+
return _get_paged_results($c, Koha::Course::Reserves->new, $attributes);
93+
}
94+
95+
sub _get_paged_results
96+
{
97+
my ($c, $result_set, $attributes) = @_;
98+
99+
return try {
100+
# Extract reserved params
101+
my $args = $c->validation->output;
102+
my ( $filtered_params, $reserved_params ) = $c->extract_reserved_params($args);
103+
$attributes //= {};
104+
105+
# Merge sorting into query attributes
106+
$c->dbic_merge_sorting(
107+
{
108+
attributes => $attributes,
109+
params => $reserved_params,
110+
result_set => $result_set
111+
}
112+
);
113+
114+
# Merge pagination into query attributes
115+
$c->dbic_merge_pagination(
116+
{
117+
filter => $attributes,
118+
params => $reserved_params
119+
}
120+
);
121+
122+
if ( defined $filtered_params ) {
123+
# Apply the mapping function to the passed params
124+
$filtered_params = $result_set->attributes_from_api($filtered_params);
125+
$filtered_params = $c->build_query_params( $filtered_params, $reserved_params );
126+
# course_id might be in joined tables as well, so prefix with 'me.'
127+
if (exists $filtered_params->{course_id}) {
128+
$filtered_params->{'me.course_id'} = $filtered_params->{course_id};
129+
delete $filtered_params->{course_id};
130+
}
131+
# Map patron_id to borrowernumber
132+
if (exists $filtered_params->{patron_id}) {
133+
$filtered_params->{'borrowernumber'} = $filtered_params->{patron_id};
134+
delete $filtered_params->{patron_id};
135+
}
136+
137+
}
138+
139+
# Perform search
140+
my $results = $result_set->search($filtered_params, $attributes);
141+
142+
if ($results->is_paged) {
143+
$c->add_pagination_headers({
144+
total => $results->pager->total_entries,
145+
params => $args,
146+
});
147+
}
148+
149+
return $c->render(status => 200, openapi => $results->to_api());
150+
} catch {
151+
if ( $_->isa('DBIx::Class::Exception') ) {
152+
return $c->render(status => 500, openapi => { error => $_->msg });
153+
}
154+
else {
155+
return $c->render(
156+
status => 500,
157+
openapi => { error => "Something went wrong, check the logs." }
158+
);
159+
}
160+
};
161+
}
162+
163+
1;

0 commit comments

Comments
 (0)