Bug 10513: display a warning/message when returning a chosen item type
[koha.git] / C4 / Review.pm
index 6d3f6c8..dd989ed 100644 (file)
@@ -13,22 +13,24 @@ package C4::Review;
 # 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., 59 Temple Place,
-# Suite 330, Boston, MA  02111-1307 USA
+# 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.
 
 use strict;
+use warnings;
+
 use C4::Context;
 
 use vars qw($VERSION @ISA @EXPORT);
 
 BEGIN {
        # set the version for version checking
-       $VERSION = 3.00;
+    $VERSION = 3.07.00.049;
        require Exporter;
        @ISA    = qw(Exporter);
-       @EXPORT = qw(getreview savereview updatereview numberofreviews
-               getreviews getallreviews approvereview deletereview);
+       @EXPORT = qw(getreview savereview updatereview numberofreviews numberofreviewsbybiblionumber
+               getreviews getallreviews approvereview unapprovereview deletereview);
 }
 
 =head1 NAME
@@ -42,7 +44,8 @@ C4::Review - Perl Module containing routines for dealing with reviews of items
   my $review=getreview($biblionumber,$borrowernumber);
   savereview($biblionumber,$borrowernumber,$review);
   updatereview($biblionumber,$borrowernumber,$review);
-  my $count=numberofreviews($biblionumber);
+  my $count=numberofreviews($status);
+  my $count=numberofreviewsbybiblionumber($biblionumber);
   my $reviews=getreviews($biblionumber);
   my $reviews=getallreviews($status);
 
@@ -67,9 +70,7 @@ sub getreview {
       "SELECT * FROM reviews WHERE biblionumber=? and borrowernumber=?";
     my $sth = $dbh->prepare($query);
     $sth->execute( $biblionumber, $borrowernumber );
-    my $review = $sth->fetchrow_hashref();
-    $sth->finish();
-    return $review;
+    return $sth->fetchrow_hashref();
 }
 
 sub savereview {
@@ -77,33 +78,38 @@ sub savereview {
     my $dbh   = C4::Context->dbh;
     my $query = "INSERT INTO reviews (borrowernumber,biblionumber,
        review,approved,datereviewed) VALUES 
-  (?,?,?,?,now())";
+  (?,?,?,0,now())";
     my $sth = $dbh->prepare($query);
-    $sth->execute( $borrowernumber, $biblionumber, $review, 0 );
-    $sth->finish();
+    $sth->execute( $borrowernumber, $biblionumber, $review);
 }
 
 sub updatereview {
     my ( $biblionumber, $borrowernumber, $review ) = @_;
     my $dbh   = C4::Context->dbh;
-    my $query = "UPDATE reviews SET review=?,datereviewed=now(),approved=?
-  WHERE borrowernumber=? and biblionumber=?";
+    my $query = "UPDATE reviews SET review=?,datereviewed=now(),approved=0  WHERE borrowernumber=? and biblionumber=?";
     my $sth = $dbh->prepare($query);
-    $sth->execute( $review, 0, $borrowernumber, $biblionumber );
-    $sth->finish();
+    $sth->execute( $review, $borrowernumber, $biblionumber );
 }
 
 sub numberofreviews {
+    my ($param) = @_;
+    my $status = (defined($param) ? $param : 1);
+    my $dbh            = C4::Context->dbh;
+    my $query          =
+      "SELECT count(*) FROM reviews WHERE approved=?";
+    my $sth = $dbh->prepare($query);
+    $sth->execute( $status );
+  return $sth->fetchrow;
+}
+
+sub numberofreviewsbybiblionumber {
     my ($biblionumber) = @_;
     my $dbh            = C4::Context->dbh;
     my $query          =
       "SELECT count(*) FROM reviews WHERE biblionumber=? and approved=?";
     my $sth = $dbh->prepare($query);
     $sth->execute( $biblionumber, 1 );
-    my $count = $sth->fetchrow_hashref;
-
-    $sth->finish();
-    return ( $count->{'count(*)'} );
+       return $sth->fetchrow;
 }
 
 sub getreviews {
@@ -113,27 +119,18 @@ sub getreviews {
 "SELECT * FROM reviews WHERE biblionumber=? and approved=? order by datereviewed desc";
     my $sth = $dbh->prepare($query) || warn $dbh->err_str;
     $sth->execute( $biblionumber, $approved );
-    my @results;
-    while ( my $data = $sth->fetchrow_hashref() ) {
-        push @results, $data;
-    }
-    $sth->finish();
-    return ( \@results );
+       return $sth->fetchall_arrayref({});
 }
 
 sub getallreviews {
-    my ($status) = @_;
+    my ($status, $offset, $row_count) = @_;
+    my @params = ($status,($offset ? $offset : 0),($row_count ? $row_count : 20));
     my $dbh      = C4::Context->dbh;
     my $query    =
-      "SELECT * FROM reviews WHERE approved=? order by datereviewed desc";
-    my $sth = $dbh->prepare($query);
-    $sth->execute($status);
-    my @results;
-    while ( my $data = $sth->fetchrow_hashref() ) {
-        push @results, $data;
-    }
-    $sth->finish();
-    return ( \@results );
+      "SELECT * FROM reviews WHERE approved=? order by datereviewed desc LIMIT ?, ?";
+    my $sth = $dbh->prepare($query) || warn $dbh->err_str;
+    $sth->execute(@params);
+       return $sth->fetchall_arrayref({});
 }
 
 =head2 approvereview
@@ -152,7 +149,24 @@ sub approvereview {
                WHERE reviewid=?";
     my $sth = $dbh->prepare($query);
     $sth->execute( 1, $reviewid );
-    $sth->finish();
+}
+
+=head2 unapprovereview
+
+  unapprovereview($reviewid);
+
+Takes a reviewid and marks that review as not approved
+
+=cut
+
+sub unapprovereview {
+    my ($reviewid) = @_;
+    my $dbh        = C4::Context->dbh();
+    my $query      = "UPDATE reviews
+               SET approved=?
+               WHERE reviewid=?";
+    my $sth = $dbh->prepare($query);
+    $sth->execute( 0, $reviewid );
 }
 
 =head2 deletereview
@@ -170,7 +184,6 @@ sub deletereview {
                WHERE reviewid=?";
     my $sth = $dbh->prepare($query);
     $sth->execute($reviewid);
-    $sth->finish();
 }
 
 1;