Bug 10306: (QA follow-up) More feedback for admins in koha2marclinks
[koha.git] / tools / viewlog.pl
blob00469ba845216262afb13e042470e2971b841912
1 #!/usr/bin/perl
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>.
21 use Modern::Perl;
23 use C4::Auth;
24 use CGI qw ( -utf8 );
25 use Text::CSV::Encoded;
26 use C4::Context;
27 use C4::Koha;
28 use C4::Output;
29 use C4::Log;
30 use C4::Items;
31 use C4::Debug;
32 use C4::Search; # enabled_staff_search_views
33 use Koha::Patrons;
35 use vars qw($debug $cgi_debug);
37 =head1 viewlog.pl
39 plugin that shows stats
41 =cut
43 my $input = new CGI;
45 $debug or $debug = $cgi_debug;
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 $info = $input->param("info");
53 my $datefrom = $input->param("from");
54 my $dateto = $input->param("to");
55 my $basename = $input->param("basename");
56 my $output = $input->param("output") || "screen";
57 my $src = $input->param("src") || ""; # this param allows us to be told where we were called from -fbcit
59 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
61 template_name => "tools/viewlog.tt",
62 query => $input,
63 type => "intranet",
64 authnotrequired => 0,
65 flagsrequired => { tools => 'view_system_logs' },
66 debug => 1,
70 if ( $src eq 'circ' ) {
72 # if we were called from circulation, use the circulation menu and get data to populate it -fbcit
73 use C4::Members;
74 use C4::Members::Attributes qw(GetBorrowerAttributes);
75 my $borrowernumber = $object;
76 my $patron = Koha::Patrons->find( $borrowernumber );
77 unless ( $patron ) {
78 print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber");
79 exit;
81 $template->param( picture => 1 ) if $patron->image;
82 my $data = $patron->unblessed;
84 if ( C4::Context->preference('ExtendedPatronAttributes') ) {
85 my $attributes = GetBorrowerAttributes( $data->{'borrowernumber'} );
86 $template->param(
87 ExtendedPatronAttributes => 1,
88 extendedattributes => $attributes
92 $template->param(%$data);
94 $template->param(
95 menu => 1,
96 borrowernumber => $borrowernumber,
97 categoryname => $patron->category->description,
98 RoutingSerials => C4::Context->preference('RoutingSerials'),
102 $template->param(
103 debug => $debug,
104 C4::Search::enabled_staff_search_views,
105 object => $object,
108 if ($do_it) {
110 my @data;
111 my ( $results, $modules, $actions, $interfaces );
112 if ( defined $actions[0] && $actions[0] ne '' ) { $actions = \@actions; } # match All means no limit
113 if ( $modules[0] ne '' ) { $modules = \@modules; } # match All means no limit
114 if ( defined $interfaces[0] && $interfaces[0] ne '' ) { $interfaces = \@interfaces; } # match All means no limit
115 $results = GetLogs( $datefrom, $dateto, $user, $modules, $actions, $object, $info, $interfaces );
116 @data = @$results;
117 foreach my $result (@data) {
119 # Init additional columns for CSV export
120 $result->{'biblionumber'} = q{};
121 $result->{'biblioitemnumber'} = q{};
122 $result->{'barcode'} = q{};
123 $result->{'userfirstname'} = q{};
124 $result->{'usersurname'} = q{};
125 $result->{'borrowerfirstname'} = q{};
126 $result->{'borrowersurname'} = q{};
128 if ( substr( $result->{'info'}, 0, 4 ) eq 'item' || $result->{module} eq "CIRCULATION" ) {
130 # get item information so we can create a working link
131 my $itemnumber = $result->{'object'};
132 $itemnumber = $result->{'info'} if ( $result->{module} eq "CIRCULATION" );
133 my $item = GetItem($itemnumber);
134 if ($item) {
135 $result->{'biblionumber'} = $item->{'biblionumber'};
136 $result->{'biblioitemnumber'} = $item->{'biblionumber'};
137 $result->{'barcode'} = $item->{'barcode'};
141 #always add firstname and surname for librarian/user
142 if ( $result->{'user'} ) {
143 my $patron = Koha::Patrons->find( $result->{'user'} );
144 if ($patron) {
145 $result->{'userfirstname'} = $patron->firstname;
146 $result->{'usersurname'} = $patron->surname;
150 #add firstname and surname for borrower, when using the CIRCULATION, MEMBERS, FINES
151 if ( $result->{module} eq "CIRCULATION" || $result->{module} eq "MEMBERS" || $result->{module} eq "FINES" ) {
152 if ( $result->{'object'} ) {
153 my $patron = Koha::Patrons->find( $result->{'object'} );
154 if ($patron) {
155 $result->{'borrowerfirstname'} = $patron->firstname;
156 $result->{'borrowersurname'} = $patron->surname;
162 if ( $output eq "screen" ) {
164 # Printing results to screen
165 $template->param(
166 logview => 1,
167 total => scalar @data,
168 looprow => \@data,
169 do_it => 1,
170 datefrom => $datefrom,
171 dateto => $dateto,
172 user => $user,
173 info => $info,
174 src => $src,
175 modules => \@modules,
176 actions => \@actions,
177 interfaces => \@interfaces
180 # Used modules
181 foreach my $module (@modules) {
182 $template->param( $module => 1 );
185 output_html_with_http_headers $input, $cookie, $template->output;
187 else {
189 # Printing to a csv file
190 my $content = q{};
191 my $delimiter = C4::Context->preference('delimiter') || ',';
192 if (@data) {
193 my $csv = Text::CSV::Encoded->new( { encoding_out => 'utf8', sep_char => $delimiter } );
194 $csv or die "Text::CSV::Encoded->new FAILED: " . Text::CSV::Encoded->error_diag();
196 # First line with heading
197 # Exporting bd id seems useless
198 my @headings = grep { $_ ne 'action_id' } sort keys %{$data[0]};
199 if ( $csv->combine(@headings) ) {
200 $content .= $csv->string() . "\n";
203 # Lines of logs
204 foreach my $line (@data) {
205 my @cells = map { $line->{$_} } @headings;
206 if ( $csv->combine(@cells) ) {
207 $content .= $csv->string() . "\n";
212 # Output
213 print $input->header(
214 -type => 'text/csv',
215 -attachment => $basename . '.csv',
217 print $content;
219 exit;
221 else {
222 output_html_with_http_headers $input, $cookie, $template->output;