/** * Test the core_tag_tag::add_item_tag() and core_tag_tag::remove_item_tag() functions. */ public function test_add_remove_item_tag() { global $DB; // Create a course to tag. $course = $this->getDataGenerator()->create_course(); // Create the tag and tag instance we are going to delete. core_tag_tag::add_item_tag('core', 'course', $course->id, context_course::instance($course->id), 'A random tag'); $this->assertEquals(1, $DB->count_records('tag')); $this->assertEquals(1, $DB->count_records('tag_instance')); // Call the tag_set_delete function. core_tag_tag::remove_item_tag('core', 'course', $course->id, 'A random tag'); // Now check that there are no tags or tag instances. $this->assertEquals(0, $DB->count_records('tag')); $this->assertEquals(0, $DB->count_records('tag_instance')); }
$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); $tagobject = core_tag_tag::get($id, '*', MUST_EXIST); $tagobject->flag(); redirect($tagobject->get_view_url(), get_string('responsiblewillbenotified', 'tag')); break; default: print_error('unknowaction'); break; }
/** * Removes a tag from a record, without overwriting other current tags. * * This function remains here for backward compatiblity. It is recommended to use * {@link core_tag_tag::remove_item_tag()} 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 delete * @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_delete($itemtype, $itemid, $tag, $component = null, $contextid = null) { debugging('Function tag_set_delete() is deprecated. Use ' . ' core_tag_tag::remove_item_tag() instead', DEBUG_DEVELOPER); return core_tag_tag::remove_item_tag($component, $itemtype, $itemid, $tag); }
/** * 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()); } }