diff options
author | Tim Almdal <tnalmdal@shaw.ca> | 2008-11-25 18:14:52 +0000 |
---|---|---|
committer | Tim Almdal <tnalmdal@shaw.ca> | 2008-11-25 18:14:52 +0000 |
commit | d554adc484faa8bd605d230f49b8e3db2bc595b9 (patch) | |
tree | f9bdba45b90e20ffd9bd090f72e8b4363be267a3 | |
parent | 53f45b3079d08813b427305ed12ac91530d7e06e (diff) |
1) Changed how the test tags are generated to be able to create a better distribution
2) Added a new helper function "load_buckets" assign the class suffix to each tag
3) Created a unit test to test the load_buckets function
-rw-r--r-- | core/controllers/welcome.php | 29 | ||||
-rw-r--r-- | modules/tag/helpers/tag.php | 54 | ||||
-rw-r--r-- | modules/tag/tests/Tag_Test.php | 40 |
3 files changed, 116 insertions, 7 deletions
diff --git a/core/controllers/welcome.php b/core/controllers/welcome.php index 95013cfa..83e6a608 100644 --- a/core/controllers/welcome.php +++ b/core/controllers/welcome.php @@ -218,12 +218,7 @@ class Welcome_Controller extends Template_Controller { $items = ORM::factory("item")->find_all()->as_array(); if (!empty($items)) { - $tags = array( - "animation", "art", "blind", "blog", "bug-tracker", "bugs20", "canvas", - "classification", "cocktail", "exhibtion", "forum", "geo-tagging", "german", "germany", - "glaser", "graffiti", "illustration", "ITP", "javascript", "miami", "miknow", "nyc", "NYU", - "ontology", "open-source", "project", "school-of-information", "screenshot", "shiftspace", - "shop", "tagging", "talkingpoints", "university-of-michigan", "usability", "writing"); + $tags = $this->_generateTags($count); while ($count-- > 0) { $tag_name = $tags[array_rand($tags)]; @@ -236,6 +231,27 @@ class Welcome_Controller extends Template_Controller { url::redirect("welcome"); } + private function _generateTags($number){ + + list($usec, $sec) = explode(' ', microtime()); + srand((float) $sec + ((float) $usec * 100000)); + + $validchars = "0123456789abcdfghjkmnpqrstvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; + + $tags = array(); + + for ($tag_count = 0; $tag_count < $number; $tag_count++) { + $tag = ""; + $tag_length = rand(3, 16); + for ($counter = 0; $counter < $tag_length; $counter++) { + $tag .= substr($validchars, rand(0, strlen($validchars)-1), 1);; + } + $tags[] = $tag; + } + + return $tags; + } + public function session($key) { Session::instance()->set($key, $this->input->get("value", false)); $this->auto_render = false; @@ -394,4 +410,5 @@ class Welcome_Controller extends Template_Controller { ORM::factory("group")->where("name", $name)->find()->delete(); url::redirect("welcome"); } + } diff --git a/modules/tag/helpers/tag.php b/modules/tag/helpers/tag.php index 1090aa42..6dfbcf38 100644 --- a/modules/tag/helpers/tag.php +++ b/modules/tag/helpers/tag.php @@ -18,6 +18,8 @@ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ class tag_Core { + public static $NUMBER_OF_BUCKETS = 7; + /** * Associate a tag with an item. Create the tag if it doesn't already exist. * @@ -47,4 +49,56 @@ class tag_Core { $tag->save(); } } + + /** + * Assign a css class to the tags based on frequency of use. Optionally, allow a filter value + * which allows for adjusting the granularity of the cloud by ignoring any frequencies below + * the specified value. This code is based on the code from: http://www.hawkee.com/snippet/1485/ + * + * @param int $filter Minimum frequency to be included in the tag cloud + * @return array List of tags each entry has the following format: + * array("name" => "tag_name", "count" => "frequency", "class" => "bucket") + */ + public static function load_buckets($filter=1) { + $tag_list = array(); + $tags = ORM::factory("tag") + ->where("count >=", $filter) + ->orderby("count", "ASC") + ->find_all() + ->as_array(); + if (count($tags) > 0) { + $min_tags = count($tags) / self::$NUMBER_OF_BUCKETS; + $bucket_count = 0; + $bucket_items = 0; + $tags_set = 0; + + foreach($tags as $key => $tag) { + if (($bucket_items >= $min_tags) && $last_count != $tag->count && + $bucket_count < self::$NUMBER_OF_BUCKETS) { + $bucket_count++; + $bucket_items = 0; + + // Calculate a new minimum number if tags for the remaining classes. + $remaining_tags = count($tags) - $tags_set; + $min_tags = $remaining_tags / self::$NUMBER_OF_BUCKETS; + } + + // Set the tag to the current class + $tag_list[$key] = array("name" => $tag->name, "count" => $tag->count, + "class" => "$bucket_count"); + $bucket_items++; + $tags_set++; + $last_count = $tag->count; + } + usort($tag_list, array("tag", "alphasort")); + } + return $tag_list; + } + + public function alphasort($tag1, $tag2) { + if ($tag1["name"] == $tag2["name"]) { + return 0; + } + return $tag1["name"] < $tag2["name"] ? -1 : 1; + } } diff --git a/modules/tag/tests/Tag_Test.php b/modules/tag/tests/Tag_Test.php index 989efb3c..0a7f878c 100644 --- a/modules/tag/tests/Tag_Test.php +++ b/modules/tag/tests/Tag_Test.php @@ -38,5 +38,43 @@ class Tag_Test extends Unit_Test_Case { $tag = ORM::factory("tag")->where("name", $tag1)->find(); $this->assert_true(2, $tag->count); } - // @todo put some tests here + + public function load_buckets_test() { + + $tags = array(); + for ($tag_count = 1; $tag_count <= 8; $tag_count++) { + $rand = rand(); + $album = album::create(1, $rand, $rand, $rand); + for ($idx = 0; $idx < $tag_count; $idx++) { + tag::add($album, "tag$idx"); + } + } + + $tag_list = tag::load_buckets(); + Kohana::log("debug", print_r($tag_list, true)); + $expected_tag_list = array( + array("name" => "tag0", "count" => 8, "class" => 5), + array("name" => "tag1", "count" => 9, "class" => 6), + array("name" => "tag2", "count" => 6, "class" => 4), + array("name" => "tag3", "count" => 5, "class" => 3), + array("name" => "tag4", "count" => 4, "class" => 2), + array("name" => "tag5", "count" => 3, "class" => 1), + array("name" => "tag6", "count" => 2, "class" => 0), + array("name" => "tag7", "count" => 1, "class" => 0) + ); + $this->assert_equal($expected_tag_list, $tag_list, "incorrect non filtered tag list"); + + $tag_list = tag::load_buckets(2); + Kohana::log("debug", print_r($tag_list, true)); + $expected_tag_list = array( + array("name" => "tag0", "count" => 8, "class" => 5), + array("name" => "tag1", "count" => 9, "class" => 6), + array("name" => "tag2", "count" => 6, "class" => 4), + array("name" => "tag3", "count" => 5, "class" => 3), + array("name" => "tag4", "count" => 4, "class" => 2), + array("name" => "tag5", "count" => 3, "class" => 1), + array("name" => "tag6", "count" => 2, "class" => 0) + ); + $this->assert_equal($expected_tag_list, $tag_list, "incorrect filtered tag list"); + } }
\ No newline at end of file |