Bug 17089: Koha::Ratings - Remove AddRating
[koha.git] / opac / opac-ratings-ajax.pl
blob1e69650e844afe8c3d5c549257b0a9dacee8457f
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);
36 use C4::Ratings;
38 use Koha::Ratings;
40 use JSON;
42 my $is_ajax = is_ajax();
44 my ( $query, $auth_status );
45 if ($is_ajax) {
46 ( $query, $auth_status ) = &ajax_auth_cgi( {} );
48 else {
49 $query = CGI->new();
52 my $biblionumber = $query->param('biblionumber');
53 my $rating_value = $query->param('rating_value');
54 my $rating_old_value = $query->param('rating_old_value');
56 my ( $template, $loggedinuser, $cookie );
57 if ($is_ajax) {
58 $loggedinuser = C4::Context->userenv->{'number'};
60 else {
61 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
63 template_name => "opac-detail.tt",
64 query => $query,
65 type => "opac",
66 authnotrequired => 0, # auth required to add tags
67 debug => 1,
72 my $rating;
73 $rating_value //= '';
75 if ( $rating_value eq '' ) {
76 #### delete
77 $rating = DelRating( $biblionumber, $loggedinuser );
80 elsif ( $rating_value and !$rating_old_value ) {
81 $rating = Koha::Rating->new( { biblionumber => $biblionumber, borrowernumber => $loggedinuser, rating_value => $rating_value, })->store;
84 elsif ( $rating_value ne $rating_old_value ) {
85 #### mod
86 $rating = ModRating( $biblionumber, $loggedinuser, $rating_value );
89 my %js_reply = (
90 rating_total => $rating->{'rating_total'},
91 rating_avg => $rating->{'rating_avg'},
92 rating_avg_int => $rating->{'rating_avg_int'},
93 rating_value => $rating->{'rating_value'},
94 auth_status => $auth_status,
98 my $json_reply = JSON->new->encode( \%js_reply );
100 #### $rating
101 #### %js_reply
102 #### $json_reply
104 output_ajax_with_http_headers( $query, $json_reply );
105 exit;
107 # a ratings specific ajax return sub, returns CGI object, and an 'auth_success' value
108 sub ajax_auth_cgi {
109 my $needed_flags = shift;
110 my %cookies = CGI::Cookie->fetch;
111 my $input = CGI->new;
112 my $sessid = $cookies{'CGISESSID'}->value || $input->param('CGISESSID');
113 my ( $auth_status, $auth_sessid ) =
114 check_cookie_auth( $sessid, $needed_flags );
115 return $input, $auth_status;