CRM-18439 - Report Fixes to include Full Group by clause
[civicrm-core.git] / CRM / Report / Form / Event / Summary.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
fa938177 6 | Copyright CiviCRM LLC (c) 2004-2016 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
fa938177 31 * @copyright CiviCRM LLC (c) 2004-2016
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_Report_Form_Event_Summary extends CRM_Report_Form_Event {
36
37 protected $_summary = NULL;
38
39 protected $_charts = array(
40 '' => 'Tabular',
41 'barChart' => 'Bar Chart',
42 'pieChart' => 'Pie Chart',
43 );
44
45 protected $_add2groupSupported = FALSE;
46
47 protected $_customGroupExtends = array(
21dfd5f5 48 'Event',
9d72cede 49 );
6a488035 50 public $_drilldownReport = array('event/income' => 'Link to Detail Report');
2f4c2f5d 51
74cf4551 52 /**
74cf4551
EM
53 */
54 /**
74cf4551 55 */
00be9182 56 public function __construct() {
6a488035
TO
57
58 $this->_columns = array(
9d72cede 59 'civicrm_event' => array(
6a488035 60 'dao' => 'CRM_Event_DAO_Event',
9d72cede 61 'fields' => array(
6a488035
TO
62 'id' => array(
63 'no_display' => TRUE,
64 'required' => TRUE,
65 ),
9d72cede
EM
66 'title' => array(
67 'title' => ts('Event Title'),
6a488035
TO
68 'required' => TRUE,
69 ),
9d72cede
EM
70 'event_type_id' => array(
71 'title' => ts('Event Type'),
6a488035
TO
72 'required' => TRUE,
73 ),
74 'fee_label' => array('title' => ts('Fee Label')),
9d72cede
EM
75 'event_start_date' => array(
76 'title' => ts('Event Start Date'),
6a488035
TO
77 ),
78 'event_end_date' => array('title' => ts('Event End Date')),
9d72cede
EM
79 'max_participants' => array(
80 'title' => ts('Capacity'),
6a488035
TO
81 'type' => CRM_Utils_Type::T_INT,
82 ),
83 ),
9d72cede 84 'filters' => array(
2107cde9
CW
85 'id' => array(
86 'title' => ts('Event'),
87 'operatorType' => CRM_Report_Form::OP_ENTITYREF,
88 'type' => CRM_Utils_Type::T_INT,
89 'attributes' => array('select' => array('minimumInputLength' => 0)),
6a488035
TO
90 ),
91 'event_type_id' => array(
92 'name' => 'event_type_id',
93 'title' => ts('Event Type'),
525ae77a 94 'type' => CRM_Utils_Type::T_INT,
6a488035
TO
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 );
9bf1940a 109 $this->_currencyColumn = 'civicrm_participant_fee_currency';
6a488035
TO
110 parent::__construct();
111 }
112
00be9182 113 public function preProcess() {
6a488035
TO
114 parent::preProcess();
115 }
116
00be9182 117 public function select() {
6a488035
TO
118 $select = array();
119 foreach ($this->_columns as $tableName => $table) {
120 if (array_key_exists('fields', $table)) {
121 foreach ($table['fields'] as $fieldName => $field) {
9d72cede
EM
122 if (!empty($field['required']) ||
123 !empty($this->_params['fields'][$fieldName])
124 ) {
6a488035
TO
125 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
126 }
127 }
128 }
129 }
130
d1641c51 131 $this->_selectClauses = $select;
1b06955f 132 $this->_select = 'SELECT ' . implode(', ', $select);
6a488035
TO
133 }
134
00be9182 135 public function from() {
6a488035
TO
136 $this->_from = " FROM civicrm_event {$this->_aliases['civicrm_event']} ";
137 }
138
00be9182 139 public function where() {
6a488035
TO
140 $clauses = array();
141 $this->_participantWhere = "";
142 foreach ($this->_columns as $tableName => $table) {
143 if (array_key_exists('filters', $table)) {
144 foreach ($table['filters'] as $fieldName => $field) {
145 $clause = NULL;
146 if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) {
147 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
9d72cede
EM
148 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
149 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
6a488035
TO
150
151 if ($relative || $from || $to) {
152 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
153 }
154 }
155 else {
156 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
157 if ($op) {
158 $clause = $this->whereClause($field,
159 $op,
160 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
161 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
162 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
163 );
164 }
165 }
166 if (!empty($this->_params['id_value'])) {
77f60b00
FG
167 $idValue = is_array($this->_params['id_value']) ? implode(',', $this->_params['id_value']) : $this->_params['id_value'];
168 $this->_participantWhere = " AND civicrm_participant.event_id IN ( $idValue ) ";
6a488035
TO
169 }
170
171 if (!empty($clause)) {
172 $clauses[] = $clause;
173 }
174 }
175 }
176 }
99f13d8d 177 $clauses[] = "{$this->_aliases['civicrm_event']}.is_template = 0";
1b06955f 178 $this->_where = 'WHERE ' . implode(' AND ', $clauses);
6a488035
TO
179 }
180
00be9182 181 public function groupBy() {
6a488035
TO
182 $this->assign('chartSupported', TRUE);
183 $this->_groupBy = " GROUP BY {$this->_aliases['civicrm_event']}.id";
d1641c51 184 $this->_groupBy .= CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, "{$this->_aliases['civicrm_event']}.id");
6a488035
TO
185 }
186
74cf4551 187 /**
fe482240 188 * get participants information for events.
74cf4551
EM
189 * @return array
190 */
00be9182 191 public function participantInfo() {
6a488035 192
1b06955f 193 $statusType1 = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 1');
194 $statusType2 = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 0');
6a488035
TO
195
196 $sql = "
2f4c2f5d 197 SELECT civicrm_participant.event_id AS event_id,
198 civicrm_participant.status_id AS statusId,
199 COUNT( civicrm_participant.id ) AS participant,
1b06955f 200 SUM( civicrm_participant.fee_amount ) AS amount,
201 civicrm_participant.fee_currency
6a488035
TO
202
203 FROM civicrm_participant
204
2f4c2f5d 205 WHERE civicrm_participant.is_test = 0
6a488035
TO
206 $this->_participantWhere
207
2f4c2f5d 208 GROUP BY civicrm_participant.event_id,
d1641c51 209 civicrm_participant.status_id,
210 civicrm_participant.fee_currency";
6a488035
TO
211
212 $info = CRM_Core_DAO::executeQuery($sql);
1b06955f 213 $participant_data = $participant_info = $currency = array();
6a488035
TO
214
215 while ($info->fetch()) {
216 $participant_data[$info->event_id][$info->statusId]['participant'] = $info->participant;
217 $participant_data[$info->event_id][$info->statusId]['amount'] = $info->amount;
1b06955f 218 $currency[$info->event_id] = $info->fee_currency;
6a488035
TO
219 }
220
221 $amt = $particiType1 = $particiType2 = 0;
222
223 foreach ($participant_data as $event_id => $event_data) {
224 foreach ($event_data as $status_id => $data) {
225
226 if (array_key_exists($status_id, $statusType1)) {
227 //total income of event
228 $amt = $amt + $data['amount'];
229
230 //number of Registered/Attended participants
231 $particiType1 = $particiType1 + $data['participant'];
232 }
233 elseif (array_key_exists($status_id, $statusType2)) {
234
235 //number of No-show/Cancelled/Pending participants
236 $particiType2 = $particiType2 + $data['participant'];
237 }
238 }
239
2437f726 240 $participant_info[$event_id]['totalAmount'] = $amt;
6a488035
TO
241 $participant_info[$event_id]['statusType1'] = $particiType1;
242 $participant_info[$event_id]['statusType2'] = $particiType2;
9d72cede 243 $participant_info[$event_id]['currency'] = $currency[$event_id];
6a488035
TO
244 $amt = $particiType1 = $particiType2 = 0;
245 }
246
247 return $participant_info;
248 }
249
795492f3
TO
250 /**
251 * Build header for table.
252 */
00be9182 253 public function buildColumnHeaders() {
6a488035
TO
254 $this->_columnHeaders = array();
255 foreach ($this->_columns as $tableName => $table) {
256 if (array_key_exists('fields', $table)) {
257 foreach ($table['fields'] as $fieldName => $field) {
9d72cede
EM
258 if (!empty($field['required']) ||
259 !empty($this->_params['fields'][$fieldName])
260 ) {
6a488035
TO
261
262 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
263 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field);
264 }
265 }
266 }
267 }
268
1b06955f 269 $statusType1 = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 1');
270 $statusType2 = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 0');
6a488035
TO
271
272 //make column header for participant status Registered/Attended
273 $type1_header = implode('/', $statusType1);
274
275 //make column header for participant status No-show/Cancelled/Pending
276 $type2_header = implode('/', $statusType2);
277
278 $this->_columnHeaders['statusType1'] = array(
279 'title' => $type1_header,
280 'type' => CRM_Utils_Type::T_INT,
281 );
282 $this->_columnHeaders['statusType2'] = array(
283 'title' => $type2_header,
284 'type' => CRM_Utils_Type::T_INT,
285 );
286 $this->_columnHeaders['totalAmount'] = array(
287 'title' => 'Total Income',
1b06955f 288 'type' => CRM_Utils_Type::T_STRING,
6a488035
TO
289 );
290 }
291
00be9182 292 public function postProcess() {
6a488035
TO
293
294 $this->beginPostProcess();
295
296 $this->buildColumnHeaders();
297
298 $sql = $this->buildQuery(TRUE);
299
300 $dao = CRM_Core_DAO::executeQuery($sql);
301
302 //set pager before exicution of query in function participantInfo()
303 $this->setPager();
304
305 $rows = $graphRows = array();
306 $count = 0;
307 while ($dao->fetch()) {
308 $row = array();
309 foreach ($this->_columnHeaders as $key => $value) {
9d72cede
EM
310 if (($key == 'civicrm_event_start_date') ||
311 ($key == 'civicrm_event_end_date')
312 ) {
6a488035
TO
313 //get event start date and end date in custom datetime format
314 $row[$key] = CRM_Utils_Date::customFormat($dao->$key);
315 }
316 else {
317 if (isset($dao->$key)) {
318 $row[$key] = $dao->$key;
319 }
320 }
321 }
322 $rows[] = $row;
323 }
324 if (!empty($rows)) {
325 $participant_info = $this->participantInfo();
326 foreach ($rows as $key => $value) {
327 if (array_key_exists($value['civicrm_event_id'], $participant_info)) {
328 foreach ($participant_info[$value['civicrm_event_id']] as $k => $v) {
329 $rows[$key][$k] = $v;
330 }
331 }
332 }
333 }
334 // do not call pager here
335 $this->formatDisplay($rows, FALSE);
336 unset($this->_columnHeaders['civicrm_event_id']);
337
338 $this->doTemplateAssignment($rows);
339
340 $this->endPostProcess($rows);
341 }
342
74cf4551
EM
343 /**
344 * @param $rows
345 */
00be9182 346 public function buildChart(&$rows) {
6a488035
TO
347 $this->_interval = 'events';
348 $countEvent = NULL;
a7488080 349 if (!empty($this->_params['charts'])) {
6a488035
TO
350 foreach ($rows as $key => $value) {
351 $graphRows['totalAmount'][] = $graphRows['value'][] = CRM_Utils_Array::value('totalAmount', $rows[$key]);
795492f3 352 $graphRows[$this->_interval][] = substr($rows[$key]['civicrm_event_title'], 0, 12) . "..(" .
9d72cede 353 $rows[$key]['civicrm_event_id'] . ") ";
6a488035
TO
354 }
355
356 if (CRM_Utils_Array::value('totalAmount', $rows[$key]) == 0) {
357 $countEvent = count($rows);
358 }
359
360 if ((!empty($rows)) && $countEvent != 1) {
361 $config = CRM_Core_Config::Singleton();
362 $chartInfo = array(
363 'legend' => 'Event Summary',
364 'xname' => 'Event',
365 'yname' => "Total Amount ({$config->defaultCurrency})",
366 );
367 if (!empty($graphRows)) {
368 foreach ($graphRows[$this->_interval] as $key => $val) {
369 $graph[$val] = $graphRows['value'][$key];
370 }
371 $chartInfo['values'] = $graph;
372 $chartInfo['xLabelAngle'] = 20;
373
374 // build the chart.
375 CRM_Utils_OpenFlashChart::buildChart($chartInfo, $this->_params['charts']);
376 $this->assign('chartType', $this->_params['charts']);
377 }
378 }
379 }
380 }
381
74cf4551 382 /**
ced9bfed
EM
383 * Alter display of rows.
384 *
385 * Iterate through the rows retrieved via SQL and make changes for display purposes,
386 * such as rendering contacts as links.
387 *
388 * @param array $rows
389 * Rows generated by SQL, with an array for each row.
74cf4551 390 */
00be9182 391 public function alterDisplay(&$rows) {
6a488035
TO
392
393 if (is_array($rows)) {
394 $eventType = CRM_Core_OptionGroup::values('event_type');
6a488035 395
2437f726 396 foreach ($rows as $rowNum => $row) {
9d72cede
EM
397 if (array_key_exists('totalAmount', $row) &&
398 array_key_exists('currency', $row)
399 ) {
2437f726
PJ
400 $rows[$rowNum]['totalAmount'] = CRM_Utils_Money::format($rows[$rowNum]['totalAmount'], $rows[$rowNum]['currency']);
401 }
6a488035
TO
402 if (array_key_exists('civicrm_event_title', $row)) {
403 if ($value = $row['civicrm_event_id']) {
404 //CRM_Event_PseudoConstant::event( $value, false );
405 $url = CRM_Report_Utils_Report::getNextUrl('event/income',
406 'reset=1&force=1&id_op=in&id_value=' . $value,
407 $this->_absoluteUrl, $this->_id, $this->_drilldownReport
408 );
409 $rows[$rowNum]['civicrm_event_title_link'] = $url;
1b06955f 410 $rows[$rowNum]['civicrm_event_title_hover'] = ts('View Event Income For this Event');
6a488035
TO
411 }
412 }
413
414 //handle event type
415 if (array_key_exists('civicrm_event_event_type_id', $row)) {
416 if ($value = $row['civicrm_event_event_type_id']) {
417 $rows[$rowNum]['civicrm_event_event_type_id'] = $eventType[$value];
418 }
419 }
420 }
421 }
422 }
96025800 423
6a488035 424}