Skip to content

Commit

Permalink
Merge branch 'MDL-77378_402_v3' of https://github.com/TomoTsuyuki/moodle
Browse files Browse the repository at this point in the history
 into MOODLE_402_STABLE
  • Loading branch information
junpataleta authored and HuongNV13 committed Aug 10, 2023
2 parents 82c04ab + 1d1b590 commit 5aac33d
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
9 changes: 9 additions & 0 deletions question/type/multianswer/question.php
Expand Up @@ -147,6 +147,9 @@ public function get_min_fraction() {
$fractionmax += $subq->defaultmark;
$fractionsum += $subq->defaultmark * $subq->get_min_fraction();
}
if (empty($fractionsum)) {
return 0;
}
return $fractionsum / (!empty($this->subquestions) ? $fractionmax : 1);
}

Expand All @@ -157,6 +160,9 @@ public function get_max_fraction() {
$fractionmax += $subq->defaultmark;
$fractionsum += $subq->defaultmark * $subq->get_max_fraction();
}
if (empty($fractionsum)) {
return 1;
}
return $fractionsum / (!empty($this->subquestions) ? $fractionmax : 1);
}

Expand Down Expand Up @@ -299,6 +305,9 @@ public function grade_response(array $response) {
$overallstate = $this->combine_states($overallstate, $newstate);
}
}
if (empty($fractionmax)) {
return array(null, $overallstate ?? question_state::$finished);
}
return array($fractionsum / $fractionmax, $overallstate);
}

Expand Down
46 changes: 45 additions & 1 deletion question/type/multianswer/tests/helper.php
Expand Up @@ -37,7 +37,7 @@
*/
class qtype_multianswer_test_helper extends question_test_helper {
public function get_test_questions() {
return array('twosubq', 'fourmc', 'numericalzero', 'dollarsigns', 'multiple');
return array('twosubq', 'fourmc', 'numericalzero', 'dollarsigns', 'multiple', 'zeroweight');
}

/**
Expand Down Expand Up @@ -484,4 +484,48 @@ public function make_multianswer_question_multiple() {
return $q;
}

/**
* Makes a multianswer question with zero weight.
* This is used for testing the MDL-77378 bug.
* @return qtype_multianswer_question
*/
public function make_multianswer_question_zeroweight() {
question_bank::load_question_definition_classes('multianswer');
$q = new qtype_multianswer_question();
test_question_maker::initialise_a_question($q);
$q->name = 'Zero weight';
$q->questiontext =
'Optional question: {#1}.';
$q->generalfeedback = '';
$q->qtype = question_bank::get_qtype('multianswer');
$q->textfragments = array(
'Optional question: ',
'.',
);
$q->places = array('1' => '1');

// Shortanswer subquestion.
question_bank::load_question_definition_classes('shortanswer');
$sa = new qtype_shortanswer_question();
test_question_maker::initialise_a_question($sa);
$sa->name = 'Zero weight';
$sa->questiontext = '{0:SHORTANSWER:~%0%Input box~%100%*}';
$sa->questiontextformat = FORMAT_HTML;
$sa->generalfeedback = '';
$sa->generalfeedbackformat = FORMAT_HTML;
$sa->usecase = true;
$sa->answers = array(
13 => new question_answer(13, 'Input box', 0.0, '', FORMAT_HTML),
14 => new question_answer(14, '*', 1.0, '', FORMAT_HTML),
);
$sa->qtype = question_bank::get_qtype('shortanswer');
$sa->defaultmark = 0;

$q->subquestions = array(
1 => $sa,
);

return $q;
}

}
19 changes: 19 additions & 0 deletions question/type/multianswer/tests/question_test.php
Expand Up @@ -350,4 +350,23 @@ public function test_update_attempt_state_date_from_old_version_ok() {
$newquestion->update_attempt_state_data_for_new_version($oldstep, $question));
}

/**
* Test functions work with zero weight.
* This is used for testing the MDL-77378 bug.
*/
public function test_zeroweight() {
$this->resetAfterTest();
/** @var \qtype_multianswer_question $question */
$question = \test_question_maker::make_question('multianswer', 'zeroweight');
$question->start_attempt(new question_attempt_step(), 1);

$this->assertEquals([null, question_state::$gradedright], $question->grade_response(
['sub1_answer' => 'Something']));
$this->assertEquals([null, question_state::$gradedwrong], $question->grade_response(
['sub1_answer' => 'Input box']));

$this->assertEquals(1, $question->get_max_fraction());
$this->assertEquals(0, $question->get_min_fraction());
}

}

0 comments on commit 5aac33d

Please sign in to comment.