Bug 18591: Allow any number of comments on ILLs

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>
This commit is contained in:
Magnus Enger 2017-11-20 14:14:30 +00:00 committed by Nick Clemens
parent cf5835fc52
commit 2460eb0597
6 changed files with 257 additions and 3 deletions

58
Koha/Illcomment.pm Normal file
View File

@ -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;

18
Koha/Illcomments.pm Normal file
View File

@ -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;

View 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;

View File

@ -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);

View File

@ -51,7 +51,8 @@
'status',
'updated',
'illrequest_id',
'action'
'comments',
'action' // Action should always be last
];
// Remove any fields we're ignoring
@ -205,6 +206,18 @@
}
};
// 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();
@ -231,7 +244,7 @@
// 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
@ -604,6 +617,47 @@
</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>
@ -621,6 +675,7 @@
<th>Status</th>
<th>Updated on</th>
<th>Request number</th>
<th>Comments</th>
<th class="actions"></th>
</tr>
</thead>

View File

@ -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;