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>.
25 use Text
::CSV
::Encoded
;
32 use C4
::Search
; # enabled_staff_search_views
40 use vars
qw($debug $cgi_debug);
44 plugin that shows stats
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 $object_type = $input->param("object_type") // '';
58 my $info = $input->param("info");
59 my $datefrom = $input->param("from");
60 my $dateto = $input->param("to");
61 my $basename = $input->param("basename");
62 my $output = $input->param("output") || "screen";
63 my $src = $input->param("src") || ""; # this param allows us to be told where we were called from -fbcit
65 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
67 template_name => "tools/viewlog.tt",
70 flagsrequired => { tools => 'view_system_logs' },
75 if ( $src eq 'circ' ) {
77 my $borrowernumber = $object;
78 my $patron = Koha::Patrons->find( $borrowernumber );
86 circulation => $circ_info,
92 C4::Search::enabled_staff_search_views,
93 subscriptionsnumber => ( $object ? CountSubscriptionFromBiblionumber($object) : 0 ),
98 my $dtf = Koha::Database->new->schema->storage->datetime_parser;
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
108 $search_params{'timestamp'} = {
110 $dtf->format_datetime( dt_from_string($datefrom) ),
111 $dtf->format_datetime( $dateto_endday ),
114 } elsif ($datefrom) {
115 $search_params{'timestamp'} = {
116 '>=' => $dtf->format_datetime( dt_from_string($datefrom) )
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
125 $search_params{'timestamp'} = {
126 '<=' => $dtf->format_datetime( $dateto_endday )
129 # Circulation uses RENEWAL, but Patrons uses RENEW, this helps to find both
130 if ( grep { $_ eq 'RENEW'} @actions ) { push @actions, 'RENEWAL' };
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{interface} = { -in => [ @interfaces ] } if ( defined $interfaces[0] && $interfaces[0] ne '' );
137 if ( @modules == 1 && $object_type eq 'biblio' ) {
138 # Handle 'Modification log' from cataloguing
139 my @itemnumbers = Koha::Items->search({ biblionumber => $object })->get_column('itemnumber');
140 $search_params{'-or'} = [
141 { -and => { object => $object, info => { -like => 'biblio%' }}},
142 { -and => { object => \@itemnumbers, info => { -like => 'item%' }}},
145 $search_params{info} = { -like => '%' . $info . '%' } if $info;
146 $search_params{object} = $object if $object;
149 my @logs = Koha::ActionLogs->search(\%search_params);
152 foreach my $log (@logs) {
153 my $result = $log->unblessed;
154 # Init additional columns for CSV export
155 $result->{'biblionumber'} = q{};
156 $result->{'biblioitemnumber'} = q{};
157 $result->{'barcode'} = q{};
159 if ( substr( $log->info, 0, 4 ) eq 'item' || $log->module eq "CIRCULATION" ) {
161 # get item information so we can create a working link
162 my $itemnumber = $log->object;
163 $itemnumber = $log->info if ( $log->module eq "CIRCULATION" );
164 my $item = Koha
::Items
->find($itemnumber);
166 $result->{'biblionumber'} = $item->biblionumber;
167 $result->{'biblioitemnumber'} = $item->biblionumber;
168 $result->{'barcode'} = $item->barcode;
172 #always add firstname and surname for librarian/user
174 my $patron = Koha
::Patrons
->find( $log->user );
175 if ($patron && $output eq 'screen') {
176 $result->{librarian
} = $patron;
180 #add firstname and surname for borrower, when using the CIRCULATION, MEMBERS, FINES
181 if ( $log->module eq "CIRCULATION" || $log->module eq "MEMBERS" || $log->module eq "FINES" ) {
182 if ( $log->object ) {
183 my $patron = Koha
::Patrons
->find( $log->object );
184 if ($patron && $output eq 'screen') {
185 $result->{patron
} = $patron;
191 if ( $output eq "screen" ) {
193 # Printing results to screen
196 total
=> scalar @data,
199 datefrom
=> $datefrom,
204 modules
=> \
@modules,
205 actions
=> \
@actions,
206 interfaces
=> \
@interfaces
210 foreach my $module (@modules) {
211 $template->param( $module => 1 );
213 output_html_with_http_headers
$input, $cookie, $template->output;
217 # Printing to a csv file
219 my $delimiter = C4
::Context
->preference('delimiter') || ',';
221 my $csv = Text
::CSV
::Encoded
->new( { encoding_out
=> 'utf8', sep_char
=> $delimiter } );
222 $csv or die "Text::CSV::Encoded->new FAILED: " . Text
::CSV
::Encoded
->error_diag();
224 # First line with heading
225 # Exporting bd id seems useless
226 my @headings = grep { $_ ne 'action_id' } sort keys %{$data[0]};
227 if ( $csv->combine(@headings) ) {
228 $content .= $csv->string() . "\n";
232 foreach my $line (@data) {
233 my @cells = map { $line->{$_} } @headings;
234 if ( $csv->combine(@cells) ) {
235 $content .= $csv->string() . "\n";
241 print $input->header(
243 -attachment
=> $basename . '.csv',
250 output_html_with_http_headers
$input, $cookie, $template->output;