/** * Test functions core_tag_tag::create_if_missing() and core_tag_tag::get_by_name_bulk(). */ public function test_create_get() { $tagset = array('Cat', ' Dog ', '<Mouse', '<>', 'mouse', 'Dog'); $collid = core_tag_collection::get_default(); $tags = core_tag_tag::create_if_missing($collid, $tagset); $this->assertEquals(array('cat', 'dog', 'mouse'), array_keys($tags)); $this->assertEquals('Dog', $tags['dog']->rawname); $this->assertEquals('mouse', $tags['mouse']->rawname); // Case of the last tag wins. $tags2 = core_tag_tag::create_if_missing($collid, array('CAT', 'Elephant')); $this->assertEquals(array('cat', 'elephant'), array_keys($tags2)); $this->assertEquals('Cat', $tags2['cat']->rawname); $this->assertEquals('Elephant', $tags2['elephant']->rawname); $this->assertEquals($tags['cat']->id, $tags2['cat']->id); // Tag 'cat' already existed and was not created again. $tags3 = core_tag_tag::get_by_name_bulk($collid, $tagset); $this->assertEquals(array('cat', 'dog', 'mouse'), array_keys($tags3)); $this->assertEquals('Dog', $tags3['dog']->rawname); $this->assertEquals('mouse', $tags3['mouse']->rawname); }
redirect($PAGE->url); break; case 'changetype': require_sesskey(); if ($tagid && $tagobject->update(array('isstandard' => $isstandard ? 1 : 0))) { redirect(new moodle_url($PAGE->url, array('notice' => 'typechanged'))); } redirect($PAGE->url); break; case 'addstandardtag': require_sesskey(); $tagobjects = null; if ($tagcoll) { $otagsadd = optional_param('otagsadd', '', PARAM_RAW); $newtags = preg_split('/\\s*,\\s*/', trim($otagsadd), -1, PREG_SPLIT_NO_EMPTY); $tagobjects = core_tag_tag::create_if_missing($tagcoll->id, $newtags, true); } foreach ($tagobjects as $tagobject) { if (!$tagobject->isstandard) { $tagobject->update(array('isstandard' => 1)); } } redirect(new moodle_url($PAGE->url, $tagobjects ? array('notice' => 'added') : null)); break; } echo $OUTPUT->header(); if ($notice && get_string_manager()->string_exists($notice, 'tag')) { echo $OUTPUT->notification(get_string($notice, 'tag'), 'notifysuccess'); } if (!$tagcoll) { // Tag collection is not specified. Display the overview of tag collections and tag areas.
/** * Adds one or more tag in the database. This function should not be called directly : you should * use tag_set. * * @package core_tag * @deprecated since 3.1 * @param mixed $tags one tag, or an array of tags, to be created * @param string $type type of tag to be created ("default" is the default value and "official" is the only other supported * value at this time). An official tag is kept even if there are no records tagged with it. * @return array $tags ids indexed by their lowercase normalized names. Any boolean false in the array indicates an error while * adding the tag. */ function tag_add($tags, $type = "default") { debugging('Function tag_add() is deprecated. You can use core_tag_tag::create_if_missing(), however it should not be necessary ' . 'since tags are created automatically when assigned to items', DEBUG_DEVELOPER); if (!is_array($tags)) { $tags = array($tags); } $objects = core_tag_tag::create_if_missing(core_tag_collection::get_default(), $tags, $type === 'official'); // New function returns the tags in different format, for BC we keep the format that this function used to have. $rv = array(); foreach ($objects as $name => $tagobject) { if (isset($tagobject->id)) { $rv[$tagobject->name] = $tagobject->id; } else { $rv[$name] = false; } } return $rv; }
/** * Test the tag created event. */ public function test_tag_created() { global $DB; // Trigger and capture the event for creating a tag. $sink = $this->redirectEvents(); core_tag_tag::create_if_missing(core_tag_area::get_collection('core', 'course'), array('A really awesome tag!')); $events = $sink->get_events(); $event = reset($events); // Check that the tag was created and the event data is valid. $this->assertEquals(1, $DB->count_records('tag')); $this->assertInstanceOf('\\core\\event\\tag_created', $event); $this->assertEquals(context_system::instance(), $event->get_context()); }