Bug 20589: Remove expanded_facet variable and fix tests
[koha.git] / C4 / Stats.pm
blob8a8835c3462e38bfea5f89d8134446617bbbbe7b
1 package C4::Stats;
4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
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.
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.
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>.
21 use strict;
22 use warnings;
23 require Exporter;
24 use Carp;
25 use C4::Context;
26 use C4::Debug;
27 use vars qw(@ISA @EXPORT);
29 our $debug;
31 BEGIN {
32 @ISA = qw(Exporter);
33 @EXPORT = qw(
34 &UpdateStats
39 =head1 NAME
41 C4::Stats - Update Koha statistics (log)
43 =head1 SYNOPSIS
45 use C4::Stats;
47 =head1 DESCRIPTION
49 The functions of this module deals with statistics table of Koha database.
51 =head1 FUNCTIONS
53 =head2 UpdateStats
55 &UpdateStats($params);
57 Adds an entry to the statistics table in the Koha database, which acts as an activity log.
59 C<$params> is an hashref whose expected keys are:
60 branch : the branch where the transaction occurred
61 type : the type of transaction (renew, issue, localuse, return, writeoff, payment
62 itemnumber : the itemnumber of the item
63 borrowernumber : the borrowernumber of the patron
64 amount : the amount of the transaction
65 other : sipmode
66 itemtype : the type of the item
67 ccode : the collection code of the item
69 type key is mandatory.
70 For types used in C4::Circulation (renew,issue,localuse,return), the following other keys are mandatory:
71 branch, borrowernumber, itemnumber, ccode, itemtype
72 For types used in C4::Accounts (writeoff, payment), the following other keys are mandatory:
73 branch, borrowernumber, itemnumber, ccode, itemtype
74 If an optional key is not provided, the value '' is used for this key.
76 Returns undef if no C<$param> is given
78 =cut
80 sub UpdateStats {
81 my ($params) = @_;
82 # make some controls
83 return () if ! defined $params;
84 # change these arrays if new types of transaction or new parameters are allowed
85 my @allowed_keys = qw (type branch amount other itemnumber itemtype borrowernumber ccode location);
86 my @allowed_circulation_types = qw (renew issue localuse return onsite_checkout);
87 my @allowed_accounts_types = qw (writeoff payment);
88 my @circulation_mandatory_keys = qw (type branch borrowernumber itemnumber ccode itemtype);
89 my @accounts_mandatory_keys = qw (type branch borrowernumber amount);
91 my @mandatory_keys = ();
92 if (! exists $params->{type} or ! defined $params->{type}) {
93 croak ("UpdateStats did not received type param");
95 if (grep ($_ eq $params->{type}, @allowed_circulation_types )) {
96 @mandatory_keys = @circulation_mandatory_keys;
97 } elsif (grep ($_ eq $params->{type}, @allowed_accounts_types )) {
98 @mandatory_keys = @accounts_mandatory_keys;
99 } else {
100 croak ("UpdateStats received forbidden type param: ".$params->{type});
102 my @missing_params = ();
103 for my $mykey (@mandatory_keys ) {
104 push @missing_params, $mykey if !grep (/^$mykey/, keys %$params);
106 if (scalar @missing_params > 0 ) {
107 croak ("UpdateStats did not received mandatory param(s): ".join (", ",@missing_params ));
109 my @invalid_params = ();
110 for my $myparam (keys %$params ) {
111 push @invalid_params, $myparam unless grep (/^$myparam$/, @allowed_keys);
113 if (scalar @invalid_params > 0 ) {
114 croak ("UpdateStats received invalid param(s): ".join (", ",@invalid_params ));
116 # get the parameters
117 my $branch = $params->{branch};
118 my $type = $params->{type};
119 my $borrowernumber = exists $params->{borrowernumber} ? $params->{borrowernumber} : '';
120 my $itemnumber = exists $params->{itemnumber} ? $params->{itemnumber} : undef;
121 my $amount = exists $params->{amount} ? $params->{amount} : 0;
122 my $other = exists $params->{other} ? $params->{other} : '';
123 my $itemtype = exists $params->{itemtype} ? $params->{itemtype} : '';
124 my $location = exists $params->{location} ? $params->{location} : undef;
125 my $ccode = exists $params->{ccode} ? $params->{ccode} : '';
127 my $dbh = C4::Context->dbh;
128 my $sth = $dbh->prepare(
129 "INSERT INTO statistics
130 (datetime,
131 branch, type, value,
132 other, itemnumber, itemtype, location,
133 borrowernumber, ccode)
134 VALUES (now(),?,?,?,?,?,?,?,?,?)"
136 $sth->execute(
137 $branch, $type, $amount, $other,
138 $itemnumber, $itemtype, $location, $borrowernumber,
139 $ccode
144 __END__
146 =head1 AUTHOR
148 Koha Development Team <http://koha-community.org/>
150 =cut