From de8472df88720e3df67590de5fd9d16e6c554088 Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Wed, 23 Mar 2022 13:08:30 +0000 Subject: [PATCH] MDL-74309 quiz: removed obsolete code related to random tags --- mod/quiz/locallib.php | 35 ------------ mod/quiz/tests/locallib_test.php | 91 -------------------------------- mod/quiz/tests/tags_test.php | 19 ++++++- mod/quiz/upgrade.txt | 6 ++- 4 files changed, 22 insertions(+), 129 deletions(-) diff --git a/mod/quiz/locallib.php b/mod/quiz/locallib.php index 72678e2f4e4..dcf5d999220 100644 --- a/mod/quiz/locallib.php +++ b/mod/quiz/locallib.php @@ -2790,41 +2790,6 @@ function quiz_retrieve_tags_for_slot_ids($slotids) { ); } -/** - * Retrieves tag information for the given quiz slot. - * A quiz slot have some tags if and only if it is representing a random question by tags. - * - * @param int $slotid The id of the quiz slot. - * @return array List of tags. - */ -function quiz_retrieve_slot_tags($slotid) { - $referencedata = \mod_quiz\question\bank\qbank_helper::get_random_question_data_from_slot($slotid); - if (isset($referencedata->filtercondition)) { - $filtercondition = json_decode($referencedata->filtercondition); - if (isset($filtercondition->tags)) { - return $filtercondition->tags; - } - } - return []; -} - -/** - * Retrieves tag ids for the given quiz slot. - * A quiz slot have some tags if and only if it is representing a random question by tags. - * - * @param int $slotid The id of the quiz slot. - * @return int[] - */ -function quiz_retrieve_slot_tag_ids($slotid) { - $tagids = []; - $tags = quiz_retrieve_slot_tags($slotid); - foreach ($tags as $tag) { - $tagstring = explode(',', $tag); - $tagids [] = $tagstring[0]; - } - return $tagids; -} - /** * Get quiz attempt and handling error. * diff --git a/mod/quiz/tests/locallib_test.php b/mod/quiz/tests/locallib_test.php index 4c068c2b9d4..ae6f4d1d1fe 100644 --- a/mod/quiz/tests/locallib_test.php +++ b/mod/quiz/tests/locallib_test.php @@ -510,97 +510,6 @@ class locallib_test extends \advanced_testcase { return array($quiz, $tagobjects); } - public function test_quiz_retrieve_slot_tags() { - global $DB; - - $this->resetAfterTest(); - $this->setAdminUser(); - - list($quiz, $tags) = $this->setup_quiz_and_tags(1, 1, [['foo', 'bar']], ['baz']); - - // Get the random question's slotid. It is at the second slot. - $slotid = $DB->get_field('quiz_slots', 'id', array('quizid' => $quiz->id, 'slot' => 2)); - $slottags = quiz_retrieve_slot_tags($slotid); - sort($slottags); - - $this->assertEquals( - [ - "{$tags['foo']->id},{$tags['foo']->name}", - "{$tags['bar']->id},{$tags['bar']->name}", - ], $slottags); - } - - public function test_quiz_retrieve_slot_tags_with_removed_tag() { - global $DB; - - $this->resetAfterTest(); - $this->setAdminUser(); - - list($quiz, $tags) = $this->setup_quiz_and_tags(1, 1, [['foo', 'bar']], ['baz']); - - // Get the random question's slotid. It is at the second slot. - $slotid = $DB->get_field('quiz_slots', 'id', array('quizid' => $quiz->id, 'slot' => 2)); - $slottags = quiz_retrieve_slot_tags($slotid); - - // Now remove the foo tag and check again. - \core_tag_tag::delete_tags([$tags['foo']->id]); - $slottags = quiz_retrieve_slot_tags($slotid); - sort($slottags); - - $this->assertEquals( - [ - "{$tags['foo']->id},{$tags['foo']->name}", - "{$tags['bar']->id},{$tags['bar']->name}", - - ], - $slottags); - } - - public function test_quiz_retrieve_slot_tags_for_standard_question() { - global $DB; - - $this->resetAfterTest(); - $this->setAdminUser(); - - list($quiz, $tags) = $this->setup_quiz_and_tags(1, 1, [['foo', 'bar']]); - - // Get the standard question's slotid. It is at the first slot. - $slotid = $DB->get_field('quiz_slots', 'id', array('quizid' => $quiz->id, 'slot' => 1)); - - // There should be no slot tags for a non-random question. - $this->assertCount(0, quiz_retrieve_slot_tags($slotid)); - } - - public function test_quiz_retrieve_slot_tag_ids() { - global $DB; - - $this->resetAfterTest(); - $this->setAdminUser(); - - list($quiz, $tags) = $this->setup_quiz_and_tags(1, 1, [['foo', 'bar']], ['baz']); - - // Get the random question's slotid. It is at the second slot. - $slotid = $DB->get_field('quiz_slots', 'id', array('quizid' => $quiz->id, 'slot' => 2)); - $tagids = quiz_retrieve_slot_tag_ids($slotid); - - $this->assertEqualsCanonicalizing([$tags['foo']->id, $tags['bar']->id], $tagids); - } - - public function test_quiz_retrieve_slot_tag_ids_for_standard_question() { - global $DB; - - $this->resetAfterTest(); - $this->setAdminUser(); - - list($quiz, $tags) = $this->setup_quiz_and_tags(1, 1, [['foo', 'bar']], ['baz']); - - // Get the standard question's slotid. It is at the first slot. - $slotid = $DB->get_field('quiz_slots', 'id', array('quizid' => $quiz->id, 'slot' => 1)); - $tagids = quiz_retrieve_slot_tag_ids($slotid); - - $this->assertEqualsCanonicalizing([], $tagids); - } - public function test_quiz_override_summary() { global $DB, $PAGE; $this->resetAfterTest(); diff --git a/mod/quiz/tests/tags_test.php b/mod/quiz/tests/tags_test.php index c6cb24863b2..8f7ecaf8239 100644 --- a/mod/quiz/tests/tags_test.php +++ b/mod/quiz/tests/tags_test.php @@ -73,7 +73,7 @@ class mod_quiz_tags_testcase extends advanced_testcase { $tag3 = core_tag_tag::get_by_name(0, 't3', 'id, name'); $this->assertNotFalse($tag3); - $slottags = quiz_retrieve_slot_tags($question->slotid); + $slottags = $this->get_tags_for_slot($question->slotid); $slottags = reset($slottags); $slottags = explode(',', $slottags); $this->assertEquals("{$tag2->id},{$tag2->name}", "{$slottags[0]},{$slottags[1]}"); @@ -84,4 +84,21 @@ class mod_quiz_tags_testcase extends advanced_testcase { $filtercondition = json_decode($randomincludingsubcategories->filtercondition); $this->assertEquals(0, $filtercondition->includingsubcategories); } + + /** + * Helper to get the random tags, if any, for a slot. + * + * @param int $slotid the id of the slot to get tags for. + * @return array the tags. + */ + protected function get_tags_for_slot(int $slotid): array { + $referencedata = \mod_quiz\question\bank\qbank_helper::get_random_question_data_from_slot($slotid); + if (isset($referencedata->filtercondition)) { + $filtercondition = json_decode($referencedata->filtercondition); + if (isset($filtercondition->tags)) { + return $filtercondition->tags; + } + } + return []; + } } diff --git a/mod/quiz/upgrade.txt b/mod/quiz/upgrade.txt index f65cf16e2dc..da7c4ebfeae 100644 --- a/mod/quiz/upgrade.txt +++ b/mod/quiz/upgrade.txt @@ -14,8 +14,10 @@ This files describes API changes in the quiz code. The fields removed are now manage by new core_question tables: - question_references -> Records where a specific question is used. - question_set_references -> Records where groups of questions are used (e.g. random questions). - The quiz_slots_tags table has been removed entirely, as has the get_slot_tags_for_slot_id() method - from mod/quiz/classes/structure.php. That information now comes from question_set_references. +* The quiz_slots_tags database table has been removed entirely, as has the get_slot_tags_for_slot_id() method + from mod/quiz/classes/structure.php and the the locallib.php functions quiz_retrieve_slot_tags and + quiz_retrieve_slot_tag_ids. This information is now stored in question_set_references + and can be accessed using qbank_helper::get_random_question_data_from_slot. === 3.11 ===