From 5f797757a30e44ad59a0fda1051e3880ba0571e7 Mon Sep 17 00:00:00 2001 From: Mark Tompsett Date: Fri, 26 May 2017 10:02:46 -0400 Subject: [PATCH] Bug 1985: Introduction of review alerts In comment #11, it was mentioned that title and biblionumber from the biblio table were not available as variables in the letter. This corrects this by converting the parameter list to a hashref, and adding the missing biblio number. It also cleans up the license on C4/Review.pm to be in line with the current coding guidelines license. Also strict and warning have been changed to the 'Use Modern::Perl;' now. Additionally, <> is now usable in the letter, as is <>. These were required additions, because comment #1 expressly says there should be a direct link to approve or delete. I also included a review everything link, since there could be multiple comments outstanding. The default letter was tweaked as well to reflect these changes. Fixed bad URLs as noted in comment #26. TEST PLAN --------- 1) Create test branch and apply patch 2) run installer/data/mysql/updatedatabase.pl -- this will add the required default letter. 3) In the staff client: a) set the CommentModeratorsEmail system preference. -- not setting it will mean you won't get any queued letters. a) set the staffClientBaseURL system preference. -- not setting it will mean your email links won't be valid (i.e http:///...). b) modify the COMMENT_CREATED default letter as desired. -- feel free to add other things. 4) In the OPAC: a) Log in b) Find something c) Click on the 'Comments' tab -- Holdings/Title notes/Comments (#) is at the bottom d) Click 'Post or edit your comments on this item' e) In the window that pops up, type your comment. f) Click 'Submit and close this window'. g) Click 'Edit' and repeat steps e and f again. 5) In a mysql client: a) Open the koha database b) SELECT * FROM message_queue WHERE letter_code='COMMENT_CREATED'; -- There should be at least two. -- They should be able to contain the borrower number, biblio title, and other borrower and biblio related things. -- Comments are at a biblio level, so items, and issues don't make sense as variables for the letter. -- Confirm the URLs are valid by pasting into a browser. 6) prove -v t/db_dependent/Koha/Reviews.t -- six new tests should run. 7) Run the koha qa test tool. --- C4/Letters.pm | 6 ++- Koha/Review.pm | 48 ++++++++++++++++++++++ installer/data/mysql/atomicupdate/bug_1985.sql | 38 +++++++++++++++++ .../data/mysql/en/mandatory/sample_notices.sql | 25 +++++++++++ installer/data/mysql/sysprefs.sql | 1 + .../prog/en/modules/admin/preferences/opac.pref | 4 ++ opac/opac-review.pl | 31 ++++++++++---- t/db_dependent/Koha/Reviews.t | 44 +++++++++++++++++++- 8 files changed, 187 insertions(+), 10 deletions(-) create mode 100644 installer/data/mysql/atomicupdate/bug_1985.sql diff --git a/C4/Letters.pm b/C4/Letters.pm index 8df9df2..97e5efc 100644 --- a/C4/Letters.pm +++ b/C4/Letters.pm @@ -719,9 +719,12 @@ sub GetPreparedLetter { } } - my $OPACBaseURL = C4::Context->preference('OPACBaseURL'); + my $OPACBaseURL = C4::Context->preference('OPACBaseURL') // ''; $letter->{content} =~ s/<>/$OPACBaseURL/go; + my $staffClientBaseURL = C4::Context->preference('staffClientBaseURL') // ''; + $letter->{content} =~ s/<>/$staffClientBaseURL/go; + if ($want_librarian) { # parsing librarian name my $userenv = C4::Context->userenv; @@ -841,6 +844,7 @@ sub _parseletter_sth { ($table eq 'aqbooksellers') ? "SELECT * FROM $table WHERE id = ?" : ($table eq 'aqorders' ) ? "SELECT * FROM $table WHERE ordernumber = ?" : ($table eq 'opac_news' ) ? "SELECT * FROM $table WHERE idnew = ?" : + ($table eq 'reviews' ) ? "SELECT * FROM $table WHERE reviewid = ?" : ($table eq 'article_requests') ? "SELECT * FROM $table WHERE id = ?" : ($table eq 'borrower_modifications') ? "SELECT * FROM $table WHERE verification_token = ?" : ($table eq 'subscription') ? "SELECT * FROM $table WHERE subscriptionid = ?" : diff --git a/Koha/Review.pm b/Koha/Review.pm index 72e3e2f..75da3e3 100644 --- a/Koha/Review.pm +++ b/Koha/Review.pm @@ -20,6 +20,7 @@ use Modern::Perl; use Carp; use Koha::Database; +use C4::Letters; use base qw(Koha::Object); @@ -67,4 +68,51 @@ sub _type { return 'Review'; } +=head3 SendReviewAlert + + $review->SendReviewAlert($parameters); + +Queue an outgoing email alert when a borrower posts a review, +using the passed parameters in a hash reference. + +=cut + +sub SendReviewAlert { + my ( $self, $params ) = @_; + + my $borrowernumber = $params->{borrowernumber}; + my $reviewid = $params->{reviewid}; + my $biblionumber = $params->{biblionumber}; + + my $moderatorEmail = C4::Context->preference('CommentModeratorsEmail'); + unless ($moderatorEmail) { + carp 'CommentModeratorsEmail is not set!'; + return; + } + + my $letter = C4::Letters::GetPreparedLetter ( + module => 'members', + letter_code => 'COMMENT_CREATED', + tables => { + 'reviews' => $reviewid, + 'borrowers' => $borrowernumber, + 'biblio' => $biblionumber, + 'biblioitems' => $biblionumber, + } + ); + unless ($letter) { + carp 'COMMENT_CREATED member letter not found!'; + return; + } + + C4::Letters::EnqueueLetter({ + letter => $letter, + borrowernumber => $borrowernumber, + message_transport_type => 'email', + to_address => $moderatorEmail, + }); + + return 1; +} + 1; diff --git a/installer/data/mysql/atomicupdate/bug_1985.sql b/installer/data/mysql/atomicupdate/bug_1985.sql new file mode 100644 index 0000000..6c23ef1 --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug_1985.sql @@ -0,0 +1,38 @@ +INSERT IGNORE INTO letter + (module, code, branchcode, name, is_html, + title, + content, message_transport_type) +VALUES + ('members', 'COMMENT_CREATED', '', 'Comment created notification', '0', + 'Comment from <> is waiting for moderation', +'Dear moderator, + +We want to inform you that borrower <> has just created a new comment on <>, which is biblionumber <>. + +--- BEGIN COMMENT --- +<> +--- END COMMENT --- + +To approve: +http://<>/cgi-bin/koha/reviews/reviewswaiting.pl?op=approve&reviewid=<> + +To delete: +http://<>/cgi-bin/koha/reviews/reviewswaiting.pl?op=delete&reviewid=<> + +Or to review all comments: +http://<>/cgi-bin/koha/reviews/reviewswaiting.pl + +Check it out! + +Your library.', +'email' + ); + +INSERT IGNORE INTO systempreferences + (variable, value,options, + explanation, + type) +VALUES + ('CommentModeratorsEmail','', '', + 'The email address where to send a notification (template code COMMENT_CREATED) when a Borrower adds/modifies a review/comment for a Biblio. Set to empty to disable sending email notifications.', + 'Textarea'); diff --git a/installer/data/mysql/en/mandatory/sample_notices.sql b/installer/data/mysql/en/mandatory/sample_notices.sql index 7a5c4da..b911abc 100644 --- a/installer/data/mysql/en/mandatory/sample_notices.sql +++ b/installer/data/mysql/en/mandatory/sample_notices.sql @@ -129,6 +129,31 @@ Your library account has been created. Please verify your email address by click If you did not initiate this request, you may safely ignore this one-time message. The request will expire shortly.' ); +INSERT INTO letter (module, code, branchcode, name, is_html, title, content, message_transport_type) +VALUES ( 'members', 'COMMENT_CREATED', '', 'Comment created notification', '0', 'Comment from <> is waiting for moderation', +'Dear moderator, + +We want to inform you that borrower <> has just created a new comment on <>, which is biblionumber <>. + +--- BEGIN COMMENT --- +<> +--- END COMMENT --- + +To approve: +http://<>/cgi-bin/koha/reviews/reviewswaiting.pl?op=approve&reviewid=<> + +To delete: +http://<>/cgi-bin/koha/reviews/reviewswaiting.pl?op=delete&reviewid=<> + +Or to review all comments: +http://<>/cgi-bin/koha/reviews/reviewswaiting.pl + +Check it out! + +Your library.', +'email' +); + INSERT INTO letter (module, code, branchcode, name, is_html, title, content) VALUES ('members', 'SHARE_INVITE', '', 'Invitation for sharing a list', '0', 'Share list <>', 'Dear patron, diff --git a/installer/data/mysql/sysprefs.sql b/installer/data/mysql/sysprefs.sql index 0bd8cac..993b299 100644 --- a/installer/data/mysql/sysprefs.sql +++ b/installer/data/mysql/sysprefs.sql @@ -110,6 +110,7 @@ INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` ('CoceHost', '', NULL, 'Coce server URL', 'Free'), ('CoceProviders', '', 'aws,gb,ol', 'Coce providers', 'multiple'), ('COinSinOPACResults','1','','If ON, use COinS in OPAC search results page. NOTE: this can slow down search response time significantly','YesNo'), +('CommentModeratorsEmail','','','The email address where to send a notification (template code COMMENT_CREATED) when a Borrower adds/modifies a review/comment for a Biblio. Set to empty to disable sending email notifications.','Textarea'), ('ConfirmFutureHolds','0','','Number of days for confirming future holds','Integer'), ('ConsiderOnSiteCheckoutsAsNormalCheckouts','1',NULL,'Consider on-site checkouts as normal checkouts','YesNo'), ('CronjobLog','0',NULL,'If ON, log information from cron jobs.','YesNo'), diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/opac.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/opac.pref index b6954ef..5ffd1a9 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/opac.pref +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/opac.pref @@ -450,6 +450,10 @@ OPAC: no: Hide - reviewer's avatar beside comments in OPAC. The avatar will be searched on www.libravatar.org using the patron's e-mail address. - + - The email address where to send a notification (template code COMMENT_CREATED) when a Borrower adds/modifies a review/comment for a Biblio. Set to empty to disable sending email notifications. + - pref: CommentModeratorsEmail + class: textarea + - - pref: RequestOnOpac choices: yes: Allow diff --git a/opac/opac-review.pl b/opac/opac-review.pl index 8fc50ec..24f9aef 100755 --- a/opac/opac-review.pl +++ b/opac/opac-review.pl @@ -31,8 +31,8 @@ use Koha::Reviews; my $query = new CGI; my $biblionumber = $query->param('biblionumber'); -my $review = $query->param('review'); -my $reviewid = $query->param('reviewid'); +my $review = $query->param('review'); # if defined submit +my $reviewid = $query->param('reviewid'); # if defined edit my ( $template, $borrowernumber, $cookie ) = get_template_and_user( { template_name => "opac-review.tt", @@ -44,7 +44,11 @@ my ( $template, $borrowernumber, $cookie ) = get_template_and_user( # FIXME: need to allow user to delete their own comment(s) -my ( $clean, @errors, $savedreview ); +my ( $clean, @errors, $savedreview, $affectedreview ); + +my $submitting = ( defined $review && $review ne q{} ) ? 1 : 0; +my $editing = ( defined $reviewid ) ? 1: 0; + my $biblio = GetBiblioData($biblionumber); if( !$biblio ) { @@ -58,7 +62,7 @@ if( !$biblio ) { $review = $savedreview? $savedreview->review: $review; } -if( !@errors && defined $review ) { +if( !@errors && $submitting ) { if ($review !~ /\S/) { push @errors, {empty=>1}; } else { @@ -72,22 +76,33 @@ if( !@errors && defined $review ) { my $js_ok_review = $clean; $js_ok_review =~ s/"/"/g; # probably redundant w/ TMPL ESCAPE=JS $template->param(clean_review=>$js_ok_review); - if ($savedreview) { - $savedreview->set( + if ($editing) { + my $updatedreview = $savedreview->set( { review => $clean, approved => 0, datereviewed => dt_from_string } )->store; + $affectedreview = $updatedreview; } else { - $reviewid = Koha::Review->new( + my $newreview = Koha::Review->new( { biblionumber => $biblionumber, borrowernumber => $borrowernumber, review => $clean, } - )->store->reviewid; + )->store; + $reviewid = $newreview ? $newreview->reviewid : 0; + $affectedreview = $newreview; } + if ($reviewid) { + $affectedreview->SendReviewAlert({ + 'borrowernumber' => $borrowernumber, + 'reviewid' => $reviewid, + 'biblionumber' => $biblionumber + }); + } + unless (@errors){ $template->param(WINDOW_CLOSE=>1); } } } diff --git a/t/db_dependent/Koha/Reviews.t b/t/db_dependent/Koha/Reviews.t index da164f7..e10da80 100644 --- a/t/db_dependent/Koha/Reviews.t +++ b/t/db_dependent/Koha/Reviews.t @@ -19,10 +19,14 @@ use Modern::Perl; -use Test::More tests => 7; +use Test::More tests => 13; +use Test::Warn; use Koha::Review; use Koha::Reviews; +use Koha::Notice::Messages; +use Koha::Notice::Templates; +use t::lib::Mocks; use Koha::Database; use t::lib::TestBuilder; @@ -35,6 +39,7 @@ my $patron_1 = $builder->build({ source => 'Borrower' }); my $patron_2 = $builder->build({ source => 'Borrower' }); my $biblio_1 = $builder->build({ source => 'Biblio' }); my $biblio_2 = $builder->build({ source => 'Biblio' }); +my $nb_of_messages = Koha::Notice::Messages->search->count; my $nb_of_reviews = Koha::Reviews->search->count; my $nb_of_approved_reviews = Koha::Reviews->search({ approved => 1 })->count; my $new_review_1_1 = Koha::Review->new({ @@ -68,6 +73,43 @@ is( $retrieved_review_1_1->review, $new_review_1_1->review, 'Find a review by id $retrieved_review_1_1->delete; is( Koha::Reviews->search->count, $nb_of_reviews + 2, 'Delete should have deleted the review' ); +# Test SendReview Alert + +# empty CommentModeratorsEmail +t::lib::Mocks::mock_preference('CommentModeratorsEmail',undef); +warning_like { +$new_review_1_2->SendReviewAlert({ + borrowernumber => $patron_1->{borrowernumber}, + biblionumber => $biblio_2->{biblionumber}, + reviewid => $new_review_1_2->reviewid +}) } qr/CommentModeratorsEmail is not set!/, 'SendReviewAlert triggered expected CommentModeratorsEmail warning.'; +is( Koha::Notice::Messages->search->count, $nb_of_messages, 'SendReviewAlert lacking CommentModeratorsEmail triggered nothing.' ); + +# set CommentModeratorsEmail +t::lib::Mocks::mock_preference('CommentModeratorsEmail','test@test.test'); +warning_is { +$new_review_1_2->SendReviewAlert({ + borrowernumber => $patron_1->{borrowernumber}, + biblionumber => $biblio_2->{biblionumber}, + reviewid => $new_review_1_2->reviewid +}) } '', 'SendReviewAlert triggered no warnings as expected.'; +is( Koha::Notice::Messages->search->count, $nb_of_messages+1, 'SendReviewAlert triggered notice.' ); + +# letter missing +Koha::Notice::Templates->search({ + module => 'members', + code => 'COMMENT_CREATED' +})->delete; +warnings_like { +$new_review_1_2->SendReviewAlert({ + borrowernumber => $patron_1->{borrowernumber}, + biblionumber => $biblio_2->{biblionumber}, + reviewid => $new_review_1_2->reviewid +}) } +[ qr/No members COMMENT_CREATED letter transported by email/, + qr/COMMENT_CREATED member letter not found!/ ] , 'SendReviewAlert triggered expected COMMENT_CREATED warnings.'; +is( Koha::Notice::Messages->search->count, $nb_of_messages+1, 'SendReviewAlert lacking letter triggered nothing new.' ); + $schema->storage->txn_rollback; 1; -- 2.1.4