View | Details | Raw Unified | Return to bug 11926
Collapse All | Expand All

(-)a/C4/UsageStats.pm (+95 lines)
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;
(-)a/installer/data/mysql/sysprefs.sql (+4 lines)
Lines 403-408 INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` Link Here
403
('UpdateTotalIssuesOnCirc','0',NULL,'Whether to update the totalissues field in the biblio on each circ.','YesNo'),
403
('UpdateTotalIssuesOnCirc','0',NULL,'Whether to update the totalissues field in the biblio on each circ.','YesNo'),
404
('uppercasesurnames','0',NULL,'If ON, surnames are converted to upper case in patron entry form','YesNo'),
404
('uppercasesurnames','0',NULL,'If ON, surnames are converted to upper case in patron entry form','YesNo'),
405
('URLLinkText','',NULL,'Text to display as the link anchor in the OPAC','free'),
405
('URLLinkText','',NULL,'Text to display as the link anchor in the OPAC','free'),
406
('UsageStatsLastUpdateTime', '', '', '', 'Free'),
407
('UsageStatsID', '', '', '', 'Free'),
408
('UsageStatsShare', 'no', '0|1|2', 'Tell if you want to share you data volumetry with koha community.  You can be anonymous', 'Choice'),
409
('UsageStatsLibraryName', '', '', 'Your library name shown on koha community site. Can be disabled with UsageStatsShare system preference.', 'Free'),
406
('UseAuthoritiesForTracings','1','0','Use authority record numbers for subject tracings instead of heading strings.','YesNo'),
410
('UseAuthoritiesForTracings','1','0','Use authority record numbers for subject tracings instead of heading strings.','YesNo'),
407
('UseBranchTransferLimits','0','','If ON, Koha will will use the rules defined in branch_transfer_limits to decide if an item transfer should be allowed.','YesNo'),
411
('UseBranchTransferLimits','0','','If ON, Koha will will use the rules defined in branch_transfer_limits to decide if an item transfer should be allowed.','YesNo'),
408
('UseControlNumber','0','','If ON, record control number (w subfields) and control number (001) are used for linking of bibliographic records.','YesNo'),
412
('UseControlNumber','0','','If ON, record control number (w subfields) and control number (001) are used for linking of bibliographic records.','YesNo'),
(-)a/installer/data/mysql/updatedatabase.pl (+10 lines)
Lines 8056-8061 if ( CheckVersion($DBversion) ) { Link Here
8056
   SetVersion ($DBversion);
8056
   SetVersion ($DBversion);
8057
}
8057
}
8058
8058
8059
$DBversion = "3.15.00.XXX";
8060
if ( CheckVersion($DBversion) ) {
8061
   $dbh->do("INSERT INTO systempreferences VALUES ('UsageStatsLastUpdateTime', '', '', '', 'Free');");
8062
   $dbh->do("INSERT INTO systempreferences VALUES ('UsageStatsID', '', '', '', 'Free');");
8063
   $dbh->do("INSERT INTO systempreferences VALUES ('UsageStatsShare', 'no', '0|1|2', 'Tell if you want to share you data volumetry with koha community.  You can be anonymous', 'Choice');");
8064
   $dbh->do("INSERT INTO systempreferences VALUES ('UsageStatsLibraryName', '', '', 'Your library name shown on koha community site. Can be disabled with UsageStatsShare system preference.', 'Free');");
8065
   print "Upgrade to $DBversion done (Bug 11926: Setup basic config for community usage koha statistics)\n";
8066
   SetVersion ($DBversion);
8067
}
8068
8059
=head1 FUNCTIONS
8069
=head1 FUNCTIONS
8060
8070
8061
=head2 TableExists($table)
8071
=head2 TableExists($table)
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/admin.pref (-1 / +17 lines)
Lines 113-115 Administration: Link Here
113
                Solr: Solr
113
                Solr: Solr
114
                Zebra: Zebra
114
                Zebra: Zebra
115
            - is the search engine used.
115
            - is the search engine used.
116
- 
116
    Usage Stats:
117
        -
118
            - pref: UsageStatsLastUpdateTime
119
              default: 0
120
            - stores the last time when cron were launch 
121
        -
122
            - pref: UsageStatsID
123
              default: 
124
            - 
125
        -
126
            - pref: UsageStatsShare
127
              default: 
128
            - 
129
        -
130
            - pref: UsageStatsLibraryName
131
              default: 
132
            - 

Return to bug 11926