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
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.
24 use POSIX qw(strftime);
27 use URI::Encode qw(uri_encode);
29 =head1 NAME C4::UsageStats
33 This package contains what is needed to report Koha statistics to hea
34 hea.koha-community.org is the server that centralize Koha setups informations
35 Koha libraries are encouraged to provide informations about their collections,
38 This package is normally only called by a cronjob, like
39 0 3 1 * * export KOHA_CONF=/home/koha/etc/koha-conf.xml; export PERL5LIB=/home/koha/src; perl /home/koha/src/C4/UsageStats.pm
41 IMPORTANT : please do NOT run the cron on the 1st, but on another day. The idea is to avoid all
42 Koha libraries sending their data at the same time ! So choose any day between 1 and 28 !
47 $needUpdateYN = C4::UsageStats::NeedUpdate;
49 Returns Y (1) if the last update is more than 1 month old
50 This way, even if the cronjob is run every minute, the webservice will be called
56 my $lastupdated = C4::Context->preference('UsageStatsLastUpdateTime') || 0;
57 my $now = strftime("%s", localtime);
59 # Need to launch cron.
60 return 1 if $now - $lastupdated >= 2592000;
62 # Cron no need to be launched.
70 Compute results and send them to the centralized server
75 if (!C4::Context->preference('UsageStatsShare')) {
76 die ("UsageStats is not configured");
79 C4::Context->set_preference('UsageStatsLastUpdateTime', strftime("%s", localtime));
80 my $data = BuildReport();
81 ReportToCommunity($data);
89 retrieve some database volumety and systempreferences that will be sent to hea server
96 'name' => C4::Context->preference('UsageStatsLibraryName'),
97 'id' => C4::Context->preference('UsageStatsID') || 0,
101 # Get database volumetry.
102 foreach (qw/biblio auth_header old_issues old_reserves borrowers aqorders subscription/) {
103 $report->{volumetry}{$_} = _count($_);
106 # Get systempreferences.
107 foreach (qw/ AcqCreateItem
108 AcqWarnOnDuplicateInvoice
124 AutoCreateAuthorities
125 BiblioAddsAuthorities
127 UseAuthoritiesForTracings
130 IntranetBiblioDefaultView
136 DefaultClassificationSource
137 EasyAnalyticalRecords
144 SpineLabelShowPrintOnBibDetails
145 BlockReturnOfWithdrawnItems
146 CalculateFinesOnReturn
147 AgeRestrictionOverride
150 AllowItemsOnHoldCheckout
151 AllowNotForLoanOverride
152 AllowRenewalLimitOverride
156 AutoRemoveOverduesRestrictions
159 HomeOrHoldingBranchReturn
160 InProcessingToShelvingCart
163 ManInvInNoissuesCharge
167 RentalsInNoissuesCharge
170 TransfersMaxDaysWarning
171 UseBranchTransferLimits
173 UseTransportCostMatrix
176 FinesIncludeGracePeriod
178 RefundLostItemFeeOnReturn
179 WhenLostChargeReplacementFee
181 AllowHoldDateInFuture
182 AllowHoldPolicyOverride
183 AllowHoldsOnDamagedItems
184 AllowHoldsOnPatronsPossessions
186 AutoResumeSuspendedHolds
187 canreservefromotherbranches
188 decreaseLoanHighHolds
189 DisplayMultiPlaceHold
190 emailLibrarianWhenHoldIsPlaced
191 ExpireReservesMaxPickUpDelay
192 OPACAllowHoldDateInFuture
193 OPACAllowUserToChooseBranch
194 ReservesControlBranch
198 TransferWhenCancelAllWaitingHolds
199 AllowAllMessageDeletion
200 AllowOfflineCirculation
202 CircAutoPrintQuickSlip
203 DisplayClearScreenButton
204 FilterBeforeOverdueReport
206 itemBarcodeFallbackSearch
207 itemBarcodeInputFilter
208 previousIssuesDefaultSortOrder
209 RecordLocalUseOnReturn
212 todaysIssuesDefaultSortOrder
213 UpdateTotalIssuesOnCirc
215 WaitingNotifyAtCheckin
216 AllowSelfCheckReturns
221 OPACAmazonCoverImages
226 IDreamBooksReadometer
229 LibraryThingForLibrariesEnabled
232 NovelistSelectEnabled
238 CalendarFirstDayOfWeek
248 AuthorisedValueImages
253 HighlightOwnItemsOnOPAC
254 OpacAddMastheadLibraryPulldown
255 OPACDisplay856uAsImage
263 OpacShowFiltersPulldownMobile
264 OPACShowHoldQueueDetails
265 OpacShowLibrariesPulldownMobile
266 OpacShowRecentComments
267 OPACShowUnusedAuthorities
270 OPACURLOpenInNewWindow
282 OPACPopupAuthorsSearch
292 AllowPurchaseSuggestionBranchChoice
293 OpacAllowPublicListCreation
294 OpacAllowSharingPrivateLists
298 OPACViewOthersSuggestions
302 EnableOpacSearchHistory
306 PatronSelfRegistration
310 AutoEmailPrimaryAddress
312 BorrowerRenewalPeriodBase
315 EnhancedMessagingPreferences
316 ExtendedPatronAttributes
317 intranetreadinghistory
320 TalkingTechItivaPhoneNotification
322 IncludeSeeFromInSearches
328 TraceCompleteSubfields
329 TraceSubjectSubdivisions
335 OPACItemsResultsDisplay
337 IntranetNumbersPreferPhrase
338 OPACNumbersPreferPhrase
340 RenewSerialAddsSuggestion
341 RoutingListAddReserves
346 StaffAuthorisedValueImages
351 StaffDetailItemSelection
358 $report->{systempreferences}{$_} = C4::Context->preference($_);
363 =head2 ReportToCommunity
367 Send to hea.koha-community.org database informations
371 sub ReportToCommunity {
373 my $json = uri_encode(to_json($data), 1);
375 my $ua = LWP::UserAgent->new;
376 my $req = HTTP::Request->new(POST => "http://hea.koha-community.org/upload.pl");
377 $req->content_type('application/x-www-form-urlencoded');
378 $req->content("data=$json");
379 my $res = $ua->request($req);
380 my $content = from_json($res->decoded_content);
381 C4::Context->set_preference('UsageStatsID', $content->{library}{library_id});
386 $data = _count($table);
388 Count the number of records in $table tables
395 my $dbh = C4::Context->dbh;
396 my $sth = $dbh->prepare("SELECT count(*) from $table");
398 return $sth->fetchrow_array;