Skip to content

Commit

Permalink
MDL-77464 questions: regrading was losing question_attempt metadata
Browse files Browse the repository at this point in the history
This is very similar to MDL-77090, but at the time, I missed that this
also needed to be handled. (Question metadata is, I think, only used
by the quiz 'Try another question like this one' feature.)
  • Loading branch information
timhunt committed Mar 7, 2023
1 parent 33e98af commit 1397ce3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
4 changes: 4 additions & 0 deletions mod/quiz/tests/behat/attempt_redo_questions.feature
Expand Up @@ -55,6 +55,10 @@ Feature: Allow students to redo questions in a practice quiz, without starting a
Then I should see "Finished regrading (1/1)"
And I should see "Regrade completed"
And I press "Continue"
# Regrade a second time, to ensure the first regrade did not corrupt any data.
And I press "Regrade all"
And I should see "Finished regrading (1/1)"
And I should see "Regrade completed"

@javascript
Scenario: Start attempt, teacher edits question, redo picks up latest non-draft version
Expand Down
9 changes: 7 additions & 2 deletions question/engine/questionattempt.php
Expand Up @@ -1472,11 +1472,16 @@ protected function get_attempt_state_data_to_regrade_with_version(question_attem
if ($this->get_question(false) === $otherversion) {
return $oldstep->get_all_data();
} else {
// Update the data belonging to the question type by asking the question to do it.
$attemptstatedata = $this->get_question(false)->update_attempt_state_data_for_new_version(
$oldstep, $otherversion);

foreach ($oldstep->get_behaviour_data() as $name => $value) {
$attemptstatedata['-' . $name] = $value;
// Then copy over all the behaviour and metadata variables.
// This terminology is explained in the class comment on {@see question_attempt_step}.
foreach ($oldstep->get_all_data() as $name => $value) {
if (substr($name, 0, 1) === '-' || substr($name, 0, 2) === ':_') {
$attemptstatedata[$name] = $value;
}
}
return $attemptstatedata;
}
Expand Down
34 changes: 33 additions & 1 deletion question/engine/tests/walkthrough_test.php
Expand Up @@ -134,7 +134,7 @@ public function test_action_author_with_display_options_testcase() {
*/
public function test_regrading_an_interactive_attempt_while_in_progress() {

// Start at attempt at a matching question.
// Start an attempt at a matching question.
$q = test_question_maker::make_question('match');
$this->start_attempt_at_question($q, 'interactive', 1);
$this->save_quba();
Expand All @@ -156,4 +156,36 @@ public function test_regrading_an_interactive_attempt_while_in_progress() {
$this->check_step_count(1);
$this->check_current_output($this->get_tries_remaining_expectation(1));
}

/**
* @covers \question_usage_by_activity::regrade_question
* @covers \question_attempt::regrade
* @covers \question_attempt::get_attempt_state_data_to_regrade_with_version
*/
public function test_regrading_does_not_lose_metadata() {

// Start an attempt at a matching question.
$q = test_question_maker::make_question('match');
$this->start_attempt_at_question($q, 'interactive', 1);
// Like in process_redo_question in mod_quiz.
$this->quba->set_question_attempt_metadata($this->slot, 'originalslot', 42);
$this->save_quba();

// Verify.
$this->check_current_state(question_state::$todo);
$this->check_current_mark(null);
$this->check_step_count(1);
$this->check_current_output($this->get_tries_remaining_expectation(1));

// Regrade the attempt.
$reloadedquestion = clone($q);
$this->quba->regrade_question($this->slot, false, null, $reloadedquestion);

// Verify.
$this->check_current_state(question_state::$todo);
$this->check_current_mark(null);
$this->check_step_count(1);
$this->check_current_output($this->get_tries_remaining_expectation(1));
$this->assertEquals(42, $this->quba->get_question_attempt_metadata($this->slot, 'originalslot'));
}
}

0 comments on commit 1397ce3

Please sign in to comment.