New set of routines for HEAD.
[koha.git] / C4 / Suggestions.pm
1 package C4::Suggestions;
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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 # $Id$
21
22 use strict;
23 require Exporter;
24 use C4::Context;
25 use C4::Output;
26 use Mail::Sendmail;
27 use vars qw($VERSION @ISA @EXPORT);
28
29 # set the version for version checking
30 $VERSION = do { my @v = '$Revision$' =~ /\d+/g;
31   shift(@v) . "." . join("_", map {sprintf "%03d", $_ } @v); };
32
33 =head1 NAME
34
35 C4::Suggestions - Some useful functions for dealings with suggestions.
36
37 =head1 SYNOPSIS
38
39 use C4::Suggestions;
40
41 =head1 DESCRIPTION
42
43 =over 4
44
45 The functions in this module deal with the suggestions in OPAC and in librarian interface
46
47 A suggestion is done in the OPAC. It has the status "ASKED"
48
49 When a librarian manages the suggestion, he can set the status to "REJECTED" or "ACCEPTED".
50
51 When the book is ordered, the suggestion status becomes "ORDERED"
52
53 When a book is ordered and arrived in the library, the status becomes "AVAILABLE"
54
55 All suggestions of a borrower can be seen by the borrower itself.
56 Suggestions done by other borrowers can be seen when not "AVAILABLE"
57
58 =back
59
60 =head1 FUNCTIONS
61
62 =cut
63
64 @ISA = qw(Exporter);
65 @EXPORT = qw(
66     &NewSuggestion
67     &SearchSuggestion
68     &GetSuggestion
69     &DelSuggestion
70     &CountSuggestion
71     &ModStatus
72     &ConnectSuggestionAndBiblio
73     &GetSuggestionFromBiblionumber
74  );
75
76 =head2 SearchSuggestion
77
78 =over 4
79
80 (\@array) = &SearchSuggestion($user,$author,$title,$publishercode,$status,$suggestedbyme)
81
82 searches for a suggestion
83
84 return :
85 C<\@array> : the suggestions found. Array of hash.
86 Note the status is stored twice :
87 * in the status field
88 * as parameter ( for example ASKED => 1, or REJECTED => 1) . This is for template & translation purposes.
89
90 =back
91
92 =cut
93
94 sub SearchSuggestion  {
95     my ($user,$author,$title,$publishercode,$status,$suggestedbyme)=@_;
96     my $dbh = C4::Context->dbh;
97     my $query = qq|
98     SELECT suggestions.*,
99         U1.surname   AS surnamesuggestedby,
100         U1.firstname AS firstnamesuggestedby,
101         U2.surname   AS surnamemanagedby,
102         U2.firstname AS firstnamemanagedby
103     FROM suggestions
104     LEFT JOIN borrowers AS U1 ON suggestedby=U1.borrowernumber
105     LEFT JOIN borrowers AS U2 ON managedby=U2.borrowernumber
106     WHERE 1=1 |;
107
108     my @sql_params;
109     if ($author) {
110        push @sql_params,"%".$author."%";
111        $query .= " and author like ?";
112     }
113     if ($title) {
114         push @sql_params,"%".$title."%";
115         $query .= " and suggestions.title like ?";
116     }
117     if ($publishercode) {
118         push @sql_params,"%".$publishercode."%";
119         $query .= " and publishercode like ?";
120     }
121     if ($status) {
122         push @sql_params,$status;
123         $query .= " and status=?";
124     }
125
126     if (C4::Context->preference("IndependantBranches")) {
127         my $userenv = C4::Context->userenv;
128         if ($userenv) {
129             unless ($userenv->{flags} == 1){
130                 push @sql_params,$userenv->{branch};
131                 $query .= " and (U1.branchcode = ? or U1.branchcode ='')";
132             }
133         }
134     }
135     if ($suggestedbyme) {
136         unless ($suggestedbyme eq -1) {
137             push @sql_params,$user;
138             $query .= " and suggestedby=?";
139         }
140     } else {
141         $query .= " and managedby is NULL";
142     }
143     my $sth=$dbh->prepare($query);
144     $sth->execute(@sql_params);
145     my @results;
146     my $even=1; # the even variable is used to set even / odd lines, for highlighting
147     while (my $data=$sth->fetchrow_hashref){
148         $data->{$data->{STATUS}} = 1;
149         if ($even) {
150             $even=0;
151             $data->{even}=1;
152         } else {
153             $even=1;
154         }
155         push(@results,$data);
156     }
157     return (\@results);
158 }
159
160 =head2 GetSuggestion
161
162 =over 4
163
164 \%sth = &GetSuggestion($suggestionid)
165
166 this function get the detail of the suggestion $suggestionid (input arg)
167
168 return :
169     the result of the SQL query as a hash : $sth->fetchrow_hashref.
170
171 =back
172
173 =cut
174 sub GetSuggestion {
175     my ($suggestionid) = @_;
176     my $dbh = C4::Context->dbh;
177     my $query = qq|
178         SELECT *
179         FROM   suggestions
180         WHERE  suggestionid=?
181     |;
182     my $sth = $dbh->prepare($query);
183     $sth->execute($suggestionid);
184     return($sth->fetchrow_hashref);
185 }
186
187 =head2 GetSuggestionFromBiblionumber
188
189 =over 4
190
191 $suggestionid = &GetSuggestionFromBiblionumber($dbh,$biblionumber)
192
193 Get a suggestion from it's biblionumber.
194
195 return :
196 the id of the suggestion which is related to the biblionumber given on input args.
197
198 =back
199
200 =cut
201 sub GetSuggestionFromBiblionumber {
202     my ($dbh,$biblionumber) = @_;
203     my $query = qq|
204         SELECT suggestionid
205         FROM   suggestions
206         WHERE  biblionumber=?
207     |;
208     my $sth = $dbh->prepare($query);
209     $sth->execute($biblionumber);
210     my ($suggestionid) = $sth->fetchrow;
211     return $suggestionid;
212 }
213
214
215 =head2 CountSuggestion
216
217 =over 4
218
219 &CountSuggestion($status)
220
221 Count the number of suggestions with the status given on input argument.
222 the arg status can be :
223
224 =over
225
226 =over
227
228 =item * ASKED : asked by the user, not dealed by the librarian
229
230 =item * ACCEPTED : accepted by the librarian, but not yet ordered
231
232 =item * REJECTED : rejected by the librarian (definitive status)
233
234 =item * ORDERED : ordered by the librarian (acquisition module)
235
236 =back
237
238 =back
239
240 return :
241 the number of suggestion with this status.
242
243 =back
244
245 =cut
246 sub CountSuggestion {
247     my ($status) = @_;
248     my $dbh = C4::Context->dbh;
249     my $sth;
250     if (C4::Context->preference("IndependantBranches")){
251         my $userenv = C4::Context->userenv;
252         if ($userenv->{flags} == 1){
253             my $query = qq |
254                 SELECT count(*)
255                 FROM   suggestions
256                 WHERE  status=?
257             |;
258             $sth = $dbh->prepare($query);
259             $sth->execute($status);
260         }
261         else {
262             my $query = qq |
263                 SELECT count(*)
264                 FROM suggestions,borrowers
265                 WHERE status=?
266                 AND borrowers.borrowernumber=suggestions.suggestedby
267                 AND (borrowers.branchcode='' OR borrowers.branchcode =?)
268             |;
269             $sth = $dbh->prepare($query);
270             $sth->execute($status,$userenv->{branch});
271         }
272     }
273     else {
274         my $query = qq |
275             SELECT count(*)
276             FROM suggestions
277             WHERE status=?
278         |;
279          $sth = $dbh->prepare($query);
280         $sth->execute($status);
281     }
282     my ($result) = $sth->fetchrow;
283     return $result;
284 }
285
286 =head2 NewSuggestion
287
288
289 =over 4
290
291 &NewSuggestion($borrowernumber,$title,$author,$publishercode,$note,$copyrightdate,$volumedesc,$publicationyear,$place,$isbn,$biblionumber)
292
293 Insert a new suggestion on database with value given on input arg.
294
295 =back
296
297 =cut
298 sub NewSuggestion {
299     my ($borrowernumber,$title,$author,$publishercode,$note,$copyrightdate,$volumedesc,$publicationyear,$place,$isbn,$biblionumber) = @_;
300     my $dbh = C4::Context->dbh;
301     my $query = qq |
302         INSERT INTO suggestions
303             (status,suggestedby,title,author,publishercode,note,copyrightdate,
304             volumedesc,publicationyear,place,isbn,biblionumber)
305         VALUES ('ASKED',?,?,?,?,?,?,?,?,?,?,?)
306     |;
307     my $sth = $dbh->prepare($query);
308     $sth->execute($borrowernumber,$title,$author,$publishercode,$note,$copyrightdate,$volumedesc,$publicationyear,$place,$isbn,$biblionumber);
309 }
310
311 =head2 ModStatus
312
313 =over 4
314
315 &ModStatus($suggestionid,$status,$managedby,$biblionumber)
316
317 Modify the status (status can be 'ASKED', 'ACCEPTED', 'REJECTED', 'ORDERED')
318 and send a mail to notify the user that did the suggestion.
319
320 Note that there is no function to modify a suggestion : only the status can be modified, thus the name of the function.
321
322 =back
323
324 =cut
325 sub ModStatus {
326     my ($suggestionid,$status,$managedby,$biblionumber) = @_;
327     my $dbh = C4::Context->dbh;
328     my $sth;
329     if ($managedby>0) {
330         if ($biblionumber) {
331         my $query = qq|
332             UPDATE suggestions
333             SET    status=?,managedby=?,biblionumber=?
334             WHERE  suggestionid=?
335         |;
336         $sth = $dbh->prepare($query);
337         $sth->execute($status,$managedby,$biblionumber,$suggestionid);
338         } else {
339             my $query = qq|
340                 UPDATE suggestions
341                 SET    status=?,managedby=?
342                 WHERE  suggestionid=?
343             |;
344             $sth = $dbh->prepare($query);
345             $sth->execute($status,$managedby,$suggestionid);
346         }
347    } else {
348         if ($biblionumber) {
349             my $query = qq|
350                 UPDATE suggestions
351                 SET    status=?,biblionumber=?
352                 WHERE  suggestionid=?
353             |;
354             $sth = $dbh->prepare($query);
355             $sth->execute($status,$biblionumber,$suggestionid);
356         }
357         else {
358             my $query = qq|
359                 UPDATE suggestions
360                 SET    status=?
361                 WHERE  suggestionid=?
362             |;
363             $sth = $dbh->prepare($query);
364             $sth->execute($status,$suggestionid);
365         }
366     }
367     # check mail sending.
368     my $queryMail = qq|
369         SELECT suggestions.*,
370             boby.surname AS bysurname,
371             boby.firstname AS byfirstname,
372             boby.emailaddress AS byemail,
373             lib.surname AS libsurname,
374             lib.firstname AS libfirstname,
375             lib.emailaddress AS libemail
376         FROM suggestions
377             LEFT JOIN borrowers AS boby ON boby.borrowernumber=suggestedby
378             LEFT JOIN borrowers AS lib ON lib.borrowernumber=managedby
379         WHERE suggestionid=?
380     |;
381     $sth = $dbh->prepare($queryMail);
382     $sth->execute($suggestionid);
383     my $emailinfo = $sth->fetchrow_hashref;
384 if ($emailinfo->{byemail}){
385     my $template = gettemplate("suggestion/mail_suggestion_$status.tmpl","intranet");
386
387     $template->param(
388         byemail => $emailinfo->{byemail},
389         libemail => $emailinfo->{libemail},
390         status => $emailinfo->{status},
391         title => $emailinfo->{title},
392         author =>$emailinfo->{author},
393         libsurname => $emailinfo->{libsurname},
394         libfirstname => $emailinfo->{libfirstname},
395         byfirstname => $emailinfo->{byfirstname},
396         bysurname => $emailinfo->{bysurname},
397     );
398     my %mail = (
399         To => $emailinfo->{byemail},
400         From => $emailinfo->{libemail},
401         Subject => 'Koha suggestion',
402         Message => "".$template->output
403     );
404     sendmail(%mail);
405 }
406 }
407
408 =head2 ConnectSuggestionAndBiblio
409
410 =over 4
411 &ConnectSuggestionAndBiblio($suggestionid,$biblionumber)
412
413 connect a suggestion to an existing biblio
414
415 =back
416
417 =cut
418 sub ConnectSuggestionAndBiblio {
419     my ($suggestionid,$biblionumber) = @_;
420     my $dbh=C4::Context->dbh;
421     my $query = qq |
422         UPDATE suggestions
423         SET    biblionumber=?
424         WHERE  suggestionid=?
425     |;
426     my $sth = $dbh->prepare($query);
427     $sth->execute($biblionumber,$suggestionid);
428 }
429
430 =head2 DelSuggestion
431
432 =over 4
433
434 &DelSuggestion($borrowernumber,$suggestionid)
435
436 Delete a suggestion. A borrower can delete a suggestion only if he is its owner.
437
438 =back
439
440 =cut
441
442 sub DelSuggestion {
443     my ($borrowernumber,$suggestionid) = @_;
444     my $dbh = C4::Context->dbh;
445     # check that the suggestion comes from the suggestor
446     my $query = qq |
447         SELECT suggestedby
448         FROM   suggestions
449         WHERE  suggestionid=?
450     |;
451     my $sth = $dbh->prepare($query);
452     $sth->execute($suggestionid);
453     my ($suggestedby) = $sth->fetchrow;
454     if ($suggestedby eq $borrowernumber) {
455         my $queryDelete = qq|
456             DELETE FROM suggestions
457             WHERE suggestionid=?
458         |;
459         $sth = $dbh->prepare($queryDelete);
460         $sth->execute($suggestionid);
461     }
462 }