From 70a31874a73fdfaf83371ecd0c80359301dbd45c Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 9 Aug 2016 15:01:21 +0100 Subject: [PATCH] Bug 17089: Koha::Ratings - Remove GetRating Signed-off-by: Owen Leonard Signed-off-by: Marcel de Rooy Signed-off-by: Kyle M Hall --- Koha/Ratings.pm | 19 +++++++++++++++++++ .../bootstrap/en/modules/opac-detail.tt | 19 ++++++++++--------- .../bootstrap/en/modules/opac-results.tt | 13 ++++++------- opac/opac-detail.pl | 11 +++++------ opac/opac-ratings-ajax.pl | 18 ++++++++++-------- opac/opac-ratings.pl | 1 - opac/opac-search.pl | 10 ++++------ 7 files changed, 54 insertions(+), 37 deletions(-) diff --git a/Koha/Ratings.pm b/Koha/Ratings.pm index 27ee47e07e..d449a85b00 100644 --- a/Koha/Ratings.pm +++ b/Koha/Ratings.pm @@ -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 diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-detail.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-detail.tt index 1757d41302..13dc57ecf1 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-detail.tt +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-detail.tt @@ -1,3 +1,4 @@ +[% USE Math %] [% USE Koha %] [% USE KohaDates %] [% USE Branches %] @@ -468,10 +469,12 @@
+ [% 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 %] - [% ELSIF rating_avg == i %] + [% ELSIF rating_avg_int == i %] [% ELSIF borrowernumber %] @@ -483,19 +486,17 @@ - - - + [% UNLESS ( rating_readonly ) %]  [% END %]  - [% IF ( rating_value ) %] - your rating: [% rating_value %], + [% IF my_rating %] + your rating: [% my_rating.rating_value %], [% ELSE %] [% END %] - average rating: [% rating_avg_int %] ([% rating_total %] votes) + average rating: [% rating_avg %] ([% ratings.count %] votes)
[% END # / IF OpacStarRatings != 'disable' %] @@ -1597,7 +1598,7 @@ $("#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"); diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-results.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-results.tt index 7021638ed1..41a046ac40 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-results.tt +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-results.tt @@ -468,19 +468,18 @@ [% IF ( OpacStarRatings == 'all' ) %]
+ [% 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 %] - [% ELSE %] + [% ELSE %] [% END %] [% END %] - - - - [% IF SEARCH_RESULT.rating_total && SEARCH_RESULT.rating_total > 0 %] -   ([% SEARCH_RESULT.rating_total %] votes) + [% IF SEARCH_RESULT.ratings.count > 0 %] +   ([% SEARCH_RESULT.ratings.count %] votes) [% ELSE %]
[% END %] diff --git a/opac/opac-detail.pl b/opac/opac-detail.pl index 072572dd1c..2d019b1d44 100755 --- a/opac/opac-detail.pl +++ b/opac/opac-detail.pl @@ -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 ); } diff --git a/opac/opac-ratings-ajax.pl b/opac/opac-ratings-ajax.pl index a99a335842..672e7f35d5 100755 --- a/opac/opac-ratings-ajax.pl +++ b/opac/opac-ratings-ajax.pl @@ -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, ); diff --git a/opac/opac-ratings.pl b/opac/opac-ratings.pl index 33d4aee5a5..d936b13b65 100755 --- a/opac/opac-ratings.pl +++ b/opac/opac-ratings.pl @@ -31,7 +31,6 @@ use CGI qw ( -utf8 ); use C4::Auth; use C4::Context; -use C4::Ratings; use C4::Debug; use Koha::Ratings; diff --git a/opac/opac-search.pl b/opac/opac-search.pl index 8dda1f0cc9..37342f5cda 100755 --- a/opac/opac-search.pl +++ b/opac/opac-search.pl @@ -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; } } -- 2.39.5