You are here

function tagadelic_build_weighted_tags in Tagadelic 6

Same name and namespace in other branches
  1. 5 tagadelic.module \tagadelic_build_weighted_tags()
  2. 7 tagadelic.module \tagadelic_build_weighted_tags()

API that returns an array with weighted tags.

This is the hard part. People with better ideas are very very welcome to send these to ber@webschuur.com. Distribution is one thing that needs attention.

Parameters

$result: A query result, any query result that contains an <em>object</em> with the following attributes: $tag->count, $tag->tid, $tag->name and $tag->vid. Refer to tagadelic_get_weighted_tags() for an example.

$steps: The amount of tag-sizes you will be using. If you give "12" you sill get six different "weights". Defaults to 6 and is optional.

Return value

An <em>unordered</em> array with tags-objects, containing the attribute $tag->weight.

1 call to tagadelic_build_weighted_tags()
tagadelic_get_weighted_tags in ./tagadelic.module
Function that gets the information from the database, passes it along to the weight builder and returns these weighted tags. Note that the tags are unordered at this stage, hence they need ordering either by calling our api or by your own ordering data.

File

./tagadelic.module, line 321

Code

function tagadelic_build_weighted_tags($result, $steps = 6) {

  // Find minimum and maximum log-count. By our MatheMagician Steven Wittens aka
  // UnConeD.
  $tags = array();
  $min = 1000000000.0;
  $max = -1000000000.0;
  while ($tag = db_fetch_object($result)) {
    $tag->number_of_posts = $tag->count;
    $tag->count = log($tag->count);
    $min = min($min, $tag->count);
    $max = max($max, $tag->count);
    $tags[$tag->tid] = $tag;
  }

  // Note: we need to ensure the range is slightly too large to make sure even
  // the largest element is rounded down.
  $range = max(0.01, $max - $min) * 1.0001;
  foreach ($tags as $key => $value) {
    $tags[$key]->weight = 1 + floor($steps * ($value->count - $min) / $range);
  }
  return $tags;
}