Bug 11969: Show patrons star rating on their reading history
[koha.git] / opac / svc / recordedbooks
1 #!/usr/bin/perl
2
3 # Copyright 2016 Catalyst IT
4 # This file is part of Koha.
5 #
6 # Koha is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # Koha is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with Koha; if not, see <http://www.gnu.org/licenses>.
18
19 use Modern::Perl;
20 use CGI qw ( -utf8 );
21 use JSON qw(encode_json);
22 use C4::Auth qw(checkauth);
23 use C4::Output;
24 use Koha::Logger;
25 use Koha::ExternalContent::RecordedBooks;
26
27 our $cgi = new CGI;
28 my $logger = Koha::Logger->get({ interface => 'opac' });
29 my $page_url = $cgi->referer();
30
31 my ( $user, $cookie, $sessionID, $flags ) = checkauth( $cgi, 1, {}, 'opac' );
32
33 my $action = $cgi->param('action') or response_bad_request("No 'action' specified");
34
35 ($user && $sessionID) || $action eq 'search' or response_bad_request("User not logged in");
36
37 local $@;
38 my $rb = eval { Koha::ExternalContent::RecordedBooks->new({ koha_session_id => $sessionID }) };
39 unless ($rb) {
40     $logger->error($@) if $@;
41     response({
42         error => $@,
43         is_identified => JSON::false,
44     });
45 }
46
47 my $is_identified = $rb->is_identified;
48 my %data = (
49     is_identified => $is_identified ? JSON::true : JSON::false,
50 );
51 response(\%data) unless $is_identified || $action eq 'search';
52
53 eval {
54     {
55         $action eq 'search' && do {
56             my $query = $cgi->param('q');
57             my $page  = $cgi->param('page');
58             my $page_size = $cgi->param('page_size');
59             my $sort = $cgi->param('sort');
60             my $res = $rb->search({
61                 query => $query,
62                 page  => $page,
63                 page_size => $page_size,
64                 sort => $sort,
65             });
66             $data{results} = $res;
67             last;
68         };
69
70         $action eq 'account' && do {
71             eval {
72                 $data{account} = $rb->patron;
73                 $data{checkouts} = $rb->checkouts;
74                 $data{holds} = $rb->holds;
75             };
76             response_bad_request($rb->error_message($@)) if $@;
77             last;
78         };
79
80         $action eq 'checkout' && do {
81             my $isbn = $cgi->param('isbn')
82               or response_bad_request("No 'isbn' specified");
83             my $format = $cgi->param('format');
84             $data{action} = eval {  $rb->checkout($isbn, $format) };
85             response_bad_request($rb->error_message($@)) if $@;
86
87             eval {
88                 $data{checkouts} = $rb->checkouts;
89                 $data{holds} = $rb->holds;
90             };
91             $data{error} = $rb->error_message($@) if $@;
92             last;
93         };
94
95         $action eq 'return' && do {
96             my $isbn = $cgi->param('isbn')
97               or response_bad_request("No 'isbn' specified");
98             $data{action} = eval { $rb->return($isbn) };
99             response_bad_request($rb->error_message($@)) if $@;
100
101             $data{checkouts} = eval { $rb->checkouts };
102             $data{error} = $rb->error_message($@) if $@;
103             last;
104         };
105
106         $action eq 'place_hold' && do {
107             my $isbn = $cgi->param('isbn')
108               or response_bad_request("No 'isbn' specified");
109             $data{action} = eval { $rb->place_hold($isbn) };
110             response_bad_request($rb->error_message($@)) if $@;
111
112             $data{holds} = eval { $rb->holds };
113             $data{error} = $rb->error_message($@) if $@;
114             last;
115         };
116
117         $action eq 'remove_hold' && do {
118             my $isbn = $cgi->param('isbn')
119               or response_bad_request("No 'isbn' specified");
120             $data{action} = eval { $rb->remove_hold($isbn) };
121             response_bad_request($rb->error_message($@)) if $@;
122
123             $data{holds} = eval { $rb->holds };
124             $data{error} = $rb->error_message($@) if $@;
125             last;
126         };
127
128         response_bad_request("Invalid 'action': $action");
129     }
130 };
131 if ($@) {
132     $logger->error($@);
133     $data{error} = $rb->error_message("$@");
134 }
135
136 response(\%data);
137
138
139 sub response_bad_request {
140     my ($error) = @_;
141     response({error => $error}, "400 $error");
142 }
143 sub response {
144     my ($data, $status_line) = @_;
145     $status_line ||= "200 OK";
146     output_with_http_headers $cgi, undef, encode_json($data), 'json', $status_line;
147     exit;
148 }