CRM-9394
[civicrm-core.git] / CRM / Report / Form / Event / Summary.php
1 <?php
2 // $Id$
3
4 /*
5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.3 |
7 +--------------------------------------------------------------------+
8 | Copyright CiviCRM LLC (c) 2004-2013 |
9 +--------------------------------------------------------------------+
10 | This file is a part of CiviCRM. |
11 | |
12 | CiviCRM is free software; you can copy, modify, and distribute it |
13 | under the terms of the GNU Affero General Public License |
14 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | |
16 | CiviCRM is distributed in the hope that it will be useful, but |
17 | WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
19 | See the GNU Affero General Public License for more details. |
20 | |
21 | You should have received a copy of the GNU Affero General Public |
22 | License and the CiviCRM Licensing Exception along |
23 | with this program; if not, contact CiviCRM LLC |
24 | at info[AT]civicrm[DOT]org. If you have questions about the |
25 | GNU Affero General Public License or the licensing of CiviCRM, |
26 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
27 +--------------------------------------------------------------------+
28 */
29
30 /**
31 *
32 * @package CRM
33 * @copyright CiviCRM LLC (c) 2004-2013
34 * $Id$
35 *
36 */
37 class CRM_Report_Form_Event_Summary extends CRM_Report_Form_Event {
38
39 protected $_summary = NULL;
40
41 protected $_charts = array(
42 '' => 'Tabular',
43 'barChart' => 'Bar Chart',
44 'pieChart' => 'Pie Chart',
45 );
46
47 protected $_add2groupSupported = FALSE;
48
49 protected $_customGroupExtends = array(
50 'Event');
51 public $_drilldownReport = array('event/income' => 'Link to Detail Report');
52
53 function __construct() {
54
55 $this->_columns = array(
56 'civicrm_event' =>
57 array(
58 'dao' => 'CRM_Event_DAO_Event',
59 'fields' =>
60 array(
61 'id' => array(
62 'no_display' => TRUE,
63 'required' => TRUE,
64 ),
65 'title' => array('title' => ts('Event Title'),
66 'required' => TRUE,
67 ),
68 'event_type_id' => array('title' => ts('Event Type'),
69 'required' => TRUE,
70 ),
71 'fee_label' => array('title' => ts('Fee Label')),
72 'event_start_date' => array('title' => ts('Event Start Date'),
73 ),
74 'event_end_date' => array('title' => ts('Event End Date')),
75 'max_participants' => array('title' => ts('Capacity'),
76 'type' => CRM_Utils_Type::T_INT,
77 ),
78 ),
79 'filters' =>
80 array(
81 'id' => array('title' => ts('Event Title'),
82 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
83 'options' => $this->getEventFilterOptions(),
84 ),
85 'event_type_id' => array(
86 'name' => 'event_type_id',
87 'title' => ts('Event Type'),
88 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
89 'options' => CRM_Core_OptionGroup::values('event_type'),
90 ),
91 'event_start_date' => array(
92 'title' => 'Event Start Date',
93 'operatorType' => CRM_Report_Form::OP_DATE,
94 ),
95 'event_end_date' => array(
96 'title' => 'Event End Date',
97 'operatorType' => CRM_Report_Form::OP_DATE,
98 ),
99 ),
100 ),
101 );
102
103 parent::__construct();
104 }
105
106 function preProcess() {
107 parent::preProcess();
108 }
109
110 function select() {
111 $select = array();
112 foreach ($this->_columns as $tableName => $table) {
113 if (array_key_exists('fields', $table)) {
114 foreach ($table['fields'] as $fieldName => $field) {
115 if (CRM_Utils_Array::value('required', $field) ||
116 CRM_Utils_Array::value($fieldName, $this->_params['fields'])
117 ) {
118 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
119 }
120 }
121 }
122 }
123
124 $this->_select = 'SELECT ' . implode(', ', $select);
125 }
126
127 function from() {
128 $this->_from = " FROM civicrm_event {$this->_aliases['civicrm_event']} ";
129 }
130
131 function where() {
132 $clauses = array();
133 $this->_participantWhere = "";
134 foreach ($this->_columns as $tableName => $table) {
135 if (array_key_exists('filters', $table)) {
136 foreach ($table['filters'] as $fieldName => $field) {
137 $clause = NULL;
138 if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) {
139 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
140 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
141 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
142
143 if ($relative || $from || $to) {
144 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
145 }
146 }
147 else {
148 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
149 if ($op) {
150 $clause = $this->whereClause($field,
151 $op,
152 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
153 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
154 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
155 );
156 }
157 }
158 if (!empty($this->_params['id_value'])) {
159 $participant = implode(', ', $this->_params['id_value']);
160 $this->_participantWhere = " AND civicrm_participant.event_id IN ( {$participant} ) ";
161 }
162
163 if (!empty($clause)) {
164 $clauses[] = $clause;
165 }
166 }
167 }
168 }
169 $clauses[] = "({$this->_aliases['civicrm_event']}.is_template IS NULL OR {$this->_aliases['civicrm_event']}.is_template = 0)";
170 $this->_where = 'WHERE ' . implode(' AND ', $clauses);
171 }
172
173 function groupBy() {
174 $this->assign('chartSupported', TRUE);
175 $this->_groupBy = " GROUP BY {$this->_aliases['civicrm_event']}.id";
176 }
177
178 //get participants information for events
179 function participantInfo() {
180
181 $statusType1 = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 1');
182 $statusType2 = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 0');
183
184 $sql = "
185 SELECT civicrm_participant.event_id AS event_id,
186 civicrm_participant.status_id AS statusId,
187 COUNT( civicrm_participant.id ) AS participant,
188 SUM( civicrm_participant.fee_amount ) AS amount,
189 civicrm_participant.fee_currency
190
191 FROM civicrm_participant
192
193 WHERE civicrm_participant.is_test = 0
194 $this->_participantWhere
195
196 GROUP BY civicrm_participant.event_id,
197 civicrm_participant.status_id";
198
199 $info = CRM_Core_DAO::executeQuery($sql);
200 $participant_data = $participant_info = $currency = array();
201
202 while ($info->fetch()) {
203 $participant_data[$info->event_id][$info->statusId]['participant'] = $info->participant;
204 $participant_data[$info->event_id][$info->statusId]['amount'] = $info->amount;
205 $currency[$info->event_id] = $info->fee_currency;
206 }
207
208 $amt = $particiType1 = $particiType2 = 0;
209
210 foreach ($participant_data as $event_id => $event_data) {
211 foreach ($event_data as $status_id => $data) {
212
213 if (array_key_exists($status_id, $statusType1)) {
214 //total income of event
215 $amt = $amt + $data['amount'];
216
217 //number of Registered/Attended participants
218 $particiType1 = $particiType1 + $data['participant'];
219 }
220 elseif (array_key_exists($status_id, $statusType2)) {
221
222 //number of No-show/Cancelled/Pending participants
223 $particiType2 = $particiType2 + $data['participant'];
224 }
225 }
226
227 $participant_info[$event_id]['totalAmount'] = CRM_Utils_Money::format($amt, $currency[$event_id]);
228 $participant_info[$event_id]['statusType1'] = $particiType1;
229 $participant_info[$event_id]['statusType2'] = $particiType2;
230 $amt = $particiType1 = $particiType2 = 0;
231 }
232
233 return $participant_info;
234 }
235
236 //build header for table
237 function buildColumnHeaders() {
238
239 $this->_columnHeaders = array();
240 foreach ($this->_columns as $tableName => $table) {
241 if (array_key_exists('fields', $table)) {
242 foreach ($table['fields'] as $fieldName => $field) {
243 if (CRM_Utils_Array::value('required', $field) ||
244 CRM_Utils_Array::value($fieldName, $this->_params['fields'])
245 ) {
246
247 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
248 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array::value('title', $field);
249 }
250 }
251 }
252 }
253
254 $statusType1 = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 1');
255 $statusType2 = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 0');
256
257 //make column header for participant status Registered/Attended
258 $type1_header = implode('/', $statusType1);
259
260 //make column header for participant status No-show/Cancelled/Pending
261 $type2_header = implode('/', $statusType2);
262
263 $this->_columnHeaders['statusType1'] = array(
264 'title' => $type1_header,
265 'type' => CRM_Utils_Type::T_INT,
266 );
267 $this->_columnHeaders['statusType2'] = array(
268 'title' => $type2_header,
269 'type' => CRM_Utils_Type::T_INT,
270 );
271 $this->_columnHeaders['totalAmount'] = array(
272 'title' => 'Total Income',
273 'type' => CRM_Utils_Type::T_STRING,
274 );
275 }
276
277 function postProcess() {
278
279 $this->beginPostProcess();
280
281 $this->buildColumnHeaders();
282
283 $sql = $this->buildQuery(TRUE);
284
285 $dao = CRM_Core_DAO::executeQuery($sql);
286
287 //set pager before exicution of query in function participantInfo()
288 $this->setPager();
289
290 $rows = $graphRows = array();
291 $count = 0;
292 while ($dao->fetch()) {
293 $row = array();
294 foreach ($this->_columnHeaders as $key => $value) {
295 if (($key == 'civicrm_event_start_date') || ($key == 'civicrm_event_end_date')) {
296 //get event start date and end date in custom datetime format
297 $row[$key] = CRM_Utils_Date::customFormat($dao->$key);
298 }
299 else {
300 if (isset($dao->$key)) {
301 $row[$key] = $dao->$key;
302 }
303 }
304 }
305 $rows[] = $row;
306 }
307 if (!empty($rows)) {
308 $participant_info = $this->participantInfo();
309 foreach ($rows as $key => $value) {
310 if (array_key_exists($value['civicrm_event_id'], $participant_info)) {
311 foreach ($participant_info[$value['civicrm_event_id']] as $k => $v) {
312 $rows[$key][$k] = $v;
313 }
314 }
315 }
316 }
317 // do not call pager here
318 $this->formatDisplay($rows, FALSE);
319 unset($this->_columnHeaders['civicrm_event_id']);
320
321 $this->doTemplateAssignment($rows);
322
323 $this->endPostProcess($rows);
324 }
325
326 function buildChart(&$rows) {
327 $this->_interval = 'events';
328 $countEvent = NULL;
329 if (CRM_Utils_Array::value('charts', $this->_params)) {
330 foreach ($rows as $key => $value) {
331 $graphRows['totalAmount'][] = $graphRows['value'][] = CRM_Utils_Array::value('totalAmount', $rows[$key]);
332 $graphRows[$this->_interval][] = substr($rows[$key]['civicrm_event_title'], 0, 12) . "..(" . $rows[$key]['civicrm_event_id'] . ") ";
333 }
334
335 if (CRM_Utils_Array::value('totalAmount', $rows[$key]) == 0) {
336 $countEvent = count($rows);
337 }
338
339 if ((!empty($rows)) && $countEvent != 1) {
340 $config = CRM_Core_Config::Singleton();
341 $chartInfo = array(
342 'legend' => 'Event Summary',
343 'xname' => 'Event',
344 'yname' => "Total Amount ({$config->defaultCurrency})",
345 );
346 if (!empty($graphRows)) {
347 foreach ($graphRows[$this->_interval] as $key => $val) {
348 $graph[$val] = $graphRows['value'][$key];
349 }
350 $chartInfo['values'] = $graph;
351 $chartInfo['xLabelAngle'] = 20;
352
353 // build the chart.
354 CRM_Utils_OpenFlashChart::buildChart($chartInfo, $this->_params['charts']);
355 $this->assign('chartType', $this->_params['charts']);
356 }
357 }
358 }
359 }
360
361 function alterDisplay(&$rows) {
362
363 if (is_array($rows)) {
364 $eventType = CRM_Core_OptionGroup::values('event_type');
365 foreach ($rows as $rowNum => $row) {
366
367 if (array_key_exists('civicrm_event_title', $row)) {
368 if ($value = $row['civicrm_event_id']) {
369 //CRM_Event_PseudoConstant::event( $value, false );
370 $url = CRM_Report_Utils_Report::getNextUrl('event/income',
371 'reset=1&force=1&id_op=in&id_value=' . $value,
372 $this->_absoluteUrl, $this->_id, $this->_drilldownReport
373 );
374 $rows[$rowNum]['civicrm_event_title_link'] = $url;
375 $rows[$rowNum]['civicrm_event_title_hover'] = ts('View Event Income For this Event');
376 }
377 }
378
379 //handle event type
380 if (array_key_exists('civicrm_event_event_type_id', $row)) {
381 if ($value = $row['civicrm_event_event_type_id']) {
382 $rows[$rowNum]['civicrm_event_event_type_id'] = $eventType[$value];
383 }
384 }
385 }
386 }
387 }
388 }
389