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

(-)a/Koha/Patron.pm (-1 / +68 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. You should
805
pass one of those; otherwise an exception is thrown.
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
        Koha::Exceptions::MissingParameter->throw('is_active needs date or period');
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
        {
836
            timestamp_column_name => 'timestamp', from => $dt,
837
        }
838
    )->count;
839
    return 1 if $self->old_holds->filter_by_last_update(
840
        {
841
            timestamp_column_name => 'timestamp', from => $dt,
842
        }
843
    )->count;
844
    return 1 if $self->checkouts->filter_by_last_update(
845
        {
846
            timestamp_column_name => 'timestamp', from => $dt,
847
        }
848
    )->count;
849
    return 1 if $self->old_checkouts->filter_by_last_update(
850
        {
851
            timestamp_column_name => 'timestamp', from => $dt,
852
        }
853
    )->count;
854
    return 1 if $self->article_requests->filter_by_last_update(
855
        {
856
            timestamp_column_name => 'updated_on', from => $dt,
857
        }
858
    )->count;
859
860
    return 0;
861
}
862
795
=head3 password_expired
863
=head3 password_expired
796
864
797
my $password_expired = $patron->password_expired;
865
my $password_expired = $patron->password_expired;
798
- 

Return to bug 33245