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>.
31 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
46 C4::Ratings - a module to manage user ratings of Koha biblios
50 Ratings.pm provides simple functionality for a user to 'rate' a biblio, and to retrieve a biblio's rating info
52 the 4 subroutines allow a user to add, delete modify and retrieve rating info for a biblio.
54 The rating can be from 1 to 5 stars, (5 stars being the highest rating)
58 Get a rating for a bib
59 my $rating_hashref = GetRating( $biblionumber, undef );
60 my $rating_hashref = GetRating( $biblionumber, $borrowernumber );
62 Add a rating for a bib
63 my $rating_hashref = AddRating( $biblionumber, $borrowernumber, $rating_value );
65 Mod a rating for a bib
66 my $rating_hashref = ModRating( $biblionumber, $borrowernumber, $rating_value );
68 Delete a rating for a bib
69 my $rating_hashref = DelRating( $biblionumber, $borrowernumber );
72 All subroutines in Ratings.pm return a hashref which contain 4 keys
74 for example, after executing this statement below...
76 my $rating_hashref = GetRating ( $biblionumber, $borrowernumber ) ;
78 $rating_hashref now contains a hashref that looks like this...
82 rating_avg_int => '2.3',
83 rating_total => '432',
87 they 4 keys returned in the hashref are...
89 rating_avg: average rating of a biblio
90 rating_avg_int: average rating of a biblio, rounded to 1dp
91 rating_total: total number of ratings of a biblio
92 rating_value: logged-in user's rating of a biblio
96 Please use bugs.koha-community.org for tracking bugs.
98 =head1 SOURCE AVAILABILITY
100 The source is available from the koha-community.org git server
101 L<http://git.koha-community.org>
105 Original code: Mason James <mtj@kohaaloha.com>
109 Copyright (c) 2011 Mason James <mtj@kohaaloha.com>
113 C4::Ratings is free software. You can redistribute it and/or
114 modify it under the same terms as Koha itself.
118 Mason James <mtj@kohaaloha.com>
119 Koha Dev Team <http://koha-community.org>
124 GetRating($biblionumber, [$borrowernumber])
126 Get a rating for a bib
127 my $rating_hashref = GetRating( $biblionumber, undef );
128 my $rating_hashref = GetRating( $biblionumber, $borrowernumber );
130 This returns the rating for the supplied biblionumber. It will also return
131 the rating that the supplied user gave to the provided biblio. If a particular
132 value can't be supplied, '0' is returned for that value.
136 A hashref containing:
140 =item * rating_avg - average rating of a biblio
142 =item * rating_avg_int - average rating of a biblio, rounded to 1dp
144 =item * rating_total - total number of ratings of a biblio
146 =item * rating_value - logged-in user's rating of a biblio
153 my ( $biblionumber, $borrowernumber ) = @_;
155 my $ratings = Koha::Database->new()->schema->resultset('Rating')->search(
157 biblionumber => $biblionumber,
161 my $sum = $ratings->get_column('rating_value')->sum();
162 my $total = $ratings->count();
164 my ( $avg, $avg_int ) = 0;
166 if ( $sum and $total ) {
167 eval { $avg = $sum / $total };
170 $avg_int = sprintf( "%.1f", $avg );
171 $avg = sprintf( "%.0f", $avg );
174 $rating_hash{rating_total} = $total || 0;
175 $rating_hash{rating_avg} = $avg || 0;
176 $rating_hash{rating_avg_int} = $avg_int || 0;
178 if ($borrowernumber) {
179 my $rating = Koha::Database->new()->schema->resultset('Rating')->find(
181 biblionumber => $biblionumber,
182 borrowernumber => $borrowernumber,
185 return unless $rating;
186 $rating_hash{'rating_value'} = $rating->rating_value();
189 $rating_hash{rating_value} = undef;
192 return \%rating_hash;
197 my $rating_hashref = AddRating( $biblionumber, $borrowernumber, $rating_value );
199 Add a rating for a bib
201 This adds or updates a rating for a particular user on a biblio. If the value
202 is 0, then the rating will be deleted. If the value is out of the range of
203 0-5, nothing will happen.
208 my ( $biblionumber, $borrowernumber, $rating_value ) = @_;
210 my $rating = Koha::Database->new()->schema->resultset('Rating')->create(
212 biblionumber => $biblionumber,
213 borrowernumber => $borrowernumber,
214 rating_value => $rating_value
218 return GetRating( $biblionumber, $borrowernumber );
223 my $rating_hashref = ModRating( $biblionumber, $borrowernumber, $rating_value );
225 Mod a rating for a bib
230 my ( $biblionumber, $borrowernumber, $rating_value ) = @_;
232 my $rating = Koha::Database->new()->schema->resultset('Rating')->find(
234 borrowernumber => $borrowernumber,
235 biblionumber => $biblionumber
239 $rating->update( { rating_value => $rating_value } );
241 return GetRating( $biblionumber, $borrowernumber );
246 my $rating_hashref = DelRating( $biblionumber, $borrowernumber );
248 Delete a rating for a bib
253 my ( $biblionumber, $borrowernumber ) = @_;
255 my $rating = Koha::Database->new()->schema->resultset('Rating')->find(
257 borrowernumber => $borrowernumber,
258 biblionumber => $biblionumber
262 $rating->delete() if $rating;
264 return GetRating($biblionumber);