bugfix
[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 C4::Output;
27 # use C4::Interface::CGI::Output;
28 use vars qw($VERSION @ISA @EXPORT);
29
30 # set the version for version checking
31 $VERSION = 0.01;
32
33 =head1 NAME
34
35 C4::Accounts - Functions for dealing with Koha authorities
36
37 =head1 SYNOPSIS
38
39   use C4::Suggestions;
40
41 =head1 DESCRIPTION
42
43 The functions in this module deal with the suggestions :
44 * in OPAC
45 * in librarian interface
46
47 A suggestion is done in the OPAC. It has the status "ASKED"
48 When a librarian manages the suggestion, he can set the status to "REJECTED" or "ORDERED".
49 When a book is ordered and arrived in the library, the status becomes "AVAILABLE"
50 All suggestions of a borrower by the borrower itself.
51 Suggestions done by other can be seen when not "AVAILABLE"
52
53 =head1 FUNCTIONS
54
55 =over 2
56
57 =cut
58
59 @ISA = qw(Exporter);
60 @EXPORT = qw(   &newsuggestion
61                                 &searchsuggestion
62                                 &getsuggestion
63                                 &delsuggestion
64                                 &countsuggestion
65                                 &changestatus
66                         );
67
68 =item SearchSuggestion
69
70   (\@array) = &SearchSuggestion($user)
71
72   searches for a suggestion
73
74 C<$user> is the user code (used as suggestor filter)
75
76 return :
77 C<\@array> : the suggestions found. Array of hash.
78 Note the status is stored twice :
79 * in the status field
80 * as parameter ( for example ASKED => 1, or REJECTED => 1) . This is for template & translation purposes.
81
82 =cut
83 sub searchsuggestion  {
84         my ($user,$author,$title,$publishercode,$status,$suggestedbyme)=@_;
85         my $dbh = C4::Context->dbh;
86         my $query="Select suggestions.*,
87                                                 U1.surname as surnamesuggestedby,U1.firstname as firstnamesuggestedby,
88                                                 U2.surname as surnamemanagedby,U2.firstname as firstnamemanagedby 
89                                                 from suggestions
90                                                 left join borrowers as U1 on suggestedby=U1.borrowernumber
91                                                 left join borrowers as U2  on managedby=U2.borrowernumber
92                                                 where 1=1";
93         my @sql_params;
94         if ($author) {
95                 push @sql_params,"%".$author."%";
96                 $query .= " and author like ?";
97         }
98         if ($title) {
99                 push @sql_params,"%".$title."%";
100                 $query .= " and suggestions.title like ?";
101         }
102         if ($publishercode) {
103                 push @sql_params,"%".$publishercode."%";
104                 $query .= " and publishercode like ?";
105         }
106         if ($status) {
107                 push @sql_params,$status;
108                 $query .= " and status=?";
109         }
110         if ($suggestedbyme) {
111                 push @sql_params,$user;
112                 $query .= " and suggestedby=?";
113         } else {
114                 $query .= " and managedby is NULL";
115         }
116         my $sth=$dbh->prepare($query);
117         $sth->execute(@sql_params);
118         my @results;
119         my $even=1; # the even variable is used to set even / odd lines, for highlighting
120         while (my $data=$sth->fetchrow_hashref){
121                         $data->{$data->{status}} = 1;
122                         if ($even) {
123                                 $even=0;
124                                 $data->{even}=1;
125                         } else {
126                                 $even=1;
127                         }
128                         push(@results,$data);
129         }
130         return (\@results);
131 }
132
133 sub newsuggestion {
134         my ($borrowernumber,$title,$author,$publishercode,$note,$copyrightdate,$volumedesc,$publicationyear,$place,$isbn,$biblionumber) = @_;
135         my $dbh = C4::Context->dbh;
136         my $sth = $dbh->prepare("insert into suggestions (status,suggestedby,title,author,publishercode,note,copyrightdate,volumedesc,publicationyear,place,isbn,biblionumber) values ('ASKED',?,?,?,?,?,?,?,?,?,?,?)");
137         $sth->execute($borrowernumber,$title,$author,$publishercode,$note,$copyrightdate,$volumedesc,$publicationyear,$place,$isbn,$biblionumber);
138 }
139
140 sub getsuggestion {
141         my ($suggestionid) = @_;
142         my $dbh = C4::Context->dbh;
143         my $sth = $dbh->prepare("select * from suggestions where suggestionid=?");
144         $sth->execute($suggestionid);
145         return($sth->fetchrow_hashref);
146 }
147
148 sub delsuggestion {
149         my ($borrowernumber,$suggestionid) = @_;
150         my $dbh = C4::Context->dbh;
151         # check that the suggestion comes from the suggestor
152         my $sth = $dbh->prepare("select suggestedby from suggestions where suggestionid=?");
153         $sth->execute($suggestionid);
154         my ($suggestedby) = $sth->fetchrow;
155         if ($suggestedby eq $borrowernumber) {
156                 $sth = $dbh->prepare("delete from suggestions where suggestionid=?");
157                 $sth->execute($suggestionid);
158         }
159 }
160
161 sub countsuggestion {
162         my ($status) = @_;
163         my $dbh = C4::Context->dbh;
164         my $sth = $dbh->prepare("select count(*) from suggestions where status=?");
165         $sth->execute($status);
166         my ($result) = $sth->fetchrow;
167         return $result;
168 }
169
170 sub changestatus {
171         my ($suggestionid,$status,$managedby) = @_;
172         my $dbh = C4::Context->dbh;
173         my $sth;
174         if ($managedby>0) {
175                 $sth = $dbh->prepare("update suggestions set status=?,managedby=? where suggestionid=?");
176                 $sth->execute($status,$managedby,$suggestionid);
177         } else {
178                 $sth = $dbh->prepare("update suggestions set status=? where suggestionid=?");
179                 $sth->execute($status,$suggestionid);
180
181         }
182         # check mail sending.
183         $sth = $dbh->prepare("select suggestions.*,
184                                                         boby.surname as bysurname, boby.firstname as byfirstname, boby.emailaddress as byemail,
185                                                         lib.surname as libsurname,lib.firstname as libfirstname,lib.emailaddress as libemail
186                                                 from suggestions left join borrowers as boby on boby.borrowernumber=suggestedby left join borrowers as lib on lib.borrowernumber=managedby where suggestionid=?");
187         $sth->execute($suggestionid);
188         my $emailinfo = $sth->fetchrow_hashref;
189         my $template = gettemplate("suggestion/mail_suggestion_$status.tmpl","intranet");
190 #                                query =>'',
191 #                            authnotrequired => 1,
192 #                        });
193         $template->param(byemail => $emailinfo->{byemail},
194                                         libemail => $emailinfo->{libemail},
195                                         status => $emailinfo->{status},
196                                         title => $emailinfo->{title},
197                                         author =>$emailinfo->{author},
198                                         libsurname => $emailinfo->{libsurname},
199                                         libfirstname => $emailinfo->{libfirstname},
200                                         byfirstname => $emailinfo->{byfirstname},
201                                         bysurname => $emailinfo->{bysurname},
202                                         );
203         warn "mailing => ".$template->output;
204 #       warn "sending email to $emailinfo->{byemail} from $emailinfo->{libemail} to notice new status $emailinfo->{status} for $emailinfo->{title} / $emailinfo->{author}";
205 }
206
207 =back
208
209 =head1 SEE ALSO
210
211 =cut