diff --git a/question/type/numerical/db/upgrade.php b/question/type/numerical/db/upgrade.php index 84f2047f2e6..327cbcbb8e7 100644 --- a/question/type/numerical/db/upgrade.php +++ b/question/type/numerical/db/upgrade.php @@ -120,7 +120,7 @@ function xmldb_qtype_numerical_upgrade($oldversion) { qnu.unit AS defaultunit FROM {question} q - FROM {question_categories} qc ON qc.id = q.category + JOIN {question_categories} qc ON qc.id = q.category JOIN {question_numerical_options} qno ON qno.question = q.id JOIN {question_numerical_units} qnu ON qnu.id = ( SELECT min(id) diff --git a/question/type/numerical/lang/en/qtype_numerical.php b/question/type/numerical/lang/en/qtype_numerical.php index abe1c0600b9..ebaf7ce52d9 100644 --- a/question/type/numerical/lang/en/qtype_numerical.php +++ b/question/type/numerical/lang/en/qtype_numerical.php @@ -76,6 +76,7 @@ $string['unitchoice'] = 'a multiple choice selection'; $string['unitedit'] = 'Edit unit'; $string['unitgraded'] = 'The unit must be given, and will be graded.'; $string['unithdr'] = 'Unit {$a}'; +$string['unitincorrect'] = 'You did not give the correct unit.'; $string['unitmandatory'] = 'Mandatory'; $string['unitmandatory_help'] = ' @@ -92,7 +93,6 @@ $string['unitoptional_help'] = ' * If the unit is badly written or unknown, the response will be considered as non valid. '; $string['unitnotvalid'] = ' Unit not valid with this numerical value'; -$string['unitunknown'] = ' Undefined unit '; $string['unitpenalty'] = 'Unit penalty'; $string['unitpenalty_help'] = 'The penalty is applied if diff --git a/question/type/numerical/question.php b/question/type/numerical/question.php index 60453ea6102..9c6210ee8b2 100644 --- a/question/type/numerical/question.php +++ b/question/type/numerical/question.php @@ -155,7 +155,7 @@ class qtype_numerical_question extends question_graded_automatically { return null; } - protected function apply_unit_penalty($fraction, $unit) { + public function apply_unit_penalty($fraction, $unit) { if (!empty($unit) && $this->ap->is_known_unit($unit)) { return $fraction; } diff --git a/question/type/numerical/questiontype.php b/question/type/numerical/questiontype.php index a3cf6f8426e..44708eb09d4 100644 --- a/question/type/numerical/questiontype.php +++ b/question/type/numerical/questiontype.php @@ -363,16 +363,16 @@ class qtype_numerical extends question_type { } protected function make_answer_processor($units, $unitsleft) { - if (empty($questiondata->options->units)) { + if (empty($units)) { return new qtype_numerical_answer_processor(array()); } - $units = array(); - foreach ($questiondata->options->units as $unit) { - $units[$unit->unit] = $unit->multiplier; + $cleanedunits = array(); + foreach ($units as $unit) { + $cleanedunits[$unit->unit] = $unit->multiplier; } - return new qtype_numerical_answer_processor($units, $questiondata->options->unitsleft); + return new qtype_numerical_answer_processor($cleanedunits, $unitsleft); } function delete_question($questionid, $contextid) { diff --git a/question/type/numerical/renderer.php b/question/type/numerical/renderer.php index a0ccec77f7d..cac0543cc85 100644 --- a/question/type/numerical/renderer.php +++ b/question/type/numerical/renderer.php @@ -56,7 +56,7 @@ class qtype_numerical_renderer extends qtype_renderer { list($value, $unit) = $question->ap->apply_units($currentanswer); $answer = $question->get_matching_answer($value); if ($answer) { - $fraction = $answer->fraction; + $fraction = $question->apply_unit_penalty($answer->fraction, $unit); } else { $fraction = 0; } @@ -101,12 +101,19 @@ class qtype_numerical_renderer extends qtype_renderer { list($value, $unit) = $question->ap->apply_units($qa->get_last_qt_var('answer')); $answer = $question->get_matching_answer($value); - if (!$answer || !$answer->feedback) { - return ''; + + if ($answer && $answer->feedback) { + $feedback = $question->format_text($answer->feedback, $answer->feedbackformat, + $qa, 'question', 'answerfeedback', $answer->id); + } else { + $feedback = ''; } - return $question->format_text($answer->feedback, $answer->feedbackformat, - $qa, 'question', 'answerfeedback', $answer->id); + if ($question->unitgradingtype && !$question->ap->is_known_unit($unit)) { + $feedback .= html_writer::tag('p', get_string('unitincorrect', 'qtype_numerical')); + } + + return $feedback; } public function correct_response(question_attempt $qa) {