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
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
27 use C4::Dates qw(format_date);
28 use vars qw($VERSION @ISA @EXPORT);
31 # set the version for version checking
39 &GetSuggestionByStatus
43 &ConnectSuggestionAndBiblio
44 &GetSuggestionFromBiblionumber
50 C4::Suggestions - Some useful functions for dealings with suggestions.
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"
73 =head2 SearchSuggestion
75 (\@array) = &SearchSuggestion($user,$author,$title,$publishercode,$status,$suggestedbyme)
77 searches for a suggestion
80 C<\@array> : the suggestions found. Array of hash.
81 Note the status is stored twice :
83 * as parameter ( for example ASKED => 1, or REJECTED => 1) . This is for template & translation purposes.
87 sub SearchSuggestion {
88 my ($user,$author,$title,$publishercode,$status,$suggestedbyme)=@_;
89 my $dbh = C4::Context->dbh;
92 U1.surname AS surnamesuggestedby,
93 U1.firstname AS firstnamesuggestedby,
94 U1.borrowernumber AS borrnumsuggestedby,
95 U2.surname AS surnamemanagedby,
96 U2.firstname AS firstnamemanagedby,
97 U2.borrowernumber AS borrnummanagedby
99 LEFT JOIN borrowers AS U1 ON suggestedby=U1.borrowernumber
100 LEFT JOIN borrowers AS U2 ON managedby=U2.borrowernumber
105 push @sql_params,"%".$author."%";
106 $query .= " and author like ?";
109 push @sql_params,"%".$title."%";
110 $query .= " and suggestions.title like ?";
112 if ($publishercode) {
113 push @sql_params,"%".$publishercode."%";
114 $query .= " and publishercode like ?";
116 if (C4::Context->preference("IndependantBranches")) {
117 my $userenv = C4::Context->userenv;
119 unless ($userenv->{flags} == 1){
120 push @sql_params,$userenv->{branch};
121 $query .= " and (U1.branchcode = ? or U1.branchcode ='')";
126 push @sql_params,$status;
127 $query .= " and status=?";
129 if ($suggestedbyme) {
130 unless ($suggestedbyme eq -1) {
131 push @sql_params,$user;
132 $query .= " and suggestedby=?";
135 $query .= " and managedby is NULL";
137 my $sth=$dbh->prepare($query);
138 $sth->execute(@sql_params);
140 my $even=1; # the even variable is used to set even / odd lines, for highlighting
141 while (my $data=$sth->fetchrow_hashref){
142 $data->{$data->{STATUS}} = 1;
149 push(@results,$data);
156 \%sth = &GetSuggestion($suggestionid)
158 this function get the detail of the suggestion $suggestionid (input arg)
161 the result of the SQL query as a hash : $sth->fetchrow_hashref.
166 my ($suggestionid) = @_;
167 my $dbh = C4::Context->dbh;
173 my $sth = $dbh->prepare($query);
174 $sth->execute($suggestionid);
175 return($sth->fetchrow_hashref);
178 =head2 GetSuggestionFromBiblionumber
180 $suggestionid = &GetSuggestionFromBiblionumber($dbh,$biblionumber)
182 Get a suggestion from it's biblionumber.
185 the id of the suggestion which is related to the biblionumber given on input args.
189 sub GetSuggestionFromBiblionumber {
190 my ($dbh,$biblionumber) = @_;
196 my $sth = $dbh->prepare($query);
197 $sth->execute($biblionumber);
198 my ($suggestionid) = $sth->fetchrow;
199 return $suggestionid;
202 =head2 GetSuggestionByStatus
204 $suggestions = &GetSuggestionByStatus($status)
206 Get a suggestion from it's status
209 all the suggestion with C<$status>
213 sub GetSuggestionByStatus {
215 my $dbh = C4::Context->dbh;
216 my $query = "SELECT suggestions.*,
217 U1.surname AS surnamesuggestedby,
218 U1.firstname AS firstnamesuggestedby,
219 U1.borrowernumber AS borrnumsuggestedby,
220 U2.surname AS surnamemanagedby,
221 U2.firstname AS firstnamemanagedby,
222 U2.borrowernumber AS borrnummanagedby
224 LEFT JOIN borrowers AS U1 ON suggestedby=U1.borrowernumber
225 LEFT JOIN borrowers AS U2 ON managedby=U2.borrowernumber
228 my $sth = $dbh->prepare($query);
229 $sth->execute($status);
232 while(my $data = $sth->fetchrow_hashref){
233 $data->{date} = format_date($data->{date});
239 =head2 CountSuggestion
241 &CountSuggestion($status)
243 Count the number of suggestions with the status given on input argument.
244 the arg status can be :
248 =item * ASKED : asked by the user, not dealed by the librarian
250 =item * ACCEPTED : accepted by the librarian, but not yet ordered
252 =item * REJECTED : rejected by the librarian (definitive status)
254 =item * ORDERED : ordered by the librarian (acquisition module)
259 the number of suggestion with this status.
263 sub CountSuggestion {
265 my $dbh = C4::Context->dbh;
267 if (C4::Context->preference("IndependantBranches")){
268 my $userenv = C4::Context->userenv;
269 if ($userenv->{flags} == 1){
275 $sth = $dbh->prepare($query);
276 $sth->execute($status);
281 FROM suggestions LEFT JOIN borrowers ON borrowers.borrowernumber=suggestions.suggestedby
283 AND (borrowers.branchcode='' OR borrowers.branchcode =?)
285 $sth = $dbh->prepare($query);
286 $sth->execute($status,$userenv->{branch});
295 $sth = $dbh->prepare($query);
296 $sth->execute($status);
298 my ($result) = $sth->fetchrow;
305 &NewSuggestion($borrowernumber,$title,$author,$publishercode,$note,$copyrightdate,$volumedesc,$publicationyear,$place,$isbn,$biblionumber)
307 Insert a new suggestion on database with value given on input arg.
312 my ($borrowernumber,$title,$author,$publishercode,$note,$copyrightdate,$volumedesc,$publicationyear,$place,$isbn,$biblionumber,$reason) = @_;
313 my $dbh = C4::Context->dbh;
315 INSERT INTO suggestions
316 (status,suggestedby,title,author,publishercode,note,copyrightdate,
317 volumedesc,publicationyear,place,isbn,biblionumber,reason)
318 VALUES ('ASKED',?,?,?,?,?,?,?,?,?,?,?,?)
320 my $sth = $dbh->prepare($query);
321 $sth->execute($borrowernumber,$title,$author,$publishercode,$note,$copyrightdate,$volumedesc,$publicationyear,$place,$isbn,$biblionumber,$reason);
326 &ModStatus($suggestionid,$status,$managedby,$biblionumber)
328 Modify the status (status can be 'ASKED', 'ACCEPTED', 'REJECTED', 'ORDERED')
329 and send a mail to notify the user that did the suggestion.
331 Note that there is no function to modify a suggestion : only the status can be modified, thus the name of the function.
336 my ($suggestionid,$status,$managedby,$biblionumber,$reason) = @_;
337 my $dbh = C4::Context->dbh;
343 SET status=?,managedby=?,biblionumber=?,reason=?
346 $sth = $dbh->prepare($query);
347 $sth->execute($status,$managedby,$biblionumber,$reason,$suggestionid);
351 SET status=?,managedby=?,reason=?
354 $sth = $dbh->prepare($query);
355 $sth->execute($status,$managedby,$reason,$suggestionid);
361 SET status=?,biblionumber=?,reason=?
364 $sth = $dbh->prepare($query);
365 $sth->execute($status,$biblionumber,$reason,$suggestionid);
370 SET status=?,reason=?
373 $sth = $dbh->prepare($query);
374 $sth->execute($status,$reason,$suggestionid);
377 # check mail sending.
379 SELECT suggestions.*,
380 boby.surname AS bysurname,
381 boby.firstname AS byfirstname,
382 boby.email AS byemail,
383 lib.surname AS libsurname,
384 lib.firstname AS libfirstname,
385 lib.email AS libemail
387 LEFT JOIN borrowers AS boby ON boby.borrowernumber=suggestedby
388 LEFT JOIN borrowers AS lib ON lib.borrowernumber=managedby
391 $sth = $dbh->prepare($queryMail);
392 $sth->execute($suggestionid);
393 my $emailinfo = $sth->fetchrow_hashref;
394 my $template = gettemplate("suggestion/mail_suggestion_$status.tmpl", "intranet", CGI->new());
397 byemail => $emailinfo->{byemail},
398 libemail => $emailinfo->{libemail},
399 status => $emailinfo->{status},
400 title => $emailinfo->{title},
401 author =>$emailinfo->{author},
402 libsurname => $emailinfo->{libsurname},
403 libfirstname => $emailinfo->{libfirstname},
404 byfirstname => $emailinfo->{byfirstname},
405 bysurname => $emailinfo->{bysurname},
406 reason => $emailinfo->{reason}
409 To => $emailinfo->{byemail},
410 From => $emailinfo->{libemail},
411 Subject => 'Koha suggestion',
412 Message => "".$template->output
417 =head2 ConnectSuggestionAndBiblio
419 &ConnectSuggestionAndBiblio($suggestionid,$biblionumber)
421 connect a suggestion to an existing biblio
425 sub ConnectSuggestionAndBiblio {
426 my ($suggestionid,$biblionumber) = @_;
427 my $dbh=C4::Context->dbh;
433 my $sth = $dbh->prepare($query);
434 $sth->execute($biblionumber,$suggestionid);
439 &DelSuggestion($borrowernumber,$suggestionid)
441 Delete a suggestion. A borrower can delete a suggestion only if he is its owner.
446 my ($borrowernumber,$suggestionid) = @_;
447 my $dbh = C4::Context->dbh;
448 # check that the suggestion comes from the suggestor
454 my $sth = $dbh->prepare($query);
455 $sth->execute($suggestionid);
456 my ($suggestedby) = $sth->fetchrow;
457 if ($suggestedby eq $borrowernumber) {
459 DELETE FROM suggestions
462 $sth = $dbh->prepare($queryDelete);
463 $sth->execute($suggestionid);
473 Koha Developement team <info@koha.org>