You are here

function tagclouds_build_weighted_tags in TagCloud 7

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

$tags: A list of <em>objects</em> with the following attributes: $tag->count, $tag->tid, $tag->name and $tag->vid. Each Tag will be calculated and turned into a tag. Refer to tagclouds_get__tags() for an example.

$steps: The amount of tag-sizes you will be using. If you give "12" you still 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 tagclouds_build_weighted_tags()
tagclouds_get_tags in ./tagclouds.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

./tagclouds.module, line 326

Code

function tagclouds_build_weighted_tags($tags, $steps = 6) {

  // Find minimum and maximum log-count. By our MatheMagician Steven Wittens aka
  // UnConeD.
  $tags_tmp = array();
  $min = 1000000000.0;
  $max = -1000000000.0;
  foreach ($tags as $id => $tag) {
    $tag->number_of_posts = $tag->count;
    $tag->weightcount = log($tag->count);
    $min = min($min, $tag->weightcount);
    $max = max($max, $tag->weightcount);
    $tags_tmp[$id] = $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_tmp as $key => $value) {
    $tags[$key]->weight = 1 + floor($steps * ($value->weightcount - $min) / $range);
  }
  return $tags;
}