Skip to content
This repository has been archived by the owner on Apr 13, 2022. It is now read-only.

Commit

Permalink
Words To Avoid
Browse files Browse the repository at this point in the history
Implementation of the words to avoid functionality
- added to MGTags Component a method listing tags with a compound weight above a certain level
- added wordstoavoid to Zentag, words that have been listed as to avoid are weight as 0
- added type column to tag_use to have a record who was repoinsible for the weigthing of this tag_use
- added words to avoid threshold input field to ZenTag
- smaller clean ups in GamesController
- added comments to several methods
  • Loading branch information
novazembla committed Sep 13, 2011
1 parent 22f8ca4 commit 19f7e40
Show file tree
Hide file tree
Showing 15 changed files with 316 additions and 54 deletions.
Binary file added documentation/mg_database_schema_v0.6.2.mwb
Binary file not shown.
119 changes: 112 additions & 7 deletions www/protected/components/MGTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,24 @@
class MGTags {

/**
* @param array $image_ids array of the image(s) which tags shall be retrieved
* This method gets the tags that have been used for the images identified by $images_ids.
* Only tag uses with a weight >=1 will be regarded.
*
* It will return an array of arrays
*
* array(
* image_id = array(
* tag_id => array(
* "tag" => "tag.tag" // the value of the tag column in the database
* )
* ...
* )
* ...
* )
*
* @param array $image_ids array of the image(s) which tags shall be retrieved
* @param int $user_id if set only tag that have been used by the user will be shown
* @return array the found tags for the image(s)
*/
public static function getTags($image_ids, $user_id=null) {
$tags = array();
Expand All @@ -26,7 +42,8 @@ public static function getTags($image_ids, $user_id=null) {
->leftJoin('{{tag}} t', 't.id = tu.tag_id')
->leftJoin('{{game_submission}} gs', 'gs.id = tu.game_submission_id')
->leftJoin('{{session}} s', 's.id = gs.session_id')
->where(array('and', 's.user_id=:userID', array('in', 'tu.image_id', array_values($image_ids))), array(':userID' => $user_id))
->where(array('and', 's.user_id=:userID', 'tu.weight >= 1', array( 'in', 'tu.image_id', array_values($image_ids))),
array(':userID' => $user_id))
->queryAll();

} else {
Expand All @@ -35,7 +52,7 @@ public static function getTags($image_ids, $user_id=null) {
->select('tu.image_id, t.id as tag_id, t.tag')
->from('{{tag_use}} tu')
->leftJoin('{{tag}} t', 't.id = tu.tag_id')
->where(array('in', 'tu.image_id', array_values($image_ids)))
->where(array('and', 'tu.weight >= 1', array('in', 'tu.image_id', array_values($image_ids))))
->queryAll();

}
Expand All @@ -50,13 +67,102 @@ public static function getTags($image_ids, $user_id=null) {
}

/**
* @param mixed $image_ids array or int of the image(s) which tags shall be retrieved
* This method gets the tags that have been used for the images identified by $images_ids.
* Only tag uses with a weight >=1 will be regarded.
*
* It will return an array of arrays
*
* array(
* image_id = array(
* tag_id => array(
* "tag" => "tag.tag" // the value of the tag column in the database
* )
* ...
* )
* ...
* )
*
* @param array $image_ids array of the image(s) which tags shall be retrieved
* @param int $user_id if set only tag that have been used by the user will be shown
* @return array the found tags for the image(s)
*/
public static function getUsersTags($image_ids, $user_id) {
return self::getTags($image_ids, $user_id);
}

/**
* This method gets the tags with a certain compound weight that have been used for the images identified by $images_ids.
*
* THE used SQL is:
*
* SELECT tu.image_id, tu.tag_id, t.tag, SUM(tu.weight) as total
* FROM tag_use tu
* LEFT JOIN tag t ON t.id=tu.tag_id
* WHERE tu.weight >= 1 AND tu.image_id IN ($image_ids)
* GROUP BY tu.image_id, tu.tag_id, t.tag
* HAVING total >= $weight
* ORDER BY tu.image_id, total
*
* It will return an array of arrays
*
* array(
* image_id = array(
* tag_id => array(
* "tag" => "tag.tag" // the value of the tag column in the database
* "total" => "SUM(tu.weight)" // the total weight of tag uses for that tag and image
* )
* ...
* )
* ...
* )
*
* @param array $image_ids array of the image(s) which tags shall be retrieved
* @param int $weight return only tags that have a compound weight equal or great than this value
* @param int $user_id if set only tag that have been used by the user will be shown
* @return array the found tags for the image(s)
*/
public static function getTagsByWeightThreshold($image_ids, $weight, $user_id=null) {
$tags = array();
$used_tags = array();

$builder = Yii::app()->db->getCommandBuilder();

if ($user_id) {
$used_tags = Yii::app()->db->createCommand()
->select('tu.image_id, tu.tag_id, t.tag, SUM(tu.weight) as total')
->from('{{tag_use}} tu')
->leftJoin('{{tag}} t', 't.id = tu.tag_id')
->leftJoin('{{game_submission}} gs', 'gs.id = tu.game_submission_id')
->leftJoin('{{session}} s', 's.id = gs.session_id')
->where(array('and', 's.user_id=:userID', 'tu.weight >= 1', array('in', 'tu.image_id', array_values($image_ids))),
array(':userID' => $user_id))
->group('tu.image_id, tu.tag_id, t.tag')
->having('total >= :weight', array(":weight" => $weight))
->order('tu.image_id, total DESC')
->queryAll();

} else {
$used_tags = Yii::app()->db->createCommand()
->select('tu.image_id, tu.tag_id, t.tag, SUM(tu.weight) as total')
->from('{{tag_use}} tu')
->leftJoin('{{tag}} t', 't.id = tu.tag_id')
->where(array('and', 'tu.weight >= 1', array('in', 'tu.image_id', array_values($image_ids))))
->group('tu.image_id, tu.tag_id, t.tag')
->having('total >= :weight', array(":weight" => $weight))
->order('tu.image_id, total DESC')
->queryAll();

}
foreach($used_tags as $tag) {
if (!isset($tags[$tag["image_id"]]))
$tags[$tag["image_id"]] = array();

$tags[$tag["image_id"]][$tag["tag_id"]] = array("tag" => $tag["tag"], "total" => $tag["total"]);
}

return $tags;
}

/**
*
* $tags = array(
Expand Down Expand Up @@ -130,6 +236,7 @@ public static function saveTags($tags, $game_submission_id) {
$tag_use_model->tag_id = (int)$tags[$image_id][$tag]["tag_id"];
$tag_use_model->image_id = (int)$image_id;
$tag_use_model->weight = (int)$tags[$image_id][$tag]["weight"];
$tag_use_model->type = (string)$tags[$image_id][$tag]["type"];
$tag_use_model->game_submission_id = (int)$game_submission_id;
$tag_use_model->created = date('Y-m-d H:i:s');

Expand Down Expand Up @@ -163,16 +270,14 @@ public static function parseTags($tags) {
}
return array_unique(array_values($tags));
}

/**
* Used as a callback to trim tags.
* @access private
* @param string $item
* @param string $key
* @return string
*/



public static function trim(&$item, $key) {
$item = preg_replace("/\s/", " ", $item);
while (strpos($item, " ") !== FALSE) {
Expand Down
4 changes: 3 additions & 1 deletion www/protected/data/fbvsettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@
'score_expert' => '3',
'image_width' => '450',
'image_height' => '450',
'words_to_avoid_threshold' => '2',
),
'ZenTagPlayOnceMoveOn' =>
array (
Expand All @@ -165,12 +166,13 @@
'score_expert' => '3',
'image_width' => '450',
'image_height' => '450',
'words_to_avoid_threshold' => '10',
),
),
'settings' =>
array (
'app_name' => 'Meta Data Games Test',
'throttle_interval' => '2500',
'throttle_interval' => '500',
'app_email' => '[email protected]',
'pagination_size' => '25',
'app_upload_path' => '/../uploads',
Expand Down
3 changes: 2 additions & 1 deletion www/protected/data/mg.mysql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -483,8 +483,9 @@ CREATE TABLE IF NOT EXISTS `tag_use` (
`image_id` INT(11) NOT NULL ,
`tag_id` INT(11) NOT NULL ,
`weight` INT(3) NOT NULL DEFAULT 0 ,
`created` DATETIME NOT NULL ,
`type` VARCHAR(64) NOT NULL DEFAULT '' ,
`game_submission_id` INT UNSIGNED NOT NULL ,
`created` DATETIME NOT NULL ,
PRIMARY KEY (`id`) ,
CONSTRAINT `fk_tag_uses_images1`
FOREIGN KEY (`image_id` )
Expand Down
12 changes: 8 additions & 4 deletions www/protected/models/_base/BaseTagUse.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
* @property integer $image_id
* @property integer $tag_id
* @property integer $weight
* @property string $created
* @property string $type
* @property string $game_submission_id
*
* @property string $created
*
* @property TagOriginalVersion[] $tagOriginalVersions
* @property GameSubmission $gameSubmission
* @property Image $image
Expand Down Expand Up @@ -44,8 +45,9 @@ public function rules() {
array('image_id, tag_id, created, game_submission_id', 'required'),
array('image_id, tag_id, weight', 'numerical', 'integerOnly'=>true),
array('game_submission_id', 'length', 'max'=>10),
array('weight', 'default', 'setOnEmpty' => true, 'value' => null),
array('id, image_id, tag_id, weight, created, game_submission_id', 'safe', 'on'=>'search'),
array('type', 'length', 'max'=>45),
array('weight, type', 'default', 'setOnEmpty' => true, 'value' => null),
array('id, image_id, tag_id, weight, created, game_submission_id, type', 'safe', 'on'=>'search'),
);
}

Expand All @@ -71,6 +73,7 @@ public function attributeLabels() {
'weight' => Yii::t('app', 'Weight'),
'created' => Yii::t('app', 'Created'),
'game_submission_id' => null,
'type' => Yii::t('app', 'Type'),
'tagOriginalVersions' => null,
'gameSubmission' => null,
'image' => null,
Expand All @@ -87,6 +90,7 @@ public function search() {
$criteria->compare('weight', $this->weight);
$criteria->compare('created', $this->created, true);
$criteria->compare('game_submission_id', $this->game_submission_id);
$criteria->compare('type', $this->type, true);

return new CActiveDataProvider($this, array(
'criteria' => $criteria,
Expand Down
Loading

0 comments on commit 19f7e40

Please sign in to comment.