Skip to content

Commit

Permalink
Merge branch 'MDL-78008_400' of https://github.com/timhunt/moodle int…
Browse files Browse the repository at this point in the history
…o MOODLE_400_STABLE
  • Loading branch information
andrewnicols committed Jun 8, 2023
2 parents 382f973 + d550d13 commit 3b1205d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
9 changes: 8 additions & 1 deletion question/type/multianswer/questiontype.php
Expand Up @@ -314,11 +314,18 @@ public function get_random_guess_score($questiondata) {
$fractionsum = 0;
$fractionmax = 0;
foreach ($questiondata->options->questions as $key => $subqdata) {
if ($subqdata->qtype == 'subquestion_replacement') {
continue;
}
$fractionmax += $subqdata->defaultmark;
$fractionsum += question_bank::get_qtype(
$subqdata->qtype)->get_random_guess_score($subqdata);
}
return $fractionsum / $fractionmax;
if ($fractionmax > question_utils::MARK_TOLERANCE) {
return $fractionsum / $fractionmax;
} else {
return null;
}
}

public function move_files($questionid, $oldcontextid, $newcontextid) {
Expand Down
42 changes: 37 additions & 5 deletions question/type/multianswer/tests/question_type_test.php
Expand Up @@ -34,9 +34,10 @@
/**
* Unit tests for the multianswer question definition class.
*
* @package qtype_multianswer
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @package qtype_multianswer
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \qtype_multianswer
*/
class question_type_test extends \advanced_testcase {
/** @var qtype_multianswer instance of the question type class to test. */
Expand Down Expand Up @@ -112,15 +113,46 @@ public function test_can_analyse_responses() {
}

public function test_get_random_guess_score() {
$q = \test_question_maker::get_question_data('multianswer', 'twosubq');
$q = test_question_maker::get_question_data('multianswer', 'twosubq');
$this->assertEqualsWithDelta(0.1666667, $this->qtype->get_random_guess_score($q), 0.0000001);
}

public function test_get_random_guess_score_with_missing_subquestion() {
global $DB;
$this->resetAfterTest();

// Create a question referring to a subquesion that has got lost (which happens some time).
/** @var \core_question_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_question');
$category = $generator->create_question_category();
$question = $generator->create_question('multianswer', 'twosubq', ['category' => $category->id]);
// Add a non-existent subquestion id to the list.
$sequence = $DB->get_field('question_multianswer', 'sequence', ['question' => $question->id], MUST_EXIST);
$DB->set_field('question_multianswer', 'sequence', $sequence . ',-1', ['question' => $question->id]);

// Verify that computing the random guess score does not give an error.
$questiondata = question_bank::load_question_data($question->id);
$this->assertEqualsWithDelta(0.1666667, $this->qtype->get_random_guess_score($questiondata), 0.0000001);
}

public function test_get_random_guess_score_with_all_missing_subquestions() {
$this->resetAfterTest();

// Create a question where all subquestions are missing.
$questiondata = test_question_maker::get_question_data('multianswer', 'twosubq');
foreach ($questiondata->options->questions as $subq) {
$subq->qtype = 'subquestion_replacement';
}

// Verify that computing the random guess score does not give an error.
$this->assertNull($this->qtype->get_random_guess_score($questiondata));
}

public function test_load_question() {
$this->resetAfterTest();

$syscontext = \context_system::instance();
/** @var core_question_generator $generator */
/** @var \core_question_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_question');
$category = $generator->create_question_category(['contextid' => $syscontext->id]);

Expand Down

0 comments on commit 3b1205d

Please sign in to comment.