From a370e4e4fa00f5818cadadda70a830a2b77afb8b Mon Sep 17 00:00:00 2001 From: Sam Hemelryk Date: Wed, 20 Apr 2011 12:04:02 +0800 Subject: [PATCH] MDL-24355 Added code to delete any tag correlations that wern't just identified --- tag/lib.php | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/tag/lib.php b/tag/lib.php index 47c8c003c52..4d9916680aa 100644 --- a/tag/lib.php +++ b/tag/lib.php @@ -831,8 +831,7 @@ function tag_compute_correlations($mincorrelation = 2) { // correlationid : This is the id of the row in the tag_correlation table that // relates to the tagid field and will be NULL if there are no // existing correlations - $sql = 'SELECT pairs.tagid, pairs.correlation, pairs.ocurrences, - co.id AS correlationid, co.correlatedtags + $sql = 'SELECT pairs.tagid, pairs.correlation, pairs.ocurrences, co.id AS correlationid FROM ( SELECT ta.tagid, tb.tagid AS correlation, COUNT(*) AS ocurrences FROM {tag_instance} ta @@ -850,12 +849,19 @@ function tag_compute_correlations($mincorrelation = 2) { $tagcorrelation->tagid = null; $tagcorrelation->correlatedtags = array(); + // We store each correction id in this array so we can remove any correlations + // that no longer exist. + $correlations = array(); + // Iterate each row of the result set and build them into tag correlations. foreach ($rs as $row) { if ($row->tagid != $tagcorrelation->tagid) { // The tag id has changed so its now time to process the tag // correlation information we have. - tag_process_computed_correlation($tagcorrelation); + $tagcorrelationid = tag_process_computed_correlation($tagcorrelation); + if ($tagcorrelationid) { + $correlations[] = $tagcorrelationid; + } // Now we reset the tag correlation object so we can reuse it and set it // up for the current record. $tagcorrelation = new stdClass; @@ -866,10 +872,18 @@ function tag_compute_correlations($mincorrelation = 2) { $tagcorrelation->correlatedtags[] = $row->correlation; } // Update the current correlation after the last record. - tag_process_computed_correlation($tagcorrelation); + $tagcorrelationid = tag_process_computed_correlation($tagcorrelation); + if ($tagcorrelationid) { + $correlations[] = $tagcorrelationid; + } + // Close the recordset $rs->close(); + + // Remove any correlations that weren't just identified + list($sql, $params) = $DB->get_in_or_equal($correlations, SQL_PARAMS_NAMED, 'param0000', false); + $DB->delete_records_select('tag_correlation', 'id '.$sql, $params); } /** @@ -880,7 +894,7 @@ function tag_compute_correlations($mincorrelation = 2) { * property that is an array. * * @param stdClass $tagcorrelation - * @return bool True if the function completed, false if something was wrong. + * @return int The id of the tag correlation that was just processed. */ function tag_process_computed_correlation(stdClass $tagcorrelation) { global $DB; @@ -899,9 +913,9 @@ function tag_process_computed_correlation(stdClass $tagcorrelation) { $DB->update_record('tag_correlation', $tagcorrelation); } else { // This is a new correlation to insert - $DB->insert_record('tag_correlation', $tagcorrelation); + $tagcorrelation->id = $DB->insert_record('tag_correlation', $tagcorrelation); } - return true; + return $tagcorrelation->id; } /**