@@ -, +, @@ --- 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 --- a/C4/Reserves.pm +++ a/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 --- a/t/db_dependent/Reserves/IsHoldNoteRequired.t +++ a/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; --