c7f427356507d5c9e15ec56beaa662644dcbedfb
[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->{'biblionumber'}     = $item->biblionumber;
161                 $result->{'biblioitemnumber'} = $item->biblionumber;
162                 $result->{'barcode'}          = $item->barcode;
163             }
164         }
165
166         #always add firstname and surname for librarian/user
167         if ( $log->user ) {
168             my $patron = Koha::Patrons->find( $log->user );
169             if ($patron && $output eq 'screen') {
170                 $result->{librarian} = $patron;
171             }
172         }
173
174         #add firstname and surname for borrower, when using the CIRCULATION, MEMBERS, FINES
175         if ( $log->module eq "CIRCULATION" || $log->module eq "MEMBERS" || $log->module eq "FINES" ) {
176             if ( $log->object ) {
177                 my $patron = Koha::Patrons->find( $log->object );
178                 if ($patron && $output eq 'screen') {
179                     $result->{patron} = $patron;
180                 }
181             }
182         }
183
184         if ( $log->module eq 'NOTICES' ) {
185             if ( $log->object ) {
186                 my $notice = Koha::Notice::Templates->find( { id => $log->object } );
187                 if ($notice && $output eq 'screen') {
188                     $result->{notice} = $notice->unblessed;
189                 }
190             }
191         }
192
193         # get recall information
194         if ( $log->module eq "RECALLS" ) {
195             if ( $log->object ) {
196                 my $recall = Koha::Recalls->find( $log->object );
197                 if ( $recall && $output eq 'screen' ) {
198                     $result->{recall} = $recall;
199                 }
200             }
201         }
202         push @data, $result;
203     }
204     if ( $output eq "screen" ) {
205
206         # Printing results to screen
207         $template->param(
208             logview  => 1,
209             total    => scalar @data,
210             looprow  => \@data,
211             do_it    => 1,
212             datefrom => $datefrom,
213             dateto   => $dateto,
214             user     => $user,
215             info     => $info,
216             src      => $src,
217             modules  => \@modules,
218             actions  => \@actions,
219             interfaces => \@interfaces
220         );
221
222         # Used modules
223         foreach my $module (@modules) {
224             $template->param( $module => 1 );
225         }
226         output_html_with_http_headers $input, $cookie, $template->output;
227     }
228     else {
229
230         # Printing to a csv file
231         my $content = q{};
232         my $delimiter = C4::Context->preference('CSVDelimiter') || ',';
233         if (@data) {
234             my $csv = Text::CSV::Encoded->new( { encoding_out => 'utf8', sep_char => $delimiter } );
235             $csv or die "Text::CSV::Encoded->new FAILED: " . Text::CSV::Encoded->error_diag();
236
237             # First line with heading
238             # Exporting bd id seems useless
239             my @headings = grep { $_ ne 'action_id' } sort keys %{$data[0]};
240             if ( $csv->combine(@headings) ) {
241                 $content .= $csv->string() . "\n";
242             }
243
244             # Lines of logs
245             foreach my $line (@data) {
246                 my @cells = map { $line->{$_} } @headings;
247                 if ( $csv->combine(@cells) ) {
248                     $content .= $csv->string() . "\n";
249                 }
250             }
251         }
252
253         # Output
254         print $input->header(
255             -type       => 'text/csv',
256             -attachment => $basename . '.csv',
257         );
258         print $content;
259     }
260     exit;
261 }
262 else {
263     output_html_with_http_headers $input, $cookie, $template->output;
264 }