예제 #1
0
require_once '../tag/lib.php';
$action = optional_param('action', '', PARAM_ALPHA);
require_login();
if (empty($CFG->usetags)) {
    error('Tags are disabled!');
}
if (isguestuser()) {
    print_error('noguest');
}
if (!confirm_sesskey()) {
    print_error('sesskey');
}
switch ($action) {
    case 'addinterest':
        $id = optional_param('id', 0, PARAM_INT);
        $name = optional_param('name', '', PARAM_TEXT);
        if (empty($name) && $id) {
            $name = tag_name($id);
        }
        tag_an_item('user', $USER->id, $name);
        if (!empty($name) && !$id) {
            $id = tag_id(tag_normalize($name));
        }
        redirect($CFG->wwwroot . '/tag/index.php?id=' . $id);
        break;
    case 'flaginappropriate':
        $id = required_param('id', PARAM_INT);
        tag_flag_inappropriate($id);
        redirect($CFG->wwwroot . '/tag/index.php?id=' . $id, get_string('responsiblewillbenotified', 'tag'));
        break;
}
예제 #2
0
/**
 * 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_id($ptag)) {
                tag_an_item('blog', $postid, $ctag);
            } else {
                // create a personal tag
                if ($tagid = tag_create($ptag)) {
                    if ($tagid = array_shift($tagid)) {
                        tag_an_item('blog', $postid, $tagid);
                    }
                }
            }
        }
    }
}
예제 #3
0
/**
 * Updates the tags associated with an item
 *
 * Ex 1:
 *  Suppose user 1 is tagged only with "algorithms", "computers" and "software"
 *  By calling update_item_tags('user', 1, 'algorithms, software, mathematics')
 *  User 1 will now be tagged only with "algorithms", "software" and "mathematics"
 *
 * Ex 2:
 *   update_item_tags('user', '1', 'algorithms, 12, 13')
 *   User 1 will now be tagged only with "algorithms", and with tags of ids 12 and 13
 *
 *
 * @param string $item_type name of the table where the item is stored. Ex: 'user'
 * @param string $item_id id of the item to be tagged
 * @param string $tag_names_or_ids_csv comma separated tag names (can be unormalized) or ids of existing tags
 * @param string $tag_type type of the tags that are beeing added (optional, default value is "default")
 */
function update_item_tags($item_type, $item_id, $tag_names_or_ids_csv, $tag_type = "default")
{
    //if $tag_names_csv is an empty string, remove all tag associations of the item
    if (empty($tag_names_or_ids_csv)) {
        untag_an_item($item_type, $item_id);
        return;
    }
    //convert any tag ids passed to their corresponding tag names
    $tag_names_csv = tag_name_from_string($tag_names_or_ids_csv);
    //associate the tags passed with the item
    tag_an_item($item_type, $item_id, $tag_names_csv, $tag_type);
    //get the ids of the tags passed
    $existing_and_new_tags_ids = tags_id(tag_normalize($tag_names_csv));
    // delete any tag instance with $item_type and $item_id
    // that are not in $tag_names_csv
    $tags_id_csv = "'" . implode("','", $existing_and_new_tags_ids) . "'";
    $select = "\n        itemid = '{$item_id}'\n    AND\n        itemtype = '{$item_type}'\n    AND\n        tagid NOT IN ({$tags_id_csv})\n    ";
    delete_records_select('tag_instance', $select);
}