CRM-13966 - Improve event selection in reports
[civicrm-core.git] / CRM / Report / Form / Event / IncomeCountSummary.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. |
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 along 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
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_Report_Form_Event_IncomeCountSummary 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(
2f4c2f5d 48 'Event');
6a488035
TO
49
50 public $_drilldownReport = array('event/participantlist' => 'Link to Detail Report');
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 'title' => array('title' => ts('Event'),
67 'required' => TRUE,
68 ),
69 'id' => array(
70 'no_display' => TRUE,
71 'required' => TRUE,
72 ),
73 'event_type_id' => array('title' => ts('Event Type'),
74 ),
75 'fee_label' => array('title' => ts('Fee Label')),
76 'event_start_date' => array('title' => ts('Event Start Date'),
77 ),
78 'event_end_date' => array('title' => ts('Event End Date'),
79 ),
80 'max_participants' => array('title' => ts('Capacity'),
81 'type' => CRM_Utils_Type::T_INT,
82 ),
83 ),
84 'filters' =>
85 array(
2107cde9
CW
86 'id' => array(
87 'title' => ts('Event'),
88 'operatorType' => CRM_Report_Form::OP_ENTITYREF,
89 'type' => CRM_Utils_Type::T_INT,
90 'attributes' => array('select' => array('minimumInputLength' => 0)),
6a488035
TO
91 ),
92 'event_type_id' => array(
93 'name' => 'event_type_id',
94 'title' => ts('Event Type'),
95 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
96 'options' => CRM_Core_OptionGroup::values('event_type'),
97 ),
98 'event_start_date' => array('title' => ts('Event Start Date'),
99 'operatorType' => CRM_Report_Form::OP_DATE,
100 ),
101 'event_end_date' => array('title' => ts('Event End Date'),
102 'operatorType' => CRM_Report_Form::OP_DATE,
103 ),
104 ),
105 ),
106 'civicrm_line_item' =>
107 array(
108 'dao' => 'CRM_Price_DAO_LineItem',
109 'fields' =>
110 array(
111 'participant_count' => array(
112 'title' => ts('Participants'),
113 'default' => TRUE,
114 'statistics' =>
115 array('count' => ts('Participants'),
116 ),
117 ),
118 'line_total' => array(
119 'title' => ts('Income Statistics'),
120 'type' => CRM_Utils_Type::T_MONEY,
121 'default' => TRUE,
122 'statistics' =>
123 array('sum' => ts('Income'),
124 'avg' => ts('Average'),
125 ),
126 ),
127 ),
128 ),
129 'civicrm_participant' =>
130 array(
131 'dao' => 'CRM_Event_DAO_Participant',
132 'filters' =>
133 array(
134 'sid' => array('name' => 'status_id',
135 'title' => ts('Participant Status'),
136 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
137 'options' => CRM_Event_PseudoConstant::participantStatus(),
138 ),
139 'rid' => array(
140 'name' => 'role_id',
141 'title' => ts('Participant Role'),
142 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
143 'options' => CRM_Event_PseudoConstant::participantRole(),
144 ),
145 'participant_register_date' => array('title' => ts('Registration Date'),
146 'operatorType' => CRM_Report_Form::OP_DATE,
147 ),
148 ),
149 ),
150 );
151 parent::__construct();
152 }
153
154 function preProcess() {
155 parent::preProcess();
156 }
157
158 function select() {
159 $select = array();
160 foreach ($this->_columns as $tableName => $table) {
161 if (array_key_exists('fields', $table)) {
162 foreach ($table['fields'] as $fieldName => $field) {
8cc574cf 163 if (!empty($field['required']) || !empty($this->_params['fields'][$fieldName])) {
a7488080 164 if (!empty($field['statistics'])) {
6a488035
TO
165 foreach ($field['statistics'] as $stat => $label) {
166 switch (strtolower($stat)) {
167 case 'count':
168 $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}";
169 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type::T_INT;
170 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
171 $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}";
172 break;
173
174 case 'sum':
175 $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}";
176 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type::T_MONEY;
177
178 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
179 $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}";
180 break;
181
182 case 'avg':
183 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
184
185 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type::T_MONEY;
186 $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}";
187 }
188 }
189 }
190 else {
191 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
192 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
193 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title'];
194 }
195 }
196 }
197 }
198 }
199
200 $this->_select = "SELECT " . implode(', ', $select);
201 }
202
203 function from() {
2f4c2f5d 204 $this->_from = "
6a488035 205 FROM civicrm_event {$this->_aliases['civicrm_event']}
2f4c2f5d 206 LEFT JOIN civicrm_participant {$this->_aliases['civicrm_participant']}
207 ON {$this->_aliases['civicrm_event']}.id = {$this->_aliases['civicrm_participant']}.event_id AND
208 {$this->_aliases['civicrm_participant']}.is_test = 0
6a488035 209 LEFT JOIN civicrm_line_item {$this->_aliases['civicrm_line_item']}
2f4c2f5d 210 ON {$this->_aliases['civicrm_participant']}.id ={$this->_aliases['civicrm_line_item']}.entity_id AND
6a488035
TO
211 {$this->_aliases['civicrm_line_item']}.entity_table = 'civicrm_participant' ";
212 }
213
214 function where() {
215 $clauses = array();
216 $this->_participantWhere = "";
217 foreach ($this->_columns as $tableName => $table) {
218 if (array_key_exists('filters', $table)) {
219 foreach ($table['filters'] as $fieldName => $field) {
220 $clause = NULL;
221 if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) {
222 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
223 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
224 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
225
226 if ($relative || $from || $to) {
227 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
228 }
229 }
230 else {
231 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
232 if ($op) {
233 $clause = $this->whereClause($field,
234 $op,
235 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
236 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
237 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
238 );
239 }
240 }
241 if (!empty($this->_params['id_value'])) {
2107cde9 242 $this->_participantWhere = " AND civicrm_participant.event_id IN ( {$this->_params['id_value']} ) ";
6a488035
TO
243 }
244
245 if (!empty($clause)) {
246 $clauses[] = $clause;
247 }
248 }
249 }
250 }
99f13d8d 251 $clauses[] = "{$this->_aliases['civicrm_event']}.is_template = 0";
6a488035
TO
252 $this->_where = "WHERE " . implode(' AND ', $clauses);
253 }
254
74cf4551
EM
255 /**
256 * @param $rows
257 *
258 * @return array
259 */
6a488035
TO
260 function statistics(&$rows) {
261 $statistics = parent::statistics($rows);
262 $select = "
263 SELECT SUM( {$this->_aliases['civicrm_line_item']}.participant_count ) as count,
264 SUM( {$this->_aliases['civicrm_line_item']}.line_total ) as amount";
265
266 $sql = "{$select} {$this->_from} {$this->_where}";
267
268 $dao = CRM_Core_DAO::executeQuery($sql);
269
270 if ($dao->fetch()) {
271 if ($dao->count && $dao->amount) {
272 $avg = $dao->amount / $dao->count;
273 }
274 $statistics['counts']['count'] = array(
275 'value' => $dao->count,
276 'title' => 'Total Participants',
277 'type' => CRM_Utils_Type::T_INT,
278 );
279 $statistics['counts']['amount'] = array(
280 'value' => $dao->amount,
281 'title' => 'Total Income',
282 'type' => CRM_Utils_Type::T_MONEY,
283 );
284 $statistics['counts']['avg '] = array(
285 'value' => $avg,
286 'title' => 'Average',
287 'type' => CRM_Utils_Type::T_MONEY,
288 );
289 }
290 return $statistics;
291 }
292
293 function groupBy() {
294 $this->assign('chartSupported', TRUE);
295 $this->_rollup = " WITH ROLLUP";
296 $this->_groupBy = " GROUP BY {$this->_aliases['civicrm_event']}.id {$this->_rollup}";
297 }
298
299 function postProcess() {
300
301 $this->beginPostProcess();
302
303 $sql = $this->buildQuery(TRUE);
304
305 $dao = CRM_Core_DAO::executeQuery($sql);
306
307 //set pager before execution of query in function participantInfo()
308 $this->setPager();
309
310 $rows = $graphRows = array();
311 $count = 0;
312
313 while ($dao->fetch()) {
314 $row = array();
315 foreach ($this->_columnHeaders as $key => $value) {
316 if (($key == 'civicrm_event_start_date') || ($key == 'civicrm_event_end_date')) {
317 //get event start date and end date in custom datetime format
318 $row[$key] = CRM_Utils_Date::customFormat($dao->$key);
319 }
320 elseif ($key == 'civicrm_participant_fee_amount_avg') {
321 if ($dao->civicrm_participant_fee_amount_sum && $dao->civicrm_line_item_participant_count_count) {
322 $row[$key] = $dao->civicrm_participant_fee_amount_sum / $dao->civicrm_line_item_participant_count_count;
323 }
324 }
325 elseif ($key == 'civicrm_line_item_line_total_avg') {
326 if ($dao->civicrm_line_item_line_total_sum && $dao->civicrm_line_item_participant_count_count) {
327 $row[$key] = $dao->civicrm_line_item_line_total_sum / $dao->civicrm_line_item_participant_count_count;
328 }
329 }
330 else {
331 if (isset($dao->$key)) {
332 $row[$key] = $dao->$key;
333 }
334 }
335 }
336 $rows[] = $row;
337 }
338
339 // do not call pager here
340 $this->formatDisplay($rows, FALSE);
341 unset($this->_columnHeaders['civicrm_event_id']);
342
343 $this->doTemplateAssignment($rows);
344
345 $this->endPostProcess($rows);
346 }
347
74cf4551
EM
348 /**
349 * @param $rows
350 */
6a488035
TO
351 function buildChart(&$rows) {
352
353 $this->_interval = 'events';
354 $countEvent = NULL;
a7488080 355 if (!empty($this->_params['charts'])) {
6a488035
TO
356 foreach ($rows as $key => $value) {
357 if ($value['civicrm_event_id']) {
358 $graphRows['totalParticipants'][] = ($rows[$key]['civicrm_line_item_participant_count_count']);
359 $graphRows[$this->_interval][] = substr($rows[$key]['civicrm_event_title'], 0, 12) . "..(" . $rows[$key]['civicrm_event_id'] . ") ";
360 $graphRows['value'][] = ($rows[$key]['civicrm_line_item_participant_count_count']);
361 }
362 }
363
364 if (($rows[$key]['civicrm_line_item_participant_count_count']) == 0) {
365 $countEvent = count($rows);
366 }
367
368 if ((!empty($rows)) && $countEvent != 1) {
369 $chartInfo = array(
370 'legend' => 'Participants Summary',
371 'xname' => 'Event',
372 'yname' => 'Total Participants',
373 );
374 if (!empty($graphRows)) {
375 foreach ($graphRows[$this->_interval] as $key => $val) {
376 $graph[$val] = $graphRows['value'][$key];
377 }
378 $chartInfo['values'] = $graph;
379 $chartInfo['tip'] = 'Participants : #val#';
380 $chartInfo['xLabelAngle'] = 20;
381
382 // build the chart.
383 CRM_Utils_OpenFlashChart::buildChart($chartInfo, $this->_params['charts']);
384 }
385 }
386 }
387 }
388
74cf4551
EM
389 /**
390 * @param $rows
391 */
6a488035
TO
392 function alterDisplay(&$rows) {
393
394 if (is_array($rows)) {
395 $eventType = CRM_Core_OptionGroup::values('event_type');
396
397 foreach ($rows as $rowNum => $row) {
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/participantlist',
402 'reset=1&force=1&event_id_op=eq&event_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 Participants 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