syncing dev-week and HEAD
[koha.git] / C4 / Log.pm
1 package C4::Log; #assumes 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 with
20 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
21 # Suite 330, Boston, MA  02111-1307 USA
22
23 use strict;
24 use C4::Context;
25 use C4::Date;
26
27 require Exporter;
28
29 use vars qw($VERSION @ISA @EXPORT);
30
31 # set the version for version checking
32 $VERSION = 0.01;
33
34 =head1 NAME
35
36 C4::Log - Koha Log Facility functions
37
38 =head1 SYNOPSIS
39
40   use C4::Log;
41
42 =head1 DESCRIPTION
43
44 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.
45
46 =head1 FUNCTIONS
47
48 =over 2
49
50 =cut
51
52 @ISA = qw(Exporter);
53 @EXPORT = qw(&logaction &logstatus &displaylog);
54
55 =item logaction
56
57   &logaction($usernumber, $modulename, $actionname, $infos);
58
59 Adds a record into action_logs table to report the different changes upon the database
60
61 =cut
62 #'
63 sub logaction{
64   my ($usernumber,$modulename, $actionname, $objectnumber, $infos)=@_;
65         $usernumber='' unless $usernumber;
66         my $dbh = C4::Context->dbh;
67         my $sth=$dbh->prepare("Insert into action_logs (timestamp,user,module,action,object,info) values (now(),?,?,?,?,?)");
68         $sth->execute($usernumber,$modulename,$actionname,$objectnumber,$infos);
69         $sth->finish;
70 }
71
72 =item logstatus
73
74   &logstatus;
75
76 returns True If Activate_Log variable is equal to On
77 Activate_Log is a system preference Variable
78 =cut
79 #'
80 sub logstatus{
81         return C4::Context->preference("Activate_Log");
82 }
83
84 =item displaylog
85
86   &displaylog($modulename, @filters);
87   $modulename is the name of the module on which the user wants to display logs
88   @filters is an optional table of hash containing :
89         - name : the name of the variable to filter
90         - value : the value of the filter.... May be with * joker
91
92 returns a table of hash containing who did what on which object at what time
93
94 =cut
95 #'
96 sub displaylog{
97   my ($modulename, @filters)=@_;
98         my $dbh = C4::Context->dbh;
99         my $strsth;
100         if ($modulename eq "catalogue"){
101                 $strsth="select action_logs.timestamp, action_logs.action, action_logs.info, borrowers.cardnumber, borrowers.surname, borrowers.firstname, borrowers.userid,";
102                 $strsth .= "biblio.biblionumber, biblio.title, biblio.author" ;#if ($modulename eq "acqui.simple");
103                 $strsth .= " FROM borrowers,action_logs ";
104                 $strsth .= ",biblio " ;#if ($modulename eq "acqui.simple");
105         
106                 $strsth .=" WHERE borrowers.borrowernumber=action_logs.user";
107                 $strsth .=" AND action_logs.module = 'acqui.simple' AND action_logs.object=biblio.biblionumber ";# if ($modulename eq "acqui.simple");
108                 if (@filters){
109                         foreach my $filter (@filters){
110                                 if ($filter->{name} =~ /user/){
111                                         $filter->{value}=~s/\*/%/g;
112                                         $strsth .= " AND borrowers.surname like ".$filter->{value};
113                                 }elsif ($filter->{name} =~ /title/){
114                                         $filter->{value}=~s/\*/%/g;
115                                         $strsth .= " AND biblio.title like ".$filter->{value};
116                                 }elsif ($filter->{name} =~ /author/){
117                                         $filter->{value}=~s/\*/%/g;
118                                         $strsth .= " AND biblio.author like ".$filter->{value};
119                                 }
120                         }
121                 }
122         } elsif ($modulename eq "acqui")  {
123                 $strsth="select action_logs.timestamp, action_logs.action, action_logs.info, borrowers.cardnumber, borrowers.surname, borrowers.firstname, borrowers.userid,";
124                 $strsth .= "biblio.biblionumber, biblio.title, biblio.author" ;#if ($modulename eq "acqui.simple");
125                 $strsth .= "FROM borrowers,action_logs ";
126                 $strsth .= ",biblio " ;#if ($modulename eq "acqui.simple");
127         
128                 $strsth .=" WHERE borrowers.borrowernumber=action_logs.user";
129                 $strsth .= "AND action_logs.module = 'acqui.simple' AND action_logs.object=biblio.biblionumber ";# if ($modulename eq "acqui.simple");
130                 if (@filters){
131                         foreach my $filter (@filters){
132                                 if ($filter->{name} =~ /user/){
133                                         $filter->{value}=~s/\*/%/g;
134                                         $strsth .= " AND borrowers.surname like ".$filter->{value};
135                                 }elsif ($filter->{name} =~ /title/){
136                                         $filter->{value}=~s/\*/%/g;
137                                         $strsth .= " AND biblio.title like ".$filter->{value};
138                                 }elsif ($filter->{name} =~ /author/){
139                                         $filter->{value}=~s/\*/%/g;
140                                         $strsth .= " AND biblio.author like ".$filter->{value};
141                                 }
142                         }
143                 }
144         } elsif ($modulename eq "members"){
145                 $strsth="select action_logs.timestamp, action_logs.action, action_logs.info, borrowers.cardnumber, borrowers.surname, borrowers.firstname, borrowers.userid,";
146                 $strsth .= "bor2.cardnumber, bor2.surname, bor2.firstname, bor2.userid,";
147                 $strsth .= "FROM borrowers,action_logs,borrowers as bor2 ";
148         
149                 $strsth .=" WHERE borrowers.borrowernumber=action_logs.user";
150                 $strsth .= "AND action_logs.module = 'members' AND action_logs.object=bor2.borrowernumber ";# if ($modulename eq "acqui.simple");
151                 if (@filters){
152                         foreach my $filter (@filters){
153                                 if ($filter->{name} =~ /user/){
154                                         $filter->{value}=~s/\*/%/g;
155                                         $strsth .= " AND borrowers.surname like ".$filter->{value};
156                                 }elsif ($filter->{name} =~ /surname/){
157                                         $filter->{value}=~s/\*/%/g;
158                                         $strsth .= " AND bor2.surname like ".$filter->{value};
159                                 }elsif ($filter->{name} =~ /firstname/){
160                                         $filter->{value}=~s/\*/%/g;
161                                         $strsth .= " AND bor2.firsntame like ".$filter->{value};
162                                 }elsif ($filter->{name} =~ /cardnumber/){
163                                         $filter->{value}=~s/\*/%/g;
164                                         $strsth .= " AND bor2.cardnumber like ".$filter->{value};
165                                 }
166                         }
167                 }
168         }
169 #       warn "displaylog :".$strsth;
170         if ($strsth){
171                 my $sth=$dbh->prepare($strsth);
172                 $sth->execute;
173                 my @results;
174                 my $count;
175                 my $hilighted=1;
176                 while (my $data = $sth->fetchrow_hashref){
177                         $data->{hilighted} = ($hilighted>0);
178                         $data->{info} =~ s/\n/<br\/>/g;
179                         $data->{day} = format_date($data->{timestamp});
180                         push @results, $data;
181                         $count++;
182                         $hilighted = -$hilighted;
183                 }
184                 return ($count, \@results);
185         } else {return 0;}
186 }
187 END { }       # module clean-up code here (global destructor)
188
189 1;
190 __END__
191
192 =back
193
194 =head1 AUTHOR
195
196 Koha Developement team <info@koha.org>
197
198 =cut