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