aboutsummaryrefslogtreecommitdiff
path: root/db/migrations/1.296_biest_10803_fix.php
blob: 0c25127a53fe949e25bc39125bf86b6524739d97 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
/**
 * This migration will cleanup the table mvv_modul_deskriptor and alter the
 * table by adding a unique key on `modul_id`.
 *
 * @author Jan-Hendrik Willms <tleilax+studip@gmail.com>
 * @see    https://develop.studip.de/trac/ticket/10803
 */
class Biest10803Fix extends Migration
{
    public function description()
    {
        return 'This migration will cleanup mvv_modul_deskriptor and add '
             . 'a unique key on column modul_id';
    }

    public function up()
    {
        // Select neccessary modul ids
        $query = "SELECT `modul_id`
                  FROM `mvv_modul_deskriptor`
                  GROUP BY `modul_id`
                  HAVING COUNT(*) > 1";
        $statement = DBManager::get()->query($query);
        $modul_ids = $statement->fetchAll(PDO::FETCH_COLUMN);

        // Prepare statement that reads all deskriptor info
        $query = "SELECT *
                  FROM `mvv_modul_deskriptor`
                  WHERE `modul_id` = :id
                  ORDER BY `chdate` ASC";
        $data_statement = DBManager::get()->prepare($query);

        // Prepare query that updates a module deskriptor
        $query = "UPDATE `mvv_modul_deskriptor`
                  SET `verantwortlich` = :verantwortlich,
                      `bezeichnung` = :bezeichnung,
                      `voraussetzung` = :voraussetzung,
                      `kompetenzziele` = :kompetenzziele,
                      `inhalte` = :inhalte,
                      `literatur` = :literatur,
                      `links` = :links,
                      `kommentar` = :kommentar,
                      `turnus` = :turnus,
                      `kommentar_kapazitaet` = :kommentar_kapazitaet,
                      `kommentar_sws` = :kommentar_sws,
                      `kommentar_wl_selbst` = :kommentar_wl_selbst,
                      `kommentar_wl_pruef` = :kommentar_wl_pruef,
                      `kommentar_note` = :kommentar_note,
                      `pruef_vorleistung` = :pruef_vorleistung,
                      `pruef_leistung` = :pruef_leistung,
                      `pruef_wiederholung` = :pruef_wiederholung,
                      `ersatztext` = :ersatztext
                  WHERE `deskriptor_id` = :id";
        $update_statement = DBManager::get()->prepare($query);

        // Prepare statements that removes all unneccessary deskriptors
        $query = "DELETE FROM `mvv_modul_deskriptor`
                  WHERE `deskriptor_id` IN (:ids)";
        $remove_statement = DBManager::get()->prepare($query);

        // For each module id, gather all info chronologically and combine
        // them. This way, hopefully no valid information will be lost.
        foreach ($modul_ids as $modul_id) {
            $data_statement->bindValue(':id', $modul_id);
            $data_statement->execute();
            $data_statement->setFetchMode(PDO::FETCH_ASSOC);

            $remove_desk_ids = [];

            $data = [];
            foreach ($data_statement as $row) {
                if (!$data) {
                    $data = $row;
                    continue;
                }

                foreach ($row as $key => $value) {
                    if (in_array($key, ['deskriptor_id', 'mkdate', 'chdate'])) {
                        continue;
                    }
                    if ($value || $row['author_id'] || $row['editor_id']) {
                        $data[$key] = $value;
                    }
                }

                $remove_desk_ids[] = $row['deskriptor_id'];
            }

            $update_statement->bindValue(':verantwortlich', $data['verantwortlich']);
            $update_statement->bindValue(':bezeichnung', $data['bezeichnung']);
            $update_statement->bindValue(':voraussetzung', $data['voraussetzung']);
            $update_statement->bindValue(':kompetenzziele', $data['kompetenzziele']);
            $update_statement->bindValue(':inhalte', $data['inhalte']);
            $update_statement->bindValue(':literatur', $data['literatur']);
            $update_statement->bindValue(':links', $data['links']);
            $update_statement->bindValue(':kommentar', $data['kommentar']);
            $update_statement->bindValue(':turnus', $data['turnus']);
            $update_statement->bindValue(':kommentar_kapazitaet', $data['kommentar_kapazitaet']);
            $update_statement->bindValue(':kommentar_sws', $data['kommentar_sws']);
            $update_statement->bindValue(':kommentar_wl_selbst', $data['kommentar_wl_selbst']);
            $update_statement->bindValue(':kommentar_wl_pruef', $data['kommentar_wl_pruef']);
            $update_statement->bindValue(':kommentar_note', $data['kommentar_note']);
            $update_statement->bindValue(':pruef_vorleistung', $data['pruef_vorleistung']);
            $update_statement->bindValue(':pruef_leistung', $data['pruef_leistung']);
            $update_statement->bindValue(':pruef_wiederholung', $data['pruef_wiederholung']);
            $update_statement->bindValue(':ersatztext', $data['ersatztext']);
            $update_statement->bindValue(':id', $data['deskriptor_id']);
            $update_statement->execute();

            if ($remove_desk_ids) {
                $remove_statement->bindValue(':ids', $remove_desk_ids);
                $remove_statement->execute();
            }
        }

        // Add unique key on column modul_id
        $query = "ALTER TABLE `mvv_modul_deskriptor`
                    DROP KEY `modul_id`,
                    ADD UNIQUE KEY `modul_id` (`modul_id`)";
        DBManager::get()->exec($query);
    }

    public function down()
    {
        // Drop unique key on column modul_id
        $query = "ALTER TABLE `mvv_modul_deskriptor`
                    DROP KEY `modul_id`,
                    ADD KEY `modul_id` (`modul_id`)";
        DBManager::get()->exec($query);
    }
}