Bug 12598: Add use Koha::Patron::debarments statement
[koha.git] / tools / viewlog.pl
1 #!/usr/bin/perl
2
3 # Copyright 2010 BibLibre
4 # Copyright 2011 MJ Ray and software.coop
5 #
6 # This file is part of Koha.
7 #
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.
12 #
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.
17 #
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>.
20
21 use Modern::Perl;
22
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;
34
35 use vars qw($debug $cgi_debug);
36
37 =head1 viewlog.pl
38
39 plugin that shows stats
40
41 =cut
42
43 my $input = new CGI;
44
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
58
59 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
60     {
61         template_name   => "tools/viewlog.tt",
62         query           => $input,
63         type            => "intranet",
64         authnotrequired => 0,
65         flagsrequired   => { tools => 'view_system_logs' },
66         debug           => 1,
67     }
68 );
69
70 if ( $src eq 'circ' ) {
71
72     # 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 $patron = Koha::Patrons->find( $borrowernumber );
76     unless ( $patron ) {
77         print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber");
78         exit;
79     }
80     if ( C4::Context->preference('ExtendedPatronAttributes') ) {
81         my $attributes = GetBorrowerAttributes( $borrowernumber );
82         $template->param(
83             ExtendedPatronAttributes => 1,
84             extendedattributes       => $attributes
85         );
86     }
87
88     $template->param(
89         patron      => $patron,
90         circulation => 1,
91     );
92 }
93
94 $template->param(
95     debug => $debug,
96     C4::Search::enabled_staff_search_views,
97     object => $object,
98 );
99
100 if ($do_it) {
101
102     my @data;
103     my ( $results, $modules, $actions, $interfaces );
104     if ( defined $actions[0] && $actions[0] ne '' ) { $actions  = \@actions; }     # match All means no limit
105     if ( $modules[0] ne '' ) { $modules = \@modules; }    # match All means no limit
106     if ( defined $interfaces[0] && $interfaces[0] ne '' ) { $interfaces = \@interfaces; }    # match All means no limit
107     $results = GetLogs( $datefrom, $dateto, $user, $modules, $actions, $object, $info, $interfaces );
108     @data = @$results;
109     foreach my $result (@data) {
110
111         # Init additional columns for CSV export
112         $result->{'biblionumber'}      = q{};
113         $result->{'biblioitemnumber'}  = q{};
114         $result->{'barcode'}           = q{};
115
116         if ( substr( $result->{'info'}, 0, 4 ) eq 'item' || $result->{module} eq "CIRCULATION" ) {
117
118             # get item information so we can create a working link
119             my $itemnumber = $result->{'object'};
120             $itemnumber = $result->{'info'} if ( $result->{module} eq "CIRCULATION" );
121             my $item = GetItem($itemnumber);
122             if ($item) {
123                 $result->{'biblionumber'}     = $item->{'biblionumber'};
124                 $result->{'biblioitemnumber'} = $item->{'biblionumber'};
125                 $result->{'barcode'}          = $item->{'barcode'};
126             }
127         }
128
129         #always add firstname and surname for librarian/user
130         if ( $result->{'user'} ) {
131             my $patron = Koha::Patrons->find( $result->{'user'} );
132             if ($patron) {
133                 $result->{librarian} = $patron;
134             }
135         }
136
137         #add firstname and surname for borrower, when using the CIRCULATION, MEMBERS, FINES
138         if ( $result->{module} eq "CIRCULATION" || $result->{module} eq "MEMBERS" || $result->{module} eq "FINES" ) {
139             if ( $result->{'object'} ) {
140                 my $patron = Koha::Patrons->find( $result->{'object'} );
141                 if ($patron) {
142                     $result->{patron} = $patron;
143                 }
144             }
145         }
146     }
147
148     if ( $output eq "screen" ) {
149
150         # Printing results to screen
151         $template->param(
152             logview  => 1,
153             total    => scalar @data,
154             looprow  => \@data,
155             do_it    => 1,
156             datefrom => $datefrom,
157             dateto   => $dateto,
158             user     => $user,
159             info     => $info,
160             src      => $src,
161             modules  => \@modules,
162             actions  => \@actions,
163             interfaces => \@interfaces
164         );
165
166         # Used modules
167         foreach my $module (@modules) {
168             $template->param( $module => 1 );
169         }
170
171         output_html_with_http_headers $input, $cookie, $template->output;
172     }
173     else {
174
175         # Printing to a csv file
176         my $content = q{};
177         my $delimiter = C4::Context->preference('delimiter') || ',';
178         if (@data) {
179             my $csv = Text::CSV::Encoded->new( { encoding_out => 'utf8', sep_char => $delimiter } );
180             $csv or die "Text::CSV::Encoded->new FAILED: " . Text::CSV::Encoded->error_diag();
181
182             # First line with heading
183             # Exporting bd id seems useless
184             my @headings = grep { $_ ne 'action_id' } sort keys %{$data[0]};
185             if ( $csv->combine(@headings) ) {
186                 $content .= $csv->string() . "\n";
187             }
188
189             # Lines of logs
190             foreach my $line (@data) {
191                 my @cells = map { $line->{$_} } @headings;
192                 if ( $csv->combine(@cells) ) {
193                     $content .= $csv->string() . "\n";
194                 }
195             }
196         }
197
198         # Output
199         print $input->header(
200             -type       => 'text/csv',
201             -attachment => $basename . '.csv',
202         );
203         print $content;
204     }
205     exit;
206 }
207 else {
208     output_html_with_http_headers $input, $cookie, $template->output;
209 }