MT1883 : Serials enddate was not cleanly used
[koha.git] / C4 / Log.pm
blob5774fbaed3d79a651879d6cfd60c2bf834ce9d78
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 warnings;
26 use C4::Context;
27 use C4::Dates qw(format_date);
29 use vars qw($VERSION @ISA @EXPORT);
31 BEGIN {
32 # set the version for version checking
33 $VERSION = 3.01;
34 require Exporter;
35 @ISA = qw(Exporter);
36 @EXPORT = qw(&logaction &GetLogStatus &displaylog &GetLogs);
39 =head1 NAME
41 C4::Log - Koha Log Facility functions
43 =head1 SYNOPSIS
45 use C4::Log;
47 =head1 DESCRIPTION
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.
51 =head1 FUNCTIONS
53 =over 2
55 =item logaction
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.
64 =cut
67 sub logaction {
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
72 # the scalar '0'.
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);
79 $sth->finish;
82 =item GetLogStatus
84 $status = GetLogStatus;
86 C<$status> is a hasref like this example:
87 $hash = {
88 BorrowersLog => 1,
89 CataloguingLog => 0,
90 IssueLog => 0,
91 ...
94 =cut
97 sub GetLogStatus {
98 my %hash;
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");
106 return \%hash;
109 =item displaylog
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
119 =cut
122 sub displaylog {
123 my ($modulename, @filters) = @_;
124 my $dbh = C4::Context->dbh;
125 my $strsth=qq|
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
129 FROM action_logs
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'
134 my %filtermap = ();
135 if ($modulename eq "catalogue" or $modulename eq "acqui") {
136 %filtermap = (
137 user => 'borrowers.surname',
138 title => 'biblio.title',
139 author => 'biblio.author',
141 } elsif ($modulename eq "members") {
142 $strsth=qq|
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
146 FROM action_logs
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'
151 %filtermap = (
152 user => 'borrowers.surname',
153 surname => 'bor2.surname',
154 firstname => 'bor2.firstname',
155 cardnumber => 'bor2.cardnumber',
157 } else {
158 return 0;
161 if (@filters) {
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);
170 $sth->execute;
171 my @results;
172 my $count;
173 my $hilighted=1;
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;
179 $count++;
180 $hilighted = -$hilighted;
182 return ($count, \@results);
185 =item GetLogs
187 $logs = GetLogs($datefrom,$dateto,$user,\@modules,$action,$object,$info);
189 Return:
190 C<$logs> is a ref to a hash which containts all columns from action_logs
192 =cut
194 sub GetLogs {
195 my $datefrom = shift;
196 my $dateto = shift;
197 my $user = shift;
198 my $modules = shift;
199 my $action = shift;
200 my $object = shift;
201 my $info = 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;
207 my $query = "
208 SELECT *
209 FROM action_logs
210 WHERE 1
213 my @parameters;
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;
216 if($user) {
217 $query .= " AND user LIKE ? ";
218 push(@parameters,"%".$user."%");
220 if(scalar @$modules > 1 or @$modules[0] ne "") {
221 $query .= " AND (1 = 2"; #always false but used to build the query
222 foreach my $module (@$modules) {
223 next if $module eq "";
224 $query .= " or module = ?";
225 push(@parameters,$module);
227 $query .= ")";
229 if($action) {
230 $query .= " AND action LIKE ? ";
231 push(@parameters,"%".$action."%");
233 if($object) {
234 $query .= " AND object LIKE ? ";
235 push(@parameters,"%".$object."%");
237 if($info) {
238 $query .= " AND info LIKE ? ";
239 push(@parameters,"%".$info."%");
242 my $sth = $dbh->prepare($query);
243 $sth->execute(@parameters);
245 my @logs;
246 while( my $row = $sth->fetchrow_hashref ) {
247 $row->{$row->{module}} = 1;
248 push @logs , $row;
250 return \@logs;
254 __END__
256 =back
258 =head1 AUTHOR
260 Koha Developement team <info@koha.org>
262 =cut