Bug 18309: UNIMARC update from IFLA - authority (fr) (FA)
[koha.git] / Koha / Illrequest / Logger.pm
blob0625370237a1a4740c239ac1caff55442f1204de
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 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.
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.
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.
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;
30 =head1 NAME
32 Koha::Illrequest::Logger - Koha ILL Action / Event logger
34 =head1 SYNOPSIS
36 Object-oriented class that provides event logging functionality for
37 ILL requests
39 =head1 DESCRIPTION
41 This class provides the ability to log arbitrary actions or events
42 relating to Illrequest to the action log.
44 =head1 API
46 =head2 Class Methods
48 =head3 new
50 my $config = Koha::Illrequest::Logger->new();
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
56 =cut
58 sub new {
59 my ( $class ) = @_;
60 my $self = {};
62 $self->{loggers} = {
63 status => sub {
64 $self->log_status_change(@_);
68 my ( $htdocs, $theme, $lang, $base ) =
69 C4::Templates::_get_template_file('ill/log/', 'intranet');
71 $self->{templates} = {
72 STATUS_CHANGE => $base . 'status_change.tt'
75 bless $self, $class;
77 return $self;
80 =head3 log_maybe
82 Koha::IllRequest::Logger->log_maybe($params);
84 Receive params hashref, containing a request object and an attrs
85 hashref (which may or may not be defined) If the attrs hashref contains
86 a key matching our "loggers" hashref then we want to log it
88 =cut
90 sub log_maybe {
91 my ($self, $params) = @_;
93 if (defined $params->{request} && defined $params->{attrs}) {
94 foreach my $key (keys %{ $params->{attrs} }) {
95 if (defined($self->{loggers}->{$key})) {
96 $self->{loggers}->{$key}(
97 $params->{request},
98 $params->{attrs}->{$key}
105 =head3 log_status_change
107 Koha::IllRequest::Logger->log_status_change($params);
109 Receive a hashref containing a request object and a status to log,
110 and log it
112 =cut
114 sub log_status_change {
115 my ( $self, $params ) = @_;
117 if (defined $params->{request} && defined $params->{value}) {
118 $self->log_something({
119 modulename => 'ILL',
120 actionname => 'STATUS_CHANGE',
121 objectnumber => $params->{request}->id,
122 infos => to_json({
123 log_origin => 'core',
124 status_before => $params->{request}->{previous_status},
125 status_after => $params->{value}
131 =head3 log_something
133 Koha::IllRequest::Logger->log_something({
134 modulename => 'ILL',
135 actionname => 'STATUS_CHANGE',
136 objectnumber => $req->id,
137 infos => to_json({
138 log_origin => 'core',
139 status_before => $req->{previous_status},
140 status_after => $new_status
144 If we have the required data passed, log an action
146 =cut
148 sub log_something {
149 my ( $self, $to_log ) = @_;
151 if (
152 defined $to_log->{modulename} &&
153 defined $to_log->{actionname} &&
154 defined $to_log->{objectnumber} &&
155 defined $to_log->{infos} &&
156 C4::Context->preference("IllLog")
158 logaction(
159 $to_log->{modulename},
160 $to_log->{actionname},
161 $to_log->{objectnumber},
162 $to_log->{infos}
167 =head3 get_log_template
169 $template_path = get_log_template($params);
171 Given a log's origin and action, get the appropriate display template
173 =cut
175 sub get_log_template {
176 my ($self, $params) = @_;
178 my $origin = $params->{origin};
179 my $action = $params->{action};
181 if ($origin eq 'core') {
182 # It's a core log, so we can just get the template path from
183 # the hashref above
184 return $self->{templates}->{$action};
185 } else {
186 # It's probably a backend log, so we need to get the path to the
187 # template from the backend
188 my $backend =$params->{request}->{_my_backend};
189 return $backend->get_log_template_path($action);
193 =head3 get_request_logs
195 $requestlogs = Koha::IllRequest::Logger->get_request_logs($request_id);
197 Get all logged actions for a given request
199 =cut
201 sub get_request_logs {
202 my ( $self, $request ) = @_;
204 my $logs = Koha::ActionLogs->search(
206 module => 'ILL',
207 object => $request->id
209 { order_by => { -desc => "timestamp" } }
210 )->unblessed;
212 # Populate a lookup table for status aliases
213 my $aliases = GetAuthorisedValues('ILLSTATUS');
214 my $alias_hash;
215 foreach my $alias(@{$aliases}) {
216 $alias_hash->{$alias->{authorised_value}} = $alias;
218 foreach my $log(@{$logs}) {
219 $log->{aliases} = $alias_hash;
220 $log->{info} = from_json($log->{info});
221 $log->{template} = $self->get_log_template({
222 request => $request,
223 origin => $log->{info}->{log_origin},
224 action => $log->{action}
228 return $logs;
231 =head1 AUTHOR
233 Andrew Isherwood <andrew.isherwood@ptfs-europe.com>
235 =cut