Bug 29408: Add JSDoc documentation for kohaTable function
[koha-ffzg.git] / opac / opac-ratings-ajax.pl
index 672e7f3..dcbeaeb 100755 (executable)
@@ -23,16 +23,14 @@ A script that takes an ajax json query, and then inserts or modifies a star-rati
 
 =cut
 
-use strict;
-use warnings;
+use Modern::Perl;
 
 use CGI qw ( -utf8 );
 use CGI::Cookie;  # need to check cookies before having CGI parse the POST request
 
-use C4::Auth qw(:DEFAULT check_cookie_auth);
+use C4::Auth qw( get_template_and_user check_cookie_auth );
 use C4::Context;
-use C4::Debug;
-use C4::Output qw(:html :ajax pagination_bar);
+use C4::Output qw( is_ajax output_ajax_with_http_headers );
 
 use Koha::Ratings;
 
@@ -63,7 +61,6 @@ else {
             query           => $query,
             type            => "opac",
             authnotrequired => 0,                    # auth required to add tags
-            debug           => 1,
         }
     );
 }
@@ -72,7 +69,8 @@ my $rating;
 $rating_value //= '';
 
 if ( $rating_value eq '' ) {
-    Koha::Ratings->find( { biblionumber => $biblionumber, borrowernumber => $loggedinuser } )->delete;
+    my $rating = Koha::Ratings->find( { biblionumber => $biblionumber, borrowernumber => $loggedinuser } );
+    $rating->delete if $rating;
 }
 
 elsif ( $rating_value and !$rating_old_value ) {
@@ -80,7 +78,8 @@ elsif ( $rating_value and !$rating_old_value ) {
 }
 
 elsif ( $rating_value ne $rating_old_value ) {
-    Koha::Ratings->find( { biblionumber => $biblionumber, borrowernumber => $loggedinuser })->rating_value($rating_value)->store;
+    my $rating = Koha::Ratings->find( { biblionumber => $biblionumber, borrowernumber => $loggedinuser });
+    $rating->rating_value($rating_value)->store if $rating
 }
 
 my $ratings = Koha::Ratings->search({ biblionumber => $biblionumber });
@@ -91,7 +90,7 @@ my %js_reply = (
     rating_total   => $ratings->count,
     rating_avg     => $avg,
     rating_avg_int => sprintf("%.0f", $avg),
-    rating_value   => $my_rating->rating_value,
+    rating_value   => $my_rating ? $my_rating->rating_value : undef,
     auth_status    => $auth_status,
 
 );
@@ -111,7 +110,7 @@ sub ajax_auth_cgi {
     my %cookies      = CGI::Cookie->fetch;
     my $input        = CGI->new;
     my $sessid = $cookies{'CGISESSID'}->value || $input->param('CGISESSID');
-    my ( $auth_status, $auth_sessid ) =
+    my ( $auth_status ) =
       check_cookie_auth( $sessid, $needed_flags );
     return $input, $auth_status;
 }