Lines 1-6
Link Here
|
1 |
package Koha::Patrons; |
1 |
package Koha::Patrons; |
2 |
|
2 |
|
3 |
# Copyright ByWater Solutions 2014 |
3 |
# Copyright 2014 ByWater Solutions |
|
|
4 |
# Copyright 2016 Koha Development Team |
4 |
# |
5 |
# |
5 |
# This file is part of Koha. |
6 |
# This file is part of Koha. |
6 |
# |
7 |
# |
Lines 22-27
use Modern::Perl;
Link Here
|
22 |
use Carp; |
23 |
use Carp; |
23 |
|
24 |
|
24 |
use Koha::Database; |
25 |
use Koha::Database; |
|
|
26 |
use Koha::DateUtils; |
25 |
|
27 |
|
26 |
use Koha::Patron; |
28 |
use Koha::Patron; |
27 |
|
29 |
|
Lines 37-42
Koha::Patron - Koha Patron Object class
Link Here
|
37 |
|
39 |
|
38 |
=cut |
40 |
=cut |
39 |
|
41 |
|
|
|
42 |
=head3 search_patrons_to_anonymise |
43 |
|
44 |
my $patrons = Koha::Patrons->search_patrons_to_anonymise( $date ); |
45 |
|
46 |
This method returns all patrons who has an issue history older than a given date. |
47 |
|
48 |
=cut |
49 |
|
50 |
sub search_patrons_to_anonymise { |
51 |
my ( $class, $older_than_date ) = @_; |
52 |
$older_than_date = $older_than_date ? dt_from_string($older_than_date) : dt_from_string; |
53 |
my $library = |
54 |
( C4::Context->preference('IndependentBranches') && C4::Context->userenv && !C4::Context->IsSuperLibrarian() && C4::Context->userenv->{branch} ) |
55 |
? C4::Context->userenv->{branch} |
56 |
: undef; |
57 |
|
58 |
my $dtf = Koha::Database->new->schema->storage->datetime_parser; |
59 |
my $rs = $class->search( |
60 |
{ returndate => { '<' => $dtf->format_datetime($older_than_date), }, |
61 |
'old_issues.borrowernumber' => { 'not' => undef }, |
62 |
privacy => { '<>' => 0 }, # Keep forever |
63 |
( $library ? ( 'old_issues.branchcode' => $library ) : () ), |
64 |
}, |
65 |
{ join => ["old_issues"], |
66 |
group_by => 'borrowernumber' |
67 |
} |
68 |
); |
69 |
return Koha::Patrons->_new_from_dbic($rs); |
70 |
} |
71 |
|
72 |
=head3 anonymise_issue_history |
73 |
|
74 |
Koha::Patrons->search->anonymise_issue_history( $older_than_date ); |
75 |
|
76 |
Anonymise issue history (old_issues) for all patrons older than the given date. |
77 |
To make sure all the conditions are met, the caller has the responsability to |
78 |
call search_patrons_to_anonymise to filter the Koha::Patrons set |
79 |
|
80 |
=cut |
81 |
|
82 |
sub anonymise_issue_history { |
83 |
my ( $self, $older_than_date ) = @_; |
84 |
|
85 |
return unless $older_than_date; |
86 |
$older_than_date = dt_from_string $older_than_date; |
87 |
|
88 |
# The default of 0 does not work due to foreign key constraints |
89 |
# The anonymisation should not fail quietly if AnonymousPatron is not a valid entry |
90 |
# Set it to undef (NULL) |
91 |
my $dtf = Koha::Database->new->schema->storage->datetime_parser; |
92 |
my $old_issues_to_anonymise = $self->search_related( 'old_issues', { returndate => { '<' => $dtf->format_datetime($older_than_date) } } ); |
93 |
my $anonymous_patron = C4::Context->preference('AnonymousPatron') || undef; |
94 |
$old_issues_to_anonymise->update( { 'old_issues.borrowernumber' => $anonymous_patron } ); |
95 |
} |
96 |
|
40 |
=head3 type |
97 |
=head3 type |
41 |
|
98 |
|
42 |
=cut |
99 |
=cut |