Bug 22922: Allow to modify hold dates on reserve/request.pl
[koha.git] / Koha / Illrequest / Logger.pm
blobbdd4e8f5ff890cca2aaa301c50e72081b0d940ad
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 $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'
76 bless $self, $class;
78 return $self;
81 =head3 log_maybe
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
89 =cut
91 sub log_maybe {
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}(
98 $params->{request},
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,
111 and log it
113 =cut
115 sub log_status_change {
116 my ( $self, $params ) = @_;
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}
132 =head3 log_something
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
145 If we have the required data passed, log an action
147 =cut
149 sub log_something {
150 my ( $self, $to_log ) = @_;
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")
159 logaction(
160 $to_log->{modulename},
161 $to_log->{actionname},
162 $to_log->{objectnumber},
163 $to_log->{infos}
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
174 =cut
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
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);
194 =head3 get_request_logs
196 $requestlogs = Koha::IllRequest::Logger->get_request_logs($request_id);
198 Get all logged actions for a given request
200 =cut
202 sub get_request_logs {
203 my ( $self, $request ) = @_;
205 my $logs = Koha::ActionLogs->search(
207 module => 'ILL',
208 object => $request->id
210 { order_by => { -desc => "timestamp" } }
211 )->unblessed;
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;
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}
229 return $logs;
232 =head1 AUTHOR
234 Andrew Isherwood <andrew.isherwood@ptfs-europe.com>
236 =cut