1 package Koha::BackgroundJobs;
3 # This file is part of Koha.
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.
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.
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>.
20 use Koha::BackgroundJob;
22 use base qw(Koha::Objects);
26 Koha::BackgroundJobs - Koha BackgroundJob Object set class
34 my $background_jobs = Koha::BackgroundJobs->search_limited( $params, $attributes );
36 Returns all background jobs the logged in user should be allowed to see
41 my ( $self, $params, $attributes ) = @_;
43 my $can_manage_background_jobs;
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' } );
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 );
57 =head3 filter_by_current
59 my $current_jobs = $jobs->filter_by_current;
61 Returns a new resultset, filtering out finished jobs.
65 sub filter_by_current {
70 status => { not_in => [ 'cancelled', 'failed', 'finished' ] }
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
81 my $count = Koha::BackgroundJobs->purge( $params );
83 Deletes finished background jobs. Returns the number of jobs that was / would've been deleted.
88 my ( $self, $params) = @_;
90 return 0 unless ( exists $params->{job_types} && scalar @{ $params->{job_types} } >= 1 );
91 return 0 unless ( exists $params->{days} );
93 my $types = $params->{job_types};
94 my $days = $params->{days};
96 my $confirm = exists $params->{confirm} ? $params->{confirm} : 0;
99 if ( $types->[0] eq 'all' ){
102 ended_on => { '<' => \[ 'date_sub(curdate(), INTERVAL ? DAY)', $days ] },
103 status => 'finished',
109 ended_on => { '<' => \[ 'date_sub(curdate(), INTERVAL ? DAY)', $days ] },
111 status => 'finished',
114 my $count = $rs->count();
115 $rs->delete if $confirm;
120 =head2 Internal methods
127 return 'BackgroundJob';
135 return 'Koha::BackgroundJob';