Bug 17089: Koha::Ratings - Remove ModRating
[koha.git] / C4 / Ratings.pm
blob0ec6e0364ea598bae9c3ac7ca8017fc0f9a7612a
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(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
33 BEGIN {
34 @ISA = qw(Exporter);
36 @EXPORT = qw(
37 &GetRating
38 &DelRating
42 =head1 NAME
44 C4::Ratings - a module to manage user ratings of Koha biblios
46 =head1 DESCRIPTION
48 Ratings.pm provides simple functionality for a user to 'rate' a biblio, and to retrieve a biblio's rating info
50 the 4 subroutines allow a user to add, delete modify and retrieve rating info for a biblio.
52 The rating can be from 1 to 5 stars, (5 stars being the highest rating)
54 =head1 SYNOPSIS
56 Get a rating for a bib
57 my $rating_hashref = GetRating( $biblionumber, undef );
58 my $rating_hashref = GetRating( $biblionumber, $borrowernumber );
60 Delete a rating for a bib
61 my $rating_hashref = DelRating( $biblionumber, $borrowernumber );
64 All subroutines in Ratings.pm return a hashref which contain 4 keys
66 for example, after executing this statement below...
68 my $rating_hashref = GetRating ( $biblionumber, $borrowernumber ) ;
70 $rating_hashref now contains a hashref that looks like this...
72 $rating = {
73 rating_avg => '2',
74 rating_avg_int => '2.3',
75 rating_total => '432',
76 rating_value => '5'
79 they 4 keys returned in the hashref are...
81 rating_avg: average rating of a biblio
82 rating_avg_int: average rating of a biblio, rounded to 1dp
83 rating_total: total number of ratings of a biblio
84 rating_value: logged-in user's rating of a biblio
86 =head1 BUGS
88 Please use bugs.koha-community.org for tracking bugs.
90 =head1 SOURCE AVAILABILITY
92 The source is available from the koha-community.org git server
93 L<http://git.koha-community.org>
95 =head1 AUTHOR
97 Original code: Mason James <mtj@kohaaloha.com>
99 =head1 COPYRIGHT
101 Copyright (c) 2011 Mason James <mtj@kohaaloha.com>
103 =head1 LICENSE
105 C4::Ratings is free software. You can redistribute it and/or
106 modify it under the same terms as Koha itself.
108 =head1 CREDITS
110 Mason James <mtj@kohaaloha.com>
111 Koha Dev Team <http://koha-community.org>
114 =head2 GetRating
116 GetRating($biblionumber, [$borrowernumber])
118 Get a rating for a bib
119 my $rating_hashref = GetRating( $biblionumber, undef );
120 my $rating_hashref = GetRating( $biblionumber, $borrowernumber );
122 This returns the rating for the supplied biblionumber. It will also return
123 the rating that the supplied user gave to the provided biblio. If a particular
124 value can't be supplied, '0' is returned for that value.
126 =head3 RETURNS
128 A hashref containing:
130 =over
132 =item * rating_avg - average rating of a biblio
134 =item * rating_avg_int - average rating of a biblio, rounded to 1dp
136 =item * rating_total - total number of ratings of a biblio
138 =item * rating_value - logged-in user's rating of a biblio
140 =back
142 =cut
144 sub GetRating {
145 my ( $biblionumber, $borrowernumber ) = @_;
147 my $ratings = Koha::Database->new()->schema->resultset('Rating')->search(
149 biblionumber => $biblionumber,
153 my $sum = $ratings->get_column('rating_value')->sum();
154 my $total = $ratings->count();
156 my ( $avg, $avg_int ) = 0;
158 if ( $sum and $total ) {
159 eval { $avg = $sum / $total };
162 $avg_int = sprintf( "%.1f", $avg );
163 $avg = sprintf( "%.0f", $avg );
165 my %rating_hash;
166 $rating_hash{rating_total} = $total || 0;
167 $rating_hash{rating_avg} = $avg || 0;
168 $rating_hash{rating_avg_int} = $avg_int || 0;
170 if ($borrowernumber) {
171 my $rating = Koha::Database->new()->schema->resultset('Rating')->find(
173 biblionumber => $biblionumber,
174 borrowernumber => $borrowernumber,
177 return unless $rating;
178 $rating_hash{'rating_value'} = $rating->rating_value();
180 else {
181 $rating_hash{rating_value} = undef;
184 return \%rating_hash;
187 =head2 DelRating
189 my $rating_hashref = DelRating( $biblionumber, $borrowernumber );
191 Delete a rating for a bib
193 =cut
195 sub DelRating {
196 my ( $biblionumber, $borrowernumber ) = @_;
198 my $rating = Koha::Database->new()->schema->resultset('Rating')->find(
200 borrowernumber => $borrowernumber,
201 biblionumber => $biblionumber
205 $rating->delete() if $rating;
207 return GetRating($biblionumber);
211 __END__