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

(-)a/Koha/ILL/Request/Workflow/HistoryCheck.pm (-68 / +20 lines)
Lines 52-74 Given $params, return true if history check should be shown Link Here
52
sub show_history_check {
52
sub show_history_check {
53
    my ( $self, $request ) = @_;
53
    my ( $self, $request ) = @_;
54
54
55
    my $opac_no_matching_requests_for_patron = 0;
56
    if ( $self->{ui_context} eq 'opac' ) {
57
        my $patron_cardnumber = C4::Context->userenv ? C4::Context->userenv->{'cardnumber'} || 0 : 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
55
    return
65
56
66
        # ILLHistoryCheck is enabled
57
        # ILLHistoryCheck is enabled
67
        C4::Context->yaml_preference("ILLHistoryCheck")
58
        C4::Context->yaml_preference("ILLHistoryCheck")
68
59
69
        # It's not OPAC with no matching requests for patron
70
        && !$opac_no_matching_requests_for_patron
71
72
        # Matching requests were found
60
        # Matching requests were found
73
        && $self->_find_matching_requests()
61
        && $self->_find_matching_requests()
74
62
Lines 107-120 sub history_check_template_params { Link Here
107
        $mapping_function = sub { return $_[0] };
95
        $mapping_function = sub { return $_[0] };
108
    }
96
    }
109
97
110
    my ( $matching_requests_for_patron, $remaining_matching_requests ) =
98
    my $matching_requests_for_patron = $self->_find_matching_requests();
111
        $self->_get_split_matching_requests( $params->{cardnumber} );
112
99
113
    return (
100
    return (
114
        whole                        => $params,
101
        whole                        => $params,
115
        metadata                     => $self->prep_metadata($params),
102
        metadata                     => $self->prep_metadata($params),
116
        matching_requests_for_patron => $matching_requests_for_patron,
103
        matching_requests_for_patron => $matching_requests_for_patron,
117
        remaining_matching_requests  => $remaining_matching_requests,
118
        $self->{ui_context} eq 'staff'
104
        $self->{ui_context} eq 'staff'
119
        ? (
105
        ? (
120
            key_mapping        => $mapping_function,
106
            key_mapping        => $mapping_function,
Lines 144-152 sub after_request_created { Link Here
144
130
145
    return if ( $self->{ui_context} ne 'opac' );
131
    return if ( $self->{ui_context} ne 'opac' );
146
132
147
    my $patron_cardnumber = C4::Context->userenv->{'cardnumber'} || 0;
133
    my $patron_cardnumber            = C4::Context->userenv->{'cardnumber'} || 0;
148
    my ( $matching_requests_for_patron, $remaining_matching_requests ) =
134
    my $matching_requests_for_patron = $self->_find_matching_requests();
149
        $self->_get_split_matching_requests($patron_cardnumber);
150
135
151
    my $staffnotes;
136
    my $staffnotes;
152
137
Lines 162-180 sub after_request_created { Link Here
162
        }
147
        }
163
    }
148
    }
164
149
165
    if ($remaining_matching_requests) {
166
        my $appended_others_note = 0;
167
        foreach my $matching_request ( @{$remaining_matching_requests} ) {
168
            next if $matching_request->illrequest_id eq $request->illrequest_id;
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;
150
    $request->append_to_note($staffnotes)->store() if $staffnotes;
179
}
151
}
180
152
Lines 196-233 sub _get_request_staff_link { Link Here
196
        . $request->illrequest_id . '</a>';
168
        . $request->illrequest_id . '</a>';
197
}
169
}
198
170
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
171
=head3 _find_matching_requests
232
172
233
    my $matching_requests = $self->_find_matching_requests();
173
    my $matching_requests = $self->_find_matching_requests();
Lines 241-258 sub _find_matching_requests { Link Here
241
181
242
    my @id_fields = ( 'doi', 'issn', 'isbn', 'pubmedid' );
182
    my @id_fields = ( 'doi', 'issn', 'isbn', 'pubmedid' );
243
183
244
    return 0 unless grep { $self->{metadata}->{$_} } @id_fields;
184
    my @existing_id_fields = grep { $self->{metadata}->{$_} } @id_fields;
185
    return 0 unless scalar @existing_id_fields;
186
187
    if ( grep { $_ eq 'doi' } @existing_id_fields ) {
188
        @existing_id_fields = grep { $_ eq 'doi' } @existing_id_fields;
189
    }
190
191
    if ( grep { $_ eq 'pubmedid' } @existing_id_fields ) {
192
        @existing_id_fields = grep { $_ eq 'pubmedid' } @existing_id_fields;
193
    }
194
195
    my $patron = Koha::Patrons->find( { cardnumber => $self->{metadata}->{cardnumber} } );
245
196
246
    my @query = ();
197
    my $query;
247
    foreach my $id_field (@id_fields) {
198
    $query->{'-and'} = [ { 'me.borrowernumber' => $patron->borrowernumber } ] if $patron;
248
        push @query, {
199
    foreach my $id_field (@existing_id_fields) {
200
        push @{ $query->{'-or'} }, {
249
            'illrequestattributes.type'  => $id_field,
201
            'illrequestattributes.type'  => $id_field,
250
            'illrequestattributes.value' => $self->{metadata}->{$id_field},
202
            'illrequestattributes.value' => $self->{metadata}->{$id_field},
251
        };
203
        };
252
    }
204
    }
253
205
254
    my $matching_requests = Koha::ILL::Requests->search(
206
    my $matching_requests = Koha::ILL::Requests->search(
255
        \@query,
207
        $query,
256
        {
208
        {
257
            join     => 'illrequestattributes',
209
            join     => 'illrequestattributes',
258
            distinct => 'illrequest_id',
210
            distinct => 'illrequest_id',
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/ill/ill-requests.tt (-15 / +5 lines)
Lines 879-903 Link Here
879
                            [% END %]
879
                            [% END %]
880
                        </table>
880
                        </table>
881
                        &nbsp;
881
                        &nbsp;
882
                        <h3>This interlibrary loan has been requested before:</h3>
883
                        [% IF matching_requests_for_patron.size %]
882
                        [% IF matching_requests_for_patron.size %]
884
                            <h4>By this patron [%- INCLUDE 'patron-title.inc' patron => request_patron_obj hide_patron_infos_if_needed => 1 -%]:</h4>
883
                            <h4>This interlibrary loan has already been placed by this patron [%- INCLUDE 'patron-title.inc' patron => request_patron_obj hide_patron_infos_if_needed => 1 -%]:</h4>
885
                            <ul class="list-unstyled">
884
                            <ul class="list-unstyled">
886
                                [% FOREACH matching_request_for_patron IN matching_requests_for_patron %]
885
                                [% FOREACH matching_request_for_patron IN matching_requests_for_patron %]
887
                                    <li>
886
                                    <li>
888
                                        <a href="/cgi-bin/koha/ill/ill-requests.pl?op=illview&amp;illrequest_id=[% matching_request_for_patron.illrequest_id %]">ILL Request #[% matching_request_for_patron.illrequest_id | html %]</a>
887
                                        <a target="_blank" href="/cgi-bin/koha/ill/ill-requests.pl?op=illview&amp;illrequest_id=[% matching_request_for_patron.illrequest_id | uri %]"
889
                                    </li>
888
                                            ><i class="fa fa-fw fa-external-link-square"></i>Request [% matching_request_for_patron.id_prefix | html %][% matching_request_for_patron.illrequest_id | html %]
890
                                [% END %]
889
                                            ([% matching_request_for_patron.status | html %]) on [% matching_request_for_patron.placed | html %]</a
891
                            </ul>
890
                                        >
892
                        [% END %]
893
                        [% IF remaining_matching_requests.size %]
894
                            [% IF matching_requests_for_patron.size %]
895
                                <h4>By other patrons:</h4>
896
                            [% END %]
897
                            <ul class="list-unstyled">
898
                                [% FOREACH remaining_matching_request IN remaining_matching_requests %]
899
                                    <li>
900
                                        <a href="/cgi-bin/koha/ill/ill-requests.pl?op=illview&amp;illrequest_id=[% remaining_matching_request.illrequest_id %]">ILL Request #[% remaining_matching_request.illrequest_id | html %]</a> by [%- INCLUDE 'patron-title.inc' patron => remaining_matching_request.patron hide_patron_infos_if_needed => 1 -%]
901
                                    </li>
891
                                    </li>
902
                                [% END %]
892
                                [% END %]
903
                            </ul>
893
                            </ul>
(-)a/t/db_dependent/Koha/ILL/Request/Workflow/HistoryCheck.t (-46 / +25 lines)
Lines 42-51 subtest 'show_history_check' => sub { Link Here
42
42
43
    use_ok('Koha::ILL::Request::Workflow::HistoryCheck');
43
    use_ok('Koha::ILL::Request::Workflow::HistoryCheck');
44
44
45
    my $fake_cardnumber = '123456789';
46
    my $ill_patron      = $builder->build_object(
47
        {
48
            class => 'Koha::Patrons',
49
            value => { cardnumber => $fake_cardnumber }
50
        }
51
    );
52
45
    my $metadata = {
53
    my $metadata = {
46
        title  => 'This is a title',
54
        title      => 'This is a title',
47
        author => 'This is an author',
55
        author     => 'This is an author',
48
        issn   => $issn
56
        issn       => $issn,
57
        cardnumber => $fake_cardnumber,
49
    };
58
    };
50
59
51
    # Because hashes can reorder themselves, we need to make sure ours is in a
60
    # Because hashes can reorder themselves, we need to make sure ours is in a
Lines 61-67 subtest 'show_history_check' => sub { Link Here
61
70
62
    is(
71
    is(
63
        $history_check->prep_metadata($sorted),
72
        $history_check->prep_metadata($sorted),
64
        'eyJhdXRob3IiOiJUaGlzIGlzIGFuIGF1dGhvciIsImlzc24iOiIzMjEzMjEzMjEiLCJ0aXRsZSI6%0AIlRoaXMgaXMgYSB0aXRsZSJ9%0A',
73
        'eyJhdXRob3IiOiJUaGlzIGlzIGFuIGF1dGhvciIsImNhcmRudW1iZXIiOiIxMjM0NTY3ODkiLCJp%0Ac3NuIjoiMzIxMzIxMzIxIiwidGl0bGUiOiJUaGlzIGlzIGEgdGl0bGUifQ%3D%3D%0A',
65
        'prep_metadata works'
74
        'prep_metadata works'
66
    );
75
    );
67
76
Lines 77-83 subtest 'show_history_check' => sub { Link Here
77
    # Mock ILLHistoryCheck enabled
86
    # Mock ILLHistoryCheck enabled
78
    t::lib::Mocks::mock_preference( 'ILLHistoryCheck', 1 );
87
    t::lib::Mocks::mock_preference( 'ILLHistoryCheck', 1 );
79
88
80
    my $ill_request = $builder->build_sample_ill_request();
89
    my $ill_request = $builder->build_sample_ill_request( { borrowernumber => $ill_patron->borrowernumber } );
81
    is(
90
    is(
82
        $history_check->show_history_check($ill_request),
91
        $history_check->show_history_check($ill_request),
83
        0, 'Request with ISSN ' . $issn . ' does not exist even though syspref is on. Not showing history check screen'
92
        0, 'Request with ISSN ' . $issn . ' does not exist even though syspref is on. Not showing history check screen'
Lines 92-98 subtest 'show_history_check' => sub { Link Here
92
101
93
    is(
102
    is(
94
        $history_check->show_history_check($ill_request),
103
        $history_check->show_history_check($ill_request),
95
        1, 'Request with ISSN ' . $issn . ' exists and syspref is on. Able to show history check screen'
104
        1, 'Request with ISSN ' . $issn . ' exists, syspref is on and is same patron. Able to show history check screen'
96
    );
105
    );
97
106
98
    # Mock ILLHistoryCheck disabled
107
    # Mock ILLHistoryCheck disabled
Lines 109-121 subtest 'show_history_check' => sub { Link Here
109
118
110
subtest 'after_request_created' => sub {
119
subtest 'after_request_created' => sub {
111
120
112
    plan tests => 4;
121
    plan tests => 2;
113
122
114
    $schema->storage->txn_begin;
123
    $schema->storage->txn_begin;
115
124
116
    my $builder = t::lib::TestBuilder->new;
125
    my $builder = t::lib::TestBuilder->new;
117
126
118
    my $metadata = {
127
    my $fake_cardnumber = '123456789';
128
    my $metadata        = {
119
        title  => 'This is a title',
129
        title  => 'This is a title',
120
        author => 'This is an author',
130
        author => 'This is an author',
121
        issn   => $issn
131
        issn   => $issn
Lines 127-150 subtest 'after_request_created' => sub { Link Here
127
        }
137
        }
128
    );
138
    );
129
139
130
    my $other_patron = $builder->build_object(
131
        {
132
            class => 'Koha::Patrons',
133
        }
134
    );
135
136
    t::lib::Mocks::mock_userenv( { patron => $authenticated_patron } );
140
    t::lib::Mocks::mock_userenv( { patron => $authenticated_patron } );
137
141
138
    # Create an old request
139
    my $existing_ill_request =
140
        $builder->build_sample_ill_request( { borrowernumber => $other_patron->borrowernumber } );
141
    $builder->build(
142
        {
143
            source => 'Illrequestattribute',
144
            value  => { illrequest_id => $existing_ill_request->illrequest_id, type => 'issn', value => $issn }
145
        }
146
    );
147
148
    # Create a new request with new issn
142
    # Create a new request with new issn
149
    my $new_ill_request =
143
    my $new_ill_request =
150
        $builder->build_sample_ill_request( { borrowernumber => $authenticated_patron->borrowernumber } );
144
        $builder->build_sample_ill_request( { borrowernumber => $authenticated_patron->borrowernumber } );
Lines 165-206 subtest 'after_request_created' => sub { Link Here
165
        'History check didn\'t find any matching requests. Staff notes have not been updated.'
159
        'History check didn\'t find any matching requests. Staff notes have not been updated.'
166
    );
160
    );
167
161
168
    # Create a third request with preexisting issn by other patron
162
    # Create a second request with preexisting issn by self patron
163
    my $second_ill_request =
164
        $builder->build_sample_ill_request( { borrowernumber => $authenticated_patron->borrowernumber } );
169
    my $third_ill_request =
165
    my $third_ill_request =
170
        $builder->build_sample_ill_request( { borrowernumber => $authenticated_patron->borrowernumber } );
166
        $builder->build_sample_ill_request( { borrowernumber => $authenticated_patron->borrowernumber } );
171
    $metadata->{issn} = $issn;
167
    $metadata->{issn} = $issn;
172
    $builder->build(
168
    $builder->build(
173
        {
169
        {
174
            source => 'Illrequestattribute',
170
            source => 'Illrequestattribute',
175
            value  => { illrequest_id => $third_ill_request->illrequest_id, type => 'issn', value => $issn }
171
            value  => { illrequest_id => $second_ill_request->illrequest_id, type => 'issn', value => $issn }
176
        }
172
        }
177
    );
173
    );
178
    $opac_history_check->after_request_created( $metadata, $third_ill_request );
179
180
    like(
181
        $third_ill_request->notesstaff, qr/Request has been submitted by other patrons in the past/,
182
        'Contains staffnotes related submissions by other patrons'
183
    );
184
185
    # Create a fourth request with preexisting issn by self patron and others
186
    my $fourth_ill_request =
187
        $builder->build_sample_ill_request( { borrowernumber => $authenticated_patron->borrowernumber } );
188
    $metadata->{issn} = $issn;
189
    $builder->build(
174
    $builder->build(
190
        {
175
        {
191
            source => 'Illrequestattribute',
176
            source => 'Illrequestattribute',
192
            value  => { illrequest_id => $fourth_ill_request->illrequest_id, type => 'issn', value => $issn }
177
            value  => { illrequest_id => $third_ill_request->illrequest_id, type => 'issn', value => $issn }
193
        }
178
        }
194
    );
179
    );
195
    $opac_history_check->after_request_created( $metadata, $fourth_ill_request );
180
    $opac_history_check->after_request_created( $metadata, $third_ill_request );
196
197
    like(
198
        $fourth_ill_request->notesstaff, qr/Request has been submitted by other patrons in the past/,
199
        'Contains staffnotes related submissions by other patrons'
200
    );
201
181
202
    like(
182
    like(
203
        $fourth_ill_request->notesstaff, qr/Request has been submitted by this patron in the past/,
183
        $third_ill_request->notesstaff, qr/Request has been submitted by this patron in the past/,
204
        'Contains staffnotes related submissions by self patron'
184
        'Contains staffnotes related submissions by self patron'
205
    );
185
    );
206
186
207
- 

Return to bug 38441