CRM-18215 Fix grouping so that Merge activities are not sometimes lost
authoreileen <emcnaughton@wikimedia.org>
Thu, 7 Apr 2016 04:35:32 +0000 (16:35 +1200)
committerEileen <eileen@fuzion.co.nz>
Sat, 23 Apr 2016 03:51:14 +0000 (03:51 +0000)
Change-Id: I729703c51768955b02e8d85c1297fdf10c90cbe1

CRM/Logging/ReportSummary.php
CRM/Report/Form/Contact/LoggingSummary.php

index 402ab2f9a6a98716efd47b7e5733e9878a0149fb..b14c100bb79d637a58402a40d68093a273cf87bf 100644 (file)
@@ -127,6 +127,10 @@ class CRM_Logging_ReportSummary extends CRM_Report_Form {
         'table_name' => 'log_civicrm_activity_contact',
         'log_type' => 'Activity',
         'field' => 'activity_id',
+        'extra_joins' => array(
+          'table' => 'log_civicrm_activity',
+          'join' => 'extra_table.id = entity_log_civireport.activity_id',
+        ),
 
         'bracket_info' => array(
           'entity_column' => 'activity_type_id',
@@ -202,6 +206,13 @@ class CRM_Logging_ReportSummary extends CRM_Report_Form {
       $this->_selectAliases[] = $alias;
       return "activity_id";
     }
+    if ($fieldName == 'log_grouping') {
+      if ($this->currentLogTable != 'log_civicrm_activity_contact') {
+        return 1;
+      }
+      $mergeActivityID = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Contact Merged');
+      return " IF (entity_log_civireport.log_action = 'Insert' AND extra_table.activity_type_id = $mergeActivityID , GROUP_CONCAT(entity_log_civireport.contact_id), 1) ";
+    }
   }
 
   public function where() {
@@ -216,7 +227,7 @@ class CRM_Logging_ReportSummary extends CRM_Report_Form {
     $this->beginPostProcess();
     $rows = array();
 
-    $tempColumns = "id int(10)";
+    $tempColumns = "id int(10),  log_civicrm_entity_log_grouping varchar(32)";
     if (!empty($this->_params['fields']['log_action'])) {
       $tempColumns .= ", log_action varchar(64)";
     }
@@ -295,7 +306,7 @@ class CRM_Logging_ReportSummary extends CRM_Report_Form {
     $sql = "{$this->_select}
 FROM civicrm_temp_civireport_logsummary entity_log_civireport
 WHERE {$logTypeTableClause}
-GROUP BY log_civicrm_entity_log_date, log_civicrm_entity_log_type_label, log_civicrm_entity_log_conn_id, log_civicrm_entity_log_user_id, log_civicrm_entity_altered_contact_id
+GROUP BY log_civicrm_entity_log_date, log_civicrm_entity_log_type_label, log_civicrm_entity_log_conn_id, log_civicrm_entity_log_user_id, log_civicrm_entity_altered_contact_id, log_civicrm_entity_log_grouping
 ORDER BY log_civicrm_entity_log_date DESC {$this->_limit}";
     $sql = str_replace('modified_contact_civireport.display_name', 'entity_log_civireport.altered_contact', $sql);
     $sql = str_replace('modified_contact_civireport.id', 'entity_log_civireport.altered_contact_id', $sql);
index 238888874bf99fc8c88c72a2929d39b43b87bd0f..17c0b3f832ac4bf7c247c20658a7fd83613b18d3 100644 (file)
@@ -53,6 +53,11 @@ class CRM_Report_Form_Contact_LoggingSummary extends CRM_Logging_ReportSummary {
             'no_display' => TRUE,
             'required' => TRUE,
           ),
+          'log_grouping' => array(
+            'required' => TRUE,
+            'title' => ts('Extra information to control grouping'),
+            'no_display' => TRUE,
+          ),
           'log_action' => array(
             'default' => TRUE,
             'title' => ts('Action'),
@@ -174,6 +179,7 @@ class CRM_Report_Form_Contact_LoggingSummary extends CRM_Logging_ReportSummary {
     $newRows = array();
 
     foreach ($rows as $key => &$row) {
+      $isMerge = 0;
       $baseQueryCriteria = "reset=1&log_conn_id={$row['log_civicrm_entity_log_conn_id']}";
       if (!CRM_Logging_Differ::checkLogCanBeUsedWithNoLogDate($row['log_civicrm_entity_log_date'])) {
         $baseQueryCriteria .= '&log_date=' . CRM_Utils_Date::isoToMysql($row['log_civicrm_entity_log_date']);
@@ -196,6 +202,10 @@ class CRM_Report_Form_Contact_LoggingSummary extends CRM_Logging_ReportSummary {
         if ($entity) {
           $row['log_civicrm_entity_altered_contact'] = $row['log_civicrm_entity_altered_contact'] . " [{$entity}]";
         }
+        if ($entity == 'Contact Merged') {
+          $isMerge = 1;
+        }
+
       }
       $row['altered_by_contact_display_name_link'] = CRM_Utils_System::url('civicrm/contact/view', 'reset=1&cid=' . $row['log_civicrm_entity_log_user_id']);
       $row['altered_by_contact_display_name_hover'] = ts("Go to contact summary");
@@ -228,6 +238,11 @@ class CRM_Report_Form_Contact_LoggingSummary extends CRM_Logging_ReportSummary {
 
       $key = $date . '_' .
         $row['log_civicrm_entity_log_type'] . '_' .
+        // This ensures merge activities are not 'lost' by aggregation.
+        // I would prefer not to lose other entities either but it's a balancing act as
+        // described in https://issues.civicrm.org/jira/browse/CRM-12867 so adding this criteria
+        // while hackish saves us from figuring out if the original decision is still good.
+        $isMerge .
         $row['log_civicrm_entity_log_conn_id'] . '_' .
         $row['log_civicrm_entity_log_user_id'] . '_' .
         $row['log_civicrm_entity_altered_contact_id'];
@@ -265,6 +280,11 @@ INNER JOIN civicrm_contact modified_contact_civireport
         ON (fk_table.{$detail['fk']} = modified_contact_civireport.id {$clause})";
     }
 
+    if (!empty($detail['extra_joins'])) {
+      $joinClause .= "
+INNER JOIN `{$this->loggingDB}`.{$detail['extra_joins']['table']} extra_table ON {$detail['extra_joins']['join']}";
+    }
+
     $this->_from = "
 FROM `{$this->loggingDB}`.$tableName entity_log_civireport
 {$joinClause}