Example #1
0
/**
 * Adds one or more tag in the database.  This function should not be called directly : you should
 * use tag_set.
 *
 * @package core_tag
 * @access  private
 * @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")
{
    global $USER, $DB;
    if (!is_array($tags)) {
        $tags = array($tags);
    }
    $tag_object = new StdClass();
    $tag_object->tagtype = $type;
    $tag_object->userid = $USER->id;
    $tag_object->timemodified = time();
    $clean_tags = tag_normalize($tags, TAG_CASE_ORIGINAL);
    $tags_ids = array();
    foreach ($clean_tags as $tag) {
        $tag = trim($tag);
        if (!$tag) {
            $tags_ids[$tag] = false;
        } else {
            // note that the difference between rawname and name is only
            // capitalization : the rawname is NOT the same at the rawtag.
            $tag_object->rawname = $tag;
            $tag_name_lc = core_text::strtolower($tag);
            $tag_object->name = $tag_name_lc;
            //var_dump($tag_object);
            $tags_ids[$tag_name_lc] = $DB->insert_record('tag', $tag_object);
            $event = \core\event\tag_created::create(array('objectid' => $tags_ids[$tag_name_lc], 'relateduserid' => $tag_object->userid, 'context' => context_system::instance(), 'other' => array('name' => $tag_object->name, 'rawname' => $tag_object->rawname)));
            $event->trigger();
        }
    }
    return $tags_ids;
}