View | Details | Raw Unified | Return to bug 38441
Collapse All | Expand All

(-)a/Koha/ILL/Request/Workflow/HistoryCheck.pm (-1 / +271 lines)
Line 0 Link Here
0
- 
1
package Koha::ILL::Request::Workflow::HistoryCheck;
2
3
# Copyright 2024 PTFS Europe Ltd
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
22
use Koha::I18N qw(__);
23
24
use base qw(Koha::ILL::Request::Workflow);
25
26
=head1 NAME
27
28
Koha::ILL::Request::HistoryCheck - Koha ILL HistoryCheck
29
30
=head1 SYNOPSIS
31
32
Object-oriented class that provides the ILL request type disclaimer
33
34
=head1 DESCRIPTION
35
36
This class provides the ability to verify if it should render type disclaimer
37
and handle the template params accordingly
38
39
=head1 API
40
41
=head2 Class Methods
42
43
=head3 show_history_check
44
45
    my $show_history_check =
46
    Koha::ILL::Request::HistoryCheck->show_history_check($params);
47
48
Given $params, return true if history check should be shown
49
50
=cut
51
52
sub show_history_check {
53
    my ( $self, $request ) = @_;
54
55
    my $opac_no_matching_requests_for_patron = 0;
56
    if ( $self->{ui_context} eq 'opac' ) {
57
        my $patron_cardnumber = C4::Context->userenv->{'cardnumber'} || 0;
58
        my ( $matching_requests_for_patron, $remaining_matching_requests ) =
59
            $self->_get_split_matching_requests($patron_cardnumber);
60
        $opac_no_matching_requests_for_patron = 1
61
            if $matching_requests_for_patron  && !scalar @{$matching_requests_for_patron};
62
    }
63
64
    return
65
66
        # ILLHistoryCheck is enabled
67
        C4::Context->yaml_preference("ILLHistoryCheck")
68
69
        # It's not OPAC with no matching requests for patron
70
        && !$opac_no_matching_requests_for_patron
71
72
        # Matching requests were found
73
        && $self->_find_matching_requests()
74
75
        # History check has not yet been submitted
76
        && !$self->{metadata}->{history_check_submitted}
77
78
        # The form has been submitted and the backend is able to create the request
79
        && $request->_backend_capability( 'can_create_request', $self->{metadata} );
80
}
81
82
=head3 history_check_template_params
83
84
    my $history_check_template_params =
85
    Koha::ILL::Request::TypeDisclaimer->history_check_template_params(
86
        $params);
87
88
Given $params, return true if history check should be rendered
89
90
=cut
91
92
sub history_check_template_params {
93
    my ( $self, $params ) = @_;
94
95
    $params->{method} = 'historycheck' if $self->{ui_context} eq 'staff';
96
    delete $params->{stage}            if $self->{ui_context} eq 'staff';
97
98
    $params->{cardnumber} = C4::Context->userenv->{'cardnumber'} || 0 if $self->{ui_context} eq 'opac';
99
100
    my $backend = Koha::ILL::Request->new->load_backend( $params->{backend} );
101
    my $mapping_function;
102
    eval {
103
        $mapping_function = sub { return $backend->{_my_backend}->_get_core_fields()->{ $_[0] } };
104
        $backend->{_my_backend}->_get_core_fields();
105
    };
106
    if ($@) {
107
        $mapping_function = sub { return $_[0] };
108
    }
109
110
    my ( $matching_requests_for_patron, $remaining_matching_requests ) =
111
        $self->_get_split_matching_requests( $params->{cardnumber} );
112
113
    return (
114
        whole                        => $params,
115
        metadata                     => $self->prep_metadata($params),
116
        matching_requests_for_patron => $matching_requests_for_patron,
117
        remaining_matching_requests  => $remaining_matching_requests,
118
        $self->{ui_context} eq 'staff'
119
        ? (
120
            key_mapping        => $mapping_function,
121
            request_patron_obj => Koha::Patrons->find( { cardnumber => $params->{cardnumber} } )
122
            )
123
        : (),
124
        $self->{ui_context} eq 'opac'
125
        ? (
126
            illrequestsview => 1,
127
            message         => $params->{message},
128
            op              => 'historycheck',
129
            )
130
        : ()
131
    );
132
}
133
134
=head3 after_request_created
135
136
    $history_check->after_request_created($params, $request);
137
138
Actions that need to be done after the request has been created
139
140
=cut
141
142
sub after_request_created {
143
    my ( $self, $params, $request ) = @_;
144
145
    return if ( $self->{ui_context} ne 'opac' );
146
147
    my $patron_cardnumber = C4::Context->userenv->{'cardnumber'} || 0;
148
    my ( $matching_requests_for_patron, $remaining_matching_requests ) =
149
        $self->_get_split_matching_requests($patron_cardnumber);
150
151
    my $staffnotes;
152
153
    if ($matching_requests_for_patron) {
154
        my $appended_self_note = 0;
155
        foreach my $matching_request ( @{$matching_requests_for_patron} ) {
156
            if ( $matching_request->illrequest_id ne $request->illrequest_id ) {
157
                if ( $appended_self_note == 0 ) {
158
                    $staffnotes .= __("Request has been submitted by this patron in the past:");
159
                    $appended_self_note = 1;
160
                }
161
                $staffnotes .= ' ' . $self->_get_request_staff_link($matching_request);
162
            }
163
        }
164
    }
165
166
    if ($remaining_matching_requests) {
167
        my $appended_others_note = 0;
168
        foreach my $matching_request ( @{$remaining_matching_requests} ) {
169
            if ( $appended_others_note == 0 ) {
170
                $staffnotes .=
171
                    ( $staffnotes ? "\n" : '' ) . __("Request has been submitted by other patrons in the past:");
172
                $appended_others_note = 1;
173
            }
174
            $staffnotes .= ' ' . $self->_get_request_staff_link($matching_request);
175
        }
176
    }
177
178
    $request->append_to_note($staffnotes)->store() if $staffnotes;
179
}
180
181
=head3 _get_request_staff_link
182
183
    $self->_get_request_staff_link($matching_request);
184
185
Returns an HTML staff link to the provided request
186
187
=cut
188
189
sub _get_request_staff_link {
190
    my ( $self, $request ) = @_;
191
192
    return
193
          '<a href="/cgi-bin/koha/ill/ill-requests.pl?op=illview&illrequest_id='
194
        . $request->illrequest_id . '">'
195
        . __("ILL Request #")
196
        . $request->illrequest_id . '</a>';
197
}
198
199
=head3 _get_split_matching_requests
200
201
    my ( $matching_requests_for_patron, $remaining_matching_requests )
202
        = $self->_get_split_matching_requests( $cardnumber );
203
204
Splits the matching requests from _find_matching_requests into two arrays.
205
206
One array contains ILL requests made by the patron with the cardnumber
207
specified, and the other contains the rest of the matching requests.
208
209
=cut
210
211
sub _get_split_matching_requests {
212
    my ( $self, $cardnumber ) = @_;
213
214
    my $all_matching_requests = $self->_find_matching_requests();
215
    my @matching_requests_for_patron;
216
    my @remaining_matching_requests;
217
218
    return ( undef, undef ) if !$all_matching_requests;
219
220
    foreach my $request ( @{$all_matching_requests} ) {
221
        if ( $request->patron && $request->patron->cardnumber eq $cardnumber ) {
222
            push @matching_requests_for_patron, $request;
223
        } else {
224
            push @remaining_matching_requests, $request;
225
        }
226
    }
227
    return ( \@matching_requests_for_patron, \@remaining_matching_requests );
228
229
}
230
231
=head3 _find_matching_requests
232
233
    my $matching_requests = $self->_find_matching_requests();
234
235
Returns a list of matching requests (match is done by doi, issn, isbn, pubmedid)
236
237
=cut
238
239
sub _find_matching_requests {
240
    my ($self) = @_;
241
242
    my @id_fields = ( 'doi', 'issn', 'isbn', 'pubmedid' );
243
244
    return 0 unless grep { $self->{metadata}->{$_} } @id_fields;
245
246
    my @query = ();
247
    foreach my $id_field (@id_fields) {
248
        push @query, {
249
            'illrequestattributes.type'  => $id_field,
250
            'illrequestattributes.value' => $self->{metadata}->{$id_field},
251
        };
252
    }
253
254
    my $matching_requests = Koha::ILL::Requests->search(
255
        \@query,
256
        {
257
            join     => 'illrequestattributes',
258
            distinct => 'illrequest_id',
259
        }
260
    );
261
262
    return $matching_requests->count ? $matching_requests->as_list : 0;
263
}
264
265
=head1 AUTHOR
266
267
Pedro Amorim <pedro.amorim@ptfs-europe.com>
268
269
=cut
270
271
1;

Return to bug 38441