Merge pull request #3989 from colemanw/CRM-15198
[civicrm-core.git] / CRM / Report / Form / Event / Summary.php
CommitLineData
6a488035 1<?php
6a488035
TO
2
3/*
4 +--------------------------------------------------------------------+
06b69b18 5 | CiviCRM version 4.5 |
6a488035 6 +--------------------------------------------------------------------+
06b69b18 7 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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
06b69b18 32 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
33 * $Id$
34 *
35 */
36class 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(
2f4c2f5d 49 'Event');
6a488035 50 public $_drilldownReport = array('event/income' => 'Link to Detail Report');
2f4c2f5d 51
74cf4551
EM
52 /**
53 *
54 */
55 /**
56 *
57 */
6a488035
TO
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('title' => ts('Event Title'),
87 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
88 'options' => $this->getEventFilterOptions(),
89 ),
90 'event_type_id' => array(
91 'name' => 'event_type_id',
92 'title' => ts('Event Type'),
93 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
94 'options' => CRM_Core_OptionGroup::values('event_type'),
95 ),
96 'event_start_date' => array(
97 'title' => 'Event Start Date',
98 'operatorType' => CRM_Report_Form::OP_DATE,
99 ),
100 'event_end_date' => array(
101 'title' => 'Event End Date',
102 'operatorType' => CRM_Report_Form::OP_DATE,
103 ),
104 ),
105 ),
106 );
9bf1940a 107 $this->_currencyColumn = 'civicrm_participant_fee_currency';
6a488035
TO
108 parent::__construct();
109 }
110
111 function preProcess() {
112 parent::preProcess();
113 }
114
115 function select() {
116 $select = array();
117 foreach ($this->_columns as $tableName => $table) {
118 if (array_key_exists('fields', $table)) {
119 foreach ($table['fields'] as $fieldName => $field) {
8cc574cf 120 if (!empty($field['required']) || !empty($this->_params['fields'][$fieldName])) {
6a488035
TO
121 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
122 }
123 }
124 }
125 }
126
1b06955f 127 $this->_select = 'SELECT ' . implode(', ', $select);
6a488035
TO
128 }
129
130 function from() {
131 $this->_from = " FROM civicrm_event {$this->_aliases['civicrm_event']} ";
132 }
133
134 function where() {
135 $clauses = array();
136 $this->_participantWhere = "";
137 foreach ($this->_columns as $tableName => $table) {
138 if (array_key_exists('filters', $table)) {
139 foreach ($table['filters'] as $fieldName => $field) {
140 $clause = NULL;
141 if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) {
142 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
143 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
144 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
145
146 if ($relative || $from || $to) {
147 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
148 }
149 }
150 else {
151 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
152 if ($op) {
153 $clause = $this->whereClause($field,
154 $op,
155 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
156 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
157 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
158 );
159 }
160 }
161 if (!empty($this->_params['id_value'])) {
162 $participant = implode(', ', $this->_params['id_value']);
163 $this->_participantWhere = " AND civicrm_participant.event_id IN ( {$participant} ) ";
164 }
165
166 if (!empty($clause)) {
167 $clauses[] = $clause;
168 }
169 }
170 }
171 }
99f13d8d 172 $clauses[] = "{$this->_aliases['civicrm_event']}.is_template = 0";
1b06955f 173 $this->_where = 'WHERE ' . implode(' AND ', $clauses);
6a488035
TO
174 }
175
176 function groupBy() {
177 $this->assign('chartSupported', TRUE);
178 $this->_groupBy = " GROUP BY {$this->_aliases['civicrm_event']}.id";
179 }
180
181 //get participants information for events
74cf4551
EM
182 /**
183 * @return array
184 */
6a488035
TO
185 function participantInfo() {
186
1b06955f 187 $statusType1 = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 1');
188 $statusType2 = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 0');
6a488035
TO
189
190 $sql = "
2f4c2f5d 191 SELECT civicrm_participant.event_id AS event_id,
192 civicrm_participant.status_id AS statusId,
193 COUNT( civicrm_participant.id ) AS participant,
1b06955f 194 SUM( civicrm_participant.fee_amount ) AS amount,
195 civicrm_participant.fee_currency
6a488035
TO
196
197 FROM civicrm_participant
198
2f4c2f5d 199 WHERE civicrm_participant.is_test = 0
6a488035
TO
200 $this->_participantWhere
201
2f4c2f5d 202 GROUP BY civicrm_participant.event_id,
6a488035
TO
203 civicrm_participant.status_id";
204
205 $info = CRM_Core_DAO::executeQuery($sql);
1b06955f 206 $participant_data = $participant_info = $currency = array();
6a488035
TO
207
208 while ($info->fetch()) {
209 $participant_data[$info->event_id][$info->statusId]['participant'] = $info->participant;
210 $participant_data[$info->event_id][$info->statusId]['amount'] = $info->amount;
1b06955f 211 $currency[$info->event_id] = $info->fee_currency;
6a488035
TO
212 }
213
214 $amt = $particiType1 = $particiType2 = 0;
215
216 foreach ($participant_data as $event_id => $event_data) {
217 foreach ($event_data as $status_id => $data) {
218
219 if (array_key_exists($status_id, $statusType1)) {
220 //total income of event
221 $amt = $amt + $data['amount'];
222
223 //number of Registered/Attended participants
224 $particiType1 = $particiType1 + $data['participant'];
225 }
226 elseif (array_key_exists($status_id, $statusType2)) {
227
228 //number of No-show/Cancelled/Pending participants
229 $particiType2 = $particiType2 + $data['participant'];
230 }
231 }
232
2437f726 233 $participant_info[$event_id]['totalAmount'] = $amt;
6a488035
TO
234 $participant_info[$event_id]['statusType1'] = $particiType1;
235 $participant_info[$event_id]['statusType2'] = $particiType2;
2437f726 236 $participant_info[$event_id]['currency'] = $currency[$event_id];
6a488035
TO
237 $amt = $particiType1 = $particiType2 = 0;
238 }
239
240 return $participant_info;
241 }
242
243 //build header for table
244 function buildColumnHeaders() {
245
246 $this->_columnHeaders = array();
247 foreach ($this->_columns as $tableName => $table) {
248 if (array_key_exists('fields', $table)) {
249 foreach ($table['fields'] as $fieldName => $field) {
8cc574cf 250 if (!empty($field['required']) || !empty($this->_params['fields'][$fieldName])) {
6a488035
TO
251
252 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
253 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field);
254 }
255 }
256 }
257 }
258
1b06955f 259 $statusType1 = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 1');
260 $statusType2 = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 0');
6a488035
TO
261
262 //make column header for participant status Registered/Attended
263 $type1_header = implode('/', $statusType1);
264
265 //make column header for participant status No-show/Cancelled/Pending
266 $type2_header = implode('/', $statusType2);
267
268 $this->_columnHeaders['statusType1'] = array(
269 'title' => $type1_header,
270 'type' => CRM_Utils_Type::T_INT,
271 );
272 $this->_columnHeaders['statusType2'] = array(
273 'title' => $type2_header,
274 'type' => CRM_Utils_Type::T_INT,
275 );
276 $this->_columnHeaders['totalAmount'] = array(
277 'title' => 'Total Income',
1b06955f 278 'type' => CRM_Utils_Type::T_STRING,
6a488035
TO
279 );
280 }
281
282 function postProcess() {
283
284 $this->beginPostProcess();
285
286 $this->buildColumnHeaders();
287
288 $sql = $this->buildQuery(TRUE);
289
290 $dao = CRM_Core_DAO::executeQuery($sql);
291
292 //set pager before exicution of query in function participantInfo()
293 $this->setPager();
294
295 $rows = $graphRows = array();
296 $count = 0;
297 while ($dao->fetch()) {
298 $row = array();
299 foreach ($this->_columnHeaders as $key => $value) {
300 if (($key == 'civicrm_event_start_date') || ($key == 'civicrm_event_end_date')) {
301 //get event start date and end date in custom datetime format
302 $row[$key] = CRM_Utils_Date::customFormat($dao->$key);
303 }
304 else {
305 if (isset($dao->$key)) {
306 $row[$key] = $dao->$key;
307 }
308 }
309 }
310 $rows[] = $row;
311 }
312 if (!empty($rows)) {
313 $participant_info = $this->participantInfo();
314 foreach ($rows as $key => $value) {
315 if (array_key_exists($value['civicrm_event_id'], $participant_info)) {
316 foreach ($participant_info[$value['civicrm_event_id']] as $k => $v) {
317 $rows[$key][$k] = $v;
318 }
319 }
320 }
321 }
322 // do not call pager here
323 $this->formatDisplay($rows, FALSE);
324 unset($this->_columnHeaders['civicrm_event_id']);
325
326 $this->doTemplateAssignment($rows);
327
328 $this->endPostProcess($rows);
329 }
330
74cf4551
EM
331 /**
332 * @param $rows
333 */
6a488035
TO
334 function buildChart(&$rows) {
335 $this->_interval = 'events';
336 $countEvent = NULL;
a7488080 337 if (!empty($this->_params['charts'])) {
6a488035
TO
338 foreach ($rows as $key => $value) {
339 $graphRows['totalAmount'][] = $graphRows['value'][] = CRM_Utils_Array::value('totalAmount', $rows[$key]);
340 $graphRows[$this->_interval][] = substr($rows[$key]['civicrm_event_title'], 0, 12) . "..(" . $rows[$key]['civicrm_event_id'] . ") ";
341 }
342
343 if (CRM_Utils_Array::value('totalAmount', $rows[$key]) == 0) {
344 $countEvent = count($rows);
345 }
346
347 if ((!empty($rows)) && $countEvent != 1) {
348 $config = CRM_Core_Config::Singleton();
349 $chartInfo = array(
350 'legend' => 'Event Summary',
351 'xname' => 'Event',
352 'yname' => "Total Amount ({$config->defaultCurrency})",
353 );
354 if (!empty($graphRows)) {
355 foreach ($graphRows[$this->_interval] as $key => $val) {
356 $graph[$val] = $graphRows['value'][$key];
357 }
358 $chartInfo['values'] = $graph;
359 $chartInfo['xLabelAngle'] = 20;
360
361 // build the chart.
362 CRM_Utils_OpenFlashChart::buildChart($chartInfo, $this->_params['charts']);
363 $this->assign('chartType', $this->_params['charts']);
364 }
365 }
366 }
367 }
368
74cf4551
EM
369 /**
370 * @param $rows
371 */
6a488035
TO
372 function alterDisplay(&$rows) {
373
374 if (is_array($rows)) {
375 $eventType = CRM_Core_OptionGroup::values('event_type');
6a488035 376
2437f726
PJ
377 foreach ($rows as $rowNum => $row) {
378 if (array_key_exists('totalAmount', $row) && array_key_exists('currency', $row)) {
379 $rows[$rowNum]['totalAmount'] = CRM_Utils_Money::format($rows[$rowNum]['totalAmount'], $rows[$rowNum]['currency']);
380 }
6a488035
TO
381 if (array_key_exists('civicrm_event_title', $row)) {
382 if ($value = $row['civicrm_event_id']) {
383 //CRM_Event_PseudoConstant::event( $value, false );
384 $url = CRM_Report_Utils_Report::getNextUrl('event/income',
385 'reset=1&force=1&id_op=in&id_value=' . $value,
386 $this->_absoluteUrl, $this->_id, $this->_drilldownReport
387 );
388 $rows[$rowNum]['civicrm_event_title_link'] = $url;
1b06955f 389 $rows[$rowNum]['civicrm_event_title_hover'] = ts('View Event Income For this Event');
6a488035
TO
390 }
391 }
392
393 //handle event type
394 if (array_key_exists('civicrm_event_event_type_id', $row)) {
395 if ($value = $row['civicrm_event_event_type_id']) {
396 $rows[$rowNum]['civicrm_event_event_type_id'] = $eventType[$value];
397 }
398 }
399 }
400 }
401 }
402}
403