Bug 31896: New after_recall_action hook
[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 =head3 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     my $can_manage_background_jobs;
44     my $logged_in_user;
45     my $userenv = C4::Context->userenv;
46     if ( $userenv and $userenv->{number} ) {
47         $logged_in_user = Koha::Patrons->find( $userenv->{number} );
48         $can_manage_background_jobs = $logged_in_user->has_permission(
49             { parameters => 'manage_background_jobs' } );
50     }
51
52     return $self->search( $params, $attributes ) if $can_manage_background_jobs;
53     my $id = $logged_in_user ? $logged_in_user->borrowernumber : undef;
54     return $self->search({ borrowernumber => $id  })->search( $params, $attributes );
55 }
56
57 =head3 filter_by_current
58
59     my $current_jobs = $jobs->filter_by_current;
60
61 Returns a new resultset, filtering out finished jobs.
62
63 =cut
64
65 sub filter_by_current {
66     my ($self) = @_;
67
68     return $self->search(
69         {
70             status => { not_in => [ 'cancelled', 'failed', 'finished' ] }
71         }
72     );
73 }
74
75 =head3 purge
76
77     my $params = { job_types => ('all') , # Arrayref of jobtypes to be purged
78                    days      => 1,        # Age in days of jobs to be purged
79                    confirm   => 1,        # Confirm deletion
80                 };
81     my $count = Koha::BackgroundJobs->purge( $params );
82
83 Deletes finished background jobs. Returns the number of jobs that was / would've been deleted.
84
85 =cut
86
87 sub purge {
88     my ( $self, $params) = @_;
89
90     return 0 unless ( exists $params->{job_types} && scalar @{ $params->{job_types} }  >= 1 );
91     return 0 unless ( exists $params->{days} );
92
93     my $types = $params->{job_types};
94     my $days  = $params->{days};
95
96     my $confirm = exists $params->{confirm} ? $params->{confirm} : 0;
97
98     my $rs;
99     if ( $types->[0] eq 'all' ){
100         $rs = $self->search(
101             {
102                 ended_on => { '<' => \[ 'date_sub(curdate(), INTERVAL ? DAY)', $days ] },
103                 status   => 'finished',
104             });
105
106     } else {
107         $rs = $self->search(
108             {
109                 ended_on => { '<' => \[ 'date_sub(curdate(), INTERVAL ? DAY)', $days ] },
110                 type     => $types,
111                 status   => 'finished',
112             });
113     }
114     my $count = $rs->count();
115     $rs->delete if $confirm;
116
117     return $count;
118 }
119
120 =head2 Internal methods
121
122 =head3 _type
123
124 =cut
125
126 sub _type {
127     return 'BackgroundJob';
128 }
129
130 =head3 object_class
131
132 =cut
133
134 sub object_class {
135     return 'Koha::BackgroundJob';
136 }
137
138 1;