From 308bd66274b846f7a33f52292c4c64e60463fc8a Mon Sep 17 00:00:00 2001
From: Jan-Hendrik Willms <tleilax+github@gmail.com>
Date: Fri, 28 Jan 2022 16:10:32 +0100
Subject: [PATCH] set sender for consultation mails, fixes #616

---
 lib/classes/ConsultationMailer.php | 66 +++++++++++++++++-------------
 lib/models/ConsultationBooking.php | 12 +++---
 2 files changed, 44 insertions(+), 34 deletions(-)

diff --git a/lib/classes/ConsultationMailer.php b/lib/classes/ConsultationMailer.php
index b5e8628aa92..aa9c7a7b6a4 100644
--- a/lib/classes/ConsultationMailer.php
+++ b/lib/classes/ConsultationMailer.php
@@ -23,13 +23,14 @@ class ConsultationMailer
     /**
      * Sends a consultation information message.
      *
-     * @param  User             $user    Recipient
-     * @param  ConsultationSlot $slot    Slot in question
-     * @param  string           $subject Subject of the message
-     * @param  string           $reason  Reason for a booking or cancelation
-     * @param  User             $sender  Sender of the message
+     * @param User|null         $sender  Sender
+     * @param User             $user    Recipient
+     * @param ConsultationSlot $slot    Slot in question
+     * @param string           $subject Subject of the message
+     * @param string           $reason  Reason for a booking or cancelation
+     * @param User             $sender  Sender of the message
      */
-    public static function sendMessage(User $user, ConsultationBooking $booking, $subject, $reason = '')
+    public static function sendMessage(?User $sender, User $user, ConsultationBooking $booking, string $subject, string $reason = '')
     {
         // Don't send message if user doesn't want it
         if (!UserConfig::get($user->id)->CONSULTATION_SEND_MESSAGES) {
@@ -44,7 +45,12 @@ class ConsultationMailer
             'reason' => $reason ?: _('Kein Grund angegeben'),
         ]);
 
-        messaging::sendSystemMessage($user, $subject, $message);
+        if ($sender === null) {
+            messaging::sendSystemMessage($user, $subject, $message);
+        } else {
+            $messaging = new messaging();
+            $messaging->insert_message($message, $user->username, $sender->id, '', '', '', '', $subject);
+        }
 
         restoreLanguage();
     }
@@ -52,9 +58,10 @@ class ConsultationMailer
     /**
      * Send a booking information message to the teacher of the booked slot.
      *
-     * @param  ConsultationBooking $booking The booking
+     * @param User|null            $sender
+     * @param ConsultationBooking $booking The booking
      */
-    public static function sendBookingMessageToResponsibilities(ConsultationBooking $booking)
+    public static function sendBookingMessageToResponsibilities(?User $sender, ConsultationBooking $booking)
     {
         foreach ($booking->slot->block->responsible_persons as $user) {
             if ($user->id === $GLOBALS['user']->id) {
@@ -62,10 +69,10 @@ class ConsultationMailer
             }
 
             self::sendMessage(
+                $sender,
                 $user,
                 $booking,
-                sprintf(_('Termin von %s zugesagt'), $booking->user->getFullName()),
-                $booking->reason
+                sprintf(_('Termin von %s zugesagt'), $booking->user->getFullName()), $booking->reason
             );
         }
     }
@@ -73,15 +80,16 @@ class ConsultationMailer
     /**
      * Send a booking information message to the user of the booked slot.
      *
+     * @param User|null            $sender
      * @param  ConsultationBooking $booking The booking
      */
-    public static function sendBookingMessageToUser(ConsultationBooking $booking)
+    public static function sendBookingMessageToUser(?User $sender, ConsultationBooking $booking)
     {
         self::sendMessage(
+            $sender,
             $booking->user,
             $booking,
-            sprintf(_('Termin bei %s zugesagt'), $booking->slot->block->range_display),
-            $booking->reason
+            sprintf(_('Termin bei %s zugesagt'), $booking->slot->block->range_display), $booking->reason
         );
     }
 
@@ -89,27 +97,28 @@ class ConsultationMailer
      * Send an information message about a changed reason to a user of the
      * booked slot.
      *
-     * @param  ConsultationBooking $booking  The booking
-     * @param  User                $receiver The receiver of the message
-     * @param  User                $sender   The sender of the message
+     * @param User                 $sender   The sender of the message
+     * @param ConsultationBooking $booking  The booking
+     * @param User                $receiver The receiver of the message
      */
-    public static function sendReasonMessage(ConsultationBooking $booking, User $receiver)
+    public static function sendReasonMessage(?User $sender, ConsultationBooking $booking, User $receiver)
     {
         self::sendMessage(
+            $sender,
             $receiver,
             $booking,
-            sprintf(_('Grund des Termins bei %s bearbeitet'), $booking->slot->block->range_display),
-            $booking->reason
+            sprintf(_('Grund des Termins bei %s bearbeitet'), $booking->slot->block->range_display), $booking->reason
         );
     }
 
     /**
      * Send a cancelation message to the teacher of the booked slot.
      *
+     * @param User|null            $sender
      * @param  ConsultationBooking $booking The booking
-     * @param  String              $reason  Reason of the cancelation
+     * @param String               $reason  Reason of the cancelation
      */
-    public static function sendCancelMessageToResponsibilities(ConsultationBooking $booking, $reason = '')
+    public static function sendCancelMessageToResponsibilities(?User $sender, ConsultationBooking $booking, string $reason = '')
     {
         foreach ($booking->slot->block->responsible_persons as $user) {
             if ($user->id === $GLOBALS['user']->id) {
@@ -117,10 +126,10 @@ class ConsultationMailer
             }
 
             self::sendMessage(
+                $sender,
                 $user,
                 $booking,
-                sprintf(_('Termin von %s abgesagt'), $booking->user->getFullName()),
-                trim($reason)
+                sprintf(_('Termin von %s abgesagt'), $booking->user->getFullName()), trim($reason)
             );
         }
     }
@@ -128,16 +137,17 @@ class ConsultationMailer
     /**
      * Send a cancelation message to the user of the booked slot.
      *
+     * @param User|null            $sender
      * @param  ConsultationBooking $booking The booking
-     * @param  String              $reason  Reason of the cancelation
+     * @param String               $reason  Reason of the cancelation
      */
-    public static function sendCancelMessageToUser(ConsultationBooking $booking, $reason)
+    public static function sendCancelMessageToUser(?User $sender, ConsultationBooking $booking, string $reason)
     {
         self::sendMessage(
+            $sender,
             $booking->user,
             $booking,
-            sprintf(_('Termin bei %s abgesagt'), $booking->slot->block->range_display),
-            trim($reason)
+            sprintf(_('Termin bei %s abgesagt'), $booking->slot->block->range_display), trim($reason)
         );
     }
 }
diff --git a/lib/models/ConsultationBooking.php b/lib/models/ConsultationBooking.php
index 1f269c43765..3e02ba014ce 100644
--- a/lib/models/ConsultationBooking.php
+++ b/lib/models/ConsultationBooking.php
@@ -59,20 +59,20 @@ class ConsultationBooking extends SimpleORMap implements PrivacyObject
         };
 
         $config['registered_callbacks']['after_create'][] = function (ConsultationBooking $booking) {
-            ConsultationMailer::sendBookingMessageToUser($booking);
-            ConsultationMailer::sendBookingMessageToResponsibilities($booking);
+            ConsultationMailer::sendBookingMessageToUser($GLOBALS['user']->getAuthenticatedUser(), $booking);
+            ConsultationMailer::sendBookingMessageToResponsibilities($GLOBALS['user']->getAuthenticatedUser(), $booking);
         };
 
         $config['registered_callbacks']['before_store'][] = function (ConsultationBooking $booking) {
             if (!$booking->isNew() && $booking->isFieldDirty('reason')) {
                 if ($GLOBALS['user']->id !== $booking->user_id) {
-                    ConsultationMailer::sendReasonMessage($booking,$booking->user);
+                    ConsultationMailer::sendReasonMessage($GLOBALS['user']->getAuthenticatedUser(), $booking, $booking->user);
                 }
 
                 $responsible_persons = $booking->slot->block->responsible_persons;
                 foreach ($responsible_persons as $user) {
                     if ($GLOBALS['user']->id !== $user->id) {
-                        ConsultationMailer::sendReasonMessage($booking, $user);
+                        ConsultationMailer::sendReasonMessage($GLOBALS['user']->getAuthenticatedUser(), $booking, $user);
                     }
                 }
             }
@@ -97,10 +97,10 @@ class ConsultationBooking extends SimpleORMap implements PrivacyObject
     public function cancel($reason = '')
     {
         if ($GLOBALS['user']->id !== $this->user_id) {
-            ConsultationMailer::sendCancelMessageToUser($this, $reason);
+            ConsultationMailer::sendCancelMessageToUser($GLOBALS['user']->getAuthenticatedUser(), $this, $reason);
         }
 
-        ConsultationMailer::sendCancelMessageToResponsibilities($this, $reason);
+        ConsultationMailer::sendCancelMessageToResponsibilities($GLOBALS['user']->getAuthenticatedUser(), $this, $reason);
 
         return $this->delete() ? 1 : 0;
     }
-- 
GitLab