function commerce_reports_patterns_association_rules in Commerce Reporting 7.4
Same name and namespace in other branches
- 7.3 modules/patterns/commerce_reports_patterns.batch.inc \commerce_reports_patterns_association_rules()
1 string reference to 'commerce_reports_patterns_association_rules'
- commerce_reports_patterns_form_submit in modules/
patterns/ commerce_reports_patterns.admin.inc
File
- modules/
patterns/ commerce_reports_patterns.batch.inc, line 154
Code
function commerce_reports_patterns_association_rules($min_confidence, &$context) {
if (empty($context['sandbox'])) {
$context['sandbox']['length'] = 2;
$context['sandbox']['partition'] = 0;
$context['sandbox']['set'] = 0;
$context['results']['count'] = 0;
$context['results']['rules'] = array();
db_query('TRUNCATE {commerce_reports_patterns}');
}
if ($context['sandbox']['set'] > count($context['results']['frequent_sets'][$context['sandbox']['length']][$context['sandbox']['partition']]['sets']) - 1) {
$context['sandbox']['partition']++;
$context['sandbox']['set'] = 0;
}
if ($context['sandbox']['partition'] > count($context['results']['frequent_sets'][$context['sandbox']['length']]) - 1) {
$context['sandbox']['length']++;
$context['sandbox']['partition'] = 0;
}
if ($context['sandbox']['length'] > count($context['results']['frequent_sets'])) {
$context['finished'] = 1;
return;
}
else {
$context['finished'] = 0;
}
$set =& $context['results']['frequent_sets'][$context['sandbox']['length']][$context['sandbox']['partition']]['sets'][$context['sandbox']['set']];
$items =& $set['items'];
$set_support_count = count($set['tidlist']);
$count = count($items);
$members = pow(2, $count);
for ($i = 0; $i < $members; $i++) {
$b = sprintf('%0' . $count . 'b', $i);
$rule = new stdClass();
$rule->size = $count;
$rule->if_clause = array();
$rule->then_clause = array();
for ($j = 0; $j < $count; $j++) {
if ($b[$j] == '1') {
$rule->if_clause[] = $items[$j];
}
else {
$rule->then_clause[] = $items[$j];
}
}
if (!empty($rule->then_clause) && ($if_count = count($rule->if_clause))) {
$if_support_count = db_query("SELECT COUNT(*) FROM (SELECT order_id FROM {commerce_line_item} WHERE line_item_label IN (:sku) GROUP BY order_id HAVING COUNT(line_item_id) = :count) cli", array(
':sku' => $rule->if_clause,
':count' => $if_count,
))
->fetchField();
if ($if_support_count && ($confidence = $set_support_count / $if_support_count) >= $min_confidence) {
$rule->confidence = $confidence;
drupal_write_record('commerce_reports_patterns', $rule);
$context['results']['count']++;
}
}
// Clear memory after set processing.
unset($set);
}
$context['sandbox']['set']++;
$context['message'] = t('Found %count association rules', array(
'%count' => $context['results']['count'],
));
}