From d3bd793d30a289f6f8f26babbc5a27d62998c697 Mon Sep 17 00:00:00 2001 From: Baptiste Wojtkowski Date: Mon, 10 Jul 2017 12:33:26 +0000 Subject: [PATCH] Bug 18922: Share review with Mana This add reviews sharing with Mana. Test plan: 1 - Check Bug 17057 is installed and works on your Koha 2 - Apply patch 3 - Activate Review Sharing with Mana (AutoShareWithMana => review) 4 - Comment a resource of your database and check you authorize sharing with Mana 5 - As librarian, validate the comment 6 - If you go again on the notice you should see the comment 7 - Try again step 4-6 without accepting to share with Mana => it won't share Note: The resource must have a valid isbn/issn/ean in the database field. --- .../atomicupdate/bug_18922_add_reader_auth.sql | 4 ++ .../en/modules/admin/preferences/web_services.pref | 1 + .../prog/en/modules/reviews/reviewswaiting.tt | 9 +++- .../opac-tmpl/bootstrap/en/modules/opac-review.tt | 1 + opac/opac-detail.pl | 58 ++++++++++++++++++++++ opac/opac-review.pl | 3 ++ reviews/reviewswaiting.pl | 47 ++++++++++++++++++ 7 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 installer/data/mysql/atomicupdate/bug_18922_add_reader_auth.sql diff --git a/installer/data/mysql/atomicupdate/bug_18922_add_reader_auth.sql b/installer/data/mysql/atomicupdate/bug_18922_add_reader_auth.sql new file mode 100644 index 0000000..b9e9a9d --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug_18922_add_reader_auth.sql @@ -0,0 +1,4 @@ +ALTER TABLE reviews ADD COLUMN share_auth integer; +ALTER TABLE reviews ADD COLUMN mana_id integer UNIQUE; + +ALTER TABLE biblioitems ADD COLUMN last_mana_update date; diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/web_services.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/web_services.pref index 279302b..f471d90 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/web_services.pref +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/web_services.pref @@ -65,6 +65,7 @@ Web services: - pref: AutoShareWithMana multiple: subscription: Subscriptions + review: Reviews Reporting: - - Only return diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/reviews/reviewswaiting.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/reviews/reviewswaiting.tt index 5373fc3..f21fd20 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/reviews/reviewswaiting.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/reviews/reviewswaiting.tt @@ -14,8 +14,15 @@
+

Comments

-

Comments

+ [% IF msg %] + [% IF code %] +
Error occured, mana server returned [% msg %] with statuscode [% code %]
+ [% ELSE %] +
[% msg %]
+ [% END %] + [% END %]
diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-review.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-review.tt index 7e537b6..127234c 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-review.tt +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-review.tt @@ -49,6 +49,7 @@ [% IF ( reviewid ) %][% END %]
+ I Accept to share anonymously this comment with all users of the Mana KB.

Note: Your comment must be approved by a librarian.

diff --git a/opac/opac-detail.pl b/opac/opac-detail.pl index f2c2f6f..fe5dd1c 100755 --- a/opac/opac-detail.pl +++ b/opac/opac-detail.pl @@ -55,6 +55,10 @@ use Koha::ItemTypes; use Koha::Virtualshelves; use Koha::Ratings; use Koha::Reviews; +use Koha::SharedContent; + +use DateTime; +use constant DURATION_BEFORE_REFRESH => DateTime::Duration->new( months => 3 ); BEGIN { if (C4::Context->preference('BakerTaylorEnabled')) { @@ -819,6 +823,60 @@ $template->param( my ( $loggedincommenter, $reviews ); if ( C4::Context->preference('reviewson') ) { + if ( C4::Context->preference( 'Mana' ) ){ + my $now = DateTime->now(); + my $biblioitem = Koha::Biblioitems->find( $biblionumber ); + $biblioitem = $biblioitem->unblessed if $biblioitem; + my $last_update; + eval {$last_update = DateTime::Format::MySQL->parse_date( $biblioitem->{last_mana_update} )}; + if ( (!$last_update) or DateTime::Duration->compare( ( $now - $last_update ), DURATION_BEFORE_REFRESH ) == -1 ){ + my $idtype; + my $documentid; + if ( $isbn ){ + $idtype= 'isbn'; + $documentid = $isbn; + } + elsif ( $biblioitem->{issn} ){ + $idtype = 'issn'; + $documentid= $biblioitem->{issn}; + } + elsif ( $biblioitem->{ean} ){ + $idtype = 'ean'; + $documentid= $ean; + } + + if ( $documentid and $idtype ){ + my $mana_ip = C4::Context->config('mana_config'); + my $url = "$mana_ip/review.json?documentid=$documentid&idtype=$idtype"; + my $request = HTTP::Request->new( GET => $url ); + $request->content_type('aplication/json'); + my $response = Koha::SharedContent::manaRequest( $request ); + my $mana_reviews = $response->{data}; + foreach my $mana_review ( @$mana_reviews ){ + my $existing = Koha::Reviews->search( mana_id => $mana_review->{id}); + unless ( scalar @{ $existing->unblessed } ){ + my $review; + $review->{borrowernumber} = undef; + $review->{biblionumber} = $biblionumber; + $review->{review} = $mana_review->{review}; + $review->{approved} = 1; + $review->{datereviewed} = DateTime->now(); + $review->{share_auth} = 1; + $review->{mana_id} = $mana_review->{id}; + Koha::Review->new( $review )->store; + } + } + } + } + my $biblioitem = Koha::Biblioitems->find( $biblionumber ); + $biblioitem->last_mana_update( $now->ymd() ); + $biblioitem->store(); + + } + + + + $reviews = Koha::Reviews->search( { biblionumber => $biblionumber, diff --git a/opac/opac-review.pl b/opac/opac-review.pl index f8e86ba..90b9265 100755 --- a/opac/opac-review.pl +++ b/opac/opac-review.pl @@ -33,6 +33,7 @@ my $query = new CGI; my $biblionumber = $query->param('biblionumber'); my $review = $query->param('review'); my $reviewid = $query->param('reviewid'); +my $share_auth = ( $query->param('share_auth') eq 'on') ? 1 : 0 ; my ( $template, $borrowernumber, $cookie ) = get_template_and_user( { template_name => "opac-review.tt", @@ -78,6 +79,7 @@ if( !@errors && defined $review ) { review => $clean, approved => 0, datereviewed => dt_from_string + share_auth => $share_auth, } )->store; } else { @@ -85,6 +87,7 @@ if( !@errors && defined $review ) { { biblionumber => $biblionumber, borrowernumber => $borrowernumber, review => $clean, + share_auth => $share_auth, } )->store->reviewid; } diff --git a/reviews/reviewswaiting.pl b/reviews/reviewswaiting.pl index 452b54c..f508a42 100755 --- a/reviews/reviewswaiting.pl +++ b/reviews/reviewswaiting.pl @@ -24,6 +24,10 @@ use C4::Context; use C4::Members; use C4::Biblio; use Koha::Reviews; +use Koha::SharedContent; + +use HTTP::Request; + my $query = new CGI; my ( $template, $loggedinuser, $cookie ) = get_template_and_user( @@ -47,6 +51,49 @@ my $total = Koha::Reviews->search({ approved => $status })->count; if ( $op eq 'approve' ) { my $review = Koha::Reviews->find( $reviewid ); $review->approve if $review; + my $biblioitem = Koha::Biblioitems->find( $review->biblionumber ); + my $documentid; + my $idtype; + if ( C4::Context->preference("Mana") + and grep {$_ eq "review"} split( /,/, C4::Context->preference('AutoShareWithMana')) + and $review->share_auth){ + + #searches for a valid identifier for Mana + if ( $biblioitem->isbn ){ + $documentid = $biblioitem->isbn; + $idtype = 'isbn'; + } + elsif ( $biblioitem->issn ){ + $documentid = $biblioitem->issn; + $idtype = 'issn'; + } + elsif ( $biblioitem->ean ){ + $documentid = $biblioitem->ean; + $idtype = 'ean'; + } + + #if the resource can be identified by Mana + if ( $documentid and $idtype ){ + my $reviewforMana = { + reviewid => $review->reviewid, + review => $review->review, + documentid => $documentid, + idtype => $idtype, + language => C4::Context->preference('language') + }; + my $result = Koha::SharedContent::manaPostRequest( 'review', $reviewforMana ); +warn Data::Dumper::Dumper( $result ); + $template->param( + msg => $result->{msg}, + ); + unless ( $result->{code} =~ /2../){ + $template->param( + code => $result->{code} + ); + } + } + } + } elsif ( $op eq 'unapprove' ) { my $review = Koha::Reviews->find( $reviewid ); -- 2.7.4