|
Line 0
Link Here
|
|
|
1 |
package UsageStats; |
| 2 |
|
| 3 |
# Copyright 2000-2003 Katipo Communications |
| 4 |
# Copyright 2010 BibLibre |
| 5 |
# Parts Copyright 2010 Catalyst IT |
| 6 |
# |
| 7 |
# This file is part of Koha. |
| 8 |
# |
| 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. |
| 13 |
# |
| 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. |
| 17 |
# |
| 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. |
| 21 |
|
| 22 |
use strict; |
| 23 |
use C4::Context; |
| 24 |
use POSIX qw(strftime); |
| 25 |
use LWP::UserAgent; |
| 26 |
use JSON; |
| 27 |
|
| 28 |
sub NeedUpdate { |
| 29 |
my $lastupdated = C4::Context->preference('UsageStatsLastUpdateTime') || 0; |
| 30 |
my $now = strftime("%s", localtime); |
| 31 |
|
| 32 |
# Need to launch cron. |
| 33 |
return 1 if $now - $lastupdated >= 2592000; |
| 34 |
|
| 35 |
# Cron no need to be launched. |
| 36 |
return 0; |
| 37 |
} |
| 38 |
|
| 39 |
sub LaunchCron { |
| 40 |
if (!C4::Context->preference('UsageStatsShare')) { |
| 41 |
die ("UsageStats is not configured"); |
| 42 |
} |
| 43 |
if (NeedUpdate) { |
| 44 |
C4::Context->set_preference('UsageStatsLastUpdateTime', strftime("%s", localtime)); |
| 45 |
my $data = BuildReport(); |
| 46 |
ReportToComunity($data); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
sub BuildReport { |
| 51 |
my $report = { |
| 52 |
'library' => { |
| 53 |
'name' => C4::Context->preference('UsageStatsLibraryName'), |
| 54 |
'id' => C4::Context->preference('UsageStatsID') || 0, |
| 55 |
}, |
| 56 |
}; |
| 57 |
|
| 58 |
# Get database volumetry. |
| 59 |
foreach (qw/biblio auth_header old_issues old_reserves borrowers aqorders subscription/) { |
| 60 |
$report->{volumetry}{$_} = _count($_); |
| 61 |
} |
| 62 |
|
| 63 |
# Get systempreferences. |
| 64 |
foreach (qw/IntranetBiblioDefaultView marcflavour/) { |
| 65 |
$report->{systempreferences}{$_} = C4::Context->preference($_); |
| 66 |
} |
| 67 |
return $report; |
| 68 |
} |
| 69 |
|
| 70 |
sub ReportToComunity { |
| 71 |
my $data = shift; |
| 72 |
my $json = to_json($data); |
| 73 |
|
| 74 |
my $url = C4::Context->config('mebaseurl'); |
| 75 |
|
| 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}); |
| 83 |
} |
| 84 |
|
| 85 |
sub _count { |
| 86 |
my $table = shift; |
| 87 |
|
| 88 |
my $dbh = C4::Context->dbh; |
| 89 |
my $sth = $dbh->prepare("SELECT count(*) from $table"); |
| 90 |
$sth->execute; |
| 91 |
return $sth->fetchrow_array; |
| 92 |
} |
| 93 |
|
| 94 |
&LaunchCron; |
| 95 |
1; |