mirror of
https://github.com/moodle/moodle.git
synced 2025-04-13 12:32:08 +02:00
MDL-77912 questions: make multi-choice stats calc robust to bad data
This commit is contained in:
parent
f29f757f5f
commit
0ece3bcfc1
@ -244,6 +244,13 @@ class qtype_multichoice extends question_type {
|
||||
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) {
|
||||
|
@ -31,9 +31,10 @@ require_once($CFG->dirroot . '/question/type/multichoice/edit_multichoice_form.p
|
||||
/**
|
||||
* 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;
|
||||
@ -72,6 +73,12 @@ class question_type_test extends \advanced_testcase {
|
||||
$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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user