Bug 26352: (QA follow-up) Add additional tests
[koha.git] / Koha / BackgroundJob / BatchUpdateBiblio.pm
1 package Koha::BackgroundJob::BatchUpdateBiblio;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19 use JSON qw( decode_json encode_json );
20
21 use Koha::BackgroundJobs;
22 use Koha::DateUtils qw( dt_from_string );
23 use Koha::Virtualshelves;
24
25 use C4::Context;
26 use C4::Biblio;
27 use C4::MarcModificationTemplates;
28
29 use base 'Koha::BackgroundJob';
30
31 =head1 NAME
32
33 Koha::BackgroundJob::BatchUpdateBiblio - Batch update bibliographic records
34
35 This is a subclass of Koha::BackgroundJob.
36
37 =head1 API
38
39 =head2 Class methods
40
41 =head3 job_type
42
43 Define the job type of this job: batch_biblio_record_modification
44
45 =cut
46
47 sub job_type {
48     return 'batch_biblio_record_modification';
49 }
50
51 =head3 process
52
53 Process the modification.
54
55 =cut
56
57 sub process {
58     my ( $self, $args ) = @_;
59
60     my $job = Koha::BackgroundJobs->find( $args->{job_id} );
61
62     if ( !exists $args->{job_id} || !$job || $job->status eq 'cancelled' ) {
63         return;
64     }
65
66     # FIXME If the job has already been started, but started again (worker has been restart for instance)
67     # Then we will start from scratch and so double process the same records
68
69     my $job_progress = 0;
70     $job->started_on(dt_from_string)
71         ->progress($job_progress)
72         ->status('started')
73         ->store;
74
75     my $mmtid = $args->{mmtid};
76     my @record_ids = @{ $args->{record_ids} };
77
78     my $report = {
79         total_records => scalar @record_ids,
80         total_success => 0,
81     };
82     my @messages;
83     RECORD_IDS: for my $biblionumber ( sort { $a <=> $b } @record_ids ) {
84
85         last if $job->get_from_storage->status eq 'cancelled';
86
87         next unless $biblionumber;
88
89         # Modify the biblio
90         my $error = eval {
91             my $record = C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber });
92             C4::MarcModificationTemplates::ModifyRecordWithTemplate( $mmtid, $record );
93             my $frameworkcode = C4::Biblio::GetFrameworkCode( $biblionumber );
94             C4::Biblio::ModBiblio( $record, $biblionumber, $frameworkcode );
95         };
96         if ( $error and $error != 1 or $@ ) { # ModBiblio returns 1 if everything as gone well
97             push @messages, {
98                 type => 'error',
99                 code => 'biblio_not_modified',
100                 biblionumber => $biblionumber,
101                 error => ($@ ? $@ : $error),
102             };
103         } else {
104             push @messages, {
105                 type => 'success',
106                 code => 'biblio_modified',
107                 biblionumber => $biblionumber,
108             };
109             $report->{total_success}++;
110         }
111         $job->progress( ++$job_progress )->store;
112     }
113
114     my $job_data = decode_json $job->data;
115     $job_data->{messages} = \@messages;
116     $job_data->{report} = $report;
117
118     $job->ended_on(dt_from_string)
119         ->data(encode_json $job_data);
120     $job->status('finished') if $job->status ne 'cancelled';
121     $job->store;
122 }
123
124 =head3 enqueue
125
126 Enqueue the new job
127
128 =cut
129
130 sub enqueue {
131     my ( $self, $args) = @_;
132
133     # TODO Raise exception instead
134     return unless exists $args->{mmtid};
135     return unless exists $args->{record_ids};
136
137     my $mmtid = $args->{mmtid};
138     my @record_ids = @{ $args->{record_ids} };
139
140     $self->SUPER::enqueue({
141         job_size => scalar @record_ids,
142         job_args => {mmtid => $mmtid, record_ids => \@record_ids,}
143     });
144 }
145
146 =head3 additional_report
147
148 Pass the list of lists/virtual shelves the logged in user has write permissions.
149
150 It will enable the "add modified records to list" feature.
151
152 =cut
153
154 sub additional_report {
155     my ($self) = @_;
156
157     my $loggedinuser = C4::Context->userenv ? C4::Context->userenv->{'number'} : undef;
158     return {
159         lists => scalar Koha::Virtualshelves->search(
160             [ { category => 1, owner => $loggedinuser }, { category => 2 } ]
161         ),
162     };
163
164 }
165
166 1;