/** * Stores a tag for a course for a user * * @uses $CFG * @param array $tags simple array of keywords to be stored * @param integer $courseid * @param integer $userid * @param string $tagtype official or default only * @param string $myurl optional for logging creation of course tags */ function coursetag_store_keywords($tags, $courseid, $userid=0, $tagtype='official', $myurl='') { global $CFG; if (is_array($tags) and !empty($tags)) { foreach($tags as $tag) { $tag = trim($tag); if (strlen($tag) > 0) { //tag_set_add('course', $courseid, $tag, $userid); //deletes official tags //add tag if does not exist if (!$tagid = tag_get_id($tag)) { $tag_id_array = tag_add(array($tag), $tagtype); $tagid = $tag_id_array[moodle_strtolower($tag)]; } //ordering $ordering = 0; if ($current_ids = tag_get_tags_ids('course', $courseid)) { end($current_ids); $ordering = key($current_ids) + 1; } //set type tag_type_set($tagid, $tagtype); //tag_instance entry tag_assign('course', $courseid, $tagid, $ordering, $userid); //logging - note only for user added tags if ($tagtype == 'default' and $myurl != '') { $url = $myurl.'?query='.urlencode($tag); add_to_log($courseid, 'coursetags', 'add', $url, 'Course tagged'); } } } } }
/** * Test the tag created event. */ public function test_tag_created() { global $DB; // Trigger and capture the event for creating a tag. $sink = $this->redirectEvents(); tag_add('A really awesome tag!'); $events = $sink->get_events(); $event = reset($events); // Check that the tag was created and the event data is valid. $this->assertEquals(1, $DB->count_records('tag')); $this->assertInstanceOf('\\core\\event\\tag_created', $event); $this->assertEquals(context_system::instance(), $event->get_context()); }
/** * Set the tags assigned to a record. This overwrites the current tags. * * This function is meant to be fed the string coming up from the user * interface, which contains all tags assigned to a record. * * @param string $record_type the type of record to tag ('post' for blogs, * 'user' for users, 'tag' for tags, etc. * @param int $record_id the id of the record to tag * @param array $tags the array of tags to set on the record. If * given an empty array, all tags will be removed. * @return void */ function tag_set($record_type, $record_id, $tags) { static $in_recursion_semaphore = false; // this is to prevent loops when tagging a tag if ($record_type == 'tag' && !$in_recursion_semaphore) { $current_tagged_tag_name = tag_get_name($record_id); } $tags_ids = tag_get_id($tags, TAG_RETURN_ARRAY); // force an array, even if we only have one tag. $cleaned_tags = tag_normalize($tags); //echo 'tags-in-tag_set'; var_dump($tags); var_dump($tags_ids); var_dump($cleaned_tags); $current_ids = tag_get_tags_ids($record_type, $record_id); //var_dump($current_ids); // for data coherence reasons, it's better to remove deleted tags // before adding new data: ordering could be duplicated. foreach ($current_ids as $current_id) { if (!in_array($current_id, $tags_ids)) { tag_delete_instance($record_type, $record_id, $current_id); if ($record_type == 'tag' && !$in_recursion_semaphore) { // if we are removing a tag-on-a-tag (manually related tag), // we need to remove the opposite relationship as well. tag_delete_instance('tag', $current_id, $record_id); } } } foreach ($tags as $ordering => $tag) { $tag = trim($tag); if (!$tag) { continue; } $clean_tag = $cleaned_tags[$tag]; $tag_current_id = $tags_ids[$clean_tag]; if (is_null($tag_current_id)) { // create new tags //echo "call to add tag $tag\n"; $new_tag = tag_add($tag); $tag_current_id = $new_tag[$clean_tag]; } tag_assign($record_type, $record_id, $tag_current_id, $ordering); // if we are tagging a tag (adding a manually-assigned related tag), we // need to create the opposite relationship as well. if ($record_type == 'tag' && !$in_recursion_semaphore) { $in_recursion_semaphore = true; tag_set_add('tag', $tag_current_id, $current_tagged_tag_name); $in_recursion_semaphore = false; } } }
$notice .= ' -- ' . get_string('updated', 'tag'); } break; case 'addofficialtag': if (!data_submitted() or !confirm_sesskey()) { break; } $new_otags = explode(',', optional_param('otagsadd', '', PARAM_TAG)); $notice = ''; foreach ($new_otags as $new_otag) { if ($new_otag_id = tag_get_id($new_otag)) { // tag exists, change the type tag_type_set($new_otag_id, 'official'); } else { require_capability('moodle/tag:create', get_context_instance(CONTEXT_SYSTEM)); tag_add($new_otag, 'official'); } $notice .= get_string('addedotag', 'tag', $new_otag) . ' '; } break; } if ($err_notice) { echo $OUTPUT->notification($err_notice, 'red'); } if ($notice) { echo $OUTPUT->notification($notice, 'green'); } // small form to add an official tag print '<form class="tag-management-form" method="post" action="' . $CFG->wwwroot . '/tag/manage.php">'; print '<input type="hidden" name="action" value="addofficialtag" />'; print '<div class="tag-management-form generalbox"><label class="accesshide" for="id_otagsadd">' . get_string('addotags', 'tag') . '</label>' . '<input name="otagsadd" id="id_otagsadd" type="text" />' . '<input type="hidden" name="sesskey" value="' . sesskey() . '">' . '<input name="addotags" value="' . get_string('addotags', 'tag') . '" onclick="skipClientValidation = true;" id="id_addotags" type="submit" />' . '</div>';
/** * Stores a tag for a course for a user * * @package core_tag * @category tag * @param array $tags simple array of keywords to be stored * @param int $courseid the id of the course we wish to store a tag for * @param int $userid the id of the user we wish to store a tag for * @param string $tagtype official or default only * @param string $myurl (optional) for logging creation of course tags */ function coursetag_store_keywords($tags, $courseid, $userid = 0, $tagtype = 'official', $myurl = '') { global $CFG; if (is_array($tags) and !empty($tags)) { foreach ($tags as $tag) { $tag = trim($tag); if (strlen($tag) > 0) { //tag_set_add('course', $courseid, $tag, $userid); //deletes official tags //add tag if does not exist if (!($tagid = tag_get_id($tag))) { $tag_id_array = tag_add(array($tag), $tagtype); $tagid = $tag_id_array[core_text::strtolower($tag)]; } //ordering $ordering = 0; if ($current_ids = tag_get_tags_ids('course', $courseid)) { end($current_ids); $ordering = key($current_ids) + 1; } //set type tag_type_set($tagid, $tagtype); //tag_instance entry tag_assign('course', $courseid, $tagid, $ordering, $userid, 'core', context_course::instance($courseid)->id); } } } }
require "foundation/aanti_refresh.php"; require "foundation/ftag.php"; //引入语言包 $a_langpackage = new albumlp(); //变量取得 $album_name = short_check(get_argp('album_name')); $album_information = short_check(get_argp('album_information')); $privacy = short_check(get_argp('privacy')); $user_id = get_sess_userid(); $user_name = get_sess_username(); $tag = short_check(get_argp('tag')); //防止重复提交 antiRePost($album_name); if ($album_name == '') { action_return(1, "", -1); exit; } //数据表定义区 $t_album = $tablePreStr . "album"; $dbo = new dbex(); //读写分离定义函数 dbtarget('w', $dbServs); $album_skin = "uploadfiles/album/logo.jpg"; $sql = "insert into {$t_album} (`album_name`,`user_id`,`user_name`,`album_info`,`add_time`,`privacy`,`album_skin`,`tag`,`update_time`) " . "values('{$album_name}',{$user_id},'{$user_name}','{$album_information}',NOW(),'{$privacy}','{$album_skin}','{$tag}',NOW()); "; $dbo->exeUpdate($sql); $album_id = mysql_insert_id(); //标签功能 $tag_id = tag_add($tag); $tag_state = tag_relation(0, $tag_id, $album_id); //回应信息 action_return(1, "", "modules.php?app=photo_upload&album_id={$album_id}");
/** * Sets 'Set' tags for groups for the forum. * Necessary to use this rather than core tag lib as that does not deal with context * and as group item ids can be the same that is an issue * Also can only have 1 unique group/tag/user record * @param int $forumid forum table id * @param int $groupid groups table id * @param array $tags array of tag rawnames e.g. Fish, frog */ public static function set_group_tags($forumid, $groupid, $tags) { global $DB, $CFG, $USER; require_once $CFG->dirroot . '/tag/lib.php'; $forum = self::get_from_id($forumid, self::CLONE_DIRECT); $context = $forum->get_context(true); $transaction = $DB->start_delegated_transaction(); // Get existing tags used. $settags = array(); $taginstances = $DB->get_records_sql("\n SELECT DISTINCT t.*, ti.id as instanceid\n FROM {tag} t\n INNER JOIN {tag_instance} ti\n ON t.id = ti.tagid\n WHERE ti.component = ? AND ti.itemtype = ? AND ti.contextid = ? AND ti.itemid = ?", array('mod_forumng', 'groups', $context->id, $groupid)); // Delete instances any not in new tags (note tag records not deleted as cleaned in cron). $tistodelete = array(); foreach ($taginstances as $tinstance) { if (!in_array($tinstance->rawname, $tags)) { $tistodelete[] = $tinstance->instanceid; } else { // Store existing tag instance used. $settags[$tinstance->instanceid] = $tinstance->rawname; } } if ($tistodelete) { list($delsql, $delparams) = $DB->get_in_or_equal($tistodelete); $DB->delete_records_select('tag_instance', "id {$delsql}", $delparams); } // Add/get new tag records. $existingtags = tag_get_id($tags, TAG_RETURN_ARRAY); // Normalize tags passed so can match to existing tags array. $normaltags = tag_normalize($tags); // Add tag instances (where needed). $ordering = 0; foreach ($normaltags as $rawname => $name) { if (in_array($rawname, $settags)) { // Pre-existing instance, skip. $ordering++; continue; } $tagid = 0; if (!array_key_exists($name, $existingtags) || empty($existingtags[$name])) { // Need to add tag. $newtag = tag_add($rawname); $tagid = array_pop($newtag); } else { $tagid = $existingtags[$name]; } // Create instance (like tag_assign()). $tag_instance_object = new stdClass(); $tag_instance_object->tagid = $tagid; $tag_instance_object->component = 'mod_forumng'; $tag_instance_object->itemid = $groupid; $tag_instance_object->itemtype = 'groups'; $tag_instance_object->contextid = $context->id; $tag_instance_object->ordering = $ordering; $tag_instance_object->timecreated = time(); $tag_instance_object->timemodified = $tag_instance_object->timecreated; $tag_instance_object->tiuserid = self::get_group_taginstance_userid($groupid, $tagid); $DB->insert_record('tag_instance', $tag_instance_object); $ordering++; } $DB->commit_delegated_transaction($transaction); }
/** * Stores a tag for a course for a user * * @deprecated since 3.0 * @package core_tag * @category tag * @param array $tags simple array of keywords to be stored * @param int $courseid the id of the course we wish to store a tag for * @param int $userid the id of the user we wish to store a tag for * @param string $tagtype official or default only * @param string $myurl (optional) for logging creation of course tags */ function coursetag_store_keywords($tags, $courseid, $userid = 0, $tagtype = 'official', $myurl = '') { debugging('Function coursetag_store_keywords() is deprecated. Userid is no longer used for tagging courses.', DEBUG_DEVELOPER); global $CFG; require_once $CFG->dirroot . '/tag/lib.php'; if (is_array($tags) and !empty($tags)) { foreach ($tags as $tag) { $tag = trim($tag); if (strlen($tag) > 0) { //tag_set_add('course', $courseid, $tag, $userid); //deletes official tags //add tag if does not exist if (!($tagid = tag_get_id($tag))) { $tag_id_array = tag_add(array($tag), $tagtype); $tagid = $tag_id_array[core_text::strtolower($tag)]; } //ordering $ordering = 0; if ($current_ids = tag_get_tags_ids('course', $courseid)) { end($current_ids); $ordering = key($current_ids) + 1; } //set type tag_type_set($tagid, $tagtype); //tag_instance entry tag_assign('course', $courseid, $tagid, $ordering, $userid, 'core', context_course::instance($courseid)->id); } } } }