MDL-66685 questions: should able to save an empty question usage

This commit is contained in:
Tim Hunt 2019-09-16 14:35:18 +01:00
parent 1c3efe48f8
commit a3624fdf0f
2 changed files with 40 additions and 8 deletions

View file

@ -100,10 +100,7 @@ class question_engine_data_mapper {
$stepdata[] = $this->insert_question_attempt($qa, $quba->get_owning_context()); $stepdata[] = $this->insert_question_attempt($qa, $quba->get_owning_context());
} }
$stepdata = call_user_func_array('array_merge', $stepdata); $this->insert_all_step_data($this->combine_step_data($stepdata));
if ($stepdata) {
$this->insert_all_step_data($stepdata);
}
$quba->set_observer(new question_engine_unit_of_work($quba)); $quba->set_observer(new question_engine_unit_of_work($quba));
} }
@ -150,7 +147,7 @@ class question_engine_data_mapper {
$stepdata[] = $this->insert_question_attempt_step($step, $record->id, $seq, $context); $stepdata[] = $this->insert_question_attempt_step($step, $record->id, $seq, $context);
} }
return call_user_func_array('array_merge', $stepdata); return $this->combine_step_data($stepdata);
} }
/** /**
@ -171,6 +168,22 @@ class question_engine_data_mapper {
return $record; return $record;
} }
/**
* Take an array of arrays, and flatten it, even if the outer array is empty.
*
* Only public so it can be called from the unit of work. Not part of the
* public API of this class.
*
* @param array $stepdata array of zero or more arrays.
* @return array made by concatenating all the separate arrays.
*/
public function combine_step_data(array $stepdata): array {
if (empty($stepdata)) {
return [];
}
return call_user_func_array('array_merge', $stepdata);
}
/** /**
* Helper method used by insert_question_attempt_step and update_question_attempt_step * Helper method used by insert_question_attempt_step and update_question_attempt_step
* @param question_attempt_step $step the step to store. * @param question_attempt_step $step the step to store.
@ -1581,9 +1594,7 @@ class question_engine_unit_of_work implements question_usage_observer {
$dm->update_questions_usage_by_activity($this->quba); $dm->update_questions_usage_by_activity($this->quba);
} }
if ($stepdata) { $dm->insert_all_step_data($dm->combine_step_data($stepdata));
$dm->insert_all_step_data(call_user_func_array('array_merge', $stepdata));
}
$this->stepsdeleted = array(); $this->stepsdeleted = array();
$this->stepsmodified = array(); $this->stepsmodified = array();

View file

@ -230,4 +230,25 @@ class question_engine_data_mapper_testcase extends qbehaviour_walkthrough_test_b
$this->assertEquals(0, $DB->count_records('question_attempts') - $initialqarows); $this->assertEquals(0, $DB->count_records('question_attempts') - $initialqarows);
$this->assertEquals(2, $DB->count_records('question_attempt_steps') - $initialqasrows); $this->assertEquals(2, $DB->count_records('question_attempt_steps') - $initialqasrows);
} }
/**
* Test that database operations on an empty usage work without errors.
*/
public function test_save_and_load_an_empty_usage() {
$this->resetAfterTest();
// Create a new usage.
$quba = question_engine::make_questions_usage_by_activity('test', context_system::instance());
$quba->set_preferred_behaviour('deferredfeedback');
// Save it.
question_engine::save_questions_usage_by_activity($quba);
// Reload it.
$reloadedquba = question_engine::load_questions_usage_by_activity($quba->get_id());
$this->assertCount(0, $quba->get_slots());
// Delete it.
question_engine::delete_questions_usage_by_activity($quba->get_id());
}
} }