-sub countsuggestion {
- my ($status) = @_;
- my $dbh = C4::Context->dbh;
- my $sth;
- if (C4::Context->preference("IndependantBranches")){
- my $userenv = C4::Context->userenv;
- if ($userenv->{flags} == 1){
- $sth = $dbh->prepare("select count(*) from suggestions where status=?");
- $sth->execute($status);
- } else {
- $sth = $dbh->prepare("select count(*) from suggestions,borrowers where status=? and borrowers.borrowernumber=suggestions.suggestedby and (borrowers.branchcode='' or borrowers.branchcode =?)");
- $sth->execute($status,$userenv->{branch});
- }
- } else {
- $sth = $dbh->prepare("select count(*) from suggestions where status=?");
- $sth->execute($status);
- }
- my ($result) = $sth->fetchrow;
- return $result;
+=head2 ModStatus
+
+&ModStatus($suggestionid,$status,$managedby,$biblionumber)
+
+Modify the status (status can be 'ASKED', 'ACCEPTED', 'REJECTED', 'ORDERED')
+and send a mail to notify the user that did the suggestion.
+
+Note that there is no function to modify a suggestion : only the status can be modified, thus the name of the function.
+
+=cut
+
+sub ModStatus {
+ my ($suggestionid,$status,$managedby,$biblionumber,$reason) = @_;
+ my $dbh = C4::Context->dbh;
+ my $sth;
+ if ($managedby>0) {
+ if ($biblionumber) {
+ my $query = qq|
+ UPDATE suggestions
+ SET status=?,managedby=?,biblionumber=?,reason=?
+ WHERE suggestionid=?
+ |;
+ $sth = $dbh->prepare($query);
+ $sth->execute($status,$managedby,$biblionumber,$reason,$suggestionid);
+ } else {
+ my $query = qq|
+ UPDATE suggestions
+ SET status=?,managedby=?,reason=?
+ WHERE suggestionid=?
+ |;
+ $sth = $dbh->prepare($query);
+ $sth->execute($status,$managedby,$reason,$suggestionid);
+ }
+ } else {
+ if ($biblionumber) {
+ my $query = qq|
+ UPDATE suggestions
+ SET status=?,biblionumber=?,reason=?
+ WHERE suggestionid=?
+ |;
+ $sth = $dbh->prepare($query);
+ $sth->execute($status,$biblionumber,$reason,$suggestionid);
+ }
+ else {
+ my $query = qq|
+ UPDATE suggestions
+ SET status=?,reason=?
+ WHERE suggestionid=?
+ |;
+ $sth = $dbh->prepare($query);
+ $sth->execute($status,$reason,$suggestionid);
+ }
+ }
+ # check mail sending.
+ my $queryMail = "
+ SELECT suggestions.*,
+ boby.surname AS bysurname,
+ boby.firstname AS byfirstname,
+ boby.email AS byemail,
+ lib.surname AS libsurname,
+ lib.firstname AS libfirstname,
+ lib.email AS libemail
+ FROM suggestions
+ LEFT JOIN borrowers AS boby ON boby.borrowernumber=suggestedby
+ LEFT JOIN borrowers AS lib ON lib.borrowernumber=managedby
+ WHERE suggestionid=?
+ ";
+ $sth = $dbh->prepare($queryMail);
+ $sth->execute($suggestionid);
+ my $emailinfo = $sth->fetchrow_hashref;
+ my $template = gettemplate("suggestion/mail_suggestion_$status.tmpl","intranet");
+
+ $template->param(
+ byemail => $emailinfo->{byemail},
+ libemail => $emailinfo->{libemail},
+ status => $emailinfo->{status},
+ title => $emailinfo->{title},
+ author =>$emailinfo->{author},
+ libsurname => $emailinfo->{libsurname},
+ libfirstname => $emailinfo->{libfirstname},
+ byfirstname => $emailinfo->{byfirstname},
+ bysurname => $emailinfo->{bysurname},
+ reason => $emailinfo->{reason}
+ );
+ my %mail = (
+ To => $emailinfo->{byemail},
+ From => $emailinfo->{libemail},
+ Subject => 'Koha suggestion',
+ Message => "".$template->output
+ );
+ sendmail(%mail);