From f0eef2f1a2b91ca694d978b9feda208de26e867d Mon Sep 17 00:00:00 2001
From: Jan-Hendrik Willms <tleilax+studip@gmail.com>
Date: Tue, 3 Sep 2024 13:16:04 +0000
Subject: [PATCH] prevent all mails from consultation bookings, fixes #4532

Closes #4532

Merge request studip/studip!3336
---
 lib/models/ConsultationEvent.php               | 18 ++++++++++++++++++
 lib/models/ConsultationSlot.php                |  4 ++++
 lib/models/calendar/CalendarDate.php           |  5 +++++
 lib/models/calendar/CalendarDateAssignment.php |  2 ++
 4 files changed, 29 insertions(+)

diff --git a/lib/models/ConsultationEvent.php b/lib/models/ConsultationEvent.php
index e0b6b5962cd..fe8a4051a49 100644
--- a/lib/models/ConsultationEvent.php
+++ b/lib/models/ConsultationEvent.php
@@ -29,6 +29,24 @@ class ConsultationEvent extends SimpleORMap
             'on_delete'         => 'delete',
         ];
 
+        $config['registered_callbacks'] = [
+            'before_delete' => [
+                function (ConsultationEvent $event) {
+                    // Suppress all mails from calendar for users that do not
+                    // want to receive emails about consultation bookings
+                    $event->event->calendars->each(function (CalendarDateAssignment $assignment) {
+                        if (
+                            $assignment->user
+                            && !$assignment->user->getConfiguration()->CONSULTATION_SEND_MESSAGES
+                        ) {
+                            $assignment->suppress_mails = true;
+                            $assignment->delete();
+                        }
+                    });
+                },
+            ],
+        ];
+
         parent::configure($config);
     }
 }
diff --git a/lib/models/ConsultationSlot.php b/lib/models/ConsultationSlot.php
index 5a59208c8a4..520b91e1021 100644
--- a/lib/models/ConsultationSlot.php
+++ b/lib/models/ConsultationSlot.php
@@ -232,6 +232,10 @@ class ConsultationSlot extends SimpleORMap
         $calendar_event = new CalendarDateAssignment();
         $calendar_event->range_id         = $user->id;
         $calendar_event->calendar_date_id = $event->id;
+
+        // Suppress mails for users that do not want mails from the consultations
+        $calendar_event->suppress_mails = !$user->getConfiguration()->CONSULTATION_SEND_MESSAGES;
+
         $calendar_event->store();
 
         return $event;
diff --git a/lib/models/calendar/CalendarDate.php b/lib/models/calendar/CalendarDate.php
index b3608b698fc..fcdd65cef1c 100644
--- a/lib/models/calendar/CalendarDate.php
+++ b/lib/models/calendar/CalendarDate.php
@@ -38,6 +38,11 @@
  * @property string mkdate database column
  * @property string chdate database column
  * @property string import_date database column
+ *
+ * @property User $author
+ * @property User $editor
+ * @property CalendarDateAssignment[]|SimpleORMapCollection $calendars
+ * @property CalendarDateException[]|SimpleORMapCollection $exceptions
  */
 class CalendarDate extends SimpleORMap implements PrivacyObject
 {
diff --git a/lib/models/calendar/CalendarDateAssignment.php b/lib/models/calendar/CalendarDateAssignment.php
index 82bbab88678..d61d124bf8b 100644
--- a/lib/models/calendar/CalendarDateAssignment.php
+++ b/lib/models/calendar/CalendarDateAssignment.php
@@ -29,6 +29,8 @@
  * @property string mkdate The creation date of the assignment.
  * @property string chdate The modification date of the assignment.
  * @property CalendarDate|null calendar_date The associated calendar date object.
+ * @property User|null $user
+ * @property Course|null $course
  */
 class CalendarDateAssignment extends SimpleORMap implements Event
 {
-- 
GitLab