3 #package to deal with Logging Actions in DB
6 # Copyright 2000-2002 Katipo Communications
7 # Copyright 2011 MJ Ray and software.coop
9 # This file is part of Koha.
11 # Koha is free software; you can redistribute it and/or modify it under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 2 of the License, or (at your option) any later
16 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License along
21 # with Koha; if not, write to the Free Software Foundation, Inc.,
22 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 use C4
::Dates
qw(format_date);
30 use vars
qw($VERSION @ISA @EXPORT);
33 # set the version for version checking
34 $VERSION = 3.07.00.049;
37 @EXPORT = qw(&logaction &GetLogStatus &displaylog &GetLogs);
42 C4::Log - Koha Log Facility functions
50 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.
58 &logaction($modulename, $actionname, $objectnumber, $infos);
60 Adds a record into action_logs table to report the different changes upon the database.
61 Each log entry includes the number of the user currently logged in. For batch
62 jobs, which operate without authenticating a user and setting up a session, the user
63 number is set to 0, which is the same as the superlibrarian's number.
69 my ($modulename, $actionname, $objectnumber, $infos)=@_;
71 # Get ID of logged in user. if called from a batch job,
72 # no user session exists and C4::Context->userenv() returns
74 my $userenv = C4
::Context
->userenv();
75 my $usernumber = (ref($userenv) eq 'HASH') ?
$userenv->{'number'} : 0;
78 my $dbh = C4
::Context
->dbh;
79 my $sth=$dbh->prepare("Insert into action_logs (timestamp,user,module,action,object,info) values (now(),?,?,?,?,?)");
80 $sth->execute($usernumber,$modulename,$actionname,$objectnumber,$infos);
86 $status = GetLogStatus;
88 C<$status> is a hasref like this example:
101 $hash{BorrowersLog
} = C4
::Context
->preference("BorrowersLog");
102 $hash{CataloguingLog
} = C4
::Context
->preference("CataloguingLog");
103 $hash{IssueLog
} = C4
::Context
->preference("IssueLog");
104 $hash{ReturnLog
} = C4
::Context
->preference("ReturnLog");
105 $hash{SubscriptionLog
} = C4
::Context
->preference("SubscriptionLog");
106 $hash{LetterLog
} = C4
::Context
->preference("LetterLog");
107 $hash{FinesLog
} = C4
::Context
->preference("FinesLog");
113 &displaylog($modulename, @filters);
114 $modulename is the name of the module on which the user wants to display logs
115 @filters is an optional table of hash containing :
116 - name : the name of the variable to filter
117 - value : the value of the filter.... May be with * joker
119 returns a table of hash containing who did what on which object at what time
125 my ($modulename, @filters) = @_;
126 my $dbh = C4
::Context
->dbh;
128 SELECT action_logs
.timestamp
, action_logs
.action
, action_logs
.info
,
129 borrowers
.cardnumber
, borrowers
.surname
, borrowers
.firstname
, borrowers
.userid
,
130 biblio
.biblionumber
, biblio
.title
, biblio
.author
132 LEFT JOIN borrowers ON borrowers
.borrowernumber
=action_logs
.user
133 LEFT JOIN biblio ON action_logs
.object
=biblio
.biblionumber
134 WHERE action_logs
.module
= 'cataloguing'
137 if ($modulename eq "catalogue" or $modulename eq "acqui") {
139 user
=> 'borrowers.surname',
140 title
=> 'biblio.title',
141 author
=> 'biblio.author',
143 } elsif ($modulename eq "members") {
145 SELECT action_logs
.timestamp
, action_logs
.action
, action_logs
.info
,
146 borrowers
.cardnumber
, borrowers
.surname
, borrowers
.firstname
, borrowers
.userid
,
147 bor2
.cardnumber
, bor2
.surname
, bor2
.firstname
, bor2
.userid
149 LEFT JOIN borrowers ON borrowers
.borrowernumber
=action_logs
.user
150 LEFT JOIN borrowers as bor2 ON action_logs
.object
=bor2
.borrowernumber
151 WHERE action_logs
.module
= 'members'
154 user
=> 'borrowers.surname',
155 surname
=> 'bor2.surname',
156 firstname
=> 'bor2.firstname',
157 cardnumber
=> 'bor2.cardnumber',
164 foreach my $filter (@filters) {
165 my $tempname = $filter->{name
} or next;
166 (grep {/^$tempname$/} keys %filtermap) or next;
167 $filter->{value
} =~ s/\*/%/g;
168 $strsth .= " AND " . $filtermap{$tempname} . " LIKE " . $filter->{value
};
171 my $sth=$dbh->prepare($strsth);
176 while (my $data = $sth->fetchrow_hashref){
177 $data->{hilighted
} = ($hilighted>0);
178 $data->{info
} =~ s/\n/<br\/>/g
;
179 $data->{day
} = format_date
($data->{timestamp
});
180 push @results, $data;
182 $hilighted = -$hilighted;
184 return ($count, \
@results);
189 $logs = GetLogs($datefrom,$dateto,$user,\@modules,$action,$object,$info);
192 C<$logs> is a ref to a hash which containts all columns from action_logs
197 my $datefrom = shift;
205 my $iso_datefrom = C4
::Dates
->new($datefrom,C4
::Context
->preference("dateformat"))->output('iso');
206 my $iso_dateto = C4
::Dates
->new($dateto,C4
::Context
->preference("dateformat"))->output('iso');
208 my $dbh = C4
::Context
->dbh;
216 $query .= " AND DATE_FORMAT(timestamp, '%Y-%m-%d') >= \"".$iso_datefrom."\" " if $iso_datefrom; #fix me - mysql specific
217 $query .= " AND DATE_FORMAT(timestamp, '%Y-%m-%d') <= \"".$iso_dateto."\" " if $iso_dateto;
219 $query .= " AND user = ? ";
220 push(@parameters,$user);
222 if($modules && scalar(@
$modules)) {
223 $query .= " AND module IN (".join(",",map {"?"} @
$modules).") ";
224 push(@parameters,@
$modules);
226 if($action && scalar(@
$action)) {
227 $query .= " AND action IN (".join(",",map {"?"} @
$action).") ";
228 push(@parameters,@
$action);
231 $query .= " AND object = ? ";
232 push(@parameters,$object);
235 $query .= " AND info LIKE ? ";
236 push(@parameters,"%".$info."%");
239 my $sth = $dbh->prepare($query);
240 $sth->execute(@parameters);
243 while( my $row = $sth->fetchrow_hashref ) {
244 $row->{$row->{module
}} = 1;
257 Koha Development Team <http://koha-community.org/>