Bug 29407: Make the pickup locations dropdown JS reusable
[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 # 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::Logger;
31
32 use vars qw(@ISA @EXPORT);
33
34 BEGIN {
35         require Exporter;
36         @ISA = qw(Exporter);
37         @EXPORT = qw(logaction cronlogaction);
38 }
39
40 =head1 NAME
41
42 C4::Log - Koha Log Facility functions
43
44 =head1 SYNOPSIS
45
46   use C4::Log;
47
48 =head1 DESCRIPTION
49
50 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
52 =head1 FUNCTIONS
53
54 =over 2
55
56 =item logaction
57
58   &logaction($modulename, $actionname, $objectnumber, $infos, $interface);
59
60 Adds a record into action_logs table to report the different changes upon the database.
61 Each log entry includes the number of the user currently logged in.  For batch
62 jobs, which operate without authenticating a user and setting up a session, the user
63 number is set to 0, which is the same as the superlibrarian's number.
64
65 =cut
66
67 #'
68 sub logaction {
69     my ($modulename, $actionname, $objectnumber, $infos, $interface)=@_;
70
71     # Get ID of logged in user.  if called from a batch job,
72     # no user session exists and C4::Context->userenv() returns
73     # the scalar '0'.
74     my $userenv = C4::Context->userenv();
75     my $usernumber = (ref($userenv) eq 'HASH') ? $userenv->{'number'} : 0;
76     $usernumber ||= 0;
77     $interface //= C4::Context->interface;
78
79     my $dbh = C4::Context->dbh;
80     my $sth=$dbh->prepare("Insert into action_logs (timestamp,user,module,action,object,info,interface) values (now(),?,?,?,?,?,?)");
81     $sth->execute($usernumber,$modulename,$actionname,$objectnumber,$infos,$interface);
82     $sth->finish;
83
84     my $logger = Koha::Logger->get(
85         {
86             interface => $interface,
87             category  => "ActionLogs.$modulename.$actionname"
88         }
89     );
90     $logger->debug(
91         sub {
92             "ACTION LOG: " . to_json(
93                 {
94                     user   => $usernumber,
95                     module => $modulename,
96                     action => $actionname,
97                     object => $objectnumber,
98                     info   => $infos
99                 }
100             );
101         }
102     );
103 }
104
105 =item cronlogaction
106
107   &cronlogaction($infos);
108
109 Convenience routine to add a record into action_logs table from a cron job.
110 Logs the path and name of the calling script plus the information privided by param $infos.
111
112 =cut
113
114 #'
115 sub cronlogaction {
116     my ($infos)=@_;
117     my $loginfo = (caller(0))[1];
118     $loginfo .= ' ' . $infos if $infos;
119     logaction( 'CRONJOBS', 'Run', undef, $loginfo ) if C4::Context->preference('CronjobLog');
120 }
121
122 1;
123 __END__
124
125 =back
126
127 =head1 AUTHOR
128
129 Koha Development Team <http://koha-community.org/>
130
131 =cut