Bug 18591: Allow any number of comments on ILLs
authorMagnus Enger <magnus@libriotech.no>
Mon, 20 Nov 2017 14:14:30 +0000 (14:14 +0000)
committerNick Clemens <nick@bywatersolutions.com>
Sat, 27 Oct 2018 13:15:58 +0000 (13:15 +0000)
This patch makes it possible to add arbitrary comments to ILL
requests. Comments are read only and displayed in chronological
order. Comments can be added by librarians, but also added automatically
based on comments in the different protocols, so that comments from the
lending library can also be added.

To test:
- Enable the ILL module ("ILLModule" syspref + config in koha-conf.xml)
- Install the Dummy backend from here:
  https://github.com/PTFS-Europe/Dummy
- Create a Dummy ILL request
- Add comments to the request, checking that the name and borrowernumber
  of the person that added the comment is displayed ok
- Check that the displayed date and time the comment was submitted follows
  the "dateformat" syspref
- On the "List requests" page, check that the correct number of comments
  is displayed in the "Comments" column
- Check that the tests pass:
  prove t/db_dependent/Illcomments.t

Signed-off-by: andrew.isherwood@ptfs-europe.com

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>

Koha/Illcomment.pm [new file with mode: 0644]
Koha/Illcomments.pm [new file with mode: 0644]
Koha/Illrequest.pm
ill/ill-requests.pl
koha-tmpl/intranet-tmpl/prog/en/modules/ill/ill-requests.tt
t/db_dependent/Illcomments.t [new file with mode: 0644]

diff --git a/Koha/Illcomment.pm b/Koha/Illcomment.pm
new file mode 100644 (file)
index 0000000..931f235
--- /dev/null
@@ -0,0 +1,58 @@
+package Koha::Illcomment;
+
+# Copyright Magnus Enger Libriotech 2017
+#
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation; either version 3 of the License, or (at your option) any later
+# version.
+#
+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
+# details.
+#
+# You should have received a copy of the GNU General Public License along with
+# Koha; if not, write to the Free Software Foundation, Inc., 51 Franklin
+# Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+use Modern::Perl;
+use Koha::Database;
+use base qw(Koha::Object);
+
+=head1 NAME
+
+Koha::Illcomment - Koha Illcomment Object class
+
+=head2 Class methods
+
+=head3 patron
+
+=cut
+
+sub patron {
+    my ( $self ) = @_;
+    return Koha::Patron->_new_from_dbic(
+        scalar $self->_result->borrowernumber
+    );
+}
+
+=head2 Internal methods
+
+=head3 _type
+
+=cut
+
+sub _type {
+    return 'Illcomment';
+}
+
+=head1 AUTHOR
+
+Magnus Enger <magnus@libriotech.no>
+
+=cut
+
+1;
diff --git a/Koha/Illcomments.pm b/Koha/Illcomments.pm
new file mode 100644 (file)
index 0000000..a0f18e3
--- /dev/null
@@ -0,0 +1,18 @@
+package Koha::Illcomments;
+
+# TODO Add POD
+
+use Modern::Perl;
+use Koha::Database;
+use Koha::Illcomment;
+use base qw(Koha::Objects);
+
+sub _type {
+    return 'Illcomments';
+}
+
+sub object_class {
+    return 'Koha::Illcomment';
+}
+
+1;
index cc61a87..cc8e321 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
@@ -1024,6 +1036,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;
index 877db4d..a2e898a 100755 (executable)
@@ -24,8 +24,10 @@ use CGI;
 use C4::Auth;
 use C4::Output;
 use Koha::AuthorisedValues;
+use Koha::Illcomment;
 use Koha::Illrequests;
 use Koha::Libraries;
+use Koha::Token;
 
 use Try::Tiny;
 
@@ -63,7 +65,10 @@ if ( $backends_available ) {
         my $request = Koha::Illrequests->find($params->{illrequest_id});
 
         $template->param(
-            request => $request
+            request    => $request,
+            csrf_token => Koha::Token->new->generate_csrf({
+                session_id => scalar $cgi->cookie('CGISESSID'),
+            }),
         );
 
     } elsif ( $op eq 'create' ) {
@@ -235,6 +240,24 @@ if ( $backends_available ) {
                 prefilters => $active_filters
             );
         }
+
+    } elsif ( $op eq "save_comment" ) {
+        die "Wrong CSRF token" unless Koha::Token->new->check_csrf({
+           session_id => scalar $cgi->cookie('CGISESSID'),
+           token      => scalar $cgi->param('csrf_token'),
+        });
+        my $comment = Koha::Illcomment->new({
+            illrequest_id  => scalar $params->{illrequest_id},
+            borrowernumber => $patronnumber,
+            comment        => scalar $params->{comment},
+        });
+        $comment->store();
+        # Redirect to view the whole request
+        print $cgi->redirect("/cgi-bin/koha/ill/ill-requests.pl?method=illview&illrequest_id=".
+            scalar $params->{illrequest_id}
+        );
+        exit;
+
     } else {
         my $request = Koha::Illrequests->find($params->{illrequest_id});
         my $backend_result = $request->custom_capability($op, $params);
index 2d7d68d..8ff3c44 100644 (file)
@@ -51,7 +51,8 @@
             'status',
             'updated',
             'illrequest_id',
-            'action'
+            'comments',
+            'action' // Action should always be last
         ];
 
         // Remove any fields we're ignoring
             }
         };
 
+        // Toggle request attributes in Illview
+        $('#toggle_requestattributes').on('click', function(e) {
+            e.preventDefault();
+            $('#requestattributes').toggleClass('content_hidden');
+        });
+
+        // Toggle new comment form in Illview
+        $('#toggle_addcomment').on('click', function(e) {
+            e.preventDefault();
+            $('#addcomment').toggleClass('content_hidden');
+        });
+
         // Filter partner list
         $('#partner_filter').keyup(function() {
             var needle = $('#partner_filter').val();
         // Get our data from the API and process it prior to passing
         // it to datatables
         var ajax = $.ajax(
-            '/api/v1/illrequests?embed=metadata,patron,capabilities,library'
+            '/api/v1/illrequests?embed=metadata,patron,capabilities,library,comments'
             ).done(function() {
                 var data = JSON.parse(ajax.responseText);
                 // Make a copy, we'll be removing columns next and need
                         </div>
                     </div>
 
+                    <div id="ill-view-panel" class="panel panel-default">
+                        <div class="panel-heading">
+                            <h3>[% request.illcomments.count %] comments</h3>
+                        </div>
+                        <div class="panel-body">
+                            [% IF request.illcomments.count && request.illcomments.count > 0 %]
+                                [% FOREACH comment IN request.illcomments %]
+                                    <div class="rows comment_[% comment.patron.categorycode %]">
+                                    <h5>Comment by:
+                                    <a href="[% borrowerlink %]" title="View borrower details">
+                                    [% comment.patron.firstname _ " " _ comment.patron.surname _ " [" _ comment.patron.cardnumber _ "]" | html %]</a>
+                                    [% comment.timestamp | $KohaDates with_hours => 1 %]</h5>
+                                    <p>[% comment.comment | html %]</p>
+                                    </div>
+                                [% END %]
+                            [% END %]
+                                <div class="rows">
+                                    <h3><a id="toggle_addcomment" href="#">Add comment</a></h3>
+                                    <div id="addcomment" class="content_hidden">
+                                        <form class="validated" method="post" action="/cgi-bin/koha/ill/ill-requests.pl">
+                                            <input type="hidden" value="save_comment" name="method">
+                                            <input type="hidden" value="[% csrf_token %]" name="csrf_token">
+                                            <input type="hidden" value="[% request.illrequest_id %]" name="illrequest_id">
+                                            <fieldset class="rows">
+                                                <ol>
+                                                    <li>
+                                                        <label class="required" for="comment">Comment: </label>
+                                                        <textarea type="text" class="required" required="required" value="" cols="80" rows="10" id="comment" name="comment"></textarea>
+                                                        <span class="required">Required</span>
+                                                    </li>
+                                                </ol>
+                                            </fieldset>
+                                            <fieldset class="action">
+                                                <input type="submit" value="Submit">
+                                            </fieldset>
+                                        </form>
+                                    </div>
+                                </div>
+                            </div>
+                    </div>
+
                 [% ELSIF query_type == 'illlist' %]
                     <!-- illlist -->
                     <h1>View ILL requests</h1>
                                     <th>Status</th>
                                     <th>Updated on</th>
                                     <th>Request number</th>
+                                    <th>Comments</th>
                                     <th class="actions"></th>
                                 </tr>
                             </thead>
diff --git a/t/db_dependent/Illcomments.t b/t/db_dependent/Illcomments.t
new file mode 100644 (file)
index 0000000..85cc630
--- /dev/null
@@ -0,0 +1,84 @@
+#!/usr/bin/perl
+
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
+
+use Modern::Perl;
+
+use File::Basename qw/basename/;
+use Koha::Database;
+use Koha::Illrequests;
+use Koha::Illrequestattributes;
+use Koha::Illrequest::Config;
+use Koha::Patrons;
+use t::lib::Mocks;
+use t::lib::TestBuilder;
+use Test::MockObject;
+use Test::MockModule;
+
+use Test::More tests => 9;
+
+my $schema = Koha::Database->new->schema;
+my $builder = t::lib::TestBuilder->new;
+use_ok('Koha::Illcomment');
+use_ok('Koha::Illcomments');
+
+$schema->storage->txn_begin;
+
+Koha::Illrequests->search->delete;
+
+# Create a patron
+my $patron = $builder->build({ source => 'Borrower' });
+
+# Create an ILL request
+my $illrq = $builder->build({
+    source => 'Illrequest',
+    value => { borrowernumber => $patron->{borrowernumber} }
+});
+my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
+isa_ok( $illrq_obj, 'Koha::Illrequest' );
+
+# Create a librarian
+my $librarian = $builder->build({ source => 'Borrower' });
+
+# Create a comment and tie it to the request and the librarian
+my $comment_text = 'xyz';
+my $illcomment = $builder->build({
+    source => 'Illcomment',
+    value => {
+        illrequest_id  => $illrq_obj->illrequest_id,
+        borrowernumber => $librarian->{borrowernumber},
+        comment        => $comment_text,
+    }
+});
+
+# Get all the comments
+my $comments = $illrq_obj->illcomments;
+isa_ok( $comments, 'Koha::Illcomments', "Illcomments" );
+my @comments_list = $comments->as_list();
+is( scalar @comments_list, 1, "We have 1 comment" );
+
+# Get the first (and only) comment
+my $comment = $comments->next();
+isa_ok( $comment, 'Koha::Illcomment', "Illcomment" );
+
+# Check the different data in the comment
+is( $comment->illrequest_id,  $illrq_obj->illrequest_id,    'illrequest_id getter works' );
+is( $comment->borrowernumber, $librarian->{borrowernumber}, 'borrowernumber getter works');
+is( $comment->comment,        $comment_text,                'comment getter works');
+
+$illrq_obj->delete;
+
+$schema->storage->txn_rollback;