diff --git a/Koha/Illrequest/Availability.pm b/Koha/Illrequest/Availability.pm deleted file mode 100644 index 9013c1749a..0000000000 --- a/Koha/Illrequest/Availability.pm +++ /dev/null @@ -1,127 +0,0 @@ -package Koha::Illrequest::Availability; - -# Copyright 2019 PTFS Europe Ltd -# -# 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 . - -use Modern::Perl; - -use JSON; -use MIME::Base64 qw( encode_base64 ); -use URI::Escape qw( uri_escape ); -use Encode qw( encode ); - -use Koha::Plugins; - -=head1 NAME - -Koha::Illrequest::Availability - Koha ILL Availability Searching - -=head1 SYNOPSIS - -Object-oriented class that provides availability searching via -availability plugins - -=head1 DESCRIPTION - -This class provides the ability to identify and fetch API services -that can be used to search for item availability - -=head1 API - -=head2 Class Methods - -=head3 new - - my $availability = Koha::Illrequest::Logger->new($metadata); - -Create a new Koha::Illrequest::Availability object. -We also store the metadata to be used for searching - -=cut - -sub new { - my ( $class, $metadata ) = @_; - my $self = {}; - - $self->{metadata} = $metadata; - - bless $self, $class; - - return $self; -} - -=head3 get_services - - my $services = Koha::Illrequest::Availability->get_services($params); - -Given our metadata, iterate plugins with the right method and -check if they can service our request and, if so, return an arrayref -of services. Optionally accept a hashref specifying additional filter -parameters - -=cut - -sub get_services { - my ( $self, $params ) = @_; - - my $plugin_filter = { - method => 'ill_availability_services' - }; - - if ($params->{metadata}) { - $plugin_filter->{metadata} = $params->{metadata}; - } - - my @candidates = Koha::Plugins->new()->GetPlugins($plugin_filter); - my @services = (); - foreach my $plugin(@candidates) { - my $valid_service = $plugin->ill_availability_services({ - metadata => $self->{metadata}, - ui_context => $params->{ui_context} - }); - push @services, $valid_service if $valid_service; - } - - return \@services; -} - -=head3 prep_metadata - - my $prepared = Koha::Illrequest::Availability->prep_metadata($metadata); - -Given our metadata, return a string representing that metadata that can be -passed in a URL (encoded in JSON then Base64 encoded) - -=cut - -sub prep_metadata { - my ( $self, $metadata ) = @_; - - # We sort the metadata hashref by key before encoding it, primarily - # so this function returns something predictable that we can test! - my $json = JSON->new; - $json->canonical([1]); - return uri_escape(encode_base64(encode('utf-8',$json->encode($metadata)))); -} - -=head1 AUTHOR - -Andrew Isherwood - -=cut - -1; diff --git a/Koha/Illrequest/Workflow.pm b/Koha/Illrequest/Workflow.pm new file mode 100644 index 0000000000..d2b4e157a8 --- /dev/null +++ b/Koha/Illrequest/Workflow.pm @@ -0,0 +1,93 @@ +package Koha::Illrequest::Workflow; + +# Copyright 2023 PTFS Europe Ltd +# +# 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 . + +use Modern::Perl; + +use JSON; +use MIME::Base64 qw( encode_base64 ); +use URI::Escape qw( uri_escape ); +use Encode qw( encode ); + +use Koha::Plugins; + +=head1 NAME + +Koha::Illrequest::TypeDisclaimer - Koha ILL TypeDisclaimer + +=head1 SYNOPSIS + +Object-oriented class that provides the ILL request type disclaimer + +=head1 DESCRIPTION + +This class provides the ability to verify if it should render type disclaimer +and handle the template params accordingly + +=head1 API + +=head2 Class Methods + +=head3 new + + my $type_disclaimer = Koha::Illrequest::Logger->new($metadata); + +Create a new Koha::Illrequest::TypeDisclaimer object. +We also store the metadata to be used to retrieve the request type + +=cut + +sub new { + my ( $class, $metadata, $ui_context ) = @_; + my $self = {}; + + $self->{metadata} = $metadata; + $self->{ui_context} = $ui_context; + + bless $self, $class; + + return $self; +} + +=head3 prep_metadata + + my $prepared = Koha::Illrequest::Workflow->prep_metadata($metadata); + +Given our metadata, return a string representing that metadata that can be +passed in a URL (encoded in JSON then Base64 encoded) + +=cut + +sub prep_metadata { + my ( $self, $metadata ) = @_; + + # We sort the metadata hashref by key before encoding it, primarily + # so this function returns something predictable that we can test! + my $json = JSON->new; + $json->canonical( [1] ); + return uri_escape( + encode_base64( encode( 'utf-8', $json->encode($metadata) ) ) ); +} + +=head1 AUTHOR + +Pedro Amorim + +=cut + +1; diff --git a/Koha/Illrequest/Workflow/Availability.pm b/Koha/Illrequest/Workflow/Availability.pm new file mode 100644 index 0000000000..4fd75a48a9 --- /dev/null +++ b/Koha/Illrequest/Workflow/Availability.pm @@ -0,0 +1,150 @@ +package Koha::Illrequest::Workflow::Availability; + +# Copyright 2019 PTFS Europe Ltd +# +# 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 . + +use Modern::Perl; + +use JSON; + +use base qw(Koha::Illrequest::Workflow); + +use Koha::Plugins; + +=head1 NAME + +Koha::Illrequest::Workflow::Availability - Koha ILL Availability Searching + +=head1 SYNOPSIS + +Object-oriented class that provides availability searching via +availability plugins + +=head1 DESCRIPTION + +This class provides the ability to identify and fetch API services +that can be used to search for item availability + +=head1 API + +=head2 Class Methods + +=head3 get_services + + my $services = + Koha::Illrequest::Workflow::Availability->get_services($params); + +Given our metadata, iterate plugins with the right method and +check if they can service our request and, if so, return an arrayref +of services. Optionally accept a hashref specifying additional filter +parameters + +=cut + +sub get_services { + my ( $self, $params ) = @_; + + my $plugin_filter = { method => 'ill_availability_services' }; + + if ( $params->{metadata} ) { + $plugin_filter->{metadata} = $params->{metadata}; + } + + my @candidates = Koha::Plugins->new()->GetPlugins($plugin_filter); + my @services = (); + foreach my $plugin (@candidates) { + my $valid_service = $plugin->ill_availability_services( + { + metadata => $self->{metadata}, + ui_context => $self->{ui_context}, + } + ); + push @services, $valid_service if $valid_service; + } + + return \@services; +} + +=head3 show_availability + + my $show_availability = + Koha::Illrequest::Workflow::Availability->show_availability($params); + +Given $params, return true if availability should be shown + +=cut + +sub show_availability { + my ( $self, $request ) = @_; + + my $services = $self->get_services; + + return + + # ILLCheckAvailability is enabled + C4::Context->preference("ILLCheckAvailability") + + # At least 1 availability service exists + && scalar @{$services} + + # Availability has not yet been checked + && !$self->{metadata}->{checked_availability} + + # The form has been submitted and the backend is able to create the request + && $request->_backend_capability( 'can_create_request', + $self->{metadata} ); +} + +=head3 availability_template_params + + my $availability_template_params = + Koha::Illrequest::Workflow::Availability->availability_template_params( + $params); + +Given $params, return true if availability should be shown + +=cut + +sub availability_template_params { + my ( $self, $params ) = @_; + + $params->{method} = 'availability' if $self->{ui_context} eq 'staff'; + delete $params->{stage} if $self->{ui_context} eq 'staff'; + my $services = $self->get_services; + + return ( + whole => $params, + metadata => $self->prep_metadata($params), + services_json => scalar encode_json($services), + services => $services, + $self->{ui_context} eq 'opac' + ? ( + illrequestsview => 1, + message => $params->{message}, + method => 'availability', + ) + : () + ); +} + +=head1 AUTHOR + +Andrew Isherwood + +=cut + +1; diff --git a/Koha/Illrequest/Workflow/TypeDisclaimer.pm b/Koha/Illrequest/Workflow/TypeDisclaimer.pm new file mode 100644 index 0000000000..675918a1d6 --- /dev/null +++ b/Koha/Illrequest/Workflow/TypeDisclaimer.pm @@ -0,0 +1,194 @@ +package Koha::Illrequest::Workflow::TypeDisclaimer; + +# Copyright 2023 PTFS Europe Ltd +# +# 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 . + +use Modern::Perl; + +use POSIX qw( strftime ); + +use base qw(Koha::Illrequest::Workflow); + +=head1 NAME + +Koha::Illrequest::TypeDisclaimer - Koha ILL TypeDisclaimer + +=head1 SYNOPSIS + +Object-oriented class that provides the ILL request type disclaimer + +=head1 DESCRIPTION + +This class provides the ability to verify if it should render type disclaimer +and handle the template params accordingly + +=head1 API + +=head2 Class Methods + +=head3 show_type_disclaimer + + my $show_type_disclaimer = + Koha::Illrequest::TypeDisclaimer->show_type_disclaimer($params); + +Given $params, return true if type disclaimer should be shown + +=cut + +sub show_type_disclaimer { + my ( $self, $request ) = @_; + + my $disc_sys_pref = $self->_get_type_disclaimer_sys_pref; + + my $disc_info = + $self->_get_type_disclaimer_info( $self->_get_type_disclaimer_sys_pref, + $self->{metadata}->{type} ); + + return + + # ILLModuleDisclaimerByType contains correct YAML + %{$disc_sys_pref} + + # Check that we have info to display for this type + && $disc_info + + # ILLModuleDisclaimerByType contains at least 'all' + && $disc_sys_pref->{all} + + # Type disclaimer has not yet been submitted + && !$self->{metadata}->{type_disclaimer_submitted} + + # The form has been submitted and the backend is able to create the request + && $request->_backend_capability( 'can_create_request', + $self->{metadata} ); +} + +=head3 type_disclaimer_template_params + + my $type_disclaimer_template_params = + Koha::Illrequest::TypeDisclaimer->type_disclaimer_template_params( + $params); + +Given $params, return true if type disclaimer should be rendered + +=cut + +sub type_disclaimer_template_params { + my ( $self, $params ) = @_; + + my $disc_info = + $self->_get_type_disclaimer_info( $self->_get_type_disclaimer_sys_pref, + $params->{type} ); + + $params->{method} = 'typedisclaimer' if $self->{ui_context} eq 'staff'; + delete $params->{stage} if $self->{ui_context} eq 'staff'; + + return ( + whole => $params, + metadata => $self->prep_metadata($params), + disclaimer => $disc_info, + $self->{ui_context} eq 'opac' + ? ( + illrequestsview => 1, + message => $params->{message}, + method => 'typedisclaimer', + ) + : () + ); +} + +=head3 after_request_created + + #example here + +#description here + +=cut + +sub after_request_created { + my ( $self, $params, $request ) = @_; + + # Store type disclaimer date and value + my $type_disclaimer_date = { + illrequest_id => $request->illrequest_id, + type => "type_disclaimer_date", + value => strftime( "%Y-%m-%dT%H:%M:%S", localtime( time() ) ), + readonly => 0 + }; + Koha::Illrequestattribute->new($type_disclaimer_date)->store; + + my $type_disclaimer_value = { + illrequest_id => $request->illrequest_id, + type => "type_disclaimer_value", + value => $params->{type_disclaimer_value}, + readonly => 0 + }; + Koha::Illrequestattribute->new($type_disclaimer_value)->store; +} + +=head3 _get_type_disclaimer_info + + my $type_disclaimer_info = + $self->_get_type_disclaimer_info( $type_disclaimer_sys_pref, $request_type ); + +Given ILLModuleDisclaimerByType sys pref and type, returns the respective +type disclaimer info +Returns undef if sys pref is empty or malformed +=cut + +sub _get_type_disclaimer_info { + my ( $self, $disc_sys_pref, $type ) = @_; + + my @matching_request_type = + map ( $_ eq $type ? $_ : (), keys %$disc_sys_pref ); + + my $disc_info = undef; + if ( scalar @matching_request_type ) { + return if $disc_sys_pref->{$type}->{bypass}; + + $disc_info->{text} = $disc_sys_pref->{$type}->{text}; + $disc_info->{av_cat} = $disc_sys_pref->{$type}->{av_category_code}; + } + elsif ( $disc_sys_pref->{all} ) { + $disc_info->{text} = $disc_sys_pref->{all}->{text}; + $disc_info->{av_cat} = $disc_sys_pref->{all}->{av_category_code}; + } + return $disc_info; +} + +=head3 _get_type_disclaimer_sys_pref + + my $disc_sys_pref = $self->_get_type_disclaimer_sys_pref; + +Returns YAML from ILLModuleDisclaimerByType syspref +Returns empty if empty or YAML error + +=cut + +sub _get_type_disclaimer_sys_pref { + my ($self) = @_; + + return C4::Context->yaml_preference("ILLModuleDisclaimerByType") // {}; +} + +=head1 AUTHOR + +Pedro Amorim + +=cut + +1;