Bug 20941: (QA follow-up) Fix return value of get_type, make templates more consistant
[koha_ffzg] / Koha / Illrequest.pm
index b6585c2..4dc64ae 100644 (file)
@@ -29,6 +29,7 @@ use Try::Tiny;
 use Koha::Database;
 use Koha::Email;
 use Koha::Exceptions::Ill;
+use Koha::Illcomments;
 use Koha::Illrequestattributes;
 use Koha::Patron;
 
@@ -119,6 +120,17 @@ sub illrequestattributes {
     );
 }
 
+=head3 illcomments
+
+=cut
+
+sub illcomments {
+    my ( $self ) = @_;
+    return Koha::Illcomments->_new_from_dbic(
+        scalar $self->_result->illcomments
+    );
+}
+
 =head3 patron
 
 =cut
@@ -478,8 +490,8 @@ Return a list of available backends.
 
 sub available_backends {
     my ( $self ) = @_;
-    my @backends = $self->_config->available_backends;
-    return \@backends;
+    my $backends = $self->_config->available_backends;
+    return $backends;
 }
 
 =head3 available_actions
@@ -600,23 +612,24 @@ sub backend_create {
     my ( $self, $params ) = @_;
 
     # Establish whether we need to do a generic copyright clearance.
-    if ( ( !$params->{stage} || $params->{stage} eq 'init' )
-             && C4::Context->preference("ILLModuleCopyrightClearance") ) {
-        return {
-            error   => 0,
-            status  => '',
-            message => '',
-            method  => 'create',
-            stage   => 'copyrightclearance',
-            value   => {
-                backend => $self->_backend->name
-            }
-        };
-    } elsif (     defined $params->{stage}
-               && $params->{stage} eq 'copyrightclearance' ) {
-        $params->{stage} = 'init';
+    if ($params->{opac}) {
+        if ( ( !$params->{stage} || $params->{stage} eq 'init' )
+                && C4::Context->preference("ILLModuleCopyrightClearance") ) {
+            return {
+                error   => 0,
+                status  => '',
+                message => '',
+                method  => 'create',
+                stage   => 'copyrightclearance',
+                value   => {
+                    backend => $self->_backend->name
+                }
+            };
+        } elsif (     defined $params->{stage}
+                && $params->{stage} eq 'copyrightclearance' ) {
+            $params->{stage} = 'init';
+        }
     }
-
     # First perform API action, then...
     my $args = {
         request => $self,
@@ -724,6 +737,21 @@ sub getPrefix {
         || "";                  # "the empty prefix"
 }
 
+=head3 get_type
+
+    my $type = $abstract->get_type();
+
+Return a string representing the material type of this request or undef
+
+=cut
+
+sub get_type {
+    my ($self) = @_;
+    my $attr = $self->illrequestattributes->find({ type => 'type'});
+    return if !$attr;
+    return $attr->value;
+};
+
 #### Illrequests Imports
 
 =head3 check_limits
@@ -1023,6 +1051,10 @@ sub TO_JSON {
                 $self->branchcode
             )->TO_JSON;
         }
+        # Augment the request response with the number of comments if appropriate
+        if ( $embed->{comments} ) {
+            $object->{comments} = $self->illcomments->count;
+        }
     }
 
     return $object;