From 767abcfa171921e49954ed76d29ebfb10a3a828c Mon Sep 17 00:00:00 2001
From: Josef Moravec <josef.moravec@gmail.com>
Date: Tue, 4 Oct 2016 13:57:03 +0200
Subject: [PATCH] Bug 17397: Show name of librarian who created circulation
 message

Test plan:
1) Apply the patch
2) Update DB structure
3) Run update_dbix_class_files.pl
4) Select patron for checking out
5) Try to add some circulation and opac messages
6) Note that now there is creator (you ;) ) shown by every message added (with link to creator profile)
7) Try to delete messages to confirm that everything works as expected
---
 Koha/Patron/Message.pm                             |  3 ++
 circ/circulation.pl                                | 34 +++++++++++++++-------
 installer/data/mysql/atomicupdate/bug_17397.sql    |  3 ++
 installer/data/mysql/kohastructure.sql             |  4 ++-
 .../prog/en/modules/circ/circulation.tt            | 15 +++++-----
 5 files changed, 39 insertions(+), 20 deletions(-)
 create mode 100644 installer/data/mysql/atomicupdate/bug_17397.sql

diff --git a/Koha/Patron/Message.pm b/Koha/Patron/Message.pm
index a928fa5..5e518fb 100644
--- a/Koha/Patron/Message.pm
+++ b/Koha/Patron/Message.pm
@@ -49,6 +49,9 @@ sub store {
               and $self->message_type
               and $self->branchcode;
 
+    my $userenv = C4::Context->userenv;
+    $self->manager_id($userenv ? $userenv->{number} : 0);
+
     C4::Log::logaction( "MEMBERS", "ADDCIRCMESSAGE", $self->borrowernumber, $self->message )
         if C4::Context->preference("BorrowersLog");
 
diff --git a/circ/circulation.pl b/circ/circulation.pl
index fcd967d..59dda6a 100755
--- a/circ/circulation.pl
+++ b/circ/circulation.pl
@@ -564,20 +564,33 @@ if ( $borrowernumber && $borrower->{'category_type'} eq 'C') {
     $template->param( 'catcode' => $patron_categories->next )  if $patron_categories->count == 1;
 }
 
-my $librarian_messages = Koha::Patron::Messages->search(
+my $all_messages = Koha::Patron::Messages->search(
     {
-        borrowernumber => $borrowernumber,
-        message_type => 'L',
-    }
-);
-
-my $patron_messages = Koha::Patron::Messages->search(
+        'me.borrowernumber' => $borrowernumber,
+    },
     {
-        borrowernumber => $borrowernumber,
-        message_type => 'B',
+       join => 'manager',
+       '+select' => ['manager.surname', 'manager.firstname' ],
+       '+as' => ['manager_surname', 'manager_firstname'],
     }
 );
 
+my @messages;
+while ( my $content = $all_messages->next ) {
+    my $this_item;
+
+    $this_item->{borrowernumber} = $content->borrowernumber;
+    $this_item->{message_id}     = $content->message_id;
+    $this_item->{branchcode}     = $content->branchcode;
+    $this_item->{message_type}   = $content->message_type;
+    $this_item->{message_date}   = $content->message_date;
+    $this_item->{message}        = $content->message;
+    $this_item->{manager_id}     = $content->manager_id;
+    $this_item->{name}           = $content->_result->get_column('manager_firstname') . ' ' . $content->_result->get_column('manager_surname');
+
+    push @messages, $this_item;
+}
+
 my $fast_cataloging = 0;
 if (defined getframeworkinfo('FA')) {
     $fast_cataloging = 1 
@@ -625,8 +638,7 @@ if ($restoreduedatespec || $stickyduedate) {
 }
 
 $template->param(
-    librarian_messages => $librarian_messages,
-    patron_messages   => $patron_messages,
+    messages           => \@messages,
     borrower          => $borrower,
     borrowernumber    => $borrowernumber,
     categoryname      => $borrower->{'description'},
diff --git a/installer/data/mysql/atomicupdate/bug_17397.sql b/installer/data/mysql/atomicupdate/bug_17397.sql
new file mode 100644
index 0000000..f571b72
--- /dev/null
+++ b/installer/data/mysql/atomicupdate/bug_17397.sql
@@ -0,0 +1,3 @@
+ALTER TABLE `messages`
+ADD `manager_id` int(11) NULL,
+ADD FOREIGN KEY (`manager_id`) REFERENCES `borrowers` (`borrowernumber`) ON DELETE SET NULL;
diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql
index e38efc5..20a0663 100644
--- a/installer/data/mysql/kohastructure.sql
+++ b/installer/data/mysql/kohastructure.sql
@@ -2659,7 +2659,9 @@ CREATE TABLE `messages` ( -- circulation messages left via the patron's check ou
   `message_type` varchar(1) NOT NULL, -- whether the message is for the librarians (L) or the patron (B)
   `message` text NOT NULL, -- the text of the message
   `message_date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, -- the date and time the message was written
-  PRIMARY KEY (`message_id`)
+  `manager_id` int(11) default NULL, -- creator of message
+  PRIMARY KEY (`message_id`),
+  CONSTRAINT `messages_ibfk_1` FOREIGN KEY (`manager_id`) REFERENCES `borrowers` (`borrowernumber`) ON DELETE SET NULL
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
 
 --
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt
index e0284b2..7c65ad0 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt
@@ -850,11 +850,16 @@ No patron matched <span class="ex">[% message | html %]</span>
     <div id="messages" class="circmessage">
         <h4>Messages:</h4>
         <ul>
-            [% FOREACH message IN librarian_messages %]
+            [% FOREACH message IN messages %]
                 <li>
-                    <span class="circ-hlt">
+                    [% IF(message.message_type == "L") %]
+                        <span class="circ-hlt">
+                    [% ELSE %]
+                        <span>
+                    [% END %]>
                         [% message.message_date | $KohaDates %]
                         [% Branches.GetName( message.branchcode ) %]
+                        ( <a href="/cgi-bin/koha/members/moremember.pl?borrowernumber=[% message.manager_id %]">[% message.name %]</a> )
                         <i>"[% message.message %]"</i>
                     </span>
                     [% IF message.branchcode == branch OR Koha.Preference('AllowAllMessageDeletion') %]
@@ -862,12 +867,6 @@ No patron matched <span class="ex">[% message | html %]</span>
                     [% END %]
                 </li>
             [% END %]
-            [% FOREACH message IN patron_messages %]
-                <li><span class="">[% message.message_date | $KohaDates %] [% Branches.GetName( message.branchcode )%] <i>"[% message.message %]"</i></span>
-                [% IF message.branchcode == branch OR Koha.Preference('AllowAllMessageDeletion') %]
-                    [<a href="/cgi-bin/koha/circ/del_message.pl?message_id=[% message.message_id %]&amp;borrowernumber=[% message.borrowernumber %]"><i class="fa fa-trash"></i> Delete</a>]
-                [% END %]</li>
-            [% END %]
         </ul>
         <a id="addnewmessageLabel" href="#add_message_form" data-toggle="modal"><i class="fa fa-plus"></i> Add a new message</a>
     </div>
-- 
2.1.4