Merge pull request #11660 from JMAConsulting/CRM-21754
[civicrm-core.git] / CRM / Report / Form / Event / Summary.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2018
32 */
33 class 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(
46 'Event',
47 );
48 public $_drilldownReport = array('event/income' => 'Link to Detail Report');
49
50 /**
51 * Class constructor.
52 */
53 public function __construct() {
54
55 $this->_columns = array(
56 'civicrm_event' => array(
57 'dao' => 'CRM_Event_DAO_Event',
58 'fields' => array(
59 'id' => array(
60 'no_display' => TRUE,
61 'required' => TRUE,
62 ),
63 'title' => array(
64 'title' => ts('Event Title'),
65 'required' => TRUE,
66 ),
67 'event_type_id' => array(
68 'title' => ts('Event Type'),
69 'required' => TRUE,
70 ),
71 'fee_label' => array('title' => ts('Fee Label')),
72 'event_start_date' => array(
73 'title' => ts('Event Start Date'),
74 ),
75 'event_end_date' => array('title' => ts('Event End Date')),
76 'max_participants' => array(
77 'title' => ts('Capacity'),
78 'type' => CRM_Utils_Type::T_INT,
79 ),
80 ),
81 'filters' => array(
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)),
87 ),
88 'event_type_id' => array(
89 'name' => 'event_type_id',
90 'title' => ts('Event Type'),
91 'type' => CRM_Utils_Type::T_INT,
92 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
93 'options' => CRM_Core_OptionGroup::values('event_type'),
94 ),
95 'event_start_date' => array(
96 'title' => ts('Event Start Date'),
97 'operatorType' => CRM_Report_Form::OP_DATE,
98 ),
99 'event_end_date' => array(
100 'title' => ts('Event End Date'),
101 'operatorType' => CRM_Report_Form::OP_DATE,
102 ),
103 ),
104 ),
105 );
106 $this->_currencyColumn = 'civicrm_participant_fee_currency';
107 parent::__construct();
108 }
109
110 public function preProcess() {
111 parent::preProcess();
112 }
113
114 public function select() {
115 $select = array();
116 foreach ($this->_columns as $tableName => $table) {
117 if (array_key_exists('fields', $table)) {
118 foreach ($table['fields'] as $fieldName => $field) {
119 if (!empty($field['required']) ||
120 !empty($this->_params['fields'][$fieldName])
121 ) {
122 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
123 }
124 }
125 }
126 }
127
128 $this->_selectClauses = $select;
129 $this->_select = 'SELECT ' . implode(', ', $select);
130 }
131
132 public function from() {
133 $this->_from = " FROM civicrm_event {$this->_aliases['civicrm_event']} ";
134 }
135
136 public 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 $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 ) ";
166 }
167
168 if (!empty($clause)) {
169 $clauses[] = $clause;
170 }
171 }
172 }
173 }
174 $clauses[] = "{$this->_aliases['civicrm_event']}.is_template = 0";
175 $this->_where = 'WHERE ' . implode(' AND ', $clauses);
176 }
177
178 public function groupBy() {
179 $this->assign('chartSupported', TRUE);
180 $this->_groupBy = CRM_Contact_BAO_Query::getGroupByFromSelectColumns($this->_selectClauses, "{$this->_aliases['civicrm_event']}.id");
181 }
182
183 /**
184 * get participants information for events.
185 * @return array
186 */
187 public function participantInfo() {
188
189 $statusType1 = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 1');
190 $statusType2 = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 0');
191
192 $sql = "
193 SELECT civicrm_participant.event_id AS event_id,
194 civicrm_participant.status_id AS statusId,
195 COUNT( civicrm_participant.id ) AS participant,
196 SUM( civicrm_participant.fee_amount ) AS amount,
197 civicrm_participant.fee_currency
198
199 FROM civicrm_participant
200
201 WHERE civicrm_participant.is_test = 0
202 $this->_participantWhere
203
204 GROUP BY civicrm_participant.event_id,
205 civicrm_participant.status_id,
206 civicrm_participant.fee_currency";
207
208 $info = CRM_Core_DAO::executeQuery($sql);
209 $participant_data = $participant_info = $currency = array();
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;
214 $currency[$info->event_id] = $info->fee_currency;
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
236 $participant_info[$event_id]['totalAmount'] = $amt;
237 $participant_info[$event_id]['statusType1'] = $particiType1;
238 $participant_info[$event_id]['statusType2'] = $particiType2;
239 $participant_info[$event_id]['currency'] = $currency[$event_id];
240 $amt = $particiType1 = $particiType2 = 0;
241 }
242
243 return $participant_info;
244 }
245
246 /**
247 * Build header for table.
248 */
249 public function buildColumnHeaders() {
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) {
254 if (!empty($field['required']) ||
255 !empty($this->_params['fields'][$fieldName])
256 ) {
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
265 $statusType1 = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 1', 'label');
266 $statusType2 = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 0', 'label');
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(
283 'title' => ts('Total Income'),
284 'type' => CRM_Utils_Type::T_STRING,
285 );
286 }
287
288 public function postProcess() {
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) {
306 if (($key == 'civicrm_event_start_date') ||
307 ($key == 'civicrm_event_end_date')
308 ) {
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
339 /**
340 * @param $rows
341 */
342 public function buildChart(&$rows) {
343 $this->_interval = 'events';
344 $countEvent = NULL;
345 if (!empty($this->_params['charts'])) {
346 foreach ($rows as $key => $value) {
347 $graphRows['totalAmount'][] = $graphRows['value'][] = CRM_Utils_Array::value('totalAmount', $rows[$key]);
348 $graphRows[$this->_interval][] = substr($rows[$key]['civicrm_event_title'], 0, 12) . "..(" .
349 $rows[$key]['civicrm_event_id'] . ") ";
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' => ts('Event Summary'),
360 'xname' => ts('Event'),
361 'yname' => ts('Total Amount (%1)', array(1 => $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
378 /**
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.
386 */
387 public function alterDisplay(&$rows) {
388
389 if (is_array($rows)) {
390 $eventType = CRM_Core_OptionGroup::values('event_type');
391
392 foreach ($rows as $rowNum => $row) {
393 if (array_key_exists('totalAmount', $row) &&
394 array_key_exists('currency', $row)
395 ) {
396 $rows[$rowNum]['totalAmount'] = CRM_Utils_Money::format($rows[$rowNum]['totalAmount'], $rows[$rowNum]['currency']);
397 }
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;
406 $rows[$rowNum]['civicrm_event_title_hover'] = ts('View Event Income For this Event');
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 }
419
420 }