Bug 6362: fixes display to re-embed items in OPAC MARC view
[koha.git] / C4 / Log.pm
1 package C4::Log;
2
3 #package to deal with Logging Actions in DB
4
5
6 # Copyright 2000-2002 Katipo Communications
7 #
8 # This file is part of Koha.
9 #
10 # Koha is free software; you can redistribute it and/or modify it under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 2 of the License, or (at your option) any later
13 # version.
14 #
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License along
20 # with Koha; if not, write to the Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22
23 use strict;
24 use warnings;
25
26 use C4::Context;
27 use C4::Dates qw(format_date);
28
29 use vars qw($VERSION @ISA @EXPORT);
30
31 BEGIN {
32         # set the version for version checking
33         $VERSION = 3.01;
34         require Exporter;
35         @ISA = qw(Exporter);
36         @EXPORT = qw(&logaction &GetLogStatus &displaylog &GetLogs);
37 }
38
39 =head1 NAME
40
41 C4::Log - Koha Log Facility functions
42
43 =head1 SYNOPSIS
44
45   use C4::Log;
46
47 =head1 DESCRIPTION
48
49 The functions in this module perform various functions in order to log all the operations done on the Database, including deleting and undeleting books, adding/editing members, etc.
50
51 =head1 FUNCTIONS
52
53 =over 2
54
55 =item logaction
56
57   &logaction($modulename, $actionname, $objectnumber, $infos);
58
59 Adds a record into action_logs table to report the different changes upon the database.
60 Each log entry includes the number of the user currently logged in.  For batch
61 jobs, which operate without authenticating a user and setting up a session, the user
62 number is set to 0, which is the same as the superlibrarian's number.
63
64 =cut
65
66 #'
67 sub logaction {
68     my ($modulename, $actionname, $objectnumber, $infos)=@_;
69
70     # Get ID of logged in user.  if called from a batch job,
71     # no user session exists and C4::Context->userenv() returns
72     # the scalar '0'.
73     my $userenv = C4::Context->userenv();
74     my $usernumber = (ref($userenv) eq 'HASH') ? $userenv->{'number'} : 0;
75
76     my $dbh = C4::Context->dbh;
77     my $sth=$dbh->prepare("Insert into action_logs (timestamp,user,module,action,object,info) values (now(),?,?,?,?,?)");
78     $sth->execute($usernumber,$modulename,$actionname,$objectnumber,$infos);
79     $sth->finish;
80 }
81
82 =item GetLogStatus
83
84   $status = GetLogStatus;
85
86 C<$status> is a hasref like this example:
87     $hash = {
88         BorrowersLog   => 1,
89         CataloguingLog => 0,
90         IssueLog       => 0,
91         ...
92     }
93
94 =cut
95
96 #'
97 sub GetLogStatus {
98     my %hash;
99     $hash{BorrowersLog}    = C4::Context->preference("BorrowersLog");
100     $hash{CataloguingLog}  = C4::Context->preference("CataloguingLog");
101     $hash{IssueLog}        = C4::Context->preference("IssueLog");
102     $hash{ReturnLog}       = C4::Context->preference("ReturnLog");
103     $hash{SubscriptionLog} = C4::Context->preference("SubscriptionLog");
104     $hash{LetterLog}       = C4::Context->preference("LetterLog");
105     $hash{FinesLog}        = C4::Context->preference("FinesLog");
106     return \%hash;
107 }
108
109 =item displaylog
110
111   &displaylog($modulename, @filters);
112   $modulename is the name of the module on which the user wants to display logs
113   @filters is an optional table of hash containing :
114       - name : the name of the variable to filter
115     - value : the value of the filter.... May be with * joker
116
117 returns a table of hash containing who did what on which object at what time
118
119 =cut
120
121 #'
122 sub displaylog {
123   my ($modulename, @filters) = @_;
124     my $dbh = C4::Context->dbh;
125     my $strsth=qq|
126                 SELECT action_logs.timestamp, action_logs.action, action_logs.info,
127                                 borrowers.cardnumber, borrowers.surname, borrowers.firstname, borrowers.userid,
128                         biblio.biblionumber, biblio.title, biblio.author
129         FROM action_logs 
130                 LEFT JOIN borrowers ON borrowers.borrowernumber=action_logs.user 
131         LEFT JOIN  biblio   ON action_logs.object=biblio.biblionumber
132         WHERE action_logs.module = 'cataloguing' 
133         |;
134         my %filtermap = ();
135     if ($modulename eq "catalogue" or $modulename eq "acqui") {
136                 %filtermap = (
137                           user => 'borrowers.surname',
138                          title => 'biblio.title',
139                         author => 'biblio.author',
140                 );
141     } elsif ($modulename eq "members") {
142         $strsth=qq|
143                 SELECT action_logs.timestamp, action_logs.action, action_logs.info, 
144                         borrowers.cardnumber, borrowers.surname, borrowers.firstname, borrowers.userid,
145                         bor2.cardnumber, bor2.surname, bor2.firstname, bor2.userid
146         FROM action_logs 
147                 LEFT JOIN borrowers ON borrowers.borrowernumber=action_logs.user 
148                 LEFT JOIN borrowers as bor2 ON action_logs.object=bor2.borrowernumber
149         WHERE action_logs.module = 'members' 
150                 |;
151                 %filtermap = (
152                        user => 'borrowers.surname',
153                     surname => 'bor2.surname',
154                   firstname => 'bor2.firstname',
155                  cardnumber => 'bor2.cardnumber',
156                 );
157     } else {
158                 return 0;
159         }
160
161     if (@filters) {
162                 foreach my $filter (@filters) {
163                         my $tempname = $filter->{name}         or next;
164                         (grep {/^$tempname$/} keys %filtermap) or next;
165                         $filter->{value} =~ s/\*/%/g;
166                         $strsth .= " AND " . $filtermap{$tempname} . " LIKE " . $filter->{value};
167                 }
168         }
169     my $sth=$dbh->prepare($strsth);
170     $sth->execute;
171     my @results;
172     my $count;
173     my $hilighted=1;
174     while (my $data = $sth->fetchrow_hashref){
175         $data->{hilighted} = ($hilighted>0);
176         $data->{info} =~ s/\n/<br\/>/g;
177         $data->{day} = format_date($data->{timestamp});
178         push @results, $data;
179         $count++;
180         $hilighted = -$hilighted;
181     }
182     return ($count, \@results);
183 }
184
185 =item GetLogs
186
187 $logs = GetLogs($datefrom,$dateto,$user,\@modules,$action,$object,$info);
188
189 Return: 
190 C<$logs> is a ref to a hash which containts all columns from action_logs
191
192 =cut
193
194 sub GetLogs {
195     my $datefrom = shift;
196     my $dateto   = shift;
197     my $user     = shift;
198     my $modules   = shift;
199     my $action   = shift;
200     my $object   = shift;
201     my $info     = shift;
202    
203     my $iso_datefrom = C4::Dates->new($datefrom,C4::Context->preference("dateformat"))->output('iso');
204     my $iso_dateto = C4::Dates->new($dateto,C4::Context->preference("dateformat"))->output('iso');
205
206     my $dbh = C4::Context->dbh;
207     my $query = "
208         SELECT *
209         FROM   action_logs
210         WHERE 1
211     ";
212
213     my @parameters;
214     $query .= " AND DATE_FORMAT(timestamp, '%Y-%m-%d') >= \"".$iso_datefrom."\" " if $iso_datefrom;   #fix me - mysql specific
215     $query .= " AND DATE_FORMAT(timestamp, '%Y-%m-%d') <= \"".$iso_dateto."\" " if $iso_dateto;
216     if($user) {
217         $query .= " AND user = ? ";
218         push(@parameters,$user);
219     }
220     if(scalar @$modules > 1 or @$modules[0] ne "") {
221         $query .= " AND module IN (".join(",",map {"?"} @$modules).") ";
222         push(@parameters,@$modules);
223     }
224     if($action && scalar(@$action)) {
225         $query .= " AND action IN (".join(",",map {"?"} @$action).") ";
226         push(@parameters,@$action);
227     }
228     if($object) {
229         $query .= " AND object = ? ";
230         push(@parameters,$object);
231     }
232     if($info) {
233         $query .= " AND info LIKE ? ";
234         push(@parameters,"%".$info."%");
235     }
236    
237     my $sth = $dbh->prepare($query);
238     $sth->execute(@parameters);
239     
240     my @logs;
241     while( my $row = $sth->fetchrow_hashref ) {
242         $row->{$row->{module}} = 1;
243         push @logs , $row;
244     }
245     return \@logs;
246 }
247
248 1;
249 __END__
250
251 =back
252
253 =head1 AUTHOR
254
255 Koha Development Team <http://koha-community.org/>
256
257 =cut