From 17a0eb70f8645900592d19c661e357e3734d9b80 Mon Sep 17 00:00:00 2001 From: Bill Raty Date: Wed, 26 Jan 2022 16:47:52 +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: Lisette Scheer Signed-off-by: Jason Robb --- Koha/Patron/Messages.pm | 34 +++++ Koha/Schema/Result/Message.pm | 14 ++ .../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 | 121 ++++++++++++++++++ 8 files changed, 207 insertions(+), 5 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..c9cf646d59 100644 --- a/Koha/Patron/Messages.pm +++ b/Koha/Patron/Messages.pm @@ -1,3 +1,4 @@ + package Koha::Patron::Messages; # This file is part of Koha. @@ -42,8 +43,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/Koha/Schema/Result/Message.pm b/Koha/Schema/Result/Message.pm index 1ed5b0e16c..1779e89ed6 100644 --- a/Koha/Schema/Result/Message.pm +++ b/Koha/Schema/Result/Message.pm @@ -79,6 +79,14 @@ the date and time the message was written creator of message +=head2 patron_read_date + + data_type: 'timestamp' + datetime_undef_if_invalid: 1 + is_nullable: 1 + +date and time the patron dismissed the message + =cut __PACKAGE__->add_columns( @@ -101,6 +109,12 @@ __PACKAGE__->add_columns( }, "manager_id", { data_type => "integer", is_foreign_key => 1, is_nullable => 1 }, + "patron_read_date", + { + data_type => "timestamp", + datetime_undef_if_invalid => 1, + is_nullable => 1, + }, ); =head1 PRIMARY KEY 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 4411ae7604..5847925f55 100644 --- a/installer/data/mysql/kohastructure.sql +++ b/installer/data/mysql/kohastructure.sql @@ -3739,6 +3739,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 0c1d8f88dc..64f4cabb55 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/patron_messages.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/patron_messages.inc @@ -216,6 +216,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 12cb72e35c..8877e20275 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-main.tt +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-main.tt @@ -261,7 +261,7 @@ [% END %] - [% IF patron_messages && patron_messages.count > 0 || opacnote %] + [% IF patron_messages && patron_messages.unread_count || opacnote %] [% IF opacnote %]
  • @@ -272,8 +272,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..3096e7c7f2 --- /dev/null +++ b/opac/dismiss_message.pl @@ -0,0 +1,121 @@ +#!/usr/bin/perl +# Copyright 2021 AutoParallel Technologies Inc. +# Copyright 2009 PTFS Inc. +# +# 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 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, + }; + + # set default result values + my $result = { + success => FALSE, + comment => 'default failure', + inputs => $inputs, + }; + + + my $rs = Koha::Patron::Messages->search($inputs); + my $message = $rs->next(); # Koha::Patron::Messages->find($message_id); + #- exit early if no message found matching message_id + if (! defined $message) { + no warnings qw/uninitialized/; + return { + %$result, + comment => "Couldn't find message for parameters", + inputs => $inputs, + }; + } + + #- found message, so success is TRUE + $result->{success} = TRUE; + + #- exit early if patron_read_date is already set + if (defined $message->patron_read_date) { + return { + %$result, + comment => "Patron read date already set", + inputs => $inputs, + }; + } + + #- update the patron_read_date to database's NOW() + $message->update({ + patron_read_date => \'NOW()', + }); + + my $patron_read_date = $message->patron_read_date || undef; + return { + %$result, + inputs => $inputs, + comment => "Set patron_read_date to ($patron_read_date)", + }; +} + +my CGI $cgi; +my $result; +try { + $cgi = CGI->new; + $result = dismiss_patron_message($cgi); + + # uncomment assignment below if you want to view environment + # $result->{env} = \%ENV; + + +} +catch { + my ($exception) = $_; + + # no warnings qw/once/; + # my $ig = 'Examine variable $exception'; + # push @DB::typeahead, 'x $exception'; + # $DB::single++; + + $result->{exception} = $exception; + $result->{success} = FALSE; +} +finally { + # redirect to referrer + my $redir_url = $ENV{HTTP_REFERRER} || REDIRECT_URL; + print $cgi->redirect($redir_url); +}; -- 2.30.2