} $changed = array(); foreach ($tagschecked as $tag_id) { if (!in_array($tagtypes[$tag_id], $existing_tagtypes)) { //can not add new types here!! continue; } // update tag type; $tag = tag_by_id($tag_id); $tag->tagtype = $tagtypes[$tag_id]; if (update_record('tag', $tag)) { $changed[] = $tag_id; } } if ($changed) { $str_changed = tag_name_from_string(implode($changed, ',')); $str_changed = str_replace(',', ', ', $str_changed); $notice = $str_changed . ' -- ' . get_string('typechanged', 'tag'); } break; case 'changename': if (!data_submitted or !confirm_sesskey()) { break; } $tags_names_changed = array(); foreach ($tagschecked as $tag_id) { if ($newnames[$tag_id] != '') { if (tag_exists($newnames[$tag_id])) { $err_notice .= $newnames[$tag_id] . '-- ' . get_string('namesalreadybeeingused', 'tag') . '<br />'; } else { $tags_names_changed[$tag_id] = $newnames[$tag_id];
/** * 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); }