MDL-30484 question engine: don't lose response files when regrading.
The problem was mostly that, in the past, we did not worry if question_attempt_step.id changed during regrade (because we deleted the old step row and inserted a new one). However, now that steps can have associated files, we can't be that slack, becuase the step id is used as the file itemid. So, now, we have to update the existing rows during a regrade. We do this by having the question engine tell the question_engine_unit_of_work that the step has first been deleted, and then added back. Then we make the unit-of-work spot that delete + add = update. This also means that during regrading, we have to pass around some extra ids so that new steps know the id of the step they are replacing. Naturally, this requires some quite trickly logic, so I finally got around to writing unit tests for question_engine_unit_of_work, which is a good thing. Along the way I also got around to renaming question_attempt->set_number_in_usage, which got missed out when everthing else was renamed to slot ages ago. Finally, while working on this code, I noticed and fixed some PHPdoc comments.
This commit is contained in:
@@ -158,4 +158,66 @@ class question_usage_by_activity_test extends UnitTestCase {
|
||||
$this->expectException('question_out_of_sequence_exception');
|
||||
$quba->process_all_actions($slot, $postdata);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unit tests for loading data into the {@link question_usage_by_activity} class.
|
||||
*
|
||||
* @copyright 2012 The Open University
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class question_usage_db_test extends data_loading_method_test_base {
|
||||
public function test_load() {
|
||||
$records = new test_recordset(array(
|
||||
array('qubaid', 'contextid', 'component', 'preferredbehaviour',
|
||||
'questionattemptid', 'contextid', 'questionusageid', 'slot',
|
||||
'behaviour', 'questionid', 'variant', 'maxmark', 'minfraction', 'flagged',
|
||||
'questionsummary', 'rightanswer', 'responsesummary', 'timemodified',
|
||||
'attemptstepid', 'sequencenumber', 'state', 'fraction',
|
||||
'timecreated', 'userid', 'name', 'value'),
|
||||
array(1, 1, 'unit_test', 'interactive', 1, 123, 1, 1, 'interactive', -1, 1, 2.0000000, 0.0000000, 0, '', '', '', 1256233790, 1, 0, 'todo', null, 1256233700, 1, null, null),
|
||||
array(1, 1, 'unit_test', 'interactive', 1, 123, 1, 1, 'interactive', -1, 1, 2.0000000, 0.0000000, 0, '', '', '', 1256233790, 2, 1, 'todo', null, 1256233705, 1, 'answer', '1'),
|
||||
array(1, 1, 'unit_test', 'interactive', 1, 123, 1, 1, 'interactive', -1, 1, 2.0000000, 0.0000000, 0, '', '', '', 1256233790, 5, 2, 'gradedright', 1.0000000, 1256233720, 1, '-finish', '1'),
|
||||
));
|
||||
|
||||
$question = test_question_maker::make_question('truefalse', 'true');
|
||||
$question->id = -1;
|
||||
|
||||
question_bank::start_unit_test();
|
||||
question_bank::load_test_question_data($question);
|
||||
$quba = question_usage_by_activity::load_from_records($records, 1);
|
||||
question_bank::end_unit_test();
|
||||
|
||||
$this->assertEqual('unit_test', $quba->get_owning_component());
|
||||
$this->assertEqual(1, $quba->get_id());
|
||||
$this->assertIsA($quba->get_observer(), 'question_engine_unit_of_work');
|
||||
$this->assertEqual('interactive', $quba->get_preferred_behaviour());
|
||||
|
||||
$qa = $quba->get_question_attempt(1);
|
||||
|
||||
$this->assertEqual($question->questiontext, $qa->get_question()->questiontext);
|
||||
|
||||
$this->assertEqual(3, $qa->get_num_steps());
|
||||
|
||||
$step = $qa->get_step(0);
|
||||
$this->assertEqual(question_state::$todo, $step->get_state());
|
||||
$this->assertNull($step->get_fraction());
|
||||
$this->assertEqual(1256233700, $step->get_timecreated());
|
||||
$this->assertEqual(1, $step->get_user_id());
|
||||
$this->assertEqual(array(), $step->get_all_data());
|
||||
|
||||
$step = $qa->get_step(1);
|
||||
$this->assertEqual(question_state::$todo, $step->get_state());
|
||||
$this->assertNull($step->get_fraction());
|
||||
$this->assertEqual(1256233705, $step->get_timecreated());
|
||||
$this->assertEqual(1, $step->get_user_id());
|
||||
$this->assertEqual(array('answer' => '1'), $step->get_all_data());
|
||||
|
||||
$step = $qa->get_step(2);
|
||||
$this->assertEqual(question_state::$gradedright, $step->get_state());
|
||||
$this->assertEqual(1, $step->get_fraction());
|
||||
$this->assertEqual(1256233720, $step->get_timecreated());
|
||||
$this->assertEqual(1, $step->get_user_id());
|
||||
$this->assertEqual(array('-finish' => '1'), $step->get_all_data());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user