From 7c63d42b541d3c5e5c1c3a3f04bce8fe28f0f3c7 Mon Sep 17 00:00:00 2001 From: Olli-Antti Kivilahti Date: Fri, 17 Oct 2014 17:23:21 +0300 Subject: [PATCH] Bug 13106 - Encapsulate Circulation::GetAgeRestriction() and modify it to check borrowers age as well. This patch moves the logic of deciding whether or not a borrower is old enough to access this material to its own function GetAgeRestriction. This makes it easier to use AgeRestriction elsewhere, like with placing holds. Unit tests included. --- C4/Circulation.pm | 65 ++++++++++++++++++++------------- t/Circulation/AgeRestrictionMarkers.t | 22 ++++++++++- 2 files changed, 61 insertions(+), 26 deletions(-) diff --git a/C4/Circulation.pm b/C4/Circulation.pm index 6a8c836..eebb1b7 100644 --- a/C4/Circulation.pm +++ b/C4/Circulation.pm @@ -983,29 +983,14 @@ sub CanBookBeIssued { } ## CHECK AGE RESTRICTION - # get $marker from preferences. Could be something like "FSK|PEGI|Alter|Age:" - my $markers = C4::Context->preference('AgeRestrictionMarker'); - my $bibvalues = $biblioitem->{'agerestriction'}; - my $restriction_age = GetAgeRestriction( $bibvalues ); - - if ( $restriction_age > 0 ) { - if ( $borrower->{'dateofbirth'} ) { - my @alloweddate = split /-/, $borrower->{'dateofbirth'}; - $alloweddate[0] += $restriction_age; - - #Prevent runime eror on leap year (invalid date) - if ( ( $alloweddate[1] == 2 ) && ( $alloweddate[2] == 29 ) ) { - $alloweddate[2] = 28; - } - - if ( Date_to_Days(Today) < Date_to_Days(@alloweddate) - 1 ) { - if ( C4::Context->preference('AgeRestrictionOverride') ) { - $needsconfirmation{AGE_RESTRICTION} = "$bibvalues"; - } - else { - $issuingimpossible{AGE_RESTRICTION} = "$bibvalues"; - } - } + my $agerestriction = $biblioitem->{'agerestriction'}; + my ($restriction_age, $daysToAgeRestriction) = GetAgeRestriction( $agerestriction, $borrower ); + if ( $daysToAgeRestriction ) { + if ( C4::Context->preference('AgeRestrictionOverride') ) { + $needsconfirmation{AGE_RESTRICTION} = "$agerestriction"; + } + else { + $issuingimpossible{AGE_RESTRICTION} = "$agerestriction"; } } @@ -3754,8 +3739,20 @@ sub IsItemIssued { return $sth->fetchrow; } +=head2 GetAgeRestriction + + my ($ageRestriction, $daysToAgeRestriction) = GetAgeRestriction($record_restrictions, $borrower); + my ($ageRestriction, $daysToAgeRestriction) = GetAgeRestriction($record_restrictions); + +@PARAM1 the koha.biblioitems.agerestriction value, like K18, PEGI 13, ... +@PARAM2 a borrower-object with koha.borrowers.dateofbirth. (OPTIONAL) +@RETURNS The age restriction age in years and the days to fulfill the age restriction for the given borrower. + Negative days mean the borrower has gone past the age restriction age. + +=cut + sub GetAgeRestriction { - my ($record_restrictions) = @_; + my ($record_restrictions, $borrower) = @_; my $markers = C4::Context->preference('AgeRestrictionMarker'); # Split $record_restrictions to something like FSK 16 or PEGI 6 @@ -3789,7 +3786,25 @@ sub GetAgeRestriction { last if ( $restriction_year > 0 ); } - return $restriction_year; + #Check if the borrower is age restricted for this material and for how long. + if ($restriction_year && $borrower) { + if ( $borrower->{'dateofbirth'} ) { + my @alloweddate = split /-/, $borrower->{'dateofbirth'}; + $alloweddate[0] += $restriction_year; + + #Prevent runime eror on leap year (invalid date) + if ( ( $alloweddate[1] == 2 ) && ( $alloweddate[2] == 29 ) ) { + $alloweddate[2] = 28; + } + + #Get how many days the borrower has to reach the age restriction + my $daysToAgeRestriction = Date_to_Days(Today) - Date_to_Days(@alloweddate); + #Negative days means the borrower went past the age restriction age + return ($restriction_year, $daysToAgeRestriction); + } + } + + return ($restriction_year); } 1; diff --git a/t/Circulation/AgeRestrictionMarkers.t b/t/Circulation/AgeRestrictionMarkers.t index 619e4ea..589c386 100644 --- a/t/Circulation/AgeRestrictionMarkers.t +++ b/t/Circulation/AgeRestrictionMarkers.t @@ -1,5 +1,8 @@ +#!/usr/bin/perl + use Modern::Perl; -use Test::More tests => 5; +use DateTime; +use Test::More tests => 10; use t::lib::Mocks; @@ -13,3 +16,20 @@ is ( C4::Circulation::GetAgeRestriction('PEGI16'), '16', 'PEGI16 returns 16' ); is ( C4::Circulation::GetAgeRestriction('Age 16'), '16', 'Age 16 returns 16' ); is ( C4::Circulation::GetAgeRestriction('K16'), '16', 'K16 returns 16' ); + +##Testing age restriction for a borrower. +my $now = DateTime->now(); +my $duration_15years = DateTime::Duration->new(years => 15); +my $past15yearsAgo = DateTime->now()->subtract_duration($duration_15years); +my $borrower = {dateofbirth => $past15yearsAgo->ymd()}; + +my ($restriction_age, $daysToAgeRestriction) = C4::Circulation::GetAgeRestriction('FSK 16', $borrower); +is ( ($daysToAgeRestriction < 0), 1, 'FSK 16 blocked for a 15 year old' ); +($restriction_age, $daysToAgeRestriction) = C4::Circulation::GetAgeRestriction('PEGI 15', $borrower); +is ( ($daysToAgeRestriction == 0), 1, 'PEGI 15 allowed for a 15 year old' ); +($restriction_age, $daysToAgeRestriction) = C4::Circulation::GetAgeRestriction('PEGI14', $borrower); +is ( ($daysToAgeRestriction > 0), 1, 'PEGI14 allowed for a 15 year old' ); +($restriction_age, $daysToAgeRestriction) = C4::Circulation::GetAgeRestriction('Age 10', $borrower); +is ( ($daysToAgeRestriction > 0), 1, 'Age 10 allowed for a 15 year old' ); +($restriction_age, $daysToAgeRestriction) = C4::Circulation::GetAgeRestriction('K18', $borrower); +is ( ($daysToAgeRestriction < 0), 1, 'K18 blocked for a 15 year old' ); \ No newline at end of file -- 1.7.9.5