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

(-)a/C4/Circulation.pm (-32 / +6 lines)
Lines 1266-1274 sub CanBookBeIssued { Link Here
1266
1266
1267
    ## CHECK AGE RESTRICTION
1267
    ## CHECK AGE RESTRICTION
1268
    my $agerestriction = $biblioitem->agerestriction;
1268
    my $agerestriction = $biblioitem->agerestriction;
1269
    my ( $restriction_age, $daysToAgeRestriction ) =
1269
    my $restriction_age = GetAgeRestriction( $agerestriction );
1270
      GetAgeRestriction( $agerestriction, $patron->unblessed );
1270
    if ( $restriction_age && $patron->dateofbirth && $restriction_age > $patron->get_age() ) {
1271
    if ( $daysToAgeRestriction && $daysToAgeRestriction > 0 ) {
1272
        if ( C4::Context->preference('AgeRestrictionOverride') ) {
1271
        if ( C4::Context->preference('AgeRestrictionOverride') ) {
1273
            $needsconfirmation{AGE_RESTRICTION} = "$agerestriction";
1272
            $needsconfirmation{AGE_RESTRICTION} = "$agerestriction";
1274
        }
1273
        }
Lines 4421-4441 sub IsItemIssued { Link Here
4421
4420
4422
=head2 GetAgeRestriction
4421
=head2 GetAgeRestriction
4423
4422
4424
  my ($ageRestriction, $daysToAgeRestriction) = GetAgeRestriction($record_restrictions, $borrower);
4423
  my $ageRestriction = GetAgeRestriction($record_restrictions);
4425
  my ($ageRestriction, $daysToAgeRestriction) = GetAgeRestriction($record_restrictions);
4426
4427
  if($daysToAgeRestriction <= 0) { #Borrower is allowed to access this material, as they are older or as old as the agerestriction }
4428
  if($daysToAgeRestriction > 0) { #Borrower is this many days from meeting the agerestriction }
4429
4424
4430
@PARAM1 the koha.biblioitems.agerestriction value, like K18, PEGI 13, ...
4425
@PARAM1 the koha.biblioitems.agerestriction value, like K18, PEGI 13, ...
4431
@PARAM2 a borrower-object with koha.borrowers.dateofbirth. (OPTIONAL)
4426
@RETURNS The age restriction age in years.
4432
@RETURNS The age restriction age in years and the days to fulfill the age restriction for the given borrower.
4433
         Negative days mean the borrower has gone past the age restriction age.
4434
4427
4435
=cut
4428
=cut
4436
4429
4437
sub GetAgeRestriction {
4430
sub GetAgeRestriction {
4438
    my ($record_restrictions, $borrower) = @_;
4431
    my ($record_restrictions) = @_;
4439
    my $markers = C4::Context->preference('AgeRestrictionMarker');
4432
    my $markers = C4::Context->preference('AgeRestrictionMarker');
4440
4433
4441
    return unless $record_restrictions;
4434
    return unless $record_restrictions;
Lines 4470-4495 sub GetAgeRestriction { Link Here
4470
        last if ( $restriction_year > 0 );
4463
        last if ( $restriction_year > 0 );
4471
    }
4464
    }
4472
4465
4473
    #Check if the borrower is age restricted for this material and for how long.
4466
    return $restriction_year;
4474
    if ($restriction_year && $borrower) {
4475
        if ( $borrower->{'dateofbirth'} ) {
4476
            my @alloweddate = split /-/, $borrower->{'dateofbirth'};
4477
            $alloweddate[0] += $restriction_year;
4478
4479
            #Prevent runime eror on leap year (invalid date)
4480
            if ( ( $alloweddate[1] == 2 ) && ( $alloweddate[2] == 29 ) ) {
4481
                $alloweddate[2] = 28;
4482
            }
4483
4484
            #Get how many days the borrower has to reach the age restriction
4485
            my @Today = split /-/, dt_from_string()->ymd();
4486
            my $daysToAgeRestriction = Date_to_Days(@alloweddate) - Date_to_Days(@Today);
4487
            #Negative days means the borrower went past the age restriction age
4488
            return ($restriction_year, $daysToAgeRestriction);
4489
        }
4490
    }
4491
4492
    return ($restriction_year);
4493
}
4467
}
4494
4468
4495
4469
(-)a/C4/Reserves.pm (-3 / +2 lines)
Lines 497-505 sub CanItemBeReserved { Link Here
497
    if( GetMarcFromKohaField('biblioitems.agerestriction') ){
497
    if( GetMarcFromKohaField('biblioitems.agerestriction') ){
498
        my $biblio = $item->biblio;
498
        my $biblio = $item->biblio;
499
        # Check for the age restriction
499
        # Check for the age restriction
500
        my ( $ageRestriction, $daysToAgeRestriction ) =
500
        my $ageRestriction = C4::Circulation::GetAgeRestriction( $biblio->biblioitem->agerestriction );
501
          C4::Circulation::GetAgeRestriction( $biblio->biblioitem->agerestriction, $borrower );
501
        return _cache { status => 'ageRestricted' } if $ageRestriction && $patron->dateofbirth && $ageRestriction > $patron->get_age();
502
        return _cache { status => 'ageRestricted' } if $daysToAgeRestriction && $daysToAgeRestriction > 0;
503
    }
502
    }
504
503
505
    # Check that the patron doesn't have an item level hold on this item already
504
    # Check that the patron doesn't have an item level hold on this item already
(-)a/t/Circulation/AgeRestrictionMarkers.t (-47 / +1 lines)
Lines 22-28 use Modern::Perl; Link Here
22
22
23
use DateTime;
23
use DateTime;
24
use Koha::DateUtils qw( dt_from_string );
24
use Koha::DateUtils qw( dt_from_string );
25
use Test::More tests => 8;
25
use Test::More tests => 6;
26
use Test::Warn;
26
use Test::Warn;
27
27
28
use t::lib::Mocks;
28
use t::lib::Mocks;
Lines 37-71 is ( C4::Circulation::GetAgeRestriction('PEGI16'), '16', 'PEGI16 returns 16' ); Link Here
37
is ( C4::Circulation::GetAgeRestriction('Age 16'), '16', 'Age 16 returns 16' );
37
is ( C4::Circulation::GetAgeRestriction('Age 16'), '16', 'Age 16 returns 16' );
38
is ( C4::Circulation::GetAgeRestriction('K16'), '16', 'K16 returns 16' );
38
is ( C4::Circulation::GetAgeRestriction('K16'), '16', 'K16 returns 16' );
39
39
40
subtest 'Patron tests - 15 years old' => sub {
41
    plan tests => 5;
42
    ##Testing age restriction for a borrower.
43
    my $now = dt_from_string();
44
    my $borrower = { dateofbirth => $now->add( years => -15 )->strftime("%Y-%m-%d") };
45
    TestPatron($borrower,0);
46
};
47
48
subtest 'Patron tests - 15 years old (Time Zone shifts)' => sub {
49
    my $CheckTimeFake = eval { require Time::Fake; 1; } || 0;
50
    SKIP: {
51
        skip "Install Time::Fake to regression test for Bug 14362.", 115 if $CheckTimeFake!=1;
52
        # 115 regression tests = 5 tests (see TestPatron) for 23 timezones.
53
        plan tests => 115;
54
        my $offset = 1;
55
        # <24 hours in a day.
56
        while ($offset<24) {
57
            Time::Fake->offset("+${offset}h");
58
59
            ##Testing age restriction for a borrower.
60
            my $now = dt_from_string();
61
            my $borrower = { dateofbirth => $now->add( years => -15 )->strftime("%Y-%m-%d") };
62
            TestPatron($borrower,$offset);
63
64
            $offset++;
65
        }
66
    }
67
};
68
69
subtest 'No age restriction' => sub {
40
subtest 'No age restriction' => sub {
70
    plan tests => 1;
41
    plan tests => 1;
71
42
Lines 76-94 subtest 'No age restriction' => sub { Link Here
76
47
77
};
48
};
78
49
79
# The Patron tests
80
sub TestPatron {
81
    my ($borrower,$offset) = @_;
82
83
    my ($restriction_age, $daysToAgeRestriction) = C4::Circulation::GetAgeRestriction('FSK 16', $borrower);
84
    is ( ($daysToAgeRestriction > 0), 1, "FSK 16 blocked for a 15 year old - $offset hours" );
85
    ($restriction_age, $daysToAgeRestriction) = C4::Circulation::GetAgeRestriction('PEGI 15', $borrower);
86
    is ( ($daysToAgeRestriction <= 0), 1, "PEGI 15 allowed for a 15 year old - $offset hours" );
87
    ($restriction_age, $daysToAgeRestriction) = C4::Circulation::GetAgeRestriction('PEGI14', $borrower);
88
    is ( ($daysToAgeRestriction <= 0), 1, "PEGI14 allowed for a 15 year old - $offset hours" );
89
    ($restriction_age, $daysToAgeRestriction) = C4::Circulation::GetAgeRestriction('Age 10', $borrower);
90
    is ( ($daysToAgeRestriction <= 0), 1, "Age 10 allowed for a 15 year old - $offset hours" );
91
    ($restriction_age, $daysToAgeRestriction) = C4::Circulation::GetAgeRestriction('K18', $borrower);
92
    is ( ($daysToAgeRestriction > 0), 1, "K18 blocked for a 15 year old - $offset hours" );
93
    return;
94
}
95
- 

Return to bug 37155