From: scyrma <scyrma>
Date: Thu, 24 Jan 2008 08:39:38 +0000 (+0000)
Subject: MDL-13124 : simplify and fix the tag correlation logic.
X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=c0491d4aca1feeb682ffb12723c1b1bb8e3f8d03;p=moodle.git

MDL-13124 : simplify and fix the tag correlation logic.
---

diff --git a/tag/lib.php b/tag/lib.php
index 4e8113ea72..b287d801df 100644
--- a/tag/lib.php
+++ b/tag/lib.php
@@ -835,7 +835,7 @@ function update_tag_correlations($item_type, $item_id) {
  * @param number $min_correlation cutoff percentage (optional, default is 0.25)
  * @param int $limitnum return a subset comprising this many records (optional, default is 10)
  */
-function cache_correlated_tags($tag_name_or_id, $min_correlation=0.25, $limitnum=10) {
+function cache_correlated_tags($tag_name_or_id, $min_correlation=2, $limitnum=10) {
     global $CFG;
 
     $tag_id = tag_id_from_string($tag_name_or_id);
@@ -845,17 +845,17 @@ function cache_correlated_tags($tag_name_or_id, $min_correlation=0.25, $limitnum
     $query = "SELECT tb.tagid , COUNT(*) nr
                 FROM {$CFG->prefix}tag_instance ta
                      INNER JOIN {$CFG->prefix}tag_instance tb ON ta.itemid = tb.itemid
-               WHERE ta.tagid = {$tag_id}
+               WHERE ta.tagid = {$tag_id} AND tb.tagid != {$tag_id}
             GROUP BY tb.tagid
             ORDER BY nr DESC";
 
     $correlated = array();
 
-    if ($tag_correlations = get_records_sql($query, 0, $limitnum)) {
-        $cutoff = $tag_correlations[$tag_id]->nr * $min_correlation;
-
+    // Correlated tags happen when they appear together in more occasions 
+    // than $min_correlation.
+    if ($tag_correlations = get_records_sql($query, 0, 10)) {
         foreach($tag_correlations as $correlation) {
-            if($correlation->nr >= $cutoff && $correlation->tagid != $tag_id ){
+            if($correlation->nr >= $min_correlation){
                 $correlated[] = $correlation->tagid;
             }
         }