View | Details | Raw Unified | Return to bug 15545
Collapse All | Expand All

(-)a/C4/Reserves.pm (+48 lines)
Lines 43-48 use Koha::Holds; Link Here
43
use Koha::Libraries;
43
use Koha::Libraries;
44
use Koha::Items;
44
use Koha::Items;
45
use Koha::ItemTypes;
45
use Koha::ItemTypes;
46
use Koha::Plugins;
47
use Koha::Plugins::Handler;
46
48
47
use List::MoreUtils qw( firstidx any );
49
use List::MoreUtils qw( firstidx any );
48
use Carp;
50
use Carp;
Lines 2481-2486 sub IsItemOnHoldAndFound { Link Here
2481
    return $found;
2483
    return $found;
2482
}
2484
}
2483
2485
2486
=head2 IsHoldNoteRequired
2487
2488
    my $boolean = IsHoldNoteRequired( $biblionumber, $marcrecord );
2489
2490
    Returns true if a hold note is required for a given record.
2491
    Otherwise returns undef.
2492
2493
    Implemented by use of Koha plugins.
2494
    The routine will use all plugins it finds with metadata tag
2495
    'implements' set to 'IsHoldNoteRequired' and calls the check method.
2496
2497
    The plugins Rijks_HoldNoteRequired_Leader and _Extent offer an
2498
    implementation that can serve as an example (see bug 15545).
2499
2500
=cut
2501
2502
sub IsHoldNoteRequired {
2503
    my ( $biblionumber, $marcrecord ) = @_;
2504
2505
    # plugins enabled?
2506
    return if !C4::Context->preference('UseKohaPlugins') ||
2507
        !C4::Context->config("enable_plugins");
2508
2509
    # look for suitable plugin(s)
2510
    my $metadata = { implements => 'IsHoldNoteRequired' };
2511
    my @plugins = Koha::Plugins->new->GetPlugins({
2512
        metadata => $metadata,
2513
    });
2514
2515
    # ask the plugin
2516
    foreach my $plugin ( @plugins ) {
2517
        my $retval = Koha::Plugins::Handler->run({
2518
            class  => ref $plugin,
2519
            method => 'check',
2520
            params => {
2521
                biblionumber => $biblionumber,
2522
                record       => $marcrecord,
2523
            },
2524
        });
2525
        return 1 if $retval;
2526
    }
2527
2528
    # coming here implies not-required
2529
    return;
2530
}
2531
2484
=head1 AUTHOR
2532
=head1 AUTHOR
2485
2533
2486
Koha Development Team <http://koha-community.org/>
2534
Koha Development Team <http://koha-community.org/>
(-)a/t/db_dependent/Reserves/IsHoldNoteRequired.t (-1 / +89 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# This script tests C4::Reserves::IsHoldNoteRequired.
4
5
# Copyright 2016 Rijksmuseum
6
#
7
# This file is part of Koha.
8
#
9
# Koha is free software; you can redistribute it and/or modify it
10
# under the terms of the GNU General Public License as published by
11
# the Free Software Foundation; either version 3 of the License, or
12
# (at your option) any later version.
13
#
14
# Koha is distributed in the hope that it will be useful, but
15
# WITHOUT ANY WARRANTY; without even the implied warranty of
16
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
# GNU General Public License for more details.
18
#
19
# You should have received a copy of the GNU General Public License
20
# along with Koha; if not, see <http://www.gnu.org/licenses>.
21
22
use Modern::Perl;
23
use File::Temp qw/tempdir/;
24
use MARC::Record;
25
use Test::More tests => 3;
26
27
use t::lib::Mocks;
28
use C4::Context;
29
use C4::Reserves;
30
use Koha::Database;
31
32
my $schema = Koha::Database->new->schema;
33
$schema->storage->txn_begin;
34
35
# Add a temporary plugin
36
C4::Context->set_preference( 'OPACHoldNotes', '1' );
37
t::lib::Mocks::mock_config( 'enable_plugins', 1);
38
my $tempdir = tempdir( CLEANUP => 1 );
39
my $org_pluginsdir = C4::Context->config('pluginsdir') // '';
40
t::lib::Mocks::mock_config( 'pluginsdir', $tempdir );
41
add_myplugin( $tempdir );
42
# Adjust searchable module dirs
43
@INC = grep { $_ ne $org_pluginsdir } ( @INC, $tempdir );
44
45
# First test with disabled plugins
46
C4::Context->set_preference( 'UseKohaPlugins', '0' );
47
isnt( C4::Reserves::IsHoldNoteRequired(), 1, 'Should be false if plugins are disabled' );
48
49
# Enable plugin now
50
# Note: the plugin actually ignores $marc and returns true for a non-zero bibno
51
my $marc = MARC::Record->new;
52
C4::Context->set_preference( 'UseKohaPlugins', '1' );
53
is( C4::Reserves::IsHoldNoteRequired( 2, $marc ), 1, 'Should be true for biblionumber 2' );
54
isnt( C4::Reserves::IsHoldNoteRequired( 0, $marc ), 1, 'Should be false for biblionumber 0' );
55
56
# End
57
$schema->storage->txn_rollback;
58
59
sub add_myplugin {
60
    my ( $dir ) = @_;
61
    system( "mkdir -p $dir/Koha/Plugin" );
62
    open my $fh, '>', $dir.'/Koha/Plugin/Rijks_MyPlugin.pm';
63
    print $fh
64
q|package Koha::Plugin::Rijks_MyPlugin;
65
use Modern::Perl;
66
use base qw(Koha::Plugins::Base);
67
68
our $metadata = {
69
    name            => 'Rijks_MyPlugin',
70
    implements      => 'IsHoldNoteRequired', # needed for C4::Reserves
71
};
72
73
sub new {
74
    my ( $class, $args ) = @_;
75
    $args->{'metadata'} = $metadata;
76
    my $self = $class->SUPER::new($args);
77
    return $self;
78
}
79
80
sub check {
81
    my ( $self, $args ) = @_;
82
    return $args->{biblionumber}; # unusual return value for testing
83
}
84
85
1;|;
86
    close $fh;
87
}
88
89
1;

Return to bug 15545