Bug 22521: DBRev 18.12.00.055
[koha.git] / Koha / Illrequest / Logger.pm
1 package Koha::Illrequest::Logger;
2
3 # Copyright 2018 PTFS Europe Ltd
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use Modern::Perl;
21 use JSON qw( to_json from_json );
22 use Time::Local;
23
24 use C4::Koha;
25 use C4::Context;
26 use C4::Templates;
27 use C4::Log qw( logaction );
28 use Koha::ActionLogs;
29
30 =head1 NAME
31
32 Koha::Illrequest::Logger - Koha ILL Action / Event logger
33
34 =head1 SYNOPSIS
35
36 Object-oriented class that provides event logging functionality for
37 ILL requests
38
39 =head1 DESCRIPTION
40
41 This class provides the ability to log arbitrary actions or events
42 relating to Illrequest to the action log.
43
44 =head1 API
45
46 =head2 Class Methods
47
48 =head3 new
49
50     my $config = Koha::Illrequest::Logger->new();
51
52 Create a new Koha::Illrequest::Logger object.
53 We also set up what can be logged, how to do it and how to display
54 log entries we get back out
55
56 =cut
57
58 sub new {
59     my ( $class ) = @_;
60     my $self  = {};
61
62     $self->{loggers} = {
63         status => sub {
64             $self->log_status_change(@_);
65         }
66     };
67
68     my ( $htdocs, $theme, $lang, $base ) =
69         C4::Templates::_get_template_file('ill/log/', 'intranet');
70
71     $self->{templates} = {
72         STATUS_CHANGE => $base . 'status_change.tt'
73     };
74
75     bless $self, $class;
76
77     return $self;
78 }
79
80 =head3 log_maybe
81
82     Koha::IllRequest::Logger->log_maybe($params);
83
84 Receive params hashref, containing a request object and an attrs
85 hashref (which may or may not be defined) If the attrs hashref contains
86 a key matching our "loggers" hashref then we want to log it
87
88 =cut
89
90 sub log_maybe {
91     my ($self, $params) = @_;
92
93     if (defined $params->{request} && defined $params->{attrs}) {
94         foreach my $key (keys %{ $params->{attrs} }) {
95             if (defined($self->{loggers}->{$key})) {
96                 $self->{loggers}->{$key}(
97                     $params->{request},
98                     $params->{attrs}->{$key}
99                 );
100             }
101         }
102     }
103 }
104
105 =head3 log_status_change
106
107     Koha::IllRequest::Logger->log_status_change($params);
108
109 Receive a hashref containing a request object and a status to log,
110 and log it
111
112 =cut
113
114 sub log_status_change {
115     my ( $self, $params ) = @_;
116
117     if (defined $params->{request} && defined $params->{value}) {
118         $self->log_something({
119             modulename   => 'ILL',
120             actionname   => 'STATUS_CHANGE',
121             objectnumber => $params->{request}->id,
122             infos        => to_json({
123                 log_origin    => 'core',
124                 status_before => $params->{request}->{previous_status},
125                 status_after  => $params->{value}
126             })
127         });
128     }
129 }
130
131 =head3 log_something
132
133     Koha::IllRequest::Logger->log_something({
134         modulename   => 'ILL',
135         actionname   => 'STATUS_CHANGE',
136         objectnumber => $req->id,
137         infos        => to_json({
138             log_origin    => 'core',
139             status_before => $req->{previous_status},
140             status_after  => $new_status
141         })
142     });
143
144 If we have the required data passed, log an action
145
146 =cut
147
148 sub log_something {
149     my ( $self, $to_log ) = @_;
150
151     if (
152         defined $to_log->{modulename} &&
153         defined $to_log->{actionname} &&
154         defined $to_log->{objectnumber} &&
155         defined $to_log->{infos} &&
156         C4::Context->preference("IllLog")
157     ) {
158         logaction(
159             $to_log->{modulename},
160             $to_log->{actionname},
161             $to_log->{objectnumber},
162             $to_log->{infos}
163         );
164     }
165 }
166
167 =head3 get_log_template
168
169     $template_path = get_log_template($params);
170
171 Given a log's origin and action, get the appropriate display template
172
173 =cut
174
175 sub get_log_template {
176     my ($self, $params) = @_;
177
178     my $origin = $params->{origin};
179     my $action = $params->{action};
180
181     if ($origin eq 'core') {
182         # It's a core log, so we can just get the template path from
183         # the hashref above
184         return $self->{templates}->{$action};
185     } else {
186         # It's probably a backend log, so we need to get the path to the
187         # template from the backend
188         my $backend =$params->{request}->{_my_backend};
189         return $backend->get_log_template_path($action);
190     }
191 }
192
193 =head3 get_request_logs
194
195     $requestlogs = Koha::IllRequest::Logger->get_request_logs($request_id);
196
197 Get all logged actions for a given request
198
199 =cut
200
201 sub get_request_logs {
202     my ( $self, $request ) = @_;
203
204     my $logs = Koha::ActionLogs->search(
205         {
206             module => 'ILL',
207             object => $request->id
208         },
209         { order_by => { -desc => "timestamp" } }
210     )->unblessed;
211
212     # Populate a lookup table for status aliases
213     my $aliases = GetAuthorisedValues('ILLSTATUS');
214     my $alias_hash;
215     foreach my $alias(@{$aliases}) {
216         $alias_hash->{$alias->{authorised_value}} = $alias;
217     }
218     foreach my $log(@{$logs}) {
219         $log->{aliases} = $alias_hash;
220         $log->{info} = from_json($log->{info});
221         $log->{template} = $self->get_log_template({
222             request => $request,
223             origin => $log->{info}->{log_origin},
224             action => $log->{action}
225         });
226     }
227
228     return $logs;
229 }
230
231 =head1 AUTHOR
232
233 Andrew Isherwood <andrew.isherwood@ptfs-europe.com>
234
235 =cut
236
237 1;