MDL-77225 questions: fix regrade of descriptions in finished attempts

The added logic is similar to the logic in other behaviours, and the new test fails without it.
This commit is contained in:
Tim Hunt 2023-02-13 15:36:24 +00:00
parent f49f934786
commit 7db1da61ee
2 changed files with 83 additions and 1 deletions

View File

@ -104,12 +104,47 @@ class qbehaviour_informationitem extends question_behaviour {
return parent::process_comment($pendingstep);
}
/**
* Handle the 'finish' case of {@see process_action()}.
*
* @param question_attempt_pending_step $pendingstep step representing the action.
* @return bool either {@see question_attempt::KEEP} or {@see question_attempt::DISCARD}.
*/
public function process_finish(question_attempt_pending_step $pendingstep) {
if ($this->qa->get_state()->is_finished()) {
return question_attempt::DISCARD;
}
if (!$this->qa->get_state()->is_active()) {
throw new coding_exception('It should be impossible for question_attempt ' . $this->qa->get_database_id() .
' using ' . get_class($this) . " to receive a 'finish' action while not active. " .
'Failing with an error, rather than continuing and doing undefined processing, ' .
'so the bug can be identified.');
}
$pendingstep->set_state(question_state::$finished);
return question_attempt::KEEP;
}
/**
* Handle the 'seen' case of {@see process_action()}.
*
* @param question_attempt_pending_step $pendingstep step representing the action.
* @return bool either {@see question_attempt::KEEP} or {@see question_attempt::DISCARD}.
*/
public function process_seen(question_attempt_pending_step $pendingstep) {
if ($this->qa->get_state()->is_finished()) {
return question_attempt::DISCARD;
}
// Assert so we get a clear error message if the assumptions on which this code relies is invalid.
if (!$this->qa->get_state()->is_active()) {
throw new coding_exception('It should be impossible for question_attempt ' . $this->qa->get_database_id() .
' using ' . get_class($this) . " to receive a 'seen' action while not active. " .
'Failing with an error, rather than continuing and doing undefined processing, ' .
'so the bug can be identified.');
}
$pendingstep->set_state(question_state::$complete);
return question_attempt::KEEP;
}

View File

@ -32,6 +32,7 @@ require_once(__DIR__ . '/../../../engine/tests/helpers.php');
* @category test
* @copyright 2009 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \qbehaviour_informationitem
*/
class walkthrough_test extends \qbehaviour_walkthrough_test_base {
public function test_informationitem_feedback_description() {
@ -56,7 +57,7 @@ class walkthrough_test extends \qbehaviour_walkthrough_test_base {
$this->displayoptions->readonly = false;
// Process a submission indicating this question has been seen.
$this->process_submission(array('-seen' => 1));
$this->process_submission(['-seen' => 1]);
$this->check_current_state(question_state::$complete);
$this->check_current_mark(null);
@ -87,4 +88,50 @@ class walkthrough_test extends \qbehaviour_walkthrough_test_base {
$this->expectException('moodle_exception');
$this->manual_grade('Not good enough!', 1, FORMAT_HTML);
}
public function test_informationitem_regrade() {
// Create a true-false question with correct answer true.
$description = \test_question_maker::make_question('description');
$this->start_attempt_at_question($description, 'deferredfeedback');
// Check the initial state.
$this->check_current_state(question_state::$todo);
$this->check_current_mark(null);
$this->check_current_output($this->get_contains_question_text_expectation($description),
$this->get_contains_hidden_expectation(
$this->quba->get_field_prefix($this->slot) . '-seen', 1),
$this->get_does_not_contain_feedback_expectation());
// Process a submission indicating this question has been seen.
$this->process_submission(['-seen' => 1]);
$this->check_current_state(question_state::$complete);
$this->check_current_mark(null);
$this->check_current_output($this->get_does_not_contain_correctness_expectation(),
new \question_no_pattern_expectation(
'/type=\"hidden\"[^>]*name=\"[^"]*seen\"|name=\"[^"]*seen\"[^>]*type=\"hidden\"/'),
$this->get_does_not_contain_feedback_expectation());
// Finish the attempt.
$this->quba->finish_all_questions();
// Verify.
$this->check_current_state(question_state::$finished);
$this->check_current_mark(null);
$this->check_step_count(3);
$this->check_current_output(
$this->get_contains_question_text_expectation($description),
$this->get_contains_general_feedback_expectation($description));
// Regrade the attempt.
$this->quba->regrade_all_questions(true);
// Verify.
$this->check_current_mark(null);
$this->check_step_count(3);
$this->check_current_output(
$this->get_contains_question_text_expectation($description),
$this->get_contains_general_feedback_expectation($description));
}
}