Bug 26922: Regression tests
[koha.git] / Koha / Illrequest / Logger.pm
blob971b3a8b761ab8573fe6b8927cbc86432e314513
1 package Koha::Illrequest::Logger;
3 # Copyright 2018 PTFS Europe Ltd
5 # This file is part of Koha.
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.
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.
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>.
20 use Modern::Perl;
21 use JSON qw( to_json from_json );
22 use Time::Local;
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;
31 =head1 NAME
33 Koha::Illrequest::Logger - Koha ILL Action / Event logger
35 =head1 SYNOPSIS
37 Object-oriented class that provides event logging functionality for
38 ILL requests
40 =head1 DESCRIPTION
42 This class provides the ability to log arbitrary actions or events
43 relating to Illrequest to the action log.
45 =head1 API
47 =head2 Class Methods
49 =head3 new
51 my $config = Koha::Illrequest::Logger->new();
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
57 =cut
59 sub new {
60 my ( $class ) = @_;
61 my $self = {};
63 $self->{loggers} = {
64 status => sub {
65 $self->log_status_change(@_);
67 patron_notice => sub {
68 $self->log_patron_notice(@_);
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);
76 $self->{templates} = {
77 STATUS_CHANGE => $base . 'status_change.tt',
78 PATRON_NOTICE => $base . 'patron_notice.tt'
81 bless $self, $class;
83 return $self;
86 =head3 log_maybe
88 Koha::IllRequest::Logger->log_maybe($params);
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
94 =cut
96 sub log_maybe {
97 my ($self, $params) = @_;
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}
111 =head3 log_patron_notice
113 Koha::IllRequest::Logger->log_patron_notice($params);
115 Receive a hashref containing a request object and params to log,
116 and log it
118 =cut
120 sub log_patron_notice {
121 my ( $self, $params ) = @_;
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}
136 =head3 log_status_change
138 Koha::IllRequest::Logger->log_status_change($params);
140 Receive a hashref containing a request object and a status to log,
141 and log it
143 =cut
145 sub log_status_change {
146 my ( $self, $params ) = @_;
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}
162 =head3 log_something
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
175 If we have the required data passed, log an action
177 =cut
179 sub log_something {
180 my ( $self, $to_log ) = @_;
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")
189 logaction(
190 $to_log->{modulename},
191 $to_log->{actionname},
192 $to_log->{objectnumber},
193 $to_log->{infos}
198 =head3 get_log_template
200 $template_path = get_log_template($params);
202 Given a log's origin and action, get the appropriate display template
204 =cut
206 sub get_log_template {
207 my ($self, $params) = @_;
209 my $origin = $params->{origin};
210 my $action = $params->{action};
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);
224 =head3 get_request_logs
226 $requestlogs = Koha::IllRequest::Logger->get_request_logs($request_id);
228 Get all logged actions for a given request
230 =cut
232 sub get_request_logs {
233 my ( $self, $request ) = @_;
235 my $logs = Koha::ActionLogs->search(
237 module => 'ILL',
238 object => $request->id
240 { order_by => { -desc => "timestamp" } }
241 )->unblessed;
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;
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;
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}
268 return $logs;
271 =head1 AUTHOR
273 Andrew Isherwood <andrew.isherwood@ptfs-europe.com>
275 =cut