From fc079b6b5fa4ebabdd42fd32c52ce1c10b131927 Mon Sep 17 00:00:00 2001 From: Lucas Gass Date: Tue, 21 Dec 2021 22:51:02 +0000 Subject: [PATCH] Bug 29749: Clean up OpacStarRatings code To test: 1. Apply patch 2. Make sure OpacStarRatings is set to 'results, details, patrons' 3. Check out some items to a patron you can login to the OPAC as 4. Do a OPAC catalog search that will turn up some of those items you checked out 5. Go to the item's detail pages, logged in as the patron with those checkouts 6. From the detail page, rate some of those items. 7. Go to the user summary page, make sure you can see and change the ratings from there. 8. Go to the reading history page, make sure you can see and change those ratins from there. 9. Go back to the detail pages and make sure any changes you made on the user summary and reading history page are acuratly reflected 10. Look at those items from the results page. You should be able to view the ratings but not change them. 11. Again visit the OPAC, this time as a unauthenticated user. You should be able to view the ratings from the details page but not interact with them. --- .../en/includes/user-star-ratings.inc | 101 ++++++++++------- .../bootstrap/en/modules/opac-detail.tt | 103 +++--------------- .../en/modules/opac-readingrecord.tt | 2 +- .../bootstrap/en/modules/opac-results.tt | 24 +--- .../bootstrap/en/modules/opac-user.tt | 2 +- opac/opac-detail.pl | 16 ++- 6 files changed, 87 insertions(+), 161 deletions(-) diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/includes/user-star-ratings.inc b/koha-tmpl/opac-tmpl/bootstrap/en/includes/user-star-ratings.inc index c0ebd2c0c7..2073dd11cd 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/includes/user-star-ratings.inc +++ b/koha-tmpl/opac-tmpl/bootstrap/en/includes/user-star-ratings.inc @@ -1,53 +1,74 @@ -
+[% SET rating_avg = item.ratings.get_avg_rating() %] +[% rating_avg_int = BLOCK %][% rating_avg | format("%.0f") %][% END %] +[% IF interact %] +
- [% SET rating_avg = item.ratings.get_avg_rating() %] - [% rating_avg_int = BLOCK %][% rating_avg | format("%.0f") %][% END %] +
+
+ [% FOREACH i IN [ 1 2 3 4 5 ] %] + [% IF rating_avg_int == i %] + + [% ELSIF rating_avg_int > i %] + + [% ELSE %] + + [% END %] + [% END %] +
+
+ + - - + + - + - - - - + [% IF item.my_rating %] +
+ Your rating: [% item.my_rating.rating_value | html %]. + Cancel rating. +
+ [% ELSE %] + + + [% END %] - [% IF item.my_rating %] -
- Your rating: [% item.my_rating.rating_value | html %]. - Cancel rating. +
Average rating: [% rating_avg | html %] ([% item.ratings.count | html %] votes)
+
+[% ELSE %] +
+
+
+ [% FOREACH i IN [ 1 2 3 4 5 ] %] + [% IF rating_avg_int == i %] + + [% ELSIF rating_avg_int > i %] + + [% ELSE %] + + [% END %] + [% END %] +
- [% ELSE %] - - - [% END %] - -
Average rating: [% rating_avg | html %] ([% item.ratings.count | html %] votes)
-
+ [% IF item.ratings.count > 0 %] +   ([% item.ratings.count | html %] votes) + [% ELSE %] +
+ [% END %] +
+[% END %] 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 f6dc6014a2..ac98e74a8c 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-detail.tt +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-detail.tt @@ -296,50 +296,10 @@ [%# Following on one line due to translation issues %] [% INCLUDE "openlibrary-readapi.inc" bib = { normalized_isbn => normalized_isbn,lccn => lccn, normalized_oclc => normalized_oclc } %] - [% IF ( OpacStarRatings != 'disable' ) %] -
- Star ratings -
- - [% SET rating_avg = ratings.get_avg_rating() %] - [% rating_avg_int = BLOCK %][% rating_avg | format("%.0f") %][% END %] - - [% IF ( borrowernumber ) %] - - [% END %] - [% IF ( rating_avg_int == 0 ) %] - - [% END %] - [% FOREACH i IN [ 1 2 3 4 5 ] %] - [% IF rating_avg_int == i %] - - [% ELSE %] - - [% END %] - [% END %] - - - - - - - - - [% UNLESS ( rating_readonly ) %]  [% END %]  - - [% IF my_rating %] - Your rating: [% my_rating.rating_value | html %]. - Cancel rating. - [% ELSE %] - - - [% END %] - - Average rating: [% rating_avg | html %] ([% ratings.count | html %] votes) -
-
+ [% IF ( OpacStarRatings != 'disable' && loggedinusername) %] + [% INCLUDE 'user-star-ratings.inc' item=rating interact=1 %] + [% ELSIF ( OpacStarRatings != 'disable' )%] + [% INCLUDE 'user-star-ratings.inc' item=rating %] [% END # / IF OpacStarRatings != 'disable' %] [% IF ( BakerTaylorContentURL ) %] @@ -1379,8 +1339,16 @@ [% INCLUDE 'datatables.inc' %] [% INCLUDE 'columns_settings.inc' %] [% INCLUDE greybox.inc %] - [% IF ( OpacStarRatings != 'disable' ) %][% Asset.js("lib/jquery/plugins/jquery.barrating.min.js") | $raw %][% END %] + [% IF ( OpacStarRatings != 'disable' ) %] + [% Asset.js("lib/jquery/plugins/jquery.barrating.min.js") | $raw %] + [% Asset.js("js/ratings.js") | $raw %] + + [% END %] [% IF ( OpacHighlightedWords ) %][% Asset.js("lib/jquery/plugins/jquery.highlight-3.js") | $raw %][% END %] [% IF ( Koha.Preference('OPACDetailQRCode') ) %] [% Asset.js("lib/kjua/kjua.min.js") | $raw %] @@ -1817,51 +1785,6 @@ }); }()); [% END # /IF ( OPACShelfBrowser ) %] - - [% IF ( OpacStarRatings != 'disable' ) %] - // ----------------------------------------------------- - // star-ratings code - // ----------------------------------------------------- - // hide 'rate' button if javascript enabled - - $('input[name="rate_button"]').remove(); - - var rating_enabled = ( $("#star_rating").data("rating-enabled") == "1" ) ? false : true; - $('#star_rating').barrating({ - theme: 'fontawesome-stars', - showSelectedRating: false, - allowEmpty: true, - deselectable: false, - readonly: rating_enabled, - onSelect: function(value, text) { - $("#rating-loading").show(); - $.post("/cgi-bin/koha/opac-ratings-ajax.pl", { - rating_old_value: $("#rating_value").attr("value"), - borrowernumber: "[% borrowernumber | html %]", - biblionumber: "[% biblio.biblionumber | html %]", - rating_value: value, - auth_error: value - }, function (data) { - $("#rating_value").val(data.rating_value); - if (data.rating_value) { - $("#rating_value_text").text(_("Your rating: %s, ").format(data.rating_value)); - $("#cancel_rating_text").show(); - } else { - $("#rating_value_text").text(''); - $("#cancel_rating_text").hide(); - } - $("#rating_text").text(_("Average rating: %s (%s votes)").format(data.rating_avg, data.rating_total)); - $("#rating-loading").hide(); - }, "json"); - } - }); - - $("#cancel_rating_text a").on("click", function(e){ - e.preventDefault(); - $("#star_rating").barrating("set", ""); - }); - - [% END # / IF ( OpacStarRatings != 'disable' )%] }); $(document).ready(function() { diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-readingrecord.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-readingrecord.tt index a3ad4f4e3d..fdc187f0e8 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-readingrecord.tt +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-readingrecord.tt @@ -149,7 +149,7 @@

[% issue.author | html %]

[% IF ( Koha.Preference( 'OpacStarRatings' ) == 'all' ) %] - [% INCLUDE 'user-star-ratings.inc' item=issue %] + [% INCLUDE 'user-star-ratings.inc' item=issue interact=1 %] [% END %] 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 98106fdcd7..f9bc67bed6 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-results.tt +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-results.tt @@ -502,29 +502,7 @@ [% INCLUDE "openlibrary-readapi.inc" bib = SEARCH_RESULT %] [% 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 rating_avg_int == i %] - - [% ELSIF rating_avg_int > i %] - - [% ELSE %] - - [% END %] - [% END %] -
-
- - [% IF SEARCH_RESULT.ratings.count > 0 %] -   ([% SEARCH_RESULT.ratings.count | html %] votes) - [% ELSE %] -
- [% END %] -
+ [% INCLUDE 'user-star-ratings.inc' item=SEARCH_RESULT %] [% END # / IF OpacStarRatings %] [% INCLUDE 'title-actions-menu.inc' items=SEARCH_RESULT %] diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-user.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-user.tt index 481fecc1a1..c36b9baaf9 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-user.tt +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-user.tt @@ -318,7 +318,7 @@ [% IF ( ISSUE.onsite_checkout ) %] (On-site checkout)[% END %] [% IF ( Koha.Preference('OpacStarRatings') == 'all' ) %] - [% INCLUDE 'user-star-ratings.inc' item=ISSUE %] + [% INCLUDE 'user-star-ratings.inc' item=ISSUE interact=1 %] [% END %] [% IF ( ISSUE.recall ) %]
This item has been recalled. Please return by the new due date.[% END %] diff --git a/opac/opac-detail.pl b/opac/opac-detail.pl index 00c52eb5aa..246bc968d2 100755 --- a/opac/opac-detail.pl +++ b/opac/opac-detail.pl @@ -1159,12 +1159,16 @@ if (C4::Context->preference("OPACURLOpenInNewWindow")) { $template->param(borrowernumber => $borrowernumber); if ( C4::Context->preference('OpacStarRatings') !~ /disable/ ) { - my $ratings = Koha::Ratings->search({ biblionumber => $biblionumber }); - my $my_rating = $borrowernumber ? $ratings->search({ borrowernumber => $borrowernumber })->next : undef; - $template->param( - ratings => $ratings, - my_rating => $my_rating, - ); + my $rating; + my $ratings = Koha::Ratings->search({ biblionumber => $biblionumber }); + my $my_rating = $borrowernumber ? $ratings->search({ borrowernumber => $borrowernumber })->next : undef; + $rating->{ratings} = $ratings; + $rating->{my_rating} = $my_rating; + $rating->{borrowernumber} = $borrowernumber; + $rating->{biblionumber} = $biblionumber; + $template->param( + rating => $rating, + ); } #Search for title in links -- 2.20.1