rename internal function
[koha.git] / C4 / Log.pm
1 package C4::Log; #assumes C4/Log
2
3 #package to deal with Logging Actions in DB
4
5
6 # Copyright 2000-2002 Katipo Communications
7 #
8 # This file is part of Koha.
9 #
10 # Koha is free software; you can redistribute it and/or modify it under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 2 of the License, or (at your option) any later
13 # version.
14 #
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License along with
20 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
21 # Suite 330, Boston, MA  02111-1307 USA
22
23 use strict;
24 use C4::Context;
25 use C4::Dates qw(format_date);
26
27 require Exporter;
28
29 use vars qw($VERSION @ISA @EXPORT);
30
31 # set the version for version checking
32 $VERSION = 3.01;
33
34 =head1 NAME
35
36 C4::Log - Koha Log Facility functions
37
38 =head1 SYNOPSIS
39
40   use C4::Log;
41
42 =head1 DESCRIPTION
43
44 The functions in this module perform various functions in order to log all the operations done on the Database, including deleting and undeleting books, adding/editing members, etc.
45
46 =head1 FUNCTIONS
47
48 =over 2
49
50 =cut
51
52 @ISA = qw(Exporter);
53 @EXPORT = qw(&logaction &GetLogStatus &displaylog &GetLogs);
54
55 =item logaction
56
57   &logaction($usernumber, $modulename, $actionname, $objectnumber, $infos);
58
59 Adds a record into action_logs table to report the different changes upon the database
60
61 =cut
62
63 #'
64 sub logaction {
65   my ($usernumber,$modulename, $actionname, $objectnumber, $infos)=@_;
66     $usernumber='' unless $usernumber;
67     my $dbh = C4::Context->dbh;
68     my $sth=$dbh->prepare("Insert into action_logs (timestamp,user,module,action,object,info) values (now(),?,?,?,?,?)");
69     $sth->execute($usernumber,$modulename,$actionname,$objectnumber,$infos);
70     $sth->finish;
71 }
72
73 =item GetLogStatus
74
75   $status = GetLogStatus;
76
77 C<$status> is a hasref like this example:
78     $hash = {
79         BorrowersLog   => 1,
80         CataloguingLog => 0,
81         IssueLog       => 0,
82         ...
83     }
84
85 =cut
86
87 #'
88 sub GetLogStatus {
89     my %hash;
90     $hash{BorrowersLog}    = C4::Context->preference("BorrowersLog");
91     $hash{CataloguingLog}  = C4::Context->preference("CataloguingLog");
92     $hash{IssueLog}        = C4::Context->preference("IssueLog");
93     $hash{ReturnLog}       = C4::Context->preference("ReturnLog");
94     $hash{SubscriptionLog} = C4::Context->preference("SubscriptionLog");
95     $hash{LetterLog}       = C4::Context->preference("LetterLog");
96     $hash{FinesLog}        = C4::Context->preference("FinesLog");
97     return \%hash;
98 }
99
100 =item displaylog
101
102   &displaylog($modulename, @filters);
103   $modulename is the name of the module on which the user wants to display logs
104   @filters is an optional table of hash containing :
105       - name : the name of the variable to filter
106     - value : the value of the filter.... May be with * joker
107
108 returns a table of hash containing who did what on which object at what time
109
110 =cut
111
112 #'
113 sub displaylog {
114   my ($modulename, @filters) = @_;
115     my $dbh = C4::Context->dbh;
116     my $strsth=qq|
117                 SELECT action_logs.timestamp, action_logs.action, action_logs.info,
118                                 borrowers.cardnumber, borrowers.surname, borrowers.firstname, borrowers.userid,
119                         biblio.biblionumber, biblio.title, biblio.author
120         FROM action_logs 
121                 LEFT JOIN borrowers ON borrowers.borrowernumber=action_logs.user 
122         LEFT JOIN  biblio   ON action_logs.object=biblio.biblionumber
123         WHERE action_logs.module = 'cataloguing' 
124         |;
125         my %filtermap = ();
126     if ($modulename eq "catalogue" or $modulename eq "acqui") {
127                 %filtermap = (
128                           user => 'borrowers.surname',
129                          title => 'biblio.title',
130                         author => 'biblio.author',
131                 );
132     } elsif ($modulename eq "members") {
133         $strsth=qq|
134                 SELECT action_logs.timestamp, action_logs.action, action_logs.info, 
135                         borrowers.cardnumber, borrowers.surname, borrowers.firstname, borrowers.userid,
136                         bor2.cardnumber, bor2.surname, bor2.firstname, bor2.userid
137         FROM action_logs 
138                 LEFT JOIN borrowers ON borrowers.borrowernumber=action_logs.user 
139                 LEFT JOIN borrowers as bor2 ON action_logs.object=bor2.borrowernumber
140         WHERE action_logs.module = 'members' 
141                 |;
142                 %filtermap = (
143                        user => 'borrowers.surname',
144                     surname => 'bor2.surname',
145                   firstname => 'bor2.firstname',
146                  cardnumber => 'bor2.cardnumber',
147                 );
148     } else {
149                 return 0;
150         }
151
152     if (@filters) {
153                 foreach my $filter (@filters) {
154                         my $tempname = $filter->{name}         or next;
155                         (grep {/^$tempname$/} keys %filtermap) or next;
156                         $filter->{value} =~ s/\*/%/g;
157                         $strsth .= " AND " . $filtermap{$tempname} . " LIKE " . $filter->{value};
158                 }
159         }
160     my $sth=$dbh->prepare($strsth);
161     $sth->execute;
162     my @results;
163     my $count;
164     my $hilighted=1;
165     while (my $data = $sth->fetchrow_hashref){
166         $data->{hilighted} = ($hilighted>0);
167         $data->{info} =~ s/\n/<br\/>/g;
168         $data->{day} = format_date($data->{timestamp});
169         push @results, $data;
170         $count++;
171         $hilighted = -$hilighted;
172     }
173     return ($count, \@results);
174 }
175
176 =head2 GetLogs
177
178 $logs = GetLogs($datefrom,$dateto,$user,$module,$action,$object,$info);
179
180 Return: 
181 C<$logs> is a ref to a hash which containts all columns from action_logs
182
183 =cut
184
185 sub GetLogs {
186     my $datefrom = shift;
187     my $dateto   = shift;
188     my $user     = shift;
189     my $module   = shift;
190     my $action   = shift;
191     my $object   = shift;
192     my $info     = shift;
193     
194     my $dbh = C4::Context->dbh;
195     my $query = "
196         SELECT *
197         FROM   action_logs
198         WHERE 1
199     ";
200     $query .= " AND DATE_FORMAT(timestamp, '%Y-%m-%d') >= \"".$datefrom."\" " if $datefrom;
201     $query .= " AND DATE_FORMAT(timestamp, '%Y-%m-%d') <= \"".$dateto."\" " if $dateto;
202     $query .= " AND user LIKE \"%".$user."%\" "     if $user;
203     $query .= " AND module LIKE \"%".$module."%\" " if $module;
204     $query .= " AND action LIKE \"%".$action."%\" " if $action;
205     $query .= " AND object LIKE \"%".$object."%\" " if $object;
206     $query .= " AND info LIKE \"%".$info."%\" "     if $info;
207     
208     my $sth = $dbh->prepare($query);
209     $sth->execute;
210     
211     my @logs;
212     while( my $row = $sth->fetchrow_hashref ) {
213         $row->{$row->{module}} = 1;
214         push @logs , $row;
215     }
216     return \@logs;
217 }
218
219 END { }       # module clean-up code here (global destructor)
220
221 1;
222 __END__
223
224 =back
225
226 =head1 AUTHOR
227
228 Koha Developement team <info@koha.org>
229
230 =cut