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

(-)a/Koha/Patron.pm (-1 / +58 lines)
Lines 24-29 use List::MoreUtils qw( any uniq ); Link Here
24
use JSON qw( to_json );
24
use JSON qw( to_json );
25
use Unicode::Normalize qw( NFKD );
25
use Unicode::Normalize qw( NFKD );
26
use Try::Tiny;
26
use Try::Tiny;
27
use DateTime ();
27
28
28
use C4::Context;
29
use C4::Context;
29
use C4::Auth qw( checkpw_hash );
30
use C4::Auth qw( checkpw_hash );
Lines 792-797 sub is_expired { Link Here
792
    return 0;
793
    return 0;
793
}
794
}
794
795
796
=head3 is_active
797
798
$patron->is_active({ [ since => $date ], [ days|weeks|months|years => $value ] })
799
800
A patron is considered 'active' if their account did not expire, and has not been anonymized,
801
and patron logged in recently, or placed a hold, article request or borrowed a book recently.
802
A recent enrollment as new patron counts too.
803
804
Recently is defined by $date or $value in days, weeks or months. If no parameter is passed,
805
we look at the last 6 months.
806
807
=cut
808
809
sub is_active {
810
    my ( $self, $params ) = @_;
811
    return 0 if $self->is_expired or $self->anonymized;
812
813
    my $dt;
814
    if( $params->{since} ) {
815
        $dt = dt_from_string( $params->{since}, 'iso' );
816
    } elsif( grep { $params->{$_} } qw(days weeks months years) ) {
817
        $dt = dt_from_string();
818
        foreach my $duration ( qw(days weeks months years) ) {
819
            $dt = $dt->subtract( $duration => $params->{$duration} ) if $params->{$duration};
820
        }
821
    } else {
822
        $dt = dt_from_string()->subtract( days => 180 ); # default fallback
823
    }
824
825
    # Enrollment within this period?
826
    return 1 if DateTime->compare( dt_from_string($self->dateenrolled), $dt ) > -1;
827
828
    # Last seen? Updated each login when you track patron activity
829
    if( C4::Context->preference('TrackLastPatronActivity') ) {
830
        return 1 if DateTime->compare( dt_from_string($self->lastseen), $dt ) > -1;
831
    }
832
833
    # Check holds, issues and article requests
834
    return 1 if $self->holds->filter_by_last_update({
835
        timestamp_column_name => 'timestamp', from => $dt,
836
    })->count;
837
    return 1 if $self->old_holds->filter_by_last_update({
838
        timestamp_column_name => 'timestamp', from => $dt,
839
    })->count;
840
    return 1 if $self->checkouts->filter_by_last_update({
841
        timestamp_column_name => 'timestamp', from => $dt,
842
    })->count;
843
    return 1 if $self->old_checkouts->filter_by_last_update({
844
        timestamp_column_name => 'timestamp', from => $dt,
845
    })->count;
846
    return 1 if $self->article_requests->filter_by_last_update({
847
        timestamp_column_name => 'updated_on', from => $dt,
848
    })->count;
849
850
    return 0;
851
}
852
795
=head3 password_expired
853
=head3 password_expired
796
854
797
my $password_expired = $patron->password_expired;
855
my $password_expired = $patron->password_expired;
798
- 

Return to bug 33245