From 64865b6ee5fb61a6d9d1c6279adbe7cd3c7e618a Mon Sep 17 00:00:00 2001 From: Baptiste Wojtkowski Date: Mon, 15 May 2017 13:58:58 +0000 Subject: [PATCH] Bug 18618: Mana - Add reading suggestion There is now a module to get suggestions from Mana. - It is activated by default on Mana. - When you browse on a notice, there is now a tab called "Suggestions" - On click on the tab, it will check in the database if it has suggestions. - Else, it will ask to Mana for suggestions and will store in the database. - The suggestions are stored in the NEW table reading_pairs - Implement new dbix objets: Reading_suggestion and Reading_suggestions Signed-off-by: Brendan A Gallagher --- Koha/Biblio.pm | 14 ++ Koha/Reading_suggestion.pm | 50 ++++++ Koha/Reading_suggestions.pm | 58 +++++++ .../atomicupdate/mana_add_table_reading_pairs.sql | 16 ++ .../bootstrap/en/modules/mana/mana_suggestions.tt | 40 +++++ .../opac-tmpl/bootstrap/en/modules/opac-detail.tt | 30 ++++ opac/svc/mana/getSuggestion | 178 +++++++++++++++++++++ 7 files changed, 386 insertions(+) create mode 100644 Koha/Reading_suggestion.pm create mode 100644 Koha/Reading_suggestions.pm create mode 100644 installer/data/mysql/atomicupdate/mana_add_table_reading_pairs.sql create mode 100644 koha-tmpl/opac-tmpl/bootstrap/en/modules/mana/mana_suggestions.tt create mode 100755 opac/svc/mana/getSuggestion diff --git a/Koha/Biblio.pm b/Koha/Biblio.pm index aad4343..8841b57 100644 --- a/Koha/Biblio.pm +++ b/Koha/Biblio.pm @@ -22,6 +22,7 @@ use Modern::Perl; use Carp; use C4::Biblio qw(); +use C4::Koha qw(NormalizeISBN); use Koha::Database; use Koha::DateUtils qw( dt_from_string ); @@ -319,6 +320,19 @@ sub subscriptions { =cut +=head3 normalized_isbn + +my $normalized_isbn = $biblio->normalizedisbn(); + +Return the normalized isbn of the biblio ( isbn-10 without hyphens ) + +=cut + +sub normalized_isbn { + my $self = shift; + return NormalizeISBN({ isbn => $self->biblioitem->isbn, format => 'ISBN-10', strip_hyphens => 1 }); +} + sub _type { return 'Biblio'; } diff --git a/Koha/Reading_suggestion.pm b/Koha/Reading_suggestion.pm new file mode 100644 index 0000000..485cdce --- /dev/null +++ b/Koha/Reading_suggestion.pm @@ -0,0 +1,50 @@ +package Koha::Reading_suggestion; + +# Copyright Biblibre 2017 - Baptiste Wojtkowski +# +# 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, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; + +use Carp; + +use Koha::Database; +use Koha::DateUtils qw( dt_from_string ); + +use base qw(Koha::Object); + + +=head1 NAME + +Koha::Biblio - Koha Biblio Object class + +=head1 API + +=head2 Class Methods + +=cut + +sub _type { + return 'ReadingSuggestion'; +} + +=head1 AUTHOR + +Baptiste Wojtkowski + +=cut + +1; diff --git a/Koha/Reading_suggestions.pm b/Koha/Reading_suggestions.pm new file mode 100644 index 0000000..93c7c14 --- /dev/null +++ b/Koha/Reading_suggestions.pm @@ -0,0 +1,58 @@ +package Koha::Reading_suggestions; + +# Copiright Biblibre 2017 - Baptiste Wojtkowski +# +# 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, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; +use Carp; +use Koha::Database; +use Koha::Reading_suggestion; +use base qw(Koha::Objects); + +=head1 NAME + +Koha::Reading_suggestions - Koha Reading_suggestion object set class + +=head1 API + +=head2 Class Methods + +=cut + +=head3 type + +=cut + +sub _type { + return 'ReadingSuggestion'; +} + +=head3 object_class + +=cut + +sub object_class { + return 'Koha::Reading_suggestion'; +} + +=head1 AUTHOR + +Baptiste Wojtkowski + +=cut + +1 diff --git a/installer/data/mysql/atomicupdate/mana_add_table_reading_pairs.sql b/installer/data/mysql/atomicupdate/mana_add_table_reading_pairs.sql new file mode 100644 index 0000000..eea656d --- /dev/null +++ b/installer/data/mysql/atomicupdate/mana_add_table_reading_pairs.sql @@ -0,0 +1,16 @@ +-- biblionumber1 not null otherwise the data is useless +CREATE TABLE `reading_suggestion` ( + `biblionumber` int(11) NOT NULL, + `biblionumber1` int(11) NOT NULL, + `biblionumber2` int(11) DEFAULT NULL, + `biblionumber3` int(11) DEFAULT NULL, + `biblionumber4` int(11) DEFAULT NULL, + `biblionumber5` int(11) DEFAULT NULL, + `biblionumber6` int(11) DEFAULT NULL, + `biblionumber7` int(11) DEFAULT NULL, + `biblionumber8` int(11) DEFAULT NULL, + `biblionumber9` int(11) DEFAULT NULL, + `biblionumber10` int(11) DEFAULT NULL, + `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (`biblionumber`) +); diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/mana/mana_suggestions.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/mana/mana_suggestions.tt new file mode 100644 index 0000000..5033a50 --- /dev/null +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/mana/mana_suggestions.tt @@ -0,0 +1,40 @@ +[% USE Koha %] +[% IF suggestions %] + + + + + + + + + + + + [% FOREACH suggestion IN suggestions %] + + + + + + + + + [% END # /FOREACH suggestions %] + +
[% issues_count %] Item(s) you should try
Cover ImageTitleAuthorPublisher
+ [% IF ( Koha.Preference('OPACAmazonCoverImages') ) %] + + [% IF ( suggestion.normalized_isbn() ) %] + View on Amazon.com + [% ELSE %] + No cover image available + [% END %] + [% END %] + + [% suggestion.title |html %] [% FOREACH subtitl IN suggestion.subtitles %] [% subtitl.subfield %][% END %] + [% suggestion.author %] + [% suggestion.biblioitem.publishercode %]
+[% ELSE %] +

There are no available suggestions on mana KB

+[% END # IF suggestions %] diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-detail.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-detail.tt index d95dc8c..4c51b5c 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-detail.tt +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-detail.tt @@ -623,6 +623,10 @@ [% IF ( IDreamBooksReviews ) %] [% END %] + + [% IF Koha.Preference( 'Mana' ) == 1 %] +
  • Suggestion
  • + [% END %] [% IF ( serialcollection ) %] @@ -899,6 +903,11 @@ [% END # / IF LibraryThingForLibrariesID && LibraryThingForLibrariesTabbedView %] + [% IF Koha.Preference( 'Mana' ) == 1 %] +
    +
    + [% END # / IF Koha.Preference( 'Mana') ==1 %] + [% IF Koha.Preference( 'reviewson' ) == 1 %]
    @@ -1926,4 +1935,25 @@ //]]> [% END # / IF OPACPopupAuthorsSearch %] + +[% IF Koha.Preference('Mana') %] + +[% END %] + [% END %] diff --git a/opac/svc/mana/getSuggestion b/opac/svc/mana/getSuggestion new file mode 100755 index 0000000..3760451 --- /dev/null +++ b/opac/svc/mana/getSuggestion @@ -0,0 +1,178 @@ +#!/usr/bin/perl + +# Copyright 2017 BibLibre Baptiste Wojtkowski +# +# 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 Koha::SharedContent; +use Koha::Biblioitems; +use Koha::Biblioitem; +use Koha::Reading_suggestions; +use Koha::Reading_suggestion; + +use Koha; +use C4::Koha; + +use C4::Auth qw(check_cookie_auth), qw(get_template_and_user); + +use CGI; +use JSON; + +use DateTime; +use DateTime::Format::MySQL; +use DateTime::Duration; + +use constant DURATION_BEFORE_REFRESH => DateTime::Duration->new( months => 3 ); + +use C4::Output qw( output_with_http_headers ); +my $input = new CGI; +binmode STDOUT, ":encoding(UTF-8)"; +my $biblionumber = $input->param( "biblionumber" ); +my $biblioitem = Koha::Biblioitems->find( $biblionumber ); + +my $local_suggestions; + +my $now; +my $timestamp; +my $temp; +#check it doesn't already have the informations localy stored +eval { + $local_suggestions = Koha::Reading_suggestions->find( $biblionumber )->unblessed(); + $now = DateTime->now(); + $timestamp = DateTime::Format::MySQL->parse_timestamp( $local_suggestions->{timestamp} ); + $temp = $now - $timestamp; +}; +my @biblios; +if ( $local_suggestions and DateTime::Duration->compare( ($now - $timestamp), DURATION_BEFORE_REFRESH ) == -1 ){ + delete $local_suggestions->{timestamp}; + delete $local_suggestions->{biblionumber}; + foreach my $key ( sort keys %{ $local_suggestions }){ + push @biblios, Koha::Biblios->find( $local_suggestions->{ $key } ); + } +} +else{ + # get all informations to ask mana + my $idtype; + my $documentid; + if ( $biblioitem->isbn ){ + $idtype= "isbn"; + $documentid= NormalizeISBN({ isbn => $biblioitem->isbn, format => 'ISBN-13', strip_hyphens => 1 }); + } + elsif ( $biblioitem->issn ){ + $idtype= "issn"; + $documentid= $biblioitem->issn; + } + elsif ( $biblioitem->ean ){ + $idtype= "ean"; + $documentid= $biblioitem->ean; + } + else{ + die "error: no propper identifier"; + } + + #request mana + my $mana_ip = C4::Context->config('mana_config'); + my $url = "$mana_ip/getsuggestion/$documentid/$idtype"; + my $request = HTTP::Request->new( GET => $url ); + $request->content_type('aplication/json'); + my $response = Koha::SharedContent::manaRequest( $request ); + + #error handling + my $resources = $response->{data}; + unless ( $resources ){ + my $msg; + $msg = $response->{msg}; + if ( $msg ){ + die $msg; + } + else{ + die "Unknown error"; + } + } + #create the list of owned suggested resources + my $ownedbiblioitem; + my $ownedbiblio; + foreach my $resource ( @{ $resources } ){ + + + #  isbn processsing + if ( $resource->{idtype} eq "isbn" ){ + $documentid= NormalizeISBN({ isbn => $resource->{documentid}, format => 'ISBN-10', strip_hyphens => 1 }); + $ownedbiblioitem = Koha::Biblioitems->search({ $resource->{idtype} => $documentid}); + + #if we don't have such a biblioitem, we try to format else the isbn + unless ( scalar @{ $ownedbiblioitem->unblessed() } ){ + $documentid = NormalizeISBN({ isbn => $resource->{documentid}, format => 'ISBN-13', strip_hyphens => 1 }); + $ownedbiblioitem = Koha::Biblioitems->search({ $resource->{idtype} => $documentid}); + } + + unless ( scalar @{ $ownedbiblioitem->unblessed() } ){ + $documentid = NormalizeISBN({ isbn => $resource->{documentid}, format => 'ISBN-10', strip_hyphens => 0 }); + $ownedbiblioitem = Koha::Biblioitems->search({ $resource->{idtype} => $documentid}); + } + + unless ( scalar @{ $ownedbiblioitem->unblessed() } ){ + $documentid = NormalizeISBN({ isbn => $resource->{documentid}, format => 'ISBN-13', strip_hyphens => 0 }); + $ownedbiblioitem = Koha::Biblioitems->search({ $resource->{idtype} => $documentid}); + } + + } + #others ids do not need any processing + else{ + $ownedbiblioitem = Koha::Biblioitems->search({ $resource->{idtype} => $documentid}); + } + if (scalar @{ $ownedbiblioitem->unblessed() } ){ + $ownedbiblio = Koha::Biblios->find( @{ $ownedbiblioitem->unblessed() }[0]->{biblionumber} ); + push @biblios, $ownedbiblio; + } + } + + #preparing new suggestion + my $newSuggestion; + my $cter = scalar @biblios; + $cter = 10 unless ($cter < 10); + $newSuggestion->{ biblionumber } = $biblionumber; + if ( scalar @biblios < 11 ){ + while ( $cter > 1 ){ + $cter--; + $newSuggestion->{ "biblionumber".$cter }=$biblios[ $cter ]->biblionumber; + } + } + if ( $local_suggestions ){ + Koha::Reading_suggestions->find( $biblionumber )->delete(); + } + Koha::Reading_suggestion->new( $newSuggestion )->store; + +} + +my ( $template, $loggedinuser, $cookie ) = get_template_and_user( + { + template_name => "mana/mana_suggestions.tt", + query => $input, + type => "opac", + authnotrequired => 1, + debug => 1, + } +); + + +$template->param( JacketImage => 1); +$template->param( suggestions => \@biblios ); + + +output_with_http_headers $input, $cookie, $template->output, 'json'; -- 2.1.4