Skip to content

Commit

Permalink
Merge branch 'MDL-77912' of https://github.com/timhunt/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
snake authored and sarjona committed Jul 13, 2023
2 parents 6d8db08 + 744e6f8 commit 66e6111
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
7 changes: 7 additions & 0 deletions question/type/multichoice/questiontype.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,13 @@ public function get_random_guess_score($questiondata) {
return null;
}

if (empty($questiondata->options->answers)) {
// A multi-choice question with no choices is senseless,
// but, seemingly, it can happen (presumably as a side-effect of bugs).
// Therefore, ensure it does not lead to errors here.
return null;
}

// Single choice questions - average choice fraction.
$totalfraction = 0;
foreach ($questiondata->options->answers as $answer) {
Expand Down
13 changes: 10 additions & 3 deletions question/type/multichoice/tests/question_type_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@
/**
* Unit tests for the multiple choice question definition class.
*
* @package qtype_multichoice
* @copyright 2009 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @package qtype_multichoice
* @copyright 2009 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \qtype_multichoice
*/
class question_type_test extends \advanced_testcase {
protected $qtype;
Expand Down Expand Up @@ -72,6 +73,12 @@ public function test_get_random_guess_score() {
$this->assertEquals(0.5, $this->qtype->get_random_guess_score($q));
}

public function test_get_random_guess_score_broken_question() {
$q = $this->get_test_question_data();
$q->options->answers = [];
$this->assertNull($this->qtype->get_random_guess_score($q));
}

public function test_get_random_guess_score_multi() {
$q = $this->get_test_question_data();
$q->options->single = false;
Expand Down

0 comments on commit 66e6111

Please sign in to comment.