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);
28 =head1 NAME C4::UsageStats
32 This package contains what is needed to report Koha statistics to hea
33 hea.koha-community.org is the server that centralize Koha setups informations
34 Koha libraries are encouraged to provide informations about their collections,
37 This package is normally only called by a cronjob, like
38 0 3 1 * * export KOHA_CONF=/home/koha/etc/koha-conf.xml; export PERL5LIB=/home/koha/src; perl /home/koha/src/C4/UsageStats.pm
40 IMPORTANT : please do NOT run the cron on the 1st, but on another day. The idea is to avoid all
41 Koha libraries sending their data at the same time ! So choose any day between 1 and 28 !
46 $needUpdateYN = C4::UsageStats::NeedUpdate;
48 Returns Y (1) if the last update is more than 1 month old
49 This way, even if the cronjob is run every minute, the webservice will be called
55 my $lastupdated = C4::Context->preference('UsageStatsLastUpdateTime') || 0;
56 my $now = strftime("%s", localtime);
58 # Need to launch cron.
59 return 1 if $now - $lastupdated >= 2592000;
61 # Cron no need to be launched.
69 Compute results and send them to the centralized server
74 if (!C4::Context->preference('UsageStatsShare')) {
75 die ("UsageStats is not configured");
78 C4::Context->set_preference('UsageStatsLastUpdateTime', strftime("%s", localtime));
79 my $data = BuildReport();
80 ReportToCommunity($data);
88 retrieve some database volumety and systempreferences that will be sent to hea server
95 'name' => C4::Context->preference('UsageStatsLibraryName'),
96 'id' => C4::Context->preference('UsageStatsID') || 0,
100 # Get database volumetry.
101 foreach (qw/biblio auth_header old_issues old_reserves borrowers aqorders subscription/) {
102 $report->{volumetry}{$_} = _count($_);
105 # Get systempreferences.
106 foreach (qw/ AcqCreateItem
107 AcqWarnOnDuplicateInvoice
123 AutoCreateAuthorities
124 BiblioAddsAuthorities
126 UseAuthoritiesForTracings
129 IntranetBiblioDefaultView
135 DefaultClassificationSource
136 EasyAnalyticalRecords
143 SpineLabelShowPrintOnBibDetails
144 BlockReturnOfWithdrawnItems
145 CalculateFinesOnReturn
146 AgeRestrictionOverride
149 AllowItemsOnHoldCheckout
150 AllowNotForLoanOverride
151 AllowRenewalLimitOverride
155 AutoRemoveOverduesRestrictions
158 HomeOrHoldingBranchReturn
159 InProcessingToShelvingCart
162 ManInvInNoissuesCharge
166 RentalsInNoissuesCharge
169 TransfersMaxDaysWarning
170 UseBranchTransferLimits
172 UseTransportCostMatrix
175 FinesIncludeGracePeriod
177 RefundLostItemFeeOnReturn
178 WhenLostChargeReplacementFee
180 AllowHoldDateInFuture
181 AllowHoldPolicyOverride
182 AllowHoldsOnDamagedItems
183 AllowHoldsOnPatronsPossessions
185 AutoResumeSuspendedHolds
186 canreservefromotherbranches
187 decreaseLoanHighHolds
188 DisplayMultiPlaceHold
189 emailLibrarianWhenHoldIsPlaced
190 ExpireReservesMaxPickUpDelay
191 OPACAllowHoldDateInFuture
192 OPACAllowUserToChooseBranch
193 ReservesControlBranch
197 TransferWhenCancelAllWaitingHolds
198 AllowAllMessageDeletion
199 AllowOfflineCirculation
201 CircAutoPrintQuickSlip
202 DisplayClearScreenButton
203 FilterBeforeOverdueReport
205 itemBarcodeFallbackSearch
206 itemBarcodeInputFilter
207 previousIssuesDefaultSortOrder
208 RecordLocalUseOnReturn
211 todaysIssuesDefaultSortOrder
212 UpdateTotalIssuesOnCirc
214 WaitingNotifyAtCheckin
215 AllowSelfCheckReturns
220 OPACAmazonCoverImages
225 IDreamBooksReadometer
228 LibraryThingForLibrariesEnabled
231 NovelistSelectEnabled
237 CalendarFirstDayOfWeek
247 AuthorisedValueImages
252 HighlightOwnItemsOnOPAC
253 OpacAddMastheadLibraryPulldown
254 OPACDisplay856uAsImage
262 OpacShowFiltersPulldownMobile
263 OPACShowHoldQueueDetails
264 OpacShowLibrariesPulldownMobile
265 OpacShowRecentComments
266 OPACShowUnusedAuthorities
269 OPACURLOpenInNewWindow
281 OPACPopupAuthorsSearch
291 AllowPurchaseSuggestionBranchChoice
292 OpacAllowPublicListCreation
293 OpacAllowSharingPrivateLists
297 OPACViewOthersSuggestions
301 EnableOpacSearchHistory
305 PatronSelfRegistration
309 AutoEmailPrimaryAddress
311 BorrowerRenewalPeriodBase
314 EnhancedMessagingPreferences
315 ExtendedPatronAttributes
316 intranetreadinghistory
319 TalkingTechItivaPhoneNotification
321 IncludeSeeFromInSearches
327 TraceCompleteSubfields
328 TraceSubjectSubdivisions
334 OPACItemsResultsDisplay
336 IntranetNumbersPreferPhrase
337 OPACNumbersPreferPhrase
339 RenewSerialAddsSuggestion
340 RoutingListAddReserves
345 StaffAuthorisedValueImages
350 StaffDetailItemSelection
357 $report->{systempreferences}{$_} = C4::Context->preference($_);
362 =head2 ReportToCommunity
366 Send to hea.koha-community.org database informations
370 sub ReportToCommunity {
372 my $json = to_json($data);
374 my $ua = LWP::UserAgent->new;
375 my $req = HTTP::Request->new(POST => "http://hea.koha-community.org/upload.pl");
376 $req->content_type('application/x-www-form-urlencoded');
377 $req->content("data=$json");
378 my $res = $ua->request($req);
379 my $content = from_json($res->decoded_content);
380 C4::Context->set_preference('UsageStatsID', $content->{library}{library_id});
385 $data = _count($table);
387 Count the number of records in $table tables
394 my $dbh = C4::Context->dbh;
395 my $sth = $dbh->prepare("SELECT count(*) from $table");
397 return $sth->fetchrow_array;