Bug 14402: Add function purge_zero_balance_fees to C4/Accounts.pm
[koha.git] / C4 / Ratings.pm
index 43870f3..f869f69 100644 (file)
@@ -5,18 +5,18 @@ package C4::Ratings;
 #
 # This file is part of Koha.
 #
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 2 of the License, or (at your option) any later
-# version.
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
 #
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License along
-# with Koha; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 use strict;
 use warnings;
@@ -26,10 +26,12 @@ use POSIX;
 use C4::Debug;
 use C4::Context;
 
+use Koha::Database;
+
 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
 
 BEGIN {
-    $VERSION = 3.00;
+    $VERSION = 3.07.00.049;
     @ISA     = qw(Exporter);
 
     @EXPORT = qw(
@@ -70,7 +72,7 @@ Delete a rating for a bib
 
 All subroutines in Ratings.pm return a hashref which contain 4 keys
 
-for example, after executing this statment below...
+for example, after executing this statement below...
 
     my $rating_hashref = GetRating ( $biblionumber, $borrowernumber ) ;
 
@@ -137,8 +139,11 @@ A hashref containing:
 =over
 
 =item * rating_avg - average rating of a biblio
+
 =item * rating_avg_int - average rating of a biblio, rounded to 1dp
+
 =item * rating_total - total number of ratings of a biblio
+
 =item * rating_value - logged-in user's rating of a biblio
 
 =back
@@ -147,41 +152,44 @@ A hashref containing:
 
 sub GetRating {
     my ( $biblionumber, $borrowernumber ) = @_;
-    my $query = qq| SELECT COUNT(*) AS total, SUM(rating_value) AS sum
-FROM ratings WHERE biblionumber = ? |;
 
-    my $sth = C4::Context->dbh->prepare($query);
-    $sth->execute($biblionumber);
-    my $res = $sth->fetchrow_hashref();
+    my $ratings = Koha::Database->new()->schema->resultset('Rating')->search(
+        {
+            biblionumber => $biblionumber,
+        }
+    );
+
+    my $sum   = $ratings->get_column('rating_value')->sum();
+    my $total = $ratings->count();
 
     my ( $avg, $avg_int ) = 0;
 
-    if ( $res->{sum} and $res->{total} ) {
-        eval { $avg = $res->{sum} / $res->{total} };
+    if ( $sum and $total ) {
+        eval { $avg = $sum / $total };
     }
 
     $avg_int = sprintf( "%.1f", $avg );
     $avg     = sprintf( "%.0f", $avg );
 
     my %rating_hash;
-    $rating_hash{rating_total}   = $res->{total} || 0;
-    $rating_hash{rating_avg}     = $avg || 0;
-    $rating_hash{rating_avg_int} = $avg_int ||0;
+    $rating_hash{rating_total}   = $total   || 0;
+    $rating_hash{rating_avg}     = $avg     || 0;
+    $rating_hash{rating_avg_int} = $avg_int || 0;
 
     if ($borrowernumber) {
-        my $q2 = qq|
-SELECT rating_value FROM ratings WHERE biblionumber = ? AND borrowernumber = ?|;
-        my $sth1 = C4::Context->dbh->prepare($q2);
-        $sth1->execute( $biblionumber, $borrowernumber );
-        my $res1 = $sth1->fetchrow_hashref();
-        $rating_hash{'rating_value'} = $res1->{"rating_value"};
+        my $rating = Koha::Database->new()->schema->resultset('Rating')->find(
+            {
+                biblionumber   => $biblionumber,
+                borrowernumber => $borrowernumber,
+            }
+        );
+        return unless $rating;
+        $rating_hash{'rating_value'} = $rating->rating_value();
     }
     else {
-        $rating_hash{rating_borrowernumber} = undef;
         $rating_hash{rating_value}          = undef;
     }
 
-#### %rating_hash
     return \%rating_hash;
 }
 
@@ -199,13 +207,16 @@ is 0, then the rating will be deleted. If the value is out of the range of
 
 sub AddRating {
     my ( $biblionumber, $borrowernumber, $rating_value ) = @_;
-    my $query =
-      qq| INSERT INTO ratings (borrowernumber,biblionumber,rating_value)
-        VALUES (?,?,?)|;
-    my $sth = C4::Context->dbh->prepare($query);
-    $sth->execute( $borrowernumber, $biblionumber, $rating_value );
-    my $rating = GetRating( $biblionumber, $borrowernumber );
-    return $rating;
+
+    my $rating = Koha::Database->new()->schema->resultset('Rating')->create(
+        {
+            biblionumber   => $biblionumber,
+            borrowernumber => $borrowernumber,
+            rating_value   => $rating_value
+        }
+    );
+
+    return GetRating( $biblionumber, $borrowernumber );
 }
 
 =head2 ModRating
@@ -218,12 +229,17 @@ Mod a rating for a bib
 
 sub ModRating {
     my ( $biblionumber, $borrowernumber, $rating_value ) = @_;
-    my $query =
-qq|UPDATE ratings SET rating_value = ? WHERE borrowernumber = ? AND biblionumber = ?|;
-    my $sth = C4::Context->dbh->prepare($query);
-    $sth->execute( $rating_value, $borrowernumber, $biblionumber );
-    my $rating = GetRating( $biblionumber, $borrowernumber );
-    return $rating;
+
+    my $rating = Koha::Database->new()->schema->resultset('Rating')->find(
+        {
+            borrowernumber => $borrowernumber,
+            biblionumber   => $biblionumber
+        }
+    );
+
+    $rating->update( { rating_value => $rating_value } );
+
+    return GetRating( $biblionumber, $borrowernumber );
 }
 
 =head2 DelRating
@@ -236,13 +252,17 @@ Delete a rating for a bib
 
 sub DelRating {
     my ( $biblionumber, $borrowernumber ) = @_;
-    my $dbh = C4::Context->dbh;
-    my $query =
-      "delete from ratings where borrowernumber = ? and biblionumber = ?";
-    my $sth    = C4::Context->dbh->prepare($query);
-    my $rv     = $sth->execute( $borrowernumber, $biblionumber );
-    my $rating = GetRating( $biblionumber, undef );
-    return $rating;
+
+    my $rating = Koha::Database->new()->schema->resultset('Rating')->find(
+        {
+            borrowernumber => $borrowernumber,
+            biblionumber   => $biblionumber
+        }
+    );
+
+    $rating->delete() if $rating;
+
+    return GetRating($biblionumber);
 }
 
 1;