From a8298beda0487fd5e5a4a286f90ba549dbd101fd Mon Sep 17 00:00:00 2001
From: Elmar Ludwig <elmar.ludwig@uni-osnabrueck.de>
Date: Fri, 20 Dec 2024 13:57:23 +0000
Subject: [PATCH] add migration to move admin tool to first position, fixes
 #4417

Closes #4417

Merge request studip/studip!3790
---
 db/migrations/5.4.18_fix_tool_positions.php | 58 +++++++++++++++++++++
 1 file changed, 58 insertions(+)
 create mode 100644 db/migrations/5.4.18_fix_tool_positions.php

diff --git a/db/migrations/5.4.18_fix_tool_positions.php b/db/migrations/5.4.18_fix_tool_positions.php
new file mode 100644
index 00000000000..72aa4ca24a1
--- /dev/null
+++ b/db/migrations/5.4.18_fix_tool_positions.php
@@ -0,0 +1,58 @@
+<?php
+
+class FixToolPositions extends Migration
+{
+    public function description()
+    {
+        return 'move admin tool to first position in course';
+    }
+
+    public function up()
+    {
+        $db = DBManager::get();
+
+        // update default order in sem_classes
+        $result = $db->query('SELECT * FROM sem_classes');
+        $update = $db->prepare('UPDATE sem_classes SET modules = ? WHERE id = ?');
+
+        foreach ($result as $row) {
+            $modules = json_decode($row['modules'], true);
+            $admin = $modules['CoreAdmin'] ?? $modules['CoreStudygroupAdmin'];
+            unset($modules['CoreAdmin'], $modules['CoreStudygroupAdmin']);
+
+            if ($row['studygroup_mode']) {
+                $modules = ['CoreStudygroupAdmin' => $admin] + $modules;
+            } else {
+                $modules = ['CoreAdmin' => $admin] + $modules;
+            }
+
+            $update->execute([json_encode($modules), $row['id']]);
+        }
+
+        // update individual order in tools_activated
+        $stmt = $db->query('SELECT pluginclassname, pluginid FROM plugins');
+        $plugins = $stmt->fetchAll(PDO::FETCH_COLUMN | PDO::FETCH_GROUP | PDO::FETCH_UNIQUE);
+        $admin_ids = [$plugins['CoreAdmin'], $plugins['CoreStudygroupAdmin']];
+
+        $result = $db->query('SELECT * FROM tools_activated ORDER BY range_id, position');
+        $update = $db->prepare('UPDATE tools_activated SET position = ? WHERE range_id = ? AND plugin_id = ?');
+        $range_id = null;
+
+        foreach ($result as $row) {
+            if ($range_id !== $row['range_id']) {
+                $range_id = $row['range_id'];
+                $position = 0;
+            }
+
+            if (in_array($row['plugin_id'], $admin_ids)) {
+                $new_position = 0;
+            } else {
+                $new_position = ++$position;
+            }
+
+            if ($row['position'] != $new_position) {
+                $update->execute([$new_position, $range_id, $row['plugin_id']]);
+            }
+        }
+    }
+}
-- 
GitLab