3 #package to deal with Logging Actions in DB
6 # Copyright 2000-2002 Katipo Communications
8 # This file is part of Koha.
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
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.
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
25 use C4::Dates qw(format_date);
27 use vars qw($VERSION @ISA @EXPORT);
30 # set the version for version checking
34 @EXPORT = qw(&logaction &GetLogStatus &displaylog &GetLogs);
39 C4::Log - Koha Log Facility functions
47 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.
55 &logaction($modulename, $actionname, $objectnumber, $infos);
57 Adds a record into action_logs table to report the different changes upon the database.
58 Each log entry includes the number of the user currently logged in. For batch
59 jobs, which operate without authenticating a user and setting up a session, the user
60 number is set to 0, which is the same as the superlibrarian's number.
66 my ($modulename, $actionname, $objectnumber, $infos)=@_;
68 # Get ID of logged in user. if called from a batch job,
69 # no user session exists and C4::Context->userenv() returns
71 my $userenv = C4::Context->userenv();
72 my $usernumber = (ref($userenv) eq 'HASH') ? $userenv->{'number'} : 0;
74 my $dbh = C4::Context->dbh;
75 my $sth=$dbh->prepare("Insert into action_logs (timestamp,user,module,action,object,info) values (now(),?,?,?,?,?)");
76 $sth->execute($usernumber,$modulename,$actionname,$objectnumber,$infos);
82 $status = GetLogStatus;
84 C<$status> is a hasref like this example:
97 $hash{BorrowersLog} = C4::Context->preference("BorrowersLog");
98 $hash{CataloguingLog} = C4::Context->preference("CataloguingLog");
99 $hash{IssueLog} = C4::Context->preference("IssueLog");
100 $hash{ReturnLog} = C4::Context->preference("ReturnLog");
101 $hash{SubscriptionLog} = C4::Context->preference("SubscriptionLog");
102 $hash{LetterLog} = C4::Context->preference("LetterLog");
103 $hash{FinesLog} = C4::Context->preference("FinesLog");
109 &displaylog($modulename, @filters);
110 $modulename is the name of the module on which the user wants to display logs
111 @filters is an optional table of hash containing :
112 - name : the name of the variable to filter
113 - value : the value of the filter.... May be with * joker
115 returns a table of hash containing who did what on which object at what time
121 my ($modulename, @filters) = @_;
122 my $dbh = C4::Context->dbh;
124 SELECT action_logs.timestamp, action_logs.action, action_logs.info,
125 borrowers.cardnumber, borrowers.surname, borrowers.firstname, borrowers.userid,
126 biblio.biblionumber, biblio.title, biblio.author
128 LEFT JOIN borrowers ON borrowers.borrowernumber=action_logs.user
129 LEFT JOIN biblio ON action_logs.object=biblio.biblionumber
130 WHERE action_logs.module = 'cataloguing'
133 if ($modulename eq "catalogue" or $modulename eq "acqui") {
135 user => 'borrowers.surname',
136 title => 'biblio.title',
137 author => 'biblio.author',
139 } elsif ($modulename eq "members") {
141 SELECT action_logs.timestamp, action_logs.action, action_logs.info,
142 borrowers.cardnumber, borrowers.surname, borrowers.firstname, borrowers.userid,
143 bor2.cardnumber, bor2.surname, bor2.firstname, bor2.userid
145 LEFT JOIN borrowers ON borrowers.borrowernumber=action_logs.user
146 LEFT JOIN borrowers as bor2 ON action_logs.object=bor2.borrowernumber
147 WHERE action_logs.module = 'members'
150 user => 'borrowers.surname',
151 surname => 'bor2.surname',
152 firstname => 'bor2.firstname',
153 cardnumber => 'bor2.cardnumber',
160 foreach my $filter (@filters) {
161 my $tempname = $filter->{name} or next;
162 (grep {/^$tempname$/} keys %filtermap) or next;
163 $filter->{value} =~ s/\*/%/g;
164 $strsth .= " AND " . $filtermap{$tempname} . " LIKE " . $filter->{value};
167 my $sth=$dbh->prepare($strsth);
172 while (my $data = $sth->fetchrow_hashref){
173 $data->{hilighted} = ($hilighted>0);
174 $data->{info} =~ s/\n/<br\/>/g;
175 $data->{day} = format_date($data->{timestamp});
176 push @results, $data;
178 $hilighted = -$hilighted;
180 return ($count, \@results);
185 $logs = GetLogs($datefrom,$dateto,$user,$module,$action,$object,$info);
188 C<$logs> is a ref to a hash which containts all columns from action_logs
193 my $datefrom = shift;
201 my $iso_datefrom = C4::Dates->new($datefrom,C4::Context->preference("dateformat"))->output('iso');
202 my $iso_dateto = C4::Dates->new($dateto,C4::Context->preference("dateformat"))->output('iso');
204 my $dbh = C4::Context->dbh;
210 $query .= " AND DATE_FORMAT(timestamp, '%Y-%m-%d') >= \"".$iso_datefrom."\" " if $iso_datefrom;
211 $query .= " AND DATE_FORMAT(timestamp, '%Y-%m-%d') <= \"".$iso_dateto."\" " if $iso_dateto;
212 $query .= " AND user LIKE \"%".$user."%\" " if $user;
213 $query .= " AND module LIKE \"%".$module."%\" " if $module;
214 $query .= " AND action LIKE \"%".$action."%\" " if $action;
215 $query .= " AND object LIKE \"%".$object."%\" " if $object;
216 $query .= " AND info LIKE \"%".$info."%\" " if $info;
218 my $sth = $dbh->prepare($query);
222 while( my $row = $sth->fetchrow_hashref ) {
223 $row->{$row->{module}} = 1;
236 Koha Developement team <info@koha.org>