From 41e0f42d92ccdeaf4d0c775fa9b92a340d7523a4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andr=C3=A9=20Noack?= <noack@data-quest.de>
Date: Thu, 28 Oct 2021 13:43:06 +0000
Subject: [PATCH] Resolve "Reihenfolge der Tools in Einrichtungen falsch"

---
 app/controllers/course/plus.php     | 16 +++++++++++++---
 app/views/course/plus/sorttools.php |  2 +-
 lib/models/Institute.class.php      |  3 ++-
 3 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/app/controllers/course/plus.php b/app/controllers/course/plus.php
index f8132df612d..1cfe36bf2d8 100644
--- a/app/controllers/course/plus.php
+++ b/app/controllers/course/plus.php
@@ -101,11 +101,21 @@ class Course_PlusController extends AuthenticatedController
         PageLayout::setTitle(_('Reihenfolge der Werkzeuge ändern'));
         if (Request::submitted('order')) {
             CSRFProtection::verifyUnsafeRequest();
-            $plugin_id = Request::get('id');
+            $plugin_id = explode('_', Request::get('id'))[1];
             $newpos = Request::get('index') + 1;
             if ($this->sem->tools->findOneBy('plugin_id', $plugin_id)) {
-                $this->sem->tools->findBy('position', $newpos, '>=')->each(function ($p) {$p->position++;});
-                $this->sem->tools->findOneBy('plugin_id', $plugin_id)->position = $newpos;
+                $oldpos = $this->sem->tools->findOneBy('plugin_id', $plugin_id)->position;
+                if ($oldpos < $newpos) {
+                    $this->sem->tools->findBy('position', $newpos, '>')->each(function ($p) {
+                        $p->position++;
+                    });
+                    $this->sem->tools->findOneBy('plugin_id', $plugin_id)->position = $newpos + 1;
+                } else {
+                    $this->sem->tools->findBy('position', $newpos, '>=')->each(function ($p) {
+                        $p->position++;
+                    });
+                    $this->sem->tools->findOneBy('plugin_id', $plugin_id)->position = $newpos;
+                }
                 $this->sem->tools->orderBy('position asc')->each(function ($p) {static $pos = 0; $p->position = $pos++;});
                 $this->sem->tools->store();
                 $this->render_nothing();
diff --git a/app/views/course/plus/sorttools.php b/app/views/course/plus/sorttools.php
index de5759298aa..8a20aea1c9e 100644
--- a/app/views/course/plus/sorttools.php
+++ b/app/views/course/plus/sorttools.php
@@ -2,7 +2,7 @@
 <? if ($sem->tools): ?>
     <? foreach ($sem->tools as $tool): ?>
     <?php if (!$tool->getStudipModule()) continue; ?>
-        <article class="draggable" id="<?= $tool->plugin_id ?>">
+        <article class="draggable" id="plugin_<?= $tool->plugin_id ?>">
             <header>
                 <span class="sg-sortable-handle"></span>
                 <h1><?= htmlready($tool->getDisplayName()) ?></h1>
diff --git a/lib/models/Institute.class.php b/lib/models/Institute.class.php
index efcb888306d..86120c1e3fa 100644
--- a/lib/models/Institute.class.php
+++ b/lib/models/Institute.class.php
@@ -197,7 +197,8 @@ class Institute extends SimpleORMap implements Range
         $config['has_many']['tools'] = [
             'class_name'        => 'ToolActivation',
             'assoc_foreign_key' => 'range_id',
-            'on_delete'         => 'delete',
+            'order_by'          => 'ORDER BY position',
+            'on_delete'         => 'delete'
         ];
         $config['additional_fields']['all_status_groups']['get'] = function ($institute) {
             return Statusgruppen::findAllByRangeId($institute->id, true);
-- 
GitLab