Bug 3746 - add to OPACViewOthersSuggestions description
[koha.git] / C4 / Suggestions.pm
blob491739c1f9c2bc3782594afc9c56a2c5e0a1a10a
1 package C4::Suggestions;
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 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.
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.
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
21 use strict;
22 use CGI;
23 use Mail::Sendmail;
25 use C4::Context;
26 use C4::Output;
27 use C4::Dates qw(format_date);
28 use vars qw($VERSION @ISA @EXPORT);
30 BEGIN {
31 # set the version for version checking
32 $VERSION = 3.01;
33 require Exporter;
34 @ISA = qw(Exporter);
35 @EXPORT = qw(
36 &NewSuggestion
37 &SearchSuggestion
38 &GetSuggestion
39 &GetSuggestionByStatus
40 &DelSuggestion
41 &CountSuggestion
42 &ModStatus
43 &ConnectSuggestionAndBiblio
44 &GetSuggestionFromBiblionumber
48 =head1 NAME
50 C4::Suggestions - Some useful functions for dealings with suggestions.
52 =head1 SYNOPSIS
54 use C4::Suggestions;
56 =head1 DESCRIPTION
58 The functions in this module deal with the suggestions in OPAC and in librarian interface
60 A suggestion is done in the OPAC. It has the status "ASKED"
62 When a librarian manages the suggestion, he can set the status to "REJECTED" or "ACCEPTED".
64 When the book is ordered, the suggestion status becomes "ORDERED"
66 When a book is ordered and arrived in the library, the status becomes "AVAILABLE"
68 All suggestions of a borrower can be seen by the borrower itself.
69 Suggestions done by other borrowers can be seen when not "AVAILABLE"
71 =head1 FUNCTIONS
73 =head2 SearchSuggestion
75 (\@array) = &SearchSuggestion($user,$author,$title,$publishercode,$status,$suggestedbyme,$branchcode)
77 searches for a suggestion
79 return :
80 C<\@array> : the suggestions found. Array of hash.
81 Note the status is stored twice :
82 * in the status field
83 * as parameter ( for example ASKED => 1, or REJECTED => 1) . This is for template & translation purposes.
85 =cut
87 sub SearchSuggestion {
88 my ($user,$author,$title,$publishercode,$status,$suggestedbyme,$branchcode)=@_;
89 my $dbh = C4::Context->dbh;
90 my $query = "
91 SELECT suggestions.*,
92 U1.branchcode AS branchcodesuggestedby,
93 B1.branchname AS branchnamesuggestedby,
94 U1.surname AS surnamesuggestedby,
95 U1.firstname AS firstnamesuggestedby,
96 U1.borrowernumber AS borrnumsuggestedby,
97 U1.categorycode AS categorycodesuggestedby,
98 C1.description AS categorydescriptionsuggestedby,
99 U2.branchcode AS branchcodemanagedby,
100 B2.branchname AS branchnamemanagedby,
101 U2.surname AS surnamemanagedby,
102 U2.firstname AS firstnamemanagedby,
103 U2.borrowernumber AS borrnummanagedby
104 FROM suggestions
105 LEFT JOIN borrowers AS U1 ON suggestedby=U1.borrowernumber
106 LEFT JOIN borrowers AS U2 ON managedby=U2.borrowernumber
107 LEFT JOIN categories AS C1 ON C1.categorycode = U1.categorycode
108 LEFT JOIN branches AS B1 ON B1.branchcode = U1.branchcode
109 LEFT JOIN branches AS B2 ON B2.branchcode = U2.branchcode
110 WHERE 1=1 ";
112 my @sql_params;
113 if ($author) {
114 push @sql_params,"%".$author."%";
115 $query .= " and author like ?";
117 if ($title) {
118 push @sql_params,"%".$title."%";
119 $query .= " and suggestions.title like ?";
121 if ($publishercode) {
122 push @sql_params,"%".$publishercode."%";
123 $query .= " and publishercode like ?";
125 if (C4::Context->preference("IndependantBranches") || $branchcode) {
126 my $userenv = C4::Context->userenv;
127 if ($userenv) {
128 unless ($userenv->{flags} % 2 == 1){
129 push @sql_params,$userenv->{branch};
130 $query .= " and (U1.branchcode = ? or U1.branchcode ='')";
133 if ($branchcode) {
134 push @sql_params,$branchcode;
135 $query .= " and (U1.branchcode = ? or U1.branchcode ='')";
138 if ($status) {
139 push @sql_params,$status;
140 $query .= " and status=?";
142 if ($suggestedbyme) {
143 unless ($suggestedbyme eq -1) {
144 push @sql_params,$user;
145 $query .= " and suggestedby=?";
147 } else {
148 $query .= " and managedby is NULL";
150 my $sth=$dbh->prepare($query);
151 $sth->execute(@sql_params);
152 my @results;
153 my $even=1; # the even variable is used to set even / odd lines, for highlighting
154 while (my $data=$sth->fetchrow_hashref){
155 $data->{$data->{STATUS}} = 1;
156 if ($even) {
157 $even=0;
158 $data->{even}=1;
159 } else {
160 $even=1;
162 # $data->{date} = format_date($data->{date});
163 push(@results,$data);
165 return (\@results);
168 =head2 GetSuggestion
170 \%sth = &GetSuggestion($suggestionid)
172 this function get the detail of the suggestion $suggestionid (input arg)
174 return :
175 the result of the SQL query as a hash : $sth->fetchrow_hashref.
177 =cut
179 sub GetSuggestion {
180 my ($suggestionid) = @_;
181 my $dbh = C4::Context->dbh;
182 my $query = "
183 SELECT *
184 FROM suggestions
185 WHERE suggestionid=?
187 my $sth = $dbh->prepare($query);
188 $sth->execute($suggestionid);
189 return($sth->fetchrow_hashref);
192 =head2 GetSuggestionFromBiblionumber
194 $suggestionid = &GetSuggestionFromBiblionumber($dbh,$biblionumber)
196 Get a suggestion from it's biblionumber.
198 return :
199 the id of the suggestion which is related to the biblionumber given on input args.
201 =cut
203 sub GetSuggestionFromBiblionumber {
204 my ($dbh,$biblionumber) = @_;
205 my $query = qq|
206 SELECT suggestionid
207 FROM suggestions
208 WHERE biblionumber=?
210 my $sth = $dbh->prepare($query);
211 $sth->execute($biblionumber);
212 my ($suggestionid) = $sth->fetchrow;
213 return $suggestionid;
216 =head2 GetSuggestionByStatus
218 $suggestions = &GetSuggestionByStatus($status,[$branchcode])
220 Get a suggestion from it's status
222 return :
223 all the suggestion with C<$status>
225 =cut
227 sub GetSuggestionByStatus {
228 my $status = shift;
229 my $branchcode = shift;
230 my $dbh = C4::Context->dbh;
231 my @sql_params=($status);
232 my $query = qq(SELECT suggestions.*,
233 U1.surname AS surnamesuggestedby,
234 U1.firstname AS firstnamesuggestedby,
235 U1.branchcode AS branchcodesuggestedby,
236 B1.branchname AS branchnamesuggestedby,
237 U1.borrowernumber AS borrnumsuggestedby,
238 U1.categorycode AS categorycodesuggestedby,
239 C1.description AS categorydescriptionsuggestedby,
240 U2.surname AS surnamemanagedby,
241 U2.firstname AS firstnamemanagedby,
242 U2.borrowernumber AS borrnummanagedby
243 FROM suggestions
244 LEFT JOIN borrowers AS U1 ON suggestedby=U1.borrowernumber
245 LEFT JOIN borrowers AS U2 ON managedby=U2.borrowernumber
246 LEFT JOIN categories AS C1 ON C1.categorycode=U1.categorycode
247 LEFT JOIN branches AS B1 on B1.branchcode = U1.branchcode
248 WHERE status = ?);
249 if (C4::Context->preference("IndependantBranches") || $branchcode) {
250 my $userenv = C4::Context->userenv;
251 if ($userenv) {
252 unless ($userenv->{flags} % 2 == 1){
253 push @sql_params,$userenv->{branch};
254 $query .= " and (U1.branchcode = ? or U1.branchcode ='')";
257 if ($branchcode) {
258 push @sql_params,$branchcode;
259 $query .= " and (U1.branchcode = ? or U1.branchcode ='')";
263 my $sth = $dbh->prepare($query);
264 $sth->execute(@sql_params);
266 my $results;
267 $results= $sth->fetchall_arrayref({});
268 # map{$_->{date} = format_date($_->{date})} @$results;
269 return $results;
272 =head2 CountSuggestion
274 &CountSuggestion($status)
276 Count the number of suggestions with the status given on input argument.
277 the arg status can be :
279 =over 2
281 =item * ASKED : asked by the user, not dealed by the librarian
283 =item * ACCEPTED : accepted by the librarian, but not yet ordered
285 =item * REJECTED : rejected by the librarian (definitive status)
287 =item * ORDERED : ordered by the librarian (acquisition module)
289 =back
291 return :
292 the number of suggestion with this status.
294 =cut
296 sub CountSuggestion {
297 my ($status) = @_;
298 my $dbh = C4::Context->dbh;
299 my $sth;
300 if (C4::Context->preference("IndependantBranches")){
301 my $userenv = C4::Context->userenv;
302 if ($userenv->{flags} % 2 == 1){
303 my $query = qq |
304 SELECT count(*)
305 FROM suggestions
306 WHERE status=?
308 $sth = $dbh->prepare($query);
309 $sth->execute($status);
311 else {
312 my $query = qq |
313 SELECT count(*)
314 FROM suggestions LEFT JOIN borrowers ON borrowers.borrowernumber=suggestions.suggestedby
315 WHERE status=?
316 AND (borrowers.branchcode='' OR borrowers.branchcode =?)
318 $sth = $dbh->prepare($query);
319 $sth->execute($status,$userenv->{branch});
322 else {
323 my $query = qq |
324 SELECT count(*)
325 FROM suggestions
326 WHERE status=?
328 $sth = $dbh->prepare($query);
329 $sth->execute($status);
331 my ($result) = $sth->fetchrow;
332 return $result;
335 =head2 NewSuggestion
338 &NewSuggestion($borrowernumber,$title,$author,$publishercode,$note,$copyrightdate,$volumedesc,$publicationyear,$place,$isbn,$biblionumber)
340 Insert a new suggestion on database with value given on input arg.
342 =cut
344 sub NewSuggestion {
345 my ($borrowernumber,$title,$author,$publishercode,$note,$copyrightdate,$volumedesc,$publicationyear,$place,$isbn,$biblionumber,$reason) = @_;
346 my $dbh = C4::Context->dbh;
347 my $query = qq |
348 INSERT INTO suggestions
349 (status,suggestedby,title,author,publishercode,note,copyrightdate,
350 volumedesc,publicationyear,place,isbn,biblionumber,reason)
351 VALUES ('ASKED',?,?,?,?,?,?,?,?,?,?,?,?)
353 my $sth = $dbh->prepare($query);
354 $sth->execute($borrowernumber,$title,$author,$publishercode,$note,$copyrightdate,$volumedesc,$publicationyear,$place,$isbn,$biblionumber,$reason);
357 =head2 ModStatus
359 &ModStatus($suggestionid,$status,$managedby,$biblionumber)
361 Modify the status (status can be 'ASKED', 'ACCEPTED', 'REJECTED', 'ORDERED')
362 and send a mail to notify the user that did the suggestion.
364 Note that there is no function to modify a suggestion : only the status can be modified, thus the name of the function.
366 =cut
368 sub ModStatus {
369 my ($suggestionid,$status,$managedby,$biblionumber,$reason) = @_;
370 my $dbh = C4::Context->dbh;
371 my $sth;
372 if ($managedby>0) {
373 if ($biblionumber) {
374 my $query = qq|
375 UPDATE suggestions
376 SET status=?,managedby=?,biblionumber=?,reason=?
377 WHERE suggestionid=?
379 $sth = $dbh->prepare($query);
380 $sth->execute($status,$managedby,$biblionumber,$reason,$suggestionid);
381 } else {
382 my $query = qq|
383 UPDATE suggestions
384 SET status=?,managedby=?,reason=?
385 WHERE suggestionid=?
387 $sth = $dbh->prepare($query);
388 $sth->execute($status,$managedby,$reason,$suggestionid);
390 } else {
391 if ($biblionumber) {
392 my $query = qq|
393 UPDATE suggestions
394 SET status=?,biblionumber=?,reason=?
395 WHERE suggestionid=?
397 $sth = $dbh->prepare($query);
398 $sth->execute($status,$biblionumber,$reason,$suggestionid);
400 else {
401 my $query = qq|
402 UPDATE suggestions
403 SET status=?,reason=?
404 WHERE suggestionid=?
406 $sth = $dbh->prepare($query);
407 $sth->execute($status,$reason,$suggestionid);
410 # check mail sending.
411 my $queryMail = "
412 SELECT suggestions.*,
413 boby.surname AS bysurname,
414 boby.firstname AS byfirstname,
415 boby.email AS byemail,
416 lib.surname AS libsurname,
417 lib.firstname AS libfirstname,
418 lib.email AS libemail
419 FROM suggestions
420 LEFT JOIN borrowers AS boby ON boby.borrowernumber=suggestedby
421 LEFT JOIN borrowers AS lib ON lib.borrowernumber=managedby
422 WHERE suggestionid=?
424 $sth = $dbh->prepare($queryMail);
425 $sth->execute($suggestionid);
426 my $emailinfo = $sth->fetchrow_hashref;
427 my $template = gettemplate("suggestion/mail_suggestion_$status.tmpl", "intranet", CGI->new());
429 $template->param(
430 byemail => $emailinfo->{byemail},
431 libemail => $emailinfo->{libemail},
432 status => $emailinfo->{status},
433 title => $emailinfo->{title},
434 author =>$emailinfo->{author},
435 libsurname => $emailinfo->{libsurname},
436 libfirstname => $emailinfo->{libfirstname},
437 byfirstname => $emailinfo->{byfirstname},
438 bysurname => $emailinfo->{bysurname},
439 reason => $emailinfo->{reason}
441 my %mail = (
442 To => $emailinfo->{byemail},
443 From => $emailinfo->{libemail},
444 Subject => 'Koha suggestion',
445 Message => "".$template->output,
446 'Content-Type' => 'text/plain; charset="utf8"',
448 sendmail(%mail);
451 =head2 ConnectSuggestionAndBiblio
453 &ConnectSuggestionAndBiblio($suggestionid,$biblionumber)
455 connect a suggestion to an existing biblio
457 =cut
459 sub ConnectSuggestionAndBiblio {
460 my ($suggestionid,$biblionumber) = @_;
461 my $dbh=C4::Context->dbh;
462 my $query = "
463 UPDATE suggestions
464 SET biblionumber=?
465 WHERE suggestionid=?
467 my $sth = $dbh->prepare($query);
468 $sth->execute($biblionumber,$suggestionid);
471 =head2 DelSuggestion
473 &DelSuggestion($borrowernumber,$suggestionid)
475 Delete a suggestion. A borrower can delete a suggestion only if he is its owner.
477 =cut
479 sub DelSuggestion {
480 my ($borrowernumber,$suggestionid,$type) = @_;
481 my $dbh = C4::Context->dbh;
482 # check that the suggestion comes from the suggestor
483 my $query = "
484 SELECT suggestedby
485 FROM suggestions
486 WHERE suggestionid=?
488 my $sth = $dbh->prepare($query);
489 $sth->execute($suggestionid);
490 my ($suggestedby) = $sth->fetchrow;
491 if ($type eq "intranet" || $suggestedby eq $borrowernumber ) {
492 my $queryDelete = "
493 DELETE FROM suggestions
494 WHERE suggestionid=?
496 $sth = $dbh->prepare($queryDelete);
497 my $suggestiondeleted=$sth->execute($suggestionid);
498 return $suggestiondeleted;
503 __END__
506 =head1 AUTHOR
508 Koha Developement team <info@koha.org>
510 =cut