From 8446bae6dde68e1030f2a47974f043644e5e3ce4 Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 18 Feb 2019 13:36:39 +1300 Subject: [PATCH] Deprecate computeStats off to the one place that still uses it. We MIGHT have reports calling it so to avoid a fatal we will not remove the function just yet --- CRM/Contribute/BAO/Contribution.php | 56 ++------------------ CRM/Report/Form/Contribute/Summary.php | 73 ++++++++++++++++++++++++-- 2 files changed, 73 insertions(+), 56 deletions(-) diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index 2563c0d42b..f17d3ab991 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -4400,6 +4400,8 @@ WHERE eft.financial_trxn_id IN ({$trxnId}, {$baseTrxnId['financialTrxnId']}) /** * Compute the stats values * + * @deprecated + * * @param string $stat either 'mode' or 'median' * @param string $sql * @param string $alias of civicrm_contribution @@ -4407,58 +4409,8 @@ WHERE eft.financial_trxn_id IN ({$trxnId}, {$baseTrxnId['financialTrxnId']}) * @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 []; } /** diff --git a/CRM/Report/Form/Contribute/Summary.php b/CRM/Report/Form/Contribute/Summary.php index b5f682a987..11a7773b47 100644 --- a/CRM/Report/Form/Contribute/Summary.php +++ b/CRM/Report/Form/Contribute/Summary.php @@ -649,10 +649,8 @@ ROUND(AVG({$this->_aliases['civicrm_contribution_soft']}.amount), 2) as civicrm_ 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); @@ -961,4 +959,71 @@ ROUND(AVG({$this->_aliases['civicrm_contribution_soft']}.amount), 2) as civicrm_ } } + /** + * 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; + } + } -- 2.25.1