From: jitendrapurohit Date: Mon, 22 Jun 2015 13:20:54 +0000 (+0530) Subject: migrate mode and median in separate function X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=ae6884ce570e35ad15b9a22614f095ac5efe7da2;p=civicrm-core.git migrate mode and median in separate function --- diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index 0d2b995222..392186e176 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -3802,4 +3802,65 @@ WHERE con.id = {$contributionId} } } + /** + * Compute the stats values + * + * @param stat either 'mode' or 'median' + * @param sql + * @param alias of civicrm_contribution + */ + public static function computeStats($stat, $sql, $alias = NULL) { + 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': + $midValue = 0; + $currencies = CRM_Core_OptionGroup::values('currencies_enabled'); + foreach ($currencies as $currency => $val) { + $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; + } + } + } diff --git a/CRM/Report/Form/Contribute/Summary.php b/CRM/Report/Form/Contribute/Summary.php index d42958f8dd..93d43faad8 100644 --- a/CRM/Report/Form/Contribute/Summary.php +++ b/CRM/Report/Form/Contribute/Summary.php @@ -577,7 +577,7 @@ ROUND(AVG({$this->_aliases['civicrm_contribution_soft']}.amount), 2) as civicrm_ $contriDAO = CRM_Core_DAO::executeQuery($contriSQL); $totalAmount = $average = $mode = $median = $softTotalAmount = $softAverage = array(); - $count = $midValue = $softCount = 0; + $count = $softCount = 0; while ($contriDAO->fetch()) { $totalAmount[] = CRM_Utils_Money::format($contriDAO->civicrm_contribution_total_amount_sum, $contriDAO->currency) . @@ -588,51 +588,14 @@ ROUND(AVG({$this->_aliases['civicrm_contribution_soft']}.amount), 2) as civicrm_ $groupBy = "\n{$group}, {$this->_aliases['civicrm_contribution']}.total_amount"; $orderBy = "\nORDER BY civicrm_contribution_total_amount_count DESC"; - $modeSQL = "SELECT civicrm_contribution_total_amount_count, civicrm_contribution_total_amount, currency - FROM (SELECT {$this->_aliases['civicrm_contribution']}.total_amount as civicrm_contribution_total_amount, + $modeSQL = "SELECT civicrm_contribution_total_amount_count, amount, currency + FROM (SELECT {$this->_aliases['civicrm_contribution']}.total_amount as amount, {$contriQuery} {$groupBy} {$orderBy}) as mode GROUP BY currency"; - $modeDAO = CRM_Core_DAO::executeQuery($modeSQL); - while ($modeDAO->fetch()) { - if ($modeDAO->civicrm_contribution_total_amount_count > 1) { - $mode[] = CRM_Utils_Money::format($modeDAO->civicrm_contribution_total_amount, $modeDAO->currency); - } - else { - $mode[] = 'N/A'; - } - } - - $currencies = CRM_Core_OptionGroup::values('currencies_enabled'); - foreach ($currencies as $currency => $val) { - $where = "AND {$this->_aliases['civicrm_contribution']}.currency = '{$currency}'"; - $rowCount = CRM_Core_DAO::singleValueQuery("SELECT count(*) as count {$this->_from} {$this->_where} {$where}"); - - $even = FALSE; - $offset = 1; - $medianRow = floor($rowCount / 2); - if ($rowCount % 2 == 0 && !empty($medianRow)) { - $even = TRUE; - $offset++; - $medianRow--; - } + $mode = CRM_Contribute_BAO_Contribution::computeStats('mode', $modeSQL); - $medianValue = "SELECT {$this->_aliases['civicrm_contribution']}.total_amount as median - {$this->_from} {$this->_where} {$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); - } - } + $medianSQL = "{$this->_from} {$this->_where}"; + $median = CRM_Contribute_BAO_Contribution::computeStats('median', $medianSQL, $this->_aliases['civicrm_contribution']); if ($softCredit) { $softDAO = CRM_Core_DAO::executeQuery($softSQL);