MDL-20636 Previewing a truefalse question in deferred feedback mode now works.
[moodle.git] / local / qedatabase / db / install.php
CommitLineData
2b7da645
TH
1<?php
2
3function xmldb_local_qedatabase_install() {
4 global $DB;
5 $dbman = $DB->get_manager();
6
c76145d3
TH
7 // TODO quiz default settings are now in config_plugins.
8
2b7da645
TH
9 // Bit of a hack to prevent errors like "Cannot downgrade local_qedatabase from ... to ...".
10 $oldversion = 2008000000;
11 $DB->set_field('config_plugins', 'value', $oldversion,
12 array('plugin' => 'local_qedatabase', 'name' => 'version'));
13
14 // Add new preferredbehaviour column to the quiz table.
15 if ($oldversion < 2008000100) {
16 $table = new xmldb_table('quiz');
17 $field = new xmldb_field('preferredbehaviour');
18 $field->set_attributes(XMLDB_TYPE_CHAR, '32', null, null, null, null, 'timeclose');
19 if (!$dbman->field_exists($table, $field)) {
20 $dbman->add_field($table, $field);
21 }
22
23 // quiz savepoint reached
24 upgrade_plugin_savepoint(true, 2008000100, 'local', 'qedatabase');
25 }
26
27 // Populate preferredbehaviour column based on old optionflags column.
28 if ($oldversion < 2008000101) {
29 $DB->set_field_select('quiz', 'preferredbehaviour', 'deferredfeedback',
30 'optionflags = 0');
31 $DB->set_field_select('quiz', 'preferredbehaviour', 'adaptive',
32 'optionflags <> 0 AND penaltyscheme <> 0');
33 $DB->set_field_select('quiz', 'preferredbehaviour', 'adaptivenopenalty',
34 'optionflags <> 0 AND penaltyscheme = 0');
35
36 set_config('quiz_preferredbehaviour', 'deferredfeedback');
37 set_config('quiz_fix_preferredbehaviour', 0);
38
39 // quiz savepoint reached
40 upgrade_plugin_savepoint(true, 2008000101, 'local', 'qedatabase');
41 }
42
43 // Add a not-NULL constraint to the preferredmodel field now that it is populated.
44 if ($oldversion < 2008000102) {
45 $table = new xmldb_table('quiz');
46 $field = new xmldb_field('preferredbehaviour');
47 $field->set_attributes(XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null, 'timeclose');
48
49 $dbman->change_field_notnull($table, $field);
50
51 // quiz savepoint reached
52 upgrade_plugin_savepoint(true, 2008000102, 'local', 'qedatabase');
53 }
54
55 // Drop the old optionflags field.
56 if ($oldversion < 2008000103) {
57 $table = new xmldb_table('quiz');
58 $field = new xmldb_field('optionflags');
59 $dbman->drop_field($table, $field);
60
61 unset_config('quiz_optionflags');
62 unset_config('quiz_fix_optionflags');
63
64 // quiz savepoint reached
65 upgrade_plugin_savepoint(true, 2008000103, 'local', 'qedatabase');
66 }
67
68 // Drop the old penaltyscheme field.
69 if ($oldversion < 2008000104) {
70 $table = new xmldb_table('quiz');
71 $field = new xmldb_field('penaltyscheme');
72 $dbman->drop_field($table, $field);
73
74 unset_config('quiz_penaltyscheme');
75 unset_config('quiz_fix_penaltyscheme');
76
77 // quiz savepoint reached
78 upgrade_plugin_savepoint(true, 2008000104, 'local', 'qedatabase');
79 }
80
81 if ($oldversion < 2008000110) {
82
83 // Changing nullability of field sumgrades on table quiz_attempts to null
84 $table = new xmldb_table('quiz_attempts');
85 $field = new xmldb_field('sumgrades');
86 $field->set_attributes(XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, 'attempt');
87
88 // Launch change of nullability for field sumgrades
89 $dbman->change_field_notnull($table, $field);
90
91 // Launch change of default for field sumgrades
92 $dbman->change_field_default($table, $field);
93
94 // quiz savepoint reached
95 upgrade_plugin_savepoint(true, 2008000110, 'local', 'qedatabase');
96 }
97
98 if ($oldversion < 2008000111) {
99
100 // Changing the default of field penalty on table question to 0.3333333
101 $table = new xmldb_table('question');
102 $field = new xmldb_field('penalty');
103 $field->set_attributes(XMLDB_TYPE_FLOAT, null, null, XMLDB_NOTNULL, null, '0.3333333', 'defaultgrade');
104
105 // Launch change of default for field penalty
106 $dbman->change_field_default($table, $field);
107
108 // quiz savepoint reached
109 upgrade_plugin_savepoint(true, 2008000111, 'local', 'qedatabase');
110 }
111
112// Update the quiz from the old single review column to seven new columns.
113
114 if ($oldversion < 2008000200) {
115
116 // Define field reviewattempt to be added to quiz
117 $table = new xmldb_table('quiz');
118 $field = new xmldb_field('reviewattempt');
119 $field->set_attributes(XMLDB_TYPE_INTEGER, '6', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'review');
120
121 // Launch add field reviewattempt
122 $dbman->add_field($table, $field);
123
124 // quiz savepoint reached
125 upgrade_plugin_savepoint(true, 2008000200, 'local', 'qedatabase');
126 }
127
128 if ($oldversion < 2008000201) {
129
130 // Define field reviewattempt to be added to quiz
131 $table = new xmldb_table('quiz');
132 $field = new xmldb_field('reviewcorrectness');
133 $field->set_attributes(XMLDB_TYPE_INTEGER, '6', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'reviewattempt');
134
135 // Launch add field reviewattempt
136 $dbman->add_field($table, $field);
137
138 // quiz savepoint reached
139 upgrade_plugin_savepoint(true, 2008000201, 'local', 'qedatabase');
140 }
141
142 if ($oldversion < 2008000202) {
143
144 // Define field reviewattempt to be added to quiz
145 $table = new xmldb_table('quiz');
146 $field = new xmldb_field('reviewmarks');
147 $field->set_attributes(XMLDB_TYPE_INTEGER, '6', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'reviewcorrectness');
148
149 // Launch add field reviewattempt
150 $dbman->add_field($table, $field);
151
152 // quiz savepoint reached
153 upgrade_plugin_savepoint(true, 2008000202, 'local', 'qedatabase');
154 }
155
156 if ($oldversion < 2008000203) {
157
158 // Define field reviewattempt to be added to quiz
159 $table = new xmldb_table('quiz');
160 $field = new xmldb_field('reviewspecificfeedback');
161 $field->set_attributes(XMLDB_TYPE_INTEGER, '6', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'reviewmarks');
162
163 // Launch add field reviewattempt
164 $dbman->add_field($table, $field);
165
166 // quiz savepoint reached
167 upgrade_plugin_savepoint(true, 2008000203, 'local', 'qedatabase');
168 }
169
170 if ($oldversion < 2008000204) {
171
172 // Define field reviewattempt to be added to quiz
173 $table = new xmldb_table('quiz');
174 $field = new xmldb_field('reviewgeneralfeedback');
175 $field->set_attributes(XMLDB_TYPE_INTEGER, '6', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'reviewspecificfeedback');
176
177 // Launch add field reviewattempt
178 $dbman->add_field($table, $field);
179
180 // quiz savepoint reached
181 upgrade_plugin_savepoint(true, 2008000204, 'local', 'qedatabase');
182 }
183
184 if ($oldversion < 2008000205) {
185
186 // Define field reviewattempt to be added to quiz
187 $table = new xmldb_table('quiz');
188 $field = new xmldb_field('reviewrightanswer');
189 $field->set_attributes(XMLDB_TYPE_INTEGER, '6', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'reviewgeneralfeedback');
190
191 // Launch add field reviewattempt
192 $dbman->add_field($table, $field);
193
194 // quiz savepoint reached
195 upgrade_plugin_savepoint(true, 2008000205, 'local', 'qedatabase');
196 }
197
198 if ($oldversion < 2008000206) {
199
200 // Define field reviewattempt to be added to quiz
201 $table = new xmldb_table('quiz');
202 $field = new xmldb_field('reviewoverallfeedback');
203 $field->set_attributes(XMLDB_TYPE_INTEGER, '6', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'reviewrightanswer');
204
205 // Launch add field reviewattempt
206 $dbman->add_field($table, $field);
207
208 // quiz savepoint reached
209 upgrade_plugin_savepoint(true, 2008000206, 'local', 'qedatabase');
210 }
211
212 define('QUIZ_NEW_DURING', 0x10000);
213 define('QUIZ_NEW_IMMEDIATELY_AFTER', 0x01000);
214 define('QUIZ_NEW_LATER_WHILE_OPEN', 0x00100);
215 define('QUIZ_NEW_AFTER_CLOSE', 0x00010);
216
217 define('QUIZ_OLD_IMMEDIATELY', 0x3c003f);
218 define('QUIZ_OLD_OPEN', 0x3c00fc0);
219 define('QUIZ_OLD_CLOSED', 0x3c03f000);
220
221 define('QUIZ_OLD_RESPONSES', 1*0x1041); // Show responses
222 define('QUIZ_OLD_SCORES', 2*0x1041); // Show scores
223 define('QUIZ_OLD_FEEDBACK', 4*0x1041); // Show question feedback
224 define('QUIZ_OLD_ANSWERS', 8*0x1041); // Show correct answers
225 define('QUIZ_OLD_SOLUTIONS', 16*0x1041); // Show solutions
226 define('QUIZ_OLD_GENERALFEEDBACK',32*0x1041); // Show question general feedback
227 define('QUIZ_OLD_OVERALLFEEDBACK', 1*0x4440000); // Show quiz overall feedback
228
229 // Copy the old review settings
230 if ($oldversion < 2008000210) {
231 $DB->execute("
232 UPDATE {quiz}
233 SET reviewattempt = " . $DB->sql_bitor($DB->sql_bitor(
234 QUIZ_NEW_DURING,
235 'CASE WHEN ' . $DB->sql_bitand('review', QUIZ_OLD_IMMEDIATELY & QUIZ_OLD_RESPONSES) .
236 ' <> 0 THEN ' . QUIZ_NEW_IMMEDIATELY_AFTER . ' ELSE 0 END'), $DB->sql_bitor(
237 'CASE WHEN ' . $DB->sql_bitand('review', QUIZ_OLD_OPEN & QUIZ_OLD_RESPONSES) .
238 ' <> 0 THEN ' . QUIZ_NEW_LATER_WHILE_OPEN . ' ELSE 0 END',
239 'CASE WHEN ' . $DB->sql_bitand('review', QUIZ_OLD_CLOSED & QUIZ_OLD_RESPONSES) .
240 ' <> 0 THEN ' . QUIZ_NEW_AFTER_CLOSE . ' ELSE 0 END')) . "
241 ");
242
243 // quiz savepoint reached
244 upgrade_plugin_savepoint(true, 2008000210, 'local', 'qedatabase');
245 }
246
247 if ($oldversion < 2008000211) {
248 $DB->execute("
249 UPDATE {quiz}
250 SET reviewcorrectness = " . $DB->sql_bitor($DB->sql_bitor(
251 QUIZ_NEW_DURING,
252 'CASE WHEN ' . $DB->sql_bitand('review', QUIZ_OLD_IMMEDIATELY & QUIZ_OLD_SCORES) .
253 ' <> 0 THEN ' . QUIZ_NEW_IMMEDIATELY_AFTER . ' ELSE 0 END'), $DB->sql_bitor(
254 'CASE WHEN ' . $DB->sql_bitand('review', QUIZ_OLD_OPEN & QUIZ_OLD_SCORES) .
255 ' <> 0 THEN ' . QUIZ_NEW_LATER_WHILE_OPEN . ' ELSE 0 END',
256 'CASE WHEN ' . $DB->sql_bitand('review', QUIZ_OLD_CLOSED & QUIZ_OLD_SCORES) .
257 ' <> 0 THEN ' . QUIZ_NEW_AFTER_CLOSE . ' ELSE 0 END')) . "
258 ");
259
260 // quiz savepoint reached
261 upgrade_plugin_savepoint(true, 2008000211, 'local', 'qedatabase');
262 }
263
264 if ($oldversion < 2008000212) {
265 $DB->execute("
266 UPDATE {quiz}
267 SET reviewmarks = " . $DB->sql_bitor($DB->sql_bitor(
268 QUIZ_NEW_DURING,
269 'CASE WHEN ' . $DB->sql_bitand('review', QUIZ_OLD_IMMEDIATELY & QUIZ_OLD_SCORES) .
270 ' <> 0 THEN ' . QUIZ_NEW_IMMEDIATELY_AFTER . ' ELSE 0 END'), $DB->sql_bitor(
271 'CASE WHEN ' . $DB->sql_bitand('review', QUIZ_OLD_OPEN & QUIZ_OLD_SCORES) .
272 ' <> 0 THEN ' . QUIZ_NEW_LATER_WHILE_OPEN . ' ELSE 0 END',
273 'CASE WHEN ' . $DB->sql_bitand('review', QUIZ_OLD_CLOSED & QUIZ_OLD_SCORES) .
274 ' <> 0 THEN ' . QUIZ_NEW_AFTER_CLOSE . ' ELSE 0 END')) . "
275 ");
276
277 // quiz savepoint reached
278 upgrade_plugin_savepoint(true, 2008000212, 'local', 'qedatabase');
279 }
280
281 if ($oldversion < 2008000213) {
282 $DB->execute("
283 UPDATE {quiz}
284 SET reviewspecificfeedback = " . $DB->sql_bitor($DB->sql_bitor(
285 'CASE WHEN ' . $DB->sql_bitand('review', QUIZ_OLD_IMMEDIATELY & QUIZ_OLD_FEEDBACK) .
286 ' <> 0 THEN ' . QUIZ_NEW_DURING . ' ELSE 0 END',
287 'CASE WHEN ' . $DB->sql_bitand('review', QUIZ_OLD_IMMEDIATELY & QUIZ_OLD_FEEDBACK) .
288 ' <> 0 THEN ' . QUIZ_NEW_IMMEDIATELY_AFTER . ' ELSE 0 END'), $DB->sql_bitor(
289 'CASE WHEN ' . $DB->sql_bitand('review', QUIZ_OLD_OPEN & QUIZ_OLD_FEEDBACK) .
290 ' <> 0 THEN ' . QUIZ_NEW_LATER_WHILE_OPEN . ' ELSE 0 END',
291 'CASE WHEN ' . $DB->sql_bitand('review', QUIZ_OLD_CLOSED & QUIZ_OLD_FEEDBACK) .
292 ' <> 0 THEN ' . QUIZ_NEW_AFTER_CLOSE . ' ELSE 0 END')) . "
293 ");
294
295 // quiz savepoint reached
296 upgrade_plugin_savepoint(true, 2008000213, 'local', 'qedatabase');
297 }
298
299 if ($oldversion < 2008000214) {
300 $DB->execute("
301 UPDATE {quiz}
302 SET reviewgeneralfeedback = " . $DB->sql_bitor($DB->sql_bitor(
303 'CASE WHEN ' . $DB->sql_bitand('review', QUIZ_OLD_IMMEDIATELY & QUIZ_OLD_GENERALFEEDBACK) .
304 ' <> 0 THEN ' . QUIZ_NEW_DURING . ' ELSE 0 END',
305 'CASE WHEN ' . $DB->sql_bitand('review', QUIZ_OLD_IMMEDIATELY & QUIZ_OLD_GENERALFEEDBACK) .
306 ' <> 0 THEN ' . QUIZ_NEW_IMMEDIATELY_AFTER . ' ELSE 0 END'), $DB->sql_bitor(
307 'CASE WHEN ' . $DB->sql_bitand('review', QUIZ_OLD_OPEN & QUIZ_OLD_GENERALFEEDBACK) .
308 ' <> 0 THEN ' . QUIZ_NEW_LATER_WHILE_OPEN . ' ELSE 0 END',
309 'CASE WHEN ' . $DB->sql_bitand('review', QUIZ_OLD_CLOSED & QUIZ_OLD_GENERALFEEDBACK) .
310 ' <> 0 THEN ' . QUIZ_NEW_AFTER_CLOSE . ' ELSE 0 END')) . "
311 ");
312
313 // quiz savepoint reached
314 upgrade_plugin_savepoint(true, 2008000214, 'local', 'qedatabase');
315 }
316
317 if ($oldversion < 2008000215) {
318 $DB->execute("
319 UPDATE {quiz}
320 SET reviewrightanswer = " . $DB->sql_bitor($DB->sql_bitor(
321 'CASE WHEN ' . $DB->sql_bitand('review', QUIZ_OLD_IMMEDIATELY & QUIZ_OLD_ANSWERS) .
322 ' <> 0 THEN ' . QUIZ_NEW_DURING . ' ELSE 0 END',
323 'CASE WHEN ' . $DB->sql_bitand('review', QUIZ_OLD_IMMEDIATELY & QUIZ_OLD_ANSWERS) .
324 ' <> 0 THEN ' . QUIZ_NEW_IMMEDIATELY_AFTER . ' ELSE 0 END'), $DB->sql_bitor(
325 'CASE WHEN ' . $DB->sql_bitand('review', QUIZ_OLD_OPEN & QUIZ_OLD_ANSWERS) .
326 ' <> 0 THEN ' . QUIZ_NEW_LATER_WHILE_OPEN . ' ELSE 0 END',
327 'CASE WHEN ' . $DB->sql_bitand('review', QUIZ_OLD_CLOSED & QUIZ_OLD_ANSWERS) .
328 ' <> 0 THEN ' . QUIZ_NEW_AFTER_CLOSE . ' ELSE 0 END')) . "
329 ");
330
331 // quiz savepoint reached
332 upgrade_plugin_savepoint(true, 2008000215, 'local', 'qedatabase');
333 }
334
335 if ($oldversion < 2008000216) {
336 $DB->execute("
337 UPDATE {quiz}
338 SET reviewoverallfeedback = " . $DB->sql_bitor($DB->sql_bitor(
339 0,
340 'CASE WHEN ' . $DB->sql_bitand('review', QUIZ_OLD_IMMEDIATELY & QUIZ_OLD_OVERALLFEEDBACK) .
341 ' <> 0 THEN ' . QUIZ_NEW_IMMEDIATELY_AFTER . ' ELSE 0 END'), $DB->sql_bitor(
342 'CASE WHEN ' . $DB->sql_bitand('review', QUIZ_OLD_OPEN & QUIZ_OLD_OVERALLFEEDBACK) .
343 ' <> 0 THEN ' . QUIZ_NEW_LATER_WHILE_OPEN . ' ELSE 0 END',
344 'CASE WHEN ' . $DB->sql_bitand('review', QUIZ_OLD_CLOSED & QUIZ_OLD_OVERALLFEEDBACK) .
345 ' <> 0 THEN ' . QUIZ_NEW_AFTER_CLOSE . ' ELSE 0 END')) . "
346 ");
347
348 // quiz savepoint reached
349 upgrade_plugin_savepoint(true, 2008000216, 'local', 'qedatabase');
350 }
351
352 // And, do the same for the defaults
353 if ($oldversion < 2008000217) {
354 if (empty($CFG->quiz_review)) {
355 $CFG->quiz_review = 0;
356 }
357
358 set_config('quiz_reviewattempt',
359 QUIZ_NEW_DURING |
360 ($CFG->quiz_review & QUIZ_OLD_IMMEDIATELY & QUIZ_OLD_RESPONSES ? QUIZ_NEW_IMMEDIATELY_AFTER : 0) |
361 ($CFG->quiz_review & QUIZ_OLD_OPEN & QUIZ_OLD_RESPONSES ? QUIZ_NEW_LATER_WHILE_OPEN : 0) |
362 ($CFG->quiz_review & QUIZ_OLD_CLOSED & QUIZ_OLD_RESPONSES ? QUIZ_NEW_AFTER_CLOSE : 0));
363
364 set_config('quiz_reviewcorrectness',
365 QUIZ_NEW_DURING |
366 ($CFG->quiz_review & QUIZ_OLD_IMMEDIATELY & QUIZ_OLD_SCORES ? QUIZ_NEW_IMMEDIATELY_AFTER : 0) |
367 ($CFG->quiz_review & QUIZ_OLD_OPEN & QUIZ_OLD_SCORES ? QUIZ_NEW_LATER_WHILE_OPEN : 0) |
368 ($CFG->quiz_review & QUIZ_OLD_CLOSED & QUIZ_OLD_SCORES ? QUIZ_NEW_AFTER_CLOSE : 0));
369
370 set_config('quiz_reviewmarks',
371 QUIZ_NEW_DURING |
372 ($CFG->quiz_review & QUIZ_OLD_IMMEDIATELY & QUIZ_OLD_SCORES ? QUIZ_NEW_IMMEDIATELY_AFTER : 0) |
373 ($CFG->quiz_review & QUIZ_OLD_OPEN & QUIZ_OLD_SCORES ? QUIZ_NEW_LATER_WHILE_OPEN : 0) |
374 ($CFG->quiz_review & QUIZ_OLD_CLOSED & QUIZ_OLD_SCORES ? QUIZ_NEW_AFTER_CLOSE : 0));
375
376 set_config('quiz_reviewspecificfeedback',
377 ($CFG->quiz_review & QUIZ_OLD_IMMEDIATELY & QUIZ_OLD_FEEDBACK ? QUIZ_NEW_DURING : 0) |
378 ($CFG->quiz_review & QUIZ_OLD_IMMEDIATELY & QUIZ_OLD_FEEDBACK ? QUIZ_NEW_IMMEDIATELY_AFTER : 0) |
379 ($CFG->quiz_review & QUIZ_OLD_OPEN & QUIZ_OLD_FEEDBACK ? QUIZ_NEW_LATER_WHILE_OPEN : 0) |
380 ($CFG->quiz_review & QUIZ_OLD_CLOSED & QUIZ_OLD_FEEDBACK ? QUIZ_NEW_AFTER_CLOSE : 0));
381
382 set_config('quiz_reviewgeneralfeedback',
383 ($CFG->quiz_review & QUIZ_OLD_IMMEDIATELY & QUIZ_OLD_GENERALFEEDBACK ? QUIZ_NEW_DURING : 0) |
384 ($CFG->quiz_review & QUIZ_OLD_IMMEDIATELY & QUIZ_OLD_GENERALFEEDBACK ? QUIZ_NEW_IMMEDIATELY_AFTER : 0) |
385 ($CFG->quiz_review & QUIZ_OLD_OPEN & QUIZ_OLD_GENERALFEEDBACK ? QUIZ_NEW_LATER_WHILE_OPEN : 0) |
386 ($CFG->quiz_review & QUIZ_OLD_CLOSED & QUIZ_OLD_GENERALFEEDBACK ? QUIZ_NEW_AFTER_CLOSE : 0));
387
388 set_config('quiz_reviewrightanswer',
389 ($CFG->quiz_review & QUIZ_OLD_IMMEDIATELY & QUIZ_OLD_ANSWERS ? QUIZ_NEW_DURING : 0) |
390 ($CFG->quiz_review & QUIZ_OLD_IMMEDIATELY & QUIZ_OLD_ANSWERS ? QUIZ_NEW_IMMEDIATELY_AFTER : 0) |
391 ($CFG->quiz_review & QUIZ_OLD_OPEN & QUIZ_OLD_ANSWERS ? QUIZ_NEW_LATER_WHILE_OPEN : 0) |
392 ($CFG->quiz_review & QUIZ_OLD_CLOSED & QUIZ_OLD_ANSWERS ? QUIZ_NEW_AFTER_CLOSE : 0));
393
394 set_config('quiz_reviewoverallfeedback',
395 0 |
396 ($CFG->quiz_review & QUIZ_OLD_IMMEDIATELY & QUIZ_OLD_OVERALLFEEDBACK ? QUIZ_NEW_IMMEDIATELY_AFTER : 0) |
397 ($CFG->quiz_review & QUIZ_OLD_OPEN & QUIZ_OLD_OVERALLFEEDBACK ? QUIZ_NEW_LATER_WHILE_OPEN : 0) |
398 ($CFG->quiz_review & QUIZ_OLD_CLOSED & QUIZ_OLD_OVERALLFEEDBACK ? QUIZ_NEW_AFTER_CLOSE : 0));
399
400 // quiz savepoint reached
401 upgrade_plugin_savepoint(true, 2008000217, 'local', 'qedatabase');
402 }
403
404 // Finally drop the old column
405 if ($oldversion < 2008000220) {
406 // Define field review to be dropped from quiz
407 $table = new xmldb_table('quiz');
408 $field = new xmldb_field('review');
409
410 // Launch drop field review
411 $dbman->drop_field($table, $field);
412
413 // quiz savepoint reached
414 upgrade_plugin_savepoint(true, 2008000220, 'local', 'qedatabase');
415 }
416
417 if ($oldversion < 2008000221) {
418 unset_config('quiz_review');
419
420 // quiz savepoint reached
421 upgrade_plugin_savepoint(true, 2008000221, 'local', 'qedatabase');
422 }
423
424 if ($oldversion < 2008000501) {
425
426 // Rename field defaultgrade on table question to defaultmark
427 $table = new xmldb_table('question');
428 $field = new xmldb_field('defaultgrade');
429 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1', 'generalfeedback');
430
431 // Launch rename field defaultmark
432 $dbman->rename_field($table, $field, 'defaultmark');
433
434 // quiz savepoint reached
435 upgrade_plugin_savepoint(true, 2008000501, 'local', 'qedatabase');
436 }
437
438 if ($oldversion < 2008000505) {
439
440 // Rename the question_attempts table to question_usages.
441 $table = new xmldb_table('question_attempts');
442 if (table_exists($table)) {
443 $dbman->rename_table($table, 'question_usages');
444 }
445
446 // quiz savepoint reached
447 upgrade_plugin_savepoint(true, 2008000505, 'local', 'qedatabase');
448 }
449
450 if ($oldversion < 2008000507) {
451
452 // Rename the modulename field to component ...
453 $table = new xmldb_table('question_usages');
454 $field = new xmldb_field('modulename');
455 $field->set_attributes(XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'contextid');
456 $dbman->rename_field($table, $field, 'component');
457
458 // ... and update its contents.
459 $DB->set_field('question_usages', 'component', 'mod_quiz', array('component' => 'quiz'));
460
461 // Add the contextid field.
462 $field = new xmldb_field('contextid');
463 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'id');
464 $dbman->add_field($table, $field);
465
466 // And populate it.
467 $quizmoduleid = $DB->get_field('modules', 'id', array('name' => 'quiz'));
468 $DB->execute("
469 UPDATE {question_usages} SET contextid = (
470 SELECT ctx.id
471 FROM {context} ctx
472 JOIN {course_modules} cm ON cm.id = ctx.instanceid AND cm.module = $quizmoduleid
473 JOIN {quiz_attempts} quiza ON quiza.quiz = cm.instance
474 WHERE ctx.contextlevel = " . CONTEXT_MODULE . "
475 AND quiza.uniqueid = {question_usages}.id
476 )
477 ");
478
479 // Then make it NOT NULL.
480 $field = new xmldb_field('contextid');
481 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'id');
482 $dbman->change_field_notnull($table, $field);
483
484 // Add the preferredbehaviour column. Populate it with a dummy value
485 // for now. We will fill in the appropriate behaviour name when
486 // updating all the rest of the attempt data.
487 $field = new xmldb_field('preferredbehaviour');
488 $field->set_attributes(XMLDB_TYPE_CHAR, '32', null, null, null, null, null, 'to_be_set_later', 'component');
489 $dbman->add_field($table, $field);
490
491 // Then remove the default value, now the column is populated.
492 $field = new xmldb_field('preferredbehaviour');
493 $field->set_attributes(XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null, 'component');
494 $dbman->change_field_default($table, $field);
495
496 // quiz savepoint reached
497 upgrade_plugin_savepoint(true, 2008000507, 'local', 'qedatabase');
498 }
499
500 if ($oldversion < 2008000513) {
501
502 // Define key contextid (foreign) to be added to question_usages
503 $table = new xmldb_table('question_usages');
504 $key = new XMLDBKey('contextid');
505 $key->set_attributes(XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
506
507 // Launch add key contextid
508 $dbman->add_key($table, $key);
509
510 // quiz savepoint reached
511 upgrade_plugin_savepoint(true, 2008000513, 'local', 'qedatabase');
512 }
513
514 if ($oldversion < 2008000514) {
515
516 // Changing precision of field component on table question_usages to (255)
517 // This was missed during the upgrade from old versions.
518 $table = new xmldb_table('question_usages');
519 $field = new xmldb_field('component');
520 $field->set_attributes(XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'contextid');
521
522 // Launch change of precision for field component
523 $dbman->change_field_precision($table, $field);
524
525 // quiz savepoint reached
526 upgrade_plugin_savepoint(true, 2008000514, 'local', 'qedatabase');
527 }
528
529 if ($oldversion < 2008000520) {
530
531 // Define table question_attempts to be created
532 $table = new xmldb_table('question_attempts');
533 if (!table_exists($table)) {
534
535 // Adding fields to table question_attempts
536 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
537 $table->add_field('questionusageid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
538 $table->add_field('slot', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
539 $table->add_field('behaviour', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
540 $table->add_field('questionid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
541 $table->add_field('maxmark', XMLDB_TYPE_NUMBER, '12, 7', null, XMLDB_NOTNULL, null, null);
542 $table->add_field('minfraction', XMLDB_TYPE_NUMBER, '12, 7', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
543 $table->add_field('flagged', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
544 $table->add_field('questionsummary', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
545 $table->add_field('rightanswer', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
546 $table->add_field('responsesummary', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
547 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
548
549 // Adding keys to table question_attempts
550 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
551 $table->add_key('questionid', XMLDB_KEY_FOREIGN, array('questionid'), 'question', array('id'));
552 $table->add_key('questionusageid', XMLDB_KEY_FOREIGN, array('questionusageid'), 'question_usages', array('id'));
553
554 // Adding indexes to table question_attempts
555 $table->add_index('questionusageid-slot', XMLDB_INDEX_UNIQUE, array('questionusageid', 'slot'));
556
557 // Launch create table for question_attempts
558 $dbman->create_table($table);
559 }
560
561 // quiz savepoint reached
562 upgrade_plugin_savepoint(true, 2008000520, 'local', 'qedatabase');
563 }
564
565 if ($oldversion < 2008000521) {
566
567 // Define table question_attempt_steps to be created
568 $table = new xmldb_table('question_attempt_steps');
569 if (!table_exists($table)) {
570
571 // Adding fields to table question_attempt_steps
572 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
573 $table->add_field('questionattemptid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
574 $table->add_field('sequencenumber', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
575 $table->add_field('state', XMLDB_TYPE_CHAR, '13', null, XMLDB_NOTNULL, null, null);
576 $table->add_field('fraction', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null);
577 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
578 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
579
580 // Adding keys to table question_attempt_steps
581 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
582 $table->add_key('questionattemptid', XMLDB_KEY_FOREIGN, array('questionattemptid'), 'question_attempts_new', array('id'));
583 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
584
585 // Adding indexes to table question_attempt_steps
586 $table->add_index('questionattemptid-sequencenumber', XMLDB_INDEX_UNIQUE, array('questionattemptid', 'sequencenumber'));
587
588 // Launch create table for question_attempt_steps
589 $dbman->create_table($table);
590 }
591
592 // quiz savepoint reached
593 upgrade_plugin_savepoint(true, 2008000521, 'local', 'qedatabase');
594 }
595
596 if ($oldversion < 2008000522) {
597
598 // Define table question_attempt_step_data to be created
599 $table = new xmldb_table('question_attempt_step_data');
600 if (!table_exists($table)) {
601
602 // Adding fields to table question_attempt_step_data
603 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
604 $table->add_field('attemptstepid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
605 $table->add_field('name', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
606 $table->add_field('value', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
607
608 // Adding keys to table question_attempt_step_data
609 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
610 $table->add_key('attemptstepid', XMLDB_KEY_FOREIGN, array('attemptstepid'), 'question_attempt_steps', array('id'));
611
612 // Adding indexes to table question_attempt_step_data
613 $table->add_index('attemptstepid-name', XMLDB_INDEX_UNIQUE, array('attemptstepid', 'name'));
614
615 // Launch create table for question_attempt_step_data
616 $dbman->create_table($table);
617 }
618
619 // quiz savepoint reached
620 upgrade_plugin_savepoint(true, 2008000522, 'local', 'qedatabase');
621 }
622
623// if ($oldversion < 2008000550) {
624// // Define field needsupgradetonewqe to be added to quiz_attempts
625// $table = new xmldb_table('quiz_attempts');
626// $field = new xmldb_field('needsupgradetonewqe');
627// $field->set_attributes(XMLDB_TYPE_INTEGER, '3', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'preview');
628//
629// // Launch add field needsupgradetonewqe
630// if (!field_exists($table, $field)) {
631// $dbman->add_field($table, $field);
632// }
633//
634// set_field_select('quiz_attempts', 'needsupgradetonewqe', 1, '', '');
635//
636// // quiz savepoint reached
637// upgrade_plugin_savepoint(true, 2008000550, 'local', 'qedatabase');
638// }
639
640// if ($oldversion < 2008000551) {
641// $table = new xmldb_table('question_states');
642// if (table_exists($table)) {
643// // First delete all data from preview attempts.
644// delete_records_select('question_states',
645// "attempt IN (SELECT uniqueid FROM {quiz_attempts} WHERE preview = 1)");
646// delete_records_select('question_sessions',
647// "attemptid IN (SELECT uniqueid FROM {quiz_attempts} WHERE preview = 1)");
648// delete_records('quiz_attempts', 'preview', 1);
649//
650// // Now update all the old attempt data.
651// $db->debug = false;
652// $oldrcachesetting = $CFG->rcache;
653// $CFG->rcache = false;
654//
655// require_once($CFG->dirroot . '/question/engine/upgradefromoldqe/upgrade.php');
656// $upgrader = new question_engine_attempt_upgrader();
657// $upgrader->convert_all_quiz_attempts();
658//
659// $CFG->rcache = $oldrcachesetting;
660// $db->debug = true;
661// }
662//
663// // quiz savepoint reached
664// upgrade_plugin_savepoint(true, 2008000551, 'local', 'qedatabase');
665// }
666
667 if ($oldversion < 2008000600) {
668
669 // Define table question_hints to be created
670 $table = new xmldb_table('question_hints');
671
672 // Adding fields to table question_hints
673 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
674 $table->add_field('questionid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
675 $table->add_field('hint', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null);
676 $table->add_field('shownumcorrect', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null);
677 $table->add_field('clearwrong', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null);
678 $table->add_field('options', XMLDB_TYPE_CHAR, '255', null, null, null, null);
679
680 // Adding keys to table question_hints
681 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
682 $table->add_key('questionid', XMLDB_KEY_FOREIGN, array('questionid'), 'question', array('id'));
683
684 // Conditionally launch create table for question_hints
685 if (!$dbman->table_exists($table)) {
686 $dbman->create_table($table);
687 }
688
689 // quiz savepoint reached
690 upgrade_plugin_savepoint(true, 2008000600, 'local', 'qedatabase');
691 }
692
693 if ($oldversion < 2008000601) {
694
695 // In the past, question_answer fractions were stored with rather
696 // sloppy rounding. Now update them to the new standard of 7 d.p.
697 $changes = array(
698 '-0.66666' => '-0.6666667',
699 '-0.33333' => '-0.3333333',
700 '-0.16666' => '-0.1666667',
701 '-0.142857' => '-0.1428571',
702 '0.11111' => '0.1111111',
703 '0.142857' => '0.1428571',
704 '0.16666' => '0.1666667',
705 '0.33333' => '0.3333333',
706 '0.333333' => '0.3333333',
707 '0.66666' => '0.6666667',
708 );
709 foreach ($changes as $from => $to) {
710 $DB->set_field('question_answers',
711 'fraction', $to, array('fraction' => $from));
712 }
713
714 // quiz savepoint reached
715 upgrade_plugin_savepoint(true, 2008000601, 'local', 'qedatabase');
716 }
717
718 if ($oldversion < 2008000602) {
719
720 // In the past, question penalties were stored with rather
721 // sloppy rounding. Now update them to the new standard of 7 d.p.
722 $DB->set_field('question',
723 'penalty', 0.3333333, array('penalty' => 33.3));
724 $DB->set_field_select('question',
725 'penalty', 0.3333333, 'penalty >= 0.33 AND penalty <= 0.34');
726 $DB->set_field_select('question',
727 'penalty', 0.6666667, 'penalty >= 0.66 AND penalty <= 0.67');
728 $DB->set_field_select('question',
729 'penalty', 1, 'penalty > 1');
730
731 // quiz savepoint reached
732 upgrade_plugin_savepoint(true, 2008000602, 'local', 'qedatabase');
733 }
734}