/** * Test deleting a group of tag instances. */ public function test_tag_bulk_delete_instances() { global $DB; $task = new \core\task\tag_cron_task(); // Setup. $user = $this->getDataGenerator()->create_user(); $course = $this->getDataGenerator()->create_course(); $context = context_course::instance($course->id); // Create some tag instances. for ($i = 0; $i < 10; $i++) { $tag = $this->getDataGenerator()->create_tag(array('userid' => $user->id)); core_tag_tag::add_item_tag('core', 'course', $course->id, $context, $tag->rawname); } // Get tag instances. tag name and rawname are required for the event fired in this function. $sql = "SELECT ti.*, t.name, t.rawname\n FROM {tag_instance} ti\n JOIN {tag} t ON t.id = ti.tagid"; $taginstances = $DB->get_records_sql($sql); $this->assertCount(10, $taginstances); // Run the function. $task->bulk_delete_instances($taginstances); // Make sure they are gone. $instancecount = $DB->count_records('tag_instance'); $this->assertEquals(0, $instancecount); }
/** * Adds a tag to a record, without overwriting the current tags. * * This function remains here for backward compatiblity. It is recommended to use * {@link core_tag_tag::add_item_tag()} or {@link core_tag_tag::add_related_tags()} instead * * @package core_tag * @deprecated since 3.1 * @param string $itemtype the type of record to tag ('post' for blogs, 'user' for users, etc.) * @param int $itemid the id of the record to tag * @param string $tag the tag to add * @param string|null $component the component that was tagged * @param int|null $contextid the context id of where this tag was assigned * @return bool|null */ function tag_set_add($itemtype, $itemid, $tag, $component = null, $contextid = null) { debugging('Function tag_set_add() is deprecated. Use ' . ' core_tag_tag::add_item_tag() instead', DEBUG_DEVELOPER); if ($itemtype === 'tag') { return core_tag_tag::get($itemid, '*', MUST_EXIST)->add_related_tags(array($tag)); } else { $context = $contextid ? context::instance_by_id($contextid) : context_system::instance(); return core_tag_tag::add_item_tag($component, $itemtype, $itemid, $context, $tag); } }
protected function process_tag($data) { global $CFG, $DB; $data = (object) $data; $newquestion = $this->get_new_parentid('question'); $questioncreated = (bool) $this->get_mappingid('question_created', $this->get_old_parentid('question')); if (!$questioncreated) { // This question already exists in the question bank. Nothing for us to do. return; } if (core_tag_tag::is_enabled('core_question', 'question')) { $tagname = $data->rawname; // Get the category, so we can then later get the context. $categoryid = $this->get_new_parentid('question_category'); if (empty($this->cachedcategory) || $this->cachedcategory->id != $categoryid) { $this->cachedcategory = $DB->get_record('question_categories', array('id' => $categoryid)); } // Add the tag to the question. core_tag_tag::add_item_tag('core_question', 'question', $newquestion, context::instance_by_id($this->cachedcategory->contextid), $tagname); } }
print_error('tagdisabled'); } if (isguestuser()) { print_error('noguest'); } if (!confirm_sesskey()) { print_error('sesskey'); } $usercontext = context_user::instance($USER->id); switch ($action) { case 'addinterest': if (!core_tag_tag::is_enabled('core', 'user')) { print_error('tagdisabled'); } $tag = required_param('tag', PARAM_TAG); core_tag_tag::add_item_tag('core', 'user', $USER->id, $usercontext, $tag); $tc = core_tag_area::get_collection('core', 'user'); redirect(core_tag_tag::make_url($tc, $tag)); break; case 'removeinterest': if (!core_tag_tag::is_enabled('core', 'user')) { print_error('tagdisabled'); } $tag = required_param('tag', PARAM_TAG); core_tag_tag::remove_item_tag('core', 'user', $USER->id, $tag); $tc = core_tag_area::get_collection('core', 'user'); redirect(core_tag_tag::make_url($tc, $tag)); break; case 'flaginappropriate': require_capability('moodle/tag:flag', context_system::instance()); $id = required_param('id', PARAM_INT);
/** * Test the tag deleted event */ public function test_tag_deleted() { global $DB; $this->setAdminUser(); // Create a course and a user. $course = $this->getDataGenerator()->create_course(); $user = $this->getDataGenerator()->create_user(); // Create tag we are going to delete. $tag = $this->getDataGenerator()->create_tag(); // Trigger and capture the event for deleting a tag. $sink = $this->redirectEvents(); tag_delete($tag->id); $this->assertDebuggingCalled(); $events = $sink->get_events(); $event = reset($events); // Check that the tag was deleted and the event data is valid. $this->assertEquals(0, $DB->count_records('tag')); $this->assertInstanceOf('\\core\\event\\tag_deleted', $event); $this->assertEquals(context_system::instance(), $event->get_context()); // Create two tags we are going to delete to ensure passing multiple tags work. $tag = $this->getDataGenerator()->create_tag(); $tag2 = $this->getDataGenerator()->create_tag(); // Trigger and capture the events for deleting multiple tags. $sink = $this->redirectEvents(); tag_delete(array($tag->id, $tag2->id)); $this->assertDebuggingCalled(); $events = $sink->get_events(); // Check that the tags were deleted and the events data is valid. $this->assertEquals(0, $DB->count_records('tag')); foreach ($events as $event) { $this->assertInstanceOf('\\core\\event\\tag_deleted', $event); $this->assertEquals(context_system::instance(), $event->get_context()); } // Add a tag instance to a course. core_tag_tag::add_item_tag('core', 'course', $course->id, context_course::instance($course->id), 'cat', $user->id); // Trigger and capture the event for deleting a personal tag for a user for a course. $sink = $this->redirectEvents(); core_tag_tag::remove_item_tag('core', 'course', $course->id, 'cat', $user->id); $events = $sink->get_events(); $event = $events[1]; // Check that the tag was deleted and the event data is valid. $this->assertEquals(0, $DB->count_records('tag')); $this->assertInstanceOf('\\core\\event\\tag_deleted', $event); $this->assertEquals(context_system::instance(), $event->get_context()); // Add the tag instance to the course again as it was deleted. core_tag_tag::add_item_tag('core', 'course', $course->id, context_course::instance($course->id), 'dog', $user->id); // Trigger and capture the event for deleting all tags in a course. $sink = $this->redirectEvents(); core_tag_tag::remove_all_item_tags('core', 'course', $course->id); $events = $sink->get_events(); $event = $events[1]; // Check that the tag was deleted and the event data is valid. $this->assertEquals(0, $DB->count_records('tag')); $this->assertInstanceOf('\\core\\event\\tag_deleted', $event); $this->assertEquals(context_system::instance(), $event->get_context()); // Add multiple tag instances now and check that it still works. core_tag_tag::set_item_tags('core', 'course', $course->id, context_course::instance($course->id), array('fish', 'hamster'), $user->id); // Trigger and capture the event for deleting all tags in a course. $sink = $this->redirectEvents(); core_tag_tag::remove_all_item_tags('core', 'course', $course->id); $events = $sink->get_events(); $events = array($events[1], $events[3]); // Check that the tags were deleted and the events data is valid. $this->assertEquals(0, $DB->count_records('tag')); foreach ($events as $event) { $this->assertInstanceOf('\\core\\event\\tag_deleted', $event); $this->assertEquals(context_system::instance(), $event->get_context()); } }
protected function process_wiki_tag($data) { global $CFG, $DB; $data = (object) $data; $oldid = $data->id; if (!core_tag_tag::is_enabled('mod_wiki', 'wiki_pages')) { // Tags disabled in server, nothing to process. return; } $tag = $data->rawname; $itemid = $this->get_new_parentid('wiki_page'); $wikiid = $this->get_new_parentid('wiki'); $context = context_module::instance($this->task->get_moduleid()); core_tag_tag::add_item_tag('mod_wiki', 'wiki_pages', $itemid, $context, $tag); }