Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / mod / quiz / db / postgres7.php
blob8e9ead52c88e7eead143019ba87b31fa7df69202
1 <?php // $Id$
3 // THIS FILE IS DEPRECATED! PLEASE DO NOT MAKE CHANGES TO IT!
4 //
5 // IT IS USED ONLY FOR UPGRADES FROM BEFORE MOODLE 1.7, ALL
6 // LATER CHANGES SHOULD USE upgrade.php IN THIS DIRECTORY.
8 function quiz_upgrade($oldversion) {
9 // This function does anything necessary to upgrade
10 // older versions to match current functionality
12 global $CFG, $db;
13 $success = true;
15 include_once("$CFG->dirroot/mod/quiz/locallib.php");
17 if ($success && $oldversion < 2003010100) {
18 $success = $success && execute_sql(" ALTER TABLE {$CFG->prefix}quiz ADD review integer DEFAULT '0' NOT NULL AFTER `grademethod` ");
21 if ($success && $oldversion < 2003010301) {
22 $success = $success && table_column("quiz_truefalse", "true", "trueanswer", "INTEGER", "10", "UNSIGNED", "0", "NOT NULL", "");
23 $success = $success && table_column("quiz_truefalse", "false", "falseanswer", "INTEGER", "10", "UNSIGNED", "0", "NOT NULL", "");
24 $success = $success && table_column("quiz_questions", "type", "qtype", "INTEGER", "10", "UNSIGNED", "0", "NOT NULL", "");
27 if ($success && $oldversion < 2003022303) {
28 $success = $success && modify_database ("", "CREATE TABLE prefix_quiz_randommatch (
29 id SERIAL PRIMARY KEY,
30 question integer NOT NULL default '0',
31 choose integer NOT NULL default '4'
32 );");
34 if ($success && $oldversion < 2003030303) {
35 $success = $success && table_column("quiz_questions", "", "defaultgrade", "INTEGER", "6", "UNSIGNED", "1", "NOT NULL", "image");
38 if ($success && $oldversion < 2003033100) {
39 $success = $success && modify_database ("", "ALTER TABLE prefix_quiz_randommatch RENAME prefix_quiz_randomsamatch ");
40 $success = $success && modify_database ("", "CREATE TABLE prefix_quiz_match_sub (
41 id SERIAL PRIMARY KEY,
42 question integer NOT NULL default '0',
43 questiontext text NOT NULL default '',
44 answertext varchar(255) NOT NULL default ''
45 );");
46 $success = $success && modify_database ("", "CREATE INDEX prefix_quiz_match_sub_question_idx ON prefix_quiz_match_sub (question);");
48 $success = $success && modify_database ("", "CREATE TABLE prefix_quiz_multichoice (
49 id SERIAL PRIMARY KEY,
50 question integer NOT NULL default '0',
51 layout integer NOT NULL default '0',
52 answers varchar(255) NOT NULL default '',
53 single integer NOT NULL default '0'
54 );");
55 $success = $success && modify_database ("", "CREATE INDEX prefix_quiz_multichoice_question_idx ON prefix_quiz_multichoice (question);");
58 if ($success && $oldversion < 2003040901) {
59 $success = $success && table_column("quiz", "", "shufflequestions", "INTEGER", "5", "UNSIGNED", "0", "NOT NULL", "review");
60 $success = $success && table_column("quiz", "", "shuffleanswers", "INTEGER", "4", "UNSIGNED", "0", "NOT NULL", "shufflequestions");
62 if ($success && $oldversion < 2003042702) {
63 $success = $success && modify_database ("", "CREATE TABLE prefix_quiz_match (
64 id SERIAL PRIMARY KEY,
65 question integer NOT NULL default '0',
66 subquestions varchar(255) NOT NULL default ''
67 );");
68 $success = $success && modify_database ("", "CREATE INDEX prefix_quiz_match_question_idx ON prefix_quiz_match (question);");
70 if ($success && $oldversion < 2003071001) {
71 $success = $success && modify_database ("", " CREATE TABLE prefix_quiz_numerical (
72 id SERIAL PRIMARY KEY,
73 question integer NOT NULL default '0',
74 answer integer NOT NULL default '0',
75 min varchar(255) NOT NULL default '',
76 max varchar(255) NOT NULL default ''
77 ); ");
78 $success = $success && modify_database ("", "CREATE INDEX prefix_quiz_numerical_answer_idx ON prefix_quiz_numerical (answer);");
81 if ($success && $oldversion < 2003072400) {
82 $success = $success && execute_sql(" INSERT INTO {$CFG->prefix}log_display (module, action, mtable, field) VALUES ('quiz', 'review', 'quiz', 'name') ");
85 if ($success && $oldversion < 2003082300) {
86 $success = $success && modify_database ("", " CREATE TABLE prefix_quiz_multianswers (
87 id SERIAL PRIMARY KEY,
88 question integer NOT NULL default '0',
89 answers varchar(255) NOT NULL default '',
90 positionkey varchar(255) NOT NULL default '',
91 answertype integer NOT NULL default '0',
92 norm integer NOT NULL default '1'
93 ); ");
94 $success = $success && modify_database ("", "CREATE INDEX prefix_quiz_multianswers_question_idx ON prefix_quiz_multianswers (question);");
96 $success = $success && table_column("quiz", "", "attemptonlast", "INTEGER", "10", "UNSIGNED", "0", "NOT NULL", "attempts");
98 $success = $success && table_column("quiz_questions", "", "stamp", "varchar", "255", "", "qtype");
101 if ($success && $oldversion < 2003082301) {
102 $success = $success && table_column("quiz_questions", "", "version", "integer", "10", "", "1", "not null", "stamp");
103 if ($questions = get_records("quiz_questions")) {
104 foreach ($questions as $question) {
105 $stamp = make_unique_id_code();
106 if (!($success = $success && set_field("quiz_questions", "stamp", $stamp, "id", $question->id))) {
107 notify("Error while adding stamp to question id = $question->id");
108 break;
114 if ($success && $oldversion < 2003082700) {
115 table_column("quiz_categories", "", "stamp", "varchar", "255", "", "", "not null");
116 if ($categories = get_records("quiz_categories")) {
117 foreach ($categories as $category) {
118 $stamp = make_unique_id_code();
119 if (!($success = $success && set_field("quiz_categories", "stamp", $stamp, "id", $category->id))) {
120 notify("Error while adding stamp to category id = $category->id");
121 break;
127 if ($success && $oldversion < 2003111100) {
128 $duplicates = get_records_sql("SELECT stamp as id,count(*) as cuenta
129 FROM {$CFG->prefix}quiz_questions
130 GROUP BY stamp
131 HAVING count(*)>1");
133 if ($duplicates) {
134 notify("You have some quiz questions with duplicate stamps IDs. Cleaning these up.");
135 foreach ($duplicates as $duplicate) {
136 $questions = get_records("quiz_questions","stamp",$duplicate->id);
137 $add = 1;
138 foreach ($questions as $question) {
139 echo "Changing question id $question->id stamp to ".$duplicate->id.$add."<br />";
140 $success = $success && set_field("quiz_questions","stamp",$duplicate->id.$add,"id",$question->id);
141 $add++;
144 } else {
145 notify("Checked your quiz questions for stamp duplication errors, but no problems were found.", "green");
149 if ($success && $oldversion < 2004021300) {
150 $success = $success && table_column("quiz_questions", "", "questiontextformat", "integer", "2", "", "0", "not null", "questiontext");
153 if ($success && $oldversion < 2004021900) {
154 $success = $success && modify_database("","INSERT INTO prefix_log_display (module, action, mtable, field) VALUES ('quiz', 'add', 'quiz', 'name');");
155 $success = $success && modify_database("","INSERT INTO prefix_log_display (module, action, mtable, field) VALUES ('quiz', 'update', 'quiz', 'name');");
158 if ($success && $oldversion < 2004051700) {
159 include_once("$CFG->dirroot/mod/quiz/lib.php");
160 $success = $success && quiz_refresh_events();
162 if ($success && $oldversion < 2004060200) {
163 $success = $success && table_column("quiz", "", "timelimit", "integer", "", "", "0", "NOT NULL", "");
166 if ($success && $oldversion < 2004070700) {
167 $success = $success && table_column("quiz", "", "password", "varchar", "255", "", "", "not null", "");
168 $success = $success && table_column("quiz", "", "subnet", "varchar", "255", "", "", "not null", "");
171 if ($success && $oldversion < 2004073001) {
172 // Six new tables:
175 $success = $success && modify_database ( "", "BEGIN;");
177 // One table for handling units for numerical questions
178 $success = $success && modify_database ("", " CREATE TABLE prefix_quiz_numerical_units (
179 id SERIAL8 PRIMARY KEY,
180 question INT8 NOT NULL default '0',
181 multiplier decimal(40,20) NOT NULL default '1.00000000000000000000',
182 unit varchar(50) NOT NULL default ''
183 );" );
186 // Four tables for handling distribution and storage of
187 // individual data for dataset dependent question types
188 $success = $success && modify_database ("", " CREATE TABLE prefix_quiz_attemptonlast_datasets (
189 id SERIAL8 PRIMARY KEY,
190 category INT8 NOT NULL default '0',
191 userid INT8 NOT NULL default '0',
192 datasetnumber INT8 NOT NULL default '0',
193 CONSTRAINT prefix_quiz_attemptonlast_datasets_category_userid UNIQUE (category,userid)
194 ) ;");
196 $success = $success && modify_database ("", " CREATE TABLE prefix_quiz_dataset_definitions (
197 id SERIAL8 PRIMARY KEY,
198 category INT8 NOT NULL default '0',
199 name varchar(255) NOT NULL default '',
200 type INT8 NOT NULL default '0',
201 options varchar(255) NOT NULL default '',
202 itemcount INT8 NOT NULL default '0'
203 ) ; ");
205 $success = $success && modify_database ("", " CREATE TABLE prefix_quiz_dataset_items (
206 id SERIAL8 PRIMARY KEY,
207 definition INT8 NOT NULL default '0',
208 number INT8 NOT NULL default '0',
209 value varchar(255) NOT NULL default ''
210 ) ; ");
212 $success = $success && modify_database ("", "CREATE INDEX prefix_quiz_dataset_items_definition_idx ON prefix_quiz_dataset_items (definition);");
214 $success = $success && modify_database ("", " CREATE TABLE prefix_quiz_question_datasets (
215 id SERIAL8 PRIMARY KEY,
216 question INT8 NOT NULL default '0',
217 datasetdefinition INT8 NOT NULL default '0'
218 ) ; ");
220 $success = $success && modify_database ("", "CREATE INDEX prefix_quiz_question_datasets_question_datasetdefinition_idx ON prefix_quiz_question_datasets (question,datasetdefinition);");
222 // One table for new question type calculated
223 // - the first dataset dependent question type
224 $success = $success && modify_database ("", " CREATE TABLE prefix_quiz_calculated (
225 id SERIAL8 PRIMARY KEY,
226 question INT8 NOT NULL default '0',
227 answer INT8 NOT NULL default '0',
228 tolerance varchar(20) NOT NULL default '0.0',
229 tolerancetype INT8 NOT NULL default '1',
230 correctanswerlength INT8 NOT NULL default '2'
231 ) ; ");
233 $success = $success && modify_database ("", "CREATE INDEX prefix_quiz_calculated_question_idx ON prefix_quiz_calculated (question);");
235 $success = $success && modify_database ( "", "COMMIT;");
238 if ($success && $oldversion < 2004111400) {
239 $success = $success && table_column("quiz_responses", "answer", "answer", "text", "", "", "", "not null");
242 if ($success && $oldversion < 2004111700) {
243 $success = $success && execute_sql("DROP INDEX {$CFG->prefix}quiz_course_idx;",false);
244 $success = $success && execute_sql("DROP INDEX {$CFG->prefix}quiz_answers_question_idx;",false);
245 $success = $success && execute_sql("DROP INDEX {$CFG->prefix}quiz_attempts_quiz_idx;",false);
246 $success = $success && execute_sql("DROP INDEX {$CFG->prefix}quiz_attempts_userid_idx;",false);
247 $success = $success && execute_sql("DROP INDEX {$CFG->prefix}quiz_calculated_answer_idx;",false);
248 $success = $success && execute_sql("DROP INDEX {$CFG->prefix}quiz_categories_course_idx;",false);
249 $success = $success && execute_sql("DROP INDEX {$CFG->prefix}quiz_dataset_definitions_category_idx;",false);
250 $success = $success && execute_sql("DROP INDEX {$CFG->prefix}quiz_grades_quiz_idx;",false);
251 $success = $success && execute_sql("DROP INDEX {$CFG->prefix}quiz_grades_userid_idx;",false);
252 $success = $success && execute_sql("DROP INDEX {$CFG->prefix}quiz_numerical_question_idx;",false);
253 $success = $success && execute_sql("DROP INDEX {$CFG->prefix}quiz_numerical_units_question_idx;",false);
254 $success = $success && execute_sql("DROP INDEX {$CFG->prefix}quiz_question_grades_quiz_idx;",false);
255 $success = $success && execute_sql("DROP INDEX {$CFG->prefix}quiz_question_grades_question_idx;",false);
256 $success = $success && execute_sql("DROP INDEX {$CFG->prefix}quiz_questions_category_idx;",false);
257 $success = $success && execute_sql("DROP INDEX {$CFG->prefix}quiz_randomsamatch_question_idx;",false);
258 $success = $success && execute_sql("DROP INDEX {$CFG->prefix}quiz_responses_attempt_idx;",false);
259 $success = $success && execute_sql("DROP INDEX {$CFG->prefix}quiz_responses_question_idx;",false);
261 $success = $success && modify_database('','CREATE INDEX prefix_quiz_course_idx ON prefix_quiz (course);');
262 $success = $success && modify_database('','CREATE INDEX prefix_quiz_answers_question_idx ON prefix_quiz_answers (question);');
263 $success = $success && modify_database('','CREATE INDEX prefix_quiz_attempts_quiz_idx ON prefix_quiz_attempts (quiz);');
264 $success = $success && modify_database('','CREATE INDEX prefix_quiz_attempts_userid_idx ON prefix_quiz_attempts (userid);');
265 $success = $success && modify_database('','CREATE INDEX prefix_quiz_calculated_answer_idx ON prefix_quiz_calculated (answer);');
266 $success = $success && modify_database('','CREATE INDEX prefix_quiz_categories_course_idx ON prefix_quiz_categories (course);');
267 $success = $success && modify_database('','CREATE INDEX prefix_quiz_dataset_definitions_category_idx ON prefix_quiz_dataset_definitions (category);');
268 $success = $success && modify_database('','CREATE INDEX prefix_quiz_grades_quiz_idx ON prefix_quiz_grades (quiz);');
269 $success = $success && modify_database('','CREATE INDEX prefix_quiz_grades_userid_idx ON prefix_quiz_grades (userid);');
270 $success = $success && modify_database('','CREATE INDEX prefix_quiz_numerical_question_idx ON prefix_quiz_numerical (question);');
271 $success = $success && modify_database('','CREATE INDEX prefix_quiz_numerical_units_question_idx ON prefix_quiz_numerical_units (question);');
272 $success = $success && modify_database('','CREATE INDEX prefix_quiz_question_grades_quiz_idx ON prefix_quiz_question_grades (quiz);');
273 $success = $success && modify_database('','CREATE INDEX prefix_quiz_question_grades_question_idx ON prefix_quiz_question_grades (question);');
274 $success = $success && modify_database('','CREATE INDEX prefix_quiz_questions_category_idx ON prefix_quiz_questions (category);');
275 $success = $success && modify_database('','CREATE INDEX prefix_quiz_randomsamatch_question_idx ON prefix_quiz_randomsamatch (question);');
276 $success = $success && modify_database('','CREATE INDEX prefix_quiz_responses_attempt_idx ON prefix_quiz_responses (attempt);');
277 $success = $success && modify_database('','CREATE INDEX prefix_quiz_responses_question_idx ON prefix_quiz_responses (question);');
280 if ($success && $oldversion < 2004112300) { //try and clean up an old mistake - try and bring us up to what is in postgres7.sql today.
281 $success = $success && execute_sql("ALTER TABLE {$CFG->prefix}quiz_attemptonlast_datasets DROP CONSTRAINT category;",false);
282 $success = $success && execute_sql("ALTER TABLE {$CFG->prefix}quiz_attemptonlast_datasets DROP CONSTRAINT {$CFG->prefix}quiz_attemptonlast_datasets_category_userid;",false);
283 $success = $success && execute_sql("ALTER TABLE {$CFG->prefix}quiz_attemptonlast_datasets DROP CONSTRAINT {$CFG->prefix}quiz_category_userid_unique;",false);
284 $success = $success && modify_database('','ALTER TABLE prefix_quiz_attemptonlast_datasets ADD CONSTRAINT prefix_quiz_category_userid_unique UNIQUE (category,userid);');
287 if ($success && $oldversion < 2004120501) {
288 $success = $success && table_column("quiz_calculated", "", "correctanswerformat", "integer", "10", "", "0", "not null", "correctanswerlength");
291 if ($success && $oldversion < 2004121400) { // New field to determine popup window behaviour
292 $success = $success && table_column("quiz", "", "popup", "integer", "4", "", "0", "not null", "subnet");
295 if ($success && $oldversion < 2005010201) {
296 $success = $success && table_column('quiz_categories', '', 'parent');
297 $success = $success && table_column('quiz_categories', '', 'sortorder', 'integer', '10', '', '999');
300 if ($success && $oldversion < 2005010300) {
301 $success = $success && table_column("quiz", "", "questionsperpage", "integer", "10", "", "0", "not null", "review");
304 if ($success && $oldversion < 2005012700) {
305 $success = $success && table_column('quiz_grades', 'grade', 'grade', 'real', 2, '');
308 if ($success && $oldversion < 2005021400) {
309 $success = $success && table_column("quiz", "", "decimalpoints", "integer", "4", "", "2", "not null", "grademethod");
312 if($success && $oldversion < 2005022800) {
313 $success = $success && table_column('quiz_questions', '', 'hidden', 'integer', '1', 'unsigned', '0', 'not null', 'version');
314 $success = $success && table_column('quiz_responses', '', 'originalquestion', 'integer', '10', 'unsigned', '0', 'not null', 'question');
315 $success = $success && modify_database ('', "CREATE TABLE prefix_quiz_question_version (
316 id SERIAL PRIMARY KEY,
317 quiz integer NOT NULL default '0',
318 oldquestion integer NOT NULL default '0',
319 newquestion integer NOT NULL default '0',
320 userid integer NOT NULL default '0',
321 timestamp integer NOT NULL default '0');");
324 if ($success && $oldversion < 2005032000) {
325 $success = $success && execute_sql(" INSERT INTO {$CFG->prefix}log_display (module, action, mtable, field) VALUES ('quiz', 'editquestions', 'quiz', 'name') ");
328 if ($success && $oldversion < 2005032300) {
329 $success = $success && modify_database ('', 'ALTER TABLE prefix_quiz_question_version RENAME TO prefix_quiz_question_versions;');
332 if ($success && $oldversion < 2005041200) { // replace wiki-like with markdown
333 include_once( "$CFG->dirroot/lib/wiki_to_markdown.php" );
334 $wtm = new WikiToMarkdown();
335 $sql = "select course from {$CFG->prefix}quiz_categories, {$CFG->prefix}quiz_questions ";
336 $sql .= "where {$CFG->prefix}quiz_category.id = {$CFG->prefix}quiz_questions.category ";
337 $sql .= "and {$CFG->prefix}quiz_questions.id = ";
338 $wtm->update( 'quiz_questions', 'questiontext', 'questiontextformat', $sql );
341 if ($success && $oldversion < 2005041300) {
342 $success = $success && modify_database('', "UPDATE prefix_quiz_questions SET hidden = '1' WHERE qtype ='".RANDOM."';");
345 if ($success && $oldversion < 2005042002) {
346 $success = $success && table_column('quiz_answers', 'answer', 'answer', 'text', '', '', '', 'not null', '');
350 if ($success && $oldversion < 2005042400) {
351 begin_sql();
353 // Changes to quiz table
355 // The bits of the optionflags field will hold various option flags
356 $success = $success && table_column('quiz', '', 'optionflags', 'integer', '10', 'unsigned', '0', 'not null', 'timeclose');
358 // The penalty scheme
359 $success = $success && table_column('quiz', '', 'penaltyscheme', 'integer', '4', 'unsigned', '0', 'not null', 'optionflags');
361 // The review options are now all stored in the bits of the review field
362 $success = $success && table_column('quiz', 'review', 'review', 'integer', 10, 'unsigned', 0, 'not null', '');
364 /// Changes to quiz_attempts table
366 // The preview flag marks teacher previews
367 $success = $success && table_column('quiz_attempts', '', 'preview', 'tinyint', '2', 'unsigned', '0', 'not null', 'timemodified');
369 // The layout is the list of questions with inserted page breaks.
370 $success = $success && table_column('quiz_attempts', '', 'layout', 'text', '', '', '', 'not null', 'timemodified');
371 // For old quiz attempts we will set this to the repaginated question list from $quiz->questions
373 /// The following updates of field values require a loop through all quizzes
374 // This is because earlier versions of mysql don't allow joins in UPDATE
375 if ($quizzes = get_records('quiz')) {
377 // turn reporting off temporarily to avoid one line output per set_field
378 $olddebug = $db->debug;
379 $db->debug = false;
380 foreach ($quizzes as $quiz) {
382 // repaginate
383 $quiz->questions = ($quiz->questionsperpage) ? quiz_repaginate($quiz->questions, $quiz->questionsperpage) : $quiz->questions;
384 if ($quiz->questionsperpage) {
385 $quiz->questions = quiz_repaginate($quiz->questions, $quiz->questionsperpage);
386 $success = $success && set_field('quiz', 'questions', $quiz->questions, 'id', $quiz->id);
388 set_field('quiz_attempts', 'layout', $quiz->questions, 'quiz', $quiz->id);
390 // set preview flag
391 if ($teachers = get_course_teachers($quiz->course)) {
392 $teacherids = implode(',', array_keys($teachers));
393 $success = $success && execute_sql("UPDATE {$CFG->prefix}quiz_attempts SET preview = 1 WHERE userid IN ($teacherids)");
396 // set review flags in quiz table
397 $review = (QUIZ_REVIEW_IMMEDIATELY & (QUIZ_REVIEW_RESPONSES + QUIZ_REVIEW_SCORES));
398 if ($quiz->feedback) {
399 $review += (QUIZ_REVIEW_IMMEDIATELY & QUIZ_REVIEW_FEEDBACK);
401 if ($quiz->correctanswers) {
402 $review += (QUIZ_REVIEW_IMMEDIATELY & QUIZ_REVIEW_ANSWERS);
404 if ($quiz->review & 1) {
405 $review += QUIZ_REVIEW_CLOSED;
407 if ($quiz->review & 2) {
408 $review += QUIZ_REVIEW_OPEN;
410 $success = $success && set_field('quiz', 'review', $review, 'id', $quiz->id);
412 $db->debug = $olddebug;
415 // We can now drop the fields whose data has been moved to the review field
416 $success = $success && execute_sql(" ALTER TABLE {$CFG->prefix}quiz DROP COLUMN feedback");
417 $success = $success && execute_sql(" ALTER TABLE {$CFG->prefix}quiz DROP COLUMN correctanswers");
419 /// Renaming tables
421 // rename the quiz_question_grades table to quiz_question_instances
422 $success = $success && modify_database ('', 'ALTER TABLE prefix_quiz_question_grades RENAME TO prefix_quiz_question_instances;');
423 $success = $success && modify_database ('', 'ALTER TABLE prefix_quiz_question_grades_id_seq RENAME TO prefix_quiz_question_instances_id_seq;');
424 $success = $success && modify_database ('', 'ALTER TABLE prefix_quiz_question_instances ALTER COLUMN id SET DEFAULT nextval(\'prefix_quiz_question_instances_id_seq\');');
425 $success = $success && modify_database ('', 'DROP INDEX prefix_quiz_question_grades_quiz_idx');
426 $success = $success && modify_database ('', 'DROP INDEX prefix_quiz_question_grades_question_idx;');
427 $success = $success && modify_database ('', 'CREATE INDEX prefix_quiz_question_instances_quiz_idx ON prefix_quiz_question_instances (quiz);');
428 $success = $success && modify_database ('', 'CREATE INDEX prefix_quiz_question_instances_question_idx ON prefix_quiz_question_instances (question);');
430 // rename the quiz_responses table quiz_states
431 $success = $success && modify_database ('', 'ALTER TABLE prefix_quiz_responses RENAME TO prefix_quiz_states;');
432 $success = $success && modify_database ('', 'ALTER TABLE prefix_quiz_responses_id_seq RENAME TO prefix_quiz_states_id_seq;');
433 $success = $success && modify_database ('', 'ALTER TABLE prefix_quiz_states ALTER COLUMN id SET DEFAULT nextval(\'prefix_quiz_states_id_seq\');');
434 $success = $success && modify_database ('', 'DROP INDEX prefix_quiz_responses_attempt_idx;');
435 $success = $success && modify_database ('', 'DROP INDEX prefix_quiz_responses_question_idx;');
436 $success = $success && modify_database ('', 'CREATE INDEX prefix_quiz_states_attempt_idx ON prefix_quiz_states (attempt);');
437 $success = $success && modify_database ('', 'CREATE INDEX prefix_quiz_states_question_idx ON prefix_quiz_states (question);');
440 /// add columns to quiz_states table
442 // The sequence number of the state.
443 $success = $success && table_column('quiz_states', '', 'seq_number', 'integer', '6', 'unsigned', '0', 'not null', 'originalquestion');
444 // For existing states we leave this at 0 because in the old quiz code there was only one response allowed
446 // The time the state was created.
447 $success = $success && table_column('quiz_states', '', 'timestamp', 'integer', '10', 'unsigned', '0', 'not null', 'answer');
448 // For existing states we will below set this to the timemodified field of the attempt
450 // The type of event that led to the creation of the state
451 $success = $success && table_column('quiz_states', '', 'event', 'integer', '4', 'unsigned', '0', 'not null', 'timestamp');
453 // The raw grade
454 $success = $success && table_column('quiz_states', '', 'raw_grade', 'varchar', '10', '', '', 'not null', 'grade');
455 // For existing states (no penalties) this is equal to the grade
456 $success = $success && execute_sql("UPDATE {$CFG->prefix}quiz_states SET raw_grade = grade");
458 // The penalty that the response attracted
459 $success = $success && table_column('quiz_states', '', 'penalty', 'varchar', '10', '', '0.0', 'not null', 'raw_grade');
460 // For existing states this can stay at 0 because penalties did not exist previously.
462 /// New table for pointers to newest and newest graded states
464 $success = $success && modify_database('', "CREATE TABLE prefix_quiz_newest_states (
465 id SERIAL PRIMARY KEY,
466 attemptid integer NOT NULL default '0',
467 questionid integer NOT NULL default '0',
468 newest integer NOT NULL default '0',
469 newgraded integer NOT NULL default '0',
470 sumpenalty varchar(10) NOT NULL default '0.0'
471 );");
472 $success = $success && modify_database('CREATE UNIQUE INDEX prefix_quiz_newest_states_attempt_idx ON prefix_quiz_newest_states (attemptid,questionid);');
474 /// Now upgrade some fields in states and newest_states tables where necessary
475 // to save time on large sites only do this for attempts that have not yet been finished.
476 if ($attempts = get_records_select('quiz_attempts', 'timefinish = 0')) {
477 // turn reporting off temporarily to avoid one line output per set_field
478 $olddebug = $db->debug;
479 $db->debug = false;
480 foreach ($attempts as $attempt) {
481 quiz_upgrade_states($attempt);
483 $db->debug = $olddebug;
486 /// Entries for the log_display table
488 $success = $success && modify_database('', " INSERT INTO prefix_log_display (module, action, mtable, field) VALUES ('quiz', 'preview', 'quiz', 'name');");
489 $success = $success && modify_database('', " INSERT INTO prefix_log_display (module, action, mtable, field) VALUES ('quiz', 'start attempt', 'quiz', 'name');");
490 $success = $success && modify_database('', " INSERT INTO prefix_log_display (module, action, mtable, field) VALUES ('quiz', 'close attempt', 'quiz', 'name');");
492 /// update the default settings in $CFG
493 $review = (QUIZ_REVIEW_IMMEDIATELY & (QUIZ_REVIEW_RESPONSES + QUIZ_REVIEW_SCORES));
494 if (!empty($CFG->quiz_feedback)) {
495 $review += (QUIZ_REVIEW_IMMEDIATELY & QUIZ_REVIEW_FEEDBACK);
497 if (!empty($CFG->quiz_correctanswers)) {
498 $review += (QUIZ_REVIEW_IMMEDIATELY & QUIZ_REVIEW_ANSWERS);
500 if (isset($CFG->quiz_review) and ($CFG->quiz_review & 1)) {
501 $review += QUIZ_REVIEW_CLOSED;
503 if (isset($CFG->quiz_review) and ($CFG->quiz_review & 2)) {
504 $review += QUIZ_REVIEW_OPEN;
506 $success = $success && set_config('quiz_review', $review);
508 /// Use tolerance instead of min and max in numerical question type
509 $success = $success && table_column('quiz_numerical', '', 'tolerance', 'varchar', '255', '', '0.0', 'not null', 'question');
510 $success = $success && execute_sql("UPDATE {$CFG->prefix}quiz_numerical SET tolerance = (max::text::real-min::text::real)/2");
511 $success = $success && modify_database('', 'ALTER TABLE prefix_quiz_numerical DROP COLUMN min'); // Replaced by tolerance
512 $success = $success && modify_database('', 'ALTER TABLE prefix_quiz_numerical DROP COLUMN max'); // Replaced by tolerance
514 /// Tables for Remote Questions
515 $success = $success && modify_database ('', "CREATE TABLE prefix_quiz_rqp (
516 id SERIAL PRIMARY KEY,
517 question integer NOT NULL default '0',
518 type integer NOT NULL default '0',
519 source text NOT NULL,
520 format varchar(255) NOT NULL default '',
521 flags integer NOT NULL default '0',
522 maxscore integer NOT NULL default '1'
523 );");
525 $success = $success && modify_database ('', "CREATE INDEX prefix_quiz_rqp_question_idx ON prefix_quiz_rqp (question);");
527 $success = $success && modify_database ('', "CREATE TABLE prefix_quiz_rqp_states (
528 id SERIAL PRIMARY KEY,
529 stateid integer NOT NULL default '0',
530 responses text NOT NULL,
531 persistent_data text NOT NULL,
532 template_vars text NOT NULL
533 );");
535 $success = $success && modify_database ('', "CREATE TABLE prefix_quiz_rqp_types (
536 id SERIAL PRIMARY KEY,
537 name varchar(255) NOT NULL default '',
538 rendering_server varchar(255) NOT NULL default '',
539 cloning_server varchar(255) NOT NULL default '',
540 flags integer NOT NULL default '0'
541 );");
543 $success = $success && modify_database ('', "CREATE UNIQUE INDEX prefix_quiz_rqp_types_name_uk ON prefix_quiz_rqp_types (name);");
545 if ($success) {
546 $success = $success && commit_sql();
547 } else {
548 rollback_sql();
552 if ($success && $oldversion < 2005042900 && false) { // We don't want this to be executed any more!!!
554 begin_sql();
556 $success = $success && table_column('quiz_multianswers', '', 'sequence', 'varchar', '255', '', '', 'not null', 'question');
557 $success = $success && table_column('quiz_numerical', '', 'answers', 'varchar', '255', '', '', 'not null', 'answer');
558 $success = $success && modify_database('', 'UPDATE prefix_quiz_numerical SET answers = answer');
559 $success = $success && table_column('quiz_questions', '', 'parent', 'integer', '10', 'unsigned', '0', 'not null', 'category');
560 $success = $success && modify_database('', "UPDATE prefix_quiz_questions SET parent = id WHERE qtype ='".RANDOM."';");
562 // convert multianswer questions to the new model
563 if ($multianswers = get_records_sql("SELECT m.id, q.category, q.id AS parent,
564 q.name, q.questiontextformat, m.norm AS
565 defaultgrade, m.answertype AS qtype,
566 q.version, q.hidden, m.answers,
567 m.positionkey
568 FROM {$CFG->prefix}quiz_questions q,
569 {$CFG->prefix}quiz_multianswers m
570 WHERE q.qtype = '".MULTIANSWER."'
571 AND q.id = m.question
572 ORDER BY q.id ASC, m.positionkey ASC")) {
573 $multianswers = array_values($multianswers);
574 $n = count($multianswers);
575 $parent = $multianswers[0]->parent;
576 $sequence = array();
578 // turn reporting off temporarily to avoid one line output per set_field
579 $olddebug = $db->debug;
580 $db->debug = false;
581 for ($i = 0; $i < $n; $i++) {
582 $answers = $multianswers[$i]->answers; unset($multianswers[$i]->answers);
583 $pos = $multianswers[$i]->positionkey; unset($multianswers[$i]->positionkey);
585 // create questions for all the multianswer victims
586 unset($multianswers[$i]->id);
587 $multianswers[$i]->length = 0;
588 $multianswers[$i]->questiontext = '';
589 $multianswers[$i]->stamp = make_unique_id_code();
590 $id = insert_record('quiz_questions', $multianswers[$i]);
591 $success = $success && $id;
592 $sequence[$pos] = $id;
594 // update the answers table to point to these new questions
595 $success = $success && modify_database('', "UPDATE prefix_quiz_answers SET question = '$id' WHERE id IN ($answers);");
596 // update the questiontype tables to point to these new questions
597 if (SHORTANSWER == $multianswers[$i]->qtype) {
598 $success = $success && modify_database('', "UPDATE prefix_quiz_shortanswer SET question = '$id' WHERE answers = '$answers';");
599 } else if (NUMERICAL == $multianswers[$i]->qtype) {
600 if (strpos($answers, ',')) {
601 $numerical = get_records_list('quiz_numerical', 'answer', $answers);
602 // Get the biggest tolerance value
603 $tolerance = 0;
604 foreach ($numerical as $num) {
605 $tolerance = ($tolerance < $num->tolerance ? $num->tolerance : $tolerance);
607 $success = $success && delete_records_select('quiz_numerical', "answer IN ($answers)");
608 $new = new stdClass;
609 $new->question = $id;
610 $new->tolerance = $tolerance;
611 $new->answers = $answers;
612 $success = $success && insert_record('quiz_numerical', $new);
613 unset($numerical, $new, $tolerance);
614 } else {
615 $success = $success && modify_database('', "UPDATE prefix_quiz_numerical SET question = '$id', answers = '$answers' WHERE answer IN ($answers);");
617 } else if (MULTICHOICE == $multianswers[$i]->qtype) {
618 $success = $success && modify_database('', "UPDATE prefix_quiz_multichoice SET question = '$id' WHERE answers = '$answers';");
621 if (!isset($multianswers[$i+1]) || $parent != $multianswers[$i+1]->parent) {
622 $success = $success && delete_records('quiz_multianswers', 'question', $parent);
623 $multi = new stdClass;
624 $multi->question = $parent;
625 $multi->sequence = implode(',', $sequence);
626 $success = $success && insert_record('quiz_multianswers', $multi);
627 if (isset($multianswers[$i+1])) {
628 $parent = $multianswers[$i+1]->parent;
629 $sequence = array();
633 $db->debug = $olddebug;
636 // Remove redundant fields from quiz_multianswers
637 $success = $success && modify_database('', 'ALTER TABLE prefix_quiz_multianswers DROP COLUMN answers');
638 $success = $success && modify_database('', 'ALTER TABLE prefix_quiz_multianswers DROP COLUMN positionkey');
639 $success = $success && modify_database('', 'ALTER TABLE prefix_quiz_multianswers DROP COLUMN answertype');
640 $success = $success && modify_database('', 'ALTER TABLE prefix_quiz_multianswers DROP COLUMN norm');
642 // Change numerical from answer to answers
643 $success = $success && modify_database('', 'ALTER TABLE prefix_quiz_numerical DROP COLUMN answer');
645 if ($success) {
646 $success = $success && commit_sql();
647 } else {
648 rollback_sql();
652 if ($success && $oldversion < 2005050300) {
653 // length of question determines question numbering. Currently all questions require one
654 // question number except for DESCRIPTION questions.
655 $success = $success && table_column('quiz_questions', '', 'length', 'integer', '10', 'unsigned', '1', 'not null', 'qtype');
656 $success = $success && execute_sql("UPDATE {$CFG->prefix}quiz_questions SET length = 0 WHERE qtype = '7'");
659 if ($success && $oldversion < 2005050408) {
660 $success = $success && table_column('quiz_questions', '', 'penalty', 'float', '', '', '0.1', 'not null', 'defaultgrade');
663 if ($success && $oldversion < 2005051401) {
664 // Some earlier changes are undone here, so we need another condition
665 if ($oldversion >= 2005042900) {
666 // Restore the answer field
667 $success = $success && table_column('quiz_numerical', '', 'answer', 'integer', '10', 'unsigned', '0', 'not null', 'answers');
668 $singleanswer = array();
669 if ($numericals = get_records('quiz_numerical')) {
670 $numericals = array_values($numericals);
671 $n = count($numericals);
672 for ($i = 0; $i < $n; $i++) {
673 $numerical =& $numericals[$i];
674 if (strpos($numerical->answers, ',')) { // comma separated list?
675 // Back this up to delete the record after the new ones are created
676 $id = $numerical->id;
677 unset($numerical->id);
678 // We need to create a record for each answer id
679 $answers = explode(',', $numerical->answers);
680 foreach ($answers as $answer) {
681 $numerical->answer = $answer;
682 $success = $success && insert_record('quiz_numerical', $numerical);
684 // ... and get rid of the old record
685 $success = $success && delete_records('quiz_numerical', 'id', $id);
686 } else {
687 $singleanswer[] = $numerical->id;
692 // Do all of these at once
693 if (!empty($singleanswer)) {
694 $singleanswer = implode(',', $singleanswer);
695 $success = $success && modify_database('', "UPDATE prefix_quiz_numerical SET answer = answers WHERE id IN ($singleanswer);");
698 // All answer fields are set, so we can delete the answers field
699 $success = $success && modify_database('', 'ALTER TABLE prefix_quiz_numerical DROP answers');
701 // If the earlier changes weren't made we can safely do only the
702 // bits here.
703 } else {
704 // Comma separated questionids will be stored as sequence
705 $success = $success && table_column('quiz_multianswers', '', 'sequence', 'varchar', '255', '', '', 'not null', 'question');
706 // Change the type of positionkey to int, so that the sorting works!
707 $success = $success && table_column('quiz_multianswers', 'positionkey', 'positionkey', 'integer', '10', 'unsigned', '0', 'not null', '');
708 $success = $success && table_column('quiz_questions', '', 'parent', 'integer', '10', 'unsigned', '0', 'not null', 'category');
709 $success = $success && modify_database('', "UPDATE prefix_quiz_questions SET parent = id WHERE qtype ='".RANDOM."';");
711 // Each multianswer record is converted to a question object and then
712 // inserted as a new question into the quiz_questions table.
713 // After that the question fields in the quiz_answers table and the
714 // qtype specific tables are updated to point to the new question id.
715 // Note: The quiz_numerical table is different as it stores one record
716 // per defined answer (to allow different tolerance values for
717 // different possible answers. (Currently multiple answers are
718 // not supported by the numerical editing interface, but all
719 // all processing code does support that possibility.
720 if ($multianswers = get_records_sql("SELECT m.id, q.category, " .
721 "q.id AS parent, " . // question id (of multianswer question) as parent
722 "q.name, q.questiontextformat, " .
723 "m.norm AS defaultgrade, " . // norm is snow stored as defaultgrade
724 "m.answertype AS qtype, " . // just rename this
725 "q.version, q.hidden, m.answers, " .
726 "m.positionkey " .
727 "FROM {$CFG->prefix}quiz_questions q, " .
728 " {$CFG->prefix}quiz_multianswers m " .
729 "WHERE q.qtype = '".MULTIANSWER."' " .
730 "AND q.id = m.question " .
731 "ORDER BY q.id ASC, m.positionkey ASC")) { // ordered by positionkey
732 $multianswers = array_values($multianswers);
733 $n = count($multianswers);
734 $parent = $multianswers[0]->parent;
735 $sequence = array();
736 $positions = array();
738 // Turn reporting off temporarily to avoid one line output per set_field
739 global $db;
740 $olddebug = $db->debug;
741 // $db->debug = false;
742 for ($i = 0; $i < $n; $i++) {
743 // Backup these two values before unsetting the object fields
744 $answers = $multianswers[$i]->answers; unset($multianswers[$i]->answers);
745 $pos = $multianswers[$i]->positionkey; unset($multianswers[$i]->positionkey);
747 // Needed for substituting multianswer ids with position keys in the $state->answer field
748 $positions[$multianswers[$i]->id] = $pos;
750 // Create questions for all the multianswer victims
751 unset($multianswers[$i]->id);
752 $multianswers[$i]->length = 0;
753 $multianswers[$i]->questiontext = '';
754 $multianswers[$i]->stamp = make_unique_id_code();
755 // $multianswers[$i]->parent is set in the query
756 // $multianswers[$i]->defaultgrade is set in the query
757 // $multianswers[$i]->qtype is set in the query
758 $id = insert_record('quiz_questions', $multianswers[$i]);
759 $success = $success && $id;
760 $sequence[$pos] = $id;
762 // Update the quiz_answers table to point to these new questions
763 $success = $success && modify_database('', "UPDATE prefix_quiz_answers SET question = '$id' WHERE id IN ($answers);");
764 // Update the questiontype tables to point to these new questions
766 if (SHORTANSWER == $multianswers[$i]->qtype) {
767 $success = $success && modify_database('', "UPDATE prefix_quiz_shortanswer SET question = '$id' WHERE answers = '$answers';");
768 } else if (MULTICHOICE == $multianswers[$i]->qtype) {
769 $success = $success && modify_database('', "UPDATE prefix_quiz_multichoice SET question = '$id' WHERE answers = '$answers';");
770 } else if (NUMERICAL == $multianswers[$i]->qtype) {
771 $success = $success && modify_database('', "UPDATE prefix_quiz_numerical SET question = '$id' WHERE answer IN ($answers);");
774 // Whenever we're through with the subquestions of one multianswer
775 // question we delete the old records in the multianswers table,
776 // store a new record with the sequence in the multianswers table
777 // and point $parent to the next multianswer question.
778 if (!isset($multianswers[$i+1]) || $parent != $multianswers[$i+1]->parent) {
780 // Substituting multianswer ids with position keys in the $state->answer field
781 if ($states = get_records('quiz_states', 'question', $parent)) {
782 foreach ($states as $state) {
783 $reg = array();
784 preg_match_all('/(?:^|,)([0-9]+)-([^,]*)/', $state->answer, $reg);
785 $state->answer = '';
786 $m = count($reg[1]);
787 for ($j = 0; $j < $m; $j++) {
788 if (isset($positions[$reg[1][$j]])) {
789 $state->answer .= $positions[$reg[1][$j]] . '-' . $reg[2][$j] . ',';
790 } else {
791 notify("Undefined multianswer id ({$reg[1][$j]}) used in state #{$state->id}!");
792 $state->answer .= $j+1 . '-' . $reg[2][$j] . ',';
795 $state->answer = rtrim($state->answer, ','); // strip trailing comma
796 $success = $success && update_record('quiz_states', $state);
800 $success = $success && delete_records('quiz_multianswers', 'question', $parent);
801 $multi = new stdClass;
802 $multi->question = $parent;
803 $multi->sequence = implode(',', $sequence);
804 $success = $success && insert_record('quiz_multianswers', $multi);
806 if (isset($multianswers[$i+1])) {
807 $parent = $multianswers[$i+1]->parent;
808 $sequence = array();
809 $positions = array();
813 $db->debug = $olddebug;
816 // Remove redundant fields from quiz_multianswers
817 $success = $success && modify_database('', 'ALTER TABLE prefix_quiz_multianswers DROP answers');
818 $success = $success && modify_database('', 'ALTER TABLE prefix_quiz_multianswers DROP positionkey');
819 $success = $success && modify_database('', 'ALTER TABLE prefix_quiz_multianswers DROP answertype');
820 $success = $success && modify_database('', 'ALTER TABLE prefix_quiz_multianswers DROP norm');
824 if ($success && $oldversion < 2005051402) {
825 $success = $success && execute_sql("ALTER TABLE {$CFG->prefix}quiz_attemptonlast_datasets DROP CONSTRAINT category;",false);
826 $success = $success && execute_sql("ALTER TABLE {$CFG->prefix}quiz_attemptonlast_datasets DROP CONSTRAINT {$CFG->prefix}attemptonlast_datasets_category_userid;",false);
827 $success = $success && execute_sql("ALTER TABLE {$CFG->prefix}quiz_attemptonlast_datasets DROP CONSTRAINT {$CFG->prefix}quiz_category_userid_unique;",false);
828 $success = $success && modify_database('','ALTER TABLE prefix_quiz_attemptonlast_datasets ADD CONSTRAINT prefix_quiz_category_userid_unique UNIQUE (category,userid);');
831 if ($success && $oldversion < 2005060300) {
832 // We need to remove some duplicate entries that may be present in some databases
833 // due to a faulty restore script
835 // Remove duplicate entries from quiz_numerical
836 if ($dups = get_records_sql("
837 SELECT question, answer, count(*) as num
838 FROM {$CFG->prefix}quiz_numerical
839 GROUP BY question, answer
840 HAVING count(*) > 1"
841 )) {
842 foreach ($dups as $dup) {
843 $ids = get_records_sql("
844 SELECT id, id
845 FROM {$CFG->prefix}quiz_numerical
846 WHERE question = '$dup->question'
847 AND answer = '$dup->answer'"
849 $skip = true;
850 foreach ($ids as $id) {
851 if ($skip) {
852 $skip = false;
853 } else {
854 $success = $success && delete_records('quiz_numerical','id', $id->id);
860 // Remove duplicate entries from quiz_shortanswer
861 if ($dups = get_records_sql("
862 SELECT question, answers, count(*) as num
863 FROM {$CFG->prefix}quiz_shortanswer
864 GROUP BY question, answers
865 HAVING count(*) > 1"
866 )) {
867 foreach ($dups as $dup) {
868 $ids = get_records_sql("
869 SELECT id, id
870 FROM {$CFG->prefix}quiz_shortanswer
871 WHERE question = '$dup->question'
872 AND answers = '$dup->answers'"
874 $skip = true;
875 foreach ($ids as $id) {
876 if ($skip) {
877 $skip = false;
878 } else {
879 $success = $success && delete_records('quiz_shortanswer','id', $id->id);
885 // Remove duplicate entries from quiz_multichoice
886 if ($dups = get_records_sql("
887 SELECT question, answers, count(*) as num
888 FROM {$CFG->prefix}quiz_multichoice
889 GROUP BY question, answers
890 HAVING count(*) > 1"
891 )) {
892 foreach ($dups as $dup) {
893 $ids = get_records_sql("
894 SELECT id, id
895 FROM {$CFG->prefix}quiz_multichoice
896 WHERE question = '$dup->question'
897 AND answers = '$dup->answers'"
899 $skip = true;
900 foreach ($ids as $id) {
901 if ($skip) {
902 $skip = false;
903 } else {
904 $success = $success && delete_records('quiz_multichoice','id', $id->id);
910 //Search all the orphan categories (those whose course doesn't exist)
911 //and process them, deleting or moving them to site course - Bug 2459
913 //Set debug to false
914 $olddebug = $db->debug;
915 $db->debug = false;
917 //Iterate over all the quiz_categories records to get their course id
918 if ($courses = get_records_sql ("SELECT DISTINCT course as id, course
919 FROM {$CFG->prefix}quiz_categories")) {
920 //Iterate over courses
921 foreach ($courses as $course) {
922 //If the course doesn't exist, orphan category found!
923 //Process it with question_delete_course(). It will do all the hard work.
924 if (!record_exists('course', 'id', $course->id)) {
925 require_once("$CFG->libdir/questionlib.php");
926 $success = $success && question_delete_course($course);
930 //Reset rebug to its original state
931 $db->debug = $olddebug;
934 if ($success && $oldversion < 2005060301) {
935 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz_rqp_type RENAME TO '.$CFG->prefix.'quiz_rqp_types');
936 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz_rqp_type_id_seq RENAME TO '.$CFG->prefix.'rqp_types_id_seq');
937 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz_rqp_types ALTER COLUMN id SET DEFAULT nextval(\''.$CFG->prefix.'quiz_rqp_types_id_seq\')');
938 $success = $success && execute_sql('DROP INDEX '.$CFG->prefix.'quiz_rqp_type_name_uk');
939 $success = $success && execute_sql('CREATE UNIQUE INDEX '.$CFG->prefix.'quiz_rqp_types_name_uk ON '.$CFG->prefix.'quiz_rqp_types (name);');
942 if ($success && $oldversion < 2005060302) { // Mass cleanup of bad postgres upgrade scripts
943 $success = $success && execute_sql('CREATE UNIQUE INDEX '.$CFG->prefix.'quiz_newest_states_attempt_idx ON '.$CFG->prefix.'quiz_newest_states (attemptid, questionid)',false);
944 $success = $success && execute_sql('ALTER TABLE ONLY '.$CFG->prefix.'quiz_attemptonlast_datasets DROP CONSTRAINT '.$CFG->prefix.'quiz_category_userid_unique',false);
945 $success = $success && execute_sql('ALTER TABLE ONLY '.$CFG->prefix.'quiz_attemptonlast_datasets ADD CONSTRAINT '.$CFG->prefix.'quiz_attemptonlast_datasets_category_userid UNIQUE (category, userid)',false);
946 $success = $success && execute_sql('ALTER TABLE ONLY '.$CFG->prefix.'quiz_question_instances DROP CONSTRAINT '.$CFG->prefix.'quiz_question_grades_pkey',false);
947 $success = $success && execute_sql('ALTER TABLE ONLY '.$CFG->prefix.'quiz_question_instances ADD CONSTRAINT '.$CFG->prefix.'quiz_question_instances_pkey PRIMARY KEY (id)',false);
948 $success = $success && execute_sql('ALTER TABLE ONLY '.$CFG->prefix.'quiz_question_versions DROP CONSTRAINT '.$CFG->prefix.'quiz_question_version_pkey',false);
949 $success = $success && execute_sql('ALTER TABLE ONLY '.$CFG->prefix.'quiz_question_versions ADD CONSTRAINT '.$CFG->prefix.'quiz_question_versions_pkey PRIMARY KEY (id)',false);
950 $success = $success && execute_sql('ALTER TABLE ONLY '.$CFG->prefix.'quiz_states DROP CONSTRAINT '.$CFG->prefix.'quiz_responses_pkey',false);
951 $success = $success && execute_sql('ALTER TABLE ONLY '.$CFG->prefix.'quiz_states ADD CONSTRAINT '.$CFG->prefix.'quiz_states_pkey PRIMARY KEY (id)',false);
952 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz ALTER decimalpoints SET NOT NULL',false);
953 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz ALTER optionflags SET NOT NULL',false);
954 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz ALTER penaltyscheme SET NOT NULL',false);
955 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz ALTER popup SET NOT NULL',false);
956 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz ALTER questionsperpage SET NOT NULL',false);
957 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz ALTER review SET NOT NULL',false);
958 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz_answers ALTER answer SET NOT NULL',false);
959 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz_attempts ALTER layout SET NOT NULL',false);
960 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz_attempts ALTER preview SET NOT NULL',false);
962 $success = $success && table_column('quiz_calculated','correctanswerformat','correctanswerformat','integer','16','unsigned','2');
964 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz_categories ALTER parent SET NOT NULL',false);
965 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz_categories ALTER sortorder SET NOT NULL',false);
966 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz_grades ALTER grade SET NOT NULL',false);
967 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz_multianswers ALTER sequence SET NOT NULL',false);
968 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz_numerical ALTER tolerance SET NOT NULL',false);
969 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz_questions ALTER hidden SET NOT NULL',false);
970 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz_questions ALTER length SET NOT NULL',false);
971 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz_questions ALTER parent SET NOT NULL',false);
973 $success = $success && table_column('quiz_questions','penalty','penalty','real','','UNSIGNED','0.1');
975 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz_states ALTER answer SET NOT NULL',false);
976 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz_states ALTER event SET NOT NULL',false);
977 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz_states ALTER originalquestion SET NOT NULL',false);
978 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz_states ALTER penalty SET NOT NULL',false);
979 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz_states ALTER raw_grade SET NOT NULL',false);
980 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz_states ALTER seq_number SET NOT NULL',false);
981 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz_states ALTER timestamp SET NOT NULL',false);
984 if ($success && $oldversion < 2005100500) {
985 // clean up an old mistake. This mistake may not have been made, so don't worry about failures.
986 $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz_question_version_id_seq RENAME TO '.$CFG->prefix.'quiz_question_versions_id_seq',false);
987 $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz_question_versions ALTER COLUMN id SET DEFAULT nextval(\''.$CFG->prefix.'quiz_question_versions_id_seq\')',false);
989 if ($success && $oldversion < 2006020801) {
990 $success = $success && table_column("quiz", "", "delay1", "INTEGER", "10", "UNSIGNED", "0", "NOT NULL", "popup");
991 $success = $success && table_column("quiz", "", "delay2", "INTEGER", "10", "UNSIGNED", "0", "NOT NULL", "delay1");
994 if ($success && $oldversion < 2006021101) {
995 // set defaultgrade field properly (probably not necessary, but better make sure)
996 $success && execute_sql("UPDATE {$CFG->prefix}quiz_questions SET defaultgrade = '1' WHERE defaultgrade = '0'", false);
997 $success && execute_sql("UPDATE {$CFG->prefix}quiz_questions SET defaultgrade = '0' WHERE qtype = '7'", false);
1000 if ($success && $oldversion < 2006021103) {
1001 // add new field to store the question-level shuffleanswers option
1002 $success = $success && table_column('quiz_match', '', 'shuffleanswers', 'tinyint', '4', 'unsigned', '1', 'not null', 'subquestions');
1003 $success = $success && table_column('quiz_multichoice', '', 'shuffleanswers', 'tinyint', '4', 'unsigned', '1', 'not null', 'single');
1004 $success = $success && table_column('quiz_randomsamatch', '', 'shuffleanswers', 'tinyint', '4', 'unsigned', '1', 'not null', 'choose');
1007 if ($success && $oldversion < 2006021104) {
1008 // add originalversion field for the new versioning mechanism
1009 $success = $success && table_column('quiz_question_versions', '', 'originalquestion', 'int', '10', 'unsigned', '0', 'not null', 'newquestion');
1012 if ($success && $oldversion < 2006021302) {
1013 $success = $success && table_column('quiz_match_sub', '', 'code', 'int', '10', 'unsigned', '0', 'not null', 'id');
1014 $success = $success && execute_sql("UPDATE {$CFG->prefix}quiz_match_sub SET code = id", false);
1017 if ($success && $oldversion < 2006021304) {
1018 // convert sequence field to text to accomodate very long sequences, see bug 4257
1019 $success = $success && table_column('quiz_multianswers', 'sequence', 'sequence', 'text', '', '', '', 'not null', 'question');
1022 if ($success && $oldversion < 2006021400) {
1023 // modify_database('','CREATE UNIQUE INDEX prefix_quiz_attempts_uniqueid_uk ON prefix_quiz_attempts (uniqueid);');
1024 // this index will not be created since uniqueid was not added, proper upgrade will be on 2006042801
1027 if ($success && $oldversion < 2006021501) {
1028 $success = $success && execute_sql("ALTER TABLE {$CFG->prefix}quiz_newest_states RENAME TO {$CFG->prefix}question_sessions", false);
1031 if ($success && $oldversion < 2006021900) {
1033 $success = $success && modify_database ('', "
1034 CREATE TABLE prefix_quiz_essay (
1035 id SERIAL PRIMARY KEY,
1036 question integer NOT NULL default '0',
1037 answer varchar(255) NOT NULL default ''
1038 ) ");
1040 $success = $success && modify_database ('', "
1041 CREATE TABLE prefix_quiz_essay_states (
1042 id SERIAL PRIMARY KEY,
1043 stateid integer NOT NULL default '0',
1044 graded integer NOT NULL default '0',
1045 fraction varchar(10) NOT NULL default '0.0',
1046 response text NOT NULL default ''
1047 );");
1049 // convert grade fields to real
1050 $success = $success && table_column('quiz_attempts', 'sumgrades', 'sumgrades', 'real', '', '', '0', 'not null');
1051 $success = $success && table_column('quiz_answers', 'fraction', 'fraction', 'real', '', '', '0', 'not null');
1052 $success = $success && table_column('quiz_essay_states', 'fraction', 'fraction', 'real', '', '', '0', 'not null');
1053 $success = $success && set_field('quiz_states', 'grade', 0, 'grade', ''); // Some values may be wrong, which caused errors in the following table_column calls.
1054 $success = $success && set_field('quiz_states', 'raw_grade', 0, 'raw_grade', '');
1055 $success = $success && set_field('quiz_states', 'penalty', 0, 'penalty', '');
1056 $success = $success && table_column('quiz_states', 'grade', 'grade', 'real', '', '', '0', 'not null');
1057 $success = $success && table_column('quiz_states', 'raw_grade', 'raw_grade', 'real', '', '', '0', 'not null');
1058 $success = $success && table_column('quiz_states', 'penalty', 'penalty', 'real', '', '', '0', 'not null');
1059 $success = $success && table_column('question_sessions', 'sumpenalty', 'sumpenalty', 'real', '', '', '0', 'not null');
1062 if ($success && $oldversion < 2006030100) {
1063 // Fix up another table rename :(
1064 // THIS caused the mistake: execute_sql("ALTER TABLE {$CFG->prefix}quiz_newest_states RENAME TO {$CFG->prefix}question_sessions", false);
1065 $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz_newest_states_id_seq RENAME TO '.$CFG->prefix.'question_sessions_id_seq',false);
1066 $success && execute_sql('ALTER TABLE '.$CFG->prefix.'question_sessions ALTER COLUMN id SET DEFAULT nextval(\''.$CFG->prefix.'question_sessions_id_seq\')',false);
1069 if ($success && $oldversion < 2006030101) {
1070 $success = $success && execute_sql("ALTER TABLE {$CFG->prefix}quiz_questions RENAME TO {$CFG->prefix}question");
1071 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz_questions_id_seq RENAME TO '.$CFG->prefix.'question_id_seq');
1072 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'question ALTER COLUMN id SET DEFAULT nextval(\''.$CFG->prefix.'question_id_seq\')');
1074 $success = $success && execute_sql("ALTER TABLE {$CFG->prefix}quiz_states RENAME TO {$CFG->prefix}question_states");
1075 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz_states_id_seq RENAME TO '.$CFG->prefix.'question_states_id_seq');
1076 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'question_states ALTER COLUMN id SET DEFAULT nextval(\''.$CFG->prefix.'question_states_id_seq\')');
1078 $success = $success && execute_sql("ALTER TABLE {$CFG->prefix}quiz_answers RENAME TO {$CFG->prefix}question_answers");
1079 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz_answers_id_seq RENAME TO '.$CFG->prefix.'question_answers_id_seq');
1080 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'question_answers ALTER COLUMN id SET DEFAULT nextval(\''.$CFG->prefix.'question_answers_id_seq\')');
1082 $success = $success && execute_sql("ALTER TABLE {$CFG->prefix}quiz_categories RENAME TO {$CFG->prefix}question_categories");
1083 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz_categories_id_seq RENAME TO '.$CFG->prefix.'question_categories_id_seq');
1084 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'question_categories ALTER COLUMN id SET DEFAULT nextval(\''.$CFG->prefix.'question_categories_id_seq\')');
1087 if ($success && $oldversion < 2006031202) {
1088 $success = $success && execute_sql("ALTER TABLE {$CFG->prefix}quiz_truefalse RENAME TO {$CFG->prefix}question_truefalse");
1089 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz_truefalse_id_seq RENAME TO '.$CFG->prefix.'question_truefalse_id_seq');
1090 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'question_truefalse ALTER COLUMN id SET DEFAULT nextval(\''.$CFG->prefix.'question_truefalse_id_seq\')');
1092 $success = $success && execute_sql("ALTER TABLE {$CFG->prefix}quiz_shortanswer RENAME TO {$CFG->prefix}question_shortanswer");
1093 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz_shortanswer_id_seq RENAME TO '.$CFG->prefix.'question_shortanswer_id_seq');
1094 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'question_shortanswer ALTER COLUMN id SET DEFAULT nextval(\''.$CFG->prefix.'question_shortanswer_id_seq\')');
1096 $success = $success && execute_sql("ALTER TABLE {$CFG->prefix}quiz_multianswers RENAME TO {$CFG->prefix}question_multianswer");
1097 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz_multianswers_id_seq RENAME TO '.$CFG->prefix.'question_multianswer_id_seq');
1098 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'question_multianswer ALTER COLUMN id SET DEFAULT nextval(\''.$CFG->prefix.'question_multianswer_id_seq\')');
1100 $success = $success && execute_sql("ALTER TABLE {$CFG->prefix}quiz_multichoice RENAME TO {$CFG->prefix}question_multichoice");
1101 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz_multichoice_id_seq RENAME TO '.$CFG->prefix.'question_multichoice_id_seq');
1102 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'question_multichoice ALTER COLUMN id SET DEFAULT nextval(\''.$CFG->prefix.'question_multichoice_id_seq\')');
1104 $success = $success && execute_sql("ALTER TABLE {$CFG->prefix}quiz_numerical RENAME TO {$CFG->prefix}question_numerical");
1105 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz_numerical_id_seq RENAME TO '.$CFG->prefix.'question_numerical_id_seq');
1106 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'question_numerical ALTER COLUMN id SET DEFAULT nextval(\''.$CFG->prefix.'question_numerical_id_seq\')');
1108 $success = $success && execute_sql("ALTER TABLE {$CFG->prefix}quiz_numerical_units RENAME TO {$CFG->prefix}question_numerical_units");
1109 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz_numerical_units_id_seq RENAME TO '.$CFG->prefix.'question_numerical_units_id_seq');
1110 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'question_numerical_units ALTER COLUMN id SET DEFAULT nextval(\''.$CFG->prefix.'question_numerical_units_id_seq\')');
1112 $success = $success && execute_sql("ALTER TABLE {$CFG->prefix}quiz_randomsamatch RENAME TO {$CFG->prefix}question_randomsamatch");
1113 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz_randomsamatch_id_seq RENAME TO '.$CFG->prefix.'question_randomsamatch_id_seq');
1114 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'question_randomsamatch ALTER COLUMN id SET DEFAULT nextval(\''.$CFG->prefix.'question_randomsamatch_id_seq\')');
1116 $success = $success && execute_sql("ALTER TABLE {$CFG->prefix}quiz_match RENAME TO {$CFG->prefix}question_match");
1117 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz_match_id_seq RENAME TO '.$CFG->prefix.'question_match_id_seq');
1118 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'question_match ALTER COLUMN id SET DEFAULT nextval(\''.$CFG->prefix.'question_match_id_seq\')');
1120 $success = $success && execute_sql("ALTER TABLE {$CFG->prefix}quiz_match_sub RENAME TO {$CFG->prefix}question_match_sub");
1121 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz_match_sub_id_seq RENAME TO '.$CFG->prefix.'question_match_sub_id_seq');
1122 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'question_match_sub ALTER COLUMN id SET DEFAULT nextval(\''.$CFG->prefix.'question_match_sub_id_seq\')');
1124 $success = $success && execute_sql("ALTER TABLE {$CFG->prefix}quiz_calculated RENAME TO {$CFG->prefix}question_calculated");
1125 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz_calculated_id_seq RENAME TO '.$CFG->prefix.'question_calculated_id_seq');
1126 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'question_calculated ALTER COLUMN id SET DEFAULT nextval(\''.$CFG->prefix.'question_calculated_id_seq\')');
1128 $success = $success && execute_sql("ALTER TABLE {$CFG->prefix}quiz_dataset_definitions RENAME TO {$CFG->prefix}question_dataset_definitions");
1129 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz_dataset_definitions_id_seq RENAME TO '.$CFG->prefix.'question_dataset_definitions_id_seq');
1130 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'question_dataset_definitions ALTER COLUMN id SET DEFAULT nextval(\''.$CFG->prefix.'question_dataset_definitions_id_seq\')');
1132 $success = $success && execute_sql("ALTER TABLE {$CFG->prefix}quiz_dataset_items RENAME TO {$CFG->prefix}question_dataset_items");
1133 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz_dataset_items_id_seq RENAME TO '.$CFG->prefix.'question_dataset_items_id_seq');
1134 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'question_dataset_items ALTER COLUMN id SET DEFAULT nextval(\''.$CFG->prefix.'question_dataset_items_id_seq\')');
1136 $success = $success && execute_sql("ALTER TABLE {$CFG->prefix}quiz_question_datasets RENAME TO {$CFG->prefix}question_datasets");
1137 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz_question_datasets_id_seq RENAME TO '.$CFG->prefix.'question_datasets_id_seq');
1138 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'question_datasets ALTER COLUMN id SET DEFAULT nextval(\''.$CFG->prefix.'question_datasets_id_seq\')');
1140 $success = $success && execute_sql("ALTER TABLE {$CFG->prefix}quiz_rqp RENAME TO {$CFG->prefix}question_rqp");
1141 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz_rqp_id_seq RENAME TO '.$CFG->prefix.'question_rqp_id_seq');
1142 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'question_rqp ALTER COLUMN id SET DEFAULT nextval(\''.$CFG->prefix.'question_rqp_id_seq\')');
1144 $success = $success && execute_sql("ALTER TABLE {$CFG->prefix}quiz_rqp_states RENAME TO {$CFG->prefix}question_rqp_states");
1145 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz_rqp_states_id_seq RENAME TO '.$CFG->prefix.'question_rqp_states_id_seq');
1146 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'question_rqp_states ALTER COLUMN id SET DEFAULT nextval(\''.$CFG->prefix.'question_rqp_states_id_seq\')');
1148 $success = $success && execute_sql("ALTER TABLE {$CFG->prefix}quiz_rqp_types RENAME TO {$CFG->prefix}question_rqp_types");
1149 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz_rqp_types_id_seq RENAME TO '.$CFG->prefix.'question_rqp_types_id_seq');
1150 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'question_rqp_types ALTER COLUMN id SET DEFAULT nextval(\''.$CFG->prefix.'question_rqp_types_id_seq\')');
1152 $success = $success && execute_sql("ALTER TABLE {$CFG->prefix}quiz_essay RENAME TO {$CFG->prefix}question_essay");
1153 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz_essay_id_seq RENAME TO '.$CFG->prefix.'question_essay_id_seq');
1154 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'question_essay ALTER COLUMN id SET DEFAULT nextval(\''.$CFG->prefix.'question_essay_id_seq\')');
1156 $success = $success && execute_sql("ALTER TABLE {$CFG->prefix}quiz_essay_states RENAME TO {$CFG->prefix}question_essay_states");
1157 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'quiz_essay_states_id_seq RENAME TO '.$CFG->prefix.'question_essay_states_id_seq');
1158 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'question_essay_states ALTER COLUMN id SET DEFAULT nextval(\''.$CFG->prefix.'question_essay_states_id_seq\')');
1162 if ($success && $oldversion < 2006032100) {
1163 // change from the old questiontype numbers to using the questiontype names
1164 $success = $success && table_column('question', 'qtype', 'qtype', 'varchar', 20, '', '', 'not null');
1165 $success = $success && set_field('question', 'qtype', 'shortanswer', 'qtype', 1);
1166 $success = $success && set_field('question', 'qtype', 'truefalse', 'qtype', 2);
1167 $success = $success && set_field('question', 'qtype', 'multichoice', 'qtype', 3);
1168 $success = $success && set_field('question', 'qtype', 'random', 'qtype', 4);
1169 $success = $success && set_field('question', 'qtype', 'match', 'qtype', 5);
1170 $success = $success && set_field('question', 'qtype', 'randomsamatch', 'qtype', 6);
1171 $success = $success && set_field('question', 'qtype', 'description', 'qtype', 7);
1172 $success = $success && set_field('question', 'qtype', 'numerical', 'qtype', 8);
1173 $success = $success && set_field('question', 'qtype', 'multianswer', 'qtype', 9);
1174 $success = $success && set_field('question', 'qtype', 'calculated', 'qtype', 10);
1175 $success = $success && set_field('question', 'qtype', 'rqp', 'qtype', 11);
1176 $success = $success && set_field('question', 'qtype', 'essay', 'qtype', 12);
1179 if ($success && $oldversion < 2006032200) {
1180 // set version for all questiontypes that already have their tables installed
1181 $success = $success && set_config('qtype_calculated_version', 2006032100);
1182 $success = $success && set_config('qtype_essay_version', 2006032100);
1183 $success = $success && set_config('qtype_match_version', 2006032100);
1184 $success = $success && set_config('qtype_multianswer_version', 2006032100);
1185 $success = $success && set_config('qtype_multichoice_version', 2006032100);
1186 $success = $success && set_config('qtype_numerical_version', 2006032100);
1187 $success = $success && set_config('qtype_randomsamatch_version', 2006032100);
1188 $success = $success && set_config('qtype_rqp_version', 2006032100);
1189 $success = $success && set_config('qtype_shortanswer_version', 2006032100);
1190 $success = $success && set_config('qtype_truefalse_version', 2006032100);
1193 if ($success && $oldversion < 2006040600) {
1194 $success = $success && table_column('question_sessions', '', 'comment', 'text', '', '', '', 'not null', 'sumpenalty');
1197 if ($success && $oldversion < 2006040900) {
1198 $success = $success && modify_database('', "UPDATE prefix_question SET parent = id WHERE qtype ='random';");
1201 if ($success && $oldversion < 2006041000) {
1202 $success = $success && modify_database('', " INSERT INTO prefix_log_display (module, action, mtable, field) VALUES ('quiz', 'continue attempt', 'quiz', 'name');");
1205 if ($success && $oldversion < 2006041001) {
1206 $success = $success && table_column('question', 'version', 'version', 'varchar', 255);
1209 if ($success && $oldversion < 2006042800) {
1210 // Check we have some un-renamed tables (verified in some servers)
1211 if ($tables = $db->MetaTables('TABLES')) {
1212 if (in_array($CFG->prefix.'quiz_randommatch', $tables) &&
1213 !in_array($CFG->prefix.'question_randomsamatch', $tables)) {
1214 $success = $success && modify_database ("", "ALTER TABLE prefix_quiz_randommatch RENAME prefix_question_randomsamatch ");
1215 $success = $success && modify_database ("", "ALTER TABLE prefix_quiz_randommatch_id_seq RENAME prefix_question_randomsamatch_id_seq ");
1216 $success = $success && execute_sql('ALTER TABLE '.$CFG->prefix.'question_randomsamatch ALTER COLUMN id SET DEFAULT nextval(\''.$CFG->prefix.'question_randomsamatch_id_seq\')');
1218 // Check for one possible missing field in one table
1219 if ($columns = $db->MetaColumnNames($CFG->prefix.'question_randomsamatch')) {
1220 if (!in_array('shuffleanswers', $columns)) {
1221 $success = $success && table_column('question_randomsamatch', '', 'shuffleanswers', 'tinyint', '4', 'unsigned', '1', 'not null', 'choose');
1227 if ($success && $oldversion < 2006051300) { // this block also exec'ed by 2006042801 on MOODLE_16_STABLE
1228 // The newgraded field must always point to a valid state
1229 $success = $success && modify_database("","UPDATE prefix_question_sessions SET newgraded = newest where newgraded = '0'");
1231 // Only perform this if hasn't been performed before (in MOODLE_16_STABLE branch - bug 5717)
1232 $tables = $db->MetaTables('TABLES');
1233 if (!in_array($CFG->prefix . 'question_attempts', $tables)) {
1234 // The following table is discussed in bug 5468
1235 $success = $success && modify_database ("", "CREATE TABLE prefix_question_attempts (
1236 id SERIAL PRIMARY KEY,
1237 modulename varchar(20) NOT NULL default 'quiz'
1238 );");
1242 if ($success && $oldversion < 2006051700) { // this block also exec'd by 2006042802 on MOODLE_16_STABLE
1244 notify("The next set of upgrade operations may report an
1245 error if you are upgrading from v1.6.
1246 This error mesage is normal, and can be ignored.");
1247 // this block is taken from mysql.php 2005070202
1248 // add new unique id to prepare the way for lesson module to have its own attempts table
1249 table_column('quiz_attempts', '', 'uniqueid', 'integer', '10', 'unsigned', '0', 'not null', 'id');
1250 // create one entry for all the existing quiz attempts
1251 // initially we can use the id as the unique id because no other modules use attempts yet.
1252 $success = $success && execute_sql("UPDATE {$CFG->prefix}quiz_attempts SET uniqueid = id");
1253 // we set $CFG->attemptuniqueid to the next available id
1254 $record = get_record_sql("SELECT nextval('{$CFG->prefix}quiz_attempts_id_seq')");
1255 $success = $success && set_config('attemptuniqueid', empty($record->nextid) ? 1 : $record->nextid);
1256 // the above will be a race condition, see bug 5468
1258 modify_database('','CREATE UNIQUE INDEX prefix_quiz_attempts_uniqueid_uk ON prefix_quiz_attempts (uniqueid);');
1260 // create one entry for all the existing quiz attempts
1261 $success = $success && modify_database ("", "INSERT INTO prefix_question_attempts (id)
1262 SELECT uniqueid
1263 FROM prefix_quiz_attempts;");
1266 if ($success && $oldversion < 2006042802) {
1268 // Copy the teacher comments from the question_essay_states table to the new
1269 // question_sessions table.
1271 // Get the attempt unique ID, teacher comment, graded flag, state ID, and question ID
1272 // based on the quesiont_essay_states
1273 if ($results = get_records_sql("SELECT a.uniqueid, es.response AS essaycomment, es.graded AS isgraded,
1274 qs.id AS stateid, qs.question AS questionid
1275 FROM {$CFG->prefix}question_states as qs,
1276 {$CFG->prefix}question_essay_states es,
1277 {$CFG->prefix}quiz_attempts a
1278 WHERE es.stateid = qs.id AND a.uniqueid = qs.attempt")) {
1279 foreach ($results as $result) {
1280 // Create a state object to be used for updating
1281 $state = new stdClass;
1282 $state->id = $result->stateid;
1284 if ($result->isgraded) {
1285 // Graded - save comment to the sessions and change state event to QUESTION_EVENTMANUALGRADE
1286 if (!($success = $success && set_field('question_sessions', 'comment', $result->essaycomment, 'attemptid', $result->uniqueid, 'questionid', $result->questionid))) {
1287 notify("Essay Table Migration: Cannot save comment");
1288 break;
1290 $state->event = 9; //QUESTION_EVENTMANUALGRADE;
1291 } else {
1292 // Not graded
1293 $state->event = 7; //QUESTION_EVENTSUBMIT;
1296 // Save the event
1297 if (!($success = $success && update_record('question_states', $state))) {
1298 notify("Essay Table Migration: Cannot update state");
1299 break;
1304 // dropping unused tables
1305 $success = $success && execute_sql('DROP TABLE '.$CFG->prefix.'question_essay_states');
1306 $success = $success && execute_sql('DROP TABLE '.$CFG->prefix.'question_essay');
1307 $success = $success && execute_sql('DROP TABLE '.$CFG->prefix.'quiz_attemptonlast_datasets');
1309 $success = $success && modify_database('', 'ALTER TABLE prefix_question
1310 ALTER COLUMN qtype SET DEFAULT \'0\'');
1311 $success = $success && modify_database('', 'ALTER TABLE prefix_question
1312 ALTER COLUMN version SET DEFAULT \'\'');
1314 // recreate the indexes that was not moved while quiz was transitioning to question lib
1315 $success && notify('Errors on indexes not being able to drop or already exists can be ignored as they may have been properly upgraded previously');
1316 $success && modify_database('','DROP INDEX prefix_quiz_numerical_answer_idx');
1317 $success && modify_database('','DROP INDEX prefix_quiz_numerical_question_idx');
1318 $success = $success && modify_database('','CREATE INDEX prefix_question_numerical_question_idx ON prefix_question_numerical (question)');
1319 $success = $success && modify_database('','CREATE INDEX prefix_question_numerical_answer_idx ON prefix_question_numerical (answer)');
1320 $success && modify_database('','DROP INDEX prefix_quiz_question_datasets_question_datasetdefinition_idx');
1321 $success = $success && modify_database('','CREATE INDEX prefix_question_datasets_question_datasetdefinition_idx ON prefix_question_datasets (question, datasetdefinition)');
1323 $success && modify_database('','DROP INDEX prefix_quiz_multichoice_question_idx');
1324 $success = $success && modify_database('','CREATE INDEX prefix_question_multichoice_question_idx ON prefix_question_multichoice (question)');
1326 $success && modify_database('','DROP INDEX prefix_quiz_categories_course_idx');
1327 $success = $success && modify_database('','CREATE INDEX prefix_question_categories_course_idx ON prefix_question_categories (course)');
1329 $success && modify_database('','DROP INDEX prefix_quiz_shortanswer_question_idx');
1330 $success = $success && modify_database('','CREATE INDEX prefix_question_shortanswer_question_idx ON prefix_question_shortanswer (question)');
1332 $success && modify_database('','DROP INDEX prefix_quiz_questions_category_idx');
1333 $success = $success && modify_database('','CREATE INDEX prefix_question_category_idx ON prefix_question (category)');
1335 $success && modify_database('','DROP INDEX prefix_quiz_calculated_answer_idx');
1336 $success && modify_database('','DROP INDEX prefix_quiz_calculated_question_idx');
1337 $success = $success && modify_database('','CREATE INDEX prefix_question_calculated_question_idx ON prefix_question_calculated (question)');
1338 $success = $success && modify_database('','CREATE INDEX prefix_question_calculated_answer_idx ON prefix_question_calculated (answer)');
1340 $success && modify_database('','DROP INDEX prefix_quiz_answers_question_idx');
1341 $success = $success && modify_database('','CREATE INDEX prefix_question_answers_question_idx ON prefix_question_answers (question)');
1343 $success && modify_database('','DROP INDEX prefix_quiz_dataset_items_definition_idx');
1344 $success = $success && modify_database('','CREATE INDEX prefix_question_dataset_items_definition_idx ON prefix_question_dataset_items (definition)');
1346 $success && modify_database('','DROP INDEX prefix_quiz_numerical_units_question_idx');
1347 $success = $success && modify_database('','CREATE INDEX prefix_question_numerical_units_question_idx ON prefix_question_numerical_units (question)');
1349 $success && modify_database('','DROP INDEX prefix_quiz_randomsamatch_question_idx');
1350 $success = $success && modify_database('','CREATE INDEX prefix_question_randomsamatch_question_idx ON prefix_question_randomsamatch (question)');
1352 $success && modify_database('','DROP INDEX prefix_quiz_states_question_idx');
1353 $success && modify_database('','DROP INDEX prefix_quiz_states_attempt_idx');
1354 $success = $success && modify_database('','CREATE INDEX prefix_question_states_question_idx ON prefix_question_states (question)');
1355 $success = $success && modify_database('','CREATE INDEX prefix_question_states_attempt_idx ON prefix_question_states (attempt)');
1357 $success && modify_database('','DROP INDEX prefix_quiz_match_question_idx');
1358 $success = $success && modify_database('','CREATE INDEX prefix_question_match_question_idx ON prefix_question_match (question)');
1360 $success && modify_database('','DROP INDEX prefix_quiz_match_sub_question_idx');
1361 $success = $success && modify_database('','CREATE INDEX prefix_question_match_sub_question_idx ON prefix_question_match_sub (question)');
1363 $success && modify_database('','DROP INDEX prefix_quiz_multianswers_question_idx');
1364 $success = $success && modify_database('','CREATE INDEX prefix_question_multianswer_question_idx ON prefix_question_multianswer (question)');
1366 $success && modify_database('','DROP INDEX prefix_quiz_dataset_definitions_category_idx');
1367 $success = $success && modify_database('','CREATE INDEX prefix_question_dataset_definitions_category_idx ON prefix_question_dataset_definitions (category)');
1369 $success = $success && modify_database('','CREATE INDEX prefix_log_timecoursemoduleaction_idx ON prefix_log ("time", course, module, "action")');
1370 $success = $success && modify_database('','CREATE INDEX prefix_log_coursemoduleaction_idx ON prefix_log (course, module, "action")');
1372 $success && modify_database('','DROP INDEX prefix_quiz_rqp_question_idx');
1373 $success = $success && modify_database('','CREATE INDEX prefix_question_rqp_question_idx ON prefix_question_rqp (question)');
1375 $success && modify_database('','DROP INDEX prefix_quiz_truefalse_question_idx');
1376 $success = $success && modify_database('','CREATE INDEX prefix_question_truefalse_question_idx ON prefix_question_truefalse (question)');
1377 $success && notify('End of upgrading of indexes');
1380 $success && notify('Renaming primary key names');
1381 $success = $success && modify_database('', 'ALTER TABLE prefix_question_numerical DROP CONSTRAINT prefix_quiz_numerical_pkey');
1382 $success = $success && modify_database('', 'ALTER TABLE prefix_question_numerical ADD CONSTRAINT prefix_question_numerical_pkey PRIMARY KEY (id)');
1384 $success = $success && modify_database('', 'ALTER TABLE prefix_question_datasets DROP CONSTRAINT prefix_quiz_question_datasets_pkey');
1385 $success = $success && modify_database('', 'ALTER TABLE prefix_question_datasets ADD CONSTRAINT prefix_question_datasets_pkey PRIMARY KEY (id)');
1387 $success = $success && modify_database('', 'ALTER TABLE prefix_question_multichoice DROP CONSTRAINT prefix_quiz_multichoice_pkey');
1388 $success = $success && modify_database('', 'ALTER TABLE prefix_question_multichoice ADD CONSTRAINT prefix_question_multichoice_pkey PRIMARY KEY (id)');
1390 $success = $success && modify_database('', 'ALTER TABLE prefix_question_rqp_states DROP CONSTRAINT prefix_quiz_rqp_states_pkey');
1391 $success = $success && modify_database('', 'ALTER TABLE prefix_question_rqp_states ADD CONSTRAINT prefix_question_rqp_states_pkey PRIMARY KEY (id)');
1393 $success = $success && modify_database('', 'ALTER TABLE prefix_question_categories DROP CONSTRAINT prefix_quiz_categories_pkey');
1394 $success = $success && modify_database('', 'ALTER TABLE prefix_question_categories ADD CONSTRAINT prefix_question_categories_pkey PRIMARY KEY (id)');
1396 $success = $success && modify_database('', 'ALTER TABLE prefix_question_shortanswer DROP CONSTRAINT prefix_quiz_shortanswer_pkey');
1397 $success = $success && modify_database('', 'ALTER TABLE prefix_question_shortanswer ADD CONSTRAINT prefix_question_shortanswer_pkey PRIMARY KEY (id)');
1399 $success = $success && modify_database('', 'ALTER TABLE prefix_question DROP CONSTRAINT prefix_quiz_questions_pkey');
1400 $success = $success && modify_database('', 'ALTER TABLE prefix_question ADD CONSTRAINT prefix_question_pkey PRIMARY KEY (id)');
1402 $success = $success && modify_database('', 'ALTER TABLE prefix_question_sessions DROP CONSTRAINT prefix_quiz_newest_states_pkey');
1403 $success = $success && modify_database('', 'ALTER TABLE prefix_question_sessions ADD CONSTRAINT prefix_question_sessions_pkey PRIMARY KEY (id)');
1405 $success = $success && modify_database('', 'ALTER TABLE prefix_question_calculated DROP CONSTRAINT prefix_quiz_calculated_pkey');
1406 $success = $success && modify_database('', 'ALTER TABLE prefix_question_calculated ADD CONSTRAINT prefix_question_calculated_pkey PRIMARY KEY (id)');
1408 $success = $success && modify_database('', 'ALTER TABLE prefix_question_answers DROP CONSTRAINT prefix_quiz_answers_pkey');
1409 $success = $success && modify_database('', 'ALTER TABLE prefix_question_answers ADD CONSTRAINT prefix_question_answers_pkey PRIMARY KEY (id)');
1411 $success = $success && modify_database('', 'ALTER TABLE prefix_question_dataset_items DROP CONSTRAINT prefix_quiz_dataset_items_pkey');
1412 $success = $success && modify_database('', 'ALTER TABLE prefix_question_dataset_items ADD CONSTRAINT prefix_question_dataset_items_pkey PRIMARY KEY (id)');
1414 $success = $success && modify_database('', 'ALTER TABLE prefix_question_numerical_units DROP CONSTRAINT prefix_quiz_numerical_units_pkey');
1415 $success = $success && modify_database('', 'ALTER TABLE prefix_question_numerical_units ADD CONSTRAINT prefix_question_numerical_units_pkey PRIMARY KEY (id)');
1417 $success = $success && modify_database('', 'ALTER TABLE prefix_question_randomsamatch DROP CONSTRAINT prefix_quiz_randomsamatch_pkey');
1418 $success = $success && modify_database('', 'ALTER TABLE prefix_question_randomsamatch ADD CONSTRAINT prefix_question_randomsamatch_pkey PRIMARY KEY (id)');
1420 $success = $success && modify_database('', 'ALTER TABLE prefix_question_rqp_types DROP CONSTRAINT prefix_quiz_rqp_types_pkey');
1421 $success = $success && modify_database('', 'ALTER TABLE prefix_question_rqp_types ADD CONSTRAINT prefix_question_rqp_types_pkey PRIMARY KEY (id)');
1423 $success = $success && modify_database('', 'ALTER TABLE prefix_question_states DROP CONSTRAINT prefix_quiz_states_pkey');
1424 $success = $success && modify_database('', 'ALTER TABLE prefix_question_states ADD CONSTRAINT prefix_question_states_pkey PRIMARY KEY (id)');
1426 $success = $success && modify_database('', 'ALTER TABLE prefix_question_match DROP CONSTRAINT prefix_quiz_match_pkey');
1427 $success = $success && modify_database('', 'ALTER TABLE prefix_question_match ADD CONSTRAINT prefix_question_match_pkey PRIMARY KEY (id)');
1429 $success = $success && modify_database('', 'ALTER TABLE prefix_question_match_sub DROP CONSTRAINT prefix_quiz_match_sub_pkey');
1430 $success = $success && modify_database('', 'ALTER TABLE prefix_question_match_sub ADD CONSTRAINT prefix_question_match_sub_pkey PRIMARY KEY (id)');
1432 $success = $success && modify_database('', 'ALTER TABLE prefix_question_multianswer DROP CONSTRAINT prefix_quiz_multianswers_pkey');
1433 $success = $success && modify_database('', 'ALTER TABLE prefix_question_multianswer ADD CONSTRAINT prefix_question_multianswer_pkey PRIMARY KEY (id)');
1435 $success = $success && modify_database('', 'ALTER TABLE prefix_question_dataset_definitions DROP CONSTRAINT prefix_quiz_dataset_definitions_pkey');
1436 $success = $success && modify_database('', 'ALTER TABLE prefix_question_dataset_definitions ADD CONSTRAINT prefix_question_dataset_definitions_pkey PRIMARY KEY (id)');
1438 $success = $success && modify_database('', 'ALTER TABLE prefix_question_rqp DROP CONSTRAINT prefix_quiz_rqp_pkey');
1439 $success = $success && modify_database('', 'ALTER TABLE prefix_question_rqp ADD CONSTRAINT prefix_question_rqp_pkey PRIMARY KEY (id)');
1441 $success = $success && modify_database('', 'ALTER TABLE prefix_question_truefalse DROP CONSTRAINT prefix_quiz_truefalse_pkey');
1442 $success = $success && modify_database('', 'ALTER TABLE prefix_question_truefalse ADD CONSTRAINT prefix_question_truefalse_pkey PRIMARY KEY (id)');
1443 $success && notify('End of renaming primary keys');
1447 if ($oldversion < 2006081000) {
1448 // Add a column to the the question table to store the question general feedback.
1449 $success = $success && table_column('question', '', 'commentarytext', 'text', '', '', '', 'not null', 'image');
1451 // Adjust the quiz review options so that general feedback is displayed whenever feedback is.
1452 $success = $success && execute_sql('UPDATE ' . $CFG->prefix . 'quiz SET review = ' .
1453 '(review & ~' . QUIZ_REVIEW_GENERALFEEDBACK . ') | ' . // Clear any existing junk from the commenary bits.
1454 '((review & ' . QUIZ_REVIEW_FEEDBACK . ') * 8)'); // Set the general feedback bits to be the same as the feedback ones.
1456 // Same adjustment to the defaults for new quizzes.
1457 $success = $success && set_config('quiz_review', ($CFG->quiz_review & ~QUIZ_REVIEW_GENERALFEEDBACK) |
1458 (($CFG->quiz_review & QUIZ_REVIEW_FEEDBACK) << 3));
1461 if ($success && $oldversion < 2006081400) {
1462 $success = $success && modify_database('', "
1463 CREATE TABLE prefix_quiz_feedback (
1464 id SERIAL PRIMARY KEY,
1465 quizid integer NOT NULL default '0',
1466 feedbacktext text NOT NULL default '',
1467 maxgrade real NOT NULL default '0',
1468 mingrade real NOT NULL default '0'
1471 $success = $success && modify_database('',
1472 "CREATE INDEX prefix_quiz_feedback_quizid_idx ON prefix_quiz_feedback (quizid);");
1474 $success = $success && execute_sql("
1475 INSERT INTO {$CFG->prefix}quiz_feedback (quizid, feedbacktext, maxgrade, mingrade)
1476 SELECT id, '', grade + 1, 0 FROM {$CFG->prefix}quiz;
1480 if ($success && $oldversion < 2006082400) {
1481 $success = $success && table_column('question_sessions', 'comment', 'manualcomment', 'text', '', '', '');
1484 if ($success && $oldversion < 2006091900) {
1485 $success = $success && table_column('question_dataset_items', 'number', 'itemnumber', 'integer');
1488 if ($success && $oldversion < 2006091901) {
1489 $success = $success && table_column('question', 'commentarytext', 'generalfeedback', 'text', '', '', '');
1492 ////// DO NOT ADD NEW THINGS HERE!! USE upgrade.php and the lib/ddllib.php functions.
1494 return $success;