Bug 34820: (QA follow-up) add filter, update CSV output
[koha.git] / tools / viewlog.pl
1 #!/usr/bin/perl
2
3 # Copyright 2010 BibLibre
4 # Copyright 2011 MJ Ray and software.coop
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 use Modern::Perl;
22
23 use C4::Auth qw( get_template_and_user );
24 use CGI qw ( -utf8 );
25 use Text::CSV::Encoded;
26 use C4::Context;
27 use C4::Output qw( output_html_with_http_headers );
28 use C4::Serials qw( CountSubscriptionFromBiblionumber );
29 use C4::Search qw( enabled_staff_search_views );
30
31 use Koha::ActionLogs;
32 use Koha::Database;
33 use Koha::DateUtils qw( dt_from_string );
34 use Koha::Items;
35 use Koha::Patrons;
36 use Koha::Recalls;
37
38
39 =head1 viewlog.pl
40
41 plugin that shows stats
42
43 =cut
44
45 my $input = CGI->new;
46
47 my $do_it    = $input->param('do_it');
48 my @modules  = $input->multi_param("modules");
49 my $user     = $input->param("user") // '';
50 my @actions  = $input->multi_param("actions");
51 my @interfaces  = $input->multi_param("interfaces");
52 my $object   = $input->param("object");
53 my $object_type = $input->param("object_type") // '';
54 my $info     = $input->param("info");
55 my $datefrom = $input->param("from");
56 my $dateto   = $input->param("to");
57 my $basename = $input->param("basename");
58 my $output   = $input->param("output") || "screen";
59 my $src      = $input->param("src") || ""; # this param allows us to be told where we were called from -fbcit
60
61 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
62     {
63         template_name   => "tools/viewlog.tt",
64         query           => $input,
65         type            => "intranet",
66         flagsrequired   => { tools => 'view_system_logs' },
67     }
68 );
69
70 if ( $src eq 'circ' ) {
71
72     my $borrowernumber = $object;
73     my $patron = Koha::Patrons->find( $borrowernumber );
74     my $circ_info = 1;
75     unless ( $patron ) {
76          $circ_info = 0;
77     }
78
79     $template->param(
80         patron      => $patron,
81         circulation => $circ_info,
82     );
83 }
84
85 $template->param(
86     C4::Search::enabled_staff_search_views,
87     subscriptionsnumber => ( $object ? CountSubscriptionFromBiblionumber($object) : 0 ),
88     object => $object,
89 );
90
91 if ($do_it) {
92     my $dtf = Koha::Database->new->schema->storage->datetime_parser;
93     my %search_params;
94
95     if ($datefrom and $dateto ) {
96         my $dateto_endday = dt_from_string($dateto);
97         $dateto_endday->set( # We set last second of day to see all log from that day
98             hour => 23,
99             minute => 59,
100             second => 59
101         );
102         $search_params{'timestamp'} = {
103             -between => [
104                 $dtf->format_datetime( dt_from_string($datefrom) ),
105                 $dtf->format_datetime( $dateto_endday ),
106             ]
107         };
108     } elsif ($datefrom) {
109         $search_params{'timestamp'} = {
110             '>=' => $dtf->format_datetime( dt_from_string($datefrom) )
111         };
112     } elsif ($dateto) {
113         my $dateto_endday = dt_from_string($dateto);
114         $dateto_endday->set( # We set last second of day to see all log from that day
115             hour => 23,
116             minute => 59,
117             second => 59
118         );
119         $search_params{'timestamp'} = {
120             '<=' => $dtf->format_datetime( $dateto_endday )
121         };
122     }
123     # Circulation uses RENEWAL, but Patrons uses RENEW, this helps to find both
124     if ( grep { $_ eq 'RENEW'} @actions ) { push @actions, 'RENEWAL' };
125
126     $search_params{user} = $user if $user;
127     $search_params{module} = { -in => [ @modules ] } if ( defined $modules[0] and $modules[0] ne '' ) ;
128     $search_params{action} = { -in => [ @actions ] } if ( defined $actions[0] && $actions[0] ne '' );
129     $search_params{interface} = { -in => [ @interfaces ] } if ( defined $interfaces[0] && $interfaces[0] ne '' );
130
131     if ( @modules == 1 && $object_type eq 'biblio' ) {
132         # Handle 'Modification log' from cataloguing
133         my @itemnumbers = Koha::Items->search({ biblionumber => $object })->get_column('itemnumber');
134         $search_params{'-or'} = [
135             { -and => { object => $object, info => { -like => 'biblio%' }}},
136             { -and => { object => \@itemnumbers, info => { -like => 'item%' }}},
137         ];
138     } else {
139         $search_params{info} = { -like => '%' . $info . '%' } if $info;
140         $search_params{object} = $object if $object;
141     }
142
143     my @logs = Koha::ActionLogs->search(\%search_params)->as_list;
144
145     my @data;
146     foreach my $log (@logs) {
147         my $result = $log->unblessed;
148         # Init additional columns for CSV export
149         $result->{'biblionumber'}      = q{};
150         $result->{'biblioitemnumber'}  = q{};
151         $result->{'barcode'}           = q{};
152
153         if ( substr( $log->info, 0, 4 ) eq 'item' || $log->module eq "CIRCULATION" ) {
154
155             # get item information so we can create a working link
156             my $itemnumber = $log->object;
157             $itemnumber = $log->info if ( $log->module eq "CIRCULATION" );
158             my $item = Koha::Items->find($itemnumber);
159             if ($item) {
160                 $result->{'object_found'}     = 1;
161                 $result->{'biblionumber'}     = $item->biblionumber;
162                 $result->{'biblioitemnumber'} = $item->biblionumber;
163                 $result->{'barcode'}          = $item->barcode;
164             }
165         }
166
167         #always add firstname and surname for librarian/user
168         if ( $log->user ) {
169             my $patron = Koha::Patrons->find( $log->user );
170             if ($patron && $output eq 'screen') {
171                 $result->{librarian} = $patron;
172             }
173         }
174
175         #add firstname and surname for borrower, when using the CIRCULATION, MEMBERS, FINES
176         if ( $log->module eq "CIRCULATION" || $log->module eq "MEMBERS" || $log->module eq "FINES" ) {
177             if ( $log->object ) {
178                 my $patron = Koha::Patrons->find( $log->object );
179                 if ($patron && $output eq 'screen') {
180                     $result->{patron} = $patron;
181                 }
182             }
183         }
184
185         if ( $log->module eq 'NOTICES' ) {
186             if ( $log->object ) {
187                 my $notice = Koha::Notice::Templates->find( { id => $log->object } );
188                 if ($notice && $output eq 'screen') {
189                     $result->{notice} = $notice->unblessed;
190                 }
191             }
192         }
193
194         # get recall information
195         if ( $log->module eq "RECALLS" ) {
196             if ( $log->object ) {
197                 my $recall = Koha::Recalls->find( $log->object );
198                 if ( $recall && $output eq 'screen' ) {
199                     $result->{recall} = $recall;
200                 }
201             }
202         }
203         push @data, $result;
204     }
205     if ( $output eq "screen" ) {
206
207         # Printing results to screen
208         $template->param(
209             logview  => 1,
210             total    => scalar @data,
211             looprow  => \@data,
212             do_it    => 1,
213             datefrom => $datefrom,
214             dateto   => $dateto,
215             user     => $user,
216             info     => $info,
217             src      => $src,
218             modules  => \@modules,
219             actions  => \@actions,
220             interfaces => \@interfaces
221         );
222
223         # Used modules
224         foreach my $module (@modules) {
225             $template->param( $module => 1 );
226         }
227         output_html_with_http_headers $input, $cookie, $template->output;
228     }
229     else {
230
231         # Printing to a csv file
232         my $content = q{};
233         if (@data) {
234             my $delimiter = C4::Context->csv_delimiter;
235             my $csv = Text::CSV::Encoded->new( { encoding_out => 'utf8', sep_char => $delimiter } );
236             $csv or die "Text::CSV::Encoded->new FAILED: " . Text::CSV::Encoded->error_diag();
237
238             # First line with heading
239             # Exporting bd id seems useless
240             my @headings = grep { $_ ne 'action_id' } sort keys %{$data[0]};
241             if ( $csv->combine(@headings) ) {
242                 $content .= $csv->string() . "\n";
243             }
244
245             # Lines of logs
246             foreach my $line (@data) {
247                 my @cells = map { $line->{$_} } @headings;
248                 if ( $csv->combine(@cells) ) {
249                     $content .= $csv->string() . "\n";
250                 }
251             }
252         }
253
254         # Output
255         print $input->header(
256             -type       => 'text/csv',
257             -attachment => $basename . '.csv',
258         );
259         print $content;
260     }
261     exit;
262 }
263 else {
264     output_html_with_http_headers $input, $cookie, $template->output;
265 }