quiz MDL-24727 column missed from install.xml
[moodle.git] / mod / quiz / db / upgrade.php
CommitLineData
83192608 1<?php
b8a342d7 2
271e6dec 3// This file keeps track of upgrades to
b8a342d7 4// the quiz module
5//
6// Sometimes, changes between versions involve
7// alterations to database structures and other
8// major things that may break installations.
9//
10// The upgrade function in this file will attempt
11// to perform all the necessary actions to upgrade
2e0406a5 12// your older installation to the current version.
b8a342d7 13//
14// If there's something it cannot do itself, it
15// will tell you what you need to do.
16//
17// The commands in here will all be database-neutral,
b1f93b15 18// using the methods of database_manager class
775f811a 19//
20// Please do not forget to use upgrade_set_timeout()
21// before any action that may take longer time to finish.
b8a342d7 22
775f811a 23function xmldb_quiz_upgrade($oldversion) {
24 global $CFG, $DB;
84e628a0 25
f37f30d4 26 $dbman = $DB->get_manager();
b8a342d7 27
219f652b 28//===== 1.9.0 upgrade line ======//
271e6dec 29
a4cdd6d2 30 if ($oldversion < 2008062000) {
f37f30d4 31
32 /// Define table quiz_report to be created
33 $table = new xmldb_table('quiz_report');
34
35 /// Adding fields to table quiz_report
2a88f626 36 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
37 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, null, null, null);
38 $table->add_field('displayorder', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
f37f30d4 39
40 /// Adding keys to table quiz_report
41 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
42
43 /// Conditionally launch create table for quiz_report
44 if (!$dbman->table_exists($table)) {
eee5d9bb 45 $dbman->create_table($table);
f37f30d4 46 }
47
a4cdd6d2 48 upgrade_mod_savepoint(true, 2008062000, 'quiz');
f37f30d4 49 }
04264aed 50
a4cdd6d2 51 if ($oldversion < 2008062001) {
39790bd8 52 $reporttoinsert = new stdClass();
f37f30d4 53 $reporttoinsert->name = 'overview';
54 $reporttoinsert->displayorder = 10000;
a4cdd6d2 55 $DB->insert_record('quiz_report', $reporttoinsert);
f37f30d4 56
39790bd8 57 $reporttoinsert = new stdClass();
f37f30d4 58 $reporttoinsert->name = 'responses';
59 $reporttoinsert->displayorder = 9000;
a4cdd6d2 60 $DB->insert_record('quiz_report', $reporttoinsert);
f37f30d4 61
39790bd8 62 $reporttoinsert = new stdClass();
f37f30d4 63 $reporttoinsert->name = 'regrade';
64 $reporttoinsert->displayorder = 7000;
a4cdd6d2 65 $DB->insert_record('quiz_report', $reporttoinsert);
f37f30d4 66
39790bd8 67 $reporttoinsert = new stdClass();
f37f30d4 68 $reporttoinsert->name = 'grading';
69 $reporttoinsert->displayorder = 6000;
a4cdd6d2 70 $DB->insert_record('quiz_report', $reporttoinsert);
04264aed 71
a4cdd6d2 72 upgrade_mod_savepoint(true, 2008062001, 'quiz');
f37f30d4 73 }
84e628a0 74
a4cdd6d2 75 if ($oldversion < 2008072402) {
17f1782c 76
77 /// Define field lastcron to be added to quiz_report
78 $table = new xmldb_table('quiz_report');
2a88f626 79 $field = new xmldb_field('lastcron', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'displayorder');
17f1782c 80
81 /// Conditionally launch add field lastcron
82 if (!$dbman->field_exists($table, $field)) {
83 $dbman->add_field($table, $field);
84 }
85
86 /// Define field cron to be added to quiz_report
2a88f626 87 $field = new xmldb_field('cron', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'lastcron');
17f1782c 88
89 /// Conditionally launch add field cron
90 if (!$dbman->field_exists($table, $field)) {
91 $dbman->add_field($table, $field);
92 }
93
94 /// quiz savepoint reached
a4cdd6d2 95 upgrade_mod_savepoint(true, 2008072402, 'quiz');
17f1782c 96 }
f37f30d4 97
a4cdd6d2 98 if ($oldversion < 2008072900) {
4d9f2c36 99 /// Delete the regrade report - it is now part of the overview report.
a4cdd6d2 100 $DB->delete_records('quiz_report', array('name' => 'regrade'));
4d9f2c36 101
102 /// quiz savepoint reached
a4cdd6d2 103 upgrade_mod_savepoint(true, 2008072900, 'quiz');
4d9f2c36 104 }
f37f30d4 105
a4cdd6d2 106 if ($oldversion < 2008081500) {
6b224376 107 /// Define table quiz_question_versions to be dropped
108 $table = new xmldb_table('quiz_question_versions');
109
110 /// Launch drop table for quiz_question_versions
111 $dbman->drop_table($table);
112
113 /// quiz savepoint reached
a4cdd6d2 114 upgrade_mod_savepoint(true, 2008081500, 'quiz');
6b224376 115 }
116
f9a2cf86 117 /// Changing the type of all the columns that store grades to be NUMBER(10, 5) or similar.
a4cdd6d2 118 if ($oldversion < 2008081501) {
f9a2cf86 119 $table = new xmldb_table('quiz');
2a88f626 120 $field = new xmldb_field('sumgrades', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, 'questions');
f9a2cf86 121 $dbman->change_field_type($table, $field);
a4cdd6d2 122 upgrade_mod_savepoint(true, 2008081501, 'quiz');
f9a2cf86 123 }
124
a4cdd6d2 125 if ($oldversion < 2008081502) {
f9a2cf86 126 $table = new xmldb_table('quiz');
2a88f626 127 $field = new xmldb_field('grade', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, 'sumgrades');
f9a2cf86 128 $dbman->change_field_type($table, $field);
a4cdd6d2 129 upgrade_mod_savepoint(true, 2008081502, 'quiz');
f9a2cf86 130 }
131
a4cdd6d2 132 if ($oldversion < 2008081503) {
f9a2cf86 133 $table = new xmldb_table('quiz_attempts');
2a88f626 134 $field = new xmldb_field('sumgrades', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, 'attempt');
f9a2cf86 135 $dbman->change_field_type($table, $field);
a4cdd6d2 136 upgrade_mod_savepoint(true, 2008081503, 'quiz');
f9a2cf86 137 }
138
a4cdd6d2 139 if ($oldversion < 2008081504) {
f9a2cf86 140 $table = new xmldb_table('quiz_feedback');
2a88f626 141 $field = new xmldb_field('mingrade', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, 'feedbacktext');
f9a2cf86 142 $dbman->change_field_type($table, $field);
a4cdd6d2 143 upgrade_mod_savepoint(true, 2008081504, 'quiz');
f9a2cf86 144 }
145
a4cdd6d2 146 if ($oldversion < 2008081505) {
f9a2cf86 147 $table = new xmldb_table('quiz_feedback');
2a88f626 148 $field = new xmldb_field('maxgrade', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, 'mingrade');
f9a2cf86 149 $dbman->change_field_type($table, $field);
a4cdd6d2 150 upgrade_mod_savepoint(true, 2008081505, 'quiz');
f9a2cf86 151 }
152
a4cdd6d2 153 if ($oldversion < 2008081506) {
f9a2cf86 154 $table = new xmldb_table('quiz_grades');
2a88f626 155 $field = new xmldb_field('grade', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, 'userid');
f9a2cf86 156 $dbman->change_field_type($table, $field);
a4cdd6d2 157 upgrade_mod_savepoint(true, 2008081506, 'quiz');
f9a2cf86 158 }
159
a4cdd6d2 160 if ($oldversion < 2008081507) {
f9a2cf86 161 $table = new xmldb_table('quiz_question_instances');
2a88f626 162 $field = new xmldb_field('grade', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'question');
f9a2cf86 163 $dbman->change_field_type($table, $field);
a4cdd6d2 164 upgrade_mod_savepoint(true, 2008081507, 'quiz');
f9a2cf86 165 }
166
e2249afe 167 /// Move all of the quiz config settings from $CFG to the config_plugins table.
a4cdd6d2 168 if ($oldversion < 2008082200) {
e2249afe 169 foreach (get_object_vars($CFG) as $name => $value) {
170 if (strpos($name, 'quiz_') === 0) {
171 $shortname = substr($name, 5);
172 if ($shortname == 'fix_adaptive') {
173 // Special case - remove old inconsistency.
174 $shortname == 'fix_optionflags';
175 }
a4cdd6d2
PS
176 set_config($shortname, $value, 'quiz');
177 unset_config($name);
e2249afe 178 }
179 }
a4cdd6d2 180 upgrade_mod_savepoint(true, 2008082200, 'quiz');
e2249afe 181 }
182
e4686e89 183 /// Now that the quiz is no longer responsible for creating all the question
184 /// bank tables, and some of the tables are now the responsibility of the
185 /// datasetdependent question type, which did not have a version.php file before,
186 /// we need to say that these tables are already installed, otherwise XMLDB
187 /// will try to create them again and give an error.
a4cdd6d2 188 if ($oldversion < 2008082600) {
98e82289 189 // Since MDL-16505 was fixed, and we eliminated the datasetdependent
190 // question type, this is now a no-op.
a4cdd6d2 191 upgrade_mod_savepoint(true, 2008082600, 'quiz');
e4686e89 192 }
84e628a0 193
a4cdd6d2 194 if ($oldversion < 2008112101) {
bbf4f440 195
196 /// Define field lastcron to be added to quiz_report
197 $table = new xmldb_table('quiz_report');
2a88f626 198 $field = new xmldb_field('capability', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'cron');
bbf4f440 199
200 /// Conditionally launch add field lastcron
201 if (!$dbman->field_exists($table, $field)) {
202 $dbman->add_field($table, $field);
203 }
84e628a0 204
bbf4f440 205 /// quiz savepoint reached
a4cdd6d2 206 upgrade_mod_savepoint(true, 2008112101, 'quiz');
bbf4f440 207 }
e4686e89 208
a4cdd6d2 209 if ($oldversion < 2009010700) {
a733c4b9 210
211 /// Define field showuserpicture to be added to quiz
212 $table = new xmldb_table('quiz');
2a88f626 213 $field = new xmldb_field('showuserpicture', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0', 'delay2');
a733c4b9 214
215 /// Conditionally launch add field showuserpicture
216 if (!$dbman->field_exists($table, $field)) {
217 $dbman->add_field($table, $field);
218 }
219
220 /// quiz savepoint reached
a4cdd6d2 221 upgrade_mod_savepoint(true, 2009010700, 'quiz');
a733c4b9 222 }
223
a4cdd6d2 224 if ($oldversion < 2009030900) {
84e628a0 225 /// If there are no quiz settings set to advanced yet, the set up the default
226 /// advanced fields from Moodle 2.0.
227 $quizconfig = get_config('quiz');
228 $arealreadyadvanced = false;
229 foreach (array($quizconfig) as $name => $value) {
230 if (strpos($name, 'fix_') === 0 && !empty($value)) {
231 $arealreadyadvanced = true;
232 break;
233 }
234 }
235
236 if (!$arealreadyadvanced) {
237 set_config('fix_penaltyscheme', 1, 'quiz');
238 set_config('fix_attemptonlast', 1, 'quiz');
239 set_config('fix_questiondecimalpoints', 1, 'quiz');
240 set_config('fix_password', 1, 'quiz');
241 set_config('fix_subnet', 1, 'quiz');
242 set_config('fix_delay1', 1, 'quiz');
243 set_config('fix_delay2', 1, 'quiz');
244 set_config('fix_popup', 1, 'quiz');
245 }
246
247 /// quiz savepoint reached
a4cdd6d2 248 upgrade_mod_savepoint(true, 2009030900, 'quiz');
84e628a0 249 }
250
a4cdd6d2 251 if ($oldversion < 2009031000) {
84e628a0 252 /// Add new questiondecimaldigits setting, separate form the overall decimaldigits one.
253 $table = new xmldb_table('quiz');
2a88f626 254 $field = new xmldb_field('questiondecimalpoints', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '2', 'decimalpoints');
84e628a0 255 if (!$dbman->field_exists($table, $field)) {
256 $dbman->add_field($table, $field);
257 }
258
259 /// quiz savepoint reached
a4cdd6d2 260 upgrade_mod_savepoint(true, 2009031000, 'quiz');
84e628a0 261 }
262
a4cdd6d2 263 if ($oldversion < 2009031001) {
84e628a0 264 /// Convert quiz.timelimit from minutes to seconds.
265 $DB->execute('UPDATE {quiz} SET timelimit = timelimit * 60');
266 $default = get_config('quiz', 'timelimit');
267 set_config('timelimit', 60 * $default, 'quiz');
268
269 /// quiz savepoint reached
a4cdd6d2 270 upgrade_mod_savepoint(true, 2009031001, 'quiz');
84e628a0 271 }
272
a4cdd6d2 273 if ($oldversion < 2009042000) {
18b5df91 274
275 /// Define field introformat to be added to quiz
276 $table = new xmldb_table('quiz');
2a88f626 277 $field = new xmldb_field('introformat', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'intro');
18b5df91 278
07cb6678
PS
279 if (!$dbman->field_exists($table, $field)) {
280 $dbman->add_field($table, $field);
281 }
18b5df91 282
07cb6678
PS
283 // conditionally migrate to html format in intro
284 if ($CFG->texteditors !== 'textarea') {
285 $rs = $DB->get_recordset('quiz', array('introformat'=>FORMAT_MOODLE), '', 'id,intro,introformat');
286 foreach ($rs as $q) {
287 $q->intro = text_to_html($q->intro, false, false, true);
288 $q->introformat = FORMAT_HTML;
289 $DB->update_record('quiz', $q);
290 upgrade_set_timeout();
291 }
292 $rs->close();
293 }
18b5df91 294
295 /// quiz savepoint reached
a4cdd6d2 296 upgrade_mod_savepoint(true, 2009042000, 'quiz');
18b5df91 297 }
298
a4cdd6d2 299 if ($oldversion < 2010030501) {
990650f9
TH
300 /// Define table quiz_overrides to be created
301 $table = new xmldb_table('quiz_overrides');
302
303 /// Adding fields to table quiz_overrides
304 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
305 $table->add_field('quiz', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
306 $table->add_field('groupid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
307 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
308 $table->add_field('timeopen', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
309 $table->add_field('timeclose', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
310 $table->add_field('timelimit', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
311 $table->add_field('attempts', XMLDB_TYPE_INTEGER, '6', XMLDB_UNSIGNED, null, null, null);
312 $table->add_field('password', XMLDB_TYPE_CHAR, '255', null, null, null, null);
313
314 /// Adding keys to table quiz_overrides
315 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
316 $table->add_key('quiz', XMLDB_KEY_FOREIGN, array('quiz'), 'quiz', array('id'));
317 $table->add_key('groupid', XMLDB_KEY_FOREIGN, array('groupid'), 'groups', array('id'));
318 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
319
320 /// Conditionally launch create table for quiz_overrides
321 if (!$dbman->table_exists($table)) {
322 $dbman->create_table($table);
323 }
324
325 /// quiz savepoint reached
a4cdd6d2 326 upgrade_mod_savepoint(true, 2010030501, 'quiz');
990650f9
TH
327 }
328
a4cdd6d2 329 if ($oldversion < 2010051800) {
56ed242b 330
9bb6663e 331 // Define field showblocks to be added to quiz
56ed242b
SH
332 $table = new xmldb_table('quiz');
333 $field = new xmldb_field('showblocks', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0', 'showuserpicture');
334
9bb6663e 335 // Conditionally launch add field showblocks
56ed242b
SH
336 if (!$dbman->field_exists($table, $field)) {
337 $dbman->add_field($table, $field);
338 }
339
9bb6663e 340 // quiz savepoint reached
a4cdd6d2 341 upgrade_mod_savepoint(true, 2010051800, 'quiz');
56ed242b
SH
342 }
343
fe6ce234
DC
344 if ($oldversion < 2010080600) {
345
346 // Define field feedbacktextformat to be added to quiz_feedback
347 $table = new xmldb_table('quiz_feedback');
348 $field = new xmldb_field('feedbacktextformat', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'feedbacktext');
349
350 // Conditionally launch add field feedbacktextformat
351 if (!$dbman->field_exists($table, $field)) {
352 $dbman->add_field($table, $field);
353 }
354
355 // quiz savepoint reached
356 upgrade_mod_savepoint(true, 2010080600, 'quiz');
357 }
358
9bb6663e
TH
359 if ($oldversion < 2010102000) {
360
361 // Define field showblocks to be added to quiz
362 // Repeat this step, because the column was missing from install.xml for a time.
363 $table = new xmldb_table('quiz');
364 $field = new xmldb_field('showblocks', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0', 'showuserpicture');
365
366 // Conditionally launch add field showblocks
367 if (!$dbman->field_exists($table, $field)) {
368 $dbman->add_field($table, $field);
369 }
370
371 // quiz savepoint reached
372 upgrade_mod_savepoint(true, 2010102000, 'quiz');
373 }
fe6ce234 374
a4cdd6d2 375 return true;
b8a342d7 376}
377