diff --git a/mod/quiz/tests/behat/attempt_redo_questions.feature b/mod/quiz/tests/behat/attempt_redo_questions.feature index 58e8e950eca..7c68f6b1852 100644 --- a/mod/quiz/tests/behat/attempt_redo_questions.feature +++ b/mod/quiz/tests/behat/attempt_redo_questions.feature @@ -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 diff --git a/question/engine/questionattempt.php b/question/engine/questionattempt.php index 92f57afa620..e73ec8ad581 100644 --- a/question/engine/questionattempt.php +++ b/question/engine/questionattempt.php @@ -1472,11 +1472,16 @@ class question_attempt { 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; } diff --git a/question/engine/tests/walkthrough_test.php b/question/engine/tests/walkthrough_test.php index 4dc699db9c9..a834b003aa4 100644 --- a/question/engine/tests/walkthrough_test.php +++ b/question/engine/tests/walkthrough_test.php @@ -134,7 +134,7 @@ class walkthrough_test extends \qbehaviour_walkthrough_test_base { */ 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(); @@ -156,4 +156,36 @@ class walkthrough_test extends \qbehaviour_walkthrough_test_base { $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')); + } }