From 0e70cd0393cf12de474597b7ddbabd41eb64a901 Mon Sep 17 00:00:00 2001
From: Aleisha Amohia <aleishaamohia@hotmail.com>
Date: Wed, 15 Feb 2023 21:57:08 +1300
Subject: [PATCH] Bug 12029: Ability for patrons to dismiss OPAC messages

This enhancement adds the ability for patrons to dismiss an OPAC
message, marking it as read to remove it from their summary page.

To test:
1) Update database and restart services
2) Log into the staff interface and go to your patron account
3) Click the Add message button
4) Add a message for the OPAC and Save
5) Log into the OPAC. Note there is a message on the homepage saying you
have a message. Go to your user summary and confirm the message
displays.
6) Click the button to dismiss the message. A confirmation box should
pop up - hitting Cancel should stop the action.
7) Dismiss the message again and this time Confirm. Make sure the
message is gone from the OPAC user summary and from the homepage.
8) Confirm tests pass t/db_dependent/Koha/Patron/Messages.t

Sponsored-by: Koha-US
---
 Koha/Patron/Messages.pm                            | 23 +++++++++
 .../prog/en/includes/patron_messages.inc           |  3 ++
 .../opac-tmpl/bootstrap/en/includes/opac-note.inc  |  9 +++-
 .../opac-tmpl/bootstrap/en/modules/opac-main.tt    |  2 +-
 .../opac-tmpl/bootstrap/en/modules/opac-user.tt    |  4 ++
 opac/opac-dismiss-message.pl                       | 54 ++++++++++++++++++++++
 6 files changed, 92 insertions(+), 3 deletions(-)
 create mode 100755 opac/opac-dismiss-message.pl

diff --git a/Koha/Patron/Messages.pm b/Koha/Patron/Messages.pm
index 7cc729ac867..063c70364b2 100644
--- a/Koha/Patron/Messages.pm
+++ b/Koha/Patron/Messages.pm
@@ -34,6 +34,25 @@ Koha::Patron::Messages - Koha Message Object set class
 
 =cut
 
+=head3 filter_by_unread
+
+    Returns a resultset of messages that have not been marked read by the patron
+
+=cut
+
+sub filter_by_unread {
+    my ( $self, $params ) = @_;
+
+    $params ||= {};
+
+    return $self->search(
+        {
+            patron_read_date => { is => undef },
+            %$params,
+        }
+    );
+}
+
 =head3 _type
 
 =cut
@@ -42,6 +61,10 @@ sub _type {
     return 'Message';
 }
 
+=head3 object_class
+
+=cut
+
 sub object_class {
     return 'Koha::Patron::Message';
 }
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 db9c3430fcf..13e160eefe0 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/includes/patron_messages.inc
+++ b/koha-tmpl/intranet-tmpl/prog/en/includes/patron_messages.inc
@@ -308,6 +308,9 @@
                             ( <a href="/cgi-bin/koha/members/moremember.pl?borrowernumber=[% patron_message.manager_id | uri %]">[% patron_message.get_column('manager_firstname') | html %] [% patron_message.get_column('manager_surname') | html %]</a> )
                         [% END %]
                         <em>"[% patron_message.message | html %]"</em>
+                        [% IF patron_message.patron_read_date %]
+                            Read: <em>[% patron_message.patron_read_date | $KohaDates %]</em>
+                        [% END %]
                     </span>
                     [% IF patron_message.branchcode == Branches.GetLoggedInBranchcode OR Koha.Preference('AllowAllMessageDeletion') %]
                         [% IF moremember %]
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 22e6a068ceb..6ed5e724f2c 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,17 @@
-[% IF patron_messages.count OR opacnote %]
+[% IF patron_messages.filter_by_unread.count OR opacnote %]
     <div class="alert alert-info">
         <h3>Messages for you</h3>
         <ul>
-            [% FOREACH message IN patron_messages %]
+            [% FOREACH message IN patron_messages.filter_by_unread %]
                 <li>
                     <strong>[% message.message | html | html_line_break %]</strong><br>
                     &nbsp;&nbsp;&nbsp;<em>Written on [% message.message_date | $KohaDates %] by [% Branches.GetName(message.branchcode) | html %]</em>
                 </li>
+                <form id="dismiss-message-form" action="/cgi-bin/koha/opac-dismiss-message.pl" method="post">
+                    <input type="hidden" name="message_id" value="[% message.message_id | html %]">
+                    <input type="hidden" name="patron_id" value="[% message.borrowernumber | html %]">
+                    <button type="submit" class="dismiss-message-button btn btn-primary"><i class="fa fa-trash" aria-hidden="true"></i> Dismiss</button>
+                </form>
             [% END %]
 
             [% IF ( opacnote ) %]<li>[% opacnote | html | html_line_break %]</li>[% 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 040ceb0d96c..1208093008e 100644
--- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-main.tt
+++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-main.tt
@@ -283,7 +283,7 @@
                                             </a>
                                         </li>
                                     [% END %]
-                                    [% IF patron_messages && patron_messages.count > 0 || opacnote %]
+                                    [% IF patron_messages && patron_messages.filter_by_unread.count > 0 || opacnote %]
                                         [% IF opacnote %]
                                             <li>
                                                 <a href="/cgi-bin/koha/opac-user.pl">
diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-user.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-user.tt
index 97cb29852e7..8dcd9f4a0fa 100644
--- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-user.tt
+++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-user.tt
@@ -1286,6 +1286,10 @@
                 $("#addNoteTitle").text("");
                 $("#addNote").val("");
             });
+
+            $(".dismiss-message-button").click(function(e){
+                return confirmDelete(_("Are you sure you want to dismiss this message?"));
+            });
         });
 
         function submitNote( title, issue_id, note ){
diff --git a/opac/opac-dismiss-message.pl b/opac/opac-dismiss-message.pl
new file mode 100755
index 00000000000..84c5906b6ac
--- /dev/null
+++ b/opac/opac-dismiss-message.pl
@@ -0,0 +1,54 @@
+#!/usr/bin/perl
+
+# Copyright 2023 Aleisha Amohia <aleisha@catalyst.net.nz>
+#
+# 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 <http://www.gnu.org/licenses>.
+
+use Modern::Perl;
+use CGI qw ( -utf8 );
+use C4::Koha;
+use C4::Context;
+use C4::Output qw( output_html_with_http_headers );
+use C4::Auth qw( get_template_and_user );
+use Koha::DateUtils qw( dt_from_string );
+use Koha::Patrons;
+
+my $query = CGI->new;
+
+my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
+    {
+        template_name   => "opac-user.tt",
+        query           => $query,
+        type            => "opac",
+    }
+);
+
+my $patron_id = $query->param('patron_id');
+my $patron = Koha::Patrons->find( $patron_id );
+my $message_id = $query->param('message_id');
+my $message = $patron->messages->find( $message_id );
+
+unless ( $message ) {
+    # exit early
+    print $query->redirect("/cgi-bin/koha/opac-user.pl");
+    exit;
+}
+
+$message->update({ patron_read_date => dt_from_string });
+
+print $query->redirect("/cgi-bin/koha/opac-user.pl");
+
+output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };
-- 
2.11.0