Bug 11926: Render community koha statistic usages
[koha.git] / C4 / UsageStats.pm
blob5338df95f7e43728414f863e9bcd99e8d11ecd78
1 package UsageStats;
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
12 # version.
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.
22 use strict;
23 use C4::Context;
24 use POSIX qw(strftime);
25 use LWP::UserAgent;
26 use JSON;
28 sub NeedUpdate {
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.
36 return 0;
39 sub LaunchCron {
40 if (!C4::Context->preference('UsageStatsShare')) {
41 die ("UsageStats is not configured");
43 if (NeedUpdate) {
44 C4::Context->set_preference('UsageStatsLastUpdateTime', strftime("%s", localtime));
45 my $data = BuildReport();
46 ReportToComunity($data);
50 sub BuildReport {
51 my $report = {
52 'library' => {
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($_);
67 return $report;
70 sub ReportToComunity {
71 my $data = shift;
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});
85 sub _count {
86 my $table = shift;
88 my $dbh = C4::Context->dbh;
89 my $sth = $dbh->prepare("SELECT count(*) from $table");
90 $sth->execute;
91 return $sth->fetchrow_array;
94 &LaunchCron;