/** * This function will delete numerous tag instances efficiently. * This removes tag instances only. It doesn't check to see if it is the last use of a tag. * * @param array $instances An array of tag instance objects with the addition of the tagname and tagrawname * (used for recording a delete event). */ function tag_bulk_delete_instances($instances) { global $DB; $instanceids = array(); foreach ($instances as $instance) { $instanceids[] = $instance->id; } // This is a multi db compatible method of creating the correct sql when using the 'IN' value. // $insql is the sql statement, $params are the id numbers. list($insql, $params) = $DB->get_in_or_equal($instanceids); $sql = 'id ' . $insql; $DB->delete_records_select('tag_instance', $sql, $params); // Now go through and record each tag individually with the event system. foreach ($instances as $instance) { // Trigger tag removed event (i.e. The tag instance has been removed). $event = \core\event\tag_removed::create(array('objectid' => $instance->id, 'contextid' => $instance->contextid, 'other' => array('tagid' => $instance->tagid, 'tagname' => $instance->name, 'tagrawname' => $instance->rawname, 'itemid' => $instance->itemid, 'itemtype' => $instance->itemtype))); unset($instance->name); unset($instance->rawname); $event->add_record_snapshot('tag_instance', $instance); $event->trigger(); } }
/** * Delete one instance of a tag. If the last instance was deleted, it will also delete the tag, unless its type is 'official'. * * @package core_tag * @category tag * @access public * @param string $record_type the type of the record for which to remove the instance * @param int $record_id the id of the record for which to remove the instance * @param int $tagid the tagid that needs to be removed * @param int $userid (optional) the userid * @return bool true on success, false otherwise */ function tag_delete_instance($record_type, $record_id, $tagid, $userid = null) { global $DB; if (is_null($userid)) { $taginstance = $DB->get_record('tag_instance', array('tagid' => $tagid, 'itemtype' => $record_type, 'itemid' => $record_id)); } else { $taginstance = $DB->get_record('tag_instance', array('tagid' => $tagid, 'itemtype' => $record_type, 'itemid' => $record_id, 'tiuserid' => $userid)); } if ($taginstance) { // Get the tag. $tag = $DB->get_record('tag', array('id' => $tagid)); $DB->delete_records('tag_instance', array('id' => $taginstance->id)); // We can not fire an event with 'null' as the contextid. if (is_null($taginstance->contextid)) { $taginstance->contextid = context_system::instance()->id; } // Trigger tag removed event. $event = \core\event\tag_removed::create(array('objectid' => $taginstance->id, 'contextid' => $taginstance->contextid, 'other' => array('tagid' => $tag->id, 'tagname' => $tag->name, 'tagrawname' => $tag->rawname, 'itemid' => $taginstance->itemid, 'itemtype' => $taginstance->itemtype))); $event->add_record_snapshot('tag_instance', $taginstance); $event->trigger(); // If there are no other instances of the tag then consider deleting the tag as well. if (!$DB->record_exists('tag_instance', array('tagid' => $tagid))) { // If the tag is a personal tag then delete it - don't delete official tags. if ($tag->tagtype == 'default') { tag_delete($tagid); } } } else { return false; } return true; }