MDL-65399 mod_quiz: remove_slot should update the existing info

The remove_slot method should update the remaining slot's slot
attribute. It also should update the remaining slot's question number
and section info.
This commit is contained in:
Shamim Rezaie 2019-04-23 11:15:43 +10:00
parent 8affd823a4
commit a834294228
2 changed files with 30 additions and 0 deletions

View File

@ -900,7 +900,9 @@ class structure {
/**
* Remove a slot from a quiz
*
* @param int $slotnumber The number of the slot to be deleted.
* @throws \coding_exception
*/
public function remove_slot($slotnumber) {
global $DB;
@ -923,6 +925,9 @@ class structure {
for ($i = $slot->slot + 1; $i <= $maxslot; $i++) {
$DB->set_field('quiz_slots', 'slot', $i - 1,
array('quizid' => $this->get_quizid(), 'slot' => $i));
$this->slotsinorder[$i]->slot = $i - 1;
$this->slotsinorder[$i - 1] = $this->slotsinorder[$i];
unset($this->slotsinorder[$i]);
}
$qtype = $DB->get_field('question', 'qtype', array('id' => $slot->questionid));
@ -932,6 +937,13 @@ class structure {
}
quiz_update_section_firstslots($this->get_quizid(), -1, $slotnumber);
foreach ($this->sections as $key => $section) {
if ($section->firstslot > $slotnumber) {
$this->sections[$key]->firstslot--;
}
}
$this->populate_slots_with_sections();
$this->populate_question_numbers();
unset($this->questions[$slot->questionid]);
$this->refresh_page_numbers_and_update_db();

View File

@ -720,6 +720,24 @@ class mod_quiz_structure_testcase extends advanced_testcase {
$this->assertFalse($DB->record_exists('question', array('id' => $randomq->id)));
}
/**
* Unit test to make sue it is not possible to remove all slots in a section at once.
*
* @expectedException coding_exception
*/
public function test_cannot_remove_all_slots_in_a_section() {
$quizobj = $this->create_test_quiz(array(
array('TF1', 1, 'truefalse'),
array('TF2', 1, 'truefalse'),
'Heading 2',
array('TF3', 2, 'truefalse'),
));
$structure = \mod_quiz\structure::create_for_quiz($quizobj);
$structure->remove_slot(1);
$structure->remove_slot(2);
}
/**
* @expectedException coding_exception
*/