971b3a8b761ab8573fe6b8927cbc86432e314513
[srvgit] / 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( 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 use Koha::Notice::Template;
30
31 =head1 NAME
32
33 Koha::Illrequest::Logger - Koha ILL Action / Event logger
34
35 =head1 SYNOPSIS
36
37 Object-oriented class that provides event logging functionality for
38 ILL requests
39
40 =head1 DESCRIPTION
41
42 This class provides the ability to log arbitrary actions or events
43 relating to Illrequest to the action log.
44
45 =head1 API
46
47 =head2 Class Methods
48
49 =head3 new
50
51     my $config = Koha::Illrequest::Logger->new();
52
53 Create a new Koha::Illrequest::Logger object.
54 We also set up what can be logged, how to do it and how to display
55 log entries we get back out
56
57 =cut
58
59 sub new {
60     my ( $class ) = @_;
61     my $self  = {};
62
63     $self->{loggers} = {
64         status => sub {
65             $self->log_status_change(@_);
66         },
67         patron_notice => sub {
68             $self->log_patron_notice(@_);
69         }
70     };
71
72     my $query = CGI->new; # To keep C4::Templates::_get_template_file() from complaining
73     my ( $htdocs, $theme, $lang, $base ) =
74         C4::Templates::_get_template_file('ill/log/', 'intranet', $query);
75
76     $self->{templates} = {
77         STATUS_CHANGE => $base . 'status_change.tt',
78         PATRON_NOTICE => $base . 'patron_notice.tt'
79     };
80
81     bless $self, $class;
82
83     return $self;
84 }
85
86 =head3 log_maybe
87
88     Koha::IllRequest::Logger->log_maybe($params);
89
90 Receive params hashref, containing a request object and an attrs
91 hashref (which may or may not be defined) If the attrs hashref contains
92 a key matching our "loggers" hashref then we want to log it
93
94 =cut
95
96 sub log_maybe {
97     my ($self, $params) = @_;
98
99     if (defined $params->{request} && defined $params->{attrs}) {
100         foreach my $key (keys %{ $params->{attrs} }) {
101             if (defined($self->{loggers}->{$key})) {
102                 $self->{loggers}->{$key}(
103                     $params->{request},
104                     $params->{attrs}->{$key}
105                 );
106             }
107         }
108     }
109 }
110
111 =head3 log_patron_notice
112
113     Koha::IllRequest::Logger->log_patron_notice($params);
114
115 Receive a hashref containing a request object and params to log,
116 and log it
117
118 =cut
119
120 sub log_patron_notice {
121     my ( $self, $params ) = @_;
122
123     if (defined $params->{request} && defined $params->{notice_code}) {
124         $self->log_something({
125             modulename   => 'ILL',
126             actionname   => 'PATRON_NOTICE',
127             objectnumber => $params->{request}->id,
128             infos        => to_json({
129                 log_origin    => 'core',
130                 notice_code => $params->{notice_code}
131             })
132         });
133     }
134 }
135
136 =head3 log_status_change
137
138     Koha::IllRequest::Logger->log_status_change($params);
139
140 Receive a hashref containing a request object and a status to log,
141 and log it
142
143 =cut
144
145 sub log_status_change {
146     my ( $self, $params ) = @_;
147
148     if (defined $params->{request} && defined $params->{value}) {
149         $self->log_something({
150             modulename   => 'ILL',
151             actionname   => 'STATUS_CHANGE',
152             objectnumber => $params->{request}->id,
153             infos        => to_json({
154                 log_origin    => 'core',
155                 status_before => $params->{request}->{previous_status},
156                 status_after  => $params->{value}
157             })
158         });
159     }
160 }
161
162 =head3 log_something
163
164     Koha::IllRequest::Logger->log_something({
165         modulename   => 'ILL',
166         actionname   => 'STATUS_CHANGE',
167         objectnumber => $req->id,
168         infos        => to_json({
169             log_origin    => 'core',
170             status_before => $req->{previous_status},
171             status_after  => $new_status
172         })
173     });
174
175 If we have the required data passed, log an action
176
177 =cut
178
179 sub log_something {
180     my ( $self, $to_log ) = @_;
181
182     if (
183         defined $to_log->{modulename} &&
184         defined $to_log->{actionname} &&
185         defined $to_log->{objectnumber} &&
186         defined $to_log->{infos} &&
187         C4::Context->preference("IllLog")
188     ) {
189         logaction(
190             $to_log->{modulename},
191             $to_log->{actionname},
192             $to_log->{objectnumber},
193             $to_log->{infos}
194         );
195     }
196 }
197
198 =head3 get_log_template
199
200     $template_path = get_log_template($params);
201
202 Given a log's origin and action, get the appropriate display template
203
204 =cut
205
206 sub get_log_template {
207     my ($self, $params) = @_;
208
209     my $origin = $params->{origin};
210     my $action = $params->{action};
211
212     if ($origin eq 'core') {
213         # It's a core log, so we can just get the template path from
214         # the hashref above
215         return $self->{templates}->{$action};
216     } else {
217         # It's probably a backend log, so we need to get the path to the
218         # template from the backend
219         my $backend =$params->{request}->{_my_backend};
220         return $backend->get_log_template_path($action);
221     }
222 }
223
224 =head3 get_request_logs
225
226     $requestlogs = Koha::IllRequest::Logger->get_request_logs($request_id);
227
228 Get all logged actions for a given request
229
230 =cut
231
232 sub get_request_logs {
233     my ( $self, $request ) = @_;
234
235     my $logs = Koha::ActionLogs->search(
236         {
237             module => 'ILL',
238             object => $request->id
239         },
240         { order_by => { -desc => "timestamp" } }
241     )->unblessed;
242
243     # Populate a lookup table for all ILL notice types
244     my $notice_types = Koha::Notice::Templates->search({
245         module => 'ill'
246     })->unblessed;
247     my $notice_hash;
248     foreach my $notice(@{$notice_types}) {
249         $notice_hash->{$notice->{code}} = $notice;
250     }
251     # Populate a lookup table for status aliases
252     my $aliases = C4::Koha::GetAuthorisedValues('ILLSTATUS');
253     my $alias_hash;
254     foreach my $alias(@{$aliases}) {
255         $alias_hash->{$alias->{authorised_value}} = $alias;
256     }
257     foreach my $log(@{$logs}) {
258         $log->{notice_types} = $notice_hash;
259         $log->{aliases} = $alias_hash;
260         $log->{info} = from_json($log->{info});
261         $log->{template} = $self->get_log_template({
262             request => $request,
263             origin => $log->{info}->{log_origin},
264             action => $log->{action}
265         });
266     }
267
268     return $logs;
269 }
270
271 =head1 AUTHOR
272
273 Andrew Isherwood <andrew.isherwood@ptfs-europe.com>
274
275 =cut
276
277 1;