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

(-)a/Koha/Old/Holds.pm (+44 lines)
Lines 22-27 use Modern::Perl; Link Here
22
22
23
use Koha::Database;
23
use Koha::Database;
24
24
25
use Koha::DateUtils qw(dt_from_string);
25
use Koha::Old::Hold;
26
use Koha::Old::Hold;
26
27
27
use base qw(Koha::Holds);
28
use base qw(Koha::Holds);
Lines 36-41 This object represents a set of holds that have been filled or canceled Link Here
36
37
37
=head2 Class methods
38
=head2 Class methods
38
39
40
=head3 filter_by_anonymizable
41
42
    my $holds = $patron->old_holds;
43
    my $anonymizable_holds = $holds->filter_by_anonymizable( { [ before => $older_than_date, ] } );
44
45
This method filters a I<Koha::Old::Holds> resultset, so it only contains holds that can be
46
anonimized given the patron privacy settings. It accepts the following parameters:
47
48
=over 4
49
50
=item I<before>: a I<DateTime> object. Holds older than this date are eligible for anonymization. If omitted, the current date is used.
51
52
=back
53
54
=cut
55
56
sub filter_by_anonymizable {
57
    my ( $self, $params ) = @_;
58
    my $before     = $params->{before};
59
60
    $before //= dt_from_string();
61
62
    my $anonymous_patron = C4::Context->preference('AnonymousPatron') || undef;
63
64
    my $dtf = Koha::Database->new->schema->storage->datetime_parser;
65
    return $self->search(
66
        {
67
            timestamp           => { '<'   => $dtf->format_datetime($before) },
68
            'me.borrowernumber' => { 'not' => undef },
69
            'patron.privacy' => { '<>'  => 0 },
70
            (
71
                $anonymous_patron
72
                ? ( 'me.borrowernumber' => { '!=' => $anonymous_patron } )
73
                : ()
74
            ),
75
        },
76
        {
77
            join     => ['patron'],
78
            order_by => ['me.reserve_id'],
79
        }
80
    );
81
}
82
39
=head3 anonymize
83
=head3 anonymize
40
84
41
    $patron->old_holds->anonymize();
85
    $patron->old_holds->anonymize();
(-)a/Koha/Schema/Result/OldReserve.pm (+12 lines)
Lines 394-399 __PACKAGE__->belongs_to( Link Here
394
  },
394
  },
395
);
395
);
396
396
397
__PACKAGE__->belongs_to(
398
  "patron",
399
  "Koha::Schema::Result::Borrower",
400
  { borrowernumber => "borrowernumber" },
401
  {
402
    is_deferrable => 1,
403
    join_type     => "LEFT",
404
    on_delete     => "SET NULL",
405
    on_update     => "SET NULL",
406
  },
407
);
408
397
__PACKAGE__->add_columns(
409
__PACKAGE__->add_columns(
398
    '+item_level_hold' => { is_boolean => 1 },
410
    '+item_level_hold' => { is_boolean => 1 },
399
    '+lowestPriority'  => { is_boolean => 1 },
411
    '+lowestPriority'  => { is_boolean => 1 },
(-)a/t/db_dependent/Koha/Old/Holds.t (-2 / +77 lines)
Lines 17-23 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 1;
20
use Test::More tests => 2;
21
21
22
use Koha::Database;
22
use Koha::Database;
23
use Koha::DateUtils qw(dt_from_string);
23
use Koha::DateUtils qw(dt_from_string);
Lines 85-87 subtest 'anonymize() tests' => sub { Link Here
85
85
86
    $schema->storage->txn_rollback;
86
    $schema->storage->txn_rollback;
87
};
87
};
88
- 
88
89
subtest 'filter_by_anonymizable() tests' => sub {
90
91
    plan tests => 7;
92
93
    $schema->storage->txn_begin;
94
95
    # patron_1 => keep records forever
96
    my $patron_1 = $builder->build_object(
97
        { class => 'Koha::Patrons', value => { privacy => 0 } } );
98
99
    # patron_2 => never keep records
100
    my $patron_2 = $builder->build_object(
101
        { class => 'Koha::Patrons', value => { privacy => 1 } } );
102
103
    is( $patron_1->old_holds->count, 0, 'patron_1 has no old holds' );
104
    is( $patron_2->old_holds->count, 0, 'patron_2 has no old holds' );
105
106
    my $hold_1 = $builder->build_object(
107
        {
108
            class => 'Koha::Holds',
109
            value => {
110
                borrowernumber => $patron_1->id,
111
            }
112
        }
113
    )->_move_to_old;
114
    my $hold_2 = $builder->build_object(
115
        {
116
            class => 'Koha::Holds',
117
            value => {
118
                borrowernumber => $patron_2->id,
119
            }
120
        }
121
    )->_move_to_old;
122
    my $hold_3 = $builder->build_object(
123
        {
124
            class => 'Koha::Holds',
125
            value => {
126
                borrowernumber => $patron_1->id,
127
            }
128
        }
129
    )->_move_to_old;
130
    my $hold_4 = $builder->build_object(
131
        {
132
            class => 'Koha::Holds',
133
            value => {
134
                borrowernumber => $patron_2->id,
135
            }
136
        }
137
    )->_move_to_old;
138
139
    $hold_1 = Koha::Old::Holds->find( $hold_1->id )
140
      ->set( { timestamp => dt_from_string() } )->store;
141
    $hold_2 = Koha::Old::Holds->find( $hold_2->id )
142
      ->set( { timestamp => dt_from_string()->subtract( days => 1 ) } )->store;
143
    $hold_3 = Koha::Old::Holds->find( $hold_3->id )
144
      ->set( { timestamp => dt_from_string()->subtract( days => 2 ) } )->store;
145
    $hold_4 = Koha::Old::Holds->find( $hold_4->id )
146
      ->set( { timestamp => dt_from_string()->subtract( days => 3 ) } )->store;
147
148
    is( $patron_1->old_holds->count, 2, 'patron_1 has 2 completed holds' );
149
    is( $patron_2->old_holds->count, 2, 'patron_2 has 2 completed holds' );
150
151
    # filter them so only the older two are part of the resultset
152
    my $holds = Koha::Old::Holds->search(
153
        { 'me.borrowernumber' => [ $patron_1->id, $patron_2->id ] } );
154
    is( $holds->count, 4, 'Total of 4 holds returned correctly' );
155
    my $rs = $holds->filter_by_anonymizable;
156
    is( $rs->count, 2, 'Only 2 can be anonymized' );
157
158
    $rs = $holds->filter_by_anonymizable(
159
        { before => dt_from_string()->subtract( days => 1 ) } );
160
    is( $rs->count, 1, 'Only 1 can be anonymized with passed before date' );
161
162
    $schema->storage->txn_rollback;
163
};

Return to bug 29525