From 2eacfbf52eb16068b801af5e5c4ac4c53685300e Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Mon, 10 Jan 2022 11:23:10 -0300 Subject: [PATCH] Bug 29843: Add ->anonymize and ->filter_by_anonymizable to Koha::Old::Checkouts This patch implements high-level methods for: * Filtering a Koha::Old::Checkouts resultset so only anonymizable rows are left * Anonymizing the rows in a resultset To test: 1. Apply this patches 2. Run: $ kshell k$ prove t/db_dependent/Koha/Old/Checkouts.t => SUCCESS: Tests pass! 3. Sign off :-D Signed-off-by: Andrew Fuerste-Henry --- Koha/Old/Checkouts.pm | 71 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/Koha/Old/Checkouts.pm b/Koha/Old/Checkouts.pm index ec8b223907..5ba2bec6d2 100644 --- a/Koha/Old/Checkouts.pm +++ b/Koha/Old/Checkouts.pm @@ -23,6 +23,51 @@ use Koha::Old::Checkout; use base qw(Koha::Objects); +=head1 NAME + +Koha::Old::Checkouts - Koha Old Checkouts object set class + +This object represents a set of completed checkouts + +=head1 API + +=head2 Class methods + +=head3 filter_by_anonymizable + + my $checkouts = $patron->old_checkouts; + my $anonymizable_checkouts = $checkouts->filter_by_anonymizable; + +This method filters the resultset, so it only contains checkouts that can be +anonymized given the patron privacy settings. + +=cut + +sub filter_by_anonymizable { + my ( $self, $params ) = @_; + + my $anonymous_patron = C4::Context->preference('AnonymousPatron') || undef; + + return $self->search( + { + 'me.borrowernumber' => { 'not' => undef }, + 'patron.privacy' => { '<>' => 0 }, + ( + $anonymous_patron + ? ( 'me.borrowernumber' => { '!=' => $anonymous_patron } ) + : () + ), + }, + { + join => ['patron'], + } + ); +} + +=head3 filter_by_todays_checkins + +=cut + sub filter_by_todays_checkins { my ( $self ) = @_; @@ -40,10 +85,36 @@ sub filter_by_todays_checkins { }); } +=head3 anonymize + + $patron->old_checkouts->anonymize(); + +Anonymize the given I resultset. + +=cut + +sub anonymize { + my ( $self, $params ) = @_; + + my $anonymous_id = C4::Context->preference('AnonymousPatron') || undef; + + return $self->update( { borrowernumber => $anonymous_id }, { no_triggers => 1 } ); +} + +=head2 Internal methods + +=head3 _type + +=cut + sub _type { return 'OldIssue'; } +=head3 object_class + +=cut + sub object_class { return 'Koha::Old::Checkout'; } -- 2.30.2