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 );
41 plugin that shows stats
47 my $do_it = $input->param('do_it');
48 my @modules = $input->multi_param("modules");
49 my $user = $input->param("user") // '';
50 my @actions = $input->multi_param("actions");
51 my @interfaces = $input->multi_param("interfaces");
52 my $object = $input->param("object");
53 my $object_type = $input->param("object_type") // '';
54 my $info = $input->param("info");
55 my $datefrom = $input->param("from");
56 my $dateto = $input->param("to");
57 my $basename = $input->param("basename");
58 my $output = $input->param("output") || "screen";
59 my $src = $input->param("src") || ""; # this param allows us to be told where we were called from -fbcit
61 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
63 template_name => "tools/viewlog.tt",
66 flagsrequired => { tools => 'view_system_logs' },
70 if ( $src eq 'circ' ) {
72 my $borrowernumber = $object;
73 my $patron = Koha::Patrons->find( $borrowernumber );
81 circulation => $circ_info,
86 C4::Search::enabled_staff_search_views,
87 subscriptionsnumber => ( $object ? CountSubscriptionFromBiblionumber($object) : 0 ),
92 my $dtf = Koha::Database->new->schema->storage->datetime_parser;
95 if ($datefrom and $dateto ) {
96 my $dateto_endday = dt_from_string($dateto);
97 $dateto_endday->set( # We set last second of day to see all log from that day
102 $search_params{'timestamp'} = {
104 $dtf->format_datetime( dt_from_string($datefrom) ),
105 $dtf->format_datetime( $dateto_endday ),
108 } elsif ($datefrom) {
109 $search_params{'timestamp'} = {
110 '>=' => $dtf->format_datetime( dt_from_string($datefrom) )
113 my $dateto_endday = dt_from_string($dateto);
114 $dateto_endday->set( # We set last second of day to see all log from that day
119 $search_params{'timestamp'} = {
120 '<=' => $dtf->format_datetime( $dateto_endday )
123 # Circulation uses RENEWAL, but Patrons uses RENEW, this helps to find both
124 if ( grep { $_ eq 'RENEW'} @actions ) { push @actions, 'RENEWAL' };
126 $search_params{user} = $user if $user;
127 $search_params{module} = { -in => [ @modules ] } if ( defined $modules[0] and $modules[0] ne '' ) ;
128 $search_params{action} = { -in => [ @actions ] } if ( defined $actions[0] && $actions[0] ne '' );
129 $search_params{interface} = { -in => [ @interfaces ] } if ( defined $interfaces[0] && $interfaces[0] ne '' );
131 if ( @modules == 1 && $object_type eq 'biblio' ) {
132 # Handle 'Modification log' from cataloguing
133 my @itemnumbers = Koha::Items->search({ biblionumber => $object })->get_column('itemnumber');
134 $search_params{'-or'} = [
135 { -and => { object => $object, info => { -like => 'biblio%' }}},
136 { -and => { object => \@itemnumbers, info => { -like => 'item%' }}},
139 $search_params{info} = { -like => '%' . $info . '%' } if $info;
140 $search_params{object} = $object if $object;
143 my @logs = Koha::ActionLogs->search(\%search_params)->as_list;
146 foreach my $log (@logs) {
147 my $result = $log->unblessed;
148 # Init additional columns for CSV export
149 $result->{'biblionumber'} = q{};
150 $result->{'biblioitemnumber'} = q{};
151 $result->{'barcode'} = q{};
153 if ( substr( $log->info, 0, 4 ) eq 'item' || $log->module eq "CIRCULATION" ) {
155 # get item information so we can create a working link
156 my $itemnumber = $log->object;
157 $itemnumber = $log->info if ( $log->module eq "CIRCULATION" );
158 my $item = Koha::Items->find($itemnumber);
160 $result->{'biblionumber'} = $item->biblionumber;
161 $result->{'biblioitemnumber'} = $item->biblionumber;
162 $result->{'barcode'} = $item->barcode;
166 #always add firstname and surname for librarian/user
168 my $patron = Koha::Patrons->find( $log->user );
169 if ($patron && $output eq 'screen') {
170 $result->{librarian} = $patron;
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;
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;
193 # get recall information
194 if ( $log->module eq "RECALLS" ) {
195 if ( $log->object ) {
196 my $recall = Koha::Recalls->find( $log->object );
197 if ( $recall && $output eq 'screen' ) {
198 $result->{recall} = $recall;
204 if ( $output eq "screen" ) {
206 # Printing results to screen
209 total => scalar @data,
212 datefrom => $datefrom,
217 modules => \@modules,
218 actions => \@actions,
219 interfaces => \@interfaces
223 foreach my $module (@modules) {
224 $template->param( $module => 1 );
226 output_html_with_http_headers $input, $cookie, $template->output;
230 # Printing to a csv file
232 my $delimiter = C4::Context->preference('CSVDelimiter') || ',';
234 my $csv = Text::CSV::Encoded->new( { encoding_out => 'utf8', sep_char => $delimiter } );
235 $csv or die "Text::CSV::Encoded->new FAILED: " . Text::CSV::Encoded->error_diag();
237 # First line with heading
238 # Exporting bd id seems useless
239 my @headings = grep { $_ ne 'action_id' } sort keys %{$data[0]};
240 if ( $csv->combine(@headings) ) {
241 $content .= $csv->string() . "\n";
245 foreach my $line (@data) {
246 my @cells = map { $line->{$_} } @headings;
247 if ( $csv->combine(@cells) ) {
248 $content .= $csv->string() . "\n";
254 print $input->header(
256 -attachment => $basename . '.csv',
263 output_html_with_http_headers $input, $cookie, $template->output;