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::ArticleRequests; |
28 |
use Koha::ArticleRequests; |
27 |
use Koha::ArticleRequest::Status; |
29 |
use Koha::ArticleRequest::Status; |
Lines 148-153
sub article_requests_finished {
Link Here
|
148 |
return $self->{_article_requests_finished}; |
150 |
return $self->{_article_requests_finished}; |
149 |
} |
151 |
} |
150 |
|
152 |
|
|
|
153 |
=head3 search_patrons_to_anonymise |
154 |
|
155 |
my $patrons = Koha::Patrons->search_patrons_to_anonymise( $date ); |
156 |
|
157 |
This method returns all patrons who has an issue history older than a given date. |
158 |
|
159 |
=cut |
160 |
|
161 |
sub search_patrons_to_anonymise { |
162 |
my ( $class, $older_than_date, $library ) = @_; |
163 |
$older_than_date = $older_than_date ? dt_from_string($older_than_date) : dt_from_string; |
164 |
$library ||= |
165 |
( C4::Context->preference('IndependentBranches') && C4::Context->userenv && !C4::Context->IsSuperLibrarian() && C4::Context->userenv->{branch} ) |
166 |
? C4::Context->userenv->{branch} |
167 |
: undef; |
168 |
|
169 |
my $dtf = Koha::Database->new->schema->storage->datetime_parser; |
170 |
my $rs = $class->search( |
171 |
{ returndate => { '<' => $dtf->format_datetime($older_than_date), }, |
172 |
'old_issues.borrowernumber' => { 'not' => undef }, |
173 |
privacy => { '<>' => 0 }, # Keep forever |
174 |
( $library ? ( 'old_issues.branchcode' => $library ) : () ), |
175 |
}, |
176 |
{ join => ["old_issues"], |
177 |
group_by => 'borrowernumber' |
178 |
} |
179 |
); |
180 |
return Koha::Patrons->_new_from_dbic($rs); |
181 |
} |
182 |
|
183 |
=head3 anonymise_issue_history |
184 |
|
185 |
Koha::Patrons->search->anonymise_issue_history( $older_than_date ); |
186 |
|
187 |
Anonymise issue history (old_issues) for all patrons older than the given date. |
188 |
To make sure all the conditions are met, the caller has the responsability to |
189 |
call search_patrons_to_anonymise to filter the Koha::Patrons set |
190 |
|
191 |
=cut |
192 |
|
193 |
sub anonymise_issue_history { |
194 |
my ( $self, $older_than_date ) = @_; |
195 |
|
196 |
return unless $older_than_date; |
197 |
$older_than_date = dt_from_string $older_than_date; |
198 |
|
199 |
# The default of 0 does not work due to foreign key constraints |
200 |
# The anonymisation should not fail quietly if AnonymousPatron is not a valid entry |
201 |
# Set it to undef (NULL) |
202 |
my $dtf = Koha::Database->new->schema->storage->datetime_parser; |
203 |
my $old_issues_to_anonymise = $self->search_related( 'old_issues', { returndate => { '<' => $dtf->format_datetime($older_than_date) } } ); |
204 |
my $anonymous_patron = C4::Context->preference('AnonymousPatron') || undef; |
205 |
$old_issues_to_anonymise->update( { 'old_issues.borrowernumber' => $anonymous_patron } ); |
206 |
} |
207 |
|
151 |
=head3 type |
208 |
=head3 type |
152 |
|
209 |
|
153 |
=cut |
210 |
=cut |