adding suggestions in OPAC
[koha.git] / C4 / Suggestions.pm
1 package C4::Suggestions;
2
3 # $Id$
4
5 # Copyright 2000-2002 Katipo Communications
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
12 # version.
13 #
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA  02111-1307 USA
21
22 use strict;
23 require Exporter;
24 use DBI;
25 use C4::Context;
26 use vars qw($VERSION @ISA @EXPORT);
27
28 # set the version for version checking
29 $VERSION = 0.01;
30
31 =head1 NAME
32
33 C4::Accounts - Functions for dealing with Koha authorities
34
35 =head1 SYNOPSIS
36
37   use C4::Suggestions;
38
39 =head1 DESCRIPTION
40
41 The functions in this module deal with the suggestions :
42 * in OPAC
43 * in librarian interface
44
45 A suggestion is done in the OPAC. It has the status "ASKED"
46 When a librarian manages the suggestion, he can set the status to "REJECTED" or "ORDERED".
47 When a book is ordered and arrived in the library, the status becomes "AVAILABLE"
48 All suggestions of a borrower by the borrower itself.
49 Suggestions done by other can be seen when not "AVAILABLE"
50
51 =head1 FUNCTIONS
52
53 =over 2
54
55 =cut
56
57 @ISA = qw(Exporter);
58 @EXPORT = qw(   &newsuggestion
59                                 &searchsuggestion
60                                 &delsuggestion
61                         );
62
63 =item SearchSuggestion
64
65   (\@array) = &SearchSuggestion($user)
66
67   searches for a suggestion
68
69 C<$user> is the user code (used as suggestor filter)
70
71 return :
72 C<\@array> : the suggestions found. Array of hash.
73 Note the status is stored twice :
74 * in the status field
75 * as parameter ( for example ASKED => 1, or REJECTED => 1) . This is for template & translation purposes.
76
77 =cut
78 sub searchsuggestion  {
79         my ($user,$author,$title,$publishercode,$status,$suggestedbyme)=@_;
80         my $dbh = C4::Context->dbh;
81         my $query="Select suggestions.*,
82                                                 U1.surname as surnamesuggestedby,U1.firstname as firstnamesuggestedby,
83                                                 U2.surname as surnamemanagedby,U2.firstname as firstnamemanagedby 
84                                                 from suggestions,borrowers as U1 
85                                                 left join borrowers as U2  on managedby=U2.borrowernumber
86                                                 where suggestedby=U1.borrowernumber";
87         my @sql_params;
88         if ($author) {
89                 push @sql_params,"%".$author."%";
90                 $query .= " and author like ?";
91         }
92         if ($title) {
93                 push @sql_params,"%".$title."%";
94                 $query .= " and suggestions.title like ?";
95         }
96         if ($publishercode) {
97                 push @sql_params,"%".$publishercode."%";
98                 $query .= " and publishercode like ?";
99         }
100         if ($status) {
101                 push @sql_params,$status;
102                 $query .= " and status=?";
103         }
104         if ($suggestedbyme) {
105                 push @sql_params,$user;
106                 $query .= " and suggestedby=?";
107         } else {
108                 $query .= " and managedby is NULL";
109         }
110         my $sth=$dbh->prepare($query);
111         $sth->execute(@sql_params);
112         my @results;
113         my $even=1; # the even variable is used to set even / odd lines, for highlighting
114         while (my $data=$sth->fetchrow_hashref){
115                         $data->{$data->{status}} = 1;
116                         if ($even) {
117                                 $even=0;
118                                 $data->{even}=1;
119                         } else {
120                                 $even=1;
121                         }
122                         push(@results,$data);
123         }
124         return (\@results);
125 }
126
127 sub newsuggestion {
128         my ($borrowernumber,$title,$author,$publishercode,$note) = @_;
129         my $dbh = C4::Context->dbh;
130         my $sth = $dbh->prepare("insert into suggestions (suggestedby,title,author,publishercode,note) values (?,?,?,?,?)");
131         $sth->execute($borrowernumber,$title,$author,$publishercode,$note);
132 }
133
134 sub delsuggestion {
135         my ($borrowernumber,$suggestionnumber) = @_;
136         my $dbh = C4::Context->dbh;
137         # check that the suggestion comes from the suggestor
138         my $sth = $dbh->prepare("select suggestedby from suggestions where suggestionnumber=?");
139         $sth->execute($suggestionnumber);
140         my ($suggestedby) = $sth->fetchrow;
141         if ($suggestedby eq $borrowernumber) {
142                 $sth = $dbh->prepare("delete from suggestions where suggestionnumber=?");
143                 $sth->execute($suggestionnumber);
144         }
145 }
146
147 =back
148
149 =head1 SEE ALSO
150
151 =cut