Merge pull request #4140 from colemanw/CRM-15295
[civicrm-core.git] / CRM / Report / Form / Event / Summary.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.5 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27 */
28
29 /**
30 *
31 * @package CRM
32 * @copyright CiviCRM LLC (c) 2004-2014
33 * $Id$
34 *
35 */
36 class CRM_Report_Form_Event_Summary extends CRM_Report_Form_Event {
37
38 protected $_summary = NULL;
39
40 protected $_charts = array(
41 '' => 'Tabular',
42 'barChart' => 'Bar Chart',
43 'pieChart' => 'Pie Chart',
44 );
45
46 protected $_add2groupSupported = FALSE;
47
48 protected $_customGroupExtends = array(
49 'Event');
50 public $_drilldownReport = array('event/income' => 'Link to Detail Report');
51
52 /**
53 *
54 */
55 /**
56 *
57 */
58 function __construct() {
59
60 $this->_columns = array(
61 'civicrm_event' =>
62 array(
63 'dao' => 'CRM_Event_DAO_Event',
64 'fields' =>
65 array(
66 'id' => array(
67 'no_display' => TRUE,
68 'required' => TRUE,
69 ),
70 'title' => array('title' => ts('Event Title'),
71 'required' => TRUE,
72 ),
73 'event_type_id' => array('title' => ts('Event Type'),
74 'required' => TRUE,
75 ),
76 'fee_label' => array('title' => ts('Fee Label')),
77 'event_start_date' => array('title' => ts('Event Start Date'),
78 ),
79 'event_end_date' => array('title' => ts('Event End Date')),
80 'max_participants' => array('title' => ts('Capacity'),
81 'type' => CRM_Utils_Type::T_INT,
82 ),
83 ),
84 'filters' =>
85 array(
86 'id' => array(
87 'title' => ts('Event'),
88 'operatorType' => CRM_Report_Form::OP_ENTITYREF,
89 'type' => CRM_Utils_Type::T_INT,
90 'attributes' => array('select' => array('minimumInputLength' => 0)),
91 ),
92 'event_type_id' => array(
93 'name' => 'event_type_id',
94 'title' => ts('Event Type'),
95 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
96 'options' => CRM_Core_OptionGroup::values('event_type'),
97 ),
98 'event_start_date' => array(
99 'title' => 'Event Start Date',
100 'operatorType' => CRM_Report_Form::OP_DATE,
101 ),
102 'event_end_date' => array(
103 'title' => 'Event End Date',
104 'operatorType' => CRM_Report_Form::OP_DATE,
105 ),
106 ),
107 ),
108 );
109 $this->_currencyColumn = 'civicrm_participant_fee_currency';
110 parent::__construct();
111 }
112
113 function preProcess() {
114 parent::preProcess();
115 }
116
117 function select() {
118 $select = array();
119 foreach ($this->_columns as $tableName => $table) {
120 if (array_key_exists('fields', $table)) {
121 foreach ($table['fields'] as $fieldName => $field) {
122 if (!empty($field['required']) || !empty($this->_params['fields'][$fieldName])) {
123 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
124 }
125 }
126 }
127 }
128
129 $this->_select = 'SELECT ' . implode(', ', $select);
130 }
131
132 function from() {
133 $this->_from = " FROM civicrm_event {$this->_aliases['civicrm_event']} ";
134 }
135
136 function where() {
137 $clauses = array();
138 $this->_participantWhere = "";
139 foreach ($this->_columns as $tableName => $table) {
140 if (array_key_exists('filters', $table)) {
141 foreach ($table['filters'] as $fieldName => $field) {
142 $clause = NULL;
143 if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) {
144 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
145 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
146 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
147
148 if ($relative || $from || $to) {
149 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
150 }
151 }
152 else {
153 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
154 if ($op) {
155 $clause = $this->whereClause($field,
156 $op,
157 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
158 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
159 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
160 );
161 }
162 }
163 if (!empty($this->_params['id_value'])) {
164 $this->_participantWhere = " AND civicrm_participant.event_id IN ( {$this->_params['id_value']} ) ";
165 }
166
167 if (!empty($clause)) {
168 $clauses[] = $clause;
169 }
170 }
171 }
172 }
173 $clauses[] = "{$this->_aliases['civicrm_event']}.is_template = 0";
174 $this->_where = 'WHERE ' . implode(' AND ', $clauses);
175 }
176
177 function groupBy() {
178 $this->assign('chartSupported', TRUE);
179 $this->_groupBy = " GROUP BY {$this->_aliases['civicrm_event']}.id";
180 }
181
182 //get participants information for events
183 /**
184 * @return array
185 */
186 function participantInfo() {
187
188 $statusType1 = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 1');
189 $statusType2 = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 0');
190
191 $sql = "
192 SELECT civicrm_participant.event_id AS event_id,
193 civicrm_participant.status_id AS statusId,
194 COUNT( civicrm_participant.id ) AS participant,
195 SUM( civicrm_participant.fee_amount ) AS amount,
196 civicrm_participant.fee_currency
197
198 FROM civicrm_participant
199
200 WHERE civicrm_participant.is_test = 0
201 $this->_participantWhere
202
203 GROUP BY civicrm_participant.event_id,
204 civicrm_participant.status_id";
205
206 $info = CRM_Core_DAO::executeQuery($sql);
207 $participant_data = $participant_info = $currency = array();
208
209 while ($info->fetch()) {
210 $participant_data[$info->event_id][$info->statusId]['participant'] = $info->participant;
211 $participant_data[$info->event_id][$info->statusId]['amount'] = $info->amount;
212 $currency[$info->event_id] = $info->fee_currency;
213 }
214
215 $amt = $particiType1 = $particiType2 = 0;
216
217 foreach ($participant_data as $event_id => $event_data) {
218 foreach ($event_data as $status_id => $data) {
219
220 if (array_key_exists($status_id, $statusType1)) {
221 //total income of event
222 $amt = $amt + $data['amount'];
223
224 //number of Registered/Attended participants
225 $particiType1 = $particiType1 + $data['participant'];
226 }
227 elseif (array_key_exists($status_id, $statusType2)) {
228
229 //number of No-show/Cancelled/Pending participants
230 $particiType2 = $particiType2 + $data['participant'];
231 }
232 }
233
234 $participant_info[$event_id]['totalAmount'] = $amt;
235 $participant_info[$event_id]['statusType1'] = $particiType1;
236 $participant_info[$event_id]['statusType2'] = $particiType2;
237 $participant_info[$event_id]['currency'] = $currency[$event_id];
238 $amt = $particiType1 = $particiType2 = 0;
239 }
240
241 return $participant_info;
242 }
243
244 //build header for table
245 function buildColumnHeaders() {
246
247 $this->_columnHeaders = array();
248 foreach ($this->_columns as $tableName => $table) {
249 if (array_key_exists('fields', $table)) {
250 foreach ($table['fields'] as $fieldName => $field) {
251 if (!empty($field['required']) || !empty($this->_params['fields'][$fieldName])) {
252
253 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
254 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field);
255 }
256 }
257 }
258 }
259
260 $statusType1 = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 1');
261 $statusType2 = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 0');
262
263 //make column header for participant status Registered/Attended
264 $type1_header = implode('/', $statusType1);
265
266 //make column header for participant status No-show/Cancelled/Pending
267 $type2_header = implode('/', $statusType2);
268
269 $this->_columnHeaders['statusType1'] = array(
270 'title' => $type1_header,
271 'type' => CRM_Utils_Type::T_INT,
272 );
273 $this->_columnHeaders['statusType2'] = array(
274 'title' => $type2_header,
275 'type' => CRM_Utils_Type::T_INT,
276 );
277 $this->_columnHeaders['totalAmount'] = array(
278 'title' => 'Total Income',
279 'type' => CRM_Utils_Type::T_STRING,
280 );
281 }
282
283 function postProcess() {
284
285 $this->beginPostProcess();
286
287 $this->buildColumnHeaders();
288
289 $sql = $this->buildQuery(TRUE);
290
291 $dao = CRM_Core_DAO::executeQuery($sql);
292
293 //set pager before exicution of query in function participantInfo()
294 $this->setPager();
295
296 $rows = $graphRows = array();
297 $count = 0;
298 while ($dao->fetch()) {
299 $row = array();
300 foreach ($this->_columnHeaders as $key => $value) {
301 if (($key == 'civicrm_event_start_date') || ($key == 'civicrm_event_end_date')) {
302 //get event start date and end date in custom datetime format
303 $row[$key] = CRM_Utils_Date::customFormat($dao->$key);
304 }
305 else {
306 if (isset($dao->$key)) {
307 $row[$key] = $dao->$key;
308 }
309 }
310 }
311 $rows[] = $row;
312 }
313 if (!empty($rows)) {
314 $participant_info = $this->participantInfo();
315 foreach ($rows as $key => $value) {
316 if (array_key_exists($value['civicrm_event_id'], $participant_info)) {
317 foreach ($participant_info[$value['civicrm_event_id']] as $k => $v) {
318 $rows[$key][$k] = $v;
319 }
320 }
321 }
322 }
323 // do not call pager here
324 $this->formatDisplay($rows, FALSE);
325 unset($this->_columnHeaders['civicrm_event_id']);
326
327 $this->doTemplateAssignment($rows);
328
329 $this->endPostProcess($rows);
330 }
331
332 /**
333 * @param $rows
334 */
335 function buildChart(&$rows) {
336 $this->_interval = 'events';
337 $countEvent = NULL;
338 if (!empty($this->_params['charts'])) {
339 foreach ($rows as $key => $value) {
340 $graphRows['totalAmount'][] = $graphRows['value'][] = CRM_Utils_Array::value('totalAmount', $rows[$key]);
341 $graphRows[$this->_interval][] = substr($rows[$key]['civicrm_event_title'], 0, 12) . "..(" . $rows[$key]['civicrm_event_id'] . ") ";
342 }
343
344 if (CRM_Utils_Array::value('totalAmount', $rows[$key]) == 0) {
345 $countEvent = count($rows);
346 }
347
348 if ((!empty($rows)) && $countEvent != 1) {
349 $config = CRM_Core_Config::Singleton();
350 $chartInfo = array(
351 'legend' => 'Event Summary',
352 'xname' => 'Event',
353 'yname' => "Total Amount ({$config->defaultCurrency})",
354 );
355 if (!empty($graphRows)) {
356 foreach ($graphRows[$this->_interval] as $key => $val) {
357 $graph[$val] = $graphRows['value'][$key];
358 }
359 $chartInfo['values'] = $graph;
360 $chartInfo['xLabelAngle'] = 20;
361
362 // build the chart.
363 CRM_Utils_OpenFlashChart::buildChart($chartInfo, $this->_params['charts']);
364 $this->assign('chartType', $this->_params['charts']);
365 }
366 }
367 }
368 }
369
370 /**
371 * @param $rows
372 */
373 function alterDisplay(&$rows) {
374
375 if (is_array($rows)) {
376 $eventType = CRM_Core_OptionGroup::values('event_type');
377
378 foreach ($rows as $rowNum => $row) {
379 if (array_key_exists('totalAmount', $row) && array_key_exists('currency', $row)) {
380 $rows[$rowNum]['totalAmount'] = CRM_Utils_Money::format($rows[$rowNum]['totalAmount'], $rows[$rowNum]['currency']);
381 }
382 if (array_key_exists('civicrm_event_title', $row)) {
383 if ($value = $row['civicrm_event_id']) {
384 //CRM_Event_PseudoConstant::event( $value, false );
385 $url = CRM_Report_Utils_Report::getNextUrl('event/income',
386 'reset=1&force=1&id_op=in&id_value=' . $value,
387 $this->_absoluteUrl, $this->_id, $this->_drilldownReport
388 );
389 $rows[$rowNum]['civicrm_event_title_link'] = $url;
390 $rows[$rowNum]['civicrm_event_title_hover'] = ts('View Event Income For this Event');
391 }
392 }
393
394 //handle event type
395 if (array_key_exists('civicrm_event_event_type_id', $row)) {
396 if ($value = $row['civicrm_event_event_type_id']) {
397 $rows[$rowNum]['civicrm_event_event_type_id'] = $eventType[$value];
398 }
399 }
400 }
401 }
402 }
403 }
404