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

(-)a/Koha/ArticleRequests.pm (-8 / +31 lines)
Lines 38-43 Koha::ArticleRequests - Koha ArticleRequests Object class Link Here
38
38
39
=cut
39
=cut
40
40
41
=head3 search_limited
42
43
my $article_requests = Koha::ArticleRequests->search_limited( $params, $attributes );
44
45
Search for article requests according to logged in patron restrictions
46
47
=cut
48
49
sub search_limited {
50
    my ( $self, $params, $attributes ) = @_;
51
52
    my $userenv = C4::Context->userenv;
53
    my @restricted_branchcodes;
54
    if ( $userenv ) {
55
        my $logged_in_user = Koha::Patrons->find( $userenv->{number} );
56
        @restricted_branchcodes = $logged_in_user->libraries_where_can_see_patrons;
57
    }
58
    # TODO This 'borrowernumber' relation name is confusing and needs to be renamed
59
    $params->{'borrowernumber.branchcode'} = { -in => \@restricted_branchcodes } if @restricted_branchcodes;
60
    $attributes->{join} = 'borrowernumber';
61
    return $self->search( $params, $attributes );
62
}
63
41
=head3 pending
64
=head3 pending
42
65
43
=cut
66
=cut
Lines 45-52 Koha::ArticleRequests - Koha ArticleRequests Object class Link Here
45
sub pending {
68
sub pending {
46
    my ( $self, $branchcode ) = @_;
69
    my ( $self, $branchcode ) = @_;
47
    my $params = { status => Koha::ArticleRequest::Status::Pending };
70
    my $params = { status => Koha::ArticleRequest::Status::Pending };
48
    $params->{branchcode} = $branchcode if $branchcode;
71
    $params->{'me.branchcode'} = $branchcode if $branchcode;
49
    return Koha::ArticleRequests->search( $params );
72
    return Koha::ArticleRequests->search_limited( $params );
50
}
73
}
51
74
52
=head3 processing
75
=head3 processing
Lines 56-63 sub pending { Link Here
56
sub processing {
79
sub processing {
57
    my ( $self, $branchcode ) = @_;
80
    my ( $self, $branchcode ) = @_;
58
    my $params = { status => Koha::ArticleRequest::Status::Processing };
81
    my $params = { status => Koha::ArticleRequest::Status::Processing };
59
    $params->{branchcode} = $branchcode if $branchcode;
82
    $params->{'me.branchcode'} = $branchcode if $branchcode;
60
    return Koha::ArticleRequests->search( $params );
83
    return Koha::ArticleRequests->search_limited( $params );
61
}
84
}
62
85
63
=head3 completed
86
=head3 completed
Lines 67-74 sub processing { Link Here
67
sub completed {
90
sub completed {
68
    my ( $self, $branchcode ) = @_;
91
    my ( $self, $branchcode ) = @_;
69
    my $params = { status => Koha::ArticleRequest::Status::Completed };
92
    my $params = { status => Koha::ArticleRequest::Status::Completed };
70
    $params->{branchcode} = $branchcode if $branchcode;
93
    $params->{'me.branchcode'} = $branchcode if $branchcode;
71
    return Koha::ArticleRequests->search( $params );
94
    return Koha::ArticleRequests->search_limited( $params );
72
}
95
}
73
96
74
=head3 canceled
97
=head3 canceled
Lines 78-85 sub completed { Link Here
78
sub canceled {
101
sub canceled {
79
    my ( $self, $branchcode ) = @_;
102
    my ( $self, $branchcode ) = @_;
80
    my $params = { status => Koha::ArticleRequest::Status::Canceled };
103
    my $params = { status => Koha::ArticleRequest::Status::Canceled };
81
    $params->{branchcode} = $branchcode if $branchcode;
104
    $params->{'me.branchcode'} = $branchcode if $branchcode;
82
    return Koha::ArticleRequests->search( $params );
105
    return Koha::ArticleRequests->search_limited( $params );
83
}
106
}
84
107
85
=head3 _type
108
=head3 _type
(-)a/t/db_dependent/ArticleRequests.t (-2 / +32 lines)
Lines 19-31 use Modern::Perl; Link Here
19
19
20
use POSIX qw(strftime);
20
use POSIX qw(strftime);
21
21
22
use Test::More tests => 49;
22
use Test::More tests => 50;
23
use Koha::Database;
23
use Koha::Database;
24
24
25
use Koha::Biblio;
25
use Koha::Biblio;
26
use Koha::Patron;
26
use Koha::Patron;
27
use Koha::Library;
27
use Koha::Library;
28
28
29
use t::lib::TestBuilder;
30
29
BEGIN {
31
BEGIN {
30
    use_ok('Koha::ArticleRequest');
32
    use_ok('Koha::ArticleRequest');
31
    use_ok('Koha::ArticleRequests');
33
    use_ok('Koha::ArticleRequests');
Lines 35-40 BEGIN { Link Here
35
my $schema = Koha::Database->new()->schema();
37
my $schema = Koha::Database->new()->schema();
36
$schema->storage->txn_begin();
38
$schema->storage->txn_begin();
37
39
40
my $builder = t::lib::TestBuilder->new;
38
my $dbh = C4::Context->dbh;
41
my $dbh = C4::Context->dbh;
39
$dbh->{RaiseError} = 1;
42
$dbh->{RaiseError} = 1;
40
43
Lines 65-73 my $patron = Koha::Patron->new( Link Here
65
    {
68
    {
66
        categorycode => $category->id,
69
        categorycode => $category->id,
67
        branchcode   => $branch->id,
70
        branchcode   => $branch->id,
71
        flags        => 1,# superlibrarian
68
    }
72
    }
69
)->store();
73
)->store();
70
ok( $patron->id, 'Koha::Patron created' );
74
ok( $patron->id, 'Koha::Patron created' );
75
my $patron_2 = $builder->build({ source => 'Borrower' });
76
$patron_2 = Koha::Patrons->find( $patron_2->{borrowernumber} );
77
78
my $nb_article_requests = Koha::ArticleRequests->count;
71
79
72
my $article_request = Koha::ArticleRequest->new(
80
my $article_request = Koha::ArticleRequest->new(
73
    {
81
    {
Lines 173-176 ok( !$item->can_article_request($patron), 'Item is not requestable with rule t Link Here
173
is( $item->article_request_type($patron), 'no', 'Item article request type is no' );
181
is( $item->article_request_type($patron), 'no', 'Item article request type is no' );
174
$rule->delete();
182
$rule->delete();
175
183
184
subtest 'search_limited' => sub {
185
    plan tests => 2;
186
    C4::Context->_new_userenv('xxx');
187
    my $group_1 = Koha::Library::Group->new( { title => 'TEST Group 1' } )->store;
188
    my $group_2 = Koha::Library::Group->new( { title => 'TEST Group 2' } )->store;
189
    Koha::Library::Group->new({ parent_id => $group_1->id,  branchcode => $patron->branchcode })->store();
190
    Koha::Library::Group->new({ parent_id => $group_2->id,  branchcode => $patron_2->branchcode })->store();
191
    set_logged_in_user( $patron ); # Is superlibrarian
192
    is( Koha::ArticleRequests->count, 3, 'Koha::ArticleRequests should return all article requests' );
193
    is( Koha::ArticleRequests->search_limited->count, 2, 'Koha::ArticleRequests->search_limited should return reviews depending on patron permissions' );
194
};
195
176
$schema->storage->txn_rollback();
196
$schema->storage->txn_rollback();
177
- 
197
198
sub set_logged_in_user {
199
    my ($patron) = @_;
200
    C4::Context->set_userenv(
201
        $patron->borrowernumber, $patron->userid,
202
        $patron->cardnumber,     'firstname',
203
        'surname',               $patron->library->branchcode,
204
        'Midway Public Library', $patron->flags,
205
        '',                      ''
206
    );
207
}

Return to bug 18403