From 0af43bf85db17f8e84ee772bb494fe1224c33dc7 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Mon, 26 Jul 2021 19:22:29 +0200 Subject: [PATCH] Bug 28445: Add the capability to provide more info to the background job detail view We already had the need for that, when bibliographic records are modified in batch we wanted to add a "Add to list" feature, and so pass a list of lists/virtual shelves to the template. Here we will want to pass the infos of the items that have been modified to display a table. --- Koha/BackgroundJob.pm | 48 +++++++++++++++++++------ Koha/BackgroundJob/BatchUpdateBiblio.pm | 23 ++++++++++++ t/db_dependent/Koha/BackgroundJobs.t | 8 ++++- 3 files changed, 68 insertions(+), 11 deletions(-) diff --git a/Koha/BackgroundJob.pm b/Koha/BackgroundJob.pm index 1498f51de63..553d7ce6e64 100644 --- a/Koha/BackgroundJob.pm +++ b/Koha/BackgroundJob.pm @@ -152,16 +152,14 @@ Process the job! sub process { my ( $self, $args ) = @_; - my $job_type = $self->type; - return $job_type eq 'batch_biblio_record_modification' - ? Koha::BackgroundJob::BatchUpdateBiblio->process($args) - : $job_type eq 'batch_authority_record_modification' - ? Koha::BackgroundJob::BatchUpdateAuthority->process($args) - : $job_type eq 'batch_biblio_record_deletion' - ? Koha::BackgroundJob::BatchDeleteBiblio->process($args) - : $job_type eq 'batch_authority_record_deletion' - ? Koha::BackgroundJob::BatchDeleteAuthority->process($args) - : Koha::Exceptions::Exception->throw('->process called without valid job_type'); + return {} if ref($self) ne 'Koha::BackgroundJob'; + + my $derived_class = $self->_derived_class; + + $args ||= {}; + + return $derived_class->process({job_id => $self->id, %$args}); + } =head3 job_type @@ -203,6 +201,22 @@ sub report { return $data_dump->{report}; } +=head3 additional_report + +Build additional variables for the job detail view. + +=cut + +sub additional_report { + my ( $self ) = @_; + + return {} if ref($self) ne 'Koha::BackgroundJob'; + + my $derived_class = $self->_derived_class; + + return $derived_class->additional_report({job_id => $self->id}); +} + =head3 cancel Cancel a job. @@ -214,6 +228,20 @@ sub cancel { $self->status('cancelled')->store; } +sub _derived_class { + my ( $self ) = @_; + my $job_type = $self->type; + return $job_type eq 'batch_biblio_record_modification' + ? Koha::BackgroundJob::BatchUpdateBiblio->new + : $job_type eq 'batch_authority_record_modification' + ? Koha::BackgroundJob::BatchUpdateAuthority->new + : $job_type eq 'batch_biblio_record_deletion' + ? Koha::BackgroundJob::BatchDeleteBiblio->new + : $job_type eq 'batch_authority_record_deletion' + ? Koha::BackgroundJob::BatchDeleteAuthority->new + : Koha::Exceptions::Exception->throw($job_type . ' is not a valid job_type') +} + sub _type { return 'BackgroundJob'; } diff --git a/Koha/BackgroundJob/BatchUpdateBiblio.pm b/Koha/BackgroundJob/BatchUpdateBiblio.pm index 9371e2212ff..34f732978a3 100644 --- a/Koha/BackgroundJob/BatchUpdateBiblio.pm +++ b/Koha/BackgroundJob/BatchUpdateBiblio.pm @@ -20,6 +20,9 @@ use JSON qw( decode_json encode_json ); use Koha::BackgroundJobs; use Koha::DateUtils qw( dt_from_string ); +use Koha::Virtualshelves; + +use C4::Context; use C4::Biblio; use C4::MarcModificationTemplates; @@ -140,4 +143,24 @@ sub enqueue { }); } +=head3 additional_report + +Pass the list of lists/virtual shelves the logged in user has write permissions. + +It will enable the "add modified records to list" feature. + +=cut + +sub additional_report { + my ($self) = @_; + + my $loggedinuser = C4::Context->userenv ? C4::Context->userenv->{'number'} : undef; + return { + lists => scalar Koha::Virtualshelves->search( + [ { category => 1, owner => $loggedinuser }, { category => 2 } ] + ), + }; + +} + 1; diff --git a/t/db_dependent/Koha/BackgroundJobs.t b/t/db_dependent/Koha/BackgroundJobs.t index f678e206357..ce753683605 100755 --- a/t/db_dependent/Koha/BackgroundJobs.t +++ b/t/db_dependent/Koha/BackgroundJobs.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 11; +use Test::More tests => 12; use Test::MockModule; use JSON qw( decode_json ); @@ -40,6 +40,10 @@ t::lib::Mocks::mock_userenv; my $net_stomp = Test::MockModule->new('Net::Stomp'); $net_stomp->mock( 'send_with_receipt', sub { return 1 } ); +my $background_job_module = Test::MockModule->new('Koha::BackgroundJob'); +$background_job_module->mock( '_derived_class', + sub { t::lib::Koha::BackgroundJob::BatchTest->new } ); + my $data = { a => 'aaa', b => 'bbb' }; my $job_size = 10; my $job_id = t::lib::Koha::BackgroundJob::BatchTest->new->enqueue( @@ -80,4 +84,6 @@ is_deeply( 'Correct number of records processed' ); +is_deeply( $new_job->additional_report(), {} ); + $schema->storage->txn_rollback; -- 2.20.1