fix for bug 2488: OPACItemsResultsDisplay/singlebranchmode
[koha.git] / C4 / Log.pm
blob8fb4488feb269a308bcb65a83135c552aadb71ed
1 package C4::Log;
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
13 # version.
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
23 use strict;
24 use C4::Context;
25 use C4::Dates qw(format_date);
27 use vars qw($VERSION @ISA @EXPORT);
29 BEGIN {
30 # set the version for version checking
31 $VERSION = 3.01;
32 require Exporter;
33 @ISA = qw(Exporter);
34 @EXPORT = qw(&logaction &GetLogStatus &displaylog &GetLogs);
37 =head1 NAME
39 C4::Log - Koha Log Facility functions
41 =head1 SYNOPSIS
43 use C4::Log;
45 =head1 DESCRIPTION
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.
49 =head1 FUNCTIONS
51 =over 2
53 =item logaction
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.
62 =cut
65 sub logaction {
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
70 # the scalar '0'.
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);
77 $sth->finish;
80 =item GetLogStatus
82 $status = GetLogStatus;
84 C<$status> is a hasref like this example:
85 $hash = {
86 BorrowersLog => 1,
87 CataloguingLog => 0,
88 IssueLog => 0,
89 ...
92 =cut
95 sub GetLogStatus {
96 my %hash;
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");
104 return \%hash;
107 =item displaylog
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
117 =cut
120 sub displaylog {
121 my ($modulename, @filters) = @_;
122 my $dbh = C4::Context->dbh;
123 my $strsth=qq|
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
127 FROM action_logs
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'
132 my %filtermap = ();
133 if ($modulename eq "catalogue" or $modulename eq "acqui") {
134 %filtermap = (
135 user => 'borrowers.surname',
136 title => 'biblio.title',
137 author => 'biblio.author',
139 } elsif ($modulename eq "members") {
140 $strsth=qq|
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
144 FROM action_logs
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'
149 %filtermap = (
150 user => 'borrowers.surname',
151 surname => 'bor2.surname',
152 firstname => 'bor2.firstname',
153 cardnumber => 'bor2.cardnumber',
155 } else {
156 return 0;
159 if (@filters) {
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);
168 $sth->execute;
169 my @results;
170 my $count;
171 my $hilighted=1;
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;
177 $count++;
178 $hilighted = -$hilighted;
180 return ($count, \@results);
183 =item GetLogs
185 $logs = GetLogs($datefrom,$dateto,$user,$module,$action,$object,$info);
187 Return:
188 C<$logs> is a ref to a hash which containts all columns from action_logs
190 =cut
192 sub GetLogs {
193 my $datefrom = shift;
194 my $dateto = shift;
195 my $user = shift;
196 my $module = shift;
197 my $action = shift;
198 my $object = shift;
199 my $info = 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;
205 my $query = "
206 SELECT *
207 FROM action_logs
208 WHERE 1
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);
219 $sth->execute;
221 my @logs;
222 while( my $row = $sth->fetchrow_hashref ) {
223 $row->{$row->{module}} = 1;
224 push @logs , $row;
226 return \@logs;
230 __END__
232 =back
234 =head1 AUTHOR
236 Koha Developement team <info@koha.org>
238 =cut