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 2482-2487 sub IsItemOnHoldAndFound { Link Here
2482
    return $found;
2484
    return $found;
2483
}
2485
}
2484
2486
2487
=head2 IsHoldNoteRequired
2488
2489
    my $boolean = IsHoldNoteRequired( $biblionumber, $marcrecord );
2490
2491
    Returns true if a hold note is required for a given record.
2492
    Otherwise returns undef.
2493
2494
    Implemented by use of Koha plugins.
2495
    The routine will use all plugins it finds with metadata tag
2496
    'implements' set to 'IsHoldNoteRequired' and calls the check method.
2497
2498
    The plugins Rijks_HoldNoteRequired_Leader and _Extent offer an
2499
    implementation that can serve as an example (see bug 15545).
2500
2501
=cut
2502
2503
sub IsHoldNoteRequired {
2504
    my ( $biblionumber, $marcrecord ) = @_;
2505
2506
    # plugins enabled?
2507
    return if !C4::Context->preference('UseKohaPlugins') ||
2508
        !C4::Context->config("enable_plugins");
2509
2510
    # look for suitable plugin(s)
2511
    my $metadata = { implements => 'IsHoldNoteRequired' };
2512
    my @plugins = Koha::Plugins->new->GetPlugins({
2513
        metadata => $metadata,
2514
    });
2515
2516
    # ask the plugin
2517
    foreach my $plugin ( @plugins ) {
2518
        my $retval = Koha::Plugins::Handler->run({
2519
            class  => ref $plugin,
2520
            method => 'check',
2521
            params => {
2522
                biblionumber => $biblionumber,
2523
                record       => $marcrecord,
2524
            },
2525
        });
2526
        return 1 if $retval;
2527
    }
2528
2529
    # coming here implies not-required
2530
    return;
2531
}
2532
2485
=head1 AUTHOR
2533
=head1 AUTHOR
2486
2534
2487
Koha Development Team <http://koha-community.org/>
2535
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