aboutsummaryrefslogtreecommitdiff
path: root/db/migrations/5.4.18_fix_tool_positions.php
blob: 72aa4ca24a108e5d283ee033d7a66bc24208ba72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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']]);
            }
        }
    }
}