Translation updates for Koha 3.22.0 release
[koha.git] / C4 / Ratings.pm
blobf869f690c9e10c14796a004e6e07778bc02d1b07
1 package C4::Ratings;
3 # Copyright 2011 KohaAloha, NZ
4 # Parts copyright 2011, Catalyst IT, NZ.
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21 use strict;
22 use warnings;
23 use Carp;
24 use Exporter;
25 use POSIX;
26 use C4::Debug;
27 use C4::Context;
29 use Koha::Database;
31 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
33 BEGIN {
34 $VERSION = 3.07.00.049;
35 @ISA = qw(Exporter);
37 @EXPORT = qw(
38 &GetRating
39 &AddRating
40 &ModRating
41 &DelRating
45 =head1 NAME
47 C4::Ratings - a module to manage user ratings of Koha biblios
49 =head1 DESCRIPTION
51 Ratings.pm provides simple functionality for a user to 'rate' a biblio, and to retrieve a biblio's rating info
53 the 4 subroutines allow a user to add, delete modify and retrieve rating info for a biblio.
55 The rating can be from 1 to 5 stars, (5 stars being the highest rating)
57 =head1 SYNOPSIS
59 Get a rating for a bib
60 my $rating_hashref = GetRating( $biblionumber, undef );
61 my $rating_hashref = GetRating( $biblionumber, $borrowernumber );
63 Add a rating for a bib
64 my $rating_hashref = AddRating( $biblionumber, $borrowernumber, $rating_value );
66 Mod a rating for a bib
67 my $rating_hashref = ModRating( $biblionumber, $borrowernumber, $rating_value );
69 Delete a rating for a bib
70 my $rating_hashref = DelRating( $biblionumber, $borrowernumber );
73 All subroutines in Ratings.pm return a hashref which contain 4 keys
75 for example, after executing this statement below...
77 my $rating_hashref = GetRating ( $biblionumber, $borrowernumber ) ;
79 $rating_hashref now contains a hashref that looks like this...
81 $rating = {
82 rating_avg => '2',
83 rating_avg_int => '2.3',
84 rating_total => '432',
85 rating_value => '5'
88 they 4 keys returned in the hashref are...
90 rating_avg: average rating of a biblio
91 rating_avg_int: average rating of a biblio, rounded to 1dp
92 rating_total: total number of ratings of a biblio
93 rating_value: logged-in user's rating of a biblio
95 =head1 BUGS
97 Please use bugs.koha-community.org for tracking bugs.
99 =head1 SOURCE AVAILABILITY
101 The source is available from the koha-community.org git server
102 L<http://git.koha-community.org>
104 =head1 AUTHOR
106 Original code: Mason James <mtj@kohaaloha.com>
108 =head1 COPYRIGHT
110 Copyright (c) 2011 Mason James <mtj@kohaaloha.com>
112 =head1 LICENSE
114 C4::Ratings is free software. You can redistribute it and/or
115 modify it under the same terms as Koha itself.
117 =head1 CREDITS
119 Mason James <mtj@kohaaloha.com>
120 Koha Dev Team <http://koha-community.org>
123 =head2 GetRating
125 GetRating($biblionumber, [$borrowernumber])
127 Get a rating for a bib
128 my $rating_hashref = GetRating( $biblionumber, undef );
129 my $rating_hashref = GetRating( $biblionumber, $borrowernumber );
131 This returns the rating for the supplied biblionumber. It will also return
132 the rating that the supplied user gave to the provided biblio. If a particular
133 value can't be supplied, '0' is returned for that value.
135 =head3 RETURNS
137 A hashref containing:
139 =over
141 =item * rating_avg - average rating of a biblio
143 =item * rating_avg_int - average rating of a biblio, rounded to 1dp
145 =item * rating_total - total number of ratings of a biblio
147 =item * rating_value - logged-in user's rating of a biblio
149 =back
151 =cut
153 sub GetRating {
154 my ( $biblionumber, $borrowernumber ) = @_;
156 my $ratings = Koha::Database->new()->schema->resultset('Rating')->search(
158 biblionumber => $biblionumber,
162 my $sum = $ratings->get_column('rating_value')->sum();
163 my $total = $ratings->count();
165 my ( $avg, $avg_int ) = 0;
167 if ( $sum and $total ) {
168 eval { $avg = $sum / $total };
171 $avg_int = sprintf( "%.1f", $avg );
172 $avg = sprintf( "%.0f", $avg );
174 my %rating_hash;
175 $rating_hash{rating_total} = $total || 0;
176 $rating_hash{rating_avg} = $avg || 0;
177 $rating_hash{rating_avg_int} = $avg_int || 0;
179 if ($borrowernumber) {
180 my $rating = Koha::Database->new()->schema->resultset('Rating')->find(
182 biblionumber => $biblionumber,
183 borrowernumber => $borrowernumber,
186 return unless $rating;
187 $rating_hash{'rating_value'} = $rating->rating_value();
189 else {
190 $rating_hash{rating_value} = undef;
193 return \%rating_hash;
196 =head2 AddRating
198 my $rating_hashref = AddRating( $biblionumber, $borrowernumber, $rating_value );
200 Add a rating for a bib
202 This adds or updates a rating for a particular user on a biblio. If the value
203 is 0, then the rating will be deleted. If the value is out of the range of
204 0-5, nothing will happen.
206 =cut
208 sub AddRating {
209 my ( $biblionumber, $borrowernumber, $rating_value ) = @_;
211 my $rating = Koha::Database->new()->schema->resultset('Rating')->create(
213 biblionumber => $biblionumber,
214 borrowernumber => $borrowernumber,
215 rating_value => $rating_value
219 return GetRating( $biblionumber, $borrowernumber );
222 =head2 ModRating
224 my $rating_hashref = ModRating( $biblionumber, $borrowernumber, $rating_value );
226 Mod a rating for a bib
228 =cut
230 sub ModRating {
231 my ( $biblionumber, $borrowernumber, $rating_value ) = @_;
233 my $rating = Koha::Database->new()->schema->resultset('Rating')->find(
235 borrowernumber => $borrowernumber,
236 biblionumber => $biblionumber
240 $rating->update( { rating_value => $rating_value } );
242 return GetRating( $biblionumber, $borrowernumber );
245 =head2 DelRating
247 my $rating_hashref = DelRating( $biblionumber, $borrowernumber );
249 Delete a rating for a bib
251 =cut
253 sub DelRating {
254 my ( $biblionumber, $borrowernumber ) = @_;
256 my $rating = Koha::Database->new()->schema->resultset('Rating')->find(
258 borrowernumber => $borrowernumber,
259 biblionumber => $biblionumber
263 $rating->delete() if $rating;
265 return GetRating($biblionumber);
269 __END__