Bugzilla – Attachment 57221 Details for
Bug 17557
Move GetAge to Koha::Patron->get_age (and remove SetAge)
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
[SIGNED-OFF] Bug 17557: Koha::Patrons - Move GetAge to ->set_age (and remove SetAge)
SIGNED-OFF-Bug-17557-KohaPatrons---Move-GetAge-to-.patch (text/plain), 7.91 KB, created by
Josef Moravec
on 2016-11-04 17:21:02 UTC
(
hide
)
Description:
[SIGNED-OFF] Bug 17557: Koha::Patrons - Move GetAge to ->set_age (and remove SetAge)
Filename:
MIME Type:
Creator:
Josef Moravec
Created:
2016-11-04 17:21:02 UTC
Size:
7.91 KB
patch
obsolete
>From ca8af968f73e59fdd4fcd000d45604133eb89682 Mon Sep 17 00:00:00 2001 >From: Jonathan Druart <jonathan.druart@bugs.koha-community.org> >Date: Fri, 4 Nov 2016 16:21:03 +0000 >Subject: [PATCH] [SIGNED-OFF] Bug 17557: Koha::Patrons - Move GetAge to > ->set_age (and remove SetAge) > >As said in the previous commit, I considered SetAge as unnecessary and >removed it. > >Test plan: >1/ Edit a patron using the different 'Edit' links >2/ Play with the patron category limited to age ranges, and date of >birth >3/ You should get the expected warning if the date of birth is inside >the patron category date range. > >To finish: > prove t/Circulation/AgeRestrictionMarkers.t t/db_dependent/Reserves.t \ > t/db_dependent/Koha/Patrons.t t/db_dependent/Members.t >should return green > >Signed-off-by: Josef Moravec <josef.moravec@gmail.com> >--- > C4/Members.pm | 71 ----------------------------------- > Koha/Patron.pm | 24 ++++++++++++ > members/memberentry.pl | 3 +- > members/moremember.pl | 4 +- > t/Circulation/AgeRestrictionMarkers.t | 6 +-- > t/db_dependent/Reserves.t | 5 +-- > 6 files changed, 31 insertions(+), 82 deletions(-) > >diff --git a/C4/Members.pm b/C4/Members.pm >index 8953d85..4ee44ab 100644 >--- a/C4/Members.pm >+++ b/C4/Members.pm >@@ -70,7 +70,6 @@ BEGIN { > &GetFirstValidEmailAddress > &GetNoticeEmailAddress > >- &GetAge > &GetTitles > > &GetHideLostItemsPreference >@@ -1211,76 +1210,6 @@ sub GetUpcomingMembershipExpires { > return $results; > } > >-=head2 GetAge >- >- $dateofbirth,$date = &GetAge($date); >- >-this function return the borrowers age with the value of dateofbirth >- >-=cut >- >-#' >-sub GetAge{ >- my ( $date, $date_ref ) = @_; >- >- if ( not defined $date_ref ) { >- $date_ref = sprintf( '%04d-%02d-%02d', Today() ); >- } >- >- my ( $year1, $month1, $day1 ) = split /-/, $date; >- my ( $year2, $month2, $day2 ) = split /-/, $date_ref; >- >- my $age = $year2 - $year1; >- if ( $month1 . $day1 > $month2 . $day2 ) { >- $age--; >- } >- >- return $age; >-} # sub get_age >- >-=head2 SetAge >- >- $borrower = C4::Members::SetAge($borrower, $datetimeduration); >- $borrower = C4::Members::SetAge($borrower, '0015-12-10'); >- $borrower = C4::Members::SetAge($borrower, $datetimeduration, $datetime_reference); >- >- eval { $borrower = C4::Members::SetAge($borrower, '015-1-10'); }; >- if ($@) {print $@;} #Catch a bad ISO Date or kill your script! >- >-This function sets the borrower's dateofbirth to match the given age. >-Optionally relative to the given $datetime_reference. >- >-@PARAM1 koha.borrowers-object >-@PARAM2 DateTime::Duration-object as the desired age >- OR a ISO 8601 Date. (To make the API more pleasant) >-@PARAM3 DateTime-object as the relative date, defaults to now(). >-RETURNS The given borrower reference @PARAM1. >-DIES If there was an error with the ISO Date handling. >- >-=cut >- >-#' >-sub SetAge{ >- my ( $borrower, $datetimeduration, $datetime_ref ) = @_; >- $datetime_ref = DateTime->now() unless $datetime_ref; >- >- if ($datetimeduration && ref $datetimeduration ne 'DateTime::Duration') { >- if ($datetimeduration =~ /^(\d{4})-(\d{2})-(\d{2})/) { >- $datetimeduration = DateTime::Duration->new(years => $1, months => $2, days => $3); >- } >- else { >- die "C4::Members::SetAge($borrower, $datetimeduration), datetimeduration not a valid ISO 8601 Date!\n"; >- } >- } >- >- my $new_datetime_ref = $datetime_ref->clone(); >- $new_datetime_ref->subtract_duration( $datetimeduration ); >- >- $borrower->{dateofbirth} = $new_datetime_ref->ymd(); >- >- return $borrower; >-} # sub SetAge >- > =head2 GetHideLostItemsPreference > > $hidelostitemspref = &GetHideLostItemsPreference($borrowernumber); >diff --git a/Koha/Patron.pm b/Koha/Patron.pm >index 9a2fd18..c13464f 100644 >--- a/Koha/Patron.pm >+++ b/Koha/Patron.pm >@@ -449,6 +449,30 @@ sub add_enrolment_fee_if_needed { > return $enrolment_fee || 0; > } > >+=head3 get_age >+ >+my $age = $patron->get_age >+ >+Return the age of the patron >+ >+=cut >+ >+sub get_age { >+ my ($self) = @_; >+ my $today_str = dt_from_string->strftime("%Y-%m-%d"); >+ my $dob_str = dt_from_string( $self->dateofbirth )->strftime("%Y-%m-%d"); >+ >+ my ( $dob_y, $dob_m, $dob_d ) = split /-/, $dob_str; >+ my ( $today_y, $today_m, $today_d ) = split /-/, $today_str; >+ >+ my $age = $today_y - $dob_y; >+ if ( $dob_m . $dob_d > $today_m . $today_d ) { >+ $age--; >+ } >+ >+ return $age; >+} >+ > =head3 type > > =cut >diff --git a/members/memberentry.pl b/members/memberentry.pl >index 27e6687..6f1545c 100755 >--- a/members/memberentry.pl >+++ b/members/memberentry.pl >@@ -315,7 +315,8 @@ if ($op eq 'save' || $op eq 'insert'){ > } > > if ( $dateofbirth ) { >- my $age = GetAge($dateofbirth); >+ my $patron = Koha::Patron->new({ dateofbirth => $dateofbirth }); >+ my $age = $patron->get_age; > my $borrowercategory = Koha::Patron::Categories->find($categorycode); > my ($low,$high) = ($borrowercategory->dateofbirthrequired, $borrowercategory->upperagelimit); > if (($high && ($age > $high)) or ($age < $low)) { >diff --git a/members/moremember.pl b/members/moremember.pl >index 01d16b6..190393f 100755 >--- a/members/moremember.pl >+++ b/members/moremember.pl >@@ -236,9 +236,7 @@ my $overdues_exist = 0; > my $totalprice = 0; > > # Calculate and display patron's age >-my $dateofbirth = $data->{ 'dateofbirth' }; >-my $age = GetAge($dateofbirth); >-$template->param( age => $age ); >+$template->param( age => Koha::Patron->new({ dateofbirth => $data->{dateofbirth} })->get_age ); > > ### ############################################################################### > # BUILD HTML >diff --git a/t/Circulation/AgeRestrictionMarkers.t b/t/Circulation/AgeRestrictionMarkers.t >index 39dd44e..49c1d14 100644 >--- a/t/Circulation/AgeRestrictionMarkers.t >+++ b/t/Circulation/AgeRestrictionMarkers.t >@@ -39,8 +39,7 @@ subtest 'Patron tests - 15 years old' => sub { > plan tests => 5; > ##Testing age restriction for a borrower. > my $now = DateTime->now(); >- my $borrower = {}; >- C4::Members::SetAge( $borrower, '0015-00-00' ); >+ my $borrower = { dateofbirth => $now->add( years => -15 )->strftime("%Y-%m-%d") }; > TestPatron($borrower,0); > }; > >@@ -57,8 +56,7 @@ subtest 'Patron tests - 15 years old (Time Zone shifts)' => sub { > > ##Testing age restriction for a borrower. > my $now = DateTime->now(); >- my $borrower = {}; >- C4::Members::SetAge( $borrower, '0015-00-00' ); >+ my $borrower = { dateofbirth => $now->add( years => -15 )->strftime("%Y-%m-%d") }; > TestPatron($borrower,$offset); > > $offset++; >diff --git a/t/db_dependent/Reserves.t b/t/db_dependent/Reserves.t >index 1d45893..858b138 100755 >--- a/t/db_dependent/Reserves.t >+++ b/t/db_dependent/Reserves.t >@@ -529,14 +529,13 @@ C4::Biblio::ModBiblio( $record, $bibnum, '' ); > is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblionumber) , 'OK', "Reserving an ageRestricted Biblio without a borrower dateofbirth succeeds" ); > > #Set the dateofbirth for the Borrower making him "too young". >-my $now = DateTime->now(); >-C4::Members::SetAge( $borrower, '0015-00-00' ); >+$borrower->{dateofbirth} = DateTime->now->add( years => -15 ); > C4::Members::ModMember( borrowernumber => $borrowernumber, dateofbirth => $borrower->{dateofbirth} ); > > is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblionumber) , 'ageRestricted', "Reserving a 'PEGI 16' Biblio by a 15 year old borrower fails"); > > #Set the dateofbirth for the Borrower making him "too old". >-C4::Members::SetAge( $borrower, '0030-00-00' ); >+$borrower->{dateofbirth} = DateTime->now->add( years => -30 ); > C4::Members::ModMember( borrowernumber => $borrowernumber, dateofbirth => $borrower->{dateofbirth} ); > > is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblionumber) , 'OK', "Reserving a 'PEGI 16' Biblio by a 30 year old borrower succeeds"); >-- >2.1.4
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 17557
:
57218
|
57219
|
57220
|
57221
|
57862
|
57863
|
58104
|
58105