Bug 21560: (follow-up) move use at the top
[koha.git] / tools / viewlog.pl
blob381b0f816b17a4597b4e14687ece89f53fbcdd33
1 #!/usr/bin/perl
3 # Copyright 2010 BibLibre
4 # Copyright 2011 MJ Ray and software.coop
6 # This file is part of Koha.
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.
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.
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>.
21 use Modern::Perl;
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
34 use Koha::ActionLogs;
35 use Koha::Database;
36 use Koha::DateUtils;
37 use Koha::Items;
38 use Koha::Patrons;
40 use vars qw($debug $cgi_debug);
42 =head1 viewlog.pl
44 plugin that shows stats
46 =cut
48 my $input = new CGI;
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
64 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
66 template_name => "tools/viewlog.tt",
67 query => $input,
68 type => "intranet",
69 authnotrequired => 0,
70 flagsrequired => { tools => 'view_system_logs' },
71 debug => 1,
75 if ( $src eq 'circ' ) {
77 # if we were called from circulation, use the circulation menu and get data to populate it -fbcit
78 use C4::Members::Attributes qw(GetBorrowerAttributes);
79 my $borrowernumber = $object;
80 my $patron = Koha::Patrons->find( $borrowernumber );
81 my $circ_info = 1;
82 unless ( $patron ) {
83 $circ_info = 0;
85 if ( C4::Context->preference('ExtendedPatronAttributes') ) {
86 my $attributes = GetBorrowerAttributes( $borrowernumber );
87 $template->param(
88 ExtendedPatronAttributes => 1,
89 extendedattributes => $attributes
93 $template->param(
94 patron => $patron,
95 circulation => $circ_info,
99 $template->param(
100 debug => $debug,
101 C4::Search::enabled_staff_search_views,
102 subscriptionsnumber => CountSubscriptionFromBiblionumber($input->param('object')),
103 object => $object,
106 if ($do_it) {
107 my $dtf = Koha::Database->new->schema->storage->datetime_parser;
108 my %search_params;
110 if ($datefrom and $dateto ) {
111 my $dateto_endday = dt_from_string($dateto);
112 $dateto_endday->set( # We set last second of day to see all log from that day
113 hour => 23,
114 minute => 59,
115 second => 59
117 $search_params{'timestamp'} = {
118 -between => [
119 $dtf->format_datetime( dt_from_string($datefrom) ),
120 $dtf->format_datetime( $dateto_endday ),
123 } elsif ($datefrom) {
124 $search_params{'timestamp'} = {
125 '>=' => $dtf->format_datetime( dt_from_string($datefrom) )
127 } elsif ($dateto) {
128 my $dateto_endday = dt_from_string($dateto);
129 $dateto_endday->set( # We set last second of day to see all log from that day
130 hour => 23,
131 minute => 59,
132 second => 59
134 $search_params{'timestamp'} = {
135 '<=' => $dtf->format_datetime( $dateto_endday )
138 $search_params{user} = $user if $user;
139 $search_params{module} = { -in => [ @modules ] } if ( defined $modules[0] and $modules[0] ne '' ) ;
140 $search_params{action} = { -in => [ @actions ] } if ( defined $actions[0] && $actions[0] ne '' );
141 $search_params{object} = $object if $object;
142 $search_params{info} = $info if $info;
143 $search_params{interface} = { -in => [ @interfaces ] } if ( defined $interfaces[0] && $interfaces[0] ne '' );
145 my @logs = Koha::ActionLogs->search(\%search_params);
147 my @data;
148 foreach my $log (@logs) {
149 my $result = $log->unblessed;
150 # Init additional columns for CSV export
151 $result->{'biblionumber'} = q{};
152 $result->{'biblioitemnumber'} = q{};
153 $result->{'barcode'} = q{};
155 if ( substr( $log->info, 0, 4 ) eq 'item' || $log->module eq "CIRCULATION" ) {
157 # get item information so we can create a working link
158 my $itemnumber = $log->object;
159 $itemnumber = $log->info if ( $log->module eq "CIRCULATION" );
160 my $item = Koha::Items->find($itemnumber);
161 if ($item) {
162 $result->{'biblionumber'} = $item->biblionumber;
163 $result->{'biblioitemnumber'} = $item->biblionumber;
164 $result->{'barcode'} = $item->barcode;
168 #always add firstname and surname for librarian/user
169 if ( $log->user ) {
170 my $patron = Koha::Patrons->find( $log->user );
171 if ($patron) {
172 $result->{librarian} = $patron;
176 #add firstname and surname for borrower, when using the CIRCULATION, MEMBERS, FINES
177 if ( $log->module eq "CIRCULATION" || $log->module eq "MEMBERS" || $log->module eq "FINES" ) {
178 if ( $log->object ) {
179 my $patron = Koha::Patrons->find( $log->object );
180 if ($patron) {
181 $result->{patron} = $patron;
185 push @data, $result;
187 if ( $output eq "screen" ) {
189 # Printing results to screen
190 $template->param(
191 logview => 1,
192 total => scalar @data,
193 looprow => \@data,
194 do_it => 1,
195 datefrom => $datefrom,
196 dateto => $dateto,
197 user => $user,
198 info => $info,
199 src => $src,
200 modules => \@modules,
201 actions => \@actions,
202 interfaces => \@interfaces
205 # Used modules
206 foreach my $module (@modules) {
207 $template->param( $module => 1 );
209 output_html_with_http_headers $input, $cookie, $template->output;
211 else {
213 # Printing to a csv file
214 my $content = q{};
215 my $delimiter = C4::Context->preference('delimiter') || ',';
216 if (@data) {
217 my $csv = Text::CSV::Encoded->new( { encoding_out => 'utf8', sep_char => $delimiter } );
218 $csv or die "Text::CSV::Encoded->new FAILED: " . Text::CSV::Encoded->error_diag();
220 # First line with heading
221 # Exporting bd id seems useless
222 my @headings = grep { $_ ne 'action_id' } sort keys %{$data[0]};
223 if ( $csv->combine(@headings) ) {
224 $content .= $csv->string() . "\n";
227 # Lines of logs
228 foreach my $line (@data) {
229 my @cells = map { $line->{$_} } @headings;
230 if ( $csv->combine(@cells) ) {
231 $content .= $csv->string() . "\n";
236 # Output
237 print $input->header(
238 -type => 'text/csv',
239 -attachment => $basename . '.csv',
241 print $content;
243 exit;
245 else {
246 output_html_with_http_headers $input, $cookie, $template->output;