Index: questiontype.php =================================================================== RCS file: /cvsroot/moodle/moodle/question/type/questiontype.php,v retrieving revision 1.161 diff -u -r1.161 questiontype.php --- questiontype.php 21 Sep 2010 08:20:48 -0000 1.161 +++ questiontype.php 1 Oct 2010 17:54:00 -0000 @@ -1474,11 +1474,9 @@ // This should be good enought for most simple question types. $state->raw_grade = 0; - foreach($question->options->answers as $answer) { - if($this->test_response($question, $state, $answer)) { - $state->raw_grade = $answer->fraction; - break; - } + $answer = $this->get_matching_answer($question, $state); + if ($answer !== null) { + $state->raw_grade = $answer->fraction; } // Make sure we don't assign negative or too high marks. @@ -1495,6 +1493,25 @@ } /** + * Returns answer object for given response + * + * Useful for grading and displaying appropriate feedback (removing code duplication there) + * Basic implementation is good for questions defining test_response, other question types may want to reimplement it + * Completely useless for questions where several answers may match one response (like multichoice in multiple selection mode) + * @param object question Question object + * @param object state The state of the question to grade + * @return answer object or null of there is none + */ + function get_matching_answer($question, $state) { + foreach ($question->options->answers as $answer) { + if ($this->test_response($question, $state, $answer)) { + return $answer; + } + } + return null; + } + + /** * Returns true if the editing wizard is finished, false otherwise. * * The default implementation returns true, which is suitable for all question- Index: shortanswer/questiontype.php =================================================================== RCS file: /cvsroot/moodle/moodle/question/type/shortanswer/questiontype.php,v retrieving revision 1.56 diff -u -r1.56 questiontype.php --- shortanswer/questiontype.php 21 Sep 2010 08:20:48 -0000 1.56 +++ shortanswer/questiontype.php 1 Oct 2010 18:07:33 -0000 @@ -188,17 +188,13 @@ $class = question_get_feedback_class(0); $feedbackimg = question_get_feedback_image(0); //this is OK for the first answer with a good response - foreach($question->options->answers as $answer) { - - if ($this->test_response($question, $state, $answer)) { - // Answer was correct or partially correct. - $class = question_get_feedback_class($answer->fraction); - $feedbackimg = question_get_feedback_image($answer->fraction); - if ($answer->feedback) { - $answer->feedback = quiz_rewrite_question_urls($answer->feedback, 'pluginfile.php', $context->id, 'question', 'answerfeedback', array($state->attempt, $state->question), $answer->id); - $feedback = format_text($answer->feedback, true, $formatoptions, $cmoptions->course); - } - break; + $answer = $this->get_matching_answer($question, $state); + if ($answer !== null) { + $class = question_get_feedback_class($answer->fraction); + $feedbackimg = question_get_feedback_image($answer->fraction); + if ($answer->feedback) { + $answer->feedback = quiz_rewrite_question_urls($answer->feedback, 'pluginfile.php', $context->id, 'question', 'answerfeedback', array($state->attempt, $state->question), $answer->id); + $feedback = format_text($answer->feedback, true, $formatoptions, $cmoptions->course); } } }