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