diff --git a/mod/quiz/classes/structure.php b/mod/quiz/classes/structure.php index d63cd6a9354..60ba7d8b247 100644 --- a/mod/quiz/classes/structure.php +++ b/mod/quiz/classes/structure.php @@ -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(); diff --git a/mod/quiz/tests/structure_test.php b/mod/quiz/tests/structure_test.php index f43d4c8c7c5..8bf69cbcb0b 100644 --- a/mod/quiz/tests/structure_test.php +++ b/mod/quiz/tests/structure_test.php @@ -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 */