Merge pull request #734 from mlutfy/crm12606
[civicrm-core.git] / CRM / Report / Form / Event / ParticipantListCount.php
CommitLineData
6a488035
TO
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 */
37class CRM_Report_Form_Event_ParticipantListCount extends CRM_Report_Form_Event {
38
39 protected $_summary = NULL;
40
41 protected $_customGroupExtends = array(
42 'Participant');
43
44 public $_drilldownReport = array('event/income' => 'Link to Detail Report');
45 function __construct() {
46 $this->_columns = array(
47 'civicrm_contact' =>
48 array(
49 'dao' => 'CRM_Contact_DAO_Contact',
50 'fields' =>
51 array(
52 'sort_name' =>
53 array('title' => ts('Name'),
54 'default' => TRUE,
55 'no_repeat' => TRUE,
56 'required' => TRUE,
57 ),
58 'id' =>
59 array(
60 'no_display' => TRUE,
61 'required' => TRUE,
62 ),
63 ),
64 'grouping' => 'contact-fields',
65 'filters' => array(
66 'sort_name' =>
67 array('title' => ts('Participant Name'),
68 'operator' => 'like',
69 ),
70 ),
71 ),
72 'civicrm_email' =>
73 array(
74 'dao' => 'CRM_Core_DAO_Email',
75 'fields' => array(
76 'email' =>
77 array('title' => ts('Email'),
78 'no_repeat' => TRUE,
79 ),
80 ),
81 'grouping' => 'contact-fields',
82 'filters' =>
83 array(
84 'email' =>
85 array('title' => ts('Participant E-mail'),
86 'operator' => 'like',
87 ),
88 ),
89 ),
90 'civicrm_phone' =>
91 array(
92 'dao' => 'CRM_Core_DAO_Phone',
93 'grouping' => 'contact-fields',
94 'fields' => array(
95 'phone' => array(
96 'title' => ts( 'Phone No' ),
97 'default' => true,
98 ),
99 ),
100 ),
101 'civicrm_address' =>
102 array(
103 'dao' => 'CRM_Core_DAO_Address',
104 'fields' =>
105 array(
106 'street_address' => NULL,
107 ),
108 'grouping' => 'contact-fields',
109 ),
110 'civicrm_participant' =>
111 array(
112 'dao' => 'CRM_Event_DAO_Participant',
113 'fields' =>
114 array(
115 'participant_id' =>
116 array('title' => ts('Participant ID'),
117 'default' => TRUE,
118 ),
119 'event_id' =>
120 array('title' => ts('Event'),
121 'type' => CRM_Utils_Type::T_STRING,
122 ),
123 'role_id' =>
124 array('title' => ts('Role'),
125 'default' => TRUE,
126 ),
127 'status_id' =>
128 array('title' => ts('Status'),
129 'default' => TRUE,
130 ),
131 'participant_register_date' =>
132 array('title' => ts('Registration Date'),
133 ),
134 ),
135 'grouping' => 'event-fields',
136 'filters' =>
137 array(
138 'event_id' =>
139 array(
140 'name' => 'event_id',
141 'title' => ts('Event'),
142 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
143 'options' => $this->getEventFilterOptions(),
144 ),
145 'sid' =>
146 array(
147 'name' => 'status_id',
148 'title' => ts('Participant Status'),
149 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
150 'options' => CRM_Event_PseudoConstant::participantStatus(NULL, NULL, 'label'),
151 ),
152 'rid' =>
153 array(
154 'name' => 'role_id',
155 'title' => ts('Participant Role'),
156 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
157 'options' => CRM_Event_PseudoConstant::participantRole(),
158 ),
159 'participant_register_date' => array('title' => ts('Registration Date'),
160 'operatorType' => CRM_Report_Form::OP_DATE,
161 ),
162 ),
163 'group_bys' =>
164 array(
165 'event_id' =>
166 array('title' => ts('Event'),
167 ),
168 ),
169 ),
170 'civicrm_event' =>
171 array(
172 'dao' => 'CRM_Event_DAO_Event',
173 'fields' =>
174 array(
175 'event_type_id' =>
176 array('title' => ts('Event Type'),
177 ),
178 'start_date' =>
179 array('title' => ts('Event Start Date'),
180 ),
181 'end_date' =>
182 array('title' => ts('Event End Date'),
183 ),
184 ),
185 'grouping' => 'event-fields',
186 'filters' => array(
187 'eid' =>
188 array(
189 'name' => 'event_type_id',
190 'title' => ts('Event Type'),
191 'operatorType' => CRM_Report_Form::OP_MULTISELECT,
192 'options' => CRM_Core_OptionGroup::values('event_type'),
193 ),
194 'event_start_date' =>
195 array(
196 'name' => 'start_date',
197 'title' => ts('Event Start Date'),
198 'operatorType' => CRM_Report_Form::OP_DATE,
199 ),
200 'event_end_date' =>
201 array(
202 'name' => 'end_date',
203 'title' => ts('Event End Date'),
204 'operatorType' => CRM_Report_Form::OP_DATE,
205 ),
206 ),
207 'group_bys' =>
208 array(
209 'event_type_id' =>
210 array('title' => ts('Event Type '),
211 ),
212 ),
213 ),
214 'civicrm_line_item' =>
215 array(
216 'dao' => 'CRM_Price_DAO_LineItem',
217 'fields' =>
218 array(
219 'line_total' =>
220 array('title' => ts('Income'),
221 'default' => TRUE,
222 'statistics' =>
223 array('sum' => ts('Amount'),
224 'avg' => ts('Average'),
225 ),
226 ),
227 'participant_count' =>
228 array('title' => ts('Count'),
229 'default' => TRUE,
230 'statistics' =>
231 array('sum' => ts('Count'),
232 ),
233 ),
234 ),
235 ),
236 );
237
238 $this->_options = array(
239 'blank_column_begin' =>
240 array('title' => ts('Blank column at the Begining'),
241 'type' => 'checkbox',
242 ),
243 'blank_column_end' =>
244 array('title' => ts('Blank column at the End'),
245 'type' => 'select',
246 'options' => array(
247 '' => '-select-',
248 1 => ts('One'),
249 2 => ts('Two'),
250 3 => ts('Three'),
251 ),
252 ),
253 );
254 parent::__construct();
255 }
256
257 function preProcess() {
258 parent::preProcess();
259 }
260
261 //Add The statistics
262 function statistics(&$rows) {
263
264 $statistics = parent::statistics($rows);
265 $avg = NULL;
266 $select = " SELECT SUM( {$this->_aliases['civicrm_line_item']}.participant_count ) as count,
267 SUM( {$this->_aliases['civicrm_line_item']}.line_total ) as amount
268 ";
269 $sql = "{$select} {$this->_from} {$this->_where}";
270 $dao = CRM_Core_DAO::executeQuery($sql);
271 if ($dao->fetch()) {
272
273 if ($dao->count && $dao->amount) {
274 $avg = $dao->amount / $dao->count;
275 }
276 $statistics['counts']['count'] = array(
277 'value' => $dao->count,
278 'title' => 'Total Participants',
279 'type' => CRM_Utils_Type::T_INT,
280 );
281 $statistics['counts']['amount'] = array(
282 'value' => $dao->amount,
283 'title' => 'Total Income',
284 'type' => CRM_Utils_Type::T_MONEY,
285 );
286 $statistics['counts']['avg '] = array(
287 'value' => $avg,
288 'title' => 'Average',
289 'type' => CRM_Utils_Type::T_MONEY,
290 );
291 }
292
293 return $statistics;
294 }
295
296 function select() {
297 $select = array();
298 $this->_columnHeaders = array();
299
300 //add blank column at the Start
301 if (array_key_exists('options', $this->_params) && CRM_Utils_Array::value('blank_column_begin', $this->_params['options'])) {
302 $select[] = " '' as blankColumnBegin";
303 $this->_columnHeaders['blankColumnBegin']['title'] = '_ _ _ _';
304 }
305 foreach ($this->_columns as $tableName => $table) {
306 if (array_key_exists('fields', $table)) {
307 foreach ($table['fields'] as $fieldName => $field) {
308 if (CRM_Utils_Array::value('required', $field) ||
309 CRM_Utils_Array::value($fieldName, $this->_params['fields'])
310 ) {
311 if (CRM_Utils_Array::value('statistics', $field)) {
312 foreach ($field['statistics'] as $stat => $label) {
313 switch (strtolower($stat)) {
314 case 'sum':
315 $select[] = "SUM({$field['dbAlias']}) as {$tableName}_{$fieldName}_{$stat}";
316 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['type'] = CRM_Utils_Type::T_INT;
317 $this->_columnHeaders["{$tableName}_{$fieldName}_{$stat}"]['title'] = $label;
318 $this->_statFields[] = "{$tableName}_{$fieldName}_{$stat}";
319 break;
320 }
321 }
322 }
323 else {
324 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
325 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
326 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title'];
327 }
328 }
329 }
330 }
331 }
332 //add blank column at the end
333 if ($blankcols = CRM_Utils_Array::value('blank_column_end', $this->_params)) {
334 for ($i = 1; $i <= $blankcols; $i++) {
335 $select[] = " '' as blankColumnEnd_{$i}";
336 $this->_columnHeaders["blank_{$i}"]['title'] = "_ _ _ _";
337 }
338 }
339 $this->_select = "SELECT " . implode(', ', $select) . " ";
340 }
341
342 static function formRule($fields, $files, $self) {
343 $errors = $grouping = array();
344 return $errors;
345 }
346
347 function from() {
348 $this->_from = "
349 FROM civicrm_participant {$this->_aliases['civicrm_participant']}
350 LEFT JOIN civicrm_event {$this->_aliases['civicrm_event']}
351 ON ({$this->_aliases['civicrm_event']}.id = {$this->_aliases['civicrm_participant']}.event_id ) AND ({$this->_aliases['civicrm_event']}.is_template IS NULL OR {$this->_aliases['civicrm_event']}.is_template = 0)
352 LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
353 ON ({$this->_aliases['civicrm_participant']}.contact_id = {$this->_aliases['civicrm_contact']}.id )
354 {$this->_aclFrom}
355 LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
356 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND
357 {$this->_aliases['civicrm_address']}.is_primary = 1
358 LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
359 ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND
360 {$this->_aliases['civicrm_email']}.is_primary = 1)
361 LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
362 ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
363 {$this->_aliases['civicrm_phone']}.is_primary = 1
364 LEFT JOIN civicrm_line_item {$this->_aliases['civicrm_line_item']}
365 ON {$this->_aliases['civicrm_participant']}.id ={$this->_aliases['civicrm_line_item']}.entity_id AND {$this->_aliases['civicrm_line_item']}.entity_table = 'civicrm_participant'";
366 }
367
368 function where() {
369 $clauses = array();
370 foreach ($this->_columns as $tableName => $table) {
371 if (array_key_exists('filters', $table)) {
372 foreach ($table['filters'] as $fieldName => $field) {
373 $clause = NULL;
374 if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) {
375 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
376 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
377 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
378
379 if ($relative || $from || $to) {
380 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
381 }
382 }
383 else {
384 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
385
386 if ($fieldName == 'rid') {
387 $value = CRM_Utils_Array::value("{$fieldName}_value", $this->_params);
388 if (!empty($value)) {
389 $clause = "( {$field['dbAlias']} REGEXP '[[:<:]]" . implode('[[:>:]]|[[:<:]]', $value) . "[[:>:]]' )";
390 }
391 $op = NULL;
392 }
393
394 if ($op) {
395 $clause = $this->whereClause($field,
396 $op,
397 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
398 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
399 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
400 );
401 }
402 }
403
404 if (!empty($clause)) {
405 $clauses[] = $clause;
406 }
407 }
408 }
409 }
410
411 if (empty($clauses)) {
412 $this->_where = "WHERE {$this->_aliases['civicrm_participant']}.is_test = 0 ";
413 }
414 else {
415 $this->_where = "WHERE {$this->_aliases['civicrm_participant']}.is_test = 0 AND " . implode(' AND ', $clauses);
416 }
417 if ($this->_aclWhere) {
418 $this->_where .= " AND {$this->_aclWhere} ";
419 }
420 }
421
422 function groupBy() {
423 $this->_groupBy = "";
424 if (CRM_Utils_Array::value('group_bys', $this->_params) &&
425 is_array($this->_params['group_bys']) &&
426 !empty($this->_params['group_bys'])
427 ) {
428 foreach ($this->_columns as $tableName => $table) {
429 if (array_key_exists('group_bys', $table)) {
430 foreach ($table['group_bys'] as $fieldName => $field) {
431 if (CRM_Utils_Array::value($fieldName, $this->_params['group_bys'])) {
432 $this->_groupBy[] = $field['dbAlias'];
433 }
434 }
435 }
436 }
437 }
438
439 if (!empty($this->_groupBy)) {
440 $this->_groupBy = "ORDER BY " . implode(', ', $this->_groupBy) . ", {$this->_aliases['civicrm_contact']}.sort_name";
441 }
442 else {
443 $this->_groupBy = "ORDER BY {$this->_aliases['civicrm_contact']}.sort_name";
444 }
445 $this->_groupBy = "GROUP BY {$this->_aliases['civicrm_participant']}.id " . $this->_groupBy;
446 }
447
448 function postProcess() {
449
450 // get ready with post process params
451 $this->beginPostProcess();
452
453 // get the acl clauses built before we assemble the query
454 $this->buildACLClause($this->_aliases['civicrm_contact']);
455 // build query
456 $sql = $this->buildQuery(TRUE);
457
458 // build array of result based on column headers. This method also allows
459 // modifying column headers before using it to build result set i.e $rows.
460 $this->buildRows($sql, $rows);
461
462 // format result set.
463 $this->formatDisplay($rows);
464
465 // assign variables to templates
466 $this->doTemplateAssignment($rows);
467
468 // do print / pdf / instance stuff if needed
469 $this->endPostProcess($rows);
470 }
471
472 function alterDisplay(&$rows) {
473
474 $entryFound = FALSE;
475 $eventType = CRM_Core_OptionGroup::values('event_type');
476
477 foreach ($rows as $rowNum => $row) {
478
479 // convert sort name to links
480 if (array_key_exists('civicrm_contact_sort_name', $row) &&
481 array_key_exists('civicrm_contact_id', $row)
482 ) {
483 if ($value = $row['civicrm_contact_sort_name']) {
484 $url = CRM_Utils_System::url("civicrm/contact/view",
485 'reset=1&cid=' . $row['civicrm_contact_id'],
486 $this->_absoluteUrl
487 );
488 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
489 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact Summary for this Contact.");
490 }
491 $entryFound = TRUE;
492 }
493
494 // convert participant ID to links
495 if (array_key_exists('civicrm_participant_participant_id', $row) &&
496 array_key_exists('civicrm_contact_id', $row)
497 ) {
498 if ($value = $row['civicrm_participant_participant_id']) {
499 $url = CRM_Utils_System::url("civicrm/contact/view/participant",
500 'reset=1&id=' . $row['civicrm_participant_participant_id'] . '&cid=' . $row['civicrm_contact_id'] . '&action=view&context=participant',
501 $this->_absoluteUrl
502 );
503 $rows[$rowNum]['civicrm_participant_participant_id_link'] = $url;
504 $rows[$rowNum]['civicrm_participant_participant_id_hover'] = ts("View Participant Record for this Contact.");
505 }
506 $entryFound = TRUE;
507 }
508
509 // convert event name to links
510 if (array_key_exists('civicrm_participant_event_id', $row)) {
511 if ($value = $row['civicrm_participant_event_id']) {
512 $rows[$rowNum]['civicrm_participant_event_id'] = CRM_Event_PseudoConstant::event($value, FALSE);
513 $url = CRM_Report_Utils_Report::getNextUrl('event/Income',
514 'reset=1&force=1&event_id_op=eq&event_id_value=' . $value,
515 $this->_absoluteUrl, $this->_id, $this->_drilldownReport
516 );
517 $rows[$rowNum]['civicrm_participant_event_id_link'] = $url;
518 $rows[$rowNum]['civicrm_participant_event_id_hover'] = ts("View Event Income Details for this Event");
519 }
520 $entryFound = TRUE;
521 }
522
523 // handle event type id
524 if (array_key_exists('civicrm_event_event_type_id', $row)) {
525 if ($value = $row['civicrm_event_event_type_id']) {
526 $rows[$rowNum]['civicrm_event_event_type_id'] = $eventType[$value];
527 }
528 $entryFound = TRUE;
529 }
530
531 // handle participant status id
532 if (array_key_exists('civicrm_participant_status_id', $row)) {
533 if ($value = $row['civicrm_participant_status_id']) {
534 $rows[$rowNum]['civicrm_participant_status_id'] = CRM_Event_PseudoConstant::participantStatus($value, FALSE);
535 }
536 $entryFound = TRUE;
537 }
538
539 // handle participant role id
540 if (array_key_exists('civicrm_participant_role_id', $row)) {
541 if ($value = $row['civicrm_participant_role_id']) {
542 $roles = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value);
543 $value = array();
544 foreach ($roles as $role) {
545 $value[$role] = CRM_Event_PseudoConstant::participantRole($role, FALSE);
546 }
547 $rows[$rowNum]['civicrm_participant_role_id'] = implode(', ', $value);
548 }
549 $entryFound = TRUE;
550 }
551
552
553 // skip looking further in rows, if first row itself doesn't
554 // have the column we need
555 if (!$entryFound) {
556 break;
557 }
558 }
559 }
560}
561