Bug 30982: Add Koha::BackgroundJobs->filter_by_current
[koha.git] / Koha / BackgroundJobs.pm
1 package Koha::BackgroundJobs;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Koha::BackgroundJob;
21
22 use base qw(Koha::Objects);
23
24 =head1 NAME
25
26 Koha::BackgroundJobs - Koha BackgroundJob Object set class
27
28 =head1 API
29
30 =head2 Class methods
31
32 =head2 search_limited
33
34   my $background_jobs = Koha::BackgroundJobs->search_limited( $params, $attributes );
35
36 Returns all background jobs the logged in user should be allowed to see
37
38 =cut
39
40 sub search_limited {
41     my ( $self, $params, $attributes ) = @_;
42
43     # Assume permission if context has no user
44     my $can_manage_background_jobs = 1;
45
46     my $logged_in_user;
47     my $userenv = C4::Context->userenv;
48     if ( $userenv and $userenv->{number} ) {
49         $logged_in_user = Koha::Patrons->find( $userenv->{number} );
50         $can_manage_background_jobs = $logged_in_user->has_permission(
51             { parameters => 'manage_background_jobs' } );
52     }
53
54     return $can_manage_background_jobs
55       ? $self->search( $params, $attributes )
56       : $self->search( { borrowernumber => $logged_in_user->borrowernumber } )
57       ->search( $params, $attributes );
58 }
59
60 =head3 filter_by_current
61
62     my $current_jobs = $jobs->filter_by_current;
63
64 Returns a new resultset, filtering out finished jobs.
65
66 =cut
67
68 sub filter_by_current {
69     my ($self) = @_;
70
71     return $self->search(
72         {
73             status => { not_in => [ 'cancelled', 'failed', 'finished' ] }
74         }
75     );
76 }
77
78 =head2 Internal methods
79
80 =head3 _type
81
82 =cut
83
84 sub _type {
85     return 'BackgroundJob';
86 }
87
88 =head3 object_class
89
90 =cut
91
92 sub object_class {
93     return 'Koha::BackgroundJob';
94 }
95
96 1;