Bug 16053: Add tests
[koha.git] / C4 / Review.pm
blob5c6f45aff1b404d41b454b71a3bee46e6eeaec5f
1 package C4::Review;
3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use strict;
21 use warnings;
23 use C4::Context;
25 use vars qw($VERSION @ISA @EXPORT);
27 BEGIN {
28 # set the version for version checking
29 $VERSION = 3.07.00.049;
30 require Exporter;
31 @ISA = qw(Exporter);
32 @EXPORT = qw(getreview savereview updatereview numberofreviews numberofreviewsbybiblionumber
33 getreviews getallreviews approvereview unapprovereview deletereview);
36 =head1 NAME
38 C4::Review - Perl Module containing routines for dealing with reviews of items
40 =head1 SYNOPSIS
42 use C4::Review;
44 my $review=getreview($biblionumber,$borrowernumber);
45 savereview($biblionumber,$borrowernumber,$review);
46 updatereview($biblionumber,$borrowernumber,$review);
47 my $count=numberofreviews($status);
48 my $count=numberofreviewsbybiblionumber($biblionumber);
49 my $reviews=getreviews($biblionumber, $status);
50 my $reviews=getallreviews($status, [$offset], [$row_count]);
52 =head1 DESCRIPTION
54 Review.pm provides many routines for manipulating reviews.
56 =head1 FUNCTIONS
58 =head2 getreview
60 $review = getreview($biblionumber,$borrowernumber);
62 Takes a borrowernumber and a biblionumber and returns the review of that biblio
64 =cut
66 sub getreview {
67 my ( $biblionumber, $borrowernumber ) = @_;
68 my $dbh = C4::Context->dbh;
69 my $query = "SELECT * FROM reviews WHERE biblionumber=? and borrowernumber=?";
70 my $sth = $dbh->prepare($query);
71 $sth->execute( $biblionumber, $borrowernumber );
72 return $sth->fetchrow_hashref();
75 =head2 savereview
77 savereview($biblionumber,$borrowernumber, $review);
79 Save a review in the 'reviews' database
81 =cut
83 sub savereview {
84 my ( $biblionumber, $borrowernumber, $review ) = @_;
85 my $dbh = C4::Context->dbh;
86 my $query = "INSERT INTO reviews (borrowernumber,biblionumber,
87 review,approved,datereviewed) VALUES
88 (?,?,?,0,now())";
89 my $sth = $dbh->prepare($query);
90 $sth->execute( $borrowernumber, $biblionumber, $review );
93 =head2 updatereview
95 updateview($biblionumber,$borrowernumber, $review);
97 Update the review description in the 'reviews' database
99 =cut
101 sub updatereview {
102 my ( $biblionumber, $borrowernumber, $review ) = @_;
103 my $dbh = C4::Context->dbh;
104 my $query = "UPDATE reviews SET review=?,datereviewed=now(),approved=0 WHERE borrowernumber=? and biblionumber=?";
105 my $sth = $dbh->prepare($query);
106 $sth->execute( $review, $borrowernumber, $biblionumber );
109 =head2 numberofreviews
111 my $count=numberofreviews( [$status] );
113 Return the number of reviews where in the 'reviews' database : 'approved' = $status
114 (By default $status = 1)
116 =cut
118 sub numberofreviews {
119 my ($param) = @_;
120 my $status = ( defined($param) ? $param : 1 );
121 my $dbh = C4::Context->dbh;
122 my $query = "SELECT count(*) FROM reviews WHERE approved=?";
123 my $sth = $dbh->prepare($query);
124 $sth->execute($status);
125 return $sth->fetchrow;
128 =head2 numberofreviewsbybiblionumber
130 my $count=numberofreviewsbybiblionumber($biblionumber);
132 Return the number of reviews approved for a given biblionumber
134 =cut
136 sub numberofreviewsbybiblionumber {
137 my ($biblionumber) = @_;
138 my $dbh = C4::Context->dbh;
139 my $query = "SELECT count(*) FROM reviews WHERE biblionumber=? and approved=?";
140 my $sth = $dbh->prepare($query);
141 $sth->execute( $biblionumber, 1 );
142 return $sth->fetchrow;
145 =head2 getreviews
147 my $reviews=getreviews($biblionumber, $status);
149 Return all reviews where in the 'reviews' database :
150 'biblionumber' = $biblionumber and 'approved' = $status
152 =cut
154 sub getreviews {
155 my ( $biblionumber, $approved ) = @_;
156 my $dbh = C4::Context->dbh;
157 my $query = "SELECT * FROM reviews WHERE biblionumber=? and approved=? order by datereviewed desc";
158 my $sth = $dbh->prepare($query);
159 $sth->execute( $biblionumber, $approved );
160 return $sth->fetchall_arrayref( {} );
163 =head2 getallreviews
165 my $reviews=getallreviews($status, [$offset], [$row_count]);
167 Return all reviews where in the 'reviews' database : 'approved' = $status
169 If offset and row_count are fiven, it's return all reviews between the
170 $offset position and the ($offset + $row_count) position.
171 (By default : $offset = 0 and $row_count = 20)
173 =cut
175 sub getallreviews {
176 my ( $status, $offset, $row_count ) = @_;
177 my @params = ( $status, ( $offset ? $offset : 0 ), ( $row_count ? $row_count : 20 ) );
178 my $dbh = C4::Context->dbh;
179 my $query = "SELECT * FROM reviews WHERE approved=? order by datereviewed desc LIMIT ?, ?";
180 my $sth = $dbh->prepare($query);
181 $sth->execute(@params);
182 return $sth->fetchall_arrayref( {} );
185 =head2 approvereview
187 approvereview($reviewid);
189 Takes a reviewid and marks that review approved
191 =cut
193 sub approvereview {
194 my ($reviewid) = @_;
195 my $dbh = C4::Context->dbh();
196 my $query = "UPDATE reviews
197 SET approved=?
198 WHERE reviewid=?";
199 my $sth = $dbh->prepare($query);
200 $sth->execute( 1, $reviewid );
203 =head2 unapprovereview
205 unapprovereview($reviewid);
207 Takes a reviewid and marks that review as not approved
209 =cut
211 sub unapprovereview {
212 my ($reviewid) = @_;
213 my $dbh = C4::Context->dbh();
214 my $query = "UPDATE reviews
215 SET approved=?
216 WHERE reviewid=?";
217 my $sth = $dbh->prepare($query);
218 $sth->execute( 0, $reviewid );
221 =head2 deletereview
223 deletereview($reviewid);
225 Takes a reviewid and deletes it
227 =cut
229 sub deletereview {
230 my ($reviewid) = @_;
231 my $dbh = C4::Context->dbh();
232 my $query = "DELETE FROM reviews
233 WHERE reviewid=?";
234 my $sth = $dbh->prepare($query);
235 $sth->execute($reviewid);
239 __END__
241 =head1 AUTHOR
243 Koha Team
245 =cut