Bug 33245: Introduce patron->is_active
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(Y);' Note: replace X by a valid borrowernumber and Y by date criterium. Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl> Signed-off-by: Matt Blenkinsop <matt.blenkinsop@ptfs-europe.com> Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org> Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
This commit is contained in:
parent
c6283f5d9a
commit
b9f8147cdf
|
|
@ -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::Auth qw( checkpw_hash );
|
||||
use C4::Context;
|
||||
|
|
@ -808,6 +809,73 @@ 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. You should
|
||||
pass one of those; otherwise an exception is thrown.
|
||||
|
||||
=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 {
|
||||
Koha::Exceptions::MissingParameter->throw('is_active needs date or period');
|
||||
}
|
||||
|
||||
# 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;
|
||||
|
|
|
|||
Loading…
Reference in New Issue