/**
* Test the tag_set function.
* This function was deprecated in 3.1
*/
public function test_tag_set_get()
{
global $DB;
// Create a course to tag.
$course = $this->getDataGenerator()->create_course();
// Create the tag and tag instance.
tag_set('course', $course->id, array('A random tag'), 'core', context_course::instance($course->id)->id);
$this->assertDebuggingCalled();
// Get the tag instance that should have been created.
$taginstance = $DB->get_record('tag_instance', array('itemtype' => 'course', 'itemid' => $course->id), '*', MUST_EXIST);
$this->assertEquals('core', $taginstance->component);
$this->assertEquals(context_course::instance($course->id)->id, $taginstance->contextid);
$tagbyname = tag_get('name', 'A random tag');
$this->assertDebuggingCalled();
$this->assertEquals('A random tag', $tagbyname->rawname);
$this->assertEmpty(tag_get('name', 'Non existing tag'));
$this->assertDebuggingCalled();
$tagbyid = tag_get('id', $tagbyname->id);
$this->assertDebuggingCalled();
$this->assertEquals('A random tag', $tagbyid->rawname);
$tagid = $tagbyname->id;
$this->assertEmpty(tag_get('id', $tagid + 1));
$this->assertDebuggingCalled();
tag_set('tag', $tagid, array('Some related tag'));
$this->assertDebuggingCalled();
$relatedtags = tag_get_related_tags($tagid);
$this->assertDebuggingCalled();
$this->assertCount(1, $relatedtags);
$this->assertEquals('Some related tag', $relatedtags[0]->rawname);
$tagids = tag_get_id(array('A random tag', 'Some related tag'));
$this->assertDebuggingCalled();
$this->assertCount(2, $tagids);
$this->assertEquals($tagid, $tagids['a random tag']);
$this->assertEquals($relatedtags[0]->id, $tagids['some related tag']);
}
/**
* Validates the Attach Tags group action.
* Gets called for every bug, but performs the real tag validation only
* the first time. Any invalid tags will be skipped, as there is no simple
* or clean method of presenting these errors to the user.
* @param integer Bug ID
* @return boolean True
*/
function action_attach_tags_validate($p_bug_id)
{
global $g_action_attach_tags_valid;
if (!isset($g_action_attach_tags_valid)) {
$f_tag_string = gpc_get_string('tag_string');
$f_tag_select = gpc_get_string('tag_select');
global $g_action_attach_tags_attach, $g_action_attach_tags_create, $g_action_attach_tags_failed;
$g_action_attach_tags_attach = array();
$g_action_attach_tags_create = array();
$g_action_attach_tags_failed = array();
$t_tags = tag_parse_string($f_tag_string);
$t_can_create = access_has_global_level(config_get('tag_create_threshold'));
foreach ($t_tags as $t_tag_row) {
if (-1 == $t_tag_row['id']) {
if ($t_can_create) {
$g_action_attach_tags_create[] = $t_tag_row;
} else {
$g_action_attach_tags_failed[] = $t_tag_row;
}
} elseif (-2 == $t_tag_row['id']) {
$g_action_attach_tags_failed[] = $t_tag_row;
} else {
$g_action_attach_tags_attach[] = $t_tag_row;
}
}
if (0 < $f_tag_select && tag_exists($f_tag_select)) {
$g_action_attach_tags_attach[] = tag_get($f_tag_select);
}
}
global $g_action_attach_tags_attach, $g_action_attach_tags_create, $g_action_attach_tags_failed;
return true;
}
/**
* Get a list of related tags.
* Returns a list of tags that are the most related to the given tag,
* based on the number of times they have been attached to the same bugs.
* Defaults to a list of five tags.
* @param integer Tag ID
* @param integer List size
* @return array Array of tag rows, with share count added
*/
function tag_stats_related($p_tag_id, $p_limit = 5)
{
$t_bug_table = db_get_table('bug');
$t_tag_table = db_get_table('tag');
$t_bug_tag_table = db_get_table('bug_tag');
$t_project_user_list_table = db_get_table('project_user_list');
$t_user_table = db_get_table('user');
$c_tag_id = db_prepare_int($p_tag_id);
$c_user_id = auth_get_current_user_id();
$subquery = "SELECT b.id FROM {$t_bug_table} AS b\n\t\t\t\t\tLEFT JOIN {$t_project_user_list_table} AS p\n\t\t\t\t\t\tON p.project_id=b.project_id AND p.user_id=" . db_param() . "\n\t\t\t\t\tJOIN {$t_user_table} AS u\n\t\t\t\t\t\tON u.id=" . db_param() . "\n\t\t\t\t\tJOIN {$t_bug_tag_table} AS t\n\t\t\t\t\t\tON t.bug_id=b.id\n\t\t\t\t\tWHERE ( p.access_level>b.view_state OR u.access_level>b.view_state )\n\t\t\t\t\t\tAND t.tag_id=" . db_param();
$query = "SELECT * FROM {$t_bug_tag_table}\n\t\t\t\t\tWHERE tag_id != " . db_param() . "\n\t\t\t\t\t\tAND bug_id IN ( {$subquery} ) ";
$result = db_query_bound($query, array($c_tag_id, $c_user_id, $c_user_id, $c_tag_id));
$t_tag_counts = array();
while ($row = db_fetch_array($result)) {
if (!isset($t_tag_counts[$row['tag_id']])) {
$t_tag_counts[$row['tag_id']] = 1;
} else {
$t_tag_counts[$row['tag_id']]++;
}
}
arsort($t_tag_counts);
$t_tags = array();
$i = 1;
foreach ($t_tag_counts as $t_tag_id => $t_count) {
$t_tag_row = tag_get($t_tag_id);
$t_tag_row['count'] = $t_count;
$t_tags[] = $t_tag_row;
$i++;
if ($i > $p_limit) {
break;
}
}
return $t_tags;
}
/**
* function to attach tags into a post
* @param int postid - id of the blog
*/
function add_tags_info($postid)
{
$tags = array();
if ($otags = optional_param('otags', '', PARAM_INT)) {
foreach ($otags as $tagid) {
// TODO : make this use the tag name in the form
if ($tag = tag_get('id', $tagid)) {
$tags[] = $tag->name;
}
}
}
$manual_tags = optional_param('ptags', '', PARAM_NOTAGS);
$tags = array_merge($tags, explode(',', $manual_tags));
tag_set('post', $postid, $tags);
}
/**
* Test for function tag_cleanup() that is part of tag cron
*/
public function test_cleanup()
{
global $DB;
$user = $this->getDataGenerator()->create_user();
// Setting tags will create non-official tags 'cat', 'dog' and 'fish'.
tag_set('user', $user->id, array('cat', 'dog', 'fish'), 'core', context_user::instance($user->id)->id);
$this->assertTrue($DB->record_exists('tag', array('name' => 'cat')));
$this->assertTrue($DB->record_exists('tag', array('name' => 'dog')));
$this->assertTrue($DB->record_exists('tag', array('name' => 'fish')));
// Make tag 'dog' official.
$dogtag = tag_get('name', 'dog');
$fishtag = tag_get('name', 'fish');
tag_type_set($dogtag->id, 'official');
// Manually remove the instances pointing on tags 'dog' and 'fish'.
$DB->execute('DELETE FROM {tag_instance} WHERE tagid in (?,?)', array($dogtag->id, $fishtag->id));
// Call tag_cleanup().
tag_cleanup();
// Tag 'cat' is still present because it's used. Tag 'dog' is present because it's official.
// Tag 'fish' was removed because it is not official and it is no longer used by anybody.
$this->assertTrue($DB->record_exists('tag', array('name' => 'cat')));
$this->assertTrue($DB->record_exists('tag', array('name' => 'dog')));
$this->assertFalse($DB->record_exists('tag', array('name' => 'fish')));
// Delete user without using API function.
$DB->update_record('user', array('id' => $user->id, 'deleted' => 1));
// Call tag_cleanup().
tag_cleanup();
// Tag 'cat' was now deleted too.
$this->assertFalse($DB->record_exists('tag', array('name' => 'cat')));
// Assign tag to non-existing record. Make sure tag was created in the DB.
tag_set('course', 1231231, array('bird'), 'core', context_system::instance()->id);
$this->assertTrue($DB->record_exists('tag', array('name' => 'bird')));
// Call tag_cleanup().
tag_cleanup();
// Tag 'bird' was now deleted because the related record does not exist in the DB.
$this->assertFalse($DB->record_exists('tag', array('name' => 'bird')));
// Now we have a tag instance pointing on 'sometag' tag.
$user = $this->getDataGenerator()->create_user();
tag_set('user', $user->id, array('sometag'), 'core', context_user::instance($user->id)->id);
$sometag = tag_get('name', 'sometag');
$this->assertTrue($DB->record_exists('tag_instance', array('tagid' => $sometag->id)));
// Some hacker removes the tag without using API.
$DB->delete_records('tag', array('id' => $sometag->id));
// Call tag_cleanup().
tag_cleanup();
// The tag instances were also removed.
$this->assertFalse($DB->record_exists('tag_instance', array('tagid' => $sometag->id)));
}
/**
* Change the "value" of a tag, and update the associated 'name'.
*
* @param int $tagid the id of the tag to modify
* @param string $newtag the new rawname
* @return bool true on success, false otherwise
*/
function tag_rename($tagid, $newrawname)
{
if (!($newrawname_clean = array_shift(tag_normalize($newrawname, TAG_CASE_ORIGINAL)))) {
return false;
}
if (!($newname_clean = moodle_strtolower($newrawname_clean))) {
return false;
}
// Prevent the rename if a tag with that name already exists
if ($existing = tag_get('name', $newname_clean, 'id, name, rawname')) {
if ($existing->id != $tagid) {
// Another tag already exists with this name
return false;
}
}
if ($tag = tag_get('id', $tagid, 'id, name, rawname')) {
$tag->rawname = addslashes($newrawname_clean);
$tag->name = addslashes($newname_clean);
$tag->timemodified = time();
return update_record('tag', $tag);
}
return false;
}
/**
* Change the "value" of a tag, and update the associated 'name'.
*
* @package core_tag
* @category tag
* @access public
* @param int $tagid the id of the tag to modify
* @param string $newrawname the new rawname
* @return bool true on success, false otherwise
*/
function tag_rename($tagid, $newrawname) {
global $DB;
$norm = tag_normalize($newrawname, TAG_CASE_ORIGINAL);
if (! $newrawname_clean = array_shift($norm) ) {
return false;
}
if (! $newname_clean = textlib::strtolower($newrawname_clean)) {
return false;
}
// Prevent the rename if a tag with that name already exists
if ($existing = tag_get('name', $newname_clean, 'id, name, rawname')) {
if ($existing->id != $tagid) { // Another tag already exists with this name
return false;
}
}
if ($tag = tag_get('id', $tagid, 'id, name, rawname')) {
$tag->rawname = $newrawname_clean;
$tag->name = $newname_clean;
$tag->timemodified = time();
return $DB->update_record('tag', $tag);
}
return false;
}
开发者ID:verbazend,项目名称:AWFA,代码行数:37,代码来源:lib.php
示例11: tag_stats_related
/**
* Get a list of related tags.
* Returns a list of tags that are the most related to the given tag,
* based on the number of times they have been attached to the same bugs.
* Defaults to a list of five tags.
* @param integer $p_tag_id The tag ID to retrieve statistics on.
* @param integer $p_limit List size.
* @return array Array of tag rows, with share count added
*/
function tag_stats_related($p_tag_id, $p_limit = 5)
{
$c_user_id = auth_get_current_user_id();
$t_subquery = 'SELECT b.id FROM {bug} b
LEFT JOIN {project_user_list} p
ON p.project_id=b.project_id AND p.user_id=' . db_param() . ' JOIN {user} u
ON u.id=' . db_param() . ' JOIN {bug_tag} t
ON t.bug_id=b.id
WHERE ( p.access_level>b.view_state OR u.access_level>b.view_state )
AND t.tag_id=' . db_param();
# 4th Param
$t_query = 'SELECT * FROM {bug_tag}
WHERE tag_id != ' . db_param() . ' AND bug_id IN ( ' . $t_subquery . ' ) ';
$t_result = db_query($t_query, array($p_tag_id, $c_user_id, $c_user_id, $p_tag_id));
$t_tag_counts = array();
while ($t_row = db_fetch_array($t_result)) {
if (!isset($t_tag_counts[$t_row['tag_id']])) {
$t_tag_counts[$t_row['tag_id']] = 1;
} else {
$t_tag_counts[$t_row['tag_id']]++;
}
}
arsort($t_tag_counts);
$t_tags = array();
$i = 1;
foreach ($t_tag_counts as $t_tag_id => $t_count) {
$t_tag_row = tag_get($t_tag_id);
$t_tag_row['count'] = $t_count;
$t_tags[] = $t_tag_row;
$i++;
if ($i > $p_limit) {
break;
}
}
return $t_tags;
}
开发者ID:gtn,项目名称:mantisbt,代码行数:45,代码来源:tag_api.php
示例12: tag_get
if (-1 == $t_tag_row['id']) {
if ($t_can_create) {
$t_tags_create[] = $t_tag_row;
} else {
$t_tags_failed[] = $t_tag_row;
}
} else {
if (-2 == $t_tag_row['id']) {
$t_tags_failed[] = $t_tag_row;
} else {
$t_tags_attach[] = $t_tag_row;
}
}
}
if (0 < $f_tag_select && tag_exists($f_tag_select)) {
$t_tags_attach[] = tag_get($f_tag_select);
}
// failed to attach at least one tag
if (count($t_tags_failed) > 0) {
html_page_top(lang_get('tag_attach_long') . ' ' . bug_format_summary($f_bug_id, SUMMARY_CAPTION));
?>
<br/>
<table class="width75">
<tr class="row-category">
<td colspan="2"><?php
echo lang_get('tag_attach_failed');
?>
</td>
</tr>
<tr class="spacer"><td colspan="2"></td></tr>
<?php
/**
* Change the "value" of a tag, and update the associated 'name'.
*
* @package core_tag
* @category tag
* @access public
* @param int $tagid the id of the tag to modify
* @param string $newrawname the new rawname
* @return bool true on success, false otherwise
*/
function tag_rename($tagid, $newrawname)
{
global $COURSE, $DB;
$norm = tag_normalize($newrawname, TAG_CASE_ORIGINAL);
if (!($newrawname_clean = array_shift($norm))) {
return false;
}
if (!($newname_clean = core_text::strtolower($newrawname_clean))) {
return false;
}
// Prevent the rename if a tag with that name already exists
if ($existing = tag_get('name', $newname_clean, 'id, name, rawname')) {
if ($existing->id != $tagid) {
// Another tag already exists with this name
return false;
}
}
if ($tag = tag_get('id', $tagid, 'id, userid, name, rawname')) {
// Store the name before we change it.
$oldname = $tag->name;
$tag->rawname = $newrawname_clean;
$tag->name = $newname_clean;
$tag->timemodified = time();
$DB->update_record('tag', $tag);
$event = \core\event\tag_updated::create(array('objectid' => $tag->id, 'relateduserid' => $tag->userid, 'context' => context_system::instance(), 'other' => array('name' => $newname_clean, 'rawname' => $newrawname_clean)));
$event->set_legacy_logdata(array($COURSE->id, 'tag', 'update', 'index.php?id=' . $tag->id, $oldname . '->' . $tag->name));
$event->trigger();
return true;
}
return false;
}
请发表评论