Skip to content

Commit

Permalink
Deprecate computeStats off to the one place that still uses it.
Browse files Browse the repository at this point in the history
We MIGHT have reports calling it so to avoid a fatal we will not remove the function just yet
  • Loading branch information
eileenmcnaughton committed Feb 18, 2019
1 parent b64b7a9 commit 8446bae
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 56 deletions.
56 changes: 4 additions & 52 deletions CRM/Contribute/BAO/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -4400,65 +4400,17 @@ public static function updateRelatedPledge(
/**
* Compute the stats values
*
* @deprecated
*
* @param string $stat either 'mode' or 'median'
* @param string $sql
* @param string $alias of civicrm_contribution
*
* @return array|null
*/
public static function computeStats($stat, $sql, $alias = NULL) {
$mode = $median = array();
switch ($stat) {
case 'mode':
$modeDAO = CRM_Core_DAO::executeQuery($sql);
while ($modeDAO->fetch()) {
if ($modeDAO->civicrm_contribution_total_amount_count > 1) {
$mode[] = CRM_Utils_Money::format($modeDAO->amount, $modeDAO->currency);
}
else {
$mode[] = 'N/A';
}
}
return $mode;

case 'median':
$currencies = CRM_Core_OptionGroup::values('currencies_enabled');
foreach ($currencies as $currency => $val) {
$midValue = 0;
$where = "AND {$alias}.currency = '{$currency}'";
$rowCount = CRM_Core_DAO::singleValueQuery("SELECT count(*) as count {$sql} {$where}");

$even = FALSE;
$offset = 1;
$medianRow = floor($rowCount / 2);
if ($rowCount % 2 == 0 && !empty($medianRow)) {
$even = TRUE;
$offset++;
$medianRow--;
}

$medianValue = "SELECT {$alias}.total_amount as median
{$sql} {$where}
ORDER BY median LIMIT {$medianRow},{$offset}";
$medianValDAO = CRM_Core_DAO::executeQuery($medianValue);
while ($medianValDAO->fetch()) {
if ($even) {
$midValue = $midValue + $medianValDAO->median;
}
else {
$median[] = CRM_Utils_Money::format($medianValDAO->median, $currency);
}
}
if ($even) {
$midValue = $midValue / 2;
$median[] = CRM_Utils_Money::format($midValue, $currency);
}
}
return $median;

default:
return NULL;
}
CRM_Core_Error::deprecatedFunctionWarning('computeStats is now deprecated');
return [];
}

/**
Expand Down
73 changes: 69 additions & 4 deletions CRM/Report/Form/Contribute/Summary.php
Original file line number Diff line number Diff line change
Expand Up @@ -649,10 +649,8 @@ public function statistics(&$rows) {
FROM (SELECT {$this->_aliases['civicrm_contribution']}.total_amount as amount,
{$contriQuery} {$groupBy} {$orderBy}) as mode GROUP BY currency";

$mode = CRM_Contribute_BAO_Contribution::computeStats('mode', $modeSQL);

$medianSQL = "{$this->_from} {$this->_where}";
$median = CRM_Contribute_BAO_Contribution::computeStats('median', $medianSQL, $this->_aliases['civicrm_contribution']);
$mode = $this->calculateMode($modeSQL);
$median = $this->calculateMedian();

if ($softCredit) {
$softDAO = CRM_Core_DAO::executeQuery($softSQL);
Expand Down Expand Up @@ -961,4 +959,71 @@ public function alterDisplay(&$rows) {
}
}

/**
* Calculate mode.
*
* Note this is a slow query. Alternative is extended reports.
*
* @param string $sql
* @return array|null
*/
protected function calculateMode($sql) {
$mode = [];
$modeDAO = CRM_Core_DAO::executeQuery($sql);
while ($modeDAO->fetch()) {
if ($modeDAO->civicrm_contribution_total_amount_count > 1) {
$mode[] = CRM_Utils_Money::format($modeDAO->amount, $modeDAO->currency);
}
else {
$mode[] = 'N/A';
}
}
return $mode;
}

/**
* Calculate mode.
*
* Note this is a slow query. Alternative is extended reports.
*
* @return array|null
*/
protected function calculateMedian() {
$sql = "{$this->_from} {$this->_where}";
$currencies = CRM_Core_OptionGroup::values('currencies_enabled');
$median = [];
foreach ($currencies as $currency => $val) {
$midValue = 0;
$where = "AND {$this->_aliases['civicrm_contribution']}.currency = '{$currency}'";
$rowCount = CRM_Core_DAO::singleValueQuery("SELECT count(*) as count {$sql} {$where}");

$even = FALSE;
$offset = 1;
$medianRow = floor($rowCount / 2);
if ($rowCount % 2 == 0 && !empty($medianRow)) {
$even = TRUE;
$offset++;
$medianRow--;
}

$medianValue = "SELECT {$this->_aliases['civicrm_contribution']}.total_amount as median
{$sql} {$where}
ORDER BY median LIMIT {$medianRow},{$offset}";
$medianValDAO = CRM_Core_DAO::executeQuery($medianValue);
while ($medianValDAO->fetch()) {
if ($even) {
$midValue = $midValue + $medianValDAO->median;
}
else {
$median[] = CRM_Utils_Money::format($medianValDAO->median, $currency);
}
}
if ($even) {
$midValue = $midValue / 2;
$median[] = CRM_Utils_Money::format($midValue, $currency);
}
}
return $median;
}

}

0 comments on commit 8446bae

Please sign in to comment.