Koha/koha-tmpl/opac-tmpl/bootstrap/js/ratings.js
Owen Leonard d37a3576f2
Bug 25110: Allow patrons to add star ratings to titles on their summary/checkout page
This patch adds the ability for logged-in patrons to add star ratings to
titles in their list of current checkouts and on the reading history
page.

The reading history page previously only showed existing ratings. Now it
will allow the entry of ratings as well.

The JS and markup for the feature are in separate include files to
facilitate re-use. It includes markup for non-js display of ratings and
js-driven ratings controls.

To test, apply the patch and set the OpacStarRatings to all, "results,
details, and user" pages.

 - Log in to the OPAC as a user with checkouts.
 - On the "your summary" page, test the features of setting star
   ratings:
   - Any title with existing ratings should show the rating and the
     average rating number.
   - Set a rating. Confirm that it shows a message with your rating.
   - Click the "cancel rating" link, and confirm that your rating is
     removed.
   - Set a rating and reload the page. Confirm that the rating was
     saved.
   - Test the "cancel rating" link on the title you set before you
     reloaded the page. The rating should be cancelled.
 - Test the same functionality on the "your reading history" page.
 - Test these pages with the  OpacStarRatings preference set to "only
   details" or "no." The pages should function correctly.

Signed-off-by: David Nind <david@davidnind.com>
Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
2020-04-29 17:04:32 +01:00

46 lines
1.8 KiB
JavaScript

/* global borrowernumber MSG_YOUR_RATING MSG_AVERAGE_RATING */
// -----------------------------------------------------
// star-ratings code
// -----------------------------------------------------
// hide 'rate' button if javascript enabled
$(document).ready(function(){
$("input[name='rate_button']").remove();
var star_ratings = $(".star_rating");
star_ratings.barrating({
theme: 'fontawesome-stars',
showSelectedRating: false,
allowEmpty: true,
deselectable: false,
onSelect: function( value ) {
var context = $("#" + this.$elem.data("context") );
$(".rating-loading", context ).show();
$.post("/cgi-bin/koha/opac-ratings-ajax.pl", {
rating_old_value: $(".rating_value", context ).attr("value"),
borrowernumber: borrowernumber,
biblionumber: this.$elem.data('biblionumber'),
rating_value: value,
auth_error: value
}, function (data) {
$(".rating_value", context ).val(data.rating_value);
if (data.rating_value) {
$(".rating_value_text", context ).text( MSG_YOUR_RATING.format(data.rating_value) );
$(".cancel_rating_text", context ).show();
} else {
$(".rating_value_text", context ).text("");
$(".cancel_rating_text", context ).hide();
}
$(".rating_text", context ).text( MSG_AVERAGE_RATING.format(data.rating_avg, data.rating_total) );
$(".rating-loading", context ).hide();
}, "json");
}
});
$("body").on("click", ".cancel_rating_text a", function(e){
e.preventDefault();
var context = "#" + $(this).data("context");
$(context).barrating("set", "");
});
});