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>.
23 use C4::Auth qw( get_template_and_user );
25 use Text::CSV::Encoded;
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 );
33 use Koha::DateUtils qw( dt_from_string );
40 plugin that shows stats
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
60 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
62 template_name => "tools/viewlog.tt",
65 flagsrequired => { tools => 'view_system_logs' },
69 if ( $src eq 'circ' ) {
71 my $borrowernumber = $object;
72 my $patron = Koha::Patrons->find( $borrowernumber );
80 circulation => $circ_info,
85 C4::Search::enabled_staff_search_views,
86 subscriptionsnumber => ( $object ? CountSubscriptionFromBiblionumber($object) : 0 ),
91 my $dtf = Koha::Database->new->schema->storage->datetime_parser;
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
101 $search_params{'timestamp'} = {
103 $dtf->format_datetime( dt_from_string($datefrom) ),
104 $dtf->format_datetime( $dateto_endday ),
107 } elsif ($datefrom) {
108 $search_params{'timestamp'} = {
109 '>=' => $dtf->format_datetime( dt_from_string($datefrom) )
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
118 $search_params{'timestamp'} = {
119 '<=' => $dtf->format_datetime( $dateto_endday )
122 # Circulation uses RENEWAL, but Patrons uses RENEW, this helps to find both
123 if ( grep { $_ eq 'RENEW'} @actions ) { push @actions, 'RENEWAL' };
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 '' );
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%' }}},
138 $search_params{info} = { -like => '%' . $info . '%' } if $info;
139 $search_params{object} = $object if $object;
142 my @logs = Koha::ActionLogs->search(\%search_params);
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{};
152 if ( substr( $log->info, 0, 4 ) eq 'item' || $log->module eq "CIRCULATION" ) {
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);
159 $result->{'biblionumber'} = $item->biblionumber;
160 $result->{'biblioitemnumber'} = $item->biblionumber;
161 $result->{'barcode'} = $item->barcode;
165 #always add firstname and surname for librarian/user
167 my $patron = Koha::Patrons->find( $log->user );
168 if ($patron && $output eq 'screen') {
169 $result->{librarian} = $patron;
173 #add firstname and surname for borrower, when using the CIRCULATION, MEMBERS, FINES
174 if ( $log->module eq "CIRCULATION" || $log->module eq "MEMBERS" || $log->module eq "FINES" ) {
175 if ( $log->object ) {
176 my $patron = Koha::Patrons->find( $log->object );
177 if ($patron && $output eq 'screen') {
178 $result->{patron} = $patron;
183 if ( $log->module eq 'NOTICES' ) {
184 if ( $log->object ) {
185 my $notice = Koha::Notice::Templates->find( { id => $log->object } );
186 if ($notice && $output eq 'screen') {
187 $result->{notice} = $notice->unblessed;
194 if ( $output eq "screen" ) {
196 # Printing results to screen
199 total => scalar @data,
202 datefrom => $datefrom,
207 modules => \@modules,
208 actions => \@actions,
209 interfaces => \@interfaces
213 foreach my $module (@modules) {
214 $template->param( $module => 1 );
216 output_html_with_http_headers $input, $cookie, $template->output;
220 # Printing to a csv file
222 my $delimiter = C4::Context->preference('CSVDelimiter') || ',';
224 my $csv = Text::CSV::Encoded->new( { encoding_out => 'utf8', sep_char => $delimiter } );
225 $csv or die "Text::CSV::Encoded->new FAILED: " . Text::CSV::Encoded->error_diag();
227 # First line with heading
228 # Exporting bd id seems useless
229 my @headings = grep { $_ ne 'action_id' } sort keys %{$data[0]};
230 if ( $csv->combine(@headings) ) {
231 $content .= $csv->string() . "\n";
235 foreach my $line (@data) {
236 my @cells = map { $line->{$_} } @headings;
237 if ( $csv->combine(@cells) ) {
238 $content .= $csv->string() . "\n";
244 print $input->header(
246 -attachment => $basename . '.csv',
253 output_html_with_http_headers $input, $cookie, $template->output;