|
Line 0
Link Here
|
|
|
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 |
|
| 23 |
use C4::Context; |
| 24 |
use C4::Templates; |
| 25 |
use C4::Log qw( logaction GetLogs ); |
| 26 |
|
| 27 |
=head1 NAME |
| 28 |
|
| 29 |
Koha::Illrequest::Logger - Koha ILL Action / Event logger |
| 30 |
|
| 31 |
=head1 SYNOPSIS |
| 32 |
|
| 33 |
Object-oriented class that provides event logging functionality for |
| 34 |
ILL requests |
| 35 |
|
| 36 |
=head1 DESCRIPTION |
| 37 |
|
| 38 |
This class provides the ability to log arbitrary actions or events |
| 39 |
relating to Illrequest to the action log. |
| 40 |
|
| 41 |
=head1 API |
| 42 |
|
| 43 |
=head2 Class Methods |
| 44 |
|
| 45 |
=head3 new |
| 46 |
|
| 47 |
my $config = Koha::Illrequest::Logger->new(); |
| 48 |
|
| 49 |
Create a new Koha::Illrequest::Logger object, with skeleton logging data |
| 50 |
We also set up data of what can be logged, how to do it and how to display |
| 51 |
log entries we get back out |
| 52 |
|
| 53 |
=cut |
| 54 |
|
| 55 |
sub new { |
| 56 |
my ( $class ) = @_; |
| 57 |
my $self = {}; |
| 58 |
|
| 59 |
$self->{data} = { |
| 60 |
modulename => 'ILL' |
| 61 |
}; |
| 62 |
|
| 63 |
$self->{loggers} = { |
| 64 |
status => sub { |
| 65 |
$self->log_status_change(@_); |
| 66 |
} |
| 67 |
}; |
| 68 |
|
| 69 |
my ( $htdocs, $theme, $lang, $base ) = |
| 70 |
C4::Templates::_get_template_file('ill/log/', 'intranet'); |
| 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($attrs); |
| 84 |
|
| 85 |
Receive request object and an attributes hashref (which may or may |
| 86 |
not be defined) If the hashref contains a key matching our "loggers" hashref |
| 87 |
then we want to log it |
| 88 |
|
| 89 |
=cut |
| 90 |
|
| 91 |
sub log_maybe { |
| 92 |
my ($self, $req, $attrs) = @_; |
| 93 |
|
| 94 |
if (defined $req && defined $attrs) { |
| 95 |
foreach my $key (keys %{ $attrs }) { |
| 96 |
if (defined($self->{loggers}->{$key})) { |
| 97 |
$self->{loggers}->{$key}($req, $attrs->{$key}); |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
=head3 log_status_change |
| 104 |
|
| 105 |
Koha::IllRequest::Logger->log_status_change(); |
| 106 |
|
| 107 |
Log a request's status change |
| 108 |
|
| 109 |
=cut |
| 110 |
|
| 111 |
sub log_status_change { |
| 112 |
my ( $self, $req, $new_status ) = @_; |
| 113 |
|
| 114 |
$self->set_data({ |
| 115 |
actionname => 'STATUS_CHANGE', |
| 116 |
objectnumber => $req->id, |
| 117 |
infos => to_json({ |
| 118 |
log_origin => 'core', |
| 119 |
status_before => $req->{previous_status}, |
| 120 |
status_after => $new_status |
| 121 |
}) |
| 122 |
}); |
| 123 |
|
| 124 |
$self->log_something(); |
| 125 |
} |
| 126 |
|
| 127 |
=head3 log_something |
| 128 |
|
| 129 |
Koha::IllRequest::Logger->log_something(); |
| 130 |
|
| 131 |
If we have the required data set, log an action |
| 132 |
|
| 133 |
=cut |
| 134 |
|
| 135 |
sub log_something { |
| 136 |
my ( $self ) = @_; |
| 137 |
|
| 138 |
if ( |
| 139 |
defined $self->{data}->{modulename} && |
| 140 |
defined $self->{data}->{actionname} && |
| 141 |
defined $self->{data}->{objectnumber} && |
| 142 |
defined $self->{data}->{infos} && |
| 143 |
C4::Context->preference("IllLog") |
| 144 |
) { |
| 145 |
logaction( |
| 146 |
$self->{data}->{modulename}, |
| 147 |
$self->{data}->{actionname}, |
| 148 |
$self->{data}->{objectnumber}, |
| 149 |
$self->{data}->{infos} |
| 150 |
); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
=head3 set_data |
| 155 |
|
| 156 |
Koha::IllRequest::Logger->set_data({ |
| 157 |
key => 'value', |
| 158 |
key2 => 'value2' |
| 159 |
}); |
| 160 |
|
| 161 |
Set arbitrary data propert(ies) on the logger object |
| 162 |
|
| 163 |
=cut |
| 164 |
|
| 165 |
sub set_data { |
| 166 |
my ( $self, $data ) = @_; |
| 167 |
|
| 168 |
foreach my $key (keys %{ $data }) { |
| 169 |
$self->{data}->{$key} = $data->{$key}; |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
=head3 get_log_template |
| 174 |
|
| 175 |
$template_path = get_log_template($origin, $action); |
| 176 |
|
| 177 |
Given a log's origin and action, get the appropriate display template |
| 178 |
|
| 179 |
=cut |
| 180 |
|
| 181 |
sub get_log_template { |
| 182 |
my ($self, $req, $params) = @_; |
| 183 |
|
| 184 |
my $origin = $params->{origin}; |
| 185 |
my $action = $params->{action}; |
| 186 |
|
| 187 |
if ($origin eq 'core') { |
| 188 |
# It's a core log, so we can just get the template path from |
| 189 |
# the hashref above |
| 190 |
return $self->{templates}->{$action}; |
| 191 |
} else { |
| 192 |
# It's probably a backend log, so we need to get the path to the |
| 193 |
# template from the backend |
| 194 |
my $backend =$req->{_my_backend}; |
| 195 |
return $backend->get_log_template_path($action); |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
=head3 get_request_logs |
| 200 |
|
| 201 |
$requestlogs = Koha::IllRequest::Logger->get_request_logs($request_id); |
| 202 |
|
| 203 |
Get all logged actions for a given request |
| 204 |
|
| 205 |
=cut |
| 206 |
|
| 207 |
sub get_request_logs { |
| 208 |
my ( $self, $request ) = @_; |
| 209 |
|
| 210 |
my $logs = GetLogs( |
| 211 |
undef, |
| 212 |
undef, |
| 213 |
undef, |
| 214 |
['ILL'], |
| 215 |
undef, |
| 216 |
$request->id, |
| 217 |
undef, |
| 218 |
undef |
| 219 |
); |
| 220 |
foreach my $log(@{$logs}) { |
| 221 |
$log->{info} = from_json($log->{info}); |
| 222 |
$log->{template} = $self->get_log_template( |
| 223 |
$request, |
| 224 |
{ |
| 225 |
origin => $log->{info}->{log_origin}, |
| 226 |
action => $log->{action} |
| 227 |
} |
| 228 |
); |
| 229 |
} |
| 230 |
|
| 231 |
my @sorted = sort {$$b{'timestamp'} <=> $$a{'timestamp'}} @{$logs}; |
| 232 |
|
| 233 |
return \@sorted; |
| 234 |
} |
| 235 |
|
| 236 |
=head1 AUTHOR |
| 237 |
|
| 238 |
Andrew Isherwood <andrew.isherwood@ptfs-europe.com> |
| 239 |
|
| 240 |
=cut |
| 241 |
|
| 242 |
1; |