View | Details | Raw Unified | Return to bug 12029
Collapse All | Expand All

(-)a/Koha/Patron/Messages.pm (+33 lines)
Lines 42-49 sub _type { Link Here
42
    return 'Message';
42
    return 'Message';
43
}
43
}
44
44
45
=head3 object_class
46
47
=cut
48
45
sub object_class {
49
sub object_class {
46
    return 'Koha::Patron::Message';
50
    return 'Koha::Patron::Message';
47
}
51
}
48
52
53
=head3 unread
54
55
    Returns a result set of messages that haven't been marked read by the patron
56
57
=cut
58
59
sub unread {
60
    # return resultset of messages with an undefined patron_read_date
61
    my ($self, $params) = @_;
62
    $params ||= {};
63
    return $self->search({
64
        patron_read_date => {IS => undef},
65
        %$params,
66
    });
67
}
68
69
=head3 unread_count
70
71
    Returns a count of messages that haven't been marked read by the patron
72
73
=cut
74
75
76
sub unread_count {
77
    # return count of unread
78
    my ($self, $params) = @_;
79
    $params ||= {};
80
    return $self->unread(%$params)->count();
81
}
49
1;
82
1;
(-)a/installer/data/mysql/atomicupdate/bug_12029.pl (+28 lines)
Line 0 Link Here
1
use Modern::Perl;
2
3
return {
4
    bug_number => "12029",
5
    description => "Enable patrons to delete messages",
6
    up => sub {
7
        my ($args) = @_;
8
        my ($dbh, $out) = @$args{qw(dbh out)};
9
10
        # Do you stuffs here
11
        my $alteration = qq{
12
            ALTER TABLE messages
13
            ADD COLUMN `patron_read_date` timestamp NULL DEFAULT NULL
14
            COMMENT 'date and time patron dismissed message'
15
            AFTER `manager_id`
16
        };
17
        if( column_exists('messages', 'patron_read_date') ) {
18
            say $out "NOTICE: Column 'messages.patron_read_date' already exists";
19
        }
20
        else {
21
            say $out "ALTERATION: $alteration";
22
            $dbh->do($alteration);
23
        }
24
25
        # Print useful stuff here
26
        say $out "INFO: Bug 12029 migration applied";
27
    },
28
};
(-)a/installer/data/mysql/kohastructure.sql (+1 lines)
Lines 3813-3818 CREATE TABLE `messages` ( Link Here
3813
  `message` mediumtext COLLATE utf8mb4_unicode_ci NOT NULL COMMENT 'the text of the message',
3813
  `message` mediumtext COLLATE utf8mb4_unicode_ci NOT NULL COMMENT 'the text of the message',
3814
  `message_date` timestamp NOT NULL DEFAULT current_timestamp() COMMENT 'the date and time the message was written',
3814
  `message_date` timestamp NOT NULL DEFAULT current_timestamp() COMMENT 'the date and time the message was written',
3815
  `manager_id` int(11) DEFAULT NULL COMMENT 'creator of message',
3815
  `manager_id` int(11) DEFAULT NULL COMMENT 'creator of message',
3816
  `patron_read_date` timestamp NULL DEFAULT NULL COMMENT 'date and time the patron dismissed the message',
3816
  PRIMARY KEY (`message_id`),
3817
  PRIMARY KEY (`message_id`),
3817
  KEY `messages_ibfk_1` (`manager_id`),
3818
  KEY `messages_ibfk_1` (`manager_id`),
3818
  KEY `messages_borrowernumber` (`borrowernumber`),
3819
  KEY `messages_borrowernumber` (`borrowernumber`),
(-)a/koha-tmpl/intranet-tmpl/prog/en/includes/patron_messages.inc (+3 lines)
Lines 218-223 Link Here
218
                            ( <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> )
218
                            ( <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> )
219
                        [% END %]
219
                        [% END %]
220
                        <em>"[% patron_message.message | html %]"</em>
220
                        <em>"[% patron_message.message | html %]"</em>
221
                        [% IF patron_message.patron_read_date %]
222
                           <i>read</i> [% patron_message.patron_read_date | $KohaDates %]
223
                        [% END %]
221
                    </span>
224
                    </span>
222
                    [% IF patron_message.branchcode == Branches.GetLoggedInBranchcode OR Koha.Preference('AllowAllMessageDeletion') %]
225
                    [% IF patron_message.branchcode == Branches.GetLoggedInBranchcode OR Koha.Preference('AllowAllMessageDeletion') %]
223
                        <a class="btn btn-link" href="/cgi-bin/koha/circ/del_message.pl?message_id=[% patron_message.message_id | html %]&amp;borrowernumber=[% patron_message.borrowernumber | html %]&amp;from=moremember" onclick='return confirm(_("Are you sure you want to delete this message? This cannot be undone."));'><i class="fa fa-trash"></i> Delete</a>
226
                        <a class="btn btn-link" href="/cgi-bin/koha/circ/del_message.pl?message_id=[% patron_message.message_id | html %]&amp;borrowernumber=[% patron_message.borrowernumber | html %]&amp;from=moremember" onclick='return confirm(_("Are you sure you want to delete this message? This cannot be undone."));'><i class="fa fa-trash"></i> Delete</a>
(-)a/koha-tmpl/opac-tmpl/bootstrap/en/includes/opac-note.inc (-2 / +3 lines)
Lines 1-12 Link Here
1
[% IF patron_messages.count OR opacnote %]
1
[% IF patron_messages.unread_count OR opacnote %]
2
    <div class="alert alert-info">
2
    <div class="alert alert-info">
3
        <h3>Messages for you</h3>
3
        <h3>Messages for you</h3>
4
        <ul>
4
        <ul>
5
            [% FOREACH message IN patron_messages %]
5
            [% FOREACH message IN patron_messages.unread %]
6
                <li>
6
                <li>
7
                    <strong>[% message.message | html | html_line_break %]</strong><br>
7
                    <strong>[% message.message | html | html_line_break %]</strong><br>
8
                    &nbsp;&nbsp;&nbsp;<em>Written on [% message.message_date | $KohaDates %] by [% Branches.GetName(message.branchcode) | html %]</em>
8
                    &nbsp;&nbsp;&nbsp;<em>Written on [% message.message_date | $KohaDates %] by [% Branches.GetName(message.branchcode) | html %]</em>
9
                </li>
9
                </li>
10
                <a class="btn btn-link" href="/cgi-bin/koha/dismiss_message.pl?message_id=[% message.message_id | html %]&amp;borrowernumber=[% message.borrowernumber | html %]" onclick="return true;"><i class="fa fa-trash" aria-hidden="true"></i> Dismiss</a>
10
            [% END %]
11
            [% END %]
11
12
12
            [% IF ( opacnote ) %]<li>[% opacnote | html | html_line_break %]</li>[% END %]
13
            [% IF ( opacnote ) %]<li>[% opacnote | html | html_line_break %]</li>[% END %]
(-)a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-main.tt (-3 / +3 lines)
Lines 263-269 Link Here
263
                                            </a>
263
                                            </a>
264
                                        </li>
264
                                        </li>
265
                                    [% END %]
265
                                    [% END %]
266
                                    [% IF patron_messages && patron_messages.count > 0 || opacnote %]
266
                                    [% IF patron_messages && patron_messages.unread_count || opacnote %]
267
                                        [% IF opacnote %]
267
                                        [% IF opacnote %]
268
                                            <li>
268
                                            <li>
269
                                                <a href="/cgi-bin/koha/opac-user.pl">
269
                                                <a href="/cgi-bin/koha/opac-user.pl">
Lines 274-281 Link Here
274
                                        [% ELSE %]
274
                                        [% ELSE %]
275
                                            <li>
275
                                            <li>
276
                                                <a href="/cgi-bin/koha/opac-user.pl">
276
                                                <a href="/cgi-bin/koha/opac-user.pl">
277
                                                    <span class="count_label">[% patron_messages.count | html %]</span>
277
                                                    <span class="count_label">[% patron_messages.unread_count | html %]</span>
278
                                                    [% tn('message', 'messages', patron_messages.count ) | html %]
278
                                                    [% tn('message', 'messages', patron_messages.unread_count ) | html %]
279
                                                </a>
279
                                                </a>
280
                                            </li>
280
                                            </li>
281
                                        [% END %]
281
                                        [% END %]
(-)a/opac/dismiss_message.pl (+82 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
# This file is part of Koha.
3
#
4
# Copyright (C) 2022 Auto-Parallel Technologies, Inc.
5
#
6
# Koha is free software; you can redistribute it and/or modify it
7
# under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 3 of the License, or
9
# (at your option) any later version.
10
#
11
# Koha is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with Koha; if not, see <http://www.gnu.org/licenses>.
18
19
use Modern::Perl;
20
21
use CGI qw ( -utf8 );
22
23
use C4::Auth qw( get_template_and_user );
24
use C4::Output;
25
use Koha::Patron::Messages;
26
27
use Try::Tiny;
28
use JSON qw//;
29
30
use constant {
31
    TRUE         => 1,
32
    FALSE        => 0,
33
    json         => JSON->new->pretty,
34
    REDIRECT_URL => '/cgi-bin/koha/opac-user.pl',
35
};
36
37
sub dismiss_patron_message {
38
    # accept a CGI object as input
39
    my ($cgi) = @_;
40
41
    # get input parameters
42
    my $message_id     = $cgi->param('message_id')     || undef;
43
    my $borrowernumber = $cgi->param('borrowernumber') || undef;
44
    my $inputs = {
45
        message_id     => $message_id,
46
        borrowernumber => $borrowernumber,
47
    };
48
49
    my $rs = Koha::Patron::Messages->search($inputs);
50
    my $message = $rs->next();
51
52
    #- exit early if no message found matching message_id
53
    if (! defined $message) {
54
        return;
55
    }
56
57
    #- exit early if patron_read_date is already set
58
    if (defined $message->patron_read_date) {
59
        return;
60
    }
61
62
    #- update the patron_read_date to database's NOW()
63
    $message->update({
64
        patron_read_date => \'NOW()',
65
    });
66
67
    return;
68
}
69
70
my CGI $cgi;
71
try {
72
    $cgi = CGI->new;
73
    dismiss_patron_message($cgi);
74
}
75
catch {
76
    my ($exception) = $_;
77
}
78
finally {
79
    # redirect to referrer
80
    my $redir_url = $ENV{HTTP_REFERRER} || REDIRECT_URL;
81
    print $cgi->redirect($redir_url);
82
};
(-)a/t/db_dependent/Koha/Patron/Messages.t (-2 / +40 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 12;
22
use Test::More tests => 16;
23
23
24
use C4::Context;
24
use C4::Context;
25
use Koha::ActionLogs;
25
use Koha::ActionLogs;
Lines 99-104 $new_message_2->delete; Link Here
99
is( Koha::Patron::Messages->search->count, $nb_of_messages + 1, 'Delete should have deleted the message 2' );
99
is( Koha::Patron::Messages->search->count, $nb_of_messages + 1, 'Delete should have deleted the message 2' );
100
is( get_nb_of_logactions(), $nb_of_logaction + 3, 'With BorrowersLog on, 1 new log should have been added when deleting a new message' );
100
is( get_nb_of_logactions(), $nb_of_logaction + 3, 'With BorrowersLog on, 1 new log should have been added when deleting a new message' );
101
101
102
# START BUG-12029
103
my $starting_unread_count = Koha::Patron::Messages->unread_count();
104
105
my $new_message_4 = Koha::Patron::Message->new(
106
    {
107
        borrowernumber => $patron->{borrowernumber},
108
        branchcode     => $library->{branchcode},
109
        message_type   => 'B',
110
        message        => 'testing unread',
111
        manager_id     => $patron_3->{borrowernumber},
112
    }
113
)->store;
114
115
is(
116
    Koha::Patron::Messages->unread_count(),
117
    $starting_unread_count + 1,
118
    "Unread count should return a new count one higher"
119
);
120
is(
121
    Koha::Patron::Messages->unread->count(),
122
    $starting_unread_count + 1,
123
    "Unread should be able to have count called on it and return the same number"
124
);
125
126
ok( ( grep { $_->message eq $new_message_4->message } @{ Koha::Patron::Messages->unread->as_list } ),
127
    "Unread should return our new_message_4" );
128
129
# Set a message to be read
130
$new_message_4->update(
131
    {
132
        patron_read_date => \'NOW()',
133
    }
134
);
135
136
is( Koha::Patron::Messages->unread_count(),
137
    $starting_unread_count, "Unread count should return the original count of messages" );
138
# END BUG-12029
139
140
102
$schema->storage->txn_rollback;
141
$schema->storage->txn_rollback;
103
142
104
sub get_nb_of_logactions {
143
sub get_nb_of_logactions {
105
- 

Return to bug 12029