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

(-)a/C4/Reserves.pm (+48 lines)
Lines 47-52 use Koha::IssuingRules; Link Here
47
use Koha::Items;
47
use Koha::Items;
48
use Koha::ItemTypes;
48
use Koha::ItemTypes;
49
use Koha::Patrons;
49
use Koha::Patrons;
50
use Koha::Plugins;
51
use Koha::Plugins::Handler;
50
52
51
use List::MoreUtils qw( firstidx any );
53
use List::MoreUtils qw( firstidx any );
52
use Carp;
54
use Carp;
Lines 2532-2537 sub GetHoldRule { Link Here
2532
    return $sth->fetchrow_hashref();
2534
    return $sth->fetchrow_hashref();
2533
}
2535
}
2534
2536
2537
=head2 IsHoldNoteRequired
2538
2539
    my $boolean = IsHoldNoteRequired( $biblionumber, $marcrecord );
2540
2541
    Returns true if a hold note is required for a given record.
2542
    Otherwise returns undef.
2543
2544
    Implemented by use of Koha plugins.
2545
    The routine will use all plugins it finds with metadata tag
2546
    'implements' set to 'IsHoldNoteRequired' and calls the check method.
2547
2548
    The plugins Rijks_HoldNoteRequired_Leader and _Extent offer an
2549
    implementation that can serve as an example (see bug 15545).
2550
2551
=cut
2552
2553
sub IsHoldNoteRequired {
2554
    my ( $biblionumber, $marcrecord ) = @_;
2555
2556
    # plugins enabled?
2557
    return if !C4::Context->preference('UseKohaPlugins') ||
2558
        !C4::Context->config("enable_plugins");
2559
2560
    # look for suitable plugin(s)
2561
    my $metadata = { implements => 'IsHoldNoteRequired' };
2562
    my @plugins = Koha::Plugins->new->GetPlugins({
2563
        metadata => $metadata,
2564
    });
2565
2566
    # ask the plugin
2567
    foreach my $plugin ( @plugins ) {
2568
        my $retval = Koha::Plugins::Handler->run({
2569
            class  => ref $plugin,
2570
            method => 'check',
2571
            params => {
2572
                biblionumber => $biblionumber,
2573
                record       => $marcrecord,
2574
            },
2575
        });
2576
        return 1 if $retval;
2577
    }
2578
2579
    # coming here implies not-required
2580
    return;
2581
}
2582
2535
=head1 AUTHOR
2583
=head1 AUTHOR
2536
2584
2537
Koha Development Team <http://koha-community.org/>
2585
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