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