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