Bug 24545: Fix license statements
[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
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 $query = new CGI; # To keep C4::Templates::_get_template_file() from complaining
69     my ( $htdocs, $theme, $lang, $base ) =
70         C4::Templates::_get_template_file('ill/log/', 'intranet', $query);
71
72     $self->{templates} = {
73         STATUS_CHANGE => $base . 'status_change.tt'
74     };
75
76     bless $self, $class;
77
78     return $self;
79 }
80
81 =head3 log_maybe
82
83     Koha::IllRequest::Logger->log_maybe($params);
84
85 Receive params hashref, containing a request object and an attrs
86 hashref (which may or may not be defined) If the attrs hashref contains
87 a key matching our "loggers" hashref then we want to log it
88
89 =cut
90
91 sub log_maybe {
92     my ($self, $params) = @_;
93
94     if (defined $params->{request} && defined $params->{attrs}) {
95         foreach my $key (keys %{ $params->{attrs} }) {
96             if (defined($self->{loggers}->{$key})) {
97                 $self->{loggers}->{$key}(
98                     $params->{request},
99                     $params->{attrs}->{$key}
100                 );
101             }
102         }
103     }
104 }
105
106 =head3 log_status_change
107
108     Koha::IllRequest::Logger->log_status_change($params);
109
110 Receive a hashref containing a request object and a status to log,
111 and log it
112
113 =cut
114
115 sub log_status_change {
116     my ( $self, $params ) = @_;
117
118     if (defined $params->{request} && defined $params->{value}) {
119         $self->log_something({
120             modulename   => 'ILL',
121             actionname   => 'STATUS_CHANGE',
122             objectnumber => $params->{request}->id,
123             infos        => to_json({
124                 log_origin    => 'core',
125                 status_before => $params->{request}->{previous_status},
126                 status_after  => $params->{value}
127             })
128         });
129     }
130 }
131
132 =head3 log_something
133
134     Koha::IllRequest::Logger->log_something({
135         modulename   => 'ILL',
136         actionname   => 'STATUS_CHANGE',
137         objectnumber => $req->id,
138         infos        => to_json({
139             log_origin    => 'core',
140             status_before => $req->{previous_status},
141             status_after  => $new_status
142         })
143     });
144
145 If we have the required data passed, log an action
146
147 =cut
148
149 sub log_something {
150     my ( $self, $to_log ) = @_;
151
152     if (
153         defined $to_log->{modulename} &&
154         defined $to_log->{actionname} &&
155         defined $to_log->{objectnumber} &&
156         defined $to_log->{infos} &&
157         C4::Context->preference("IllLog")
158     ) {
159         logaction(
160             $to_log->{modulename},
161             $to_log->{actionname},
162             $to_log->{objectnumber},
163             $to_log->{infos}
164         );
165     }
166 }
167
168 =head3 get_log_template
169
170     $template_path = get_log_template($params);
171
172 Given a log's origin and action, get the appropriate display template
173
174 =cut
175
176 sub get_log_template {
177     my ($self, $params) = @_;
178
179     my $origin = $params->{origin};
180     my $action = $params->{action};
181
182     if ($origin eq 'core') {
183         # It's a core log, so we can just get the template path from
184         # the hashref above
185         return $self->{templates}->{$action};
186     } else {
187         # It's probably a backend log, so we need to get the path to the
188         # template from the backend
189         my $backend =$params->{request}->{_my_backend};
190         return $backend->get_log_template_path($action);
191     }
192 }
193
194 =head3 get_request_logs
195
196     $requestlogs = Koha::IllRequest::Logger->get_request_logs($request_id);
197
198 Get all logged actions for a given request
199
200 =cut
201
202 sub get_request_logs {
203     my ( $self, $request ) = @_;
204
205     my $logs = Koha::ActionLogs->search(
206         {
207             module => 'ILL',
208             object => $request->id
209         },
210         { order_by => { -desc => "timestamp" } }
211     )->unblessed;
212
213     # Populate a lookup table for status aliases
214     my $aliases = GetAuthorisedValues('ILLSTATUS');
215     my $alias_hash;
216     foreach my $alias(@{$aliases}) {
217         $alias_hash->{$alias->{authorised_value}} = $alias;
218     }
219     foreach my $log(@{$logs}) {
220         $log->{aliases} = $alias_hash;
221         $log->{info} = from_json($log->{info});
222         $log->{template} = $self->get_log_template({
223             request => $request,
224             origin => $log->{info}->{log_origin},
225             action => $log->{action}
226         });
227     }
228
229     return $logs;
230 }
231
232 =head1 AUTHOR
233
234 Andrew Isherwood <andrew.isherwood@ptfs-europe.com>
235
236 =cut
237
238 1;