Bug 16011: $VERSION - Remove the $VERSION init
[koha.git] / C4 / Review.pm
1 package C4::Review;
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19
20 use strict;
21 use warnings;
22
23 use C4::Context;
24
25 use vars qw(@ISA @EXPORT);
26
27 BEGIN {
28     # set the version for version checking
29     require Exporter;
30     @ISA    = qw(Exporter);
31     @EXPORT = qw(getreview savereview updatereview numberofreviews numberofreviewsbybiblionumber
32       getreviews getallreviews approvereview unapprovereview deletereview);
33 }
34
35 =head1 NAME
36
37 C4::Review - Perl Module containing routines for dealing with reviews of items
38
39 =head1 SYNOPSIS
40
41   use C4::Review;
42
43   my $review=getreview($biblionumber,$borrowernumber);
44   savereview($biblionumber,$borrowernumber,$review);
45   updatereview($biblionumber,$borrowernumber,$review);
46   my $count=numberofreviews($status);
47   my $count=numberofreviewsbybiblionumber($biblionumber);
48   my $reviews=getreviews($biblionumber, $status);
49   my $reviews=getallreviews($status, [$offset], [$row_count]);
50
51 =head1 DESCRIPTION
52
53 Review.pm provides many routines for manipulating reviews.
54
55 =head1 FUNCTIONS
56
57 =head2 getreview
58
59   $review = getreview($biblionumber,$borrowernumber);
60
61 Takes a borrowernumber and a biblionumber and returns the review of that biblio
62
63 =cut
64
65 sub getreview {
66     my ( $biblionumber, $borrowernumber ) = @_;
67     my $dbh   = C4::Context->dbh;
68     my $query = "SELECT * FROM reviews WHERE biblionumber=? and borrowernumber=?";
69     my $sth   = $dbh->prepare($query);
70     $sth->execute( $biblionumber, $borrowernumber );
71     return $sth->fetchrow_hashref();
72 }
73
74 =head2 savereview
75
76   savereview($biblionumber,$borrowernumber, $review);
77
78 Save a review in the 'reviews' database
79
80 =cut
81
82 sub savereview {
83     my ( $biblionumber, $borrowernumber, $review ) = @_;
84     my $dbh   = C4::Context->dbh;
85     my $query = "INSERT INTO reviews (borrowernumber,biblionumber,
86   review,approved,datereviewed) VALUES
87   (?,?,?,0,now())";
88     my $sth = $dbh->prepare($query);
89     $sth->execute( $borrowernumber, $biblionumber, $review );
90 }
91
92 =head2 updatereview
93
94   updateview($biblionumber,$borrowernumber, $review);
95
96 Update the review description in the 'reviews' database
97
98 =cut
99
100 sub updatereview {
101     my ( $biblionumber, $borrowernumber, $review ) = @_;
102     my $dbh   = C4::Context->dbh;
103     my $query = "UPDATE reviews SET review=?,datereviewed=now(),approved=0  WHERE borrowernumber=? and biblionumber=?";
104     my $sth   = $dbh->prepare($query);
105     $sth->execute( $review, $borrowernumber, $biblionumber );
106 }
107
108 =head2 numberofreviews
109
110   my $count=numberofreviews( [$status] );
111
112 Return the number of reviews where in the 'reviews' database : 'approved' = $status
113 (By default $status = 1)
114
115 =cut
116
117 sub numberofreviews {
118     my ($param) = @_;
119     my $status = ( defined($param) ? $param : 1 );
120     my $dbh    = C4::Context->dbh;
121     my $query  = "SELECT count(*) FROM reviews WHERE approved=?";
122     my $sth    = $dbh->prepare($query);
123     $sth->execute($status);
124     return $sth->fetchrow;
125 }
126
127 =head2 numberofreviewsbybiblionumber
128
129   my $count=numberofreviewsbybiblionumber($biblionumber);
130
131 Return the number of reviews approved for a given biblionumber
132
133 =cut
134
135 sub numberofreviewsbybiblionumber {
136     my ($biblionumber) = @_;
137     my $dbh            = C4::Context->dbh;
138     my $query          = "SELECT count(*) FROM reviews WHERE biblionumber=? and approved=?";
139     my $sth            = $dbh->prepare($query);
140     $sth->execute( $biblionumber, 1 );
141     return $sth->fetchrow;
142 }
143
144 =head2 getreviews
145
146   my $reviews=getreviews($biblionumber, $status);
147
148 Return all reviews where in the 'reviews' database :
149 'biblionumber' = $biblionumber and 'approved' = $status
150
151 =cut
152
153 sub getreviews {
154     my ( $biblionumber, $approved ) = @_;
155     my $dbh   = C4::Context->dbh;
156     my $query = "SELECT * FROM reviews WHERE biblionumber=? and approved=? order by datereviewed desc";
157     my $sth   = $dbh->prepare($query);
158     $sth->execute( $biblionumber, $approved );
159     return $sth->fetchall_arrayref( {} );
160 }
161
162 =head2 getallreviews
163
164   my $reviews=getallreviews($status, [$offset], [$row_count]);
165
166 Return all reviews where in the 'reviews' database : 'approved' = $status
167
168 If offset and row_count are fiven, it's return all reviews between the
169 $offset position and the ($offset + $row_count) position.
170 (By default : $offset = 0 and $row_count = 20)
171
172 =cut
173
174 sub getallreviews {
175     my ( $status, $offset, $row_count ) = @_;
176     my @params = ( $status, ( $offset ? $offset : 0 ), ( $row_count ? $row_count : 20 ) );
177     my $dbh    = C4::Context->dbh;
178     my $query  = "SELECT * FROM reviews WHERE approved=? order by datereviewed desc LIMIT ?, ?";
179     my $sth    = $dbh->prepare($query);
180     $sth->execute(@params);
181     return $sth->fetchall_arrayref( {} );
182 }
183
184 =head2 approvereview
185
186   approvereview($reviewid);
187
188 Takes a reviewid and marks that review approved
189
190 =cut
191
192 sub approvereview {
193     my ($reviewid) = @_;
194     my $dbh        = C4::Context->dbh();
195     my $query      = "UPDATE reviews
196                SET approved=?
197                WHERE reviewid=?";
198     my $sth = $dbh->prepare($query);
199     $sth->execute( 1, $reviewid );
200 }
201
202 =head2 unapprovereview
203
204   unapprovereview($reviewid);
205
206 Takes a reviewid and marks that review as not approved
207
208 =cut
209
210 sub unapprovereview {
211     my ($reviewid) = @_;
212     my $dbh        = C4::Context->dbh();
213     my $query      = "UPDATE reviews
214                SET approved=?
215                WHERE reviewid=?";
216     my $sth = $dbh->prepare($query);
217     $sth->execute( 0, $reviewid );
218 }
219
220 =head2 deletereview
221
222   deletereview($reviewid);
223
224 Takes a reviewid and deletes it
225
226 =cut
227
228 sub deletereview {
229     my ($reviewid) = @_;
230     my $dbh        = C4::Context->dbh();
231     my $query      = "DELETE FROM reviews
232                WHERE reviewid=?";
233     my $sth = $dbh->prepare($query);
234     $sth->execute($reviewid);
235 }
236
237 1;
238 __END__
239
240 =head1 AUTHOR
241
242 Koha Team
243
244 =cut