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,$branchcode)
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,$branchcode)=@_;
89 my $dbh = C4
::Context
->dbh;
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
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
114 push @sql_params,"%".$author."%";
115 $query .= " and author like ?";
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;
128 unless ($userenv->{flags
} % 2 == 1){
129 push @sql_params,$userenv->{branch
};
130 $query .= " and (U1.branchcode = ? or U1.branchcode ='')";
134 push @sql_params,$branchcode;
135 $query .= " and (U1.branchcode = ? or U1.branchcode ='')";
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=?";
148 $query .= " and managedby is NULL";
150 my $sth=$dbh->prepare($query);
151 $sth->execute(@sql_params);
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;
162 # $data->{date} = format_date($data->{date});
163 push(@results,$data);
170 \%sth = &GetSuggestion($suggestionid)
172 this function get the detail of the suggestion $suggestionid (input arg)
175 the result of the SQL query as a hash : $sth->fetchrow_hashref.
180 my ($suggestionid) = @_;
181 my $dbh = C4
::Context
->dbh;
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.
199 the id of the suggestion which is related to the biblionumber given on input args.
203 sub GetSuggestionFromBiblionumber
{
204 my ($dbh,$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
223 all the suggestion with C<$status>
227 sub GetSuggestionByStatus
{
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
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
249 if (C4
::Context
->preference("IndependantBranches") || $branchcode) {
250 my $userenv = C4
::Context
->userenv;
252 unless ($userenv->{flags
} % 2 == 1){
253 push @sql_params,$userenv->{branch
};
254 $query .= " and (U1.branchcode = ? or U1.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);
267 $results= $sth->fetchall_arrayref({});
268 # map{$_->{date} = format_date($_->{date})} @$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 :
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)
292 the number of suggestion with this status.
296 sub CountSuggestion
{
298 my $dbh = C4
::Context
->dbh;
300 if (C4
::Context
->preference("IndependantBranches")){
301 my $userenv = C4
::Context
->userenv;
302 if ($userenv->{flags
} % 2 == 1){
308 $sth = $dbh->prepare($query);
309 $sth->execute($status);
314 FROM suggestions LEFT JOIN borrowers ON borrowers
.borrowernumber
=suggestions
.suggestedby
316 AND
(borrowers
.branchcode
='' OR borrowers
.branchcode
=?
)
318 $sth = $dbh->prepare($query);
319 $sth->execute($status,$userenv->{branch
});
328 $sth = $dbh->prepare($query);
329 $sth->execute($status);
331 my ($result) = $sth->fetchrow;
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.
345 my ($borrowernumber,$title,$author,$publishercode,$note,$copyrightdate,$volumedesc,$publicationyear,$place,$isbn,$biblionumber,$reason) = @_;
346 my $dbh = C4
::Context
->dbh;
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);
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.
369 my ($suggestionid,$status,$managedby,$biblionumber,$reason) = @_;
370 my $dbh = C4
::Context
->dbh;
376 SET status
=?
,managedby
=?
,biblionumber
=?
,reason
=?
379 $sth = $dbh->prepare($query);
380 $sth->execute($status,$managedby,$biblionumber,$reason,$suggestionid);
384 SET status
=?
,managedby
=?
,reason
=?
387 $sth = $dbh->prepare($query);
388 $sth->execute($status,$managedby,$reason,$suggestionid);
394 SET status
=?
,biblionumber
=?
,reason
=?
397 $sth = $dbh->prepare($query);
398 $sth->execute($status,$biblionumber,$reason,$suggestionid);
403 SET status
=?
,reason
=?
406 $sth = $dbh->prepare($query);
407 $sth->execute($status,$reason,$suggestionid);
410 # check mail sending.
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
420 LEFT JOIN borrowers AS boby ON boby.borrowernumber=suggestedby
421 LEFT JOIN borrowers AS lib ON lib.borrowernumber=managedby
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());
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
}
442 To
=> $emailinfo->{byemail
},
443 From
=> $emailinfo->{libemail
},
444 Subject
=> 'Koha suggestion',
445 Message
=> "".$template->output,
446 'Content-Type' => 'text/plain; charset="utf8"',
451 =head2 ConnectSuggestionAndBiblio
453 &ConnectSuggestionAndBiblio($suggestionid,$biblionumber)
455 connect a suggestion to an existing biblio
459 sub ConnectSuggestionAndBiblio
{
460 my ($suggestionid,$biblionumber) = @_;
461 my $dbh=C4
::Context
->dbh;
467 my $sth = $dbh->prepare($query);
468 $sth->execute($biblionumber,$suggestionid);
473 &DelSuggestion($borrowernumber,$suggestionid)
475 Delete a suggestion. A borrower can delete a suggestion only if he is its owner.
480 my ($borrowernumber,$suggestionid,$type) = @_;
481 my $dbh = C4
::Context
->dbh;
482 # check that the suggestion comes from the suggestor
488 my $sth = $dbh->prepare($query);
489 $sth->execute($suggestionid);
490 my ($suggestedby) = $sth->fetchrow;
491 if ($type eq "intranet" || $suggestedby eq $borrowernumber ) {
493 DELETE FROM suggestions
496 $sth = $dbh->prepare($queryDelete);
497 my $suggestiondeleted=$sth->execute($suggestionid);
498 return $suggestiondeleted;
508 Koha Developement team <info@koha.org>