833cc2605301a5c138876ff7452dad0b675ec731
[com.zyxware.civiwci.git] / CRM / Wci / BAO / ProgressBar.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM Widget Creation Interface (WCI) Version 1.0 |
5 +--------------------------------------------------------------------+
6 | Copyright Zyxware Technologies (c) 2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM WCI. |
9 | |
10 | CiviCRM WCI is free software; you can copy, modify, and distribute |
11 | it under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007. |
13 | |
14 | CiviCRM WCI is distributed in the hope that it will be useful, |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License along with this program; if not, contact Zyxware |
21 | Technologies at info[AT]zyxware[DOT]com. |
22 +--------------------------------------------------------------------+
23 */
24
25 /**
26 *
27 * @package CRM
28 * @copyright CiviCRM LLC (c) 2004-2013
29 *
30 */
31 require_once 'CRM/Wci/DAO/ProgressBar.php';
32
33 class CRM_Wci_BAO_ProgressBar extends CRM_Wci_DAO_ProgressBar {
34
35 /**
36 * Returns array of progressbars
37 * Fields : id, name, starting_amount, goal_amount
38 * @return progressbar array
39 * @access public
40 */
41 public static function getProgressbarList() {
42 $query = "SELECT * FROM civicrm_wci_progress_bar";
43 $params = array();
44 $pbList = array();
45
46 $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Wci_DAO_ProgressBar');
47
48 while ($dao->fetch()) {
49 $pbList[$dao->id] = array();
50 CRM_Core_DAO::storeValues($dao, $pbList[$dao->id]);
51 }
52
53 return $pbList;
54 }
55
56 /**
57 * Returns percentage value of a progressbar
58 * @param integer progressbar id
59 * @return decimal percentage value
60 * @access public
61 */
62 public static function getPBCollectedAmount($pbId) {
63 $bp = 0;
64 $query = "SELECT * FROM civicrm_wci_progress_bar_formula WHERE progress_bar_id =" . $pbId;
65 $params = array();
66
67 $daoPbf = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Wci_DAO_ProgressBarFormula');
68 while ($daoPbf->fetch()) {
69 $for_page[$daoPbf->id] = array();
70 CRM_Core_DAO::storeValues($daoPbf, $for_page[$daoPbf->id]);
71 $px = $for_page[$daoPbf->id]['percentage'];
72
73 $query = "SELECT * FROM civicrm_contribution where receive_date >= '2014-11-15' AND contribution_page_id =" . $for_page[$daoPbf->id]['contribution_page_id'];
74 $params = array();
75
76 $daoCon = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Contribute_DAO_Contribution');
77
78 while ($daoCon->fetch()) {
79 $contributions[$daoCon->id] = array();
80 CRM_Core_DAO::storeValues($daoCon, $contributions[$daoCon->id]);
81 $bx = $contributions[$daoCon->id]['total_amount'];
82
83 $bp += $bx * $px / 100;
84 }
85 }
86 return floor($bp);
87 }
88
89 public static function getProgressbarInfo($pbId) {
90 $ga = 0;
91 $query = "SELECT * FROM civicrm_wci_progress_bar where id=" . $pbId;
92 $params = array();
93 $pbInfo = array();
94 $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Wci_DAO_ProgressBar');
95
96 while ($dao->fetch()) {
97 $con_page[$dao->id] = array();
98 CRM_Core_DAO::storeValues($dao, $con_page[$dao->id]);
99 $pbInfo['name'] = $con_page[$dao->id]['name'];
100 $pbInfo['starting_amount'] = $con_page[$dao->id]['starting_amount'];
101 $pbInfo['goal_amount'] = $con_page[$dao->id]['goal_amount'];
102 }
103
104 return $pbInfo;
105 }
106
107 public static function getProgressbarPercentage($pbId, &$pbInfo) {
108
109 $pbInfo = CRM_Wci_BAO_ProgressBar::getProgressbarInfo($pbId);
110 $ga = $pbInfo['goal_amount'];
111 $currAmnt = CRM_Wci_BAO_ProgressBar::getPBCollectedAmount($pbId)
112 + $pbInfo['starting_amount'];
113 (0 == $ga) ? $currAmt = 0: $perc = ($currAmnt / $ga ) * 100;
114 if (100 < $perc){
115 $perc = 100;
116 }
117
118 return floor($perc);
119 }
120
121 public static function getProgressbarData($pbId, &$pbData) {
122 if(0 != $pbId) {
123 $pbInfo = array();
124 $pbData["pb_percentage"] = CRM_Wci_BAO_ProgressBar::getProgressbarPercentage($pbId, $pbInfo);
125 $pbData["starting_amount"] = floor($pbInfo['starting_amount']);
126 $pbData["goal_amount"] = ceil($pbInfo['goal_amount']);
127
128 ($pbData["show_pb_perc"]) ? $pbData["pb_caption"] = $pbData["pb_percentage"]
129 : $pbData["pb_caption"] = CRM_Wci_BAO_ProgressBar::getPBCollectedAmount($pbId)
130 + $pbData["starting_amount"];
131
132 $pbData["no_pb"] = False;
133 } else {
134 $pbData["no_pb"] = True;
135 }
136
137 return $pbData;
138 }
139 }