use base qw(Koha::Objects);
+use constant GUESSED_ITEMTYPES_KEY => 'Koha_IssuingRules_last_guess';
+
=head1 NAME
Koha::CirculationRules - Koha CirculationRule Object set class
return $rule ? $rule->rule_value : undef;
}
+=head3 article_requestable_rules
+
+ Return rules that allow article requests, optionally filtered by
+ patron categorycode.
+
+ Use with care; see guess_article_requestable_itemtypes.
+
+=cut
+
+sub article_requestable_rules {
+ my ( $class, $params ) = @_;
+ my $category = $params->{categorycode};
+
+ return if !C4::Context->preference('ArticleRequests');
+ return $class->search({
+ $category ? ( categorycode => [ $category, '*' ] ) : (),
+ rule_name => 'article_requests',
+ rule_value => { '!=' => 'no' },
+ });
+}
+
+=head3 guess_article_requestable_itemtypes
+
+ Return item types in a hashref that are likely possible to be
+ 'article requested'. Constructed by an intelligent guess in the
+ issuing rules (see article_requestable_rules).
+
+ Note: pref ArticleRequestsLinkControl overrides the algorithm.
+
+ Optional parameters: categorycode.
+
+ Note: the routine is used in opac-search to obtain a reasonable
+ estimate within performance borders (not looking at all items but
+ just using default itemtype). Also we are not looking at the
+ branchcode here, since home or holding branch of the item is
+ leading and branch may be unknown too (anonymous opac session).
+
+=cut
+
+sub guess_article_requestable_itemtypes {
+ my ( $class, $params ) = @_;
+ my $category = $params->{categorycode};
+ return {} if !C4::Context->preference('ArticleRequests');
+ return { '*' => 1 } if C4::Context->preference('ArticleRequestsLinkControl') eq 'always';
+
+ my $cache = Koha::Caches->get_instance;
+ my $last_article_requestable_guesses = $cache->get_from_cache(GUESSED_ITEMTYPES_KEY);
+ my $key = $category || '*';
+ return $last_article_requestable_guesses->{$key}
+ if $last_article_requestable_guesses && exists $last_article_requestable_guesses->{$key};
+
+ my $res = {};
+ my $rules = $class->article_requestable_rules({
+ $category ? ( categorycode => $category ) : (),
+ });
+ return $res if !$rules;
+ foreach my $rule ( $rules->as_list ) {
+ $res->{ $rule->itemtype } = 1;
+ }
+ $last_article_requestable_guesses->{$key} = $res;
+ $cache->set_in_cache(GUESSED_ITEMTYPES_KEY, $last_article_requestable_guesses);
+ return $res;
+}
+
+
=head3 type
=cut
use C4::Koha;
use C4::Languages;
use Koha::Database;
-use Koha::IssuingRules;
+use Koha::CirculationRules;
use Koha::Localizations;
use base qw(Koha::Object Koha::Object::Limit::Library);
my $itemtype = $self->itemtype;
my $category = $params->{categorycode};
- my $guess = Koha::IssuingRules->guess_article_requestable_itemtypes({
+ my $guess = Koha::CirculationRules->guess_article_requestable_itemtypes({
$category ? ( categorycode => $category ) : (),
});
return ( $guess->{ $itemtype // q{} } || $guess->{ '*' } ) ? 1 : q{};
my $language = C4::Languages::getlanguage();
my $cache = Koha::Caches->get_instance;
-$cache->clear_from_cache( Koha::IssuingRules::GUESSED_ITEMTYPES_KEY );
+$cache->clear_from_cache( Koha::CirculationRules::GUESSED_ITEMTYPES_KEY );
if ($op eq 'delete') {
my $itemtype = $input->param('itemtype');
use C4::SocialData;
use C4::External::OverDrive;
+use Koha::CirculationRules;
use Koha::Libraries;
use Koha::ItemTypes;
use Koha::Ratings;
my $art_req_itypes;
if( C4::Context->preference('ArticleRequests') ) {
- $art_req_itypes = Koha::IssuingRules->guess_article_requestable_itemtypes({ $patron ? ( categorycode => $patron->categorycode ) : () });
+ $art_req_itypes = Koha::CirculationRules->guess_article_requestable_itemtypes({ $patron ? ( categorycode => $patron->categorycode ) : () });
}
foreach my $res (@newresults) {
# mocking
t::lib::Mocks::mock_preference('ArticleRequests', 1);
t::lib::Mocks::mock_preference('ArticleRequestsLinkControl', 'calc');
- $cache->set_in_cache( Koha::IssuingRules::GUESSED_ITEMTYPES_KEY, {
+ $cache->set_in_cache( Koha::CirculationRules::GUESSED_ITEMTYPES_KEY, {
'*' => { 'CR' => 1 },
'S' => { '*' => 1 },
'PT' => { 'BK' => 1 },
is( $itemtype->may_article_request({ categorycode => 'PT' }), '1', 'Result should be true when LinkControl is set to always' );
# Cleanup
- $cache->clear_from_cache( Koha::IssuingRules::GUESSED_ITEMTYPES_KEY );
+ $cache->clear_from_cache( Koha::CirculationRules::GUESSED_ITEMTYPES_KEY );
};
$schema->storage->txn_rollback();
use t::lib::Mocks;
use t::lib::TestBuilder;
use Koha::Database;
-use Koha::IssuingRules;
+use Koha::CirculationRules;
use Koha::Caches;
my $schema = Koha::Database->new->schema;
t::lib::Mocks::mock_preference('ArticleRequests', 1);
t::lib::Mocks::mock_preference('ArticleRequestsLinkControl', 'calc');
- $cache->clear_from_cache( Koha::IssuingRules::GUESSED_ITEMTYPES_KEY );
- Koha::IssuingRules->delete;
+ $cache->clear_from_cache( Koha::CirculationRules::GUESSED_ITEMTYPES_KEY );
+ Koha::CirculationRules->delete;
my $itype1 = $builder->build_object({ class => 'Koha::ItemTypes' });
my $itype2 = $builder->build_object({ class => 'Koha::ItemTypes' });
my $catg1 = $builder->build_object({ class => 'Koha::Patron::Categories' });
my $catg2 = $builder->build_object({ class => 'Koha::Patron::Categories' });
my $rule1 = $builder->build_object({
- class => 'Koha::IssuingRules',
+ class => 'Koha::CirculationRules',
value => {
branchcode => 'MPL', # no worries: no FK
categorycode => '*',
},
});
my $rule2 = $builder->build_object({
- class => 'Koha::IssuingRules',
+ class => 'Koha::CirculationRules',
value => {
branchcode => '*',
categorycode => $catg1->categorycode,
},
});
- my $res = Koha::IssuingRules->guess_article_requestable_itemtypes;
+ my $res = Koha::CirculationRules->guess_article_requestable_itemtypes;
is( $res->{'*'}, undef, 'Item type * seems not permitted' );
is( $res->{$itype1->itemtype}, 1, 'Item type 1 seems permitted' );
is( $res->{$itype2->itemtype}, 1, 'Item type 2 seems permitted' );
- $res = Koha::IssuingRules->guess_article_requestable_itemtypes({ categorycode => $catg2->categorycode });
+ $res = Koha::CirculationRules->guess_article_requestable_itemtypes({ categorycode => $catg2->categorycode });
is( $res->{'*'}, undef, 'Item type * seems not permitted' );
is( $res->{$itype1->itemtype}, 1, 'Item type 1 seems permitted' );
is( $res->{$itype2->itemtype}, undef, 'Item type 2 seems not permitted' );
# Change the rules
$rule2->itemtype('*')->store;
- $cache->clear_from_cache( Koha::IssuingRules::GUESSED_ITEMTYPES_KEY );
- $res = Koha::IssuingRules->guess_article_requestable_itemtypes;
+ $cache->clear_from_cache( Koha::CirculationRules::GUESSED_ITEMTYPES_KEY );
+ $res = Koha::CirculationRules->guess_article_requestable_itemtypes;
is( $res->{'*'}, 1, 'Item type * seems permitted' );
is( $res->{$itype1->itemtype}, 1, 'Item type 1 seems permitted' );
is( $res->{$itype2->itemtype}, undef, 'Item type 2 seems not permitted' );
- $res = Koha::IssuingRules->guess_article_requestable_itemtypes({ categorycode => $catg2->categorycode });
+ $res = Koha::CirculationRules->guess_article_requestable_itemtypes({ categorycode => $catg2->categorycode });
is( $res->{'*'}, undef, 'Item type * seems not permitted' );
is( $res->{$itype1->itemtype}, 1, 'Item type 1 seems permitted' );
is( $res->{$itype2->itemtype}, undef, 'Item type 2 seems not permitted' );
# Finally test the overriding pref
t::lib::Mocks::mock_preference('ArticleRequestsLinkControl', 'always');
- $res = Koha::IssuingRules->guess_article_requestable_itemtypes({});
+ $res = Koha::CirculationRules->guess_article_requestable_itemtypes({});
is( $res->{'*'}, 1, 'Override algorithm with pref setting' );
- $cache->clear_from_cache( Koha::IssuingRules::GUESSED_ITEMTYPES_KEY );
+ $cache->clear_from_cache( Koha::CirculationRules::GUESSED_ITEMTYPES_KEY );
};
$schema->storage->txn_rollback;