opac.css - button background fix for input.icon
[koha.git] / C4 / Log.pm
1 package C4::Log;
2
3 #package to deal with Logging Actions in DB
4
5
6 # Copyright 2000-2002 Katipo Communications
7 #
8 # This file is part of Koha.
9 #
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.
14 #
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.
18 #
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
22
23 use strict;
24 use C4::Context;
25 use C4::Dates qw(format_date);
26
27 use vars qw($VERSION @ISA @EXPORT);
28
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);
35 }
36
37 =head1 NAME
38
39 C4::Log - Koha Log Facility functions
40
41 =head1 SYNOPSIS
42
43   use C4::Log;
44
45 =head1 DESCRIPTION
46
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.
48
49 =head1 FUNCTIONS
50
51 =over 2
52
53 =item logaction
54
55   &logaction($usernumber, $modulename, $actionname, $objectnumber, $infos);
56
57 Adds a record into action_logs table to report the different changes upon the database
58
59 =cut
60
61 #'
62 sub logaction {
63   my ($usernumber,$modulename, $actionname, $objectnumber, $infos)=@_;
64     $usernumber='' unless $usernumber;
65     my $dbh = C4::Context->dbh;
66     my $sth=$dbh->prepare("Insert into action_logs (timestamp,user,module,action,object,info) values (now(),?,?,?,?,?)");
67     $sth->execute($usernumber,$modulename,$actionname,$objectnumber,$infos);
68     $sth->finish;
69 }
70
71 =item GetLogStatus
72
73   $status = GetLogStatus;
74
75 C<$status> is a hasref like this example:
76     $hash = {
77         BorrowersLog   => 1,
78         CataloguingLog => 0,
79         IssueLog       => 0,
80         ...
81     }
82
83 =cut
84
85 #'
86 sub GetLogStatus {
87     my %hash;
88     $hash{BorrowersLog}    = C4::Context->preference("BorrowersLog");
89     $hash{CataloguingLog}  = C4::Context->preference("CataloguingLog");
90     $hash{IssueLog}        = C4::Context->preference("IssueLog");
91     $hash{ReturnLog}       = C4::Context->preference("ReturnLog");
92     $hash{SubscriptionLog} = C4::Context->preference("SubscriptionLog");
93     $hash{LetterLog}       = C4::Context->preference("LetterLog");
94     $hash{FinesLog}        = C4::Context->preference("FinesLog");
95     return \%hash;
96 }
97
98 =item displaylog
99
100   &displaylog($modulename, @filters);
101   $modulename is the name of the module on which the user wants to display logs
102   @filters is an optional table of hash containing :
103       - name : the name of the variable to filter
104     - value : the value of the filter.... May be with * joker
105
106 returns a table of hash containing who did what on which object at what time
107
108 =cut
109
110 #'
111 sub displaylog {
112   my ($modulename, @filters) = @_;
113     my $dbh = C4::Context->dbh;
114     my $strsth=qq|
115                 SELECT action_logs.timestamp, action_logs.action, action_logs.info,
116                                 borrowers.cardnumber, borrowers.surname, borrowers.firstname, borrowers.userid,
117                         biblio.biblionumber, biblio.title, biblio.author
118         FROM action_logs 
119                 LEFT JOIN borrowers ON borrowers.borrowernumber=action_logs.user 
120         LEFT JOIN  biblio   ON action_logs.object=biblio.biblionumber
121         WHERE action_logs.module = 'cataloguing' 
122         |;
123         my %filtermap = ();
124     if ($modulename eq "catalogue" or $modulename eq "acqui") {
125                 %filtermap = (
126                           user => 'borrowers.surname',
127                          title => 'biblio.title',
128                         author => 'biblio.author',
129                 );
130     } elsif ($modulename eq "members") {
131         $strsth=qq|
132                 SELECT action_logs.timestamp, action_logs.action, action_logs.info, 
133                         borrowers.cardnumber, borrowers.surname, borrowers.firstname, borrowers.userid,
134                         bor2.cardnumber, bor2.surname, bor2.firstname, bor2.userid
135         FROM action_logs 
136                 LEFT JOIN borrowers ON borrowers.borrowernumber=action_logs.user 
137                 LEFT JOIN borrowers as bor2 ON action_logs.object=bor2.borrowernumber
138         WHERE action_logs.module = 'members' 
139                 |;
140                 %filtermap = (
141                        user => 'borrowers.surname',
142                     surname => 'bor2.surname',
143                   firstname => 'bor2.firstname',
144                  cardnumber => 'bor2.cardnumber',
145                 );
146     } else {
147                 return 0;
148         }
149
150     if (@filters) {
151                 foreach my $filter (@filters) {
152                         my $tempname = $filter->{name}         or next;
153                         (grep {/^$tempname$/} keys %filtermap) or next;
154                         $filter->{value} =~ s/\*/%/g;
155                         $strsth .= " AND " . $filtermap{$tempname} . " LIKE " . $filter->{value};
156                 }
157         }
158     my $sth=$dbh->prepare($strsth);
159     $sth->execute;
160     my @results;
161     my $count;
162     my $hilighted=1;
163     while (my $data = $sth->fetchrow_hashref){
164         $data->{hilighted} = ($hilighted>0);
165         $data->{info} =~ s/\n/<br\/>/g;
166         $data->{day} = format_date($data->{timestamp});
167         push @results, $data;
168         $count++;
169         $hilighted = -$hilighted;
170     }
171     return ($count, \@results);
172 }
173
174 =head2 GetLogs
175
176 $logs = GetLogs($datefrom,$dateto,$user,$module,$action,$object,$info);
177
178 Return: 
179 C<$logs> is a ref to a hash which containts all columns from action_logs
180
181 =cut
182
183 sub GetLogs {
184     my $datefrom = shift;
185     my $dateto   = shift;
186     my $user     = shift;
187     my $module   = shift;
188     my $action   = shift;
189     my $object   = shift;
190     my $info     = shift;
191     
192     my $dbh = C4::Context->dbh;
193     my $query = "
194         SELECT *
195         FROM   action_logs
196         WHERE 1
197     ";
198     $query .= " AND DATE_FORMAT(timestamp, '%Y-%m-%d') >= \"".$datefrom."\" " if $datefrom;
199     $query .= " AND DATE_FORMAT(timestamp, '%Y-%m-%d') <= \"".$dateto."\" " if $dateto;
200     $query .= " AND user LIKE \"%".$user."%\" "     if $user;
201     $query .= " AND module LIKE \"%".$module."%\" " if $module;
202     $query .= " AND action LIKE \"%".$action."%\" " if $action;
203     $query .= " AND object LIKE \"%".$object."%\" " if $object;
204     $query .= " AND info LIKE \"%".$info."%\" "     if $info;
205     
206     my $sth = $dbh->prepare($query);
207     $sth->execute;
208     
209     my @logs;
210     while( my $row = $sth->fetchrow_hashref ) {
211         $row->{$row->{module}} = 1;
212         push @logs , $row;
213     }
214     return \@logs;
215 }
216
217 1;
218 __END__
219
220 =back
221
222 =head1 AUTHOR
223
224 Koha Developement team <info@koha.org>
225
226 =cut