From a2fe7cc0dcaa21599ff88e96d581afa53d462d43 Mon Sep 17 00:00:00 2001 From: moodler Date: Fri, 3 Jan 2003 16:01:48 +0000 Subject: [PATCH] Changed three fields to avoid SQL problems with PostgreSQL etc: quiz_truefalse: true->trueanswer and false->falseanswer quiz_questions: type->qtype --- mod/quiz/db/mysql.php | 8 +++++++- mod/quiz/db/mysql.sql | 6 +++--- mod/quiz/db/postgres7.php | 6 ++++++ mod/quiz/db/postgres7.sql | 6 +++--- mod/quiz/lib.php | 40 ++++++++++++++++++++++++++------------- mod/quiz/multichoice.html | 2 +- mod/quiz/question.php | 24 +++++++++++------------ mod/quiz/shortanswer.html | 2 +- mod/quiz/truefalse.html | 2 +- mod/quiz/version.php | 2 +- 10 files changed, 62 insertions(+), 36 deletions(-) diff --git a/mod/quiz/db/mysql.php b/mod/quiz/db/mysql.php index 754eef177d..11c787380a 100644 --- a/mod/quiz/db/mysql.php +++ b/mod/quiz/db/mysql.php @@ -29,12 +29,18 @@ function quiz_upgrade($oldversion) { execute_sql("ALTER TABLE `quiz_attempts` CHANGE `user` `userid` INT(10) UNSIGNED DEFAULT '0' NOT NULL "); } - // prefixes required from here on + // prefixes required from here on, or use table_column() if ($oldversion < 2003010100) { execute_sql(" ALTER TABLE {$CFG->prefix}quiz ADD review TINYINT(4) UNSIGNED DEFAULT '0' NOT NULL AFTER `grademethod` "); } + if ($oldversion < 2003010301) { + table_column("quiz_truefalse", "true", "trueanswer", "INTEGER", "UNSIGNED", "0", "NOT NULL", ""); + table_column("quiz_truefalse", "false", "falseanswer", "INTEGER", "UNSIGNED", "0", "NOT NULL", ""); + table_column("quiz_questions", "type", "qtype", "INTEGER", "UNSIGNED", "0", "NOT NULL", ""); + } + return true; } diff --git a/mod/quiz/db/mysql.sql b/mod/quiz/db/mysql.sql index 4c844a757a..c601b5d906 100644 --- a/mod/quiz/db/mysql.sql +++ b/mod/quiz/db/mysql.sql @@ -132,7 +132,7 @@ CREATE TABLE `prefix_quiz_questions` ( `name` varchar(255) NOT NULL default '', `questiontext` text NOT NULL, `image` varchar(255) NOT NULL default '', - `type` smallint(6) NOT NULL default '0', + `qtype` smallint(6) NOT NULL default '0', PRIMARY KEY (`id`) ) TYPE=MyISAM COMMENT='The quiz questions themselves'; # -------------------------------------------------------- @@ -172,8 +172,8 @@ CREATE TABLE `prefix_quiz_shortanswer` ( CREATE TABLE `prefix_quiz_truefalse` ( `id` int(10) unsigned NOT NULL auto_increment, `question` int(10) unsigned NOT NULL default '0', - `true` int(10) unsigned NOT NULL default '0', - `false` int(10) unsigned NOT NULL default '0', + `trueanswer` int(10) unsigned NOT NULL default '0', + `falseanswer` int(10) unsigned NOT NULL default '0', PRIMARY KEY (`id`), KEY `question` (`question`) ) TYPE=MyISAM COMMENT='Options for True-False questions'; diff --git a/mod/quiz/db/postgres7.php b/mod/quiz/db/postgres7.php index 87cd5dff45..bc7010d2c6 100644 --- a/mod/quiz/db/postgres7.php +++ b/mod/quiz/db/postgres7.php @@ -10,6 +10,12 @@ function quiz_upgrade($oldversion) { execute_sql(" ALTER TABLE {$CFG->prefix}quiz ADD review integer DEFAULT '0' NOT NULL AFTER `grademethod` "); } + if ($oldversion < 2003010301) { + table_column("quiz_truefalse", "true", "trueanswer", "INTEGER", "UNSIGNED", "0", "NOT NULL", ""); + table_column("quiz_truefalse", "false", "falseanswer", "INTEGER", "UNSIGNED", "0", "NOT NULL", ""); + table_column("quiz_questions", "type", "qtype", "INTEGER", "UNSIGNED", "0", "NOT NULL", ""); + } + return true; } diff --git a/mod/quiz/db/postgres7.sql b/mod/quiz/db/postgres7.sql index c42b7b02b0..459e22a345 100644 --- a/mod/quiz/db/postgres7.sql +++ b/mod/quiz/db/postgres7.sql @@ -124,7 +124,7 @@ CREATE TABLE prefix_quiz_questions ( name varchar(255) NOT NULL default '', questiontext text NOT NULL default '', image varchar(255) NOT NULL default '', - type integer NOT NULL default '0' + qtype integer NOT NULL default '0' ); # -------------------------------------------------------- @@ -161,8 +161,8 @@ CREATE INDEX question_prefix_quiz_shortanswer_idx ON prefix_quiz_shortanswer (qu CREATE TABLE prefix_quiz_truefalse ( id SERIAL PRIMARY KEY, question integer NOT NULL default '0', - "true" integer NOT NULL default '0', - "false" integer NOT NULL default '0' + trueanswer integer NOT NULL default '0', + falseanswer integer NOT NULL default '0' ); CREATE INDEX question_prefix_quiz_truefalse_idx ON prefix_quiz_truefalse (question); diff --git a/mod/quiz/lib.php b/mod/quiz/lib.php index 58cc00a398..bfb61ef192 100644 --- a/mod/quiz/lib.php +++ b/mod/quiz/lib.php @@ -240,7 +240,7 @@ function quiz_get_grade_records($quiz) { function quiz_get_answers($question) { // Given a question, returns the correct answers and grades global $CFG; - switch ($question->type) { + switch ($question->qtype) { case SHORTANSWER; // Could be multiple answers return get_records_sql("SELECT a.*, sa.usecase, g.grade FROM {$CFG->prefix}quiz_shortanswer sa, @@ -285,7 +285,7 @@ function quiz_get_attempt_responses($attempt) { // for regrading using quiz_grade_attempt_results() global $CFG; - if (!$responses = get_records_sql("SELECT q.id, q.type, r.answer + if (!$responses = get_records_sql("SELECT q.id, q.qtype, r.answer FROM {$CFG->prefix}quiz_responses r, {$CFG->prefix}quiz_questions q WHERE r.attempt = '$attempt->id' @@ -324,8 +324,8 @@ function quiz_print_question_icon($question) { global $QUIZ_QUESTION_TYPE; - echo "id\" TITLE=\"".$QUIZ_QUESTION_TYPE[$question->type]."\">"; - switch ($question->type) { + echo "id\" TITLE=\"".$QUIZ_QUESTION_TYPE[$question->qtype]."\">"; + switch ($question->qtype) { case SHORTANSWER: echo ""; break; @@ -350,6 +350,10 @@ function quiz_print_question($number, $questionid, $grade, $courseid, notify("Error: Question not found!"); } + if (empty($actualgrade)) { + $actualgrade = 0; + } + $stranswer = get_string("answer", "quiz"); $strmarks = get_string("marks", "quiz"); @@ -363,7 +367,7 @@ function quiz_print_question($number, $questionid, $grade, $courseid, print_spacer(1,100); echo ""; - switch ($question->type) { + switch ($question->qtype) { case SHORTANSWER: if (!$options = get_record("quiz_shortanswer", "question", $question->id)) { notify("Error: Missing question options!"); @@ -374,6 +378,8 @@ function quiz_print_question($number, $questionid, $grade, $courseid, } if ($response) { $value = "VALUE=\"$response[0]\""; + } else { + $value = ""; } echo "

$stranswer: id SIZE=20 $value>

"; if ($feedback) { @@ -389,10 +395,10 @@ function quiz_print_question($number, $questionid, $grade, $courseid, if (!$options = get_record("quiz_truefalse", "question", $question->id)) { notify("Error: Missing question options!"); } - if (!$true = get_record("quiz_answers", "id", $options->true)) { + if (!$true = get_record("quiz_answers", "id", $options->trueanswer)) { notify("Error: Missing question answers!"); } - if (!$false = get_record("quiz_answers", "id", $options->false)) { + if (!$false = get_record("quiz_answers", "id", $options->falseanswer)) { notify("Error: Missing question answers!"); } if (!$true->answer) { @@ -406,18 +412,24 @@ function quiz_print_question($number, $questionid, $grade, $courseid, print_file_picture($question->image, $courseid, 200); } - if ($response[$true->id]) { + $truechecked = ""; + $falsechecked = ""; + + if (!empty($response[$true->id])) { $truechecked = "CHECKED"; $feedbackid = $true->id; - } else if ($response[$false->id]) { + } else if (!empty($response[$false->id])) { $falsechecked = "CHECKED"; $feedbackid = $false->id; } + + $truecorrect = ""; + $falsecorrect = ""; if ($correct) { - if ($correct[$true->id]) { + if (!empty($correct[$true->id])) { $truecorrect = "CLASS=highlight"; } - if ($correct[$false->id]) { + if (!empty($correct[$false->id])) { $falsecorrect = "CLASS=highlight"; } } @@ -751,7 +763,7 @@ function quiz_print_cat_question_list($categoryid) { echo "
"; echo "$strquestion: "; - choose_from_menu($QUIZ_QUESTION_TYPE, "type", "", ""); + choose_from_menu($QUIZ_QUESTION_TYPE, "qtype", "", ""); echo "id\">"; echo ""; helpbutton("questiontypes", $strcreatenewquestion, "quiz"); @@ -1001,7 +1013,7 @@ function quiz_grade_attempt_results($quiz, $questions) { $feedback = array(); $response = array(); - switch ($question->type) { + switch ($question->qtype) { case SHORTANSWER: if ($question->answer) { $question->answer = trim($question->answer[0]); @@ -1009,9 +1021,11 @@ function quiz_grade_attempt_results($quiz, $questions) { $question->answer = ""; } $response[0] = $question->answer; + $bestshortanswer = 0; foreach($answers as $answer) { // There might be multiple right answers if ($answer->fraction > $bestshortanswer) { $correct[$answer->id] = $answer->answer; + $bestshortanswer = $answer->fraction; } if (!$answer->usecase) { // Don't compare case $answer->answer = strtolower($answer->answer); diff --git a/mod/quiz/multichoice.html b/mod/quiz/multichoice.html index 942da9df34..9a1bc4b455 100644 --- a/mod/quiz/multichoice.html +++ b/mod/quiz/multichoice.html @@ -180,7 +180,7 @@ - + "> diff --git a/mod/quiz/question.php b/mod/quiz/question.php index 2fa064c394..2f765cbf3d 100644 --- a/mod/quiz/question.php +++ b/mod/quiz/question.php @@ -7,7 +7,7 @@ optional_variable($id); - optional_variable($type); + optional_variable($qtype); optional_variable($category); if ($id) { @@ -22,7 +22,7 @@ error("This question category doesn't belong to a valid course!"); } - $type = $question->type; + $qtype = $question->qtype; } else if ($category) { @@ -34,7 +34,7 @@ } $question->category = $category->id; - $question->type = $type; + $question->qtype = $qtype; } else { error("Must specify question id or category"); @@ -116,7 +116,7 @@ // Now to save all the answers and type-specific options - switch ($question->type) { + switch ($question->qtype) { case SHORTANSWER: // Delete all the old answers delete_records("quiz_answers", "question", $question->id); @@ -180,9 +180,9 @@ } unset($options); - $options->question = $question->id; - $options->true = $true->id; - $options->false = $false->id; + $options->question = $question->id; + $options->trueanswer = $true->id; + $options->falseanswer = $false->id; if (!insert_record("quiz_truefalse", $options)) { error("Could not insert quiz truefalse options!"); } @@ -298,7 +298,7 @@ } - switch ($type) { + switch ($qtype) { case SHORTANSWER: if (!empty($question->id)) { $options = get_record("quiz_shortanswer", "question", $question->id); @@ -326,14 +326,14 @@ if (!empty($question->id)) { $options = get_record("quiz_truefalse", "question", "$question->id"); } - if (!empty($options->true)) { - $true = get_record("quiz_answers", "id", "$options->true"); + if (!empty($options->trueanswer)) { + $true = get_record("quiz_answers", "id", $options->trueanswer); } else { $true->fraction = 1; $true->feedback = ""; } - if (!empty($options->false)) { - $false = get_record("quiz_answers", "id", "$options->false"); + if (!empty($options->falseanswer)) { + $false = get_record("quiz_answers", "id", "$options->falseanswer"); } else { $false->fraction = 0; $false->feedback = ""; diff --git a/mod/quiz/shortanswer.html b/mod/quiz/shortanswer.html index 0f1f92da09..063b883cfe 100644 --- a/mod/quiz/shortanswer.html +++ b/mod/quiz/shortanswer.html @@ -159,7 +159,7 @@ - + "> diff --git a/mod/quiz/truefalse.html b/mod/quiz/truefalse.html index 308f66b246..bc43738288 100644 --- a/mod/quiz/truefalse.html +++ b/mod/quiz/truefalse.html @@ -62,7 +62,7 @@ - + "> diff --git a/mod/quiz/version.php b/mod/quiz/version.php index ebd409fa8b..55847316e2 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 = 2003010100; // The (date) version of this module +$module->version = 2003010301; // The (date) version of this module $module->cron = 0; // How often should cron check this module (seconds)? ?> -- 2.39.5