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

(-)a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-detail.tt (-1 / +1 lines)
Lines 1916-1924 Link Here
1916
1916
1917
[% IF Koha.Preference('Mana') %]
1917
[% IF Koha.Preference('Mana') %]
1918
    <script>
1918
    <script>
1919
        $("#reading_suggestions").html("");
1920
        $(document).ready( function(){
1919
        $(document).ready( function(){
1921
            $( "#tab_suggestions" ).one( "click", function(){
1920
            $( "#tab_suggestions" ).one( "click", function(){
1921
                $("#reading_suggestions").html("<h1> Asking Mana Servers.. Please wait..</h1>");
1922
                $.ajax({
1922
                $.ajax({
1923
                    type: "POST",
1923
                    type: "POST",
1924
                    url: "/cgi-bin/koha/svc/mana/getSuggestion",
1924
                    url: "/cgi-bin/koha/svc/mana/getSuggestion",
(-)a/misc/cronjobs/mana_send_pairs.pl (-7 / +11 lines)
Lines 1-3 Link Here
1
#/!usr/bin/perl
1
use Modern::Perl;
2
use Modern::Perl;
2
3
3
use C4::Context;
4
use C4::Context;
Lines 15-31 my $man = 0; Link Here
15
my $verbose = 0;
16
my $verbose = 0;
16
my $date=DateTime->now()->strftime("%F");
17
my $date=DateTime->now()->strftime("%F");
17
18
18
print $date;
19
my $securitytoken;
19
20
my $MANA_IP;
20
21
21
GetOptions(
22
GetOptions(
22
            'help|?'         => \$help,
23
            'help|?'         => \$help,
23
            'man'            => \$man,
24
            'man'            => \$man,
24
            'v'              => \$verbose,
25
            'v'              => \$verbose,
25
            'date|d:s'       => \$date
26
            'date|d:s'       => \$date
27
            'security_token|s:s' => \$securitytoken,
28
            'mana_ip|i:s'    => \$MANA_IP
26
);
29
);
27
30
28
31
my $securitytoken = C4::Context->preference('ManaToken');
29
$date.="%";
32
$date.="%";
30
my $dbh = C4::Context->dbh;
33
my $dbh = C4::Context->dbh;
31
my $query = q{
34
my $query = q{
Lines 68-73 $sth->execute; Link Here
68
my $row;
71
my $row;
69
my @reading_pairs;
72
my @reading_pairs;
70
my $cter = 0;
73
my $cter = 0;
74
75
#for each reading pair, processes the id pairs
71
while ( $row = $sth->fetchrow_hashref ){
76
while ( $row = $sth->fetchrow_hashref ){
72
$row->{isbn1} = NormalizeISBN({ isbn => $row->{isbn1}, format => 'ISBN-13', strip_hyphens => 1  });
77
$row->{isbn1} = NormalizeISBN({ isbn => $row->{isbn1}, format => 'ISBN-13', strip_hyphens => 1  });
73
$row->{isbn2} = NormalizeISBN({ isbn => $row->{isbn2}, format => 'ISBN-13', strip_hyphens => 1 });
78
$row->{isbn2} = NormalizeISBN({ isbn => $row->{isbn2}, format => 'ISBN-13', strip_hyphens => 1 });
Lines 80-96 $row->{isbn2} = NormalizeISBN({ isbn => $row->{isbn2}, format => 'ISBN-13', stri Link Here
80
                 $currentpair->{ idtype2 } = $key2;
85
                 $currentpair->{ idtype2 } = $key2;
81
                 $currentpair->{ documentid2 } = $row->{ $key2."2" };
86
                 $currentpair->{ documentid2 } = $row->{ $key2."2" };
82
                 push @reading_pairs, $currentpair;
87
                 push @reading_pairs, $currentpair;
83
84
            }
88
            }
85
        }
89
        }
86
    }
90
    }
87
}
91
}
88
92
89
my $securitytoken = C4::Context->preference('ManaToken');
90
my $MANA_IP;
91
my $content;
93
my $content;
94
95
#informations for mana
92
$MANA_IP=C4::Context->config("mana_config");
96
$MANA_IP=C4::Context->config("mana_config");
93
print $MANA_IP;
94
$content->{ securitytoken } = $securitytoken;
97
$content->{ securitytoken } = $securitytoken;
95
$content->{ reading_pairs } = \@reading_pairs;
98
$content->{ reading_pairs } = \@reading_pairs;
96
99
Lines 100-105 my $request = HTTP::Request->new( POST => $url ); Link Here
100
my $json = to_json( $content, { utf8 => 1 } );
103
my $json = to_json( $content, { utf8 => 1 } );
101
#print $json;
104
#print $json;
102
105
106
#send to mana
103
$request->content($json);
107
$request->content($json);
104
my $result = Koha::SharedContent::manaRequest( $request );
108
my $result = Koha::SharedContent::manaRequest( $request );
105
print Data::Dumper::Dumper( $result );
109
print Data::Dumper::Dumper( $result );
(-)a/opac/svc/mana/getSuggestion (-54 / +86 lines)
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
- 

Return to bug 18618