From cbe20043b517c390bbc931b4efe8f0585b8fb8b5 Mon Sep 17 00:00:00 2001 From: gustav_delius Date: Mon, 10 Apr 2006 22:10:32 +0000 Subject: [PATCH] Whenever a question is edited the version field is set to question_hash(), see bug 2958. --- lib/questionlib.php | 12 ++++++++++++ mod/quiz/db/mysql.php | 4 ++++ mod/quiz/db/mysql.sql | 2 +- mod/quiz/db/postgres7.php | 4 ++++ mod/quiz/db/postgres7.sql | 2 +- mod/quiz/version.php | 2 +- question/format.php | 4 +++- question/format/aon/format.php | 5 ++++- question/format/coursetestmanager/format.php | 3 ++- question/restorelib.php | 2 +- question/type/multianswer/questiontype.php | 3 --- question/type/questiontype.php | 17 +++++++++-------- 12 files changed, 42 insertions(+), 18 deletions(-) diff --git a/lib/questionlib.php b/lib/questionlib.php index 29fd4e6f44..5e07530549 100644 --- a/lib/questionlib.php +++ b/lib/questionlib.php @@ -1269,6 +1269,18 @@ function question_new_attempt_uniqueid() { return $CFG->attemptuniqueid; } +/* Creates a stamp that uniquely identifies this version of the question + * + * In future we want this to use a hash of the question data to guarantee that + * identical versions have the same version stamp. + * + * @param object $question + * @return string A unique version stamp + */ +function question_hash($question) { + return make_unique_id_code(); +} + /// FUNCTIONS THAT SIMPLY WRAP QUESTIONTYPE METHODS ////////////////////////////////// diff --git a/mod/quiz/db/mysql.php b/mod/quiz/db/mysql.php index de3a5e7089..8aa670375e 100644 --- a/mod/quiz/db/mysql.php +++ b/mod/quiz/db/mysql.php @@ -1015,6 +1015,10 @@ function quiz_upgrade($oldversion) { modify_database('', " INSERT INTO prefix_log_display VALUES ('quiz', 'continue attempt', 'quiz', 'name');"); } + if ($oldversion < 2006041001) { + table_column('question', 'version', 'version', 'varchar', 255); + } + return true; } diff --git a/mod/quiz/db/mysql.sql b/mod/quiz/db/mysql.sql index 8e80d11de1..cdf48f2fd2 100644 --- a/mod/quiz/db/mysql.sql +++ b/mod/quiz/db/mysql.sql @@ -255,7 +255,7 @@ CREATE TABLE prefix_question ( qtype varchar(20) NOT NULL default '', length int(10) unsigned NOT NULL default '1', stamp varchar(255) NOT NULL default '', - version int(10) NOT NULL default '1', + version varchar(255) NOT NULL default '', hidden int(1) unsigned NOT NULL default '0', PRIMARY KEY (id), KEY category (category) diff --git a/mod/quiz/db/postgres7.php b/mod/quiz/db/postgres7.php index 920ed58432..9902c38c46 100644 --- a/mod/quiz/db/postgres7.php +++ b/mod/quiz/db/postgres7.php @@ -1184,6 +1184,10 @@ function quiz_upgrade($oldversion) { modify_database('', " INSERT INTO prefix_log_display VALUES ('quiz', 'continue attempt', 'quiz', 'name');"); } + if ($oldversion < 2006041001) { + table_column('question', 'version', 'version', 'varchar', 255); + } + return true; } diff --git a/mod/quiz/db/postgres7.sql b/mod/quiz/db/postgres7.sql index 1fa5e0f6b2..94baa168e3 100644 --- a/mod/quiz/db/postgres7.sql +++ b/mod/quiz/db/postgres7.sql @@ -249,7 +249,7 @@ CREATE TABLE prefix_question ( qtype varchar(20) NOT NULL default '0', length integer NOT NULL DEFAULT '1', stamp varchar(255) NOT NULL default '', - version integer NOT NULL default '1', + version varchar(255) NOT NULL default '', hidden integer NOT NULL default '0' ); diff --git a/mod/quiz/version.php b/mod/quiz/version.php index 201e21980f..da7a2881e4 100644 --- a/mod/quiz/version.php +++ b/mod/quiz/version.php @@ -5,7 +5,7 @@ // This fragment is called by moodle_needs_upgrading() and /admin/index.php //////////////////////////////////////////////////////////////////////////////// -$module->version = 2006041000; // The (date) version of this module +$module->version = 2006041001; // The (date) version of this module $module->requires = 2006022400; // Requires this Moodle version $module->cron = 0; // How often should cron check this module (seconds)? diff --git a/question/format.php b/question/format.php index 8af4a93f48..200aa7be14 100644 --- a/question/format.php +++ b/question/format.php @@ -91,7 +91,6 @@ class qformat_default { $question->category = $this->category->id; $question->stamp = make_unique_id_code(); // Set the unique code (not to be changed) - $question->version = 1; // Original version of this question if (!$question->id = insert_record("question", $question)) { error( get_string('cannotinsert','quiz') ); @@ -114,6 +113,9 @@ class qformat_default { notify($result->notice); return true; } + + // Give the question a unique version stamp determined by question_hash() + set_field('question', 'version', question_hash($question), 'id', $question->id); } return true; } diff --git a/question/format/aon/format.php b/question/format/aon/format.php index da2163e41b..e78f9775df 100644 --- a/question/format/aon/format.php +++ b/question/format/aon/format.php @@ -191,7 +191,10 @@ class qformat_aon extends qformat_default { notify($result->notice); } - /// Delete the old short-answer questions + // Give the question a unique version stamp determined by question_hash() + set_field('question', 'version', question_hash($question), 'id', $question->id); + + /// Delete the old short-answer questions execute_sql("DELETE FROM {$CFG->prefix}question WHERE id IN ($extractids)", false); execute_sql("DELETE FROM {$CFG->prefix}question_shortanswer WHERE question IN ($extractids)", false); diff --git a/question/format/coursetestmanager/format.php b/question/format/coursetestmanager/format.php index 86455599a0..2416f394af 100755 --- a/question/format/coursetestmanager/format.php +++ b/question/format/coursetestmanager/format.php @@ -243,7 +243,6 @@ class qformat_coursetestmanager extends qformat_default { echo "

$count. ".stripslashes($question->questiontext)."

"; $question->category = $this->category->id; $question->stamp = make_unique_id_code(); // Set the unique code (not to be changed) - $question->version = 1; // Original version of this question if (!$question->id = insert_record("question", $question)) { error("Could not insert new question!"); } @@ -260,6 +259,8 @@ class qformat_coursetestmanager extends qformat_default { $this->deletedatabase($filename); return true; } + // Give the question a unique version stamp determined by question_hash() + set_field('question', 'version', question_hash($question), 'id', $question->id); } $this->deletedatabase($filename); return true; diff --git a/question/restorelib.php b/question/restorelib.php index 0b7fedf422..da1e5b9a49 100644 --- a/question/restorelib.php +++ b/question/restorelib.php @@ -212,7 +212,7 @@ } //Check if the question exists - //by category and stamp + //by category, stamp, and version $question_exists = get_record ("question","category",$question->category, "stamp",$question->stamp,"version",$question->version); diff --git a/question/type/multianswer/questiontype.php b/question/type/multianswer/questiontype.php index 4adf038e39..01afd52638 100644 --- a/question/type/multianswer/questiontype.php +++ b/question/type/multianswer/questiontype.php @@ -108,9 +108,6 @@ class embedded_cloze_qtype extends default_questiontype { $question = qtype_multianswer_extract_question ($form->questiontext); if (isset($authorizedquestion->id)) { $question->id = $authorizedquestion->id; - $question->version = $form->version = $authorizedquestion->version; - } else { - $question->version = $form->version = 1; } diff --git a/question/type/questiontype.php b/question/type/questiontype.php index a50ff8a994..3bed76d117 100644 --- a/question/type/questiontype.php +++ b/question/type/questiontype.php @@ -74,19 +74,15 @@ class default_questiontype { $question->defaultgrade = $form->defaultgrade; } - // Set the unique code - // TODO: set the stamp to a hash of the questiondata so that identical - // questions will get the same stamp. That will elliminate possible - // duplication during backup when questions get changed without changes - $question->stamp = make_unique_id_code(); - if (!empty($question->id)) { // Question already exists - $question->version ++; // Update version number of question + // keep existing unique stamp code + $question->stamp = get_field('question', 'stamp', 'id', $question->id); if (!update_record("question", $question)) { error("Could not update question!"); } } else { // Question is a new one - $question->version = 1; + // Set the unique code + $question->stamp = make_unique_id_code(); if (!$question->id = insert_record("question", $question)) { error("Could not insert new question!"); } @@ -114,6 +110,11 @@ class default_questiontype { exit; } + // Give the question a unique version stamp determined by question_hash() + if (!set_field('question', 'version', question_hash($question), 'id', $question->id)) { + error('Could not update question version field'); + } + return $question; } -- 2.39.5