From 845d643b0fad4709d43eeb3814fb03924b794317 Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Thu, 16 Mar 2023 14:34:58 +0000 Subject: [PATCH] Bug 33245: Introduce patron->is_active Content-Type: text/plain; charset=utf-8 Does only add the new patron method. NOTE: We may define additional criteria at some point, but I think that this is a good starting point. Test plan: Run perl -MKoha::Patron -e'print Koha::Patrons->find(X)->is_active;' Note: replace X by a valid borrowernumber. Signed-off-by: Marcel de Rooy --- Koha/Patron.pm | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/Koha/Patron.pm b/Koha/Patron.pm index 2a8eecf9f6..5c0597ead2 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -24,6 +24,7 @@ use List::MoreUtils qw( any uniq ); use JSON qw( to_json ); use Unicode::Normalize qw( NFKD ); use Try::Tiny; +use DateTime (); use C4::Context; use C4::Auth qw( checkpw_hash ); @@ -792,6 +793,63 @@ sub is_expired { return 0; } +=head3 is_active + +$patron->is_active({ [ since => $date ], [ days|weeks|months|years => $value ] }) + +A patron is considered 'active' if their account did not expire, and has not been anonymized, +and patron logged in recently, or placed a hold, article request or borrowed a book recently. +A recent enrollment as new patron counts too. + +Recently is defined by $date or $value in days, weeks or months. If no parameter is passed, +we look at the last 6 months. + +=cut + +sub is_active { + my ( $self, $params ) = @_; + return 0 if $self->is_expired or $self->anonymized; + + my $dt; + if( $params->{since} ) { + $dt = dt_from_string( $params->{since}, 'iso' ); + } elsif( grep { $params->{$_} } qw(days weeks months years) ) { + $dt = dt_from_string(); + foreach my $duration ( qw(days weeks months years) ) { + $dt = $dt->subtract( $duration => $params->{$duration} ) if $params->{$duration}; + } + } else { + $dt = dt_from_string()->subtract( days => 180 ); # default fallback + } + + # Enrollment within this period? + return 1 if DateTime->compare( dt_from_string($self->dateenrolled), $dt ) > -1; + + # Last seen? Updated each login when you track patron activity + if( C4::Context->preference('TrackLastPatronActivity') ) { + return 1 if DateTime->compare( dt_from_string($self->lastseen), $dt ) > -1; + } + + # Check holds, issues and article requests + return 1 if $self->holds->filter_by_last_update({ + timestamp_column_name => 'timestamp', from => $dt, + })->count; + return 1 if $self->old_holds->filter_by_last_update({ + timestamp_column_name => 'timestamp', from => $dt, + })->count; + return 1 if $self->checkouts->filter_by_last_update({ + timestamp_column_name => 'timestamp', from => $dt, + })->count; + return 1 if $self->old_checkouts->filter_by_last_update({ + timestamp_column_name => 'timestamp', from => $dt, + })->count; + return 1 if $self->article_requests->filter_by_last_update({ + timestamp_column_name => 'updated_on', from => $dt, + })->count; + + return 0; +} + =head3 password_expired my $password_expired = $patron->password_expired; -- 2.30.2