diff --git a/mod/quiz/classes/local/structure/slot_random.php b/mod/quiz/classes/local/structure/slot_random.php index ebd89f14e3a..50ef17fed98 100644 --- a/mod/quiz/classes/local/structure/slot_random.php +++ b/mod/quiz/classes/local/structure/slot_random.php @@ -43,6 +43,11 @@ class slot_random { */ protected $quiz = null; + /** + * @var \core_tag_tag[] List of tags for this slot. + */ + protected $tags = []; + /** * slot_random constructor. * @@ -52,9 +57,8 @@ class slot_random { $this->record = new \stdClass(); $properties = array( - 'id', 'slot', 'quizid', 'page', 'requireprevious', - 'questionid', 'questioncategoryid', 'includingsubcategories', - 'tags', 'maxmark'); + 'id', 'slot', 'quizid', 'page', 'requireprevious', 'questionid', + 'questioncategoryid', 'includingsubcategories', 'maxmark'); foreach ($properties as $property) { if (isset($slotrecord->$property)) { @@ -95,6 +99,29 @@ class slot_random { $this->record->quizid = $quiz->id; } + /** + * Set some tags for this quiz slot. + * + * @param \core_tag_tag[] $tags + */ + public function set_tags($tags) { + $this->tags = []; + foreach ($tags as $tag) { + // We use $tag->id as the key for the array so not only it handles duplicates of the same tag being given, + // but also it is consistent with the behaviour of set_tags_by_id() below. + $this->tags[$tag->id] = $tag; + } + } + + /** + * Set some tags for this quiz slot. This function uses tag ids to find tags. + * + * @param int[] $tagids + */ + public function set_tags_by_id($tagids) { + $this->tags = \core_tag_tag::get_bulk($tagids, 'id, name'); + } + /** * Inserts the quiz slot at the $page page. * It is required to call this function if you are building a quiz slot object from scratch. @@ -151,6 +178,19 @@ class slot_random { } $this->record->id = $DB->insert_record('quiz_slots', $this->record); + + if (!empty($this->tags)) { + $recordstoinsert = []; + foreach ($this->tags as $tag) { + $recordstoinsert[] = (object)[ + 'slotid' => $this->record->id, + 'tagid' => $tag->id, + 'tagname' => $tag->name + ]; + } + $DB->insert_records('quiz_slot_tags', $recordstoinsert); + } + $trans->allow_commit(); } } \ No newline at end of file diff --git a/mod/quiz/locallib.php b/mod/quiz/locallib.php index 7b529562ecc..d3d29632e8b 100644 --- a/mod/quiz/locallib.php +++ b/mod/quiz/locallib.php @@ -2189,18 +2189,10 @@ function quiz_add_random_questions($quiz, $addonpage, $categoryid, $number, $catcontext = context::instance_by_id($category->contextid); require_capability('moodle/question:useall', $catcontext); - $tags = []; + $tags = \core_tag_tag::get_bulk($tagids, 'id, name'); $tagstrings = []; - foreach ($tagids as $tagid) { - if ($tag = core_tag_tag::get($tagid, 'id,name')) { - $tags[] = [ - 'id' => $tagid, - 'name' => $tag->name - ]; - $tagstrings[] = "{$tagid},{$tag->name}"; - } else if (!empty($tagid)) { - print_error('invalidtagid', 'mod_quiz'); - } + foreach ($tags as $tag) { + $tagstrings[] = "{$tag->id},{$tag->name}"; } // Find existing random questions in this category that are @@ -2239,11 +2231,11 @@ function quiz_add_random_questions($quiz, $addonpage, $categoryid, $number, $randomslotdata->questionid = $question->id; $randomslotdata->questioncategoryid = $categoryid; $randomslotdata->includingsubcategories = $includesubcategories ? 1 : 0; - $randomslotdata->tags = json_encode($tags); $randomslotdata->maxmark = 1; $randomslot = new \mod_quiz\local\structure\slot_random($randomslotdata); $randomslot->set_quiz($quiz); + $randomslot->set_tags($tags); $randomslot->insert($addonpage); } } diff --git a/mod/quiz/tests/local_structure_slot_random_test.php b/mod/quiz/tests/local_structure_slot_random_test.php new file mode 100755 index 00000000000..a9c1cd5fbfd --- /dev/null +++ b/mod/quiz/tests/local_structure_slot_random_test.php @@ -0,0 +1,413 @@ +. + +/** + * Unit tests for the {@link \mod_quiz\local\structure\slot_random} class. + * + * @package mod_quiz + * @category test + * @copyright 2018 Shamim Rezaie + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +/** + * Class mod_quiz_local_structure_slot_random_test + * Class for tests related to the {@link \mod_quiz\local\structure\slot_random} class. + * + * @copyright 2018 Shamim Rezaie + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class mod_quiz_local_structure_slot_random_test extends advanced_testcase { + /** + * Constructor test. + */ + public function test_constructor() { + global $SITE; + + $this->resetAfterTest(); + $this->setAdminUser(); + + // Create a quiz. + $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz'); + $quiz = $quizgenerator->create_instance(array('course' => $SITE->id, 'questionsperpage' => 3, 'grade' => 100.0)); + + // Create a question category in the system context. + $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question'); + $category = $questiongenerator->create_question_category(); + + // Create a random question without adding it to a quiz. + // We don't want to use quiz_add_random_questions because that itself, instantiates an object from the slot_random class. + $form = new stdClass(); + $form->category = $category->id . ',' . $category->contextid; + $form->includesubcategories = true; + $form->fromtags = []; + $form->defaultmark = 1; + $form->hidden = 1; + $form->stamp = make_unique_id_code(); + $question = new stdClass(); + $question->qtype = 'random'; + $question = question_bank::get_qtype('random')->save_question($question, $form); + + $randomslotdata = new stdClass(); + $randomslotdata->quizid = $quiz->id; + $randomslotdata->questionid = $question->id; + $randomslotdata->questioncategoryid = $category->id; + $randomslotdata->includingsubcategories = 1; + $randomslotdata->maxmark = 1; + + $randomslot = new \mod_quiz\local\structure\slot_random($randomslotdata); + + $rc = new ReflectionClass('\mod_quiz\local\structure\slot_random'); + $rcp = $rc->getProperty('record'); + $rcp->setAccessible(true); + $record = $rcp->getValue($randomslot); + + $this->assertEquals($quiz->id, $record->quizid); + $this->assertEquals($question->id, $record->questionid); + $this->assertEquals($category->id, $record->questioncategoryid); + $this->assertEquals(1, $record->includingsubcategories); + $this->assertEquals(1, $record->maxmark); + } + + public function test_get_quiz_quiz() { + global $SITE, $DB; + + $this->resetAfterTest(); + $this->setAdminUser(); + + // Create a quiz. + $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz'); + $quiz = $quizgenerator->create_instance(array('course' => $SITE->id, 'questionsperpage' => 3, 'grade' => 100.0)); + + // Create a question category in the system context. + $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question'); + $category = $questiongenerator->create_question_category(); + + quiz_add_random_questions($quiz, 0, $category->id, 1, false); + + // Get the random question's id. It is at the first slot. + $questionid = $DB->get_field('quiz_slots', 'questionid', array('quizid' => $quiz->id, 'slot' => 1)); + + $randomslotdata = new stdClass(); + $randomslotdata->quizid = $quiz->id; + $randomslotdata->questionid = $questionid; + $randomslotdata->questioncategoryid = $category->id; + $randomslotdata->includingsubcategories = 1; + $randomslotdata->maxmark = 1; + + $randomslot = new \mod_quiz\local\structure\slot_random($randomslotdata); + + // The create_instance had injected an additional cmid propery to the quiz. Let's remove that. + unset($quiz->cmid); + + $this->assertEquals($quiz, $randomslot->get_quiz()); + } + + public function test_set_quiz() { + global $SITE, $DB; + + $this->resetAfterTest(); + $this->setAdminUser(); + + // Create a quiz. + $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz'); + $quiz = $quizgenerator->create_instance(array('course' => $SITE->id, 'questionsperpage' => 3, 'grade' => 100.0)); + + // Create a question category in the system context. + $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question'); + $category = $questiongenerator->create_question_category(); + + quiz_add_random_questions($quiz, 0, $category->id, 1, false); + + // Get the random question's id. It is at the first slot. + $questionid = $DB->get_field('quiz_slots', 'questionid', array('quizid' => $quiz->id, 'slot' => 1)); + + $randomslotdata = new stdClass(); + $randomslotdata->quizid = $quiz->id; + $randomslotdata->questionid = $questionid; + $randomslotdata->questioncategoryid = $category->id; + $randomslotdata->includingsubcategories = 1; + $randomslotdata->maxmark = 1; + + $randomslot = new \mod_quiz\local\structure\slot_random($randomslotdata); + + // The create_instance had injected an additional cmid propery to the quiz. Let's remove that. + unset($quiz->cmid); + + $randomslot->set_quiz($quiz); + + $rc = new ReflectionClass('\mod_quiz\local\structure\slot_random'); + $rcp = $rc->getProperty('quiz'); + $rcp->setAccessible(true); + $quizpropery = $rcp->getValue($randomslot); + + $this->assertEquals($quiz, $quizpropery); + } + + private function setup_for_test_tags($tagnames) { + global $SITE, $DB; + + // Create a quiz. + $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz'); + $quiz = $quizgenerator->create_instance(array('course' => $SITE->id, 'questionsperpage' => 3, 'grade' => 100.0)); + + // Create a question category in the system context. + $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question'); + $category = $questiongenerator->create_question_category(); + + quiz_add_random_questions($quiz, 0, $category->id, 1, false); + + // Get the random question's id. It is at the first slot. + $questionid = $DB->get_field('quiz_slots', 'questionid', array('quizid' => $quiz->id, 'slot' => 1)); + + $randomslotdata = new stdClass(); + $randomslotdata->quizid = $quiz->id; + $randomslotdata->questionid = $questionid; + $randomslotdata->questioncategoryid = $category->id; + $randomslotdata->includingsubcategories = 1; + $randomslotdata->maxmark = 1; + + $randomslot = new \mod_quiz\local\structure\slot_random($randomslotdata); + + // Create tags. + foreach ($tagnames as $tagname) { + $tagrecord = array( + 'isstandard' => 1, + 'flag' => 0, + 'rawname' => $tagname, + 'description' => $tagname . ' desc' + ); + $tags[$tagname] = $this->getDataGenerator()->create_tag($tagrecord); + } + + return array($randomslot, $tags); + } + + public function test_set_tags() { + $this->resetAfterTest(); + $this->setAdminUser(); + + list($randomslot, $tags) = $this->setup_for_test_tags(['foo', 'bar']); + $randomslot->set_tags([$tags['foo'], $tags['bar']]); + + $rc = new ReflectionClass('\mod_quiz\local\structure\slot_random'); + $rcp = $rc->getProperty('tags'); + $rcp->setAccessible(true); + $tagspropery = $rcp->getValue($randomslot); + + $this->assertEquals([ + $tags['foo']->id => $tags['foo'], + $tags['bar']->id => $tags['bar'], + ], $tagspropery); + } + + public function test_set_tags_twice() { + $this->resetAfterTest(); + $this->setAdminUser(); + + list($randomslot, $tags) = $this->setup_for_test_tags(['foo', 'bar', 'baz']); + + // Set tags for the first time. + $randomslot->set_tags([$tags['foo'], $tags['bar']]); + // Now set the tags again. + $randomslot->set_tags([$tags['baz']]); + + $rc = new ReflectionClass('\mod_quiz\local\structure\slot_random'); + $rcp = $rc->getProperty('tags'); + $rcp->setAccessible(true); + $tagspropery = $rcp->getValue($randomslot); + + $this->assertEquals([ + $tags['baz']->id => $tags['baz'], + ], $tagspropery); + } + + public function test_set_tags_duplicates() { + $this->resetAfterTest(); + $this->setAdminUser(); + + list($randomslot, $tags) = $this->setup_for_test_tags(['foo', 'bar', 'baz']); + + $randomslot->set_tags([$tags['foo'], $tags['bar'], $tags['foo']]); + + $rc = new ReflectionClass('\mod_quiz\local\structure\slot_random'); + $rcp = $rc->getProperty('tags'); + $rcp->setAccessible(true); + $tagspropery = $rcp->getValue($randomslot); + + $this->assertEquals([ + $tags['foo']->id => $tags['foo'], + $tags['bar']->id => $tags['bar'], + ], $tagspropery); + } + + public function test_set_tags_by_id() { + $this->resetAfterTest(); + $this->setAdminUser(); + + list($randomslot, $tags) = $this->setup_for_test_tags(['foo', 'bar', 'baz']); + + $randomslot->set_tags_by_id([$tags['foo']->id, $tags['bar']->id]); + + $rc = new ReflectionClass('\mod_quiz\local\structure\slot_random'); + $rcp = $rc->getProperty('tags'); + $rcp->setAccessible(true); + $tagspropery = $rcp->getValue($randomslot); + + // The set_tags_by_id function only retrieves id and name fields of the tag object. + $this->assertCount(2, $tagspropery); + $this->assertArrayHasKey($tags['foo']->id, $tagspropery); + $this->assertArrayHasKey($tags['bar']->id, $tagspropery); + $this->assertEquals( + (object)['id' => $tags['foo']->id, 'name' => $tags['foo']->name], + $tagspropery[$tags['foo']->id]->to_object() + ); + $this->assertEquals( + (object)['id' => $tags['bar']->id, 'name' => $tags['bar']->name], + $tagspropery[$tags['bar']->id]->to_object() + ); + } + + public function test_set_tags_by_id_twice() { + $this->resetAfterTest(); + $this->setAdminUser(); + + list($randomslot, $tags) = $this->setup_for_test_tags(['foo', 'bar', 'baz']); + + // Set tags for the first time. + $randomslot->set_tags_by_id([$tags['foo']->id, $tags['bar']->id]); + // Now set the tags again. + $randomslot->set_tags_by_id([$tags['baz']->id]); + + $rc = new ReflectionClass('\mod_quiz\local\structure\slot_random'); + $rcp = $rc->getProperty('tags'); + $rcp->setAccessible(true); + $tagspropery = $rcp->getValue($randomslot); + + // The set_tags_by_id function only retrieves id and name fields of the tag object. + $this->assertCount(1, $tagspropery); + $this->assertArrayHasKey($tags['baz']->id, $tagspropery); + $this->assertEquals( + (object)['id' => $tags['baz']->id, 'name' => $tags['baz']->name], + $tagspropery[$tags['baz']->id]->to_object() + ); + } + + public function test_set_tags_by_id_duplicates() { + $this->resetAfterTest(); + $this->setAdminUser(); + + list($randomslot, $tags) = $this->setup_for_test_tags(['foo', 'bar', 'baz']); + + $randomslot->set_tags_by_id([$tags['foo']->id, $tags['bar']->id], $tags['foo']->id); + + $rc = new ReflectionClass('\mod_quiz\local\structure\slot_random'); + $rcp = $rc->getProperty('tags'); + $rcp->setAccessible(true); + $tagspropery = $rcp->getValue($randomslot); + + // The set_tags_by_id function only retrieves id and name fields of the tag object. + $this->assertCount(2, $tagspropery); + $this->assertArrayHasKey($tags['foo']->id, $tagspropery); + $this->assertArrayHasKey($tags['bar']->id, $tagspropery); + $this->assertEquals( + (object)['id' => $tags['foo']->id, 'name' => $tags['foo']->name], + $tagspropery[$tags['foo']->id]->to_object() + ); + $this->assertEquals( + (object)['id' => $tags['bar']->id, 'name' => $tags['bar']->name], + $tagspropery[$tags['bar']->id]->to_object() + ); + } + + public function test_insert() { + global $SITE, $DB; + + $this->resetAfterTest(); + $this->setAdminUser(); + + // Create a quiz. + $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz'); + $quiz = $quizgenerator->create_instance(array('course' => $SITE->id, 'questionsperpage' => 3, 'grade' => 100.0)); + + // Create a question category in the system context. + $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question'); + $category = $questiongenerator->create_question_category(); + + // Create a random question without adding it to a quiz. + $form = new stdClass(); + $form->category = $category->id . ',' . $category->contextid; + $form->includesubcategories = true; + $form->fromtags = []; + $form->defaultmark = 1; + $form->hidden = 1; + $form->stamp = make_unique_id_code(); + $question = new stdClass(); + $question->qtype = 'random'; + $question = question_bank::get_qtype('random')->save_question($question, $form); + + // Prepare 2 tags. + $tagrecord = array( + 'isstandard' => 1, + 'flag' => 0, + 'rawname' => 'foo', + 'description' => 'foo desc' + ); + $footag = $this->getDataGenerator()->create_tag($tagrecord); + $tagrecord = array( + 'isstandard' => 1, + 'flag' => 0, + 'rawname' => 'bar', + 'description' => 'bar desc' + ); + $bartag = $this->getDataGenerator()->create_tag($tagrecord); + + $randomslotdata = new stdClass(); + $randomslotdata->quizid = $quiz->id; + $randomslotdata->questionid = $question->id; + $randomslotdata->questioncategoryid = $category->id; + $randomslotdata->includingsubcategories = 1; + $randomslotdata->maxmark = 1; + + // Insert the random question to the quiz. + $randomslot = new \mod_quiz\local\structure\slot_random($randomslotdata); + $randomslot->set_tags([$footag, $bartag]); + $randomslot->insert(1); // Put the question on the first page of the quiz. + + // Get the random question's quiz_slot. It is at the first slot. + $quizslot = $DB->get_record('quiz_slots', array('quizid' => $quiz->id, 'slot' => 1)); + // Get the random question's tags from quiz_slot_tags. It is at the first slot. + $quizslottags = $DB->get_records('quiz_slot_tags', array('slotid' => $quizslot->id)); + + $this->assertEquals($question->id, $quizslot->questionid); + $this->assertEquals($category->id, $quizslot->questioncategoryid); + $this->assertEquals(1, $quizslot->includingsubcategories); + $this->assertEquals(1, $quizslot->maxmark); + + $this->assertCount(2, $quizslottags); + $this->assertEquals( + [ + ['tagid' => $footag->id, 'tagname' => $footag->name], + ['tagid' => $bartag->id, 'tagname' => $bartag->name] + ], + array_map(function($slottag) { + return ['tagid' => $slottag->tagid, 'tagname' => $slottag->tagname]; + }, $quizslottags), + '', 0.0, 10, true); + } +} \ No newline at end of file diff --git a/tag/classes/tag.php b/tag/classes/tag.php index c41f80f9260..52dfd34b1ec 100644 --- a/tag/classes/tag.php +++ b/tag/classes/tag.php @@ -209,7 +209,7 @@ class core_tag_tag { } /** - * Simple function to just return a single tag object by its id + * Simple function to just return an array of tag objects by their ids * * @param int[] $ids * @param string $returnfields which fields do we want returned from table {tag}.