From 3cbcacd9bb4266bb409b5f078e23556be65f1ccf Mon Sep 17 00:00:00 2001 From: "Baptiste Wojtkowski (bwoj)" Date: Fri, 26 Sep 2025 13:56:26 +0000 Subject: [PATCH] Bug 18922: Share review with Mana This add reviews sharing with Mana. Test plan: 1 - Apply patch 2 - Activate Review Sharing with Mana (AutoShareWithMana => review) 3 - Comment a resource of your database and check you authorize sharing with Mana 4 - As librarian, validate the comment 5 - If you go again on the notice you should see the comment 6 - 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.pl | 18 +++++++ .../admin/preferences/web_services.pref | 1 + .../prog/en/modules/reviews/reviewswaiting.tt | 7 +++ .../bootstrap/en/modules/opac-review.tt | 1 + opac/opac-detail.pl | 51 +++++++++++++++++++ opac/opac-review.pl | 3 ++ reviews/reviewswaiting.pl | 39 ++++++++++++++ 7 files changed, 120 insertions(+) create mode 100755 installer/data/mysql/atomicupdate/bug_18922_add_reader_auth.pl diff --git a/installer/data/mysql/atomicupdate/bug_18922_add_reader_auth.pl b/installer/data/mysql/atomicupdate/bug_18922_add_reader_auth.pl new file mode 100755 index 0000000000..89f03c2f72 --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug_18922_add_reader_auth.pl @@ -0,0 +1,18 @@ +use Modern::Perl; + +return { + bug_number => "BUG_18922", + description => "Add flags for sharing reviews", + up => sub { + my ($args) = @_; + my ( $dbh, $out ) = @$args{qw(dbh out)}; + + $dbh->do(q{'ALTER TABLE reviews ADD COLUMN share_auth integer'}); + say $out "Added new column 'share_auth' in table 'reviews'"; + + $dbh->do(q{'ALTER TABLE reviews ADD COLUMN mana_id integer'}); + say $out "Added new column 'mana_id' in table 'reviews'"; + $dbh->do(q{'ALTER TABLE biblioitems ADD COLUMN last_mana_update date'}); + say $out "Added new column 'last_mana_update' in table 'biblioitems'"; + }, +}; 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 a1900bc746..5e4be01023 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 @@ -127,6 +127,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 051d46433a..ef7be0d660 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/reviews/reviewswaiting.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/reviews/reviewswaiting.tt @@ -43,6 +43,13 @@ [% WRAPPER 'main-container.inc' aside='tools-menu' %]

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 388909a99f..54ea96632b 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-review.tt +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-review.tt @@ -52,6 +52,7 @@ [% END %]
+ I accept to share anonymously this comment with all users of ManaKB.

Note: Your comment must be approved by a librarian.

diff --git a/opac/opac-detail.pl b/opac/opac-detail.pl index 7222edc6d6..0828b69fc0 100755 --- a/opac/opac-detail.pl +++ b/opac/opac-detail.pl @@ -78,12 +78,15 @@ use Koha::Patrons; use Koha::Plugins; use Koha::Ratings; use Koha::Reviews; +use Koha::SharedContent; use Koha::Serial::Items; use Koha::SearchEngine::Search; use Koha::SearchEngine::QueryBuilder; use Koha::Util::MARC; use JSON qw( decode_json ); +use DateTime; +use constant DURATION_BEFORE_REFRESH => DateTime::Duration->new( months => 3 ); my $query = CGI->new(); @@ -921,6 +924,54 @@ $template->param( ocoins => $coins ); my ( $loggedincommenter, $reviews ); if ( C4::Context->preference('OPACComments') ) { + 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; + } + } + } + } + $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 5f3ba40490..4e459a14cf 100755 --- a/opac/opac-review.pl +++ b/opac/opac-review.pl @@ -32,6 +32,7 @@ my $query = CGI->new; 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 $op = $query->param('op') // q{}; my ( $template, $borrowernumber, $cookie ) = get_template_and_user( @@ -74,6 +75,7 @@ if ( $op =~ /^cud-/ && !@errors && defined $review ) { { review => $clean, approved => 0, + share_auth => $share_auth, datereviewed => dt_from_string } )->store; @@ -83,6 +85,7 @@ if ( $op =~ /^cud-/ && !@errors && defined $review ) { biblionumber => $biblionumber, borrowernumber => $borrowernumber, review => $clean, + share_auth => $share_auth, datereviewed => dt_from_string } )->store->reviewid; diff --git a/reviews/reviewswaiting.pl b/reviews/reviewswaiting.pl index 451ff28dbd..1f8d4ebe27 100755 --- a/reviews/reviewswaiting.pl +++ b/reviews/reviewswaiting.pl @@ -24,6 +24,8 @@ use C4::Context; use Koha::Biblios; use Koha::Patrons; use Koha::Reviews; +use Koha::SharedContent; +use HTTP::Request; my $query = CGI->new; my ( $template, $loggedinuser, $cookie ) = get_template_and_user( @@ -46,6 +48,43 @@ my $biblionumber = $query->param('biblionumber'); if ( $op eq 'cud-approve' ) { my $review = Koha::Reviews->find($reviewid); $review->approve if $review; + + if ( C4::Context->preference("Mana") + and grep { $_ eq "review" } split( /,/, C4::Context->preference('AutoShareWithMana') ) + and $review->share_auth ) + { + my $biblioitem = Koha::Biblioitems->find( $review->biblionumber ); + my $documentid; + my $idtype; + 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 ); + $template->param( + msg => $result->{msg}, + ); + unless ( $result->{code} =~ /2../ ) { + $template->param( code => $result->{code} ); + } + } + } } elsif ( $op eq 'cud-unapprove' ) { my $review = Koha::Reviews->find($reviewid); $review->unapprove if $review; -- 2.39.5