function add_otag($otag) { global $USER; $error = ''; // When adding ofical tag, we see if there's already a personal tag // With the same Name, if there is, we just change the type if ($tag = tag_by_name($otag)) { if ($tag->tagtype == 'official') { // official tag already exist $error = get_string('tagalready'); break; } else { // might not want to do this anymore? $tag->tagtype = 'official'; update_record('tag', $tag); $tagid = $tag->id; } } else { // Brand new offical tag $tagid = tag_create($otag, 'official'); if (empty($tagid)) { error('Can not create tag!'); } } return $error; }
/** * function to attach tags into a post * @param int postid - id of the blog */ function add_tags_info($postid) { global $USER; $post = get_record('post', 'id', $postid); /// Attach official tags if ($otags = optional_param('otags', '', PARAM_INT)) { foreach ($otags as $otag) { $tag->tagid = $otag; //insert_record('blog_tag_instance', $tag); tag_an_item('blog', $postid, $otag, 'official'); } } /// Attach Personal Tags if ($ptags = optional_param('ptags', '', PARAM_NOTAGS)) { $ptags = explode(',', $ptags); foreach ($ptags as $ptag) { $ptag = trim($ptag); // check for existance // it does not matter whether it is an offical tag or personal tag // we do not want to have 1 copy of offical tag and 1 copy of personal tag (for the same tag) if ($ctag = tag_by_name($ptag)) { tag_an_item('blog', $postid, $ctag->id); } else { // create a personal tag if ($tagid = tag_create($ptag)) { if ($tagid = array_shift($tagid)) { tag_an_item('blog', $postid, $tagid); } } } } } }