aboutsummaryrefslogtreecommitdiff
path: root/db/migrations/6.0.32_integrate_garuda_plugin.php
blob: 2e0861476a6bd34bc4ab91fd5bd62b705c903ab1 (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
<?php
require_once 'lib/models/MassMail/MassMailPermission.php';
require_once 'lib/cronjobs/send_massmails.php';

return new class extends Migration
{
    use DatabaseMigrationTrait;

    public function description()
    {
        return 'Integrate the functionality from the Garuda plugin into the Stud.IP core.';
    }

    protected function up()
    {
        // Messages and templates
        DBManager::get()->exec("CREATE TABLE IF NOT EXISTS `massmail_messages` (
            `message_id` INT NOT NULL AUTO_INCREMENT,
            `sender_id` CHAR(32) COLLATE latin1_bin,
            `author_id` CHAR(32) COLLATE latin1_bin NOT NULL,
            `send_at_date` INT,
            `target` ENUM('all', 'students', 'employees', 'lecturers', 'courses', 'usernames') COLLATE latin1_bin,
            `config` LONGTEXT,
            `exclude_users` LONGTEXT,
            `cc` TEXT,
            `subject` VARCHAR(255) NOT NULL,
            `message` TEXT NOT NULL,
            `folder_id` CHAR(32) COLLATE latin1_bin,
            `is_template` TINYINT(1) NOT NULL DEFAULT 0,
            `locked` TINYINT(1) NOT NULL DEFAULT 0,
            `sent` TINYINT(1) NOT NULL DEFAULT 0,
            `protected` TINYINT(1) NOT NULL DEFAULT 0,
            `mkdate` INT UNSIGNED NOT NULL,
            `chdate` INT UNSIGNED NOT NULL,
            PRIMARY KEY (`message_id`),
            INDEX author_id (`author_id`)
        )");

        // Permissions for using this functionality
        DBManager::get()->exec("CREATE TABLE IF NOT EXISTS `massmail_permissions` (
            `permission_id` INT NOT NULL AUTO_INCREMENT,
            `institute_id` CHAR(32) COLLATE latin1_bin NOT NULL,
            `min_perm` ENUM ('admin', 'dozent', 'tutor', 'autor') COLLATE latin1_bin NOT NULL DEFAULT 'admin',
            `mkdate` INT UNSIGNED NOT NULL,
            `chdate` INT UNSIGNED NOT NULL,
            PRIMARY KEY (`permission_id`),
            UNIQUE INDEX institute_id (`institute_id`)
        )");

        // Allowed degrees
        DBManager::get()->exec("CREATE TABLE IF NOT EXISTS `massmail_permission_degree` (
            `permission_id` INT NOT NULL,
            `degree_id` CHAR(32) COLLATE latin1_bin NOT NULL,
            `mkdate` INT UNSIGNED NOT NULL,
            PRIMARY KEY (`permission_id`, `degree_id`),
            INDEX degree_id (`degree_id`)
        )");

        // Allowed subjects of study
        DBManager::get()->exec("CREATE TABLE IF NOT EXISTS `massmail_permission_subject` (
            `permission_id` INT NOT NULL,
            `subject_id` CHAR(32) COLLATE latin1_bin NOT NULL,
            `mkdate` INT UNSIGNED NOT NULL,
            PRIMARY KEY (`permission_id`, `subject_id`),
            INDEX subject_id (`subject_id`)
        )");

        // Allowed institutes
        DBManager::get()->exec("CREATE TABLE IF NOT EXISTS `massmail_permission_institute` (
            `permission_id` INT NOT NULL,
            `institute_id` CHAR(32) COLLATE latin1_bin NOT NULL,
            `mkdate` INT UNSIGNED NOT NULL,
            PRIMARY KEY (`permission_id`, `institute_id`),
            INDEX institute_id (`institute_id`)
        )");

        // User filters
        DBManager::get()->exec("CREATE TABLE IF NOT EXISTS `massmail_filter` (
            `message_id` INT NOT NULL,
            `filter_id` CHAR(32) COLLATE latin1_bin NOT NULL,
            `mkdate` INT UNSIGNED NOT NULL,
            PRIMARY KEY (`message_id`, `filter_id`),
            INDEX filter_id (`filter_id`)
        )");

        // User-specific tokens
        DBManager::get()->exec("CREATE TABLE IF NOT EXISTS `massmail_tokens` (
            `token_id` INT NOT NULL AUTO_INCREMENT,
            `message_id` INT NOT NULL,
            `user_id` CHAR(32) COLLATE latin1_bin,
            `token` VARCHAR(1024) NOT NULL,
            `mkdate` INT UNSIGNED NOT NULL,
            PRIMARY KEY (`token_id`),
            INDEX message_id (`message_id`)
        )");

        // Serial mail markers
        DBManager::get()->exec("CREATE TABLE IF NOT EXISTS `massmail_markers` (
            `marker_id` INT NOT NULL AUTO_INCREMENT,
            `marker` VARCHAR(255) NOT NULL,
            `name` VARCHAR(255) NOT NULL,
            `type` ENUM('text', 'database', 'function', 'token') COLLATE latin1_bin,
            `description` TEXT,
            `root_only` TINYINT(1) UNSIGNED DEFAULT 0,
            `replacement` TEXT,
            `replacement_female` TEXT,
            `replacement_unknown` TEXT,
            `position` TINYINT(1) UNSIGNED,
            `mkdate` INT UNSIGNED NOT NULL,
            `chdate` INT UNSIGNED NOT NULL,
            PRIMARY KEY (`marker_id`)
        )");

        $markers = [
            [
                'marker' => 'FULLNAME',
                'name' => 'Voller Name',
                'type' => 'database',
                'description' => _('Hier wird der volle Name der jeweiligen Person eingesetzt, z.B. "Prof. Max Mustermann, PhD".'),
                'replacement' => 'user_info.title_front {{FIRSTNAME}} {{LASTNAME}} user_info.title_rear',
                'position' => 2
            ],
            [
                'marker' => 'FIRSTNAME',
                'name' => 'Vorname',
                'type' => 'database',
                'description' => _('Hier wird der Vorname der jeweiligen Person eingesetzt.'),
                'replacement' => 'auth_user_md5.Vorname',
                'position' => 3
            ],
            [
                'marker' => 'LASTNAME',
                'name' => 'Nachname',
                'type' => 'database',
                'description' => _('Hier wird der Nachname der jeweiligen Person eingesetzt.'),
                'replacement' => 'auth_user_md5.Nachname',
                'position' => 4
            ],
            [
                'marker' => 'USERNAME',
                'name' => 'Benutzername',
                'type' => 'database',
                'description' => _('Hier wird der Benutzername der jeweiligen Person eingesetzt.'),
                'replacement' => 'auth_user_md5.username',
                'position' => 5
            ],
            [
                'marker' => 'SEHRGEEHRTE',
                'name' => 'Anrede mit vollem Namen',
                'type' => 'text',
                'description' => _('Hier wird eine Anrede erzeugt: "Sehr geehrte Michaela Musterfrau" bzw. "Sehr geehrter Max Mustermann".'),
                'replacement' => 'Sehr geehrter {{FULLNAME}}',
                'replacement_female' => 'Sehr geehrte {{FULLNAME}}',
                'replacement_unknown' => 'Sehr geehrte/r {{FULLNAME}}',
                'position' => 1
            ],
            [
                'marker' => 'DEARSIRMADAM',
                'name' => 'Anrede (englisch) mit vollem Namen',
                'type' => 'text',
                'description' => _('Creates a Salutation: "Dear Jane Doe" or "Dear John Doe".'),
                'replacement' => 'Dear {{FULLNAME}}',
                'position' => 6
            ],
            [
                'marker' => 'TOKEN',
                'name' => 'Personalisierter Code o.ä.',
                'type' => 'token',
                'description' => _('Hier wird ein persönlicher Teilnahmecode o.ä. aus einer hochgeladenen Datei eingesetzt.'),
                'replacement' => 'massmail_tokens.token',
                'root_only' => 1,
                'position' => 7
            ]
        ];

        foreach ($markers as $data) {
            \MassMail\MassMailMarker::create($data);
        }

        if (empty(RolePersistence::getRoleIdByName(\MassMail\MassMailPermission::MASSMAIL_ROOT_ROLE))) {
            RolePersistence::saveRole(
                new Role(Role::UNKNOWN_ROLE_ID, \MassMail\MassMailPermission::MASSMAIL_ROOT_ROLE)
            );
        }

        DBManager::get()->exec("INSERT IGNORE INTO `config`
             (`field`, `value`, `type`, `range`, `section`, `mkdate`, `chdate`, `description`)
             VALUES
             (
              'MASSMAIL_LECTURER_SEM_CATEGORIES',
              '[1]',
              'array',
              'global',
              'MassMail',
              UNIX_TIMESTAMP(),
              UNIX_TIMESTAMP(),
              'Veranstaltungskategorien, die für die Ermittlung aktiver Lehrender berücksichtigt werden'
             )"
        );
        DBManager::get()->exec("INSERT IGNORE INTO `config`
             (`field`, `value`, `type`, `range`, `section`, `mkdate`, `chdate`, `description`)
             VALUES
             (
              'MASSMAIL_GC_DAYS',
              '7',
              'integer',
              'global',
              'MassMail',
              UNIX_TIMESTAMP(),
              UNIX_TIMESTAMP(),
              'Anzahl Tage, nach denen bereits verschickte Nachrichten aus der Datenbank entfernt werden (0 bedeutet nie)'
             )"
        );

        SendMassmailsJob::register()->schedulePeriodic(-15)->activate();

        /*
         * Extend userfilter table so that we know from which context a specific UserFilter comes from,
         * allowing us to check permissions for editing.
         */
        if (!$this->columnExists('userfilter', 'range_id') && !$this->columnExists('userfilter', 'range_type')) {
            DBManager::get()->exec("ALTER TABLE `userfilter`
                ADD `range_id` VARCHAR(32) COLLATE `latin1_bin` NOT NULL AFTER `filter_id`,
                ADD `range_type` VARCHAR(255) COLLATE `latin1_bin` NOT NULL AFTER `range_id`");
        }

        /*
         * Set context values for existing userfilters (we only need to consider filters from admission rules
         * as only those are part of the core so far)
         */

        // First: filters from ConditionalAdmissions
        $conditions = DBManager::get()->fetchAll(
            "SELECT DISTINCT c.`filter_id`, r.`set_id` FROM `admission_condition` c
             JOIN `courseset_rule` r USING (`rule_id`)"
        );
        // Second: filters from PreferentialAdmissions
        $preferential = DBManager::get()->fetchAll(
            "SELECT DISTINCT p.`condition_id` AS filter_id, r.`set_id` FROM `prefadmission_condition` p
             JOIN `courseset_rule` r USING (`rule_id`)"
        );
        foreach (array_merge($conditions, $preferential) as $filter) {
            DBManager::get()->execute(
                "UPDATE `userfilter` SET `range_id` = :range, `range_type` = :type WHERE `filter_id` = :filter",
                ['range' => $filter['set_id'], 'type' => CourseSet::class, 'filter' => $filter['filter_id']]
            );
        }
    }

    protected function down()
    {
        $tables = [
            'massmail_messages',
            'massmail_permissions',
            'massmail_permission_degree',
            'massmail_permission_subject',
            'massmail_permission_institute',
            'massmail_filter',
            'massmail_tokens',
            'massmail_markers'
        ];
        DBManager::get()->execute(
            "DROP TABLE IF EXISTS `" . implode('`,`', $tables) . "`");

        $id = RolePersistence::getRoleIdByName(\MassMail\MassMailPermission::MASSMAIL_ROOT_ROLE);
        if (!empty($id)) {
            RolePersistence::deleteRole(new Role($id));
        }

        DBManager::get()->execute(
            "DELETE FROM `config_values` WHERE `field` = :field",
            ['field' => 'MASSMAIL_LECTURER_SEM_CATEGORIES']
        );
        DBManager::get()->execute(
            "DELETE FROM `config` WHERE `field` = :field",
            ['field' => 'MASSMAIL_LECTURER_SEM_CATEGORIES']
        );

        SendMassmailsJob::unregister();

        if ($this->columnExists('userfilter', 'range_id') && $this->columnExists('userfilter', 'range_type')) {
            DBManager::get()->exec("ALTER TABLE `userfilter` DROP `range_id`, DROP `range_type`");
        }
    }
};