Commit | Line | Data |
---|---|---|
1720f579 M |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.4 | | |
5 | +--------------------------------------------------------------------+ | |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
7 | +--------------------------------------------------------------------+ | |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | 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 and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
31 | * @copyright CiviCRM LLC (c) 2004-2013 | |
32 | * | |
33 | */ | |
82ba21f9 | 34 | require_once 'CRM/Wci/DAO/ProgressBar.php'; |
1720f579 | 35 | |
e364fc74 | 36 | class CRM_Wci_BAO_ProgressBar extends CRM_Wci_DAO_ProgressBar { |
1720f579 M |
37 | |
38 | /** | |
39 | * class constructor | |
40 | */ | |
41 | function __construct() { | |
42 | parent::__construct(); | |
43 | } | |
44 | ||
45 | /** | |
46 | * Function to create a ProgressBar | |
47 | * takes an associative array and creates a ProgressBar object | |
48 | * | |
49 | * This function is invoked from within the web form layer and also from the api layer | |
50 | * | |
51 | * @param array $params (reference ) an assoc array of name/value pairs | |
52 | * | |
e364fc74 | 53 | * @return object CRM_Wci_BAO_ProgressBar object |
1720f579 M |
54 | * @access public |
55 | * @static | |
56 | */ | |
57 | static function create(array $params) { | |
58 | ||
59 | // check required params | |
60 | if (!self::dataExists($params)) { | |
61 | CRM_Core_Error::fatal('Not enough data to create a progress bar.'); | |
62 | } | |
63 | ||
e364fc74 | 64 | $progress_bar = new CRM_Wci_BAO_ProgressBar(); |
1720f579 M |
65 | $progress_bar->copyValues($params); |
66 | ||
67 | $progress_bar->save(); | |
68 | ||
69 | return $progress_bar; | |
70 | } | |
71 | ||
72 | /** | |
73 | * Get a list of Widgets matching the params, where params keys are column | |
74 | * names of civicrm_wci_widget. | |
75 | * | |
76 | * @param array $params | |
e364fc74 | 77 | * @return array of CRM_Wci_BAO_ProgressBar objects |
1720f579 M |
78 | */ |
79 | static function retrieve(array $params) { | |
80 | $result = array(); | |
81 | ||
e364fc74 | 82 | $progress_bar = new CRM_Wci_BAO_ProgressBar(); |
1720f579 M |
83 | $progress_bar->copyValues($params); |
84 | $progress_bar->find(); | |
85 | ||
86 | while ($progress_bar->fetch()) { | |
87 | $result[(int) $progress_bar->id] = clone $progress_bar; | |
88 | } | |
89 | ||
90 | $progress_bar->free(); | |
91 | ||
92 | return $result; | |
93 | } | |
94 | ||
95 | /** | |
96 | * Wrapper method for retrieve | |
97 | * | |
98 | * @param mixed $id Int or int-like string representing widget ID | |
e364fc74 | 99 | * @return CRM_Wci_BAO_ProgressBar |
1720f579 M |
100 | */ |
101 | static function retrieveByID($id) { | |
102 | if (!is_int($id) && !ctype_digit($id)) { | |
103 | CRM_Core_Error::fatal(__CLASS__ . '::' . __FUNCTION__ . ' expects an integer.'); | |
104 | } | |
105 | $id = (int) $id; | |
106 | ||
107 | $progress_bars = self::retrieve(array('id' => $id)); | |
108 | ||
109 | if (!array_key_exists($id, $progress_bars)) { | |
110 | CRM_Core_Error::fatal("No Progress bar with ID $id exists."); | |
111 | } | |
112 | ||
113 | return $progress_bars[$id]; | |
114 | } | |
115 | ||
116 | /** | |
117 | * Check if there is absolute minimum of data to add the object | |
118 | * | |
119 | * @param array $params (reference ) an assoc array of name/value pairs | |
120 | * | |
121 | * @return boolean | |
122 | * @access public | |
123 | */ | |
124 | public static function dataExists($params) { | |
125 | if (CRM_Utils_Array::value('name', $params)) { | |
126 | return TRUE; | |
127 | } | |
128 | return FALSE; | |
129 | } | |
0a3819f8 | 130 | |
f00a3ea9 J |
131 | /** |
132 | * Returns array of progressbars | |
133 | * Fields : id, name, starting_amount, goal_amount | |
134 | * @return progressbar array | |
135 | * @access public | |
136 | */ | |
137 | public static function getProgressbarList() { | |
138 | $query = "SELECT * FROM civicrm_wci_progress_bar"; | |
139 | $params = array(); | |
140 | $pbList = array(); | |
141 | ||
e364fc74 | 142 | $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Wci_DAO_ProgressBar'); |
f00a3ea9 J |
143 | |
144 | while ($dao->fetch()) { | |
145 | $pbList[$dao->id] = array(); | |
146 | CRM_Core_DAO::storeValues($dao, $pbList[$dao->id]); | |
147 | } | |
148 | ||
149 | return $pbList; | |
150 | } | |
151 | ||
0a3819f8 J |
152 | /** |
153 | * Returns percentage value of a progressbar | |
154 | * | |
155 | * @param integer progressbar id | |
156 | * | |
157 | * @return decimal percentage value | |
158 | * @access public | |
159 | */ | |
47101c83 J |
160 | public static function getProgressbarPercentage($idPB) { |
161 | $bp = 0; | |
162 | $query = "SELECT * FROM civicrm_wci_progress_bar where id=" . $idPB; | |
163 | $params = array(); | |
e364fc74 | 164 | $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Wci_DAO_ProgressBar'); |
47101c83 J |
165 | |
166 | while ($dao->fetch()) { | |
167 | $con_page[$dao->id] = array(); | |
168 | CRM_Core_DAO::storeValues($dao, $con_page[$dao->id]); | |
169 | $con_page[$dao->id]['name']; | |
170 | $sa = $con_page[$dao->id]['starting_amount']; | |
47101c83 | 171 | $ga = $con_page[$dao->id]['goal_amount']; |
47101c83 | 172 | } |
7d8ac5b7 | 173 | |
47101c83 J |
174 | $query = "SELECT * FROM civicrm_wci_progress_bar_formula WHERE progress_bar_id =" . $idPB; |
175 | $params = array(); | |
176 | ||
e364fc74 | 177 | $daoPbf = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Wci_DAO_ProgressBarFormula'); |
47101c83 J |
178 | while ($daoPbf->fetch()) { |
179 | $for_page[$daoPbf->id] = array(); | |
180 | CRM_Core_DAO::storeValues($daoPbf, $for_page[$daoPbf->id]); | |
47101c83 | 181 | $px = $for_page[$daoPbf->id]['percentage']; |
47101c83 J |
182 | |
183 | $query = "SELECT * FROM civicrm_contribution where contribution_page_id =" . $for_page[$daoPbf->id]['contribution_page_id']; | |
184 | $params = array(); | |
185 | ||
47101c83 J |
186 | $daoCon = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Contribute_DAO_Contribution'); |
187 | ||
188 | while ($daoCon->fetch()) { | |
189 | $contributions[$daoCon->id] = array(); | |
190 | CRM_Core_DAO::storeValues($daoCon, $contributions[$daoCon->id]); | |
191 | $bx = $contributions[$daoCon->id]['total_amount']; | |
0a3819f8 | 192 | |
47101c83 J |
193 | $bp += $bx * $px / 100; |
194 | } | |
195 | } | |
5881d614 | 196 | (0 == $ga) ? $perc = 0: $perc = (($sa + $bp) / $ga ) * 100; |
9e991d9c J |
197 | if (100 < $perc){ |
198 | $perc = 100; | |
199 | } | |
47101c83 | 200 | return $perc; |
47101c83 | 201 | } |
143ff5a2 J |
202 | public static function getProgressbarData($pbId) { |
203 | ||
204 | $query = "SELECT * FROM civicrm_wci_progress_bar where id=".$pbId; | |
205 | $params = array(); | |
206 | ||
207 | $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Wci_DAO_Widget'); | |
208 | ||
209 | $pbData = array(); | |
210 | while ($dao->fetch()) { | |
211 | $pbData["starting_amount"] = $dao->starting_amount; | |
212 | $pbData["goal_amount"] = $dao->goal_amount; | |
213 | } | |
214 | return $pbData; | |
215 | } | |
1720f579 | 216 | } |