Cleaning up perldocs for make utility that MJ wrote. I was getting a bunch
[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 = do { my @v = '$Revision$' =~ /\d+/g; shift(@v) . "." . join( "_", map { sprintf "%03d", $_ } @v ); };
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 &GetLogStatus &displaylog &GetLogs);
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 #'
64 sub logaction {
65   my ($usernumber,$modulename, $actionname, $objectnumber, $infos)=@_;
66     $usernumber='' unless $usernumber;
67     my $dbh = C4::Context->dbh;
68     my $sth=$dbh->prepare("Insert into action_logs (timestamp,user,module,action,object,info) values (now(),?,?,?,?,?)");
69     $sth->execute($usernumber,$modulename,$actionname,$objectnumber,$infos);
70     $sth->finish;
71 }
72
73 =item GetLogStatus
74
75   $status = GetLogStatus;
76
77 C<$status> is a hasref like this example:
78     $hash = {
79         BorrowersLog   => 1,
80         CataloguingLog => 0,
81         IssueLog       => 0,
82         ...
83     }
84
85 =cut
86
87 #'
88 sub GetLogStatus {
89     my %hash;
90     $hash{BorrowersLog}    = C4::Context->preference("BorrowersLog");
91     $hash{CataloguingLog}  = C4::Context->preference("CataloguingLog");
92     $hash{IssueLog}        = C4::Context->preference("IssueLog");
93     $hash{ReturnLog}       = C4::Context->preference("CataloguingLog");
94     $hash{SubscriptionLog} = C4::Context->preference("CataloguingLog");
95     $hash{LetterLog}       = C4::Context->preference("LetterLog");
96     $hash{FinesLog}       = C4::Context->preference("FinesLog");
97     
98     return \%hash;
99 }
100
101 =item displaylog
102
103   &displaylog($modulename, @filters);
104   $modulename is the name of the module on which the user wants to display logs
105   @filters is an optional table of hash containing :
106       - name : the name of the variable to filter
107     - value : the value of the filter.... May be with * joker
108
109 returns a table of hash containing who did what on which object at what time
110
111 =cut
112
113 #'
114 sub displaylog {
115   my ($modulename, @filters) = @_;
116     my $dbh = C4::Context->dbh;
117     my $strsth;
118     if ($modulename eq "catalogue"){
119         $strsth="select action_logs.timestamp, action_logs.action, action_logs.info, borrowers.cardnumber, borrowers.surname, borrowers.firstname, borrowers.userid,";
120         $strsth .= "biblio.biblionumber, biblio.title, biblio.author" ;#if ($modulename eq "acqui.simple");
121         $strsth .= " FROM borrowers,action_logs ";
122         $strsth .= ",biblio " ;#if ($modulename eq "acqui.simple");
123     
124         $strsth .=" WHERE borrowers.borrowernumber=action_logs.user";
125         $strsth .=" AND action_logs.module = 'cataloguing' AND action_logs.object=biblio.biblionumber ";# if ($modulename eq "acqui.simple");
126         if (@filters) {
127             foreach my $filter (@filters) {
128                 if ($filter->{name} =~ /user/) {
129                     $filter->{value}=~s/\*/%/g;
130                     $strsth .= " AND borrowers.surname like ".$filter->{value};
131                 } elsif ($filter->{name} =~ /title/) {
132                     $filter->{value}=~s/\*/%/g;
133                     $strsth .= " AND biblio.title like ".$filter->{value};
134                 } elsif ($filter->{name} =~ /author/) {
135                     $filter->{value}=~s/\*/%/g;
136                     $strsth .= " AND biblio.author like ".$filter->{value};
137                 }
138             }
139         }
140     } elsif ($modulename eq "acqui") {
141         $strsth="select action_logs.timestamp, action_logs.action, action_logs.info, borrowers.cardnumber, borrowers.surname, borrowers.firstname, borrowers.userid,";
142         $strsth .= "biblio.biblionumber, biblio.title, biblio.author" ;#if ($modulename eq "acqui.simple");
143         $strsth .= "FROM borrowers,action_logs ";
144         $strsth .= ",biblio " ;#if ($modulename eq "acqui.simple");
145     
146         $strsth .=" WHERE borrowers.borrowernumber=action_logs.user";
147         $strsth .= "AND action_logs.module = 'cataloguing' AND action_logs.object=biblio.biblionumber ";# if ($modulename eq "acqui.simple");
148         if (@filters){
149             foreach my $filter (@filters){
150                 if ($filter->{name} =~ /user/){
151                     $filter->{value}=~s/\*/%/g;
152                     $strsth .= " AND borrowers.surname like ".$filter->{value};
153                 }elsif ($filter->{name} =~ /title/){
154                     $filter->{value}=~s/\*/%/g;
155                     $strsth .= " AND biblio.title like ".$filter->{value};
156                 }elsif ($filter->{name} =~ /author/){
157                     $filter->{value}=~s/\*/%/g;
158                     $strsth .= " AND biblio.author like ".$filter->{value};
159                 }
160             }
161         }
162     } elsif ($modulename eq "members"){
163         $strsth="select action_logs.timestamp, action_logs.action, action_logs.info, borrowers.cardnumber, borrowers.surname, borrowers.firstname, borrowers.userid,";
164         $strsth .= "bor2.cardnumber, bor2.surname, bor2.firstname, bor2.userid,";
165         $strsth .= "FROM borrowers,action_logs,borrowers as bor2 ";
166     
167         $strsth .=" WHERE borrowers.borrowernumber=action_logs.user";
168         $strsth .= "AND action_logs.module = 'members' AND action_logs.object=bor2.borrowernumber ";# if ($modulename eq "acqui.simple");
169         if (@filters){
170             foreach my $filter (@filters){
171                 if ($filter->{name} =~ /user/){
172                     $filter->{value}=~s/\*/%/g;
173                     $strsth .= " AND borrowers.surname like ".$filter->{value};
174                 }elsif ($filter->{name} =~ /surname/){
175                     $filter->{value}=~s/\*/%/g;
176                     $strsth .= " AND bor2.surname like ".$filter->{value};
177                 }elsif ($filter->{name} =~ /firstname/){
178                     $filter->{value}=~s/\*/%/g;
179                     $strsth .= " AND bor2.firsntame like ".$filter->{value};
180                 }elsif ($filter->{name} =~ /cardnumber/){
181                     $filter->{value}=~s/\*/%/g;
182                     $strsth .= " AND bor2.cardnumber like ".$filter->{value};
183                 }
184             }
185         }
186     }
187     
188     if ($strsth){
189         my $sth=$dbh->prepare($strsth);
190         $sth->execute;
191         my @results;
192         my $count;
193         my $hilighted=1;
194         while (my $data = $sth->fetchrow_hashref){
195             $data->{hilighted} = ($hilighted>0);
196             $data->{info} =~ s/\n/<br\/>/g;
197             $data->{day} = format_date($data->{timestamp});
198             push @results, $data;
199             $count++;
200             $hilighted = -$hilighted;
201         }
202         return ($count, \@results);
203     } else {return 0;}
204 }
205
206 =head2 GetLogs
207
208 $logs = GetLogs($datefrom,$dateto,$user,$module,$action,$object,$info);
209
210 Return: 
211 C<$logs> is a ref to a hash which containts all columns from action_logs
212
213 =cut
214
215 sub GetLogs {
216     my $datefrom = shift;
217     my $dateto   = shift;
218     my $user     = shift;
219     my $module   = shift;
220     my $action   = shift;
221     my $object   = shift;
222     my $info     = shift;
223     
224     my $dbh = C4::Context->dbh;
225     my $query = "
226         SELECT *
227         FROM   action_logs
228         WHERE 1
229     ";
230     $query .= " AND DATE_FORMAT(timestamp, '%Y-%m-%d') >= \"".$datefrom."\" " if $datefrom;
231     $query .= " AND DATE_FORMAT(timestamp, '%Y-%m-%d') <= \"".$dateto."\" " if $dateto;
232     $query .= " AND user LIKE \"%".$user."%\" "     if $user;
233     $query .= " AND module LIKE \"%".$module."%\" " if $module;
234     $query .= " AND action LIKE \"%".$action."%\" " if $action;
235     $query .= " AND object LIKE \"%".$object."%\" " if $object;
236     $query .= " AND info LIKE \"%".$info."%\" "     if $info;
237     
238     my $sth = $dbh->prepare($query);
239     $sth->execute;
240     
241     my @logs;
242     while( my $row = $sth->fetchrow_hashref ) {
243         $row->{$row->{module}} = 1;
244         push @logs , $row;
245     }
246     return \@logs;
247 }
248
249 END { }       # module clean-up code here (global destructor)
250
251 1;
252 __END__
253
254 =back
255
256 =head1 AUTHOR
257
258 Koha Developement team <info@koha.org>
259
260 =cut