aboutsummaryrefslogtreecommitdiff
path: root/db/migrations/5.2.16_remove_global_search_my_courses.php
blob: 254886261da341c288cfc2ff3723a56daf102399 (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
<?php
return new class extends Migration
{
    public function description()
    {
        return 'Remove global search for my courses';
    }

    protected function up()
    {
        foreach (['config', 'config_values'] as $table) {
            $this->removeConfiguration($table);
        }

        $query = "DELETE `config_values`
                  FROM `config`
                  LEFT JOIN `config_values` USING (`field`)
                  WHERE `field` = 'GLOBALSEARCHMODULES'
                    AND `config`.`value` = `config_values`.`value`";
        DBManager::get()->exec($query);
    }

    protected function down()
    {
        // We will not activate the search module since we cannot know it's
        // previous state
    }

    private function removeConfiguration(string $table): void
    {
        $query = "SELECT `value`
                  FROM `{$table}`
                  WHERE `field` = 'GLOBALSEARCH_MODULES'";
        $json = DBManager::get()->fetchColumn($query);

        if (!$json) {
            return;
        }

        $modules = json_decode($json, true);
        $modules = array_filter(
            $modules,
            function ($index) {
                return $index !== 'GlobalSearchMyCourses';
            },
            ARRAY_FILTER_USE_KEY
        );

        $query = "UPDATE `{$table}`
                  SET `value` = ?
                  WHERE `field` = 'GLOBALSEARCH_MODULES'";
        DBManager::get()->execute($query, [json_encode($modules)]);
    }
};