From 8c5fbb9887666f88adb4c3c04e2ff08a86f42ace Mon Sep 17 00:00:00 2001
From: Viktoria Wiebe <vwiebe@uni-osnabrueck.de>
Date: Wed, 25 May 2022 08:05:43 +0000
Subject: [PATCH] Deletes all resource request appointments #707

Closes #707

Merge request studip/studip!655
---
 app/controllers/course/timesrooms.php          |  7 +++++++
 db/migrations/5.2.10_biest_707.php             | 14 ++++++++++++++
 lib/models/resources/ResourceRequest.class.php |  9 ++++++++-
 3 files changed, 29 insertions(+), 1 deletion(-)
 create mode 100644 db/migrations/5.2.10_biest_707.php

diff --git a/app/controllers/course/timesrooms.php b/app/controllers/course/timesrooms.php
index d681a0926d9..b2ca347166c 100644
--- a/app/controllers/course/timesrooms.php
+++ b/app/controllers/course/timesrooms.php
@@ -1425,6 +1425,7 @@ class Course_TimesroomsController extends AuthenticatedController
     private function deleteDate($termin, $cancel_comment)
     {
         $seminar_id = $termin->range_id;
+        $termin_id = $termin->id;
         $termin_room = $termin->getRoomName();
         $termin_date = $termin->getFullname();
         $has_topics  = $termin->topics->count();
@@ -1440,6 +1441,12 @@ class Course_TimesroomsController extends AuthenticatedController
         } else {
             if ($termin->delete()) {
                 StudipLog::log("SEM_DELETE_SINGLEDATE", $termin->id, $seminar_id, 'appointment cancelled');
+
+                // delete attached resource request appointments if they exist
+                ResourceRequestAppointment::deleteBySQL(
+                    "appointment_id = ?",
+                    [$termin_id]
+                );
             }
         }
 
diff --git a/db/migrations/5.2.10_biest_707.php b/db/migrations/5.2.10_biest_707.php
new file mode 100644
index 00000000000..4b209d78772
--- /dev/null
+++ b/db/migrations/5.2.10_biest_707.php
@@ -0,0 +1,14 @@
+<?php
+
+class Biest707 extends Migration
+{
+    public function description()
+    {
+        return 'Removes leftover entries of deleted course dates from resource request appointments';
+    }
+    
+    public function up()
+    {
+        DBManager::get()->exec("DELETE FROM resource_request_appointments WHERE appointment_id NOT IN (SELECT termin_id FROM termine)");
+    }
+}
diff --git a/lib/models/resources/ResourceRequest.class.php b/lib/models/resources/ResourceRequest.class.php
index 648d83c9c6d..6150cf4ed99 100644
--- a/lib/models/resources/ResourceRequest.class.php
+++ b/lib/models/resources/ResourceRequest.class.php
@@ -1421,12 +1421,19 @@ class ResourceRequest extends SimpleORMap implements PrivacyObject, Studip\Calen
      */
     public function getTypeString($short = false)
     {
-        if (count($this->appointments)) {
+        if (count($this->appointments) > 1) {
             if ($short) {
                 return _('Einzeltermine');
             } else {
                 return sprintf(_('Einzeltermine (%sx)'), count($this->appointments));
             }
+        } elseif (count($this->appointments) == 1) {
+            if ($short) {
+                return _('Einzeltermin');
+            } else {
+                return sprintf(_('Einzeltermin (%s)'), 
+                    $this->appointments[0]->appointment->getFullname());
+            }
         } elseif ($this->date) {
             if ($short) {
                 return _('Einzeltermin');
-- 
GitLab