From 28ee68e9ee5cbe601393e1fe568ec0133a92fdb8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andr=C3=A9=20Noack?= <noack@data-quest.de>
Date: Fri, 3 May 2024 15:49:37 +0000
Subject: [PATCH] Resolve "Verwaltung von Veranstaltungen: Duplikate im
 Lehrendenfilter"

Closes #4054 and #4107

Merge request studip/studip!2955
---
 app/controllers/admin/courses.php | 35 ++++++++++++++++++-------------
 1 file changed, 20 insertions(+), 15 deletions(-)

diff --git a/app/controllers/admin/courses.php b/app/controllers/admin/courses.php
index e858d3717f0..d28c1bf54b6 100644
--- a/app/controllers/admin/courses.php
+++ b/app/controllers/admin/courses.php
@@ -527,12 +527,14 @@ class Admin_CoursesController extends AuthenticatedController
             'institut_id'    => 'MY_INSTITUTES_DEFAULT',
         ];
 
-        $config->store(
-            'MY_INSTITUTES_INCLUDE_CHILDREN',
-            str_contains($filters['institut_id'], '_') ? 1 : 0
-        );
-        if ($config->MY_INSTITUTES_INCLUDE_CHILDREN) {
-            $filters['institut_id'] = substr($filters['institut_id'], 0, strpos($filters['institut_id'], '_'));
+        if (!empty($filters['institut_id'])) {
+            $config->store(
+                'MY_INSTITUTES_INCLUDE_CHILDREN',
+                str_contains($filters['institut_id'], '_') ? 1 : 0
+            );
+            if ($config->MY_INSTITUTES_INCLUDE_CHILDREN) {
+                $filters['institut_id'] = substr($filters['institut_id'], 0, strpos($filters['institut_id'], '_'));
+            }
         }
 
         foreach ($mapping as $key => $field) {
@@ -1710,7 +1712,9 @@ class Admin_CoursesController extends AuthenticatedController
         if (str_contains($institut_id, '_')) {
             $institut_id = substr($institut_id, 0, strpos($institut_id, '_'));
         }
-        $teachers = DBManager::get()->fetchAll("
+        $teachers = [];
+        if ($institut_id) {
+            $teachers = DBManager::get()->fetchAll("
                 SELECT auth_user_md5.*, user_info.*
                 FROM auth_user_md5
                     LEFT JOIN user_info ON (auth_user_md5.user_id = user_info.user_id)
@@ -1718,18 +1722,19 @@ class Admin_CoursesController extends AuthenticatedController
                     INNER JOIN Institute ON (Institute.Institut_id = user_inst.Institut_id)
                 WHERE (Institute.Institut_id = :institut_id OR Institute.fakultaets_id = :institut_id)
                     AND auth_user_md5.perms = 'dozent'
+                GROUP BY auth_user_md5.user_id
                 ORDER BY auth_user_md5.Nachname ASC, auth_user_md5.Vorname ASC
             ", [
                 'institut_id' => $institut_id
             ],
-            function ($data) {
-                $ret['user_id'] = $data['user_id'];
-                unset($data['user_id']);
-                $ret['fullname'] = User::build($data)->getFullName("full_rev");
-                return $ret;
-            }
-        );
-
+                function ($data) {
+                    $ret['user_id'] = $data['user_id'];
+                    unset($data['user_id']);
+                    $ret['fullname'] = User::build($data)->getFullName("full_rev");
+                    return $ret;
+                }
+            );
+        }
 
         $list = new SelectWidget(_('Lehrendenfilter'), $this->url_for('admin/courses/index'), 'teacher_filter');
         if (!$institut_id || $institut_id === 'all') {
-- 
GitLab