Bug 26265: (QA follow-up) Remove g option from regex, add few dirs
[koha-equinox.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 # Copyright 2011 MJ Ray and software.coop
8 #
9 # This file is part of Koha.
10 #
11 # Koha is free software; you can redistribute it and/or modify it
12 # under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 3 of the License, or
14 # (at your option) any later version.
15 #
16 # Koha is distributed in the hope that it will be useful, but
17 # WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License
22 # along with Koha; if not, see <http://www.gnu.org/licenses>.
23
24 use strict;
25 use warnings;
26
27 use JSON qw( to_json );
28
29 use C4::Context;
30 use Koha::DateUtils;
31 use Koha::Logger;
32
33 use vars qw(@ISA @EXPORT);
34
35 BEGIN {
36         require Exporter;
37         @ISA = qw(Exporter);
38         @EXPORT = qw(&logaction &cronlogaction &GetLogs);
39 }
40
41 =head1 NAME
42
43 C4::Log - Koha Log Facility functions
44
45 =head1 SYNOPSIS
46
47   use C4::Log;
48
49 =head1 DESCRIPTION
50
51 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.
52
53 =head1 FUNCTIONS
54
55 =over 2
56
57 =item logaction
58
59   &logaction($modulename, $actionname, $objectnumber, $infos, $interface);
60
61 Adds a record into action_logs table to report the different changes upon the database.
62 Each log entry includes the number of the user currently logged in.  For batch
63 jobs, which operate without authenticating a user and setting up a session, the user
64 number is set to 0, which is the same as the superlibrarian's number.
65
66 =cut
67
68 #'
69 sub logaction {
70     my ($modulename, $actionname, $objectnumber, $infos, $interface)=@_;
71
72     # Get ID of logged in user.  if called from a batch job,
73     # no user session exists and C4::Context->userenv() returns
74     # the scalar '0'.
75     my $userenv = C4::Context->userenv();
76     my $usernumber = (ref($userenv) eq 'HASH') ? $userenv->{'number'} : 0;
77     $usernumber ||= 0;
78     $interface //= C4::Context->interface;
79
80     my $dbh = C4::Context->dbh;
81     my $sth=$dbh->prepare("Insert into action_logs (timestamp,user,module,action,object,info,interface) values (now(),?,?,?,?,?,?)");
82     $sth->execute($usernumber,$modulename,$actionname,$objectnumber,$infos,$interface);
83     $sth->finish;
84
85     my $logger = Koha::Logger->get(
86         {
87             interface => 'intranet',
88             category  => "ActionLogs.$modulename.$actionname"
89         }
90     );
91     $logger->debug(
92         sub {
93             "ACTION LOG: " . to_json(
94                 {
95                     user   => $usernumber,
96                     module => $modulename,
97                     action => $actionname,
98                     object => $objectnumber,
99                     info   => $infos
100                 }
101             );
102         }
103     );
104 }
105
106 =item cronlogaction
107
108   &cronlogaction($infos);
109
110 Convenience routine to add a record into action_logs table from a cron job.
111 Logs the path and name of the calling script plus the information privided by param $infos.
112
113 =cut
114
115 #'
116 sub cronlogaction {
117     my ($infos)=@_;
118     my $loginfo = (caller(0))[1];
119     $loginfo .= ' ' . $infos if $infos;
120     logaction( 'CRONJOBS', 'Run', undef, $loginfo ) if C4::Context->preference('CronjobLog');
121 }
122
123 =item GetLogs
124
125 $logs = GetLogs($datefrom,$dateto,$user,\@modules,$action,$object,$info);
126
127 Return:
128 C<$logs> is a ref to a hash which contains all columns from action_logs
129
130 =cut
131
132 sub GetLogs {
133     my $datefrom = shift;
134     my $dateto   = shift;
135     my $user     = shift;
136     my $modules  = shift;
137     my $action   = shift;
138     my $object   = shift;
139     my $info     = shift;
140     my $interfaces = shift;
141
142     my $iso_datefrom = $datefrom ? output_pref({ dt => dt_from_string( $datefrom ), dateformat => 'iso', dateonly => 1 }) : undef;
143     my $iso_dateto = $dateto ? output_pref({ dt => dt_from_string( $dateto ), dateformat => 'iso', dateonly => 1 }) : undef;
144
145     $user ||= q{};
146
147     my $dbh   = C4::Context->dbh;
148     my $query = "
149         SELECT *
150         FROM   action_logs
151         WHERE 1
152     ";
153
154     my @parameters;
155     $query .=
156       " AND DATE_FORMAT(timestamp, '%Y-%m-%d') >= \"" . $iso_datefrom . "\" "
157       if $iso_datefrom;    #fix me - mysql specific
158     $query .=
159       " AND DATE_FORMAT(timestamp, '%Y-%m-%d') <= \"" . $iso_dateto . "\" "
160       if $iso_dateto;
161     if ( $user ne q{} ) {
162         $query .= " AND user = ? ";
163         push( @parameters, $user );
164     }
165     if ( $modules && scalar(@$modules) ) {
166         $query .=
167           " AND module IN (" . join( ",", map { "?" } @$modules ) . ") ";
168         push( @parameters, @$modules );
169     }
170     if ( $action && scalar(@$action) ) {
171         $query .= " AND action IN (" . join( ",", map { "?" } @$action ) . ") ";
172         push( @parameters, @$action );
173     }
174     if ($object) {
175         $query .= " AND object = ? ";
176         push( @parameters, $object );
177     }
178     if ($info) {
179         $query .= " AND info LIKE ? ";
180         push( @parameters, "%" . $info . "%" );
181     }
182     if ( $interfaces && scalar(@$interfaces) ) {
183         $query .=
184           " AND interface IN (" . join( ",", map { "?" } @$interfaces ) . ") ";
185         push( @parameters, @$interfaces );
186     }
187
188     my $sth = $dbh->prepare($query);
189     $sth->execute(@parameters);
190
191     my @logs;
192     while ( my $row = $sth->fetchrow_hashref ) {
193         push @logs, $row;
194     }
195     return \@logs;
196 }
197
198 1;
199 __END__
200
201 =back
202
203 =head1 AUTHOR
204
205 Koha Development Team <http://koha-community.org/>
206
207 =cut