📄 postgres7.php
字号:
$success = $success && modify_database('', 'ALTER TABLE prefix_quiz_multianswers DROP COLUMN answers'); $success = $success && modify_database('', 'ALTER TABLE prefix_quiz_multianswers DROP COLUMN positionkey'); $success = $success && modify_database('', 'ALTER TABLE prefix_quiz_multianswers DROP COLUMN answertype'); $success = $success && modify_database('', 'ALTER TABLE prefix_quiz_multianswers DROP COLUMN norm'); // Change numerical from answer to answers $success = $success && modify_database('', 'ALTER TABLE prefix_quiz_numerical DROP COLUMN answer'); if ($success) { $success = $success && commit_sql(); } else { rollback_sql(); } } if ($success && $oldversion < 2005050300) { // length of question determines question numbering. Currently all questions require one // question number except for DESCRIPTION questions. $success = $success && table_column('quiz_questions', '', 'length', 'integer', '10', 'unsigned', '1', 'not null', 'qtype'); $success = $success && execute_sql("UPDATE {$CFG->prefix}quiz_questions SET length = 0 WHERE qtype = '7'"); } if ($success && $oldversion < 2005050408) { $success = $success && table_column('quiz_questions', '', 'penalty', 'float', '', '', '0.1', 'not null', 'defaultgrade'); } if ($success && $oldversion < 2005051401) { // Some earlier changes are undone here, so we need another condition if ($oldversion >= 2005042900) { // Restore the answer field $success = $success && table_column('quiz_numerical', '', 'answer', 'integer', '10', 'unsigned', '0', 'not null', 'answers'); $singleanswer = array(); if ($numericals = get_records('quiz_numerical')) { $numericals = array_values($numericals); $n = count($numericals); for ($i = 0; $i < $n; $i++) { $numerical =& $numericals[$i]; if (strpos($numerical->answers, ',')) { // comma separated list? // Back this up to delete the record after the new ones are created $id = $numerical->id; unset($numerical->id); // We need to create a record for each answer id $answers = explode(',', $numerical->answers); foreach ($answers as $answer) { $numerical->answer = $answer; $success = $success && insert_record('quiz_numerical', $numerical); } // ... and get rid of the old record $success = $success && delete_records('quiz_numerical', 'id', $id); } else { $singleanswer[] = $numerical->id; } } } // Do all of these at once if (!empty($singleanswer)) { $singleanswer = implode(',', $singleanswer); $success = $success && modify_database('', "UPDATE prefix_quiz_numerical SET answer = answers WHERE id IN ($singleanswer);"); } // All answer fields are set, so we can delete the answers field $success = $success && modify_database('', 'ALTER TABLE prefix_quiz_numerical DROP answers'); // If the earlier changes weren't made we can safely do only the // bits here. } else { // Comma separated questionids will be stored as sequence $success = $success && table_column('quiz_multianswers', '', 'sequence', 'varchar', '255', '', '', 'not null', 'question'); // Change the type of positionkey to int, so that the sorting works! $success = $success && table_column('quiz_multianswers', 'positionkey', 'positionkey', 'integer', '10', 'unsigned', '0', 'not null', ''); $success = $success && table_column('quiz_questions', '', 'parent', 'integer', '10', 'unsigned', '0', 'not null', 'category'); $success = $success && modify_database('', "UPDATE prefix_quiz_questions SET parent = id WHERE qtype ='".RANDOM."';"); // Each multianswer record is converted to a question object and then // inserted as a new question into the quiz_questions table. // After that the question fields in the quiz_answers table and the // qtype specific tables are updated to point to the new question id. // Note: The quiz_numerical table is different as it stores one record // per defined answer (to allow different tolerance values for // different possible answers. (Currently multiple answers are // not supported by the numerical editing interface, but all // all processing code does support that possibility. if ($multianswers = get_records_sql("SELECT m.id, q.category, " . "q.id AS parent, " . // question id (of multianswer question) as parent "q.name, q.questiontextformat, " . "m.norm AS defaultgrade, " . // norm is snow stored as defaultgrade "m.answertype AS qtype, " . // just rename this "q.version, q.hidden, m.answers, " . "m.positionkey " . "FROM {$CFG->prefix}quiz_questions q, " . " {$CFG->prefix}quiz_multianswers m " . "WHERE q.qtype = '".MULTIANSWER."' " . "AND q.id = m.question " . "ORDER BY q.id ASC, m.positionkey ASC")) { // ordered by positionkey $multianswers = array_values($multianswers); $n = count($multianswers); $parent = $multianswers[0]->parent; $sequence = array(); $positions = array(); // Turn reporting off temporarily to avoid one line output per set_field global $db; $olddebug = $db->debug; // $db->debug = false; for ($i = 0; $i < $n; $i++) { // Backup these two values before unsetting the object fields $answers = $multianswers[$i]->answers; unset($multianswers[$i]->answers); $pos = $multianswers[$i]->positionkey; unset($multianswers[$i]->positionkey); // Needed for substituting multianswer ids with position keys in the $state->answer field $positions[$multianswers[$i]->id] = $pos; // Create questions for all the multianswer victims unset($multianswers[$i]->id); $multianswers[$i]->length = 0; $multianswers[$i]->questiontext = ''; $multianswers[$i]->stamp = make_unique_id_code(); // $multianswers[$i]->parent is set in the query // $multianswers[$i]->defaultgrade is set in the query // $multianswers[$i]->qtype is set in the query $id = insert_record('quiz_questions', $multianswers[$i]); $success = $success && $id; $sequence[$pos] = $id; // Update the quiz_answers table to point to these new questions $success = $success && modify_database('', "UPDATE prefix_quiz_answers SET question = '$id' WHERE id IN ($answers);"); // Update the questiontype tables to point to these new questions if (SHORTANSWER == $multianswers[$i]->qtype) { $success = $success && modify_database('', "UPDATE prefix_quiz_shortanswer SET question = '$id' WHERE answers = '$answers';"); } else if (MULTICHOICE == $multianswers[$i]->qtype) { $success = $success && modify_database('', "UPDATE prefix_quiz_multichoice SET question = '$id' WHERE answers = '$answers';"); } else if (NUMERICAL == $multianswers[$i]->qtype) { $success = $success && modify_database('', "UPDATE prefix_quiz_numerical SET question = '$id' WHERE answer IN ($answers);"); } // Whenever we're through with the subquestions of one multianswer // question we delete the old records in the multianswers table, // store a new record with the sequence in the multianswers table // and point $parent to the next multianswer question. if (!isset($multianswers[$i+1]) || $parent != $multianswers[$i+1]->parent) { // Substituting multianswer ids with position keys in the $state->answer field if ($states = get_records('quiz_states', 'question', $parent)) { foreach ($states as $state) { $reg = array(); preg_match_all('/(?:^|,)([0-9]+)-([^,]*)/', $state->answer, $reg); $state->answer = ''; $m = count($reg[1]); for ($j = 0; $j < $m; $j++) { if (isset($positions[$reg[1][$j]])) { $state->answer .= $positions[$reg[1][$j]] . '-' . $reg[2][$j] . ','; } else { notify("Undefined multianswer id ({$reg[1][$j]}) used in state #{$state->id}!"); $state->answer .= $j+1 . '-' . $reg[2][$j] . ','; } } $state->answer = rtrim($state->answer, ','); // strip trailing comma $success = $success && update_record('quiz_states', $state); } } $success = $success && delete_records('quiz_multianswers', 'question', $parent); $multi = new stdClass; $multi->question = $parent; $multi->sequence = implode(',', $sequence); $success = $success && insert_record('quiz_multianswers', $multi); if (isset($multianswers[$i+1])) { $parent = $multianswers[$i+1]->parent; $sequence = array(); $positions = array(); } } } $db->debug = $olddebug; } // Remove redundant fields from quiz_multianswers $success = $success && modify_database('', 'ALTER TABLE prefix_quiz_multianswers DROP answers'); $success = $success && modify_database('', 'ALTER TABLE prefix_quiz_multianswers DROP positionkey'); $success = $success && modify_database('', 'ALTER TABLE prefix_quiz_multianswers DROP answertype'); $success = $success && modify_database('', 'ALTER TABLE prefix_quiz_multianswers DROP norm'); } } if ($success && $oldversion < 2005051402) { $success = $success && execute_sql("ALTER TABLE {$CFG->prefix}quiz_attemptonlast_datasets DROP CONSTRAINT category;",false); $success = $success && execute_sql("ALTER TABLE {$CFG->prefix}quiz_attemptonlast_datasets DROP CONSTRAINT {$CFG->prefix}attemptonlast_datasets_category_userid;",false); $success = $success && execute_sql("ALTER TABLE {$CFG->prefix}quiz_attemptonlast_datasets DROP CONSTRAINT {$CFG->prefix}quiz_category_userid_unique;",false); $success = $success && modify_database('','ALTER TABLE prefix_quiz_attemptonlast_datasets ADD CONSTRAINT prefix_quiz_category_userid_unique UNIQUE (category,userid);'); } if ($success && $oldversion < 2005060300) { // We need to remove some duplicate entries that may be present in some databases // due to a faulty restore script // Remove duplicate entries from quiz_numerical if ($dups = get_records_sql(" SELECT question, answer, count(*) as num FROM {$CFG->prefix}quiz_numerical GROUP BY question, answer HAVING count(*) > 1" )) { foreach ($dups as $dup) { $ids = get_records_sql(" SELECT id, id FROM {$CFG->prefix}quiz_numerical WHERE question = '$dup->question' AND answer = '$dup->answer'" );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -