|
Lines 42-48
use constant DURATION_BEFORE_REFRESH => DateTime::Duration->new( months => 3 );
Link Here
|
| 42 |
use C4::Output qw( output_with_http_headers ); |
42 |
use C4::Output qw( output_with_http_headers ); |
| 43 |
my $input = new CGI; |
43 |
my $input = new CGI; |
| 44 |
binmode STDOUT, ":encoding(UTF-8)"; |
44 |
binmode STDOUT, ":encoding(UTF-8)"; |
|
|
45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 45 |
my $biblionumber = $input->param( "biblionumber" ); |
49 |
my $biblionumber = $input->param( "biblionumber" ); |
|
|
50 |
|
| 51 |
#case we come with the script import_suggestions.pl |
| 52 |
my $temp_biblionumber; |
| 53 |
eval { $temp_biblionumber = from_json( $input->param( "POSTDATA" ) )->{biblionumber}; }; |
| 54 |
if ( $temp_biblionumber ){ |
| 55 |
$biblionumber = $temp_biblionumber; |
| 56 |
} |
| 57 |
|
| 46 |
my $biblioitem = Koha::Biblioitems->find( $biblionumber ); |
58 |
my $biblioitem = Koha::Biblioitems->find( $biblionumber ); |
| 47 |
|
59 |
|
| 48 |
my $local_suggestions; |
60 |
my $local_suggestions; |
|
Lines 65-74
if ( $local_suggestions and DateTime::Duration->compare( ($now - $timestamp), DU
Link Here
|
| 65 |
push @biblios, Koha::Biblios->find( $local_suggestions->{ $key } ); |
77 |
push @biblios, Koha::Biblios->find( $local_suggestions->{ $key } ); |
| 66 |
} |
78 |
} |
| 67 |
} |
79 |
} |
|
|
80 |
|
| 68 |
else{ |
81 |
else{ |
| 69 |
# get all informations to ask mana |
82 |
# get all informations to ask mana |
| 70 |
my $idtype; |
83 |
my $idtype; |
| 71 |
my $documentid; |
84 |
my $documentid; |
|
|
85 |
my $notnormalizeddocumentid; |
| 72 |
if ( $biblioitem->isbn ){ |
86 |
if ( $biblioitem->isbn ){ |
| 73 |
$idtype= "isbn"; |
87 |
$idtype= "isbn"; |
| 74 |
$documentid= NormalizeISBN({ isbn => $biblioitem->isbn, format => 'ISBN-13', strip_hyphens => 1 }); |
88 |
$documentid= NormalizeISBN({ isbn => $biblioitem->isbn, format => 'ISBN-13', strip_hyphens => 1 }); |
|
Lines 84-163
else{
Link Here
|
| 84 |
else{ |
98 |
else{ |
| 85 |
die "error: no propper identifier"; |
99 |
die "error: no propper identifier"; |
| 86 |
} |
100 |
} |
|
|
101 |
$notnormalizeddocumentid = $documentid; |
| 102 |
my $offset = 1; |
| 103 |
my $length = 10; |
| 104 |
my $mananotover; |
| 105 |
|
| 106 |
do{ |
| 107 |
#request mana |
| 108 |
my $mana_ip = C4::Context->config('mana_config'); |
| 109 |
my $url = "$mana_ip/getsuggestion/$notnormalizeddocumentid/$idtype?offset=$offset&length=$length"; |
| 110 |
my $request = HTTP::Request->new( GET => $url ); |
| 111 |
$request->content_type('aplication/json'); |
| 112 |
my $response = Koha::SharedContent::manaRequest( $request ); |
| 113 |
|
| 114 |
#error handling |
| 115 |
my $resources = $response->{data}; |
| 116 |
unless ( $resources ){ |
| 117 |
my $msg; |
| 118 |
$msg = $response->{msg}; |
| 119 |
if ( $msg ){ |
| 120 |
die $msg; |
| 121 |
} |
| 122 |
else{ |
| 123 |
die "Unknown error"; |
| 124 |
} |
| 125 |
} |
| 87 |
|
126 |
|
| 88 |
#request mana |
127 |
if ( scalar @{ $resources } < $length ){ |
| 89 |
my $mana_ip = C4::Context->config('mana_config'); |
128 |
$mananotover = 0; |
| 90 |
my $url = "$mana_ip/getsuggestion/$documentid/$idtype"; |
|
|
| 91 |
my $request = HTTP::Request->new( GET => $url ); |
| 92 |
$request->content_type('aplication/json'); |
| 93 |
my $response = Koha::SharedContent::manaRequest( $request ); |
| 94 |
|
| 95 |
#error handling |
| 96 |
my $resources = $response->{data}; |
| 97 |
unless ( $resources ){ |
| 98 |
my $msg; |
| 99 |
$msg = $response->{msg}; |
| 100 |
if ( $msg ){ |
| 101 |
die $msg; |
| 102 |
} |
129 |
} |
| 103 |
else{ |
130 |
else{ |
| 104 |
die "Unknown error"; |
131 |
$mananotover = 1; |
| 105 |
} |
132 |
} |
| 106 |
} |
|
|
| 107 |
#create the list of owned suggested resources |
| 108 |
my $ownedbiblioitem; |
| 109 |
my $ownedbiblio; |
| 110 |
foreach my $resource ( @{ $resources } ){ |
| 111 |
|
133 |
|
| 112 |
|
134 |
|
| 113 |
# isbn processsing |
|
|
| 114 |
if ( $resource->{idtype} eq "isbn" ){ |
| 115 |
$documentid= NormalizeISBN({ isbn => $resource->{documentid}, format => 'ISBN-10', strip_hyphens => 1 }); |
| 116 |
$ownedbiblioitem = Koha::Biblioitems->search({ $resource->{idtype} => $documentid}); |
| 117 |
|
135 |
|
| 118 |
#if we don't have such a biblioitem, we try to format else the isbn |
136 |
#create the list of owned suggested resources |
| 119 |
unless ( scalar @{ $ownedbiblioitem->unblessed() } ){ |
137 |
my $ownedbiblioitem; |
| 120 |
$documentid = NormalizeISBN({ isbn => $resource->{documentid}, format => 'ISBN-13', strip_hyphens => 1 }); |
138 |
foreach my $resource ( @{ $resources } ){ |
| 121 |
$ownedbiblioitem = Koha::Biblioitems->search({ $resource->{idtype} => $documentid}); |
139 |
# isbn processsing |
| 122 |
} |
140 |
if ( $resource->{idtype} eq "isbn" ){ |
|
|
141 |
$documentid= NormalizeISBN({ isbn => $resource->{documentid}, format => 'ISBN-10', strip_hyphens => 1 }); |
| 142 |
$ownedbiblioitem = Koha::Biblioitems->search({ $resource->{idtype} => $documentid}) if $documentid; |
| 123 |
|
143 |
|
| 124 |
unless ( scalar @{ $ownedbiblioitem->unblessed() } ){ |
144 |
#if we don't have such a biblioitem, we try to format else the isbn |
| 125 |
$documentid = NormalizeISBN({ isbn => $resource->{documentid}, format => 'ISBN-10', strip_hyphens => 0 }); |
145 |
unless ( scalar @{ $ownedbiblioitem->unblessed() } ){ |
| 126 |
$ownedbiblioitem = Koha::Biblioitems->search({ $resource->{idtype} => $documentid}); |
146 |
$documentid = NormalizeISBN({ isbn => $resource->{documentid}, format => 'ISBN-13', strip_hyphens => 1 }); |
| 127 |
} |
147 |
$ownedbiblioitem = Koha::Biblioitems->search({ $resource->{idtype} => $documentid}) if $documentid; |
|
|
148 |
} |
| 128 |
|
149 |
|
| 129 |
unless ( scalar @{ $ownedbiblioitem->unblessed() } ){ |
150 |
unless ( scalar @{ $ownedbiblioitem->unblessed() } ){ |
| 130 |
$documentid = NormalizeISBN({ isbn => $resource->{documentid}, format => 'ISBN-13', strip_hyphens => 0 }); |
151 |
$documentid = NormalizeISBN({ isbn => $resource->{documentid}, format => 'ISBN-10', strip_hyphens => 0 }); |
| 131 |
$ownedbiblioitem = Koha::Biblioitems->search({ $resource->{idtype} => $documentid}); |
152 |
$ownedbiblioitem = Koha::Biblioitems->search({ $resource->{idtype} => $documentid}) if $documentid; |
| 132 |
} |
153 |
} |
| 133 |
|
154 |
|
|
|
155 |
unless ( scalar @{ $ownedbiblioitem->unblessed() } ){ |
| 156 |
$documentid = NormalizeISBN({ isbn => $resource->{documentid}, format => 'ISBN-13', strip_hyphens => 0 }); |
| 157 |
$ownedbiblioitem = Koha::Biblioitems->search({ $resource->{idtype} => $documentid}) if $documentid; |
| 158 |
} |
| 159 |
|
| 160 |
} |
| 161 |
#others ids do not need any processing |
| 162 |
else{ |
| 163 |
$ownedbiblioitem = Koha::Biblioitems->search({ $resource->{idtype} => $resource->{documentid} }); |
| 164 |
} |
| 165 |
if (scalar @{ $ownedbiblioitem->unblessed() } ){ |
| 166 |
my $ownedbiblio = Koha::Biblios->find( @{ $ownedbiblioitem->unblessed() }[0]->{biblionumber} ); |
| 167 |
push @biblios, $ownedbiblio; |
| 168 |
} |
| 134 |
} |
169 |
} |
| 135 |
#others ids do not need any processing |
170 |
#the number of requested resource is inversely proportionnal to the found_items/returned_items ratio, +1 is here just to avoid 0 |
| 136 |
else{ |
171 |
my $newlength = int( ( 10*( $length + $offset ) - $length ) / (scalar @biblios + 1 )); |
| 137 |
$ownedbiblioitem = Koha::Biblioitems->search({ $resource->{idtype} => $documentid}); |
172 |
if ( $newlength > 500 ){ |
| 138 |
} |
173 |
$newlength = 500; |
| 139 |
if (scalar @{ $ownedbiblioitem->unblessed() } ){ |
174 |
} |
| 140 |
$ownedbiblio = Koha::Biblios->find( @{ $ownedbiblioitem->unblessed() }[0]->{biblionumber} ); |
175 |
$offset += $length; |
| 141 |
push @biblios, $ownedbiblio; |
176 |
$length = $newlength; |
| 142 |
} |
177 |
|
| 143 |
} |
178 |
|
|
|
179 |
} while( (scalar @biblios < 10 ) and $mananotover ); |
| 144 |
|
180 |
|
| 145 |
#preparing new suggestion |
181 |
#preparing new suggestion |
| 146 |
my $newSuggestion; |
182 |
my $newSuggestion; |
| 147 |
my $cter = scalar @biblios; |
183 |
my $cter = scalar @biblios; |
| 148 |
$cter = 10 unless ($cter < 10); |
184 |
$cter = 10 unless ($cter < 10); |
| 149 |
$newSuggestion->{ biblionumber } = $biblionumber; |
185 |
$newSuggestion->{ biblionumber } = $biblionumber; |
| 150 |
if ( scalar @biblios < 11 ){ |
186 |
while ( $cter > 0 ){ |
| 151 |
while ( $cter > 1 ){ |
187 |
if ( $biblios[ $cter-1 ] and $biblios[ $cter-1 ]->biblionumber){ |
| 152 |
$cter--; |
188 |
$newSuggestion->{ "biblionumber".$cter }=$biblios[ $cter-1 ]->biblionumber; |
| 153 |
$newSuggestion->{ "biblionumber".$cter }=$biblios[ $cter ]->biblionumber; |
|
|
| 154 |
} |
189 |
} |
|
|
190 |
$cter--; |
| 155 |
} |
191 |
} |
| 156 |
if ( $local_suggestions ){ |
192 |
if ( $local_suggestions ){ |
| 157 |
Koha::Reading_suggestions->find( $biblionumber )->delete(); |
193 |
Koha::Reading_suggestions->find( $biblionumber )->delete(); |
| 158 |
} |
194 |
} |
| 159 |
Koha::Reading_suggestion->new( $newSuggestion )->store; |
195 |
Koha::Reading_suggestion->new( $newSuggestion )->store; |
| 160 |
|
|
|
| 161 |
} |
196 |
} |
| 162 |
|
197 |
|
| 163 |
my ( $template, $loggedinuser, $cookie ) = get_template_and_user( |
198 |
my ( $template, $loggedinuser, $cookie ) = get_template_and_user( |
|
Lines 170-178
my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
Link Here
|
| 170 |
} |
205 |
} |
| 171 |
); |
206 |
); |
| 172 |
|
207 |
|
| 173 |
|
|
|
| 174 |
$template->param( JacketImage => 1); |
208 |
$template->param( JacketImage => 1); |
| 175 |
$template->param( suggestions => \@biblios ); |
209 |
$template->param( suggestions => \@biblios ); |
| 176 |
|
210 |
|
| 177 |
|
|
|
| 178 |
output_with_http_headers $input, $cookie, $template->output, 'json'; |
211 |
output_with_http_headers $input, $cookie, $template->output, 'json'; |
| 179 |
- |
|
|