This commit is contained in:
Eloy Lafuente (stronk7) 2022-03-27 16:47:08 +02:00
commit 1c2fd1cfb9
4 changed files with 22 additions and 129 deletions

View File

@ -2797,41 +2797,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.
*

View File

@ -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();

View File

@ -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]}");
@ -85,4 +85,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 [];
}
}

View File

@ -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 ===