Bug 30181: (follow-up) Remove redundant queries and parameters
[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::DateUtils qw( dt_from_string );
22 use Koha::Virtualshelves;
23
24 use C4::Context;
25 use C4::Biblio;
26 use C4::MarcModificationTemplates;
27
28 use base 'Koha::BackgroundJob';
29
30 =head1 NAME
31
32 Koha::BackgroundJob::BatchUpdateBiblio - Batch update bibliographic records
33
34 This is a subclass of Koha::BackgroundJob.
35
36 =head1 API
37
38 =head2 Class methods
39
40 =head3 job_type
41
42 Define the job type of this job: batch_biblio_record_modification
43
44 =cut
45
46 sub job_type {
47     return 'batch_biblio_record_modification';
48 }
49
50 =head3 process
51
52 Process the modification.
53
54 =cut
55
56 sub process {
57     my ( $self, $args ) = @_;
58
59     if ( $self->status eq 'cancelled' ) {
60         return;
61     }
62
63     # FIXME If the job has already been started, but started again (worker has been restart for instance)
64     # Then we will start from scratch and so double process the same records
65
66     my $job_progress = 0;
67     $self->started_on(dt_from_string)
68         ->progress($job_progress)
69         ->status('started')
70         ->store;
71
72     my $mmtid = $args->{mmtid};
73     my @record_ids = @{ $args->{record_ids} };
74
75     my $report = {
76         total_records => scalar @record_ids,
77         total_success => 0,
78     };
79     my @messages;
80     RECORD_IDS: for my $biblionumber ( sort { $a <=> $b } @record_ids ) {
81
82         last if $self->get_from_storage->status eq 'cancelled';
83
84         next unless $biblionumber;
85
86         # Modify the biblio
87         my $error = eval {
88             my $record = C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber });
89             C4::MarcModificationTemplates::ModifyRecordWithTemplate( $mmtid, $record );
90             my $frameworkcode = C4::Biblio::GetFrameworkCode( $biblionumber );
91             C4::Biblio::ModBiblio( $record, $biblionumber, $frameworkcode, {
92                 overlay_context => $args->{overlay_context},
93             });
94         };
95         if ( $error and $error != 1 or $@ ) { # ModBiblio returns 1 if everything as gone well
96             push @messages, {
97                 type => 'error',
98                 code => 'biblio_not_modified',
99                 biblionumber => $biblionumber,
100                 error => ($@ ? "$@" : $error),
101             };
102         } else {
103             push @messages, {
104                 type => 'success',
105                 code => 'biblio_modified',
106                 biblionumber => $biblionumber,
107             };
108             $report->{total_success}++;
109         }
110         $self->progress( ++$job_progress )->store;
111     }
112
113     my $job_data = decode_json $self->data;
114     $job_data->{messages} = \@messages;
115     $job_data->{report} = $report;
116
117     $self->ended_on(dt_from_string)
118         ->data(encode_json $job_data);
119     $self->status('finished') if $self->status ne 'cancelled';
120     $self->store;
121 }
122
123 =head3 enqueue
124
125 Enqueue the new job
126
127 =cut
128
129 sub enqueue {
130     my ( $self, $args) = @_;
131
132     # TODO Raise exception instead
133     return unless exists $args->{mmtid};
134     return unless exists $args->{record_ids};
135
136     $self->SUPER::enqueue({
137         job_size => scalar @{$args->{record_ids}},
138         job_args => $args,
139     });
140 }
141
142 =head3 additional_report
143
144 Pass the list of lists/virtual shelves the logged in user has write permissions.
145
146 It will enable the "add modified records to list" feature.
147
148 =cut
149
150 sub additional_report {
151     my ($self) = @_;
152
153     my $loggedinuser = C4::Context->userenv ? C4::Context->userenv->{'number'} : undef;
154     return {
155         lists => Koha::Virtualshelves->search(
156             [
157                 { public => 0, owner => $loggedinuser },
158                 { public => 1 }
159             ]
160         ),
161     };
162 }
163
164 1;