Bug 19198: Prevent the test to fail randomly
[koha.git] / opac / opac-ratings-ajax.pl
blob25675add0de30150f311bba92702d737740c4b98
1 #!/usr/bin/perl
3 # Copyright 2011 KohaAloha, NZ
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 =head1 DESCRIPTION
22 A script that takes an ajax json query, and then inserts or modifies a star-rating.
24 =cut
26 use strict;
27 use warnings;
29 use CGI qw ( -utf8 );
30 use CGI::Cookie; # need to check cookies before having CGI parse the POST request
32 use C4::Auth qw(:DEFAULT check_cookie_auth);
33 use C4::Context;
34 use C4::Debug;
35 use C4::Output qw(:html :ajax pagination_bar);
37 use Koha::Ratings;
39 use JSON;
41 my $is_ajax = is_ajax();
43 my ( $query, $auth_status );
44 if ($is_ajax) {
45 ( $query, $auth_status ) = &ajax_auth_cgi( {} );
47 else {
48 $query = CGI->new();
51 my $biblionumber = $query->param('biblionumber');
52 my $rating_value = $query->param('rating_value');
53 my $rating_old_value = $query->param('rating_old_value');
55 my ( $template, $loggedinuser, $cookie );
56 if ($is_ajax) {
57 $loggedinuser = C4::Context->userenv->{'number'};
59 else {
60 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
62 template_name => "opac-detail.tt",
63 query => $query,
64 type => "opac",
65 authnotrequired => 0, # auth required to add tags
66 debug => 1,
71 my $rating;
72 $rating_value //= '';
74 if ( $rating_value eq '' ) {
75 my $rating = Koha::Ratings->find( { biblionumber => $biblionumber, borrowernumber => $loggedinuser } );
76 $rating->delete if $rating;
79 elsif ( $rating_value and !$rating_old_value ) {
80 Koha::Rating->new( { biblionumber => $biblionumber, borrowernumber => $loggedinuser, rating_value => $rating_value, })->store;
83 elsif ( $rating_value ne $rating_old_value ) {
84 my $rating = Koha::Ratings->find( { biblionumber => $biblionumber, borrowernumber => $loggedinuser });
85 $rating->rating_value($rating_value)->store if $rating
88 my $ratings = Koha::Ratings->search({ biblionumber => $biblionumber });
89 my $my_rating = $ratings->search({ borrowernumber => $loggedinuser })->next;
90 my $avg = $ratings->get_avg_rating;
92 my %js_reply = (
93 rating_total => $ratings->count,
94 rating_avg => $avg,
95 rating_avg_int => sprintf("%.0f", $avg),
96 rating_value => $my_rating ? $my_rating->rating_value : undef,
97 auth_status => $auth_status,
101 my $json_reply = JSON->new->encode( \%js_reply );
103 #### $rating
104 #### %js_reply
105 #### $json_reply
107 output_ajax_with_http_headers( $query, $json_reply );
108 exit;
110 # a ratings specific ajax return sub, returns CGI object, and an 'auth_success' value
111 sub ajax_auth_cgi {
112 my $needed_flags = shift;
113 my %cookies = CGI::Cookie->fetch;
114 my $input = CGI->new;
115 my $sessid = $cookies{'CGISESSID'}->value || $input->param('CGISESSID');
116 my ( $auth_status, $auth_sessid ) =
117 check_cookie_auth( $sessid, $needed_flags );
118 return $input, $auth_status;