3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2018
33 class CRM_Report_Form_Event_Summary
extends CRM_Report_Form_Event
{
35 protected $_summary = NULL;
37 protected $_charts = array(
39 'barChart' => 'Bar Chart',
40 'pieChart' => 'Pie Chart',
43 protected $_add2groupSupported = FALSE;
45 protected $_customGroupExtends = array(
48 public $_drilldownReport = array('event/income' => 'Link to Detail Report');
53 public function __construct() {
55 $this->_columns
= array(
56 'civicrm_event' => array(
57 'dao' => 'CRM_Event_DAO_Event',
64 'title' => ts('Event Title'),
67 'event_type_id' => array(
68 'title' => ts('Event Type'),
71 'fee_label' => array('title' => ts('Fee Label')),
72 'event_start_date' => array(
73 'title' => ts('Event Start Date'),
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
,
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)),
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'),
95 'event_start_date' => array(
96 'title' => ts('Event Start Date'),
97 'operatorType' => CRM_Report_Form
::OP_DATE
,
99 'event_end_date' => array(
100 'title' => ts('Event End Date'),
101 'operatorType' => CRM_Report_Form
::OP_DATE
,
106 $this->_currencyColumn
= 'civicrm_participant_fee_currency';
107 parent
::__construct();
110 public function preProcess() {
111 parent
::preProcess();
114 public function select() {
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])
122 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
128 $this->_selectClauses
= $select;
129 $this->_select
= 'SELECT ' . implode(', ', $select);
132 public function from() {
133 $this->_from
= " FROM civicrm_event {$this->_aliases['civicrm_event']} ";
136 public function where() {
138 $this->_participantWhere
= "";
139 foreach ($this->_columns
as $tableName => $table) {
140 if (array_key_exists('filters', $table)) {
141 foreach ($table['filters'] as $fieldName => $field) {
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
);
148 if ($relative ||
$from ||
$to) {
149 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
153 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
155 $clause = $this->whereClause($field,
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
)
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 ) ";
168 if (!empty($clause)) {
169 $clauses[] = $clause;
174 $clauses[] = "{$this->_aliases['civicrm_event']}.is_template = 0";
175 $this->_where
= 'WHERE ' . implode(' AND ', $clauses);
178 public function groupBy() {
179 $this->assign('chartSupported', TRUE);
180 $this->_groupBy
= CRM_Contact_BAO_Query
::getGroupByFromSelectColumns($this->_selectClauses
, "{$this->_aliases['civicrm_event']}.id");
184 * get participants information for events.
187 public function participantInfo() {
189 $statusType1 = CRM_Event_PseudoConstant
::participantStatus(NULL, 'is_counted = 1');
190 $statusType2 = CRM_Event_PseudoConstant
::participantStatus(NULL, 'is_counted = 0');
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
199 FROM civicrm_participant
201 WHERE civicrm_participant.is_test = 0
202 $this->_participantWhere
204 GROUP BY civicrm_participant.event_id,
205 civicrm_participant.status_id,
206 civicrm_participant.fee_currency";
208 $info = CRM_Core_DAO
::executeQuery($sql);
209 $participant_data = $participant_info = $currency = array();
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
;
217 $amt = $particiType1 = $particiType2 = 0;
219 foreach ($participant_data as $event_id => $event_data) {
220 foreach ($event_data as $status_id => $data) {
222 if (array_key_exists($status_id, $statusType1)) {
223 //total income of event
224 $amt = $amt +
$data['amount'];
226 //number of Registered/Attended participants
227 $particiType1 = $particiType1 +
$data['participant'];
229 elseif (array_key_exists($status_id, $statusType2)) {
231 //number of No-show/Cancelled/Pending participants
232 $particiType2 = $particiType2 +
$data['participant'];
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;
243 return $participant_info;
247 * Build header for table.
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])
258 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
259 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
265 $statusType1 = CRM_Event_PseudoConstant
::participantStatus(NULL, 'is_counted = 1', 'label');
266 $statusType2 = CRM_Event_PseudoConstant
::participantStatus(NULL, 'is_counted = 0', 'label');
268 //make column header for participant status Registered/Attended
269 $type1_header = implode('/', $statusType1);
271 //make column header for participant status No-show/Cancelled/Pending
272 $type2_header = implode('/', $statusType2);
274 $this->_columnHeaders
['statusType1'] = array(
275 'title' => $type1_header,
276 'type' => CRM_Utils_Type
::T_INT
,
278 $this->_columnHeaders
['statusType2'] = array(
279 'title' => $type2_header,
280 'type' => CRM_Utils_Type
::T_INT
,
282 $this->_columnHeaders
['totalAmount'] = array(
283 'title' => ts('Total Income'),
284 'type' => CRM_Utils_Type
::T_STRING
,
288 public function postProcess() {
290 $this->beginPostProcess();
292 $this->buildColumnHeaders();
294 $sql = $this->buildQuery(TRUE);
296 $dao = CRM_Core_DAO
::executeQuery($sql);
298 //set pager before exicution of query in function participantInfo()
301 $rows = $graphRows = array();
303 while ($dao->fetch()) {
305 foreach ($this->_columnHeaders
as $key => $value) {
306 if (($key == 'civicrm_event_start_date') ||
307 ($key == 'civicrm_event_end_date')
309 //get event start date and end date in custom datetime format
310 $row[$key] = CRM_Utils_Date
::customFormat($dao->$key);
313 if (isset($dao->$key)) {
314 $row[$key] = $dao->$key;
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;
330 // do not call pager here
331 $this->formatDisplay($rows, FALSE);
332 unset($this->_columnHeaders
['civicrm_event_id']);
334 $this->doTemplateAssignment($rows);
336 $this->endPostProcess($rows);
342 public function buildChart(&$rows) {
343 $this->_interval
= 'events';
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'] . ") ";
352 if (CRM_Utils_Array
::value('totalAmount', $rows[$key]) == 0) {
353 $countEvent = count($rows);
356 if ((!empty($rows)) && $countEvent != 1) {
357 $config = CRM_Core_Config
::Singleton();
359 'legend' => 'Event Summary',
361 'yname' => "Total Amount ({$config->defaultCurrency})",
363 if (!empty($graphRows)) {
364 foreach ($graphRows[$this->_interval
] as $key => $val) {
365 $graph[$val] = $graphRows['value'][$key];
367 $chartInfo['values'] = $graph;
368 $chartInfo['xLabelAngle'] = 20;
371 CRM_Utils_OpenFlashChart
::buildChart($chartInfo, $this->_params
['charts']);
372 $this->assign('chartType', $this->_params
['charts']);
379 * Alter display of rows.
381 * Iterate through the rows retrieved via SQL and make changes for display purposes,
382 * such as rendering contacts as links.
385 * Rows generated by SQL, with an array for each row.
387 public function alterDisplay(&$rows) {
389 if (is_array($rows)) {
390 $eventType = CRM_Core_OptionGroup
::values('event_type');
392 foreach ($rows as $rowNum => $row) {
393 if (array_key_exists('totalAmount', $row) &&
394 array_key_exists('currency', $row)
396 $rows[$rowNum]['totalAmount'] = CRM_Utils_Money
::format($rows[$rowNum]['totalAmount'], $rows[$rowNum]['currency']);
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
405 $rows[$rowNum]['civicrm_event_title_link'] = $url;
406 $rows[$rowNum]['civicrm_event_title_hover'] = ts('View Event Income For this Event');
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];