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
20 # with Koha; if not, write to the Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 use C4::Dates qw(format_date);
29 use vars qw($VERSION @ISA @EXPORT);
32 # set the version for version checking
36 @EXPORT = qw(&logaction &GetLogStatus &displaylog &GetLogs);
41 C4::Log - Koha Log Facility functions
49 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.
57 &logaction($modulename, $actionname, $objectnumber, $infos);
59 Adds a record into action_logs table to report the different changes upon the database.
60 Each log entry includes the number of the user currently logged in. For batch
61 jobs, which operate without authenticating a user and setting up a session, the user
62 number is set to 0, which is the same as the superlibrarian's number.
68 my ($modulename, $actionname, $objectnumber, $infos)=@_;
70 # Get ID of logged in user. if called from a batch job,
71 # no user session exists and C4::Context->userenv() returns
73 my $userenv = C4::Context->userenv();
74 my $usernumber = (ref($userenv) eq 'HASH') ? $userenv->{'number'} : 0;
76 my $dbh = C4::Context->dbh;
77 my $sth=$dbh->prepare("Insert into action_logs (timestamp,user,module,action,object,info) values (now(),?,?,?,?,?)");
78 $sth->execute($usernumber,$modulename,$actionname,$objectnumber,$infos);
84 $status = GetLogStatus;
86 C<$status> is a hasref like this example:
99 $hash{BorrowersLog} = C4::Context->preference("BorrowersLog");
100 $hash{CataloguingLog} = C4::Context->preference("CataloguingLog");
101 $hash{IssueLog} = C4::Context->preference("IssueLog");
102 $hash{ReturnLog} = C4::Context->preference("ReturnLog");
103 $hash{SubscriptionLog} = C4::Context->preference("SubscriptionLog");
104 $hash{LetterLog} = C4::Context->preference("LetterLog");
105 $hash{FinesLog} = C4::Context->preference("FinesLog");
111 &displaylog($modulename, @filters);
112 $modulename is the name of the module on which the user wants to display logs
113 @filters is an optional table of hash containing :
114 - name : the name of the variable to filter
115 - value : the value of the filter.... May be with * joker
117 returns a table of hash containing who did what on which object at what time
123 my ($modulename, @filters) = @_;
124 my $dbh = C4::Context->dbh;
126 SELECT action_logs.timestamp, action_logs.action, action_logs.info,
127 borrowers.cardnumber, borrowers.surname, borrowers.firstname, borrowers.userid,
128 biblio.biblionumber, biblio.title, biblio.author
130 LEFT JOIN borrowers ON borrowers.borrowernumber=action_logs.user
131 LEFT JOIN biblio ON action_logs.object=biblio.biblionumber
132 WHERE action_logs.module = 'cataloguing'
135 if ($modulename eq "catalogue" or $modulename eq "acqui") {
137 user => 'borrowers.surname',
138 title => 'biblio.title',
139 author => 'biblio.author',
141 } elsif ($modulename eq "members") {
143 SELECT action_logs.timestamp, action_logs.action, action_logs.info,
144 borrowers.cardnumber, borrowers.surname, borrowers.firstname, borrowers.userid,
145 bor2.cardnumber, bor2.surname, bor2.firstname, bor2.userid
147 LEFT JOIN borrowers ON borrowers.borrowernumber=action_logs.user
148 LEFT JOIN borrowers as bor2 ON action_logs.object=bor2.borrowernumber
149 WHERE action_logs.module = 'members'
152 user => 'borrowers.surname',
153 surname => 'bor2.surname',
154 firstname => 'bor2.firstname',
155 cardnumber => 'bor2.cardnumber',
162 foreach my $filter (@filters) {
163 my $tempname = $filter->{name} or next;
164 (grep {/^$tempname$/} keys %filtermap) or next;
165 $filter->{value} =~ s/\*/%/g;
166 $strsth .= " AND " . $filtermap{$tempname} . " LIKE " . $filter->{value};
169 my $sth=$dbh->prepare($strsth);
174 while (my $data = $sth->fetchrow_hashref){
175 $data->{hilighted} = ($hilighted>0);
176 $data->{info} =~ s/\n/<br\/>/g;
177 $data->{day} = format_date($data->{timestamp});
178 push @results, $data;
180 $hilighted = -$hilighted;
182 return ($count, \@results);
187 $logs = GetLogs($datefrom,$dateto,$user,\@modules,$action,$object,$info);
190 C<$logs> is a ref to a hash which containts all columns from action_logs
195 my $datefrom = shift;
203 my $iso_datefrom = C4::Dates->new($datefrom,C4::Context->preference("dateformat"))->output('iso');
204 my $iso_dateto = C4::Dates->new($dateto,C4::Context->preference("dateformat"))->output('iso');
206 my $dbh = C4::Context->dbh;
214 $query .= " AND DATE_FORMAT(timestamp, '%Y-%m-%d') >= \"".$iso_datefrom."\" " if $iso_datefrom; #fix me - mysql specific
215 $query .= " AND DATE_FORMAT(timestamp, '%Y-%m-%d') <= \"".$iso_dateto."\" " if $iso_dateto;
217 $query .= " AND user = ? ";
218 push(@parameters,$user);
220 if(scalar @$modules > 1 or @$modules[0] ne "") {
221 $query .= " AND module IN (".join(",",map {"?"} @$modules).") ";
222 push(@parameters,@$modules);
224 if($action && scalar(@$action)) {
225 $query .= " AND action IN (".join(",",map {"?"} @$action).") ";
226 push(@parameters,@$action);
229 $query .= " AND object = ? ";
230 push(@parameters,$object);
233 $query .= " AND info LIKE ? ";
234 push(@parameters,"%".$info."%");
237 my $sth = $dbh->prepare($query);
238 $sth->execute(@parameters);
241 while( my $row = $sth->fetchrow_hashref ) {
242 $row->{$row->{module}} = 1;
255 Koha Development Team <http://koha-community.org/>