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

(-)a/Koha/CirculationRules.pm (+67 lines)
Lines 24-29 use Koha::CirculationRule; Link Here
24
24
25
use base qw(Koha::Objects);
25
use base qw(Koha::Objects);
26
26
27
use constant GUESSED_ITEMTYPES_KEY => 'Koha_IssuingRules_last_guess';
28
27
=head1 NAME
29
=head1 NAME
28
30
29
Koha::CirculationRules - Koha CirculationRule Object set class
31
Koha::CirculationRules - Koha CirculationRule Object set class
Lines 396-401 sub get_onshelfholds_policy { Link Here
396
    return $rule ? $rule->rule_value : undef;
398
    return $rule ? $rule->rule_value : undef;
397
}
399
}
398
400
401
=head3 article_requestable_rules
402
403
    Return rules that allow article requests, optionally filtered by
404
    patron categorycode.
405
406
    Use with care; see guess_article_requestable_itemtypes.
407
408
=cut
409
410
sub article_requestable_rules {
411
    my ( $class, $params ) = @_;
412
    my $category = $params->{categorycode};
413
414
    return if !C4::Context->preference('ArticleRequests');
415
    return $class->search({
416
        $category ? ( categorycode => [ $category, '*' ] ) : (),
417
        rule_name => 'article_requests',
418
        rule_value => { '!=' => 'no' },
419
    });
420
}
421
422
=head3 guess_article_requestable_itemtypes
423
424
    Return item types in a hashref that are likely possible to be
425
    'article requested'. Constructed by an intelligent guess in the
426
    issuing rules (see article_requestable_rules).
427
428
    Note: pref ArticleRequestsLinkControl overrides the algorithm.
429
430
    Optional parameters: categorycode.
431
432
    Note: the routine is used in opac-search to obtain a reasonable
433
    estimate within performance borders (not looking at all items but
434
    just using default itemtype). Also we are not looking at the
435
    branchcode here, since home or holding branch of the item is
436
    leading and branch may be unknown too (anonymous opac session).
437
438
=cut
439
440
sub guess_article_requestable_itemtypes {
441
    my ( $class, $params ) = @_;
442
    my $category = $params->{categorycode};
443
    return {} if !C4::Context->preference('ArticleRequests');
444
    return { '*' => 1 } if C4::Context->preference('ArticleRequestsLinkControl') eq 'always';
445
446
    my $cache = Koha::Caches->get_instance;
447
    my $last_article_requestable_guesses = $cache->get_from_cache(GUESSED_ITEMTYPES_KEY);
448
    my $key = $category || '*';
449
    return $last_article_requestable_guesses->{$key}
450
        if $last_article_requestable_guesses && exists $last_article_requestable_guesses->{$key};
451
452
    my $res = {};
453
    my $rules = $class->article_requestable_rules({
454
        $category ? ( categorycode => $category ) : (),
455
    });
456
    return $res if !$rules;
457
    foreach my $rule ( $rules->as_list ) {
458
        $res->{ $rule->itemtype } = 1;
459
    }
460
    $last_article_requestable_guesses->{$key} = $res;
461
    $cache->set_in_cache(GUESSED_ITEMTYPES_KEY, $last_article_requestable_guesses);
462
    return $res;
463
}
464
465
399
=head3 type
466
=head3 type
400
467
401
=cut
468
=cut
(-)a/Koha/ItemType.pm (-2 / +2 lines)
Lines 22-28 use Carp; Link Here
22
use C4::Koha;
22
use C4::Koha;
23
use C4::Languages;
23
use C4::Languages;
24
use Koha::Database;
24
use Koha::Database;
25
use Koha::IssuingRules;
25
use Koha::CirculationRules;
26
use Koha::Localizations;
26
use Koha::Localizations;
27
27
28
use base qw(Koha::Object);
28
use base qw(Koha::Object);
Lines 119-125 sub may_article_request { Link Here
119
    my $itemtype = $self->itemtype;
119
    my $itemtype = $self->itemtype;
120
    my $category = $params->{categorycode};
120
    my $category = $params->{categorycode};
121
121
122
    my $guess = Koha::IssuingRules->guess_article_requestable_itemtypes({
122
    my $guess = Koha::CirculationRules->guess_article_requestable_itemtypes({
123
        $category ? ( categorycode => $category ) : (),
123
        $category ? ( categorycode => $category ) : (),
124
    });
124
    });
125
    return ( $guess->{ $itemtype // q{} } || $guess->{ '*' } ) ? 1 : q{};
125
    return ( $guess->{ $itemtype // q{} } || $guess->{ '*' } ) ? 1 : q{};
(-)a/admin/smart-rules.pl (-1 / +1 lines)
Lines 72-78 my $op = $input->param('op') || q{}; Link Here
72
my $language = C4::Languages::getlanguage();
72
my $language = C4::Languages::getlanguage();
73
73
74
my $cache = Koha::Caches->get_instance;
74
my $cache = Koha::Caches->get_instance;
75
$cache->clear_from_cache( Koha::IssuingRules::GUESSED_ITEMTYPES_KEY );
75
$cache->clear_from_cache( Koha::CirculationRules::GUESSED_ITEMTYPES_KEY );
76
76
77
if ($op eq 'delete') {
77
if ($op eq 'delete') {
78
    my $itemtype     = $input->param('itemtype');
78
    my $itemtype     = $input->param('itemtype');
(-)a/opac/opac-search.pl (-1 / +2 lines)
Lines 52-57 use C4::Tags qw(get_tags); Link Here
52
use C4::SocialData;
52
use C4::SocialData;
53
use C4::External::OverDrive;
53
use C4::External::OverDrive;
54
54
55
use Koha::CirculationRules;
55
use Koha::Libraries;
56
use Koha::Libraries;
56
use Koha::ItemTypes;
57
use Koha::ItemTypes;
57
use Koha::Ratings;
58
use Koha::Ratings;
Lines 669-675 for (my $i=0;$i<@servers;$i++) { Link Here
669
670
670
        my $art_req_itypes;
671
        my $art_req_itypes;
671
        if( C4::Context->preference('ArticleRequests') ) {
672
        if( C4::Context->preference('ArticleRequests') ) {
672
            $art_req_itypes = Koha::IssuingRules->guess_article_requestable_itemtypes({ $patron ? ( categorycode => $patron->categorycode ) : () });
673
            $art_req_itypes = Koha::CirculationRules->guess_article_requestable_itemtypes({ $patron ? ( categorycode => $patron->categorycode ) : () });
673
        }
674
        }
674
675
675
        foreach my $res (@newresults) {
676
        foreach my $res (@newresults) {
(-)a/t/db_dependent/ArticleRequests.t (-2 / +2 lines)
Lines 252-258 subtest 'may_article_request' => sub { Link Here
252
    # mocking
252
    # mocking
253
    t::lib::Mocks::mock_preference('ArticleRequests', 1);
253
    t::lib::Mocks::mock_preference('ArticleRequests', 1);
254
    t::lib::Mocks::mock_preference('ArticleRequestsLinkControl', 'calc');
254
    t::lib::Mocks::mock_preference('ArticleRequestsLinkControl', 'calc');
255
    $cache->set_in_cache( Koha::IssuingRules::GUESSED_ITEMTYPES_KEY, {
255
    $cache->set_in_cache( Koha::CirculationRules::GUESSED_ITEMTYPES_KEY, {
256
        '*'  => { 'CR' => 1 },
256
        '*'  => { 'CR' => 1 },
257
        'S'  => { '*'  => 1 },
257
        'S'  => { '*'  => 1 },
258
        'PT' => { 'BK' => 1 },
258
        'PT' => { 'BK' => 1 },
Lines 266-272 subtest 'may_article_request' => sub { Link Here
266
    is( $itemtype->may_article_request({ categorycode => 'PT' }), '1', 'Result should be true when LinkControl is set to always' );
266
    is( $itemtype->may_article_request({ categorycode => 'PT' }), '1', 'Result should be true when LinkControl is set to always' );
267
267
268
    # Cleanup
268
    # Cleanup
269
    $cache->clear_from_cache( Koha::IssuingRules::GUESSED_ITEMTYPES_KEY );
269
    $cache->clear_from_cache( Koha::CirculationRules::GUESSED_ITEMTYPES_KEY );
270
};
270
};
271
271
272
$schema->storage->txn_rollback();
272
$schema->storage->txn_rollback();
(-)a/t/db_dependent/Koha/IssuingRules/guess_article_requestable_itemtypes.t (-13 / +12 lines)
Lines 6-12 use Test::More tests => 1; Link Here
6
use t::lib::Mocks;
6
use t::lib::Mocks;
7
use t::lib::TestBuilder;
7
use t::lib::TestBuilder;
8
use Koha::Database;
8
use Koha::Database;
9
use Koha::IssuingRules;
9
use Koha::CirculationRules;
10
use Koha::Caches;
10
use Koha::Caches;
11
11
12
my $schema = Koha::Database->new->schema;
12
my $schema = Koha::Database->new->schema;
Lines 19-32 subtest 'guess_article_requestable_itemtypes' => sub { Link Here
19
19
20
    t::lib::Mocks::mock_preference('ArticleRequests', 1);
20
    t::lib::Mocks::mock_preference('ArticleRequests', 1);
21
    t::lib::Mocks::mock_preference('ArticleRequestsLinkControl', 'calc');
21
    t::lib::Mocks::mock_preference('ArticleRequestsLinkControl', 'calc');
22
    $cache->clear_from_cache( Koha::IssuingRules::GUESSED_ITEMTYPES_KEY );
22
    $cache->clear_from_cache( Koha::CirculationRules::GUESSED_ITEMTYPES_KEY );
23
    Koha::IssuingRules->delete;
23
    Koha::CirculationRules->delete;
24
    my $itype1 = $builder->build_object({ class => 'Koha::ItemTypes' });
24
    my $itype1 = $builder->build_object({ class => 'Koha::ItemTypes' });
25
    my $itype2 = $builder->build_object({ class => 'Koha::ItemTypes' });
25
    my $itype2 = $builder->build_object({ class => 'Koha::ItemTypes' });
26
    my $catg1 = $builder->build_object({ class => 'Koha::Patron::Categories' });
26
    my $catg1 = $builder->build_object({ class => 'Koha::Patron::Categories' });
27
    my $catg2 = $builder->build_object({ class => 'Koha::Patron::Categories' });
27
    my $catg2 = $builder->build_object({ class => 'Koha::Patron::Categories' });
28
    my $rule1 = $builder->build_object({
28
    my $rule1 = $builder->build_object({
29
        class => 'Koha::IssuingRules',
29
        class => 'Koha::CirculationRules',
30
        value => {
30
        value => {
31
            branchcode => 'MPL', # no worries: no FK
31
            branchcode => 'MPL', # no worries: no FK
32
            categorycode => '*',
32
            categorycode => '*',
Lines 35-41 subtest 'guess_article_requestable_itemtypes' => sub { Link Here
35
        },
35
        },
36
    });
36
    });
37
    my $rule2 = $builder->build_object({
37
    my $rule2 = $builder->build_object({
38
        class => 'Koha::IssuingRules',
38
        class => 'Koha::CirculationRules',
39
        value => {
39
        value => {
40
            branchcode => '*',
40
            branchcode => '*',
41
            categorycode => $catg1->categorycode,
41
            categorycode => $catg1->categorycode,
Lines 44-76 subtest 'guess_article_requestable_itemtypes' => sub { Link Here
44
        },
44
        },
45
    });
45
    });
46
46
47
    my $res = Koha::IssuingRules->guess_article_requestable_itemtypes;
47
    my $res = Koha::CirculationRules->guess_article_requestable_itemtypes;
48
    is( $res->{'*'}, undef, 'Item type * seems not permitted' );
48
    is( $res->{'*'}, undef, 'Item type * seems not permitted' );
49
    is( $res->{$itype1->itemtype}, 1, 'Item type 1 seems permitted' );
49
    is( $res->{$itype1->itemtype}, 1, 'Item type 1 seems permitted' );
50
    is( $res->{$itype2->itemtype}, 1, 'Item type 2 seems permitted' );
50
    is( $res->{$itype2->itemtype}, 1, 'Item type 2 seems permitted' );
51
    $res = Koha::IssuingRules->guess_article_requestable_itemtypes({ categorycode => $catg2->categorycode });
51
    $res = Koha::CirculationRules->guess_article_requestable_itemtypes({ categorycode => $catg2->categorycode });
52
    is( $res->{'*'}, undef, 'Item type * seems not permitted' );
52
    is( $res->{'*'}, undef, 'Item type * seems not permitted' );
53
    is( $res->{$itype1->itemtype}, 1, 'Item type 1 seems permitted' );
53
    is( $res->{$itype1->itemtype}, 1, 'Item type 1 seems permitted' );
54
    is( $res->{$itype2->itemtype}, undef, 'Item type 2 seems not permitted' );
54
    is( $res->{$itype2->itemtype}, undef, 'Item type 2 seems not permitted' );
55
55
56
    # Change the rules
56
    # Change the rules
57
    $rule2->itemtype('*')->store;
57
    $rule2->itemtype('*')->store;
58
    $cache->clear_from_cache( Koha::IssuingRules::GUESSED_ITEMTYPES_KEY );
58
    $cache->clear_from_cache( Koha::CirculationRules::GUESSED_ITEMTYPES_KEY );
59
    $res = Koha::IssuingRules->guess_article_requestable_itemtypes;
59
    $res = Koha::CirculationRules->guess_article_requestable_itemtypes;
60
    is( $res->{'*'}, 1, 'Item type * seems permitted' );
60
    is( $res->{'*'}, 1, 'Item type * seems permitted' );
61
    is( $res->{$itype1->itemtype}, 1, 'Item type 1 seems permitted' );
61
    is( $res->{$itype1->itemtype}, 1, 'Item type 1 seems permitted' );
62
    is( $res->{$itype2->itemtype}, undef, 'Item type 2 seems not permitted' );
62
    is( $res->{$itype2->itemtype}, undef, 'Item type 2 seems not permitted' );
63
    $res = Koha::IssuingRules->guess_article_requestable_itemtypes({ categorycode => $catg2->categorycode });
63
    $res = Koha::CirculationRules->guess_article_requestable_itemtypes({ categorycode => $catg2->categorycode });
64
    is( $res->{'*'}, undef, 'Item type * seems not permitted' );
64
    is( $res->{'*'}, undef, 'Item type * seems not permitted' );
65
    is( $res->{$itype1->itemtype}, 1, 'Item type 1 seems permitted' );
65
    is( $res->{$itype1->itemtype}, 1, 'Item type 1 seems permitted' );
66
    is( $res->{$itype2->itemtype}, undef, 'Item type 2 seems not permitted' );
66
    is( $res->{$itype2->itemtype}, undef, 'Item type 2 seems not permitted' );
67
67
68
    # Finally test the overriding pref
68
    # Finally test the overriding pref
69
    t::lib::Mocks::mock_preference('ArticleRequestsLinkControl', 'always');
69
    t::lib::Mocks::mock_preference('ArticleRequestsLinkControl', 'always');
70
    $res = Koha::IssuingRules->guess_article_requestable_itemtypes({});
70
    $res = Koha::CirculationRules->guess_article_requestable_itemtypes({});
71
    is( $res->{'*'}, 1, 'Override algorithm with pref setting' );
71
    is( $res->{'*'}, 1, 'Override algorithm with pref setting' );
72
72
73
    $cache->clear_from_cache( Koha::IssuingRules::GUESSED_ITEMTYPES_KEY );
73
    $cache->clear_from_cache( Koha::CirculationRules::GUESSED_ITEMTYPES_KEY );
74
};
74
};
75
75
76
$schema->storage->txn_rollback;
76
$schema->storage->txn_rollback;
77
- 

Return to bug 18936