aboutsummaryrefslogtreecommitdiff
path: root/db/migrations/1.246_consultations.php
blob: 6b31373bb39b9d688b11771603d9a6b3facd10de (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
class Consultations extends Migration
{
    public function up()
    {
        // Create tables
        $query = "CREATE TABLE IF NOT EXISTS `consultation_blocks` (
                    `block_id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
                    `teacher_id` CHAR(32) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL,
                    `start` INT(11) UNSIGNED NOT NULL,
                    `end` INT(11) UNSIGNED NOT NULL,
                    `room` VARCHAR(128) NOT NULL,
                    `calendar_events` TINYINT(1) UNSIGNED NOT NULL DEFAULT 0 COMMENT 'Create events for slots',
                    `note` TEXT NOT NULL,
                    `size` TINYINT(2) UNSIGNED NOT NULL DEFAULT 1 COMMENT 'How many people may book a slot',
                    `course_id` CHAR(32) CHARACTER SET latin1 COLLATE latin1_bin NULL DEFAULT NULL,
                    `mkdate` INT(11) UNSIGNED NOT NULL,
                    `chdate` INT(11) UNSIGNED NOT NULL,
                    PRIMARY KEY (`block_id`),
                    KEY `teacher_id` (`teacher_id`)
                  ) ENGINE=InnoDB ROW_FORMAT=DYNAMIC";
        DBManager::get()->exec($query);

        $query = "CREATE TABLE IF NOT EXISTS `consultation_slots` (
                    `slot_id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
                    `block_id` INT(11) UNSIGNED NOT NULL,
                    `start_time` INT(11) UNSIGNED NOT NULL,
                    `end_time` INT(11) UNSIGNED NOT NULL,
                    `note` TEXT NOT NULL,
                    `teacher_event_id` CHAR(32) CHARACTER SET latin1 COLLATE latin1_bin NULL DEFAULT NULL,
                    `mkdate` INT(11) UNSIGNED NOT NULL,
                    `chdate` INT(11) UNSIGNED NOT NULL,
                    PRIMARY KEY (`slot_id`),
                    KEY `block_id` (`block_id`)
                  ) ENGINE=InnoDB ROW_FORMAT=DYNAMIC";
        DBManager::get()->exec($query);

        $query = "CREATE TABLE IF NOT EXISTS `consultation_bookings` (
                    `booking_id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
                    `slot_id` INT(11) UNSIGNED NOT NULL,
                    `user_id` CHAR(32) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL,
                    `reason` TEXT NOT NULL,
                    `student_event_id` CHAR(32) CHARACTER SET latin1 COLLATE latin1_bin NULL DEFAULT NULL,
                    `mkdate` INT(11) UNSIGNED NOT NULL,
                    `chdate` INT(11) UNSIGNED NOT NULL,
                    PRIMARY KEY (`booking_id`),
                    KEY `block_id` (`slot_id`),
                    KEY `user_id` (`user_id`)
                  ) ENGINE=InnoDB ROW_FORMAT=DYNAMIC";
        DBManager::get()->exec($query);

        // Add config entries (global and user)
        $query = "INSERT IGNORE INTO `config` (
                    `field`, `value`, `type`, `range`,
                    `section`, `description`,
                    `mkdate`, `chdate`
                  ) VALUES (
                      'CONSULTATION_ENABLED', '0', 'boolean', 'global',
                      'Sprechstunden', 'Schaltet die Sprechstunden global ein',
                      UNIX_TIMESTAMP(), UNIX_TIMESTAMP()
                  )";
        DBManager::get()->exec($query);

        $query = "INSERT IGNORE INTO `config` (
                    `field`, `value`, `type`, `range`,
                    `section`, `description`,
                    `mkdate`, `chdate`
                  ) VALUES (
                      'CONSULTATION_REQUIRED_PERMISSION', 'dozent', 'string', 'global',
                      'Sprechstunden', 'Ab welcher Rechtestufe dürfen Nutzer Sprechstunden anlegen (user, autor, tutor, dozent, admin, root)',
                      UNIX_TIMESTAMP(), UNIX_TIMESTAMP()
                  )";
        DBManager::get()->exec($query);

        $query = "INSERT IGNORE INTO `config` (
                    `field`, `value`, `type`, `range`,
                    `section`, `description`,
                    `mkdate`, `chdate`
                  ) VALUES (
                      'CONSULTATION_ALLOW_DOCENTS_RESERVING', '0', 'boolean', 'global',
                      'Sprechstunden', 'Dozenten können sich bei anderen Dozenten anmelden',
                      UNIX_TIMESTAMP(), UNIX_TIMESTAMP()
                  )";
        DBManager::get()->exec($query);

        $query = "INSERT IGNORE INTO `config` (
                    `field`, `value`, `type`, `range`,
                    `section`, `description`,
                    `mkdate`, `chdate`
                  ) VALUES (
                      'CONSULTATION_SEND_MESSAGES', '1', 'boolean', 'user',
                      'Sprechstunden', 'Nachrichten empfangen über Buchungen/Stornierungen',
                      UNIX_TIMESTAMP(), UNIX_TIMESTAMP()
                  )";
        DBManager::get()->exec($query);

        $this->migratePlugin();
    }

    public function down()
    {
        // Remove tables
        $query = "DROP TABLE IF EXISTS `consultation_blocks`,
                                       `consultation_slots`,
                                       `consultation_bookings`";
        DBManager::get()->exec($query);

        // Remove config entries
        $query = "DELETE `config`, `config_values`
                  FROM `config`
                  LEFT JOIN `config_values` USING (`field`)
                  WHERE `field` IN (
                      'CONSULTATION_ENABLED',
                      'CONSULTATION_REQUIRED_PERMISSION'
                  )";
        DBManager::get()->exec($query);
    }

    protected function migratePlugin()
    {
        // Detect plugin by tables
        $query = "SHOW TABLES LIKE 'SprechstundenAnmeldung'";
        $statement = DBManager::get()->query($query);

        if ($statement->rowCount() === 0) {
            // No plugin data
            return;
        }

        // Check database format
        $query = "SHOW COLUMNS FROM `SprechstundenTerminDesc`
                  WHERE `Field` IN ('note_on_schedule', 'in_calendar')";
        $statement = DBManager::get()->query($query);

        if ($statement->rowCount() !== 2) {
            $this->announce('Unable to migrate SprechstundenPlugin data due to incompatible database format');
            return;
        }


        // Detect which plugin version was used
        $query = "SHOW COLUMNS FROM SprechstundenTerminDesc LIKE 'size'";
        $has_size = (bool) DBManager::get()->query($query)->fetchColumn();
        $size_col = $has_size ? '`size`' : 1;

        // Migrate blocks
        $query = "INSERT INTO `consultation_blocks` (
                    `block_id`, `teacher_id`, `start`, `end`,
                    `room`, `calendar_events`, `note`, `size`,
                    `mkdate`, `chdate`
                ) VALUES (
                    NULL, :teacher_id, :start, :end,
                    :room, :calendar, :note, :size,
                    UNIX_TIMESTAMP(), UNIX_TIMESTAMP()
                )";
        $insert = DBManager::get()->prepare($query);

        $blocks = [];

        $query = "SELECT `id`, `dozent_id`,
                         `ort`, `in_calendar`, `note_on_schedule`, {$size_col} AS `size`,
                         `am`, `intervall`,
                         FROM_UNIXTIME(`start_date`) AS start_date,
                         FROM_UNIXTIME(`end_date`) AS end_date
                  FROM `SprechstundenTerminDesc`";
        $statement = DBManager::get()->query($query);
        $statement->setFetchMode(PDO::FETCH_ASSOC);
        foreach ($statement as $row) {
            extract($row);

            $start_date = strtotime($start_date);
            $end_date   = strtotime($end_date);

            $insert->bindValue(':teacher_id', $dozent_id);
            $insert->bindValue(':room', $ort);
            $insert->bindValue(':calendar', $in_calendar);
            $insert->bindValue(':note', $note_on_schedule);
            $insert->bindValue(':size', $size);

            $current = $start_date;
            while (date('w', $current) != $am) {
                $current = strtotime('+1 day', $current);
            }

            $blocks[$id] = [];
            while ($current <= $end_date) {
                $start = $this->adjustTimestamp($current, $start_date);
                $end   = $this->adjustTimestamp($current, $end_date);

                $insert->bindValue(':start', $start);
                $insert->bindValue(':end', $end);
                $insert->execute();

                $blocks[$id][] = [
                    'start' => $start,
                    'end'   => $end,
                    'id'    => DBManager::get()->lastInsertId(),
                ];

                $current = strtotime("+{$intervall} weeks", $current);
            }
        }

        // Migrate slots
        $query = "INSERT INTO `consultation_slots` (
                    `slot_id`, `block_id`,
                    `start_time`,
                    `end_time`,
                    `note`, `teacher_event_id`,
                    `mkdate`, `chdate`
                ) VALUES (
                    :slot_id, :block_id, :start, :end, :note, :teacher_event_id,
                    UNIX_TIMESTAMP(), UNIX_TIMESTAMP()
                )";
        $insert = DBManager::get()->prepare($query);

        $query = "SELECT szs.`id`, st.`desc_id`,
                         szs.`position`, std.`dauer`,
                         FROM_UNIXTIME(st.`start_time`) AS start_time,
                         FROM_UNIXTIME(st.`end_time`) AS end_time,
                         IFNULL(szs.`note_on_schedule`, '') AS `note`,
                         sa.`event_id_dozent`
                  FROM `SprechstundenTermin` AS st
                  JOIN `SprechstundenZeitSlot` AS szs ON szs.`termin_id` = st.`id`
                  JOIN `SprechstundenTerminDesc` AS std ON st.`desc_id` = std.`id`
                  LEFT JOIN `SprechstundenAnmeldung` AS sa ON sa.`zeitslot_id` = szs.`id`
                  GROUP BY szs.`id`";
        $statement = DBManager::get()->query($query);
        $statement->setFetchMode(PDO::FETCH_ASSOC);


        $used = [];
        foreach ($statement as $row) {
            extract($row);

            // get block id
            if (!isset($blocks[$desc_id])) {
                continue;
            }

            $duration = $dauer * ($position - 1);
            $start = strtotime("+{$duration} minutes", strtotime($start_time));
            $end   = strtotime("+{$dauer} minutes", $start);

            $block_id = false;
            foreach ($blocks[$desc_id] as $block) {
                if ($start <= $block['end'] && $end >= $block['start']) {
                    $block_id = $block['id'];
                    break;
                }
            }

            if (!in_array($block_id, $used)) {
                $used[] = $block_id;
            }

            $insert->bindValue(':slot_id', $id);
            $insert->bindValue(':block_id', $block_id);
            $insert->bindValue(':start', $start);
            $insert->bindValue(':end', $end);
            $insert->bindValue(':note', $note);
            $insert->bindValue(':teacher_event_id', $event_id_dozent);
            $insert->execute();
        }

        // Remove empty blocks
        $query = "DELETE FROM `consultation_blocks`
                  WHERE `block_id` NOT IN (?)";
        DBManager::get()->execute($query, [$used ?: '']);

        // Migrate bookings
        $query = "INSERT INTO `consultation_bookings` (
                    `booking_id`, `slot_id`, `user_id`,
                    `reason`, `student_event_id`,
                    `mkdate`, `chdate`
                  )
                  SELECT `id`, `zeitslot_id`, `user_id`,
                         `grund`, `event_id_student`,
                         UNIX_TIMESTAMP(), UNIX_TIMESTAMP()
                  FROM `SprechstundenAnmeldung`";
        DBManager::get()->exec($query);

        // Get old plugin info
        $query = "SELECT `pluginid`, `enabled` = 'yes' AS is_active, `pluginpath`
                  FROM `plugins`
                  WHERE `pluginclassname` = 'SprechstundenPlugin'";
        $info = DBManager::get()->query($query)->fetch(PDO::FETCH_ASSOC);

        if (!$info) {
            return;
        }

        // Active consultations if plugin was activated
        if ($info['is_active']) {
            $query = "INSERT INTO `config_values` (
                    `field`, `range_id`, `value`,
                    `mkdate`, `chdate`, `comment`
                  ) VALUES (
                    'CONSULTATION_ENABLED', 'studip', '1',
                    UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), ''
                  )";
            DBManager::get()->exec($query);
        }

        // Remove plugin from database
        $query = "DELETE FROM `plugins`
                  WHERE `pluginclassname` = 'SprechstundenPlugin'";
        DBManager::get()->exec($query);

        DBManager::get()->execute("DELETE FROM plugins_activated WHERE pluginid = ?", [$info['pluginid']]);
        DBManager::get()->execute("DELETE FROM roles_plugins WHERE pluginid = ?", [$info['pluginid']]);

        // Delete plugin files
        $plugin_path = "{$GLOBALS['PLUGINS_PATH']}/{$info['pluginpath']}";
        if (file_exists($plugin_path)) {
            @rmdirr($plugin_path);
        }

        // Delete old plugin tables
        $query = "DROP TABLE IF EXISTS `SprechstundenAnmeldung`";
        DBManager::get()->exec($query);

        $query = "DROP TABLE IF EXISTS `SprechstundenTermin`";
        DBManager::get()->exec($query);

        $query = "DROP TABLE IF EXISTS `SprechstundenTerminDesc`";
        DBManager::get()->exec($query);

        $query = "DROP TABLE IF EXISTS `SprechstundenZeitSlot`";
        DBManager::get()->exec($query);
    }

    private function adjustTimestamp($current, $other)
    {
        $time = date('H:i', $other);
        $current = strtotime("today {$time}", $current);
        return $current;
    }
}