1 package Koha
::Illrequest
;
3 # Copyright PTFS Europe 2016
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 51 Franklin
19 # Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 use File
::Basename
qw( basename );
25 use Encode
qw( encode );
31 use Koha
::Exceptions
::Ill
;
32 use Koha
::Illrequestattributes
;
35 use base
qw(Koha::Object);
39 Koha::Illrequest - Koha Illrequest Object class
43 An ILLRequest consists of two parts; the Illrequest Koha::Object, and a series
44 of related Illrequestattributes.
46 The former encapsulates the basic necessary information that any ILL requires
47 to be usable in Koha. The latter is a set of additional properties used by
50 The former subsumes the legacy "Status" object. The latter remains
51 encapsulated in the "Record" object.
55 - Anything invoking the ->status method; annotated with:
56 + # Old use of ->status !
60 =head2 Backend API Response Principles
62 All methods should return a hashref in the following format:
68 This should be set to 1 if an error was encountered.
72 The status should be a string from the list of statuses detailed below.
76 The message is a free text field that can be passed on to the end user.
80 The value returned by the method.
84 =head2 Interface Status Messages
88 =item * branch_address_incomplete
90 An interface request has determined branch address details are incomplete.
92 =item * cancel_success
94 The interface's cancel_request method was successful in cancelling the
95 Illrequest using the API.
99 The interface's cancel_request method failed to cancel the Illrequest using
104 The interface's request method returned saying that the desired item is not
105 available for request.
111 =head3 illrequestattributes
115 sub illrequestattributes
{
117 return Koha
::Illrequestattributes
->_new_from_dbic(
118 scalar $self->_result->illrequestattributes
128 return Koha
::Patron
->_new_from_dbic(
129 scalar $self->_result->borrowernumber
135 Require "Base.pm" from the relevant ILL backend.
140 my ( $self, $backend_id ) = @_;
142 my @raw = qw
/Koha Illbackends/; # Base Path
144 my $backend_name = $backend_id || $self->backend;
146 unless ( defined $backend_name && $backend_name ne '' ) {
147 Koha
::Exceptions
::Ill
::InvalidBackendId
->throw(
148 "An invalid backend ID was requested ('')");
151 my $location = join "/", @raw, $backend_name, "Base.pm"; # File to load
152 my $backend_class = join "::", @raw, $backend_name, "Base"; # Package name
154 $self->{_my_backend
} = $backend_class->new({ config
=> $self->_config });
161 my $backend = $abstract->_backend($new_backend);
162 my $backend = $abstract->_backend;
164 Getter/Setter for our API object.
169 my ( $self, $backend ) = @_;
170 $self->{_my_backend
} = $backend if ( $backend );
171 # Dynamically load our backend object, as late as possible.
172 $self->load_backend unless ( $self->{_my_backend
} );
173 return $self->{_my_backend
};
176 =head3 _backend_capability
178 my $backend_capability_result = $self->_backend_capability($name, $args);
180 This is a helper method to invoke optional capabilities in the backend. If
181 the capability named by $name is not supported, return 0, else invoke it,
182 passing $args along with the invocation, and return its return value.
184 NOTE: this module suffers from a confusion in termninology:
186 in _backend_capability, the notion of capability refers to an optional feature
187 that is implemented in core, but might not be supported by a given backend.
189 in capabilities & custom_capability, capability refers to entries in the
190 status_graph (after union between backend and core).
192 The easiest way to fix this would be to fix the terminology in
193 capabilities & custom_capability and their callers.
197 sub _backend_capability
{
198 my ( $self, $name, $args ) = @_;
201 $capability = $self->_backend->capabilities($name);
206 return &{$capability}($args);
214 my $config = $abstract->_config($config);
215 my $config = $abstract->_config;
217 Getter/Setter for our config object.
222 my ( $self, $config ) = @_;
223 $self->{_my_config
} = $config if ( $config );
224 # Load our config object, as late as possible.
225 unless ( $self->{_my_config
} ) {
226 $self->{_my_config
} = Koha
::Illrequest
::Config
->new;
228 return $self->{_my_config
};
237 return $self->_backend->metadata($self);
240 =head3 _core_status_graph
242 my $core_status_graph = $illrequest->_core_status_graph;
244 Returns ILL module's default status graph. A status graph defines the list of
245 available actions at any stage in the ILL workflow. This is for instance used
246 by the perl script & template to generate the correct buttons to display to
247 the end user at any given point.
251 sub _core_status_graph
{
255 prev_actions
=> [ ], # Actions containing buttons
256 # leading to this status
257 id
=> 'NEW', # ID of this status
258 name
=> 'New request', # UI name of this status
259 ui_method_name
=> 'New request', # UI name of method leading
261 method
=> 'create', # method to this status
262 next_actions
=> [ 'REQ', 'GENREQ', 'KILL' ], # buttons to add to all
263 # requests with this status
264 ui_method_icon
=> 'fa-plus', # UI Style class
267 prev_actions
=> [ 'NEW', 'REQREV', 'QUEUED', 'CANCREQ' ],
270 ui_method_name
=> 'Confirm request',
272 next_actions
=> [ 'REQREV', 'COMP' ],
273 ui_method_icon
=> 'fa-check',
276 prev_actions
=> [ 'NEW', 'REQREV' ],
278 name
=> 'Requested from partners',
279 ui_method_name
=> 'Place request with partners',
280 method
=> 'generic_confirm',
281 next_actions
=> [ 'COMP' ],
282 ui_method_icon
=> 'fa-send-o',
285 prev_actions
=> [ 'REQ' ],
287 name
=> 'Request reverted',
288 ui_method_name
=> 'Revert Request',
290 next_actions
=> [ 'REQ', 'GENREQ', 'KILL' ],
291 ui_method_icon
=> 'fa-times',
296 name
=> 'Queued request',
299 next_actions
=> [ 'REQ', 'KILL' ],
303 prev_actions
=> [ 'NEW' ],
305 name
=> 'Cancellation requested',
308 next_actions
=> [ 'KILL', 'REQ' ],
312 prev_actions
=> [ 'REQ' ],
315 ui_method_name
=> 'Mark completed',
316 method
=> 'mark_completed',
318 ui_method_icon
=> 'fa-check',
321 prev_actions
=> [ 'QUEUED', 'REQREV', 'NEW', 'CANCREQ' ],
324 ui_method_name
=> 'Delete request',
327 ui_method_icon
=> 'fa-trash',
332 =head3 _core_status_graph
334 my $status_graph = $illrequest->_core_status_graph($origin, $new_graph);
336 Return a new status_graph, the result of merging $origin & new_graph. This is
337 operation is a union over the sets defied by the two graphs.
339 Each entry in $new_graph is added to $origin. We do not provide a syntax for
340 'subtraction' of entries from $origin.
342 Whilst it is not intended that this works, you can override entries in $origin
343 with entries with the same key in $new_graph. This can lead to problematic
344 behaviour when $new_graph adds an entry, which modifies a dependent entry in
345 $origin, only for the entry in $origin to be replaced later with a new entry
348 NOTE: this procedure does not "re-link" entries in $origin or $new_graph,
349 i.e. each of the graphs need to be correct at the outset of the operation.
353 sub _status_graph_union
{
354 my ( $self, $core_status_graph, $backend_status_graph ) = @_;
355 # Create new status graph with:
356 # - all core_status_graph
357 # - for-each each backend_status_graph
358 # + add to new status graph
359 # + for each core prev_action:
360 # * locate core_status
361 # * update next_actions with additional next action.
362 # + for each core next_action:
363 # * locate core_status
364 # * update prev_actions with additional prev action
366 my @core_status_ids = keys %{$core_status_graph};
367 my $status_graph = clone
($core_status_graph);
369 foreach my $backend_status_key ( keys %{$backend_status_graph} ) {
370 my $backend_status = $backend_status_graph->{$backend_status_key};
371 # Add to new status graph
372 $status_graph->{$backend_status_key} = $backend_status;
373 # Update all core methods' next_actions.
374 foreach my $prev_action ( @
{$backend_status->{prev_actions
}} ) {
375 if ( grep $prev_action, @core_status_ids ) {
377 @
{$status_graph->{$prev_action}->{next_actions
}};
378 push @next_actions, $backend_status_key;
379 $status_graph->{$prev_action}->{next_actions
}
383 # Update all core methods' prev_actions
384 foreach my $next_action ( @
{$backend_status->{next_actions
}} ) {
385 if ( grep $next_action, @core_status_ids ) {
387 @
{$status_graph->{$next_action}->{prev_actions
}};
388 push @prev_actions, $backend_status_key;
389 $status_graph->{$next_action}->{prev_actions
}
395 return $status_graph;
402 my $capabilities = $illrequest->capabilities;
404 Return a hashref mapping methods to operation names supported by the queried
407 Example return value:
409 { create => "Create Request", confirm => "Progress Request" }
411 NOTE: this module suffers from a confusion in termninology:
413 in _backend_capability, the notion of capability refers to an optional feature
414 that is implemented in core, but might not be supported by a given backend.
416 in capabilities & custom_capability, capability refers to entries in the
417 status_graph (after union between backend and core).
419 The easiest way to fix this would be to fix the terminology in
420 capabilities & custom_capability and their callers.
425 my ( $self, $status ) = @_;
426 # Generate up to date status_graph
427 my $status_graph = $self->_status_graph_union(
428 $self->_core_status_graph,
429 $self->_backend->status_graph({
434 # Extract available actions from graph.
435 return $status_graph->{$status} if $status;
436 # Or return entire graph.
437 return $status_graph;
440 =head3 custom_capability
442 Return the result of invoking $CANDIDATE on this request's backend with
443 $PARAMS, or 0 if $CANDIDATE is an unknown method on backend.
445 NOTE: this module suffers from a confusion in termninology:
447 in _backend_capability, the notion of capability refers to an optional feature
448 that is implemented in core, but might not be supported by a given backend.
450 in capabilities & custom_capability, capability refers to entries in the
451 status_graph (after union between backend and core).
453 The easiest way to fix this would be to fix the terminology in
454 capabilities & custom_capability and their callers.
458 sub custom_capability
{
459 my ( $self, $candidate, $params ) = @_;
460 foreach my $capability ( values %{$self->capabilities} ) {
461 if ( $candidate eq $capability->{method
} ) {
463 $self->_backend->$candidate({
467 return $self->expandTemplate($response);
473 =head3 available_backends
475 Return a list of available backends.
479 sub available_backends
{
481 my $backends = $self->_config->available_backends;
485 =head3 available_actions
487 Return a list of available actions.
491 sub available_actions
{
493 my $current_action = $self->capabilities($self->status);
494 my @available_actions = map { $self->capabilities($_) }
495 @
{$current_action->{next_actions
}};
496 return \
@available_actions;
499 =head3 mark_completed
501 Mark a request as completed (status = COMP).
507 $self->status('COMP')->store;
512 method
=> 'mark_completed',
518 =head2 backend_confirm
520 Confirm a request. The backend handles setting of mandatory fields in the commit stage:
526 =item * accessurl, cost (if available).
532 sub backend_confirm
{
533 my ( $self, $params ) = @_;
535 my $response = $self->_backend->confirm({
539 return $self->expandTemplate($response);
542 =head3 backend_update_status
546 sub backend_update_status
{
547 my ( $self, $params ) = @_;
548 return $self->expandTemplate($self->_backend->update_status($params));
551 =head3 backend_cancel
553 my $ILLResponse = $illRequest->backend_cancel;
555 The standard interface method allowing for request cancellation.
560 my ( $self, $params ) = @_;
562 my $result = $self->_backend->cancel({
567 return $self->expandTemplate($result);
572 my $renew_response = $illRequest->backend_renew;
574 The standard interface method allowing for request renewal queries.
580 return $self->expandTemplate(
581 $self->_backend->renew({
587 =head3 backend_create
589 my $create_response = $abstractILL->backend_create($params);
591 Return an array of Record objects created by querying our backend with
594 In the context of the other ILL methods, this is a special method: we only
595 pass it $params, as it does not yet have any other data associated with it.
600 my ( $self, $params ) = @_;
602 # Establish whether we need to do a generic copyright clearance.
603 if ( ( !$params->{stage
} || $params->{stage
} eq 'init' )
604 && C4
::Context
->preference("ILLModuleCopyrightClearance") ) {
610 stage
=> 'copyrightclearance',
612 backend
=> $self->_backend->name
615 } elsif ( defined $params->{stage
}
616 && $params->{stage
} eq 'copyrightclearance' ) {
617 $params->{stage
} = 'init';
620 # First perform API action, then...
625 my $result = $self->_backend->create($args);
627 # ... simple case: we're not at 'commit' stage.
628 my $stage = $result->{stage
};
629 return $self->expandTemplate($result)
630 unless ( 'commit' eq $stage );
632 # ... complex case: commit!
634 # Do we still have space for an ILL or should we queue?
635 my $permitted = $self->check_limits(
636 { patron
=> $self->patron }, { librarycode
=> $self->branchcode }
639 # Now augment our committed request.
641 $result->{permitted
} = $permitted; # Queue request?
645 # ...Updating status!
646 $self->status('QUEUED')->store unless ( $permitted );
648 return $self->expandTemplate($result);
651 =head3 expandTemplate
653 my $params = $abstract->expandTemplate($params);
655 Return a version of $PARAMS augmented with our required template path.
660 my ( $self, $params ) = @_;
661 my $backend = $self->_backend->name;
662 # Generate path to file to load
663 my $backend_dir = $self->_config->backend_dir;
664 my $backend_tmpl = join "/", $backend_dir, $backend;
665 my $intra_tmpl = join "/", $backend_tmpl, "intra-includes",
666 $params->{method
} . ".inc";
667 my $opac_tmpl = join "/", $backend_tmpl, "opac-includes",
668 $params->{method
} . ".inc";
670 $params->{template
} = $intra_tmpl;
671 $params->{opac_template
} = $opac_tmpl;
675 #### Abstract Imports
679 my $limit_rules = $abstract->getLimits( {
680 type => 'brw_cat' | 'branch',
684 Return the ILL limit rules for the supplied combination of type / value.
686 As the config may have no rules for this particular type / value combination,
687 or for the default, we must define fall-back values here.
692 my ( $self, $params ) = @_;
693 my $limits = $self->_config->getLimitRules($params->{type
});
695 if ( defined $params->{value
}
696 && defined $limits->{$params->{value
}} ) {
697 return $limits->{$params->{value
}};
700 return $limits->{default} || { count
=> -1, method
=> 'active' };
706 my $prefix = $abstract->getPrefix( {
708 branch => $branch_code,
711 Return the ILL prefix as defined by our $params: either per borrower category,
712 per branch or the default.
717 my ( $self, $params ) = @_;
718 my $brn_prefixes = $self->_config->getPrefixes('branch');
719 my $brw_prefixes = $self->_config->getPrefixes('brw_cat');
721 return $brw_prefixes->{$params->{brw_cat
}}
722 || $brn_prefixes->{$params->{branch
}}
723 || $brw_prefixes->{default}
724 || ""; # "the empty prefix"
727 #### Illrequests Imports
731 my $ok = $illRequests->check_limits( {
732 borrower => $borrower,
733 branchcode => 'branchcode' | undef,
736 Given $PARAMS, a hashref containing a $borrower object and a $branchcode,
737 see whether we are still able to place ILLs.
739 LimitRules are derived from koha-conf.xml:
740 + default limit counts, and counting method
741 + branch specific limit counts & counting method
742 + borrower category specific limit counts & counting method
743 + err on the side of caution: a counting fail will cause fail, even if
744 the other counts passes.
749 my ( $self, $params ) = @_;
750 my $patron = $params->{patron
};
751 my $branchcode = $params->{librarycode
} || $patron->branchcode;
753 # Establish maximum number of allowed requests
754 my ( $branch_rules, $brw_rules ) = (
761 value
=> $patron->categorycode,
764 my ( $branch_limit, $brw_limit )
765 = ( $branch_rules->{count
}, $brw_rules->{count
} );
766 # Establish currently existing requests
767 my ( $branch_count, $brw_count ) = (
768 $self->_limit_counter(
769 $branch_rules->{method
}, { branchcode
=> $branchcode }
771 $self->_limit_counter(
772 $brw_rules->{method
}, { borrowernumber
=> $patron->borrowernumber }
777 # A limit of -1 means no limit exists.
778 # We return blocked if either branch limit or brw limit is reached.
779 if ( ( $branch_limit != -1 && $branch_limit <= $branch_count )
780 || ( $brw_limit != -1 && $brw_limit <= $brw_count ) ) {
788 my ( $self, $method, $target ) = @_;
790 # Establish parameters of counts
792 if ($method && $method eq 'annual') {
793 $resultset = Koha
::Illrequests
->search({
796 \"YEAR
(placed
) = YEAR
(NOW
())"
799 } else { # assume 'active'
800 # XXX: This status list is ugly. There should be a method in config
802 my $where = { status => { -not_in => [ 'QUEUED', 'COMP' ] } };
803 $resultset = Koha::Illrequests->search({ %{$target}, %{$where} });
807 return $resultset->count;
810 =head3 requires_moderation
812 my $status = $illRequest->requires_moderation;
814 Return the name of the status if moderation by staff is required; or 0
819 sub requires_moderation {
821 my $require_moderation = {
822 'CANCREQ' => 'CANCREQ',
824 return $require_moderation->{$self->status};
827 =head3 generic_confirm
829 my $stage_summary = $illRequest->generic_confirm;
831 Handle the generic_confirm extended method. The first stage involves creating
832 a template email for the end user to edit in the browser. The second stage
833 attempts to submit the email.
837 sub generic_confirm {
838 my ( $self, $params ) = @_;
839 my $branch = Koha::Libraries->find($params->{current_branchcode})
840 || die "Invalid current branchcode
. Are you logged
in as the database user?
";
841 if ( !$params->{stage}|| $params->{stage} eq 'init' ) {
842 my $draft->{subject} = "ILL Request
";
843 $draft->{body} = <<EOF;
846 We would like to request an interlibrary loan for a title matching the
847 following description:
851 my $details = $self->metadata;
852 while (my ($title, $value) = each %{$details}) {
853 $draft->{body
} .= " - " . $title . ": " . $value . "\n"
856 $draft->{body
} .= <<EOF;
858 Please let us know if you are able to supply this to us.
864 my @address = map { $branch->$_ }
865 qw
/ branchname branchaddress1 branchaddress2 branchaddress3
866 branchzip branchcity branchstate branchcountry branchphone
869 foreach my $line ( @address ) {
870 $address .= $line . "\n" if $line;
873 $draft->{body
} .= $address;
875 my $partners = Koha
::Patrons
->search({
876 categorycode
=> $self->_config->partner_code
882 method
=> 'generic_confirm',
886 partners
=> $partners,
890 } elsif ( 'draft' eq $params->{stage
} ) {
891 # Create the to header
892 my $to = $params->{partners
};
894 $to =~ s/^\x00//; # Strip leading NULLs
895 $to =~ s/\x00/; /; # Replace others with '; '
897 Koha
::Exceptions
::Ill
::NoTargetEmail
->throw(
898 "No target email addresses found. Either select at least one partner or check your ILL partner library records.")
900 # Create the from, replyto and sender headers
901 my $from = $branch->branchemail;
902 my $replyto = $branch->branchreplyto || $from;
903 Koha
::Exceptions
::Ill
::NoLibraryEmail
->throw(
904 "Your library has no usable email address. Please set it.")
908 my $message = Koha
::Email
->new;
909 my %mail = $message->create_message_headers(
914 subject
=> Encode
::encode
( "utf8", $params->{subject
} ),
915 message
=> Encode
::encode
( "utf8", $params->{body
} ),
916 contenttype
=> 'text/plain',
920 my $result = sendmail
(%mail);
922 $self->status("GENREQ")->store;
927 method
=> 'generic_confirm',
934 status
=> 'email_failed',
935 message
=> $Mail::Sendmail
::error
,
936 method
=> 'generic_confirm',
941 die "Unknown stage, should not have happened."
947 my $prefix = $record->id_prefix;
949 Return the prefix appropriate for the current Illrequest as derived from the
950 borrower and branch associated with this request's Status, and the config
957 my $brw = $self->patron;
958 my $brw_cat = "dummy";
959 $brw_cat = $brw->categorycode
960 unless ( 'HASH' eq ref($brw) && $brw->{deleted
} );
961 my $prefix = $self->getPrefix( {
963 branch
=> $self->branchcode,
965 $prefix .= "-" if ( $prefix );
971 my $params = $illRequest->_censor($params);
973 Return $params, modified to reflect our censorship requirements.
978 my ( $self, $params ) = @_;
979 my $censorship = $self->_config->censorship;
980 $params->{censor_notes_staff
} = $censorship->{censor_notes_staff
}
981 if ( $params->{opac
} );
982 $params->{display_reply_date
} = ( $censorship->{censor_reply_date
} ) ?
0 : 1;
989 $json = $illrequest->TO_JSON
991 Overloaded I<TO_JSON> method that takes care of inserting calculated values
992 into the unblessed representation of the object.
997 my ( $self, $embed ) = @_;
999 my $object = $self->SUPER::TO_JSON
();
1000 $object->{id_prefix
} = $self->id_prefix;
1002 if ( scalar (keys %$embed) ) {
1003 # Augment the request response with patron details if appropriate
1004 if ( $embed->{patron
} ) {
1005 my $patron = $self->patron;
1006 $object->{patron
} = {
1007 firstname
=> $patron->firstname,
1008 surname
=> $patron->surname,
1009 cardnumber
=> $patron->cardnumber
1012 # Augment the request response with metadata details if appropriate
1013 if ( $embed->{metadata
} ) {
1014 $object->{metadata
} = $self->metadata;
1016 # Augment the request response with status details if appropriate
1017 if ( $embed->{capabilities
} ) {
1018 $object->{capabilities
} = $self->capabilities;
1020 # Augment the request response with library details if appropriate
1021 if ( $embed->{library
} ) {
1022 $object->{library
} = Koha
::Libraries
->find(
1031 =head2 Internal methods
1038 return 'Illrequest';
1043 Alex Sassmannshausen <alex.sassmannshausen@ptfs-europe.com>