Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dev/report#5 - Fix mailing report unique count issue #13322

Merged
merged 3 commits into from
Dec 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CRM/Mailing/BAO/Mailing.php
Original file line number Diff line number Diff line change
Expand Up @@ -2198,6 +2198,7 @@ public static function &report($id, $skipDetails = FALSE, $isSMS = FALSE) {
'forward',
'reply',
'opened',
'opened_unique',
'optout',
) as $key) {
$url = 'mailing/detail';
Expand Down Expand Up @@ -2235,6 +2236,8 @@ public static function &report($id, $skipDetails = FALSE, $isSMS = FALSE) {
break;

case 'opened':
$reportFilter .= "&distinct=0"; // do not use group by clause in report, because same report used for total and unique open
case 'opened_unique':
$url = "mailing/opened";
$searchFilter .= "&mailing_open_status=Y";
break;
Expand Down
17 changes: 11 additions & 6 deletions CRM/Report/Form/Mailing/Opened.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,13 +289,18 @@ public function where() {
}

public function groupBy() {
$groupBys = empty($this->_params['charts']) ? array("civicrm_mailing_event_queue.email_id") : array("{$this->_aliases['civicrm_mailing']}.id");

if (!empty($this->_params['unique_opens_value'])) {
$groupBys[] = "civicrm_mailing_event_queue.id";
$groupBys = array();
// Do not use group by clause if distinct = 0 mentioned in url params. flag is used in mailing report screen, default value is TRUE
// this report is used to show total opened and unique opened
if (CRM_Utils_Request::retrieve('distinct', 'Boolean', CRM_Core_DAO::$_nullObject, FALSE, TRUE)) {
$groupBys = empty($this->_params['charts']) ? array("civicrm_mailing_event_queue.email_id") : array("{$this->_aliases['civicrm_mailing']}.id");
if (!empty($this->_params['unique_opens_value'])) {
$groupBys[] = "civicrm_mailing_event_queue.id";
}
}
if (!empty($groupBys)) {
$this->_groupBy = "GROUP BY " . implode(', ', $groupBys);
}
$this->_select = CRM_Contact_BAO_Query::appendAnyValueToSelect($this->_selectClauses, $groupBys);
$this->_groupBy = "GROUP BY " . implode(', ', $groupBys);
}

public function postProcess() {
Expand Down
2 changes: 1 addition & 1 deletion templates/CRM/Mailing/Page/Report.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
{if $report.mailing.open_tracking}
<tr><td class="label"><a href="{$report.event_totals.links.opened}&distinct=1">{ts}Unique Opens{/ts}</a></td>
<td>{$report.event_totals.opened} ({$report.event_totals.opened_rate|string_format:"%0.2f"}%)</td>
<td>{$report.event_totals.actionlinks.opened}</td></tr>
<td>{$report.event_totals.actionlinks.opened_unique}</td></tr>
<tr><td class="label"><a href="{$report.event_totals.links.opened}">{ts}Total Opens{/ts}</a></td>
<td>{$report.event_totals.total_opened}</td>
<td>{$report.event_totals.actionlinks.opened}</td></tr>
Expand Down
40 changes: 39 additions & 1 deletion tests/phpunit/api/v3/ReportTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public function hookSelectWhere($entity, &$clauses) {
*/
public function testReportTemplateGetRowsContactSummary() {
$description = "Retrieve rows from a report template (optionally providing the instance_id).";
$result = $this->callAPIAndDocument('report_template', 'getrows', array(
$result = $this->callApiSuccess('report_template', 'getrows', array(
'report_id' => 'contact/summary',
'options' => array('metadata' => array('labels', 'title')),
), __FUNCTION__, __FILE__, $description, 'Getrows');
Expand All @@ -211,6 +211,44 @@ public function testReportTemplateGetRowsContactSummary() {
*/
}

/**
* Test getrows on Mailing Opened report.
*/
public function testReportTemplateGetRowsMailingUniqueOpened() {
$description = "Retrieve rows from a mailing opened report template.";
$op = new PHPUnit_Extensions_Database_Operation_Insert();
$op->execute($this->_dbconn,
$this->createFlatXMLDataSet(
dirname(__FILE__) . '/../../CRM/Mailing/BAO/queryDataset.xml'
)
);

// Check total rows without distinct
global $_REQUEST;
$_REQUEST['distinct'] = 0;
$result = $this->callAPIAndDocument('report_template', 'getrows', array(
'report_id' => 'Mailing/opened',
'options' => array('metadata' => array('labels', 'title')),
), __FUNCTION__, __FILE__, $description, 'Getrows');
$this->assertEquals(14, $result['count']);

// Check total rows with distinct
$_REQUEST['distinct'] = 1;
$result = $this->callAPIAndDocument('report_template', 'getrows', array(
'report_id' => 'Mailing/opened',
'options' => array('metadata' => array('labels', 'title')),
), __FUNCTION__, __FILE__, $description, 'Getrows');
$this->assertEquals(5, $result['count']);

// Check total rows with distinct by passing NULL value to distinct parameter
$_REQUEST['distinct'] = NULL;
$result = $this->callAPIAndDocument('report_template', 'getrows', array(
'report_id' => 'Mailing/opened',
'options' => array('metadata' => array('labels', 'title')),
), __FUNCTION__, __FILE__, $description, 'Getrows');
$this->assertEquals(5, $result['count']);
}

/**
* Test api to get rows from reports.
*
Expand Down