This commit is contained in:
Sara Arjona 2022-06-01 17:05:11 +02:00
commit 91cc136f3d
4 changed files with 84 additions and 10 deletions

View File

@ -124,6 +124,29 @@ class qtype_match_question extends question_graded_automatically_with_countback
return null;
}
public function update_attempt_state_data_for_new_version(
question_attempt_step $oldstep, question_definition $otherversion) {
parent::update_attempt_state_data_for_new_version($oldstep, $otherversion);
// Process stems.
$mapping = array_combine(array_keys($otherversion->stems), array_keys($this->stems));
$oldstemorder = explode(',', $oldstep->get_qt_var('_stemorder'));
$newstemorder = [];
foreach ($oldstemorder as $oldid) {
$newstemorder[] = $mapping[$oldid] ?? $oldid;
}
// Process choices.
$mapping = array_combine(array_keys($otherversion->choices), array_keys($this->choices));
$oldchoiceorder = explode(',', $oldstep->get_qt_var('_choiceorder'));
$newchoiceorder = [];
foreach ($oldchoiceorder as $oldid) {
$newchoiceorder[] = $mapping[$oldid] ?? $oldid;
}
return ['_stemorder' => implode(',', $newstemorder), '_choiceorder' => implode(',', $newchoiceorder)];
}
public function get_question_summary() {
$question = $this->html_to_text($this->questiontext, $this->questiontextformat);
$stems = array();

View File

@ -273,4 +273,36 @@ class question_test extends \advanced_testcase {
$this->assertEquals(get_string('regradeissuenumchoiceschanged', 'qtype_match'),
$newm->validate_can_regrade_with_other_version($m));
}
public function test_update_attempt_state_date_from_old_version_bad() {
$m = \test_question_maker::make_question('match');
$newm = clone($m);
$newm->stems = [11 => 'Dog', 12 => 'Frog', 13 => 'Toad', 14 => 'Cat', 15 => 'Hippopotamus'];
$newm->stemformat = [11 => FORMAT_HTML, 12 => FORMAT_HTML, 13 => FORMAT_HTML, 14 => FORMAT_HTML, 15 => FORMAT_HTML];
$newm->choices = [11 => 'Mammal', 12 => 'Amphibian', 13 => 'Insect'];
$newm->right = [11 => 11, 12 => 12, 13 => 12, 14 => 11, 15 => 11];
$oldstep = new question_attempt_step();
$oldstep->set_qt_var('_stemorder', '4,1,3,2');
$oldstep->set_qt_var('_choiceorder', '2,3,1');
$this->expectExceptionMessage(get_string('regradeissuenumstemschanged', 'qtype_match'));
$newm->update_attempt_state_data_for_new_version($oldstep, $m);
}
public function test_update_attempt_state_date_from_old_version_ok() {
$m = \test_question_maker::make_question('match');
$newm = clone($m);
$newm->stems = [11 => 'Dog', 12 => 'Frog', 13 => 'Toad', 14 => 'Cat'];
$newm->stemformat = [11 => FORMAT_HTML, 12 => FORMAT_HTML, 13 => FORMAT_HTML, 14 => FORMAT_HTML];
$newm->choices = [11 => 'Mammal', 12 => 'Amphibian', 13 => 'Insect'];
$newm->right = [11 => 11, 12 => 12, 13 => 12, 14 => 11];
$oldstep = new question_attempt_step();
$oldstep->set_qt_var('_stemorder', '4,1,3,2');
$oldstep->set_qt_var('_choiceorder', '2,3,1');
$this->assertEquals(['_stemorder' => '14,11,13,12', '_choiceorder' => '12,13,11'],
$newm->update_attempt_state_data_for_new_version($oldstep, $m));
}
}

View File

@ -200,6 +200,25 @@ class walkthrough_test extends \qbehaviour_walkthrough_test_base {
$this->get_contains_select_expectation('sub1', $choices, $orderforchoice[2], false),
$this->get_contains_select_expectation('sub2', $choices, $orderforchoice[2], false),
$this->get_contains_select_expectation('sub3', $choices, $orderforchoice[1], false));
// Regrade with a new version of the question.
/** @var \qtype_match_question $oldm */
$oldm = \test_question_maker::make_question('match');
$oldm->stems = [11 => 'Dog', 12 => 'Frog', 13 => 'Toad', 14 => 'Cat'];
$oldm->stemformat = [11 => FORMAT_HTML, 12 => FORMAT_HTML, 13 => FORMAT_HTML, 14 => FORMAT_HTML];
$oldm->choices = [11 => 'Mammal', 12 => 'Amphibian', 13 => 'Insect'];
$oldm->right = [11 => 11, 12 => 12, 13 => 12, 14 => 11];
$this->quba->regrade_question($this->slot, true, null, $oldm);
// Verify.
$this->check_current_mark(4);
$this->render();
$this->assertStringContainsString('Cat', $this->currentoutput);
$this->assertStringContainsString('Insect', $this->currentoutput);
$this->assertStringNotContainsString(
get_string('deletedsubquestion', 'qtype_match'), $this->currentoutput);
$this->assertStringNotContainsString(
get_string('deletedchoice', 'qtype_match'), $this->currentoutput);
}
public function test_interactive_partial_no_submit() {

View File

@ -71,17 +71,17 @@ class walkthrough_test extends \qbehaviour_walkthrough_test_base {
new \question_pattern_expectation('/class="r1"/'));
// Regrade with a new version of the question.
$newmc = \test_question_maker::make_a_multichoice_single_question();
$newmc->answers = [
23 => $newmc->answers[13],
24 => $newmc->answers[14],
25 => $newmc->answers[15],
$oldmc = \test_question_maker::make_a_multichoice_single_question();
$oldmc->answers = [
23 => $oldmc->answers[13],
24 => $oldmc->answers[14],
25 => $oldmc->answers[15],
];
$newmc->answers[23]->fraction = 0.5;
$newmc->answers[23]->feedback = 'A is now only partially right';
$newmc->answers[24]->fraction = 1;
$newmc->answers[24]->answer = 'B is the new right answer';
$this->quba->regrade_question($this->slot, true, null, $newmc);
$oldmc->answers[23]->fraction = 0.5;
$oldmc->answers[23]->feedback = 'A is now only partially right';
$oldmc->answers[24]->fraction = 1;
$oldmc->answers[24]->answer = 'B is the new right answer';
$this->quba->regrade_question($this->slot, true, null, $oldmc);
// Verify.
$this->check_current_mark(1.5);