Merge pull request #9826 from seamuslee001/CRM-20007
[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,
d1641c51 205 civicrm_participant.status_id,
206 civicrm_participant.fee_currency";
6a488035
TO
207
208 $info = CRM_Core_DAO::executeQuery($sql);
1b06955f 209 $participant_data = $participant_info = $currency = array();
6a488035
TO
210
211 while ($info->fetch()) {
212 $participant_data[$info->event_id][$info->statusId]['participant'] = $info->participant;
213 $participant_data[$info->event_id][$info->statusId]['amount'] = $info->amount;
1b06955f 214 $currency[$info->event_id] = $info->fee_currency;
6a488035
TO
215 }
216
217 $amt = $particiType1 = $particiType2 = 0;
218
219 foreach ($participant_data as $event_id => $event_data) {
220 foreach ($event_data as $status_id => $data) {
221
222 if (array_key_exists($status_id, $statusType1)) {
223 //total income of event
224 $amt = $amt + $data['amount'];
225
226 //number of Registered/Attended participants
227 $particiType1 = $particiType1 + $data['participant'];
228 }
229 elseif (array_key_exists($status_id, $statusType2)) {
230
231 //number of No-show/Cancelled/Pending participants
232 $particiType2 = $particiType2 + $data['participant'];
233 }
234 }
235
2437f726 236 $participant_info[$event_id]['totalAmount'] = $amt;
6a488035
TO
237 $participant_info[$event_id]['statusType1'] = $particiType1;
238 $participant_info[$event_id]['statusType2'] = $particiType2;
9d72cede 239 $participant_info[$event_id]['currency'] = $currency[$event_id];
6a488035
TO
240 $amt = $particiType1 = $particiType2 = 0;
241 }
242
243 return $participant_info;
244 }
245
795492f3
TO
246 /**
247 * Build header for table.
248 */
00be9182 249 public function buildColumnHeaders() {
6a488035
TO
250 $this->_columnHeaders = array();
251 foreach ($this->_columns as $tableName => $table) {
252 if (array_key_exists('fields', $table)) {
253 foreach ($table['fields'] as $fieldName => $field) {
9d72cede
EM
254 if (!empty($field['required']) ||
255 !empty($this->_params['fields'][$fieldName])
256 ) {
6a488035
TO
257
258 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
259 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field);
260 }
261 }
262 }
263 }
264
1b06955f 265 $statusType1 = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 1');
266 $statusType2 = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 0');
6a488035
TO
267
268 //make column header for participant status Registered/Attended
269 $type1_header = implode('/', $statusType1);
270
271 //make column header for participant status No-show/Cancelled/Pending
272 $type2_header = implode('/', $statusType2);
273
274 $this->_columnHeaders['statusType1'] = array(
275 'title' => $type1_header,
276 'type' => CRM_Utils_Type::T_INT,
277 );
278 $this->_columnHeaders['statusType2'] = array(
279 'title' => $type2_header,
280 'type' => CRM_Utils_Type::T_INT,
281 );
282 $this->_columnHeaders['totalAmount'] = array(
ccc29f8e 283 'title' => ts('Total Income'),
1b06955f 284 'type' => CRM_Utils_Type::T_STRING,
6a488035
TO
285 );
286 }
287
00be9182 288 public function postProcess() {
6a488035
TO
289
290 $this->beginPostProcess();
291
292 $this->buildColumnHeaders();
293
294 $sql = $this->buildQuery(TRUE);
295
296 $dao = CRM_Core_DAO::executeQuery($sql);
297
298 //set pager before exicution of query in function participantInfo()
299 $this->setPager();
300
301 $rows = $graphRows = array();
302 $count = 0;
303 while ($dao->fetch()) {
304 $row = array();
305 foreach ($this->_columnHeaders as $key => $value) {
9d72cede
EM
306 if (($key == 'civicrm_event_start_date') ||
307 ($key == 'civicrm_event_end_date')
308 ) {
6a488035
TO
309 //get event start date and end date in custom datetime format
310 $row[$key] = CRM_Utils_Date::customFormat($dao->$key);
311 }
312 else {
313 if (isset($dao->$key)) {
314 $row[$key] = $dao->$key;
315 }
316 }
317 }
318 $rows[] = $row;
319 }
320 if (!empty($rows)) {
321 $participant_info = $this->participantInfo();
322 foreach ($rows as $key => $value) {
323 if (array_key_exists($value['civicrm_event_id'], $participant_info)) {
324 foreach ($participant_info[$value['civicrm_event_id']] as $k => $v) {
325 $rows[$key][$k] = $v;
326 }
327 }
328 }
329 }
330 // do not call pager here
331 $this->formatDisplay($rows, FALSE);
332 unset($this->_columnHeaders['civicrm_event_id']);
333
334 $this->doTemplateAssignment($rows);
335
336 $this->endPostProcess($rows);
337 }
338
74cf4551
EM
339 /**
340 * @param $rows
341 */
00be9182 342 public function buildChart(&$rows) {
6a488035
TO
343 $this->_interval = 'events';
344 $countEvent = NULL;
a7488080 345 if (!empty($this->_params['charts'])) {
6a488035
TO
346 foreach ($rows as $key => $value) {
347 $graphRows['totalAmount'][] = $graphRows['value'][] = CRM_Utils_Array::value('totalAmount', $rows[$key]);
795492f3 348 $graphRows[$this->_interval][] = substr($rows[$key]['civicrm_event_title'], 0, 12) . "..(" .
9d72cede 349 $rows[$key]['civicrm_event_id'] . ") ";
6a488035
TO
350 }
351
352 if (CRM_Utils_Array::value('totalAmount', $rows[$key]) == 0) {
353 $countEvent = count($rows);
354 }
355
356 if ((!empty($rows)) && $countEvent != 1) {
357 $config = CRM_Core_Config::Singleton();
358 $chartInfo = array(
359 'legend' => 'Event Summary',
360 'xname' => 'Event',
361 'yname' => "Total Amount ({$config->defaultCurrency})",
362 );
363 if (!empty($graphRows)) {
364 foreach ($graphRows[$this->_interval] as $key => $val) {
365 $graph[$val] = $graphRows['value'][$key];
366 }
367 $chartInfo['values'] = $graph;
368 $chartInfo['xLabelAngle'] = 20;
369
370 // build the chart.
371 CRM_Utils_OpenFlashChart::buildChart($chartInfo, $this->_params['charts']);
372 $this->assign('chartType', $this->_params['charts']);
373 }
374 }
375 }
376 }
377
74cf4551 378 /**
ced9bfed
EM
379 * Alter display of rows.
380 *
381 * Iterate through the rows retrieved via SQL and make changes for display purposes,
382 * such as rendering contacts as links.
383 *
384 * @param array $rows
385 * Rows generated by SQL, with an array for each row.
74cf4551 386 */
00be9182 387 public function alterDisplay(&$rows) {
6a488035
TO
388
389 if (is_array($rows)) {
390 $eventType = CRM_Core_OptionGroup::values('event_type');
6a488035 391
2437f726 392 foreach ($rows as $rowNum => $row) {
9d72cede
EM
393 if (array_key_exists('totalAmount', $row) &&
394 array_key_exists('currency', $row)
395 ) {
2437f726
PJ
396 $rows[$rowNum]['totalAmount'] = CRM_Utils_Money::format($rows[$rowNum]['totalAmount'], $rows[$rowNum]['currency']);
397 }
6a488035
TO
398 if (array_key_exists('civicrm_event_title', $row)) {
399 if ($value = $row['civicrm_event_id']) {
400 //CRM_Event_PseudoConstant::event( $value, false );
401 $url = CRM_Report_Utils_Report::getNextUrl('event/income',
402 'reset=1&force=1&id_op=in&id_value=' . $value,
403 $this->_absoluteUrl, $this->_id, $this->_drilldownReport
404 );
405 $rows[$rowNum]['civicrm_event_title_link'] = $url;
1b06955f 406 $rows[$rowNum]['civicrm_event_title_hover'] = ts('View Event Income For this Event');
6a488035
TO
407 }
408 }
409
410 //handle event type
411 if (array_key_exists('civicrm_event_event_type_id', $row)) {
412 if ($value = $row['civicrm_event_event_type_id']) {
413 $rows[$rowNum]['civicrm_event_event_type_id'] = $eventType[$value];
414 }
415 }
416 }
417 }
418 }
96025800 419
6a488035 420}