From 7792ada6d3ff8460018b58fbb66c585b2c552cec Mon Sep 17 00:00:00 2001 From: Owen Leonard Date: Fri, 8 Jul 2022 18:37:55 +0000 Subject: [PATCH] Bug 12029: Patrons should be able to delete their patron messages This patch adds an option for patrons to dismiss OPAC messages sent to them by the staff. The messages remain visible in the staff interface with an indication that the message has been read. To test, apply the patch and run the database update. - View a patron record in the staff interface. - Click "Add message" and select "OPAC" from the "Add a message for..." dropdown. - Add a message. - Log in to the OPAC as that patron. - On the "Your summary" page you should see the message along with a "Dismiss" link." - Clicking the "Dismiss" link should make the message disappear. - Go to the OPAC home page. The "User summary" box in the right sidebar should have the correct count of messages. - View the patron record in the staff interface again. - The list of patron messages should still include the message which was dismissed via the OPAC, but there should be indication that the message was read, e.g. "read 11-2-2022" Signed-off-by: Martin Renvoize Signed-off-by: Christopher Brannon --- Koha/Patron/Messages.pm | 33 ++++++++ .../data/mysql/atomicupdate/bug_12029.pl | 28 +++++++ installer/data/mysql/kohastructure.sql | 1 + .../prog/en/includes/patron_messages.inc | 3 + .../bootstrap/en/includes/opac-note.inc | 5 +- .../bootstrap/en/modules/opac-main.tt | 6 +- opac/dismiss_message.pl | 82 +++++++++++++++++++ t/db_dependent/Koha/Patron/Messages.t | 41 +++++++++- 8 files changed, 193 insertions(+), 6 deletions(-) create mode 100755 installer/data/mysql/atomicupdate/bug_12029.pl create mode 100755 opac/dismiss_message.pl diff --git a/Koha/Patron/Messages.pm b/Koha/Patron/Messages.pm index 7cc729ac86..33eb7d6901 100644 --- a/Koha/Patron/Messages.pm +++ b/Koha/Patron/Messages.pm @@ -42,8 +42,41 @@ sub _type { return 'Message'; } +=head3 object_class + +=cut + sub object_class { return 'Koha::Patron::Message'; } +=head3 unread + + Returns a result set of messages that haven't been marked read by the patron + +=cut + +sub unread { + # return resultset of messages with an undefined patron_read_date + my ($self, $params) = @_; + $params ||= {}; + return $self->search({ + patron_read_date => {IS => undef}, + %$params, + }); +} + +=head3 unread_count + + Returns a count of messages that haven't been marked read by the patron + +=cut + + +sub unread_count { + # return count of unread + my ($self, $params) = @_; + $params ||= {}; + return $self->unread(%$params)->count(); +} 1; diff --git a/installer/data/mysql/atomicupdate/bug_12029.pl b/installer/data/mysql/atomicupdate/bug_12029.pl new file mode 100755 index 0000000000..f352a66edf --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug_12029.pl @@ -0,0 +1,28 @@ +use Modern::Perl; + +return { + bug_number => "12029", + description => "Enable patrons to delete messages", + up => sub { + my ($args) = @_; + my ($dbh, $out) = @$args{qw(dbh out)}; + + # Do you stuffs here + my $alteration = qq{ + ALTER TABLE messages + ADD COLUMN `patron_read_date` timestamp NULL DEFAULT NULL + COMMENT 'date and time patron dismissed message' + AFTER `manager_id` + }; + if( column_exists('messages', 'patron_read_date') ) { + say $out "NOTICE: Column 'messages.patron_read_date' already exists"; + } + else { + say $out "ALTERATION: $alteration"; + $dbh->do($alteration); + } + + # Print useful stuff here + say $out "INFO: Bug 12029 migration applied"; + }, +}; diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql index 54676b54e7..72e77dd82a 100644 --- a/installer/data/mysql/kohastructure.sql +++ b/installer/data/mysql/kohastructure.sql @@ -3813,6 +3813,7 @@ CREATE TABLE `messages` ( `message` mediumtext COLLATE utf8mb4_unicode_ci NOT NULL COMMENT 'the text of the message', `message_date` timestamp NOT NULL DEFAULT current_timestamp() COMMENT 'the date and time the message was written', `manager_id` int(11) DEFAULT NULL COMMENT 'creator of message', + `patron_read_date` timestamp NULL DEFAULT NULL COMMENT 'date and time the patron dismissed the message', PRIMARY KEY (`message_id`), KEY `messages_ibfk_1` (`manager_id`), KEY `messages_borrowernumber` (`borrowernumber`), diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/patron_messages.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/patron_messages.inc index 97219917e7..4214a8caaa 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/patron_messages.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/patron_messages.inc @@ -218,6 +218,9 @@ ( [% patron_message.get_column('manager_firstname') | html %] [% patron_message.get_column('manager_surname') | html %] ) [% END %] "[% patron_message.message | html %]" + [% IF patron_message.patron_read_date %] + read [% patron_message.patron_read_date | $KohaDates %] + [% END %] [% IF patron_message.branchcode == Branches.GetLoggedInBranchcode OR Koha.Preference('AllowAllMessageDeletion') %] Delete diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/includes/opac-note.inc b/koha-tmpl/opac-tmpl/bootstrap/en/includes/opac-note.inc index 22e6a068ce..8b2c39f861 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/includes/opac-note.inc +++ b/koha-tmpl/opac-tmpl/bootstrap/en/includes/opac-note.inc @@ -1,12 +1,13 @@ -[% IF patron_messages.count OR opacnote %] +[% IF patron_messages.unread_count OR opacnote %]

Messages for you

    - [% FOREACH message IN patron_messages %] + [% FOREACH message IN patron_messages.unread %]
  • [% message.message | html | html_line_break %]
       Written on [% message.message_date | $KohaDates %] by [% Branches.GetName(message.branchcode) | html %]
  • + Dismiss [% END %] [% IF ( opacnote ) %]
  • [% opacnote | html | html_line_break %]
  • [% END %] diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-main.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-main.tt index 8850ee7927..cd679226cb 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-main.tt +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-main.tt @@ -263,7 +263,7 @@ [% END %] - [% IF patron_messages && patron_messages.count > 0 || opacnote %] + [% IF patron_messages && patron_messages.unread_count || opacnote %] [% IF opacnote %]
  • @@ -274,8 +274,8 @@ [% ELSE %]
  • - [% patron_messages.count | html %] - [% tn('message', 'messages', patron_messages.count ) | html %] + [% patron_messages.unread_count | html %] + [% tn('message', 'messages', patron_messages.unread_count ) | html %]
  • [% END %] diff --git a/opac/dismiss_message.pl b/opac/dismiss_message.pl new file mode 100755 index 0000000000..6497060860 --- /dev/null +++ b/opac/dismiss_message.pl @@ -0,0 +1,82 @@ +#!/usr/bin/perl +# This file is part of Koha. +# +# Copyright (C) 2022 Auto-Parallel Technologies, Inc. +# +# 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 CGI qw ( -utf8 ); + +use C4::Auth qw( get_template_and_user ); +use C4::Output; +use Koha::Patron::Messages; + +use Try::Tiny; +use JSON qw//; + +use constant { + TRUE => 1, + FALSE => 0, + json => JSON->new->pretty, + REDIRECT_URL => '/cgi-bin/koha/opac-user.pl', +}; + +sub dismiss_patron_message { + # accept a CGI object as input + my ($cgi) = @_; + + # get input parameters + my $message_id = $cgi->param('message_id') || undef; + my $borrowernumber = $cgi->param('borrowernumber') || undef; + my $inputs = { + message_id => $message_id, + borrowernumber => $borrowernumber, + }; + + my $rs = Koha::Patron::Messages->search($inputs); + my $message = $rs->next(); + + #- exit early if no message found matching message_id + if (! defined $message) { + return; + } + + #- exit early if patron_read_date is already set + if (defined $message->patron_read_date) { + return; + } + + #- update the patron_read_date to database's NOW() + $message->update({ + patron_read_date => \'NOW()', + }); + + return; +} + +my CGI $cgi; +try { + $cgi = CGI->new; + dismiss_patron_message($cgi); +} +catch { + my ($exception) = $_; +} +finally { + # redirect to referrer + my $redir_url = $ENV{HTTP_REFERRER} || REDIRECT_URL; + print $cgi->redirect($redir_url); +}; diff --git a/t/db_dependent/Koha/Patron/Messages.t b/t/db_dependent/Koha/Patron/Messages.t index 06de4d2934..262d89b019 100755 --- a/t/db_dependent/Koha/Patron/Messages.t +++ b/t/db_dependent/Koha/Patron/Messages.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 12; +use Test::More tests => 16; use C4::Context; use Koha::ActionLogs; @@ -99,6 +99,45 @@ $new_message_2->delete; is( Koha::Patron::Messages->search->count, $nb_of_messages + 1, 'Delete should have deleted the message 2' ); is( get_nb_of_logactions(), $nb_of_logaction + 3, 'With BorrowersLog on, 1 new log should have been added when deleting a new message' ); +# START BUG-12029 +my $starting_unread_count = Koha::Patron::Messages->unread_count(); + +my $new_message_4 = Koha::Patron::Message->new( + { + borrowernumber => $patron->{borrowernumber}, + branchcode => $library->{branchcode}, + message_type => 'B', + message => 'testing unread', + manager_id => $patron_3->{borrowernumber}, + } +)->store; + +is( + Koha::Patron::Messages->unread_count(), + $starting_unread_count + 1, + "Unread count should return a new count one higher" +); +is( + Koha::Patron::Messages->unread->count(), + $starting_unread_count + 1, + "Unread should be able to have count called on it and return the same number" +); + +ok( ( grep { $_->message eq $new_message_4->message } @{ Koha::Patron::Messages->unread->as_list } ), + "Unread should return our new_message_4" ); + +# Set a message to be read +$new_message_4->update( + { + patron_read_date => \'NOW()', + } +); + +is( Koha::Patron::Messages->unread_count(), + $starting_unread_count, "Unread count should return the original count of messages" ); +# END BUG-12029 + + $schema->storage->txn_rollback; sub get_nb_of_logactions { -- 2.30.2