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 );
29 use Koha
::Notice
::Template
;
33 Koha::Illrequest::Logger - Koha ILL Action / Event logger
37 Object-oriented class that provides event logging functionality for
42 This class provides the ability to log arbitrary actions or events
43 relating to Illrequest to the action log.
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
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'
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
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}(
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,
120 sub log_patron_notice
{
121 my ( $self, $params ) = @_;
123 if (defined $params->{request
} && defined $params->{notice_code
}) {
124 $self->log_something({
126 actionname
=> 'PATRON_NOTICE',
127 objectnumber
=> $params->{request
}->id,
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,
145 sub log_status_change
{
146 my ( $self, $params ) = @_;
148 if (defined $params->{request
} && defined $params->{value
}) {
149 $self->log_something({
151 actionname
=> 'STATUS_CHANGE',
152 objectnumber
=> $params->{request
}->id,
154 log_origin
=> 'core',
155 status_before
=> $params->{request
}->{previous_status
},
156 status_after
=> $params->{value
}
164 Koha::IllRequest::Logger->log_something({
166 actionname => 'STATUS_CHANGE',
167 objectnumber => $req->id,
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
180 my ( $self, $to_log ) = @_;
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")
190 $to_log->{modulename
},
191 $to_log->{actionname
},
192 $to_log->{objectnumber
},
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
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
215 return $self->{templates
}->{$action};
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
232 sub get_request_logs
{
233 my ( $self, $request ) = @_;
235 my $logs = Koha
::ActionLogs
->search(
238 object
=> $request->id
240 { order_by
=> { -desc
=> "timestamp" } }
243 # Populate a lookup table for all ILL notice types
244 my $notice_types = Koha
::Notice
::Templates
->search({
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');
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({
263 origin
=> $log->{info
}->{log_origin
},
264 action
=> $log->{action
}
273 Andrew Isherwood <andrew.isherwood@ptfs-europe.com>