From 28ac0bbe4a8c5caae46e067e915a834b86a2b63f Mon Sep 17 00:00:00 2001
From: Dave Greenberg <dave@civicrm.org>
Date: Wed, 2 Oct 2013 14:57:13 +0100
Subject: [PATCH] CRM-13279 Adding filters for Age, Age at Event and Birth
 Date. Also adding jQuery in the report template to expose a valid range of
 dates for birth date filter.

----------------------------------------
* CRM-13279: Include gender and date of birth in (participant) reports
  http://issues.civicrm.org/jira/browse/CRM-13279
---
 CRM/Report/Form/Event/ParticipantListing.php  | 51 +++++++++++++++++--
 .../Report/Form/Event/ParticipantListing.tpl  | 12 +++++
 2 files changed, 59 insertions(+), 4 deletions(-)

diff --git a/CRM/Report/Form/Event/ParticipantListing.php b/CRM/Report/Form/Event/ParticipantListing.php
index 859a3c44d2..39060238d0 100644
--- a/CRM/Report/Form/Event/ParticipantListing.php
+++ b/CRM/Report/Form/Event/ParticipantListing.php
@@ -38,10 +38,12 @@ class CRM_Report_Form_Event_ParticipantListing extends CRM_Report_Form_Event {
 
   protected $_contribField = FALSE;
 
-  protected $_customGroupExtends = array('Participant');
+  protected $_customGroupExtends = array('Participant', 'Contact', 'Individual',);
+
   public $_drilldownReport = array('event/income' => 'Link to Detail Report');
 
   function __construct() {
+    $this->_autoIncludeIndexedFieldsAsOrderBys = 1;
 
     // Check if CiviCampaign is a) enabled and b) has active campaigns
     $config = CRM_Core_Config::singleton();
@@ -73,11 +75,19 @@ class CRM_Report_Form_Event_ParticipantListing extends CRM_Report_Form_Event {
             'no_display' => TRUE,
             'required' => TRUE,
           ),
+          'gender_id' =>
+          array('title' => ts('Gender'),
+          ),
           'birth_date' =>
           array('title' => ts('Birth Date'),
           ),
-          'gender_id' =>
-          array('title' => ts('Gender'),
+          'age' => array(
+            'title'   => ts('Age'),
+            'dbAlias' => 'TIMESTAMPDIFF(YEAR, contact_civireport.birth_date, CURDATE())',
+          ),
+          'age_at_event' => array(
+            'title'   => ts('Age at Event'),
+            'dbAlias' => 'TIMESTAMPDIFF(YEAR, contact_civireport.birth_date, event_civireport.start_date)',
           ),
           'employer_id' =>
           array('title' => ts('Organization'),
@@ -92,6 +102,21 @@ class CRM_Report_Form_Event_ParticipantListing extends CRM_Report_Form_Event {
             'default_weight' => '0',
             'default_order' => 'ASC',
           ),
+          'gender_id' =>
+          array(
+            'name' => 'gender_id',
+            'title' => ts('Gender'),
+          ),
+          'birth_date' =>
+          array(
+            'name' => 'birth_date',
+            'title' => ts('Birth Date'),
+          ),
+          'age_at_event' =>
+          array(
+            'name' => 'age_at_event',
+            'title' => ts('Age at Event'),
+          ),
         ),
         'filters' =>
         array(
@@ -99,6 +124,16 @@ class CRM_Report_Form_Event_ParticipantListing extends CRM_Report_Form_Event {
           array('title' => ts('Participant Name'),
             'operator' => 'like',
           ),
+          'gender_id' =>
+          array('title' => ts('Gender'),
+            'operatorType' => CRM_Report_Form::OP_MULTISELECT,
+            'options' => CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'),
+          ),
+          'birth_date' => array(
+            'title' => 'Birth Date',
+            'operatorType' => CRM_Report_Form::OP_DATE,
+            'type'         => CRM_Utils_Type::T_DATE
+          ),
         ),
       ),
       'civicrm_email' =>
@@ -656,6 +691,14 @@ class CRM_Report_Form_Event_ParticipantListing extends CRM_Report_Form_Event {
         $entryFound = TRUE;
       }
 
+      // display birthday in the configured custom format
+      if (array_key_exists('civicrm_contact_birth_date', $row)) {
+        if ($value = $row['civicrm_contact_birth_date']) {
+            $rows[$rowNum]['civicrm_contact_birth_date'] = CRM_Utils_Date::customFormat($row['civicrm_contact_birth_date'], '%Y%m%d');
+        }
+        $entryFound = TRUE;
+      }
+
       // skip looking further in rows, if first row itself doesn't
       // have the column we need
       if (!$entryFound) {
@@ -663,4 +706,4 @@ class CRM_Report_Form_Event_ParticipantListing extends CRM_Report_Form_Event {
       }
     }
   }
-}
+}
\ No newline at end of file
diff --git a/templates/CRM/Report/Form/Event/ParticipantListing.tpl b/templates/CRM/Report/Form/Event/ParticipantListing.tpl
index 39c23e75cd..9ed848f052 100644
--- a/templates/CRM/Report/Form/Event/ParticipantListing.tpl
+++ b/templates/CRM/Report/Form/Event/ParticipantListing.tpl
@@ -23,4 +23,16 @@
  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
  +--------------------------------------------------------------------+
 *}
+
+{literal}
+<script type="text/javascript">
+  cj(document).ready(function(){
+    cj('#birth_date_from').attr('startOffset',200);
+    cj('#birth_date_from').attr('endoffset',0);
+    cj('#birth_date_to').attr('startOffset',200);
+    cj('#birth_date_to').attr('endoffset',0);
+  });
+</script>
+{/literal}
+
 {include file="CRM/Report/Form.tpl"}
-- 
GitLab