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;
31 use C4::Search; # enabled_staff_search_views
42 plugin that shows stats
48 my $do_it = $input->param('do_it');
49 my @modules = $input->multi_param("modules");
50 my $user = $input->param("user") // '';
51 my @actions = $input->multi_param("actions");
52 my @interfaces = $input->multi_param("interfaces");
53 my $object = $input->param("object");
54 my $object_type = $input->param("object_type") // '';
55 my $info = $input->param("info");
56 my $datefrom = $input->param("from");
57 my $dateto = $input->param("to");
58 my $basename = $input->param("basename");
59 my $output = $input->param("output") || "screen";
60 my $src = $input->param("src") || ""; # this param allows us to be told where we were called from -fbcit
62 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
64 template_name => "tools/viewlog.tt",
67 flagsrequired => { tools => 'view_system_logs' },
71 if ( $src eq 'circ' ) {
73 my $borrowernumber = $object;
74 my $patron = Koha::Patrons->find( $borrowernumber );
82 circulation => $circ_info,
87 C4::Search::enabled_staff_search_views,
88 subscriptionsnumber => ( $object ? CountSubscriptionFromBiblionumber($object) : 0 ),
93 my $dtf = Koha::Database->new->schema->storage->datetime_parser;
96 if ($datefrom and $dateto ) {
97 my $dateto_endday = dt_from_string($dateto);
98 $dateto_endday->set( # We set last second of day to see all log from that day
103 $search_params{'timestamp'} = {
105 $dtf->format_datetime( dt_from_string($datefrom) ),
106 $dtf->format_datetime( $dateto_endday ),
109 } elsif ($datefrom) {
110 $search_params{'timestamp'} = {
111 '>=' => $dtf->format_datetime( dt_from_string($datefrom) )
114 my $dateto_endday = dt_from_string($dateto);
115 $dateto_endday->set( # We set last second of day to see all log from that day
120 $search_params{'timestamp'} = {
121 '<=' => $dtf->format_datetime( $dateto_endday )
124 # Circulation uses RENEWAL, but Patrons uses RENEW, this helps to find both
125 if ( grep { $_ eq 'RENEW'} @actions ) { push @actions, 'RENEWAL' };
127 $search_params{user} = $user if $user;
128 $search_params{module} = { -in => [ @modules ] } if ( defined $modules[0] and $modules[0] ne '' ) ;
129 $search_params{action} = { -in => [ @actions ] } if ( defined $actions[0] && $actions[0] ne '' );
130 $search_params{interface} = { -in => [ @interfaces ] } if ( defined $interfaces[0] && $interfaces[0] ne '' );
132 if ( @modules == 1 && $object_type eq 'biblio' ) {
133 # Handle 'Modification log' from cataloguing
134 my @itemnumbers = Koha::Items->search({ biblionumber => $object })->get_column('itemnumber');
135 $search_params{'-or'} = [
136 { -and => { object => $object, info => { -like => 'biblio%' }}},
137 { -and => { object => \@itemnumbers, info => { -like => 'item%' }}},
140 $search_params{info} = { -like => '%' . $info . '%' } if $info;
141 $search_params{object} = $object if $object;
144 my @logs = Koha::ActionLogs->search(\%search_params);
147 foreach my $log (@logs) {
148 my $result = $log->unblessed;
149 # Init additional columns for CSV export
150 $result->{'biblionumber'} = q{};
151 $result->{'biblioitemnumber'} = q{};
152 $result->{'barcode'} = q{};
154 if ( substr( $log->info, 0, 4 ) eq 'item' || $log->module eq "CIRCULATION" ) {
156 # get item information so we can create a working link
157 my $itemnumber = $log->object;
158 $itemnumber = $log->info if ( $log->module eq "CIRCULATION" );
159 my $item = Koha::Items->find($itemnumber);
161 $result->{'biblionumber'} = $item->biblionumber;
162 $result->{'biblioitemnumber'} = $item->biblionumber;
163 $result->{'barcode'} = $item->barcode;
167 #always add firstname and surname for librarian/user
169 my $patron = Koha::Patrons->find( $log->user );
170 if ($patron && $output eq 'screen') {
171 $result->{librarian} = $patron;
175 #add firstname and surname for borrower, when using the CIRCULATION, MEMBERS, FINES
176 if ( $log->module eq "CIRCULATION" || $log->module eq "MEMBERS" || $log->module eq "FINES" ) {
177 if ( $log->object ) {
178 my $patron = Koha::Patrons->find( $log->object );
179 if ($patron && $output eq 'screen') {
180 $result->{patron} = $patron;
185 if ( $log->module eq 'NOTICES' ) {
186 if ( $log->object ) {
187 my $notice = Koha::Notice::Templates->find( { id => $log->object } );
188 if ($notice && $output eq 'screen') {
189 $result->{notice} = $notice->unblessed;
196 if ( $output eq "screen" ) {
198 # Printing results to screen
201 total => scalar @data,
204 datefrom => $datefrom,
209 modules => \@modules,
210 actions => \@actions,
211 interfaces => \@interfaces
215 foreach my $module (@modules) {
216 $template->param( $module => 1 );
218 output_html_with_http_headers $input, $cookie, $template->output;
222 # Printing to a csv file
224 my $delimiter = C4::Context->preference('CSVDelimiter') || ',';
226 my $csv = Text::CSV::Encoded->new( { encoding_out => 'utf8', sep_char => $delimiter } );
227 $csv or die "Text::CSV::Encoded->new FAILED: " . Text::CSV::Encoded->error_diag();
229 # First line with heading
230 # Exporting bd id seems useless
231 my @headings = grep { $_ ne 'action_id' } sort keys %{$data[0]};
232 if ( $csv->combine(@headings) ) {
233 $content .= $csv->string() . "\n";
237 foreach my $line (@data) {
238 my @cells = map { $line->{$_} } @headings;
239 if ( $csv->combine(@cells) ) {
240 $content .= $csv->string() . "\n";
246 print $input->header(
248 -attachment => $basename . '.csv',
255 output_html_with_http_headers $input, $cookie, $template->output;