From d4225e05524d7362c2f5eecf555bb7a5b62b7be9 Mon Sep 17 00:00:00 2001 From: Janusz Kaczmarek Date: Mon, 9 Mar 2026 19:51:01 +0000 Subject: [PATCH] Bug 42044: Automatically update record status (LDR/5) for modified records MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When a record is modified, record status (LDR/5) should be automatically set to 'c' (Corrected or revised), unless: a) the librarian has manually changed the value to something different from the original one; b) the record was created on the same day by the same librarian (when this can be determined) and the MARC 21 record has no 040 $d subfield. Test plan ========= 1. Create a new bibliographic record. Verify that LDR/5 = 'n' (default). 2. Save the record and modify it. 3. Add 040 $d. 4. Modify the record as a different user. 5. At each step, verify that LDR/5 remains 'n'. 6. Apply the patch; updatedatabase; restart_all. 7. Set AutoUpdateRecordStatus system preference to 'Do'. 8. Repeat steps 1-4 and verify that LDR/5 is set to 'c' when: a) 040 $d is present; b) the record is modified by a different user; c) 008/0–5 is changed to a different date (simulating a modification date different from the creation date). 9. Repeat the whole test procedure for authority records. 10. Set AutoUpdateRecordStatus system preference to 'Don't'. Repeat steps 1-4 and verify that LDR/5 does not change automatically. 11. Check it also for authority records. Sponsored-by: Pontificia Università di San Tommaso d'Aquino (Angelicum) --- C4/AuthoritiesMarc.pm | 82 +++++++++++++++++++++++++++++++++- C4/Biblio.pm | 100 +++++++++++++++++++++++++++++++++++++++--- 2 files changed, 174 insertions(+), 8 deletions(-) diff --git a/C4/AuthoritiesMarc.pm b/C4/AuthoritiesMarc.pm index c2070f4560..1d7cb401f1 100644 --- a/C4/AuthoritiesMarc.pm +++ b/C4/AuthoritiesMarc.pm @@ -785,6 +785,14 @@ sub ModAuthority { my $oldrecord = GetAuthority($authid); + if ( C4::Context->preference('AutoUpdateRecordStatus') && $oldrecord ) { + my $ldr_5 = substr( $record->leader, 5, 1 ); + my $ldr_5_before_mod = substr( $oldrecord->leader, 5, 1 ); + if ( $ldr_5 ne 'c' && $ldr_5 eq $ldr_5_before_mod ) { # we update LDR/5 only if it has not been already changed + _update_record_status( $record, $authid ); + } + } + #Now rewrite the $record to table with an add $authid = AddAuthority( $record, $authid, $authtypecode, { skip_record_index => $skip_record_index } ); merge( { mergefrom => $authid, MARCfrom => $oldrecord, mergeto => $authid, MARCto => $record } ) @@ -1892,7 +1900,79 @@ sub _after_authority_action_hooks { return Koha::Plugins->call( 'after_authority_action', $args ); } -END { } # module clean-up code here (global destructor) +=head2 _update_record_status + + _update_record_status( $record, $authid ) + +Updates LDR/5 if need + +=cut + +sub _update_record_status { + my ( $record, $authid ) = @_; + my $do_update_record_status; + my $marcflavour = C4::Context->preference("marcflavour"); + + # first - check date entered on file + my $today = POSIX::strftime( "%y%m%d", localtime ); + my $date_created = ''; + if ( $marcflavour eq 'MARC21' ) { + $date_created = substr( $record->field('008')->data, 0, 6 ) + if $record->field('008'); + } elsif ( $marcflavour eq 'UNIMARC' ) { + $date_created = substr( $record->subfield( '100', 'a' ), 2, 6 ) + if $record->subfield( '100', 'a' ); + + } + if ( $date_created && $date_created ne $today ) { + $do_update_record_status = 1; + } + + # second - if dates equal, check for 040 $d (for MARC 21) + if ( !$do_update_record_status + && $marcflavour eq 'MARC21' + && $record->subfield( '040', 'd' ) ) + { + $do_update_record_status = 1; + } + + # third - if created today and no 040 $d, compare creator and modifier + if ( !$do_update_record_status ) { + if ( C4::Context->preference("AuthoritiesLog") ) { + my $logs = Koha::ActionLogs->search( + { + module => 'AUTHORITIES', + action => 'ADD', + object => $authid + } + ); + my $creator; + if ( my $log = $logs->next ) { + $creator = $log->user; + } + my $userenv = C4::Context->userenv(); + my $modifier; + $modifier = $userenv->{'number'} if ref($userenv) eq 'HASH'; + + # if not sure that modified by creator - set LDR/5 + unless ( defined $creator + && defined $modifier + && $creator == $modifier ) + { + $do_update_record_status = 1; + } + } else { + $do_update_record_status = 1; + } + } + if ($do_update_record_status) { + my $leader = $record->leader; + substr( $leader, 5, 1, 'c' ); + $record->leader($leader); + } +} + +END { } # module clean-up code here (global destructor) 1; __END__ diff --git a/C4/Biblio.pm b/C4/Biblio.pm index 863f4d34b4..d26176b947 100644 --- a/C4/Biblio.pm +++ b/C4/Biblio.pm @@ -395,18 +395,32 @@ sub ModBiblio { return 0; } - if ( C4::Context->preference("CataloguingLog") ) { + my $record_before_mod; + my $decoding_error = ""; + if ( C4::Context->preference("CataloguingLog") || C4::Context->preference('AutoUpdateRecordStatus') ) { my $biblio = Koha::Biblios->find($biblionumber); - my $record; - my $decoding_error = ""; - eval { $record = $biblio->metadata->record }; + eval { $record_before_mod = $biblio->metadata->record }; if ($@) { my $exception = $@; $exception->rethrow unless ( $exception->isa('Koha::Exceptions::Metadata::Invalid') ); - $decoding_error = "There was an error with this bibliographic record: " . $exception; - $record = $biblio->metadata->record_strip_nonxml; + $decoding_error = "There was an error with this bibliographic record: " . $exception; + $record_before_mod = $biblio->metadata->record_strip_nonxml; + } + } + + if ( C4::Context->preference("CataloguingLog") ) { + logaction( + "CATALOGUING", "MODIFY", $biblionumber, + "biblio $decoding_error BEFORE=>" . $record_before_mod->as_formatted + ); + } + + if ( C4::Context->preference('AutoUpdateRecordStatus') && $record_before_mod ) { + my $ldr_5 = substr( $record->leader, 5, 1 ); + my $ldr_5_before_mod = substr( $record_before_mod->leader, 5, 1 ); + if ( $ldr_5 ne 'c' && $ldr_5 eq $ldr_5_before_mod ) { # we update LDR/5 only if it has not been already changed + _update_record_status( $record, $biblionumber ); } - logaction( "CATALOGUING", "MODIFY", $biblionumber, "biblio $decoding_error BEFORE=>" . $record->as_formatted ); } if ( !$options->{disable_autolink} && C4::Context->preference('AutoLinkBiblios') ) { @@ -3201,6 +3215,78 @@ sub _after_biblio_action_hooks { ); } +=head2 _update_record_status + + _update_record_status( $record, $biblionumber ) + +Updates LDR/5 if need + +=cut + +sub _update_record_status { + my ( $record, $biblionumber ) = @_; + my $do_update_record_status; + my $marcflavour = C4::Context->preference("marcflavour"); + + # first - check date entered on file + my $today = POSIX::strftime( "%y%m%d", localtime ); + my $date_created = ''; + if ( $marcflavour eq 'MARC21' ) { + $date_created = substr( $record->field('008')->data, 0, 6 ) + if $record->field('008'); + } elsif ( $marcflavour eq 'UNIMARC' ) { + $date_created = substr( $record->subfield( '100', 'a' ), 2, 6 ) + if $record->subfield( '100', 'a' ); + + } + if ( $date_created && $date_created ne $today ) { + $do_update_record_status = 1; + } + + # second - if dates equal, check for 040 $d (for MARC 21) + if ( !$do_update_record_status + && $marcflavour eq 'MARC21' + && $record->subfield( '040', 'd' ) ) + { + $do_update_record_status = 1; + } + + # third - if created today and no 040 $d, compare creator and modifier + if ( !$do_update_record_status ) { + if ( C4::Context->preference("CataloguingLog") ) { + my $logs = Koha::ActionLogs->search( + { + module => 'CATALOGUING', + action => 'ADD', + object => $biblionumber + } + ); + my $creator; + if ( my $log = $logs->next ) { + $creator = $log->user; + } + my $userenv = C4::Context->userenv(); + my $modifier; + $modifier = $userenv->{'number'} if ref($userenv) eq 'HASH'; + + # if not sure that modified by creator - set LDR/5 + unless ( defined $creator + && defined $modifier + && $creator == $modifier ) + { + $do_update_record_status = 1; + } + } else { + $do_update_record_status = 1; + } + } + if ($do_update_record_status) { + my $leader = $record->leader; + substr( $leader, 5, 1, 'c' ); + $record->leader($leader); + } +} + 1; __END__ -- 2.39.5