From 987a49406858daff25b33afbbda5e8ae087f930d Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Mon, 26 Jul 2021 19:22:29 +0200 Subject: [PATCH] Bug 29149: Add the capability to provide more info to the background job detail view (Patch extracted from bug 28445 to make it reusable for bug 23678) 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 (in 28445) we will want to pass the infos of the items that have been modified to display a table. Test plan: 0. Create at least one list (virtual shelf) 1. batch update biblios 2. Go to the job detail 3. Notice that dropdown list to add the record to a list => No regression found! Signed-off-by: Tomas Cohen Arazi Signed-off-by: Jonathan Druart --- Koha/BackgroundJob.pm | 48 +++++++++++++++++++------ Koha/BackgroundJob/BatchUpdateBiblio.pm | 23 ++++++++++++ admin/background_jobs.pl | 10 ++---- t/db_dependent/Koha/BackgroundJobs.t | 8 ++++- 4 files changed, 70 insertions(+), 19 deletions(-) diff --git a/Koha/BackgroundJob.pm b/Koha/BackgroundJob.pm index 1498f51de6..553d7ce6e6 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 9371e2212f..34f732978a 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/admin/background_jobs.pl b/admin/background_jobs.pl index 9a474857b6..4dadcc1784 100755 --- a/admin/background_jobs.pl +++ b/admin/background_jobs.pl @@ -51,14 +51,8 @@ if ( $op eq 'view' ) { } else { $template->param( job => $job, ); - $template->param( - lists => scalar Koha::Virtualshelves->search( - [ - { category => 1, owner => $loggedinuser }, - { category => 2 } - ] - ) - ) if $job->type eq 'batch_biblio_record_modification'; + my $report = $job->additional_report() || {}; + $template->param( %$report ); } } else { $op = 'list'; diff --git a/t/db_dependent/Koha/BackgroundJobs.t b/t/db_dependent/Koha/BackgroundJobs.t index f678e20635..ce75368360 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.39.5