Bug 17089: Koha::Ratings - Remove GetRating
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Tue, 9 Aug 2016 14:01:21 +0000 (15:01 +0100)
committerKyle M Hall <kyle@bywatersolutions.com>
Fri, 9 Sep 2016 09:31:29 +0000 (09:31 +0000)
Signed-off-by: Owen Leonard <oleonard@myacpl.org>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>

Koha/Ratings.pm
koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-detail.tt
koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-results.tt
opac/opac-detail.pl
opac/opac-ratings-ajax.pl
opac/opac-ratings.pl
opac/opac-search.pl

index 27ee47e..d449a85 100644 (file)
@@ -35,6 +35,25 @@ Koha::Ratings - Koha Rating Object set class
 
 =cut
 
+=head3 get_avg_rating
+
+=cut
+
+sub get_avg_rating {
+    my ( $self ) = @_;
+
+    my $sum   = $self->_resultset->get_column('rating_value')->sum();
+    my $total = $self->count();
+
+    my $avg = 0;
+    if ( $sum and $total ) {
+        eval { $avg = $sum / $total };
+    }
+    $avg = sprintf( "%.1f", $avg );
+
+    return $avg;
+}
+
 =head3 type
 
 =cut
index 1757d41..13dc57e 100644 (file)
@@ -1,3 +1,4 @@
+[% USE Math %]
 [% USE Koha %]
 [% USE KohaDates %]
 [% USE Branches %]
                         <form method="post" action="/cgi-bin/koha/opac-ratings.pl">
                             <div class="results_summary ratings">
 
+                                [% SET rating_avg = ratings.get_avg_rating() %]
+                                [% rating_avg_int = BLOCK %][% rating_avg | format("%.0f") %][% END %]
                                 [% FOREACH i  IN [ 1 2 3 4 5  ] %]
-                                    [% IF rating_avg == i && borrowernumber %]
+                                    [% IF rating_avg_int == i && borrowernumber %]
                                         <input class="auto-submit-star" type="radio" name="rating"  value="[% i %]"  checked="checked" />
-                                    [% ELSIF rating_avg == i %]
+                                    [% ELSIF rating_avg_int == i %]
                                         <input class="auto-submit-star" type="radio" name="rating" value="[% i %]" checked="checked" disabled="disabled" />
                                     [% ELSIF borrowernumber  %]
                                         <input class="auto-submit-star" type="radio" name="rating" value="[% i %]" />
                                 <!-- define some hidden vars for ratings -->
 
                                 <input  type="hidden" name='biblionumber'  value="[% biblionumber %]" />
-                                <input  type="hidden" name='rating_value' id='rating_value' value="[% rating_value %]" />
-                                <input  type="hidden" name='rating_total' id='rating_total' value="[% rating_total %]" />
-                                <input  type="hidden" name='rating_avg_int' id='rating_avg_int' value="[% rating_avg_int %]" />
+                                <input  type="hidden" name='rating_value' id='rating_value' value="[% my_rating.rating_value %]" />
 
                                 [% UNLESS ( rating_readonly ) %]&nbsp;  <input name="rate_button" type="submit" value="Rate me" />[% END %]&nbsp;
 
-                                [% IF ( rating_value ) %]
-                                    <span id="rating_value_text">your rating: [% rating_value %], </span>
+                                [% IF my_rating %]
+                                    <span id="rating_value_text">your rating: [% my_rating.rating_value %], </span>
                                 [% ELSE %]
                                     <span id="rating_value_text"></span>
                                 [% END %]
 
-                                <span id="rating_text">average rating: [% rating_avg_int %] ([% rating_total %] votes)</span>
+                                <span id="rating_text">average rating: [% rating_avg %] ([% ratings.count %] votes)</span>
                             </div>
                         </form>
                     [% END # / IF OpacStarRatings != 'disable' %]
                     $("#rating_value_text").text('');
                   }
 
-                  $("#rating_text").text(_("average rating: ") + data.rating_avg_int + " (" + data.rating_total + ' ' + _("votes") + ")");
+                  $("#rating_text").text(_("average rating: ") + data.rating_avg + " (" + data.rating_total + ' ' + _("votes") + ")");
 
                 }
               }, "json");
index 7021638..41a046a 100644 (file)
 
                                                 [% IF ( OpacStarRatings == 'all' ) %]
                                                     <div class="results_summary ratings">
+                                                      [% SET rating_avg = SEARCH_RESULT.ratings.get_avg_rating() %]
+                                                      [% rating_avg_int = BLOCK %][% rating_avg | format("%.0f") %][% END %]
                                                         [% FOREACH i  IN [ 1 2 3 4 5  ] %]
-                                                            [% IF ( SEARCH_RESULT.rating_avg == i ) %]
+                                                            [% IF rating_avg_int == i %]
                                                                 <input class="star" type="radio"  name="rating-[% SEARCH_RESULT.biblionumber %]" value="[% i %]" checked="checked" disabled="disabled"   />
-                                                            [% ELSE   %]
+                                                            [% ELSE %]
                                                                 <input class="star" type="radio"  name="rating-[% SEARCH_RESULT.biblionumber %]" value="[% i %]" disabled="disabled"   />
                                                             [% END %]
                                                         [% END %]
 
-                                                        <input type="hidden" name="biblionumber"  value="[% SEARCH_RESULT.biblionumber %]" />
-                                                        <input type="hidden" name="loggedinuser"  value="[% loggedinuser %]" />
-
-                                                        [% IF SEARCH_RESULT.rating_total && SEARCH_RESULT.rating_total > 0  %]
-                                                            <span id="rating_total_[% SEARCH_RESULT.biblionumber %]">&nbsp;&nbsp;([% SEARCH_RESULT.rating_total %] votes)</span>
+                                                        [% IF SEARCH_RESULT.ratings.count > 0  %]
+                                                            <span id="rating_total_[% SEARCH_RESULT.biblionumber %]">&nbsp;&nbsp;([% SEARCH_RESULT.ratings.count %] votes)</span>
                                                         [% ELSE %]
                                                             <br />
                                                         [% END %]
index 072572d..2d019b1 100755 (executable)
@@ -36,7 +36,6 @@ use C4::XISBN qw(get_xisbns get_biblionumber_from_isbn);
 use C4::External::Amazon;
 use C4::External::Syndetics qw(get_syndetics_index get_syndetics_summary get_syndetics_toc get_syndetics_excerpt get_syndetics_reviews get_syndetics_anotes );
 use C4::Review;
-use C4::Ratings;
 use C4::Members;
 use C4::XSLT;
 use C4::ShelfBrowser;
@@ -51,6 +50,7 @@ use C4::HTML5Media;
 use C4::CourseReserves qw(GetItemCourseReservesInfo);
 use Koha::RecordProcessor;
 use Koha::Virtualshelves;
+use Koha::Ratings;
 
 BEGIN {
        if (C4::Context->preference('BakerTaylorEnabled')) {
@@ -1080,12 +1080,11 @@ if (C4::Context->preference("OPACURLOpenInNewWindow")) {
 }
 
 if ( C4::Context->preference('OpacStarRatings') !~ /disable/ ) {
-    my $rating = GetRating( $biblionumber, $borrowernumber );
+    my $ratings = Koha::Ratings->search({ biblionumber => $biblionumber });
+    my $my_rating = $borrowernumber ? $ratings->search({ borrowernumber => $borrowernumber })->next : undef;
     $template->param(
-        rating_value   => $rating->{'rating_value'},
-        rating_total   => $rating->{'rating_total'},
-        rating_avg     => $rating->{'rating_avg'},
-        rating_avg_int => $rating->{'rating_avg_int'},
+        ratings => $ratings,
+        my_rating => $my_rating,
         borrowernumber => $borrowernumber
     );
 }
index a99a335..672e7f3 100755 (executable)
@@ -33,7 +33,6 @@ use C4::Auth qw(:DEFAULT check_cookie_auth);
 use C4::Context;
 use C4::Debug;
 use C4::Output qw(:html :ajax pagination_bar);
-use C4::Ratings;
 
 use Koha::Ratings;
 
@@ -74,22 +73,25 @@ $rating_value //= '';
 
 if ( $rating_value eq '' ) {
     Koha::Ratings->find( { biblionumber => $biblionumber, borrowernumber => $loggedinuser } )->delete;
-    $rating = Koha::Ratings->search({ biblionumber => $biblionumber })->get_rating;
 }
 
 elsif ( $rating_value and !$rating_old_value ) {
-    $rating = Koha::Rating->new( { biblionumber => $biblionumber, borrowernumber => $loggedinuser, rating_value => $rating_value, })->store;
+    Koha::Rating->new( { biblionumber => $biblionumber, borrowernumber => $loggedinuser, rating_value => $rating_value, })->store;
 }
 
 elsif ( $rating_value ne $rating_old_value ) {
-    $rating = Koha::Ratings->find( { biblionumber => $biblionumber, borrowernumber => $loggedinuser })->rating_value($rating_value)->store;
+    Koha::Ratings->find( { biblionumber => $biblionumber, borrowernumber => $loggedinuser })->rating_value($rating_value)->store;
 }
 
+my $ratings = Koha::Ratings->search({ biblionumber => $biblionumber });
+my $my_rating = $ratings->search({ borrowernumber => $loggedinuser })->next;
+my $avg = $ratings->get_avg_rating;
+
 my %js_reply = (
-    rating_total   => $rating->{'rating_total'},
-    rating_avg     => $rating->{'rating_avg'},
-    rating_avg_int => $rating->{'rating_avg_int'},
-    rating_value   => $rating->{'rating_value'},
+    rating_total   => $ratings->count,
+    rating_avg     => $avg,
+    rating_avg_int => sprintf("%.0f", $avg),
+    rating_value   => $my_rating->rating_value,
     auth_status    => $auth_status,
 
 );
index 33d4aee..d936b13 100755 (executable)
@@ -31,7 +31,6 @@ use CGI qw ( -utf8 );
 
 use C4::Auth;
 use C4::Context;
-use C4::Ratings;
 use C4::Debug;
 
 use Koha::Ratings;
index 8dda1f0..37342f5 100755 (executable)
@@ -50,10 +50,10 @@ use C4::Biblio;  # GetBiblioData
 use C4::Koha;
 use C4::Tags qw(get_tags);
 use C4::SocialData;
-use C4::Ratings;
 use C4::External::OverDrive;
 
 use Koha::LibraryCategories;
+use Koha::Ratings;
 
 use POSIX qw(ceil floor strftime);
 use URI::Escape;
@@ -693,11 +693,9 @@ for (my $i=0;$i<@servers;$i++) {
             }
 
             if ( C4::Context->preference('OpacStarRatings') eq 'all' ) {
-                my $rating = GetRating( $res->{'biblionumber'}, $borrowernumber );
-                $res->{'rating_value'}  = $rating->{'rating_value'};
-                $res->{'rating_total'}  = $rating->{'rating_total'};
-                $res->{'rating_avg'}    = $rating->{'rating_avg'};
-                $res->{'rating_avg_int'} = $rating->{'rating_avg_int'};
+                my $ratings = Koha::Ratings->search({ biblionumber => $res->{biblionumber} });
+                $res->{ratings} = $ratings;
+                $res->{my_rating} = $borrowernumber ? $ratings->search({ borrowernumber => $borrowernumber })->next : undef;
             }
         }