Bug 20473: Whitespace
[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
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21 use JSON qw( from_json to_json );
22
23 use C4::Koha qw( GetAuthorisedValues );
24 use C4::Context;
25 use C4::Templates;
26 use C4::Log qw( logaction );
27 use Koha::ActionLogs;
28 use Koha::Notice::Template;
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         patron_notice => sub {
67             $self->log_patron_notice(@_);
68         }
69     };
70
71     my $query = CGI->new; # To keep C4::Templates::_get_template_file() from complaining
72     my ( $htdocs, $theme, $lang, $base ) =
73         C4::Templates::_get_template_file('ill/log/', 'intranet', $query);
74
75     $self->{templates} = {
76         STATUS_CHANGE => $base . 'status_change.tt',
77         PATRON_NOTICE => $base . 'patron_notice.tt'
78     };
79
80     bless $self, $class;
81
82     return $self;
83 }
84
85 =head3 log_maybe
86
87     Koha::IllRequest::Logger->log_maybe($params);
88
89 Receive params hashref, containing a request object and an attrs
90 hashref (which may or may not be defined) If the attrs hashref contains
91 a key matching our "loggers" hashref then we want to log it
92
93 =cut
94
95 sub log_maybe {
96     my ($self, $params) = @_;
97
98     if (defined $params->{request} && defined $params->{attrs}) {
99         foreach my $key (keys %{ $params->{attrs} }) {
100             if (defined($self->{loggers}->{$key})) {
101                 $self->{loggers}->{$key}(
102                     $params->{request},
103                     $params->{attrs}->{$key}
104                 );
105             }
106         }
107     }
108 }
109
110 =head3 log_patron_notice
111
112     Koha::IllRequest::Logger->log_patron_notice($params);
113
114 Receive a hashref containing a request object and params to log,
115 and log it
116
117 =cut
118
119 sub log_patron_notice {
120     my ( $self, $params ) = @_;
121
122     if (defined $params->{request} && defined $params->{notice_code}) {
123         $self->log_something({
124             modulename   => 'ILL',
125             actionname   => 'PATRON_NOTICE',
126             objectnumber => $params->{request}->id,
127             infos        => to_json({
128                 log_origin    => 'core',
129                 notice_code => $params->{notice_code}
130             })
131         });
132     }
133 }
134
135 =head3 log_status_change
136
137     Koha::IllRequest::Logger->log_status_change($params);
138
139 Receive a hashref containing a request object and a status to log,
140 and log it
141
142 =cut
143
144 sub log_status_change {
145     my ( $self, $params ) = @_;
146
147     if (defined $params->{request} && defined $params->{value}) {
148         $self->log_something({
149             modulename   => 'ILL',
150             actionname   => 'STATUS_CHANGE',
151             objectnumber => $params->{request}->id,
152             infos        => to_json({
153                 log_origin    => 'core',
154                 status_before => $params->{request}->{previous_status},
155                 status_after  => $params->{value}
156             })
157         });
158     }
159 }
160
161 =head3 log_something
162
163     Koha::IllRequest::Logger->log_something({
164         modulename   => 'ILL',
165         actionname   => 'STATUS_CHANGE',
166         objectnumber => $req->id,
167         infos        => to_json({
168             log_origin    => 'core',
169             status_before => $req->{previous_status},
170             status_after  => $new_status
171         })
172     });
173
174 If we have the required data passed, log an action
175
176 =cut
177
178 sub log_something {
179     my ( $self, $to_log ) = @_;
180
181     if (
182         defined $to_log->{modulename} &&
183         defined $to_log->{actionname} &&
184         defined $to_log->{objectnumber} &&
185         defined $to_log->{infos} &&
186         C4::Context->preference("IllLog")
187     ) {
188         logaction(
189             $to_log->{modulename},
190             $to_log->{actionname},
191             $to_log->{objectnumber},
192             $to_log->{infos}
193         );
194     }
195 }
196
197 =head3 get_log_template
198
199     $template_path = get_log_template($params);
200
201 Given a log's origin and action, get the appropriate display template
202
203 =cut
204
205 sub get_log_template {
206     my ($self, $params) = @_;
207
208     my $origin = $params->{origin};
209     my $action = $params->{action};
210
211     if ($origin eq 'core') {
212         # It's a core log, so we can just get the template path from
213         # the hashref above
214         return $self->{templates}->{$action};
215     } else {
216         # It's probably a backend log, so we need to get the path to the
217         # template from the backend
218         #
219         # We need to load the backend that this log was made from, so we
220         # can get the template
221         $params->{request}->load_backend($origin);
222         my $backend =$params->{request}->{_my_backend};
223         return $backend->get_log_template_path($action);
224     }
225 }
226
227 =head3 get_request_logs
228
229     $requestlogs = Koha::IllRequest::Logger->get_request_logs($request_id);
230
231 Get all logged actions for a given request
232
233 =cut
234
235 sub get_request_logs {
236     my ( $self, $request ) = @_;
237
238     my $logs = Koha::ActionLogs->search(
239         {
240             module => 'ILL',
241             object => $request->id
242         },
243         { order_by => { -desc => "timestamp" } }
244     )->unblessed;
245
246     # Populate a lookup table for all ILL notice types
247     my $notice_types = Koha::Notice::Templates->search({
248         module => 'ill'
249     })->unblessed;
250     my $notice_hash;
251     foreach my $notice(@{$notice_types}) {
252         $notice_hash->{$notice->{code}} = $notice;
253     }
254     # Populate a lookup table for status aliases
255     my $aliases = C4::Koha::GetAuthorisedValues('ILLSTATUS');
256     my $alias_hash;
257     foreach my $alias(@{$aliases}) {
258         $alias_hash->{$alias->{authorised_value}} = $alias;
259     }
260     foreach my $log(@{$logs}) {
261         $log->{notice_types} = $notice_hash;
262         $log->{aliases} = $alias_hash;
263         $log->{info} = from_json($log->{info});
264         $log->{template} = $self->get_log_template({
265             request => $request,
266             origin => $log->{info}->{log_origin},
267             action => $log->{action}
268         });
269     }
270
271     return $logs;
272 }
273
274 =head1 AUTHOR
275
276 Andrew Isherwood <andrew.isherwood@ptfs-europe.com>
277
278 =cut
279
280 1;