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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
|
<?php
class AddVipsModule extends Migration
{
public function description()
{
return 'initial database setup for Vips';
}
public function up()
{
$db = DBManager::get();
// install as core plugin
$sql = "INSERT INTO plugins (pluginclassname, pluginname, plugintype, enabled, navigationpos)
VALUES ('VipsModule', 'Aufgaben', 'StudipModule,SystemPlugin,PrivacyPlugin,Courseware\\\\CoursewarePlugin', 'yes', 1)";
$db->exec($sql);
$id = $db->lastInsertId();
$sql = "INSERT INTO roles_plugins (roleid, pluginid)
SELECT roleid, ? FROM roles WHERE `system` = 'y'";
$db->execute($sql, [$id]);
// copy tool activations from Vips plugin
$sql = "INSERT INTO tools_activated
SELECT range_id, range_type, ?, position, metadata, mkdate, chdate FROM tools_activated
WHERE plugin_id = (SELECT pluginid FROM plugins WHERE pluginname = 'Vips')";
$db->execute($sql, [$id]);
// update etask tables
$sql = "ALTER TABLE etask_assignments
CHANGE type type varchar(64) COLLATE latin1_bin NOT NULL,
CHANGE active active tinyint UNSIGNED NOT NULL DEFAULT 1,
ADD weight float NOT NULL DEFAULT 0 AFTER active,
ADD block_id int DEFAULT NULL AFTER weight,
ADD KEY test_id (test_id),
ADD KEY range_id (range_id)";
$db->exec($sql);
$sql = "ALTER TABLE etask_assignment_attempts
ADD ip_address varchar(39) COLLATE latin1_bin NOT NULL AFTER end,
CHANGE options options text DEFAULT NULL,
ADD UNIQUE KEY assignment_id (assignment_id,user_id)";
$db->exec($sql);
$sql = "ALTER TABLE etask_responses
CHANGE response response mediumtext NOT NULL,
ADD student_comment text DEFAULT NULL AFTER response,
ADD ip_address varchar(39) COLLATE latin1_bin NOT NULL AFTER student_comment,
ADD commented_solution text DEFAULT NULL AFTER feedback,
ADD KEY assignment_id (assignment_id,task_id,user_id),
ADD KEY user_id (user_id),
ADD KEY task_id (task_id)";
$db->exec($sql);
$sql = "ALTER TABLE etask_tasks
CHANGE type type varchar(64) COLLATE latin1_bin NOT NULL,
CHANGE description description mediumtext NOT NULL,
CHANGE task task mediumtext NOT NULL,
ADD KEY user_id (user_id)";
$db->exec($sql);
$sql = "ALTER TABLE etask_tests
CHANGE description description mediumtext NOT NULL,
CHANGE options options text DEFAULT NULL,
ADD KEY user_id (user_id)";
$db->exec($sql);
$sql = "ALTER TABLE etask_test_tasks
ADD part int NOT NULL DEFAULT 0 AFTER position,
ADD KEY task_id (task_id)";
$db->exec($sql);
// add new tables
$sql = "CREATE TABLE etask_blocks (
id int NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL,
range_id char(32) COLLATE latin1_bin NOT NULL,
group_id char(32) COLLATE latin1_bin DEFAULT NULL,
visible tinyint NOT NULL DEFAULT 1,
weight float DEFAULT NULL,
PRIMARY KEY (id),
KEY range_id (range_id)
)";
$db->exec($sql);
$sql = "CREATE TABLE etask_group_members (
group_id char(32) COLLATE latin1_bin NOT NULL,
user_id char(32) COLLATE latin1_bin NOT NULL,
start int unsigned NOT NULL,
end int unsigned DEFAULT NULL,
PRIMARY KEY (group_id,user_id,start),
KEY user_id (user_id)
)";
$db->exec($sql);
// add settings (unless already present)
$sql = 'INSERT IGNORE INTO `config` (`field`, `value`, `type`, `range`, `mkdate`, `chdate`, `description`)
VALUES (:name, :value, :type, :range, UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), :description)';
$statement = DBManager::get()->prepare($sql);
$statement->execute([
':name' => 'VIPS_COURSE_GRADES',
':description' => 'Kursbezogenes Schema zur Notenverteilung in Vips',
':range' => 'course',
':type' => 'array',
':value' => '[]'
]);
$statement->execute([
':name' => 'VIPS_EXAM_RESTRICTIONS',
':description' => 'Sperrt während einer Klausur andere Bereiche von Stud.IP für die Teilnehmenden',
':range' => 'global',
':type' => 'boolean',
':value' => '0'
]);
$statement->execute([
':name' => 'VIPS_EXAM_ROOMS',
':description' => 'Zentral verwaltete IP-Adressen für PC-Räume',
':range' => 'global',
':type' => 'array',
':value' => '[]'
]);
$statement->execute([
':name' => 'VIPS_EXAM_TERMS',
':description' => 'Teilnahmebedingungen, die vor Beginn einer Klausur zu akzeptieren sind',
':range' => 'global',
':type' => 'string',
':value' => ''
]);
// copy data from Vips plugin
$result = $db->query("SHOW TABLES LIKE 'vips_assignment'");
if ($result->rowCount() > 0) {
$this->copyVipsData();
}
}
private function copyVipsData()
{
$db = DBManager::get();
$now = time();
$task_id = [];
$test_id = [];
$assignment_id = [];
$response_id = [];
$group_id = [];
$folder_id = [];
$task_mapping = [
'sc_exercise' => 'SingleChoiceTask',
'mc_exercise' => 'MultipleChoiceTask',
'mco_exercise' => 'MatrixChoiceTask',
'lt_exercise' => 'TextLineTask',
'tb_exercise' => 'TextTask',
'cloze_exercise' => 'ClozeTask',
'rh_exercise' => 'MatchingTask',
'seq_exercise' => 'SequenceTask'
];
// etask_tasks
$sql = 'INSERT INTO etask_tasks (type, title, description, task, user_id, mkdate, chdate, options)
VALUES (:type, :title, :description, :task, :user_id, :mkdate, :chdate, :options)';
$stmt = $db->prepare($sql);
$data = $db->query('SELECT * FROM vips_exercise');
while ($row = $data->fetch(PDO::FETCH_ASSOC)) {
$values = [
'type' => $task_mapping[$row['type']] ?? $row['type'],
'title' => $row['title'],
'description' => $row['description'],
'task' => $row['task_json'],
'user_id' => $row['user_id'],
'mkdate' => strtotime($row['created']),
'chdate' => $now,
'options' => $row['options'] ?: '[]'
];
$stmt->execute($values);
$task_id[$row['id']] = $db->lastInsertId();
}
// etask_tests
$sql = 'INSERT INTO etask_tests (title, description, user_id, mkdate, chdate, options)
VALUES (:title, :description, :user_id, :mkdate, :chdate, :options)';
$stmt = $db->prepare($sql);
$data = $db->query('SELECT * FROM vips_test');
while ($row = $data->fetch(PDO::FETCH_ASSOC)) {
$values = [
'title' => $row['title'],
'description' => $row['description'],
'user_id' => $row['user_id'],
'mkdate' => strtotime($row['created']),
'chdate' => $now,
'options' => null
];
$stmt->execute($values);
$test_id[$row['id']] = $db->lastInsertId();
}
// etask_test_tasks
$sql = 'INSERT INTO etask_test_tasks (test_id, task_id, position, part, points, options, mkdate, chdate)
VALUES (:test_id, :task_id, :position, :part, :points, :options, :mkdate, :chdate)';
$stmt = $db->prepare($sql);
$data = $db->query('SELECT * FROM vips_exercise_ref');
while ($row = $data->fetch(PDO::FETCH_ASSOC)) {
if (isset($test_id[$row['test_id']]) && isset($task_id[$row['exercise_id']])) {
$values = [
'test_id' => $test_id[$row['test_id']],
'task_id' => $task_id[$row['exercise_id']],
'position' => $row['position'],
'part' => $row['part'],
'points' => $row['points'],
'mkdate' => $now,
'chdate' => $now,
'options' => '',
];
$stmt->execute($values);
}
}
// etask_assignments
$sql = 'INSERT INTO etask_assignments (test_id, range_type, range_id, type, start, end, active, weight, block_id, options, mkdate, chdate)
VALUES (:test_id, :range_type, :range_id, :type, :start, :end, :active, :weight, :block_id, :options, :mkdate, :chdate)';
$stmt = $db->prepare($sql);
$data = $db->query('SELECT * FROM vips_assignment');
while ($row = $data->fetch(PDO::FETCH_ASSOC)) {
if (isset($test_id[$row['test_id']])) {
$options = json_decode($row['options'], true);
unset($options['shuffle_answers']);
unset($options['printable']);
$values = [
'test_id' => $test_id[$row['test_id']],
'range_type' => $row['context'],
'range_id' => $row['course_id'],
'type' => $row['type'],
'start' => strtotime($row['start']),
'end' => $row['end'] ? strtotime($row['end']) : null,
'active' => $row['active'],
'weight' => $row['weight'],
'block_id' => $row['block_id'],
'options' => json_encode($options),
'mkdate' => $now,
'chdate' => $now
];
$stmt->execute($values);
$assignment_id[$row['id']] = $db->lastInsertId();
}
}
// etask_assignment_attempts
$sql = 'INSERT INTO etask_assignment_attempts (assignment_id, user_id, start, end, ip_address, options, mkdate, chdate)
VALUES (:assignment_id, :user_id, :start, :end, :ip_address, :options, :mkdate, :chdate)';
$stmt = $db->prepare($sql);
$data = $db->query('SELECT * FROM vips_assignment_attempt');
while ($row = $data->fetch(PDO::FETCH_ASSOC)) {
if (isset($assignment_id[$row['assignment_id']])) {
$values = [
'assignment_id' => $assignment_id[$row['assignment_id']],
'user_id' => $row['user_id'],
'start' => strtotime($row['start']),
'end' => $row['end'] ? strtotime($row['end']) : null,
'ip_address' => $row['ip_address'],
'options' => $row['options'],
'mkdate' => $now,
'chdate' => $now
];
$stmt->execute($values);
}
}
// etask_responses
$sql = 'INSERT INTO etask_responses (assignment_id, task_id, user_id, response, student_comment, ip_address, state, points, feedback, commented_solution, grader_id, mkdate, chdate, options)
SELECT :assignment_id, :task_id, user_id, response, student_comment, ip_address, corrected, points, corrector_comment, commented_solution, corrector_id, UNIX_TIMESTAMP(time), UNIX_TIMESTAMP(correction_time), options
FROM :table WHERE id = :id';
$stmt = $db->prepare($sql);
$data = $db->query('SELECT id, exercise_id, assignment_id, 0 as archive FROM vips_solution UNION SELECT id, exercise_id, assignment_id, 1 as archive FROM vips_solution_archive ORDER BY id');
while ($row = $data->fetch(PDO::FETCH_ASSOC)) {
if (isset($assignment_id[$row['assignment_id']]) && isset($task_id[$row['exercise_id']])) {
$stmt->bindValue(':assignment_id', $assignment_id[$row['assignment_id']]);
$stmt->bindValue(':task_id', $task_id[$row['exercise_id']]);
$stmt->bindValue(':table', $row['archive'] ? 'vips_solution_archive' : 'vips_solution', StudipPDO::PARAM_COLUMN);
$stmt->bindValue(':id', $row['id']);
$stmt->execute();
$response_id[$row['id']] = $db->lastInsertId();
}
}
// statusgruppen
$sql = 'INSERT INTO statusgruppen (statusgruppe_id, name, range_id, position, size, mkdate, chdate)
VALUES (:statusgruppe_id, :name, :range_id, :position, :size, :mkdate, :chdate)';
$stmt = $db->prepare($sql);
$data = $db->query('SELECT * FROM vips_group');
while ($row = $data->fetch(PDO::FETCH_ASSOC)) {
$id = md5($row['id'] . ':' . uniqid('statusgruppen', true));
$position = $db->fetchColumn('SELECT MAX(position) FROM statusgruppen WHERE range_id = ?', [$row['course_id']]);
$values = [
'statusgruppe_id' => $id,
'name' => $row['name'],
'range_id' => $row['course_id'],
'position' => $position + 1,
'size' => $row['size'],
'mkdate' => $now,
'chdate' => $now
];
$stmt->execute($values);
$group_id[$row['id']] = $id;
}
// etask_blocks
$sql = 'INSERT INTO etask_blocks (id, name, range_id, group_id, visible, weight)
SELECT id, name, course_id, group_id, visible, weight FROM vips_block';
$db->exec($sql);
// etask_group_members
$sql = 'INSERT INTO etask_group_members (group_id, user_id, start, end)
VALUES (:group_id, :user_id, :start, :end)';
$stmt = $db->prepare($sql);
$sql = 'INSERT INTO statusgruppe_user (statusgruppe_id, user_id, mkdate)
VALUES (:group_id, :user_id, :start)';
$stmt2 = $db->prepare($sql);
$data = $db->query('SELECT * FROM vips_group_member');
while ($row = $data->fetch(PDO::FETCH_ASSOC)) {
if (isset($group_id[$row['group_id']])) {
$values = [
'group_id' => $group_id[$row['group_id']],
'user_id' => $row['user_id'],
'start' => strtotime($row['start']),
'end' => $row['end'] ? strtotime($row['end']) : null
];
$stmt->execute($values);
if ($row['end'] === null) {
$stmt2->execute($values);
}
}
}
// files
$sql = 'INSERT INTO files (id, user_id, mime_type, name, size, mkdate, chdate)
VALUES (:id, :user_id, :mime_type, :name, :size, :mkdate, :chdate)';
$stmt = $db->prepare($sql);
$data = $db->query('SELECT * FROM vips_file');
while ($row = $data->fetch(PDO::FETCH_ASSOC)) {
$values = [
'id' => $row['id'],
'user_id' => $row['user_id'],
'mime_type' => $row['mime_type'],
'name' => $row['name'],
'size' => $row['size'],
'mkdate' => strtotime($row['created']),
'chdate' => $now
];
$stmt->execute($values);
}
// folders and file_refs
$sql = 'INSERT INTO folders (id, user_id, parent_id, range_id, range_type, folder_type, name, data_content, description, mkdate, chdate)
VALUES (:id, :user_id, :parent_id, :range_id, :range_type, :folder_type, :name, :data_content, :description, :mkdate, :chdate)';
$stmt_folder = $db->prepare($sql);
$sql = "INSERT INTO file_refs (id, file_id, folder_id, description, content_terms_of_use_id, user_id, name, mkdate, chdate)
VALUES (:id, :file_id, :folder_id, :description, 'UNDEF_LICENSE', :user_id, :name, :mkdate, :chdate)";
$stmt_file_ref = $db->prepare($sql);
$data = $db->query('SELECT * FROM vips_file_ref JOIN vips_file ON vips_file_ref.file_id = vips_file.id');
while ($row = $data->fetch(PDO::FETCH_ASSOC)) {
if ($row['type'] === 'exercise') {
$range_id = $task_id[$row['object_id']] ?? null;
$range_type = 'task';
$folder_type = 'ExerciseFolder';
} else {
$range_id = $response_id[$row['object_id']] ?? null;
$range_type = 'response';
$folder_type = $row['type'] === 'solution' ? 'ResponseFolder' : 'FeedbackFolder';
}
if (isset($range_id)) {
if (!isset($folder_id[$row['object_id'] . ':' . $row['type']])) {
$new_folder_id = md5($row['object_id'] . ':' . uniqid('folders', true));
$values = [
'id' => $new_folder_id,
'user_id' => $row['user_id'],
'parent_id' => '',
'range_id' => $range_id,
'range_type' => $range_type,
'folder_type' => $folder_type,
'name' => '',
'data_content' => '',
'description' => '',
'mkdate' => strtotime($row['created']),
'chdate' => $now
];
$stmt_folder->execute($values);
$folder_id[$row['object_id'] . ':' . $row['type']] = $new_folder_id;
}
$file_ref_id = md5($row['file_id'] . ':' . $row['object_id'] . ':' . uniqid('file_refs' , true));
$values = [
'id' => $file_ref_id,
'file_id' => $row['file_id'],
'folder_id' => $folder_id[$row['object_id'] . ':' . $row['type']],
'description' => '',
'user_id' => $row['user_id'],
'name' => $row['name'],
'mkdate' => strtotime($row['created']),
'chdate' => $now
];
$stmt_file_ref->execute($values);
}
}
}
public function down()
{
$db = DBManager::get();
// unregister core plugin
$sql = "DELETE plugins, roles_plugins, tools_activated FROM plugins
LEFT JOIN roles_plugins USING (pluginid)
LEFT JOIN tools_activated ON plugin_id = pluginid
WHERE pluginclassname = 'VipsModule'";
$db->exec($sql);
// update etask tables
$sql = "ALTER TABLE etask_assignments
CHANGE type type varchar(64) NOT NULL,
CHANGE active active tinyint UNSIGNED NOT NULL,
DROP weight,
DROP block_id,
DROP KEY test_id,
DROP KEY range_id";
$db->exec($sql);
$sql = "ALTER TABLE etask_assignment_attempts
DROP ip_address,
CHANGE options options text NOT NULL,
DROP KEY assignment_id";
$db->exec($sql);
$sql = "ALTER TABLE etask_responses
CHANGE response response text NOT NULL,
DROP student_comment,
DROP ip_address,
DROP commented_solution,
DROP KEY assignment_id,
DROP KEY user_id,
DROP KEY task_id";
$db->exec($sql);
$sql = "ALTER TABLE etask_tasks
CHANGE type type varchar(64) NOT NULL,
CHANGE description description text NOT NULL,
CHANGE task task text NOT NULL,
DROP KEY user_id";
$db->exec($sql);
$sql = "ALTER TABLE etask_tests
CHANGE description description text NOT NULL,
CHANGE options options text NOT NULL,
DROP KEY user_id";
$db->exec($sql);
$sql = "ALTER TABLE etask_test_tasks
DROP part,
DROP KEY task_id";
$db->exec($sql);
// drop new tables
$db->exec('DROP TABLE etask_blocks, etask_group_members');
// remove config entries
$sql = "DELETE config, config_values
FROM config
LEFT JOIN config_values USING (field)
WHERE field IN (
'VIPS_COURSE_GRADES',
'VIPS_EXAM_RESTRICTIONS',
'VIPS_EXAM_ROOMS',
'VIPS_EXAM_TERMS'
)";
$db->exec($sql);
}
}
|