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
;
33 use C4
::Search
; # enabled_staff_search_views
35 use vars
qw($debug $cgi_debug);
39 plugin that shows stats
45 $debug or $debug = $cgi_debug;
46 my $do_it = $input->param('do_it');
47 my @modules = $input->param("modules");
48 my $user = $input->param("user") // '';
49 my @actions = $input->param("actions");
50 my $object = $input->param("object");
51 my $info = $input->param("info");
52 my $datefrom = $input->param("from");
53 my $dateto = $input->param("to");
54 my $basename = $input->param("basename");
55 my $output = $input->param("output") || "screen";
56 my $src = $input->param("src") || ""; # this param allows us to be told where we were called from -fbcit
58 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
60 template_name => "tools/viewlog.tt",
64 flagsrequired => { tools => 'view_system_logs' },
69 if ( $src eq 'circ' ) {
71 # if we were called from circulation, use the circulation menu and get data to populate it -fbcit
73 use C4::Members::Attributes qw(GetBorrowerAttributes);
74 my $borrowernumber = $object;
75 my $data = GetMember
( 'borrowernumber' => $borrowernumber );
76 my ( $picture, $dberror ) = GetPatronImage
( $data->{'borrowernumber'} );
77 $template->param( picture
=> 1 ) if $picture;
79 if ( C4
::Context
->preference('ExtendedPatronAttributes') ) {
80 my $attributes = GetBorrowerAttributes
( $data->{'borrowernumber'} );
82 ExtendedPatronAttributes
=> 1,
83 extendedattributes
=> $attributes
87 my $roadtype = C4
::Koha
::GetAuthorisedValueByCode
( 'ROADTYPE', $data->{streettype
} );
88 $template->param(%$data);
92 borrowernumber
=> $borrowernumber,
93 categoryname
=> $data->{'description'},
94 roadtype
=> $roadtype,
95 branchname
=> GetBranchName
( $data->{'branchcode'} ),
96 RoutingSerials
=> C4
::Context
->preference('RoutingSerials'),
102 C4
::Search
::enabled_staff_search_views
,
109 my ( $results, $modules, $actions );
110 if ( defined $actions[0] && $actions[0] ne '' ) { $actions = \
@actions; } # match All means no limit
111 if ( $modules[0] ne '' ) { $modules = \
@modules; } # match All means no limit
112 $results = GetLogs
( $datefrom, $dateto, $user, $modules, $actions, $object, $info );
114 foreach my $result (@data) {
116 # Init additional columns for CSV export
117 $result->{'biblionumber'} = q{};
118 $result->{'biblioitemnumber'} = q{};
119 $result->{'barcode'} = q{};
120 $result->{'userfirstname'} = q{};
121 $result->{'usersurname'} = q{};
122 $result->{'borrowerfirstname'} = q{};
123 $result->{'borrowersurname'} = q{};
125 if ( substr( $result->{'info'}, 0, 4 ) eq 'item' || $result->{module
} eq "CIRCULATION" ) {
127 # get item information so we can create a working link
128 my $itemnumber = $result->{'object'};
129 $itemnumber = $result->{'info'} if ( $result->{module
} eq "CIRCULATION" );
130 my $item = GetItem
($itemnumber);
132 $result->{'biblionumber'} = $item->{'biblionumber'};
133 $result->{'biblioitemnumber'} = $item->{'biblionumber'};
134 $result->{'barcode'} = $item->{'barcode'};
138 #always add firstname and surname for librarian/user
139 if ( $result->{'user'} ) {
140 my $userdetails = C4
::Members
::GetMemberDetails
( $result->{'user'} );
142 $result->{'userfirstname'} = $userdetails->{'firstname'};
143 $result->{'usersurname'} = $userdetails->{'surname'};
147 #add firstname and surname for borrower, when using the CIRCULATION, MEMBERS, FINES
148 if ( $result->{module
} eq "CIRCULATION" || $result->{module
} eq "MEMBERS" || $result->{module
} eq "FINES" ) {
149 if ( $result->{'object'} ) {
150 my $borrowerdetails = C4
::Members
::GetMemberDetails
( $result->{'object'} );
151 if ($borrowerdetails) {
152 $result->{'borrowerfirstname'} = $borrowerdetails->{'firstname'};
153 $result->{'borrowersurname'} = $borrowerdetails->{'surname'};
159 if ( $output eq "screen" ) {
161 # Printing results to screen
164 total
=> scalar @data,
167 datefrom
=> $datefrom,
172 modules
=> \
@modules,
173 actions
=> \
@actions,
177 foreach my $module (@modules) {
178 $template->param( $module => 1 );
181 output_html_with_http_headers
$input, $cookie, $template->output;
185 # Printing to a csv file
187 my $delimiter = C4
::Context
->preference('delimiter') || ',';
189 my $csv = Text
::CSV
::Encoded
->new( { encoding_out
=> 'utf8', sep_char
=> $delimiter } );
190 $csv or die "Text::CSV::Encoded->new FAILED: " . Text
::CSV
::Encoded
->error_diag();
192 # First line with heading
193 # Exporting bd id seems useless
194 my @headings = grep { $_ ne 'action_id' } sort keys %{$data[0]};
195 if ( $csv->combine(@headings) ) {
196 $content .= $csv->string() . "\n";
200 foreach my $line (@data) {
201 my @cells = map { $line->{$_} } @headings;
202 if ( $csv->combine(@cells) ) {
203 $content .= $csv->string() . "\n";
209 print $input->header(
211 -attachment
=> $basename . '.csv',
218 output_html_with_http_headers
$input, $cookie, $template->output;