aboutsummaryrefslogtreecommitdiff
path: root/engine/lib
diff options
context:
space:
mode:
authoricewing <icewing@36083f99-b078-4883-b0ff-0f9b5a30f544>2008-05-21 14:55:44 +0000
committericewing <icewing@36083f99-b078-4883-b0ff-0f9b5a30f544>2008-05-21 14:55:44 +0000
commit5f4c2a7e1af0a4c51474b26fa045b6330997e38f (patch)
treedbcaec1996e276f22b136c37633d7b575e9898c2 /engine/lib
parent11c1b520e80ced1a7f226da0bd5faaaffb11becf (diff)
downloadelgg-5f4c2a7e1af0a4c51474b26fa045b6330997e38f.tar.gz
elgg-5f4c2a7e1af0a4c51474b26fa045b6330997e38f.tar.bz2
Marcus Povey <marcus@dushka.co.uk>
* Added tag cloud functions git-svn-id: https://code.elgg.org/elgg/trunk@669 36083f99-b078-4883-b0ff-0f9b5a30f544
Diffstat (limited to 'engine/lib')
-rw-r--r--engine/lib/tags.php68
1 files changed, 68 insertions, 0 deletions
diff --git a/engine/lib/tags.php b/engine/lib/tags.php
new file mode 100644
index 000000000..9b675c018
--- /dev/null
+++ b/engine/lib/tags.php
@@ -0,0 +1,68 @@
+<?php
+ /**
+ * Elgg tags
+ * Functions for managing tags and tag clouds.
+ *
+ * @package Elgg
+ * @subpackage Core
+ * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
+ * @author Marcus Povey <marcus@dushka.co.uk>
+ * @copyright Curverider Ltd 2008
+ * @link http://elgg.org/
+ */
+
+
+ /**
+ * The algorithm working out the size of font based on the number of tags.
+ * This is quick and dirty.
+ */
+ function calculate_tag_size($min, $max, $number_of_tags)
+ {
+ $buckets = 6;
+
+ $delta = (($max - $min) / $buckets);
+ $thresholds = array();
+
+ for ($n=1; $n <= $buckets; $n++) {
+ $thresholds[$n-1] = ($min + $n) * $delta;
+ }
+
+ // Correction
+ if ($thresholds[$buckets-1]>$max) $thresholds[$buckets-1] = $max;
+
+ $size = 0;
+ for ($n = 0; $n < count($thresholds); $n++) {
+ if ($number_of_tags >= $thresholds[$n])
+ $size = $n;
+ }
+
+ return $size;
+ }
+
+ /**
+ * This function generates an array of tags with a weighting.
+ *
+ * @param array $tags The array of tags.
+ * @return An associated array of tags with a weighting, this can then be mapped to a display class.
+ */
+ function generate_tag_cloud(array $tags)
+ {
+ $cloud = array();
+
+ $min = 65535;
+ $max = 0;
+
+ foreach ($tags as $tag)
+ {
+ $cloud[$tag]++;
+
+ if ($cloud[$tag]>$max) $max = $cloud[$tag];
+ if ($cloud[$tag]<$min) $min = $cloud[$tag];
+ }
+
+ foreach ($cloud as $k => $v)
+ $cloud[$k] = calculate_tag_size($min, $max, $v);
+
+ return $cloud;
+ }
+?> \ No newline at end of file