From 48f8f303733294c0605fd3c389255ecc18e8018a Mon Sep 17 00:00:00 2001
From: Thomas Hackl <hackl@data-quest.de>
Date: Thu, 28 Oct 2021 05:42:01 +0000
Subject: [PATCH] =?UTF-8?q?Resolve=20"Anmeldeverfahren:=20H=C3=B6here=20Se?=
 =?UTF-8?q?mester=20bevorzugen=20erzeugt=20H=C3=A4rtef=C3=A4lle"?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../PreferentialAdmission.class.php           |  2 +-
 lib/classes/admission/CourseSet.class.php     | 28 +++++++++++++++----
 2 files changed, 24 insertions(+), 6 deletions(-)

diff --git a/lib/admissionrules/preferentialadmission/PreferentialAdmission.class.php b/lib/admissionrules/preferentialadmission/PreferentialAdmission.class.php
index 348d6fcca8f..6bb6cebf4c7 100644
--- a/lib/admissionrules/preferentialadmission/PreferentialAdmission.class.php
+++ b/lib/admissionrules/preferentialadmission/PreferentialAdmission.class.php
@@ -446,7 +446,7 @@ class PreferentialAdmission extends AdmissionRule
             $userlist->setUsers($members);
             $userlist->setFactor($bonus);
             $userlist->store();
-            $bonus = $bonus * ($this->bonus_difference + 1);
+            $bonus = $bonus + ($this->bonus_difference + 1);
             $courseset->addUserList($userlist->getId());
             $this->userlists[] = $userlist->getId();
         }
diff --git a/lib/classes/admission/CourseSet.class.php b/lib/classes/admission/CourseSet.class.php
index 50f983fbc05..cf6cac78893 100644
--- a/lib/classes/admission/CourseSet.class.php
+++ b/lib/classes/admission/CourseSet.class.php
@@ -597,14 +597,32 @@ class CourseSet
     public function getUserFactorList()
     {
         $factored_users = [];
+
         foreach ($this->getUserLists() as $ul_id) {
             $user_list = new AdmissionUserList($ul_id);
-            $factored_users = array_merge($factored_users,
-                                 array_combine(array_keys($user_list->getUsers()),
-                                         array_fill(0, count($user_list->getUsers()), $user_list->getFactor())
-                                         )
-                    );
+
+            // Iterate through user list.
+            foreach ($user_list->getUsers() as $user => $assigned) {
+                switch ($user_list->getFactor()) {
+                    // Maximum factor, just set it and stop further processing of user lists.
+                    case PHP_INT_MAX:
+                        $factored_users[$user] = PHP_INT_MAX;
+                        break;
+                    // Backlist, set malus and stop further processing of user lists.
+                    case 0:
+                        $factored_users[$user] = 0;
+                        break;
+                    default:
+                        // Add up current bonus if it isn't already at 0 or PHP_INT_MAX.
+                        if ($factored_users[$user] !== 0 && $factored_users[$user] != PHP_INT_MAX) {
+                            $factored_users[$user] = isset($factored_users[$user]) ?
+                                $factored_users[$user] + $user_list->getFactor() :
+                                $user_list->getFactor();
+                        }
+                }
+            }
         }
+
         return $factored_users;
     }
 
-- 
GitLab