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

(-)a/C4/UsageStats.pm (-21 / +27 lines)
Lines 1-4 Link Here
1
package UsageStats;
1
package C4::UsageStats;
2
2
3
# This file is part of Koha.
3
# This file is part of Koha.
4
#
4
#
Lines 22-28 use C4::Context; Link Here
22
use POSIX qw(strftime);
22
use POSIX qw(strftime);
23
use LWP::UserAgent;
23
use LWP::UserAgent;
24
use JSON;
24
use JSON;
25
use URI::Encode qw(uri_encode);
26
25
27
=head1 NAME C4::UsageStats
26
=head1 NAME C4::UsageStats
28
27
Lines 45-51 only once a month ! Link Here
45
44
46
sub NeedUpdate {
45
sub NeedUpdate {
47
    my $lastupdated = C4::Context->preference('UsageStatsLastUpdateTime') || 0;
46
    my $lastupdated = C4::Context->preference('UsageStatsLastUpdateTime') || 0;
48
    my $now = strftime("%s", localtime);
47
    my $now = strftime( "%s", localtime );
49
48
50
    # Need to launch cron.
49
    # Need to launch cron.
51
    return 1 if $now - $lastupdated >= 2592000;
50
    return 1 if $now - $lastupdated >= 2592000;
Lines 57-69 sub NeedUpdate { Link Here
57
sub BuildReport {
56
sub BuildReport {
58
    my $report = {
57
    my $report = {
59
        library => {
58
        library => {
59
            id   => C4::Context->preference('UsageStatsID')          || 0,
60
            name => C4::Context->preference('UsageStatsLibraryName') || q||,
60
            name => C4::Context->preference('UsageStatsLibraryName') || q||,
61
            id => C4::Context->preference('UsageStatsID') || 0,
61
            url => C4::Context->preference('UsageStatsLibraryUrl')   || q||,
62
            type => C4::Context->preference('UsageStatsLibraryType') || q||,
63
            country => C4::Context->preference('UsageStatsCountry') || q||,
62
        },
64
        },
63
    };
65
    };
64
66
65
    # Get database volumetry.
67
    # Get database volumetry.
66
    foreach (qw/biblio auth_header old_issues old_reserves borrowers aqorders subscription/) {
68
    foreach (
69
        qw/biblio auth_header old_issues old_reserves borrowers aqorders subscription/
70
      )
71
    {
67
        $report->{volumetry}{$_} = _count($_);
72
        $report->{volumetry}{$_} = _count($_);
68
    }
73
    }
69
74
Lines 338-355 Send to hea.koha-community.org database informations Link Here
338
=cut
343
=cut
339
344
340
sub ReportToCommunity {
345
sub ReportToCommunity {
341
      my $data = shift;
346
    my $data = shift;
342
      my $json = uri_encode( to_json($data), 1 );
347
    my $json = encode_json($data);
343
348
344
      my $ua = LWP::UserAgent->new;
349
    my $url = "http://hea.koha-community.org/upload.pl";
345
      my $req =
350
    my $ua = LWP::UserAgent->new;
346
        HTTP::Request->new( POST => "http://hea.koha-community.org/upload.pl" );
351
    my $res = $ua->post(
347
      $req->content_type('application/x-www-form-urlencoded');
352
        $url,
348
      $req->content("data=$json");
353
        'Content-type' => 'application/json;charset=utf-8',
349
      my $res     = $ua->request($req);
354
        Content => $json,
350
      my $content = from_json( $res->decoded_content );
355
    );
351
      C4::Context->set_preference( 'UsageStatsID',
356
    my $content = decode_json( $res->decoded_content );
352
          $content->{library}{library_id} );
357
    C4::Context->set_preference( 'UsageStatsID',
358
        $content->{library}{id} );
353
}
359
}
354
360
355
=head2 _count
361
=head2 _count
Lines 361-372 Count the number of records in $table tables Link Here
361
=cut
367
=cut
362
368
363
sub _count {
369
sub _count {
364
      my $table = shift;
370
    my $table = shift;
365
371
366
      my $dbh = C4::Context->dbh;
372
    my $dbh = C4::Context->dbh;
367
      my $sth = $dbh->prepare("SELECT count(*) from $table");
373
    my $sth = $dbh->prepare("SELECT count(*) from $table");
368
      $sth->execute;
374
    $sth->execute;
369
      return $sth->fetchrow_array;
375
    return $sth->fetchrow_array;
370
}
376
}
371
377
372
1;
378
1;
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/admin.pref (+20 lines)
Lines 127-129 Administration: Link Here
127
            - will be shown on <a href="http://hea.koha-community.org">Hea Koha community website</a>.
127
            - will be shown on <a href="http://hea.koha-community.org">Hea Koha community website</a>.
128
            - If this name is empty, you will sent data anonymously
128
            - If this name is empty, you will sent data anonymously
129
            - Note that this value has no effect if UsageStatsShare system preference is set to "Don't share"
129
            - Note that this value has no effect if UsageStatsShare system preference is set to "Don't share"
130
        -
131
            - The country where your library is
132
            - pref: UsageStatsCountry
133
            - will be shown on <a href="http://hea.koha-community.org">Hea Koha community website</a>.
134
            - Note that this value has no effect if UsageStatsShare system preference is set to "Don't share"
135
        -
136
            - The library url
137
            - pref: UsageStatsLibraryUrl
138
            - will be shown on <a href="http://hea.koha-community.org">Hea Koha community website</a>.
139
            - Note that this value has no effect if UsageStatsShare system preference is set to "Don't share"
140
141
        -
142
            - The library type
143
            - pref: UsageStatsLibraryType
144
              default: public
145
              choices:
146
                  public: "public"
147
                  university: "university"
148
            - will be shown on <a href="http://hea.koha-community.org">Hea Koha community website</a>.
149
            - Note that this value has no effect if UsageStatsShare system preference is set to "Don't share"
(-)a/misc/cronjobs/share_usage_with_koha_community.pl (-13 / +24 lines)
Lines 7-37 use Getopt::Long; Link Here
7
7
8
use C4::Context;
8
use C4::Context;
9
use C4::UsageStats;
9
use C4::UsageStats;
10
use POSIX qw(strftime);
10
11
11
12
my ( $help, $verbose, $force );
12
my ( $help, $verbose );
13
GetOptions(
13
GetOptions(
14
    'h|help'                 => \$help,
14
    'h|help'    => \$help,
15
    'v|verbose'              => \$verbose,
15
    'v|verbose' => \$verbose,
16
    'f|force'   => \$force,
16
) || pod2usage(1);
17
) || pod2usage(1);
17
18
18
if ($help) {
19
if ($help) {
19
    pod2usage(1);
20
    pod2usage(1);
20
}
21
}
21
22
22
unless( C4::Context->preference('UsageStatsShare') ) {
23
unless ( C4::Context->preference('UsageStatsShare') ) {
23
  pod2usage (q|The UsageStats system preference is not set. If your library  wants to share their usage statistics with the Koha community, you have to switch on this system preference|);
24
    pod2usage(
24
  exit 1;
25
q|
26
The UsageStats system preference is not set.
27
If your library wants to share their usage statistics with the Koha community, you have to switch on this system preference
28
|
29
    );
30
    exit 1;
25
}
31
}
26
32
27
my $need_update = C4::UsageStats::NeedUpdate();
33
my $need_update = ($force ? 1 : C4::UsageStats::NeedUpdate() );
28
34
29
if ( $need_update ) {
35
if ($need_update) {
30
    say "Data need to be updated" if $verbose;
36
    say "Data need to be updated" if $verbose;
31
    my $report = C4::UsageStats::BuildReport();
37
    my $report = C4::UsageStats::BuildReport();
32
    C4::UsageStats::ReportToCommunity($report);
38
    C4::UsageStats::ReportToCommunity($report);
33
    C4::Context->set_preference('UsageStatsLastUpdateTime', strftime("%s", localtime));
39
    C4::Context->set_preference( 'UsageStatsLastUpdateTime',
34
} elsif( $verbose ) {
40
        strftime( "%s", localtime ) );
41
}
42
elsif ($verbose) {
35
    say "Data don't need to be updated";
43
    say "Data don't need to be updated";
36
}
44
}
37
45
Lines 53-59 Only the total number is retrieved. In no case private data will be shared! Link Here
53
61
54
In order to know which parts of Koha modules are used, this script will collect some system preference values.
62
In order to know which parts of Koha modules are used, this script will collect some system preference values.
55
63
56
If you want to tell us who you are, you can fill the UsageStatsLibraryName system preference with your library name.
64
If you want to tell us who you are, you can fill the UsageStatsLibraryName system preference with your library name, UsageStatsLibraryUrl, UsageStatsLibraryType and/or UsageStatsCountry.
57
65
58
All these data will be analysed on the http://hea.koha-community.org Koha community website.
66
All these data will be analysed on the http://hea.koha-community.org Koha community website.
59
67
Lines 72-77 Print a brief help message Link Here
72
80
73
Verbose mode.
81
Verbose mode.
74
82
83
=item B<-f|--force>
84
85
Force the update.
86
75
=back
87
=back
76
88
77
=head1 AUTHOR
89
=head1 AUTHOR
78
- 

Return to bug 11926