From 1b08b468b6f8dd8e3a5cce42115fcedb199464ab Mon Sep 17 00:00:00 2001 From: Allen Shaw Date: Tue, 30 May 2017 16:26:53 -0500 Subject: [PATCH] Fix CRM-20660: Total Opens should count without DISTINCT keyword. --- CRM/Report/Form/Mailing/Summary.php | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/CRM/Report/Form/Mailing/Summary.php b/CRM/Report/Form/Mailing/Summary.php index 0431502b1d..9ff9add70f 100644 --- a/CRM/Report/Form/Mailing/Summary.php +++ b/CRM/Report/Form/Mailing/Summary.php @@ -350,6 +350,24 @@ class CRM_Report_Form_Mailing_Summary extends CRM_Report_Form { 'civicrm_mailing_event_unsubscribe', ); + // Define a list of columns that should be counted with the DISTINCT + // keyword. For example, civicrm_mailing_event_opened.unique_open_count + // should display the number of unique records, whereas something like + // civicrm_mailing_event_opened.open_count should display the total number. + // Each string here is in the form $tableName.$fieldName, where $tableName + // is the key in $this->_columns, and $fieldName is the key in that array's + // ['fields'] array. + // Reference: CRM-20660 + $distinct_count_columns = array( + 'civicrm_mailing_event_queue.queue_count', + 'civicrm_mailing_event_delivered.delivered_count', + 'civicrm_mailing_event_bounce.bounce_count', + 'civicrm_mailing_event_opened.unique_open_count', + 'civicrm_mailing_event_trackable_url_open.click_count', + 'civicrm_mailing_event_unsubscribe.unsubscribe_count', + 'civicrm_mailing_event_unsubscribe.optout_count', + ); + $select = array(); $this->_columnHeaders = array(); foreach ($this->_columns as $tableName => $table) { @@ -373,7 +391,15 @@ class CRM_Report_Form_Mailing_Summary extends CRM_Report_Form { } else { if (in_array($tableName, $count_tables)) { - $select[] = "count(DISTINCT {$field['dbAlias']}) as {$tableName}_{$fieldName}"; + // Use the DISTINCT keyword appropriately, based on the contents + // of $distinct_count_columns. + if (in_array("{$tableName}.{$fieldName}", $distinct_count_columns)) { + $distinct = 'DISTINCT'; + } + else { + $distinct = ''; + } + $select[] = "count($distinct {$field['dbAlias']}) as {$tableName}_{$fieldName}"; } else { $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}"; -- 2.25.1