From 34f452e49ecc955c7aade9ef5fe66d04fa1b3c79 Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Thu, 26 May 2016 11:38:46 +0200 Subject: [PATCH] Bug 15545: Add routine IsHoldNoteRequired to Reserves module This routine will be used to determine if a hold note is required. In the current approach this is implemented by use of Koha plugins. A former approach included a syspref. This is no longer needed. This approach is even more flexible. The routine will use all plugins it finds with metadata tag 'implements' set to 'IsHoldNoteRequired'. If one of these plugins has a check method that returns true, the hold note should be regarded as required. The plugins Rijks_HoldNoteRequired_Extent and _Leader offer an implementation where the value of the leader or MARC21 300$a determines if the hold note is required. Similarly, any other field of the bibliographic record could be used in a new plugin. Test plan: Run unit test t/db_dependent/Reserves/IsHoldNoteRequired.t. Signed-off-by: Liz Rea --- C4/Reserves.pm | 48 +++++++++++++++ t/db_dependent/Reserves/IsHoldNoteRequired.t | 89 ++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+) create mode 100755 t/db_dependent/Reserves/IsHoldNoteRequired.t diff --git a/C4/Reserves.pm b/C4/Reserves.pm index 79fb11a..102808c 100644 --- a/C4/Reserves.pm +++ b/C4/Reserves.pm @@ -43,6 +43,8 @@ use Koha::Holds; use Koha::Libraries; use Koha::Items; use Koha::ItemTypes; +use Koha::Plugins; +use Koha::Plugins::Handler; use List::MoreUtils qw( firstidx any ); use Carp; @@ -2481,6 +2483,52 @@ sub IsItemOnHoldAndFound { return $found; } +=head2 IsHoldNoteRequired + + my $boolean = IsHoldNoteRequired( $biblionumber, $marcrecord ); + + Returns true if a hold note is required for a given record. + Otherwise returns undef. + + Implemented by use of Koha plugins. + The routine will use all plugins it finds with metadata tag + 'implements' set to 'IsHoldNoteRequired' and calls the check method. + + The plugins Rijks_HoldNoteRequired_Leader and _Extent offer an + implementation that can serve as an example (see bug 15545). + +=cut + +sub IsHoldNoteRequired { + my ( $biblionumber, $marcrecord ) = @_; + + # plugins enabled? + return if !C4::Context->preference('UseKohaPlugins') || + !C4::Context->config("enable_plugins"); + + # look for suitable plugin(s) + my $metadata = { implements => 'IsHoldNoteRequired' }; + my @plugins = Koha::Plugins->new->GetPlugins({ + metadata => $metadata, + }); + + # ask the plugin + foreach my $plugin ( @plugins ) { + my $retval = Koha::Plugins::Handler->run({ + class => ref $plugin, + method => 'check', + params => { + biblionumber => $biblionumber, + record => $marcrecord, + }, + }); + return 1 if $retval; + } + + # coming here implies not-required + return; +} + =head1 AUTHOR Koha Development Team diff --git a/t/db_dependent/Reserves/IsHoldNoteRequired.t b/t/db_dependent/Reserves/IsHoldNoteRequired.t new file mode 100755 index 0000000..8dc1011 --- /dev/null +++ b/t/db_dependent/Reserves/IsHoldNoteRequired.t @@ -0,0 +1,89 @@ +#!/usr/bin/perl + +# This script tests C4::Reserves::IsHoldNoteRequired. + +# Copyright 2016 Rijksmuseum +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; +use File::Temp qw/tempdir/; +use MARC::Record; +use Test::More tests => 3; + +use t::lib::Mocks; +use C4::Context; +use C4::Reserves; +use Koha::Database; + +my $schema = Koha::Database->new->schema; +$schema->storage->txn_begin; + +# Add a temporary plugin +C4::Context->set_preference( 'OPACHoldNotes', '1' ); +t::lib::Mocks::mock_config( 'enable_plugins', 1); +my $tempdir = tempdir( CLEANUP => 1 ); +my $org_pluginsdir = C4::Context->config('pluginsdir') // ''; +t::lib::Mocks::mock_config( 'pluginsdir', $tempdir ); +add_myplugin( $tempdir ); +# Adjust searchable module dirs +@INC = grep { $_ ne $org_pluginsdir } ( @INC, $tempdir ); + +# First test with disabled plugins +C4::Context->set_preference( 'UseKohaPlugins', '0' ); +isnt( C4::Reserves::IsHoldNoteRequired(), 1, 'Should be false if plugins are disabled' ); + +# Enable plugin now +# Note: the plugin actually ignores $marc and returns true for a non-zero bibno +my $marc = MARC::Record->new; +C4::Context->set_preference( 'UseKohaPlugins', '1' ); +is( C4::Reserves::IsHoldNoteRequired( 2, $marc ), 1, 'Should be true for biblionumber 2' ); +isnt( C4::Reserves::IsHoldNoteRequired( 0, $marc ), 1, 'Should be false for biblionumber 0' ); + +# End +$schema->storage->txn_rollback; + +sub add_myplugin { + my ( $dir ) = @_; + system( "mkdir -p $dir/Koha/Plugin" ); + open my $fh, '>', $dir.'/Koha/Plugin/Rijks_MyPlugin.pm'; + print $fh +q|package Koha::Plugin::Rijks_MyPlugin; +use Modern::Perl; +use base qw(Koha::Plugins::Base); + +our $metadata = { + name => 'Rijks_MyPlugin', + implements => 'IsHoldNoteRequired', # needed for C4::Reserves +}; + +sub new { + my ( $class, $args ) = @_; + $args->{'metadata'} = $metadata; + my $self = $class->SUPER::new($args); + return $self; +} + +sub check { + my ( $self, $args ) = @_; + return $args->{biblionumber}; # unusual return value for testing +} + +1;|; + close $fh; +} + +1; -- 1.9.1