From 09db6da231412e446984075bfd3da83ebd8127fe Mon Sep 17 00:00:00 2001 From: tjhunt Date: Mon, 18 Dec 2006 16:38:58 +0000 Subject: [PATCH] MDL-7972 - improve GIFT import export for truefalse questions. Also clean up whitespace in question/format/gift/format.php. MDL-7928 - Numerical question type should use * for the match anything answer - GIFT format part. - Merged from MOODLE_17_STABLE. --- lang/en_utf8/help/quiz/formatgift.html | 6 +- question/format/gift/format.php | 148 +++++++++++-------------- 2 files changed, 70 insertions(+), 84 deletions(-) diff --git a/lang/en_utf8/help/quiz/formatgift.html b/lang/en_utf8/help/quiz/formatgift.html index 3b566cc144..8894693067 100644 --- a/lang/en_utf8/help/quiz/formatgift.html +++ b/lang/en_utf8/help/quiz/formatgift.html @@ -127,11 +127,11 @@ =no one#excellent answer! =nobody#excellent answer!} - Grant is buried in Grant's tomb.{FALSE#No one is buried in Grant's tomb.} + Grant is buried in Grant's tomb.{FALSE#Wrong, No one is buried in Grant's tomb.#Right, well done.}

For Multiple Choice questions, feedback is displayed only for the answer the student selected. For short answer, feedback is shown only when students input the corresponding correct answer. - For true-false questions, the imported feedback is saved so that it will display if the student chose the wrong answer. - So, in the last example above, the student would see the feedback only if they selected TRUE as their answer.

+ For true-false questions, there can be one or two feedback strings. The first is shown if the + student gives the wrong answer. The second if the student gives the right answer.

Percentage Answer Weights:
Percentage answer weights are available for both Multiple Choice and Short Answer questions. diff --git a/question/format/gift/format.php b/question/format/gift/format.php index 6d23a28dd7..fc6e3fe266 100755 --- a/question/format/gift/format.php +++ b/question/format/gift/format.php @@ -35,11 +35,11 @@ class qformat_gift extends qformat_default { function provide_import() { - return true; + return true; } function provide_export() { - return true; + return true; } function answerweightparser(&$answer) { @@ -65,18 +65,16 @@ class qformat_gift extends qformat_default { } function split_truefalse_comment($comment){ - // splits up comment around # marks - // returns an array of true/false feedback - $feedback = explode('#',$comment); - if (count($feedback)>=2) { - $true_feedback = $feedback[0]; - $false_feedback = $feedback[1]; - } - else { - $true_feedback = $feedback[0]; - $false_feedback = ''; - } - return array( 'true'=>$true_feedback, 'false'=>$false_feedback ); + // splits up comment around # marks + // returns an array of true/false feedback + $bits = explode('#',$comment); + $feedback = array('wrong' => $bits[0]); + if (count($bits) >= 2) { + $feedback['right'] = $bits[1]; + } else { + $feedback['right'] = ''; + } + return $feedback; } function escapedchar_pre($string) { @@ -100,17 +98,17 @@ class qformat_gift extends qformat_default { } function check_answer_count( $min, $answers, $text ) { - $countanswers = count($answers); - if ($countanswers < $min) { - if ($this->displayerrors) { - $errormessage = get_string( 'importminerror', 'quiz' ); - echo "

$text

\n"; - echo "

$errormessage

\n"; - } - return false; - } - - return true; + $countanswers = count($answers); + if ($countanswers < $min) { + if ($this->displayerrors) { + $errormessage = get_string( 'importminerror', 'quiz' ); + echo "

$text

\n"; + echo "

$errormessage

\n"; + } + return false; + } + + return true; } @@ -125,11 +123,11 @@ class qformat_gift extends qformat_default { // REMOVED COMMENTED LINES and IMPLODE foreach ($lines as $key => $line) { - $line = trim($line); - if (substr($line, 0, 2) == "//") { + $line = trim($line); + if (substr($line, 0, 2) == "//") { // echo "Commented line removed.
"; $lines[$key] = " "; - } + } } $text = trim(implode(" ", $lines)); @@ -160,7 +158,7 @@ class qformat_gift extends qformat_default { if ($namefinish === false) { $question->name = false; // name will be assigned after processing question text below - } else { + } else { $questionname = substr($text, 0, $namefinish); $question->name = addslashes(trim($this->escapedchar_post($questionname))); $text = trim(substr($text, $namefinish+2)); // Remove name from text @@ -203,13 +201,13 @@ class qformat_gift extends qformat_default { $oldquestiontext = $questiontext; $questiontextformat = 0; if (substr($questiontext,0,1)=='[') { - $questiontext = substr( $questiontext,1 ); - $rh_brace = strpos( $questiontext, ']' ); - $qtformat= substr( $questiontext, 0, $rh_brace ); - $questiontext = substr( $questiontext, $rh_brace+1 ); - if (!$questiontextformat = text_format_name( $qtformat )) { - $questiontext = $oldquestiontext; - } + $questiontext = substr( $questiontext,1 ); + $rh_brace = strpos( $questiontext, ']' ); + $qtformat= substr( $questiontext, 0, $rh_brace ); + $questiontext = substr( $questiontext, $rh_brace+1 ); + if (!$questiontextformat = text_format_name( $qtformat )) { + $questiontext = $oldquestiontext; + } } $question->questiontextformat = $questiontextformat; $question->questiontext = addslashes(trim($this->escapedchar_post($questiontext))); @@ -222,7 +220,7 @@ class qformat_gift extends qformat_default { // ensure name is not longer than 250 characters $question->name = shorten_text( $question->name, 250 ); - // determine QUESTION TYPE + // determine QUESTION TYPE $question->qtype = NULL; if ($answertext{0} == "#"){ @@ -233,8 +231,8 @@ class qformat_gift extends qformat_default { $question->qtype = MULTICHOICE; } elseif (strpos($answertext, "=") !== false - AND strpos($answertext, "->") !== false) { - // only Matching contains both = and -> + && strpos($answertext, "->") !== false) { + // only Matching contains both = and -> $question->qtype = MATCH; } else { // either TRUEFALSE or SHORTANSWER @@ -352,16 +350,16 @@ class qformat_gift extends qformat_default { case TRUEFALSE: $answer = $answertext; $comment = $this->commentparser($answer); // commentparser also removes comment from $answer - $feedback = $this->split_truefalse_comment( $comment ); + $feedback = $this->split_truefalse_comment($comment); if ($answer == "T" OR $answer == "TRUE") { $question->answer = 1; - $question->feedbackfalse = $feedback['true'];; //feedback if answer is wrong - $question->feedbacktrue = $feedback['false']; // make sure this exists to stop notifications + $question->feedbacktrue = $feedback['right']; + $question->feedbackfalse = $feedback['wrong']; } else { $question->answer = 0; - $question->feedbacktrue = $feedback['true']; //feedback if answer is wrong - $question->feedbackfalse = $feedback['false']; // make sure this exists to stop notifications + $question->feedbackfalse = $feedback['right']; + $question->feedbacktrue = $feedback['wrong']; } //$question->defaultgrade = 1; @@ -461,8 +459,7 @@ class qformat_gift extends qformat_default { $ans = trim($answer); } - if (!is_numeric($ans) - OR !is_numeric($tol)) { + if (!(is_numeric($ans) || $ans = '*') || !is_numeric($tol)) { if ($this->displayerrors) { $err = get_string( 'errornotnumbers' ); echo "

$text

$err

@@ -511,7 +508,7 @@ function repchar( $text, $format=0 ) { $newtext = str_replace( $reserved, $escaped, $text ); $format = 0; // turn this off for now if ($format) { - $newtext = format_text( $format ); + $newtext = format_text( $format ); } return $newtext; } @@ -530,46 +527,35 @@ function writequestion( $question ) { $textformat = $question->questiontextformat; $tfname = ""; if ($textformat!=FORMAT_MOODLE) { - $tfname = text_format_name( (int)$textformat ); - $tfname = "[$tfname]"; + $tfname = text_format_name( (int)$textformat ); + $tfname = "[$tfname]"; } // output depends on question type switch($question->qtype) { case TRUEFALSE: - $answers = $question->options->answers; - foreach ($answers as $answer) { - if (trim($answer->answer)=='True') { - if ($answer->fraction==1) { - $answertext = 'TRUE'; - $right_feedback = $answer->feedback; - } - else { - $answertext = 'FALSE'; - $wrong_feedback = $answer->feedback; - } - } - else { - if ($answer->fraction==1) { - $answertext = 'FALSE'; - $right_feedback = $answer->feedback; - } - else { - $answertext = 'TRUE'; - $wrong_feedback = $answer->feedback; - } - } + $trueanswer = $question->options->answers[$question->options->trueanswer]; + $falseanswer = $question->options->answers[$question->options->falseanswer]; + if ($trueanswer->fraction == 1) { + $answertext = 'TRUE'; + $right_feedback = $trueanswer->feedback; + $wrong_feedback = $falseanswer->feedback; + } else { + $answertext = 'FALSE'; + $right_feedback = $falseanswer->feedback; + $wrong_feedback = $trueanswer->feedback; } - $wrong_feedback = $this->repchar( $wrong_feedback ); - $right_feedback = $this->repchar( $right_feedback ); - $expout .= "::".$this->repchar($question->name)."::".$tfname.$this->repchar( $question->questiontext,$textformat ); - $expout .= "{".$this->repchar( $answertext ); - if ($wrong_feedback!="") { - $expout .= "#".$wrong_feedback; + $wrong_feedback = $this->repchar($wrong_feedback); + $right_feedback = $this->repchar($right_feedback); + $expout .= "::".$this->repchar($question->name)."::".$tfname.$this->repchar( $question->questiontext,$textformat )."{".$this->repchar( $answertext ); + if ($wrong_feedback) { + $expout .= "#" . $wrong_feedback; + } else if ($right_feedback) { + $expout .= "#"; } - if ($right_feedback!="") { - $expout .= "#".$right_feedback; + if ($right_feedback) { + $expout .= "#" . $right_feedback; } $expout .= "}\n"; break; @@ -583,8 +569,8 @@ function writequestion( $question ) { $answertext = '~'; } else { - $export_weight = $answer->fraction*100; - $answertext = "~%$export_weight%"; + $export_weight = $answer->fraction*100; + $answertext = "~%$export_weight%"; } $expout .= "\t".$answertext.$this->repchar( $answer->answer ); if ($answer->feedback!="") { -- 2.39.5