From 36be25f6e55006cd6fcc768cb3d5ec404d385515 Mon Sep 17 00:00:00 2001 From: gustav_delius Date: Sat, 13 May 2006 16:57:17 +0000 Subject: [PATCH] Introduce question_attempts table, closing bug 5468 Fixed incorrect check for whether to update state or insert new one in save_question_session() Deal with manually graded states correctly during regrading Increase state sequence number during manual grading Supplied missing userid to quiz_save_best_grade() during manual grading --- lib/questionlib.php | 31 ++++++++++++++++++++++--------- mod/quiz/comment.php | 6 +----- mod/quiz/db/mysql.php | 13 ++++++++++++- mod/quiz/db/mysql.sql | 12 ++++++++++++ mod/quiz/db/postgres7.php | 12 +++++++++++- mod/quiz/db/postgres7.sql | 10 ++++++++++ mod/quiz/version.php | 2 +- 7 files changed, 69 insertions(+), 17 deletions(-) diff --git a/lib/questionlib.php b/lib/questionlib.php index 185add7386..86a16de4fe 100644 --- a/lib/questionlib.php +++ b/lib/questionlib.php @@ -330,6 +330,7 @@ function delete_attempt($attemptid) { // It is important that this is done only after calling the questiontype functions delete_records("question_states", "attempt", $attemptid); delete_records("question_sessions", "attemptid", $attemptid); + delete_records("question_attempts", "id", $attemptid); return; } @@ -681,7 +682,7 @@ function save_question_session(&$question, &$state) { $state->answer = isset($state->responses['']) ? $state->responses[''] : ''; // Save the state - if (isset($state->update)) { // this forces the old state record to be overwritten + if (!empty($state->update)) { // this forces the old state record to be overwritten update_record('question_states', $state); } else { if (!$state->id = insert_record('question_states', $state)) { @@ -858,10 +859,16 @@ function regrade_question_in_attempt($question, $attempt, $cmoptions, $verbose=f } else { $action->event = $states[$j]->event; } - // Reprocess (regrade) responses - if (!question_process_responses($question, $replaystate, $action, $cmoptions, - $attempt)) { - $verbose && notify("Couldn't regrade state #{$state->id}!"); + + if ($action->event = QUESTION_EVENTMANUALGRADE) { + question_process_comment($question, $replaystate, $attempt, $states[$j]->comment, $states[$j]->grade); + } else { + + // Reprocess (regrade) responses + if (!question_process_responses($question, $replaystate, $action, $cmoptions, + $attempt)) { + $verbose && notify("Couldn't regrade state #{$state->id}!"); + } } // We need rounding here because grades in the DB get truncated @@ -873,6 +880,7 @@ function regrade_question_in_attempt($question, $attempt, $cmoptions, $verbose=f } $replaystate->id = $states[$j]->id; + $replaystate->changed = true; $replaystate->update = true; // This will ensure that the existing database entry is updated rather than a new one created save_question_session($question, $replaystate); } @@ -1144,6 +1152,7 @@ function question_process_comment($question, &$state, &$attempt, $comment, $grad $state->grade = $grade; $state->penalty = 0; $state->timestamp = time(); + $state->seq_number++; // We need to indicate that the state has changed in order for it to be saved $state->changed = 1; // We want to update existing state (rather than creating new one) if it @@ -1197,13 +1206,17 @@ function question_get_id_from_name_prefix($name) { * attempts. Hence a module, when creating a new attempt, calls this function and * stores the return value in the 'uniqueid' field of its attempts table. */ -function question_new_attempt_uniqueid() { +function question_new_attempt_uniqueid($modulename='quiz') { global $CFG; - set_config('attemptuniqueid', $CFG->attemptuniqueid + 1); - return $CFG->attemptuniqueid; + $attempt->modulename = $modulename; + if (!$id = insert_record('question_attempts', $attempt)) { + error('Could not create new entry in question_attempts table'); + } + return $id; } -/* Creates a stamp that uniquely identifies this version of the question +/** + * 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. diff --git a/mod/quiz/comment.php b/mod/quiz/comment.php index 49e993b135..34f0d1806f 100644 --- a/mod/quiz/comment.php +++ b/mod/quiz/comment.php @@ -3,10 +3,6 @@ * This page prints a review of a particular question attempt * * @version $Id$ -* @author Martin Dougiamas and many others. This has recently been completely -* rewritten by Alex Smith, Julian Sedding and Gustav Delius as part of -* the Serving Mathematics project -* {@link http://maths.york.ac.uk/serving_maths} * @license http://www.gnu.org/copyleft/gpl.html GNU Public License * @package quiz */ @@ -69,7 +65,7 @@ // If the state has changed save it and update the quiz grade if ($state->changed) { save_question_session($question, $state); - quiz_save_best_grade($quiz); + quiz_save_best_grade($quiz, $attempt->userid); } notify(get_string('changessaved')); diff --git a/mod/quiz/db/mysql.php b/mod/quiz/db/mysql.php index 721771a9a7..8f3d541f25 100644 --- a/mod/quiz/db/mysql.php +++ b/mod/quiz/db/mysql.php @@ -1035,9 +1035,20 @@ function quiz_upgrade($oldversion) { } } - if ($oldversion < 2006043000) { + if ($oldversion < 2006051300) { // The newgraded field must always point to a valid state modify_database("","UPDATE prefix_question_sessions SET newgraded = newest where newgraded = '0'"); + + // The following table is discussed in bug 5468 + modify_database ("", "CREATE TABLE prefix_question_attempts ( + id int(10) unsigned NOT NULL auto_increment, + modulename varchar(20) NOT NULL default 'quiz', + PRIMARY KEY (id) + ) TYPE=MyISAM COMMENT='Student attempts. This table gets extended by the modules';"); + // create one entry for all the existing quiz attempts + modify_database ("", "INSERT INTO prefix_question_attempts (id) + SELECT uniqueid + FROM prefix_quiz_attempts;"); } return true; diff --git a/mod/quiz/db/mysql.sql b/mod/quiz/db/mysql.sql index 9f9d34c0f9..daff1d028a 100644 --- a/mod/quiz/db/mysql.sql +++ b/mod/quiz/db/mysql.sql @@ -286,6 +286,18 @@ CREATE TABLE prefix_question_states ( -- -------------------------------------------------------- +-- +-- Table structure for table `prefix_question_attempts` +-- + +CREATE TABLE prefix_question_attempts ( + id int(10) unsigned NOT NULL auto_increment, + modulename varchar(20) NOT NULL default 'quiz', + PRIMARY KEY (id) +) TYPE=MyISAM COMMENT='Student attempts. This table gets extended by the modules'; + +-- -------------------------------------------------------- + INSERT INTO prefix_log_display (module, action, mtable, field) VALUES ('quiz', 'add', 'quiz', 'name'); INSERT INTO prefix_log_display (module, action, mtable, field) VALUES ('quiz', 'update', 'quiz', 'name'); diff --git a/mod/quiz/db/postgres7.php b/mod/quiz/db/postgres7.php index 2b217c8f1e..0338567c8c 100644 --- a/mod/quiz/db/postgres7.php +++ b/mod/quiz/db/postgres7.php @@ -1206,9 +1206,19 @@ function quiz_upgrade($oldversion) { } } - if ($oldversion < 2006043000) { + if ($oldversion < 2006051300) { // The newgraded field must always point to a valid state modify_database("","UPDATE prefix_question_sessions SET newgraded = newest where newgraded = '0'"); + + // The following table is discussed in bug 5468 + modify_database ("", "CREATE TABLE prefix_question_attempts ( + id SERIAL PRIMARY KEY, + modulename varchar(20) NOT NULL default 'quiz' + );"); + // create one entry for all the existing quiz attempts + modify_database ("", "INSERT INTO prefix_question_attempts (id) + SELECT uniqueid + FROM prefix_quiz_attempts;"); } return true; diff --git a/mod/quiz/db/postgres7.sql b/mod/quiz/db/postgres7.sql index 5c771567d8..483d330faa 100644 --- a/mod/quiz/db/postgres7.sql +++ b/mod/quiz/db/postgres7.sql @@ -255,6 +255,16 @@ CREATE TABLE prefix_question ( CREATE INDEX prefix_question_category_idx ON prefix_question (category); +# -------------------------------------------------------- + +# +# Table structure for table prefix_question_attempts +# + +CREATE TABLE prefix_question_attempts ( + id SERIAL PRIMARY KEY, + modulename varchar(20) NOT NULL default 'quiz' +); # -------------------------------------------------------- diff --git a/mod/quiz/version.php b/mod/quiz/version.php index abdfb06f0a..37b5295759 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 = 2006042800; // The (date) version of this module +$module->version = 2006051300; // The (date) version of this module $module->requires = 2006022400; // Requires this Moodle version $module->cron = 0; // How often should cron check this module (seconds)? -- 2.39.5