3 # Copyright 2000-2003 Katipo Communications
4 # Copyright 2010 BibLibre
5 # Parts Copyright 2010 Catalyst IT
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along
19 # with Koha; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 use POSIX qw(strftime);
29 my $lastupdated = C4::Context->preference('UsageStatsLastUpdateTime') || 0;
30 my $now = strftime("%s", localtime);
32 # Need to launch cron.
33 return 1 if $now - $lastupdated >= 2592000;
35 # Cron no need to be launched.
40 if (!C4::Context->preference('UsageStatsShare')) {
41 die ("UsageStats is not configured");
44 C4::Context->set_preference('UsageStatsLastUpdateTime', strftime("%s", localtime));
45 my $data = BuildReport();
46 ReportToComunity($data);
53 'name' => C4::Context->preference('UsageStatsLibraryName'),
54 'id' => C4::Context->preference('UsageStatsID') || 0,
58 # Get database volumetry.
59 foreach (qw/biblio auth_header old_issues old_reserves borrowers aqorders subscription/) {
60 $report->{volumetry}{$_} = _count($_);
63 # Get systempreferences.
64 foreach (qw/IntranetBiblioDefaultView marcflavour/) {
65 $report->{systempreferences}{$_} = C4::Context->preference($_);
70 sub ReportToComunity {
72 my $json = to_json($data);
74 my $url = C4::Context->config('mebaseurl');
76 my $ua = LWP::UserAgent->new;
77 my $req = HTTP::Request->new(POST => "$url/upload.pl");
78 $req->content_type('application/x-www-form-urlencoded');
79 $req->content("data=$json");
80 my $res = $ua->request($req);
81 my $content = from_json($res->decoded_content);
82 C4::Context->set_preference('UsageStatsID', $content->{library}{library_id});
88 my $dbh = C4::Context->dbh;
89 my $sth = $dbh->prepare("SELECT count(*) from $table");
91 return $sth->fetchrow_array;