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 under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 use vars
qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
32 $VERSION = 3.07.00.049;
45 C4::Ratings - a module to manage user ratings of Koha biblios
49 Ratings.pm provides simple functionality for a user to 'rate' a biblio, and to retrieve a biblio's rating info
51 the 4 subroutines allow a user to add, delete modify and retrieve rating info for a biblio.
53 The rating can be from 1 to 5 stars, (5 stars being the highest rating)
57 Get a rating for a bib
58 my $rating_hashref = GetRating( $biblionumber, undef );
59 my $rating_hashref = GetRating( $biblionumber, $borrowernumber );
61 Add a rating for a bib
62 my $rating_hashref = AddRating( $biblionumber, $borrowernumber, $rating_value );
64 Mod a rating for a bib
65 my $rating_hashref = ModRating( $biblionumber, $borrowernumber, $rating_value );
67 Delete a rating for a bib
68 my $rating_hashref = DelRating( $biblionumber, $borrowernumber );
71 All subroutines in Ratings.pm return a hashref which contain 4 keys
73 for example, after executing this statment below...
75 my $rating_hashref = GetRating ( $biblionumber, $borrowernumber ) ;
77 $rating_hashref now contains a hashref that looks like this...
81 rating_avg_int => '2.3',
82 rating_total => '432',
86 they 4 keys returned in the hashref are...
88 rating_avg: average rating of a biblio
89 rating_avg_int: average rating of a biblio, rounded to 1dp
90 rating_total: total number of ratings of a biblio
91 rating_value: logged-in user's rating of a biblio
95 Please use bugs.koha-community.org for tracking bugs.
97 =head1 SOURCE AVAILABILITY
99 The source is available from the koha-community.org git server
100 L<http://git.koha-community.org>
104 Original code: Mason James <mtj@kohaaloha.com>
108 Copyright (c) 2011 Mason James <mtj@kohaaloha.com>
112 C4::Ratings is free software. You can redistribute it and/or
113 modify it under the same terms as Koha itself.
117 Mason James <mtj@kohaaloha.com>
118 Koha Dev Team <http://koha-community.org>
123 GetRating($biblionumber, [$borrowernumber])
125 Get a rating for a bib
126 my $rating_hashref = GetRating( $biblionumber, undef );
127 my $rating_hashref = GetRating( $biblionumber, $borrowernumber );
129 This returns the rating for the supplied biblionumber. It will also return
130 the rating that the supplied user gave to the provided biblio. If a particular
131 value can't be supplied, '0' is returned for that value.
135 A hashref containing:
139 =item * rating_avg - average rating of a biblio
140 =item * rating_avg_int - average rating of a biblio, rounded to 1dp
141 =item * rating_total - total number of ratings of a biblio
142 =item * rating_value - logged-in user's rating of a biblio
149 my ( $biblionumber, $borrowernumber ) = @_;
151 my $ratings = Koha
::Database
->new()->schema->resultset('Rating')->search(
153 biblionumber
=> $biblionumber,
157 my $sum = $ratings->get_column('rating_value')->sum();
158 my $total = $ratings->count();
160 my ( $avg, $avg_int ) = 0;
162 if ( $sum and $total ) {
163 eval { $avg = $sum / $total };
166 $avg_int = sprintf( "%.1f", $avg );
167 $avg = sprintf( "%.0f", $avg );
170 $rating_hash{rating_total
} = $total || 0;
171 $rating_hash{rating_avg
} = $avg || 0;
172 $rating_hash{rating_avg_int
} = $avg_int || 0;
174 if ($borrowernumber) {
175 my $rating = Koha
::Database
->new()->schema->resultset('Rating')->find(
177 biblionumber
=> $biblionumber,
178 borrowernumber
=> $borrowernumber,
181 return unless $rating;
182 $rating_hash{'rating_value'} = $rating->rating_value();
185 $rating_hash{rating_value
} = undef;
188 return \
%rating_hash;
193 my $rating_hashref = AddRating( $biblionumber, $borrowernumber, $rating_value );
195 Add a rating for a bib
197 This adds or updates a rating for a particular user on a biblio. If the value
198 is 0, then the rating will be deleted. If the value is out of the range of
199 0-5, nothing will happen.
204 my ( $biblionumber, $borrowernumber, $rating_value ) = @_;
206 my $rating = Koha
::Database
->new()->schema->resultset('Rating')->create(
208 biblionumber
=> $biblionumber,
209 borrowernumber
=> $borrowernumber,
210 rating_value
=> $rating_value
214 return GetRating
( $biblionumber, $borrowernumber );
219 my $rating_hashref = ModRating( $biblionumber, $borrowernumber, $rating_value );
221 Mod a rating for a bib
226 my ( $biblionumber, $borrowernumber, $rating_value ) = @_;
228 my $rating = Koha
::Database
->new()->schema->resultset('Rating')->find(
230 borrowernumber
=> $borrowernumber,
231 biblionumber
=> $biblionumber
235 $rating->update( { rating_value
=> $rating_value } );
237 return GetRating
( $biblionumber, $borrowernumber );
242 my $rating_hashref = DelRating( $biblionumber, $borrowernumber );
244 Delete a rating for a bib
249 my ( $biblionumber, $borrowernumber ) = @_;
251 my $rating = Koha
::Database
->new()->schema->resultset('Rating')->find(
253 borrowernumber
=> $borrowernumber,
254 biblionumber
=> $biblionumber
258 $rating->delete() if $rating;
260 return GetRating
($biblionumber);