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