From 44f810fad0f2f6232a0885ec2f29a192a9a30da6 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 4 Sep 2014 11:49:27 +0200 Subject: [PATCH] Bug 11926: Add new sysprefs and fix encoding issues --- C4/UsageStats.pm | 48 ++++++++++++---------- installer/data/mysql/sysprefs.sql | 5 ++- installer/data/mysql/updatedatabase.pl | 24 ++++++----- .../prog/en/modules/admin/preferences/admin.pref | 20 +++++++++ misc/cronjobs/share_usage_with_koha_community.pl | 36 ++++++++++------ 5 files changed, 89 insertions(+), 44 deletions(-) diff --git a/C4/UsageStats.pm b/C4/UsageStats.pm index 0187f86..4783df2 100644 --- a/C4/UsageStats.pm +++ b/C4/UsageStats.pm @@ -1,4 +1,4 @@ -package UsageStats; +package C4::UsageStats; # This file is part of Koha. # @@ -22,7 +22,6 @@ use C4::Context; use POSIX qw(strftime); use LWP::UserAgent; use JSON; -use URI::Encode qw(uri_encode); =head1 NAME C4::UsageStats @@ -45,7 +44,7 @@ only once a month ! sub NeedUpdate { my $lastupdated = C4::Context->preference('UsageStatsLastUpdateTime') || 0; - my $now = strftime("%s", localtime); + my $now = strftime( "%s", localtime ); # Need to launch cron. return 1 if $now - $lastupdated >= 2592000; @@ -57,13 +56,19 @@ sub NeedUpdate { sub BuildReport { my $report = { library => { + id => C4::Context->preference('UsageStatsID') || 0, name => C4::Context->preference('UsageStatsLibraryName') || q||, - id => C4::Context->preference('UsageStatsID') || 0, + url => C4::Context->preference('UsageStatsLibraryUrl') || q||, + type => C4::Context->preference('UsageStatsLibraryType') || q||, + country => C4::Context->preference('UsageStatsCountry') || q||, }, }; # Get database volumetry. - foreach (qw/biblio auth_header old_issues old_reserves borrowers aqorders subscription/) { + foreach ( + qw/biblio auth_header old_issues old_reserves borrowers aqorders subscription/ + ) + { $report->{volumetry}{$_} = _count($_); } @@ -338,18 +343,19 @@ Send to hea.koha-community.org database informations =cut sub ReportToCommunity { - my $data = shift; - my $json = uri_encode( to_json($data), 1 ); + my $data = shift; + my $json = encode_json($data); - my $ua = LWP::UserAgent->new; - my $req = - HTTP::Request->new( POST => "http://hea.koha-community.org/upload.pl" ); - $req->content_type('application/x-www-form-urlencoded'); - $req->content("data=$json"); - my $res = $ua->request($req); - my $content = from_json( $res->decoded_content ); - C4::Context->set_preference( 'UsageStatsID', - $content->{library}{library_id} ); + my $url = "http://hea.koha-community.org/upload.pl"; + my $ua = LWP::UserAgent->new; + my $res = $ua->post( + $url, + 'Content-type' => 'application/json;charset=utf-8', + Content => $json, + ); + my $content = decode_json( $res->decoded_content ); + C4::Context->set_preference( 'UsageStatsID', + $content->{library}{id} ); } =head2 _count @@ -361,12 +367,12 @@ Count the number of records in $table tables =cut sub _count { - my $table = shift; + my $table = shift; - my $dbh = C4::Context->dbh; - my $sth = $dbh->prepare("SELECT count(*) from $table"); - $sth->execute; - return $sth->fetchrow_array; + my $dbh = C4::Context->dbh; + my $sth = $dbh->prepare("SELECT count(*) from $table"); + $sth->execute; + return $sth->fetchrow_array; } 1; diff --git a/installer/data/mysql/sysprefs.sql b/installer/data/mysql/sysprefs.sql index 77a0c7f..a96f73b 100644 --- a/installer/data/mysql/sysprefs.sql +++ b/installer/data/mysql/sysprefs.sql @@ -419,9 +419,12 @@ INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` ('UpdateTotalIssuesOnCirc','0',NULL,'Whether to update the totalissues field in the biblio on each circ.','YesNo'), ('uppercasesurnames','0',NULL,'If ON, surnames are converted to upper case in patron entry form','YesNo'), ('URLLinkText','',NULL,'Text to display as the link anchor in the OPAC','free'), +('UsageStatsCountry', '', 'The country where your library is to show on hea Koha community website', NULL, 'YesNo'), ('UsageStatsID', '', 'This pref is part of Koha but it should not be deleted or updated manually.', '', 'Free'), ('UsageStatsLastUpdateTime', '', 'This pref is part of Koha but it should not be deleted or updated manually.', '', 'Free'), -('UsageStatsLibraryName', '', 'The library name to show on hea Koha community website', NULL, 'YesNo'), +('UsageStatsLibraryName', '', 'The library name to show on hea Koha community website', NULL, 'Free'), +('UsageStatsLibraryType', 'public', 'public|university', 'The library type to show on hea Koha community website', NULL, 'Choice'), +('UsageStatsLibraryUrl', '', 'The library url to show on hea Koha community website', NULL, 'Free'), ('UsageStatsShare', 0, 'Share data volumetry with Koha community (HEA).', NULL, 'YesNo'), ('UseAuthoritiesForTracings','1','0','Use authority record numbers for subject tracings instead of heading strings.','YesNo'), ('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'), diff --git a/installer/data/mysql/updatedatabase.pl b/installer/data/mysql/updatedatabase.pl index 0e6124d..762042f 100755 --- a/installer/data/mysql/updatedatabase.pl +++ b/installer/data/mysql/updatedatabase.pl @@ -8707,16 +8707,20 @@ if ( CheckVersion($DBversion) ) { $DBversion = "3.17.00.XXX"; if ( CheckVersion($DBversion) ) { - $dbh->do(q| - INSERT INTO systempreferences (variable, value, explanation, options, type ) - VALUES - ('UsageStatsID', '', 'This pref is part of Koha but it should not be deleted or updated manually.', '', 'Free'), - ('UsageStatsLastUpdateTime', '', 'This pref is part of Koha but it should not be deleted or updated manually.', '', 'Free'), - ('UsageStatsLibraryName', '', 'The library name to show on hea Koha community website', NULL, 'YesNo'), - ('UsageStatsShare', 0, 'Share data volumetry with Koha community (HEA).', NULL, 'YesNo') - |); - print "Upgrade to $DBversion done (Bug 11926: Add UsageStats systempreferences (HEA))\n"; - SetVersion ($DBversion); + $dbh->do( + q{ + INSERT INTO systempreferences (variable, value, options, explanation, type ) + VALUES + ('UsageStatsCountry', '', NULL, 'The country where your library is to show on hea Koha community website', 'YesNo'), + ('UsageStatsID', '', NULL, 'This pref is part of Koha but it should not be deleted or updated manually.', 'Free'), + ('UsageStatsLastUpdateTime', '', NULL, 'This pref is part of Koha but it should not be deleted or updated manually.', 'Free'), + ('UsageStatsLibraryName', '', NULL, 'The library name to show on hea Koha community website', 'Free'), + ('UsageStatsLibraryType', 'public', 'public|university', 'The library type to show on hea Koha community website', 'Choice'), + ('UsageStatsLibraryUrl', '', NULL, 'The library url to show on hea Koha community website', 'Free'), + ('UsageStatsShare', 0, NULL, 'Share data volumetry with Koha community (HEA).', 'YesNo') + }); + print "Upgrade to $DBversion done (Bug 11926: Add UsageStats systempreferences (HEA))\n"; + SetVersion($DBversion); } =head1 FUNCTIONS diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/admin.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/admin.pref index 1c15ac0..d1920a7 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/admin.pref +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/admin.pref @@ -127,3 +127,23 @@ Administration: - will be shown on Hea Koha community website. - If this name is empty, you will sent data anonymously - Note that this value has no effect if UsageStatsShare system preference is set to "Don't share" + - + - The country where your library is + - pref: UsageStatsCountry + - will be shown on Hea Koha community website. + - Note that this value has no effect if UsageStatsShare system preference is set to "Don't share" + - + - The library url + - pref: UsageStatsLibraryUrl + - will be shown on Hea Koha community website. + - Note that this value has no effect if UsageStatsShare system preference is set to "Don't share" + + - + - The library type + - pref: UsageStatsLibraryType + default: public + choices: + public: "public" + university: "university" + - will be shown on Hea Koha community website. + - Note that this value has no effect if UsageStatsShare system preference is set to "Don't share" diff --git a/misc/cronjobs/share_usage_with_koha_community.pl b/misc/cronjobs/share_usage_with_koha_community.pl index acc7fd5..b55edcc 100644 --- a/misc/cronjobs/share_usage_with_koha_community.pl +++ b/misc/cronjobs/share_usage_with_koha_community.pl @@ -7,31 +7,39 @@ use Getopt::Long; use C4::Context; use C4::UsageStats; +use POSIX qw(strftime); - -my ( $help, $verbose ); +my ( $help, $verbose, $force ); GetOptions( - 'h|help' => \$help, - 'v|verbose' => \$verbose, + 'h|help' => \$help, + 'v|verbose' => \$verbose, + 'f|force' => \$force, ) || pod2usage(1); if ($help) { pod2usage(1); } -unless( C4::Context->preference('UsageStatsShare') ) { - 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|); - exit 1; +unless ( C4::Context->preference('UsageStatsShare') ) { + 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 +| + ); + exit 1; } -my $need_update = C4::UsageStats::NeedUpdate(); +my $need_update = ($force ? 1 : C4::UsageStats::NeedUpdate() ); -if ( $need_update ) { +if ($need_update) { say "Data need to be updated" if $verbose; my $report = C4::UsageStats::BuildReport(); C4::UsageStats::ReportToCommunity($report); - C4::Context->set_preference('UsageStatsLastUpdateTime', strftime("%s", localtime)); -} elsif( $verbose ) { + C4::Context->set_preference( 'UsageStatsLastUpdateTime', + strftime( "%s", localtime ) ); +} +elsif ($verbose) { say "Data don't need to be updated"; } @@ -53,7 +61,7 @@ Only the total number is retrieved. In no case private data will be shared! In order to know which parts of Koha modules are used, this script will collect some system preference values. -If you want to tell us who you are, you can fill the UsageStatsLibraryName system preference with your library name. +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. All these data will be analysed on the http://hea.koha-community.org Koha community website. @@ -72,6 +80,10 @@ Print a brief help message Verbose mode. +=item B<-f|--force> + +Force the update. + =back =head1 AUTHOR -- 2.1.0