Bug 32824: Fix cataloguing/value_builder/unimarc_field_100.pl
[koha-ffzg.git] / Koha / BackgroundJobs.pm
index d2ff223..48afbf6 100644 (file)
@@ -16,19 +16,64 @@ package Koha::BackgroundJobs;
 # along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 use Modern::Perl;
-use base qw(Koha::Objects);
+
 use Koha::BackgroundJob;
 
+use base qw(Koha::Objects);
+
 =head1 NAME
 
 Koha::BackgroundJobs - Koha BackgroundJob Object set class
 
 =head1 API
 
-=head2 Class Methods
+=head2 Class methods
+
+=head3 search_limited
+
+  my $background_jobs = Koha::BackgroundJobs->search_limited( $params, $attributes );
+
+Returns all background jobs the logged in user should be allowed to see
+
+=cut
+
+sub search_limited {
+    my ( $self, $params, $attributes ) = @_;
+
+    my $can_manage_background_jobs;
+    my $logged_in_user;
+    my $userenv = C4::Context->userenv;
+    if ( $userenv and $userenv->{number} ) {
+        $logged_in_user = Koha::Patrons->find( $userenv->{number} );
+        $can_manage_background_jobs = $logged_in_user->has_permission(
+            { parameters => 'manage_background_jobs' } );
+    }
+
+    return $self->search( $params, $attributes ) if $can_manage_background_jobs;
+    my $id = $logged_in_user ? $logged_in_user->borrowernumber : undef;
+    return $self->search({ borrowernumber => $id  })->search( $params, $attributes );
+}
+
+=head3 filter_by_current
+
+    my $current_jobs = $jobs->filter_by_current;
+
+Returns a new resultset, filtering out finished jobs.
 
 =cut
 
+sub filter_by_current {
+    my ($self) = @_;
+
+    return $self->search(
+        {
+            status => { not_in => [ 'cancelled', 'failed', 'finished' ] }
+        }
+    );
+}
+
+=head2 Internal methods
+
 =head3 _type
 
 =cut