@@ -, +, @@
---
Koha/Ratings.pm | 19 +++++++++++++++++++
.../opac-tmpl/bootstrap/en/modules/opac-detail.tt | 19 ++++++++++---------
.../opac-tmpl/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(-)
--- a/Koha/Ratings.pm
+++ a/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
--- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-detail.tt
+++ a/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 @@
[% 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");
--- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-results.tt
+++ a/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 %]
--- a/opac/opac-detail.pl
+++ a/opac/opac-detail.pl
@@ -37,7 +37,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;
@@ -52,6 +51,7 @@ use C4::HTML5Media;
use C4::CourseReserves qw(GetItemCourseReservesInfo);
use Koha::Virtualshelves;
+use Koha::Ratings;
BEGIN {
if (C4::Context->preference('BakerTaylorEnabled')) {
@@ -1068,12 +1068,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
);
}
--- a/opac/opac-ratings-ajax.pl
+++ a/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,
);
--- a/opac/opac-ratings.pl
+++ a/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;
--- a/opac/opac-search.pl
+++ a/opac/opac-search.pl
@@ -51,10 +51,10 @@ use C4::Koha;
use C4::Tags qw(get_tags);
use C4::Branch; # GetBranches
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;
@@ -695,11 +695,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;
}
}
--