Bug 26265: (QA follow-up) Remove g option from regex, add few dirs
[koha-equinox.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;
24 use CGI qw ( -utf8 );
25 use Text::CSV::Encoded;
26 use C4::Context;
27 use C4::Koha;
28 use C4::Output;
29 use C4::Items;
30 use C4::Serials;
31 use C4::Debug;
32 use C4::Search;    # enabled_staff_search_views
33
34 use Koha::ActionLogs;
35 use Koha::Database;
36 use Koha::DateUtils;
37 use Koha::Items;
38 use Koha::Patrons;
39
40 use vars qw($debug $cgi_debug);
41
42 =head1 viewlog.pl
43
44 plugin that shows stats
45
46 =cut
47
48 my $input = new CGI;
49
50 $debug or $debug = $cgi_debug;
51 my $do_it    = $input->param('do_it');
52 my @modules  = $input->multi_param("modules");
53 my $user     = $input->param("user") // '';
54 my @actions  = $input->multi_param("actions");
55 my @interfaces  = $input->multi_param("interfaces");
56 my $object   = $input->param("object");
57 my $info     = $input->param("info");
58 my $datefrom = $input->param("from");
59 my $dateto   = $input->param("to");
60 my $basename = $input->param("basename");
61 my $output   = $input->param("output") || "screen";
62 my $src      = $input->param("src") || ""; # this param allows us to be told where we were called from -fbcit
63
64 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
65     {
66         template_name   => "tools/viewlog.tt",
67         query           => $input,
68         type            => "intranet",
69         authnotrequired => 0,
70         flagsrequired   => { tools => 'view_system_logs' },
71         debug           => 1,
72     }
73 );
74
75 if ( $src eq 'circ' ) {
76
77     my $borrowernumber = $object;
78     my $patron = Koha::Patrons->find( $borrowernumber );
79     my $circ_info = 1;
80     unless ( $patron ) {
81          $circ_info = 0;
82     }
83
84     $template->param(
85         patron      => $patron,
86         circulation => $circ_info,
87     );
88 }
89
90 $template->param(
91     debug => $debug,
92     C4::Search::enabled_staff_search_views,
93     subscriptionsnumber => CountSubscriptionFromBiblionumber(scalar $input->param('object')),
94     object => $object,
95 );
96
97 if ($do_it) {
98     my $dtf = Koha::Database->new->schema->storage->datetime_parser;
99     my %search_params;
100
101     if ($datefrom and $dateto ) {
102         my $dateto_endday = dt_from_string($dateto);
103         $dateto_endday->set( # We set last second of day to see all log from that day
104             hour => 23,
105             minute => 59,
106             second => 59
107         );
108         $search_params{'timestamp'} = {
109             -between => [
110                 $dtf->format_datetime( dt_from_string($datefrom) ),
111                 $dtf->format_datetime( $dateto_endday ),
112             ]
113         };
114     } elsif ($datefrom) {
115         $search_params{'timestamp'} = {
116             '>=' => $dtf->format_datetime( dt_from_string($datefrom) )
117         };
118     } elsif ($dateto) {
119         my $dateto_endday = dt_from_string($dateto);
120         $dateto_endday->set( # We set last second of day to see all log from that day
121             hour => 23,
122             minute => 59,
123             second => 59
124         );
125         $search_params{'timestamp'} = {
126             '<=' => $dtf->format_datetime( $dateto_endday )
127         };
128     }
129     # Circulation uses RENEWAL, but Patrons uses RENEW, this helps to find both
130     if ( grep { $_ eq 'RENEW'} @actions ) { push @actions, 'RENEWAL' };
131
132     $search_params{user} = $user if $user;
133     $search_params{module} = { -in => [ @modules ] } if ( defined $modules[0] and $modules[0] ne '' ) ;
134     $search_params{action} = { -in => [ @actions ] } if ( defined $actions[0] && $actions[0] ne '' );
135     $search_params{object} = $object if $object;
136     $search_params{info} = $info if $info;
137     $search_params{interface} = { -in => [ @interfaces ] } if ( defined $interfaces[0] && $interfaces[0] ne '' );
138
139     my @logs = Koha::ActionLogs->search(\%search_params);
140
141     my @data;
142     foreach my $log (@logs) {
143         my $result = $log->unblessed;
144         # Init additional columns for CSV export
145         $result->{'biblionumber'}      = q{};
146         $result->{'biblioitemnumber'}  = q{};
147         $result->{'barcode'}           = q{};
148
149         if ( substr( $log->info, 0, 4 ) eq 'item' || $log->module eq "CIRCULATION" ) {
150
151             # get item information so we can create a working link
152             my $itemnumber = $log->object;
153             $itemnumber = $log->info if ( $log->module eq "CIRCULATION" );
154             my $item = Koha::Items->find($itemnumber);
155             if ($item) {
156                 $result->{'biblionumber'}     = $item->biblionumber;
157                 $result->{'biblioitemnumber'} = $item->biblionumber;
158                 $result->{'barcode'}          = $item->barcode;
159             }
160         }
161
162         #always add firstname and surname for librarian/user
163         if ( $log->user ) {
164             my $patron = Koha::Patrons->find( $log->user );
165             if ($patron) {
166                 $result->{librarian} = $patron;
167             }
168         }
169
170         #add firstname and surname for borrower, when using the CIRCULATION, MEMBERS, FINES
171         if ( $log->module eq "CIRCULATION" || $log->module eq "MEMBERS" || $log->module eq "FINES" ) {
172             if ( $log->object ) {
173                 my $patron = Koha::Patrons->find( $log->object );
174                 if ($patron) {
175                     $result->{patron} = $patron;
176                 }
177             }
178         }
179         push @data, $result;
180     }
181     if ( $output eq "screen" ) {
182
183         # Printing results to screen
184         $template->param(
185             logview  => 1,
186             total    => scalar @data,
187             looprow  => \@data,
188             do_it    => 1,
189             datefrom => $datefrom,
190             dateto   => $dateto,
191             user     => $user,
192             info     => $info,
193             src      => $src,
194             modules  => \@modules,
195             actions  => \@actions,
196             interfaces => \@interfaces
197         );
198
199         # Used modules
200         foreach my $module (@modules) {
201             $template->param( $module => 1 );
202         }
203         output_html_with_http_headers $input, $cookie, $template->output;
204     }
205     else {
206
207         # Printing to a csv file
208         my $content = q{};
209         my $delimiter = C4::Context->preference('delimiter') || ',';
210         if (@data) {
211             my $csv = Text::CSV::Encoded->new( { encoding_out => 'utf8', sep_char => $delimiter } );
212             $csv or die "Text::CSV::Encoded->new FAILED: " . Text::CSV::Encoded->error_diag();
213
214             # First line with heading
215             # Exporting bd id seems useless
216             my @headings = grep { $_ ne 'action_id' } sort keys %{$data[0]};
217             if ( $csv->combine(@headings) ) {
218                 $content .= $csv->string() . "\n";
219             }
220
221             # Lines of logs
222             foreach my $line (@data) {
223                 my @cells = map { $line->{$_} } @headings;
224                 if ( $csv->combine(@cells) ) {
225                     $content .= $csv->string() . "\n";
226                 }
227             }
228         }
229
230         # Output
231         print $input->header(
232             -type       => 'text/csv',
233             -attachment => $basename . '.csv',
234         );
235         print $content;
236     }
237     exit;
238 }
239 else {
240     output_html_with_http_headers $input, $cookie, $template->output;
241 }