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>.
21 use JSON
qw( to_json from_json );
27 use C4
::Log
qw( logaction );
32 Koha::Illrequest::Logger - Koha ILL Action / Event logger
36 Object-oriented class that provides event logging functionality for
41 This class provides the ability to log arbitrary actions or events
42 relating to Illrequest to the action log.
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
64 $self->log_status_change(@_);
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);
72 $self->{templates
} = {
73 STATUS_CHANGE
=> $base . 'status_change.tt'
83 Koha::IllRequest::Logger->log_maybe($params);
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
92 my ($self, $params) = @_;
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}(
99 $params->{attrs
}->{$key}
106 =head3 log_status_change
108 Koha::IllRequest::Logger->log_status_change($params);
110 Receive a hashref containing a request object and a status to log,
115 sub log_status_change
{
116 my ( $self, $params ) = @_;
118 if (defined $params->{request
} && defined $params->{value
}) {
119 $self->log_something({
121 actionname
=> 'STATUS_CHANGE',
122 objectnumber
=> $params->{request
}->id,
124 log_origin
=> 'core',
125 status_before
=> $params->{request
}->{previous_status
},
126 status_after
=> $params->{value
}
134 Koha::IllRequest::Logger->log_something({
136 actionname => 'STATUS_CHANGE',
137 objectnumber => $req->id,
139 log_origin => 'core',
140 status_before => $req->{previous_status},
141 status_after => $new_status
145 If we have the required data passed, log an action
150 my ( $self, $to_log ) = @_;
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")
160 $to_log->{modulename
},
161 $to_log->{actionname
},
162 $to_log->{objectnumber
},
168 =head3 get_log_template
170 $template_path = get_log_template($params);
172 Given a log's origin and action, get the appropriate display template
176 sub get_log_template
{
177 my ($self, $params) = @_;
179 my $origin = $params->{origin
};
180 my $action = $params->{action
};
182 if ($origin eq 'core') {
183 # It's a core log, so we can just get the template path from
185 return $self->{templates
}->{$action};
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);
194 =head3 get_request_logs
196 $requestlogs = Koha::IllRequest::Logger->get_request_logs($request_id);
198 Get all logged actions for a given request
202 sub get_request_logs
{
203 my ( $self, $request ) = @_;
205 my $logs = Koha
::ActionLogs
->search(
208 object
=> $request->id
210 { order_by
=> { -desc
=> "timestamp" } }
213 # Populate a lookup table for status aliases
214 my $aliases = C4
::Koha
::GetAuthorisedValues
('ILLSTATUS');
216 foreach my $alias(@
{$aliases}) {
217 $alias_hash->{$alias->{authorised_value
}} = $alias;
219 foreach my $log(@
{$logs}) {
220 $log->{aliases
} = $alias_hash;
221 $log->{info
} = from_json
($log->{info
});
222 $log->{template
} = $self->get_log_template({
224 origin
=> $log->{info
}->{log_origin
},
225 action
=> $log->{action
}
234 Andrew Isherwood <andrew.isherwood@ptfs-europe.com>