3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
35 class CRM_Report_Form_Campaign_SurveyDetails
extends CRM_Report_Form
{
37 protected $_emailField = FALSE;
39 protected $_phoneField = FALSE;
41 protected $_locationBasedPhoneField = FALSE;
43 protected $_summary = NULL;
44 protected $_customGroupGroupBy = FALSE;
45 protected $_customGroupExtends = array(
52 public $_drilldownReport = array('contact/detail' => 'Link to Detail Report');
54 private static $_surveyRespondentStatus;
60 public function __construct() {
61 //filter options for survey activity status.
62 $responseStatus = array('' => '- Any -');
63 self
::$_surveyRespondentStatus = array();
64 $activityStatus = CRM_Core_PseudoConstant
::activityStatus('name');
65 if ($statusId = array_search('Scheduled', $activityStatus)) {
66 $responseStatus[$statusId] = ts('Reserved');
67 self
::$_surveyRespondentStatus[$statusId] = 'Reserved';
69 if ($statusId = array_search('Completed', $activityStatus)) {
70 $responseStatus[$statusId] = ts('Interviewed');
71 self
::$_surveyRespondentStatus[$statusId] = 'Interviewed';
74 $optionGroups = CRM_Campaign_BAO_Survey
::getResultSets('name');
75 $resultOptions = array();
76 foreach ($optionGroups as $gid => $name) {
79 $value = CRM_Core_OptionGroup
::values($name);
81 $value = array_combine($value, $value);
83 $resultOptions = $resultOptions +
$value;
86 asort($resultOptions);
88 //get all interviewers.
89 $allSurveyInterviewers = CRM_Campaign_BAO_Survey
::getInterviewers();
91 $this->_columns
= array(
92 'civicrm_activity_contact' => array(
93 'dao' => 'CRM_Activity_DAO_ActivityContact',
94 'fields' => array('contact_id' => array('title' => ts('Interviewer Name'))),
96 'contact_id' => array(
97 'name' => 'contact_id',
98 'title' => ts('Interviewer Name'),
99 'type' => CRM_Utils_Type
::T_INT
,
100 'operatorType' => CRM_Report_Form
::OP_SELECT
,
102 '' => ts('- any interviewer -'),
103 ) +
$allSurveyInterviewers,
106 'grouping' => 'survey-interviewer-fields',
108 'civicrm_contact' => array(
109 'dao' => 'CRM_Contact_DAO_Contact',
112 'title' => ts('Contact ID'),
113 'no_display' => TRUE,
116 'sort_name' => array(
117 'title' => ts('Respondent Name'),
123 'sort_name' => array(
124 'title' => ts('Respondent Name'),
125 'operator' => 'like',
128 'grouping' => 'contact-fields',
129 'order_bys' => array(
130 'sort_name' => array(
131 'title' => ts('Respondent Name'),
136 'civicrm_phone' => array(
137 'dao' => 'CRM_Core_DAO_Phone',
141 'title' => ts('Phone'),
144 'grouping' => 'location-fields',
146 'civicrm_address' => array(
147 'dao' => 'CRM_Core_DAO_Address',
149 'street_number' => array(
150 'name' => 'street_number',
151 'title' => ts('Street Number'),
154 'street_name' => array(
155 'name' => 'street_name',
156 'title' => ts('Street Name'),
159 'street_unit' => array(
160 'name' => 'street_unit',
161 'title' => ts('Street Unit'),
164 'postal_code' => array(
165 'name' => 'postal_code',
166 'title' => ts('Postal Code'),
171 'title' => ts('City'),
174 'state_province_id' => array(
175 'name' => 'state_province_id',
176 'title' => ts('State/Province'),
178 'country_id' => array(
179 'name' => 'country_id',
180 'title' => ts('Country'),
184 'street_number' => array(
185 'title' => ts('Street Number'),
187 'name' => 'street_number',
189 'street_name' => array(
190 'title' => ts('Street Name'),
191 'name' => 'street_name',
192 'operator' => 'like',
194 'postal_code' => array(
195 'title' => ts('Postal Code'),
197 'name' => 'postal_code',
200 'title' => ts('City'),
201 'operator' => 'like',
204 'state_province_id' => array(
205 'name' => 'state_province_id',
206 'title' => ts('State/Province'),
207 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
208 'options' => CRM_Core_PseudoConstant
::stateProvince(),
210 'country_id' => array(
211 'name' => 'country_id',
212 'title' => ts('Country'),
213 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
214 'options' => CRM_Core_PseudoConstant
::country(),
217 'order_bys' => array(
218 'street_name' => array('title' => ts('Street Name')),
219 'street_number_odd_even' => array(
220 'title' => ts('Odd / Even Street Number'),
221 'name' => 'street_number',
222 'dbAlias' => 'address_civireport.street_number%2',
224 'street_number' => array('title' => 'Street Number'),
226 'grouping' => 'location-fields',
228 'civicrm_email' => array(
229 'dao' => 'CRM_Core_DAO_Email',
233 'title' => ts('Email'),
236 'grouping' => 'location-fields',
238 'civicrm_activity' => array(
239 'dao' => 'CRM_Activity_DAO_Activity',
240 'alias' => 'survey_activity',
242 'survey_id' => array(
243 'name' => 'source_record_id',
244 'title' => ts('Survey'),
245 'type' => CRM_Utils_Type
::T_INT
,
246 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
247 'options' => CRM_Campaign_BAO_Survey
::getSurveys(),
249 'survey_response' => array(
250 'name' => 'survey_response',
251 'title' => ts('Survey Responses'),
255 'title' => ts('Note'),
261 'title' => ts('Survey Result'),
265 'survey_id' => array(
266 'name' => 'source_record_id',
267 'title' => ts('Survey'),
268 'type' => CRM_Utils_Type
::T_INT
,
269 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
270 'options' => CRM_Campaign_BAO_Survey
::getSurveys(),
272 'status_id' => array(
273 'name' => 'status_id',
274 'title' => ts('Respondent Status'),
275 'type' => CRM_Utils_Type
::T_INT
,
276 'operatorType' => CRM_Report_Form
::OP_SELECT
,
277 'options' => $responseStatus,
280 'title' => ts('Survey Result'),
281 'type' => CRM_Utils_Type
::T_STRING
,
282 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
283 'options' => $resultOptions,
286 'grouping' => 'survey-activity-fields',
289 parent
::__construct();
292 public function preProcess() {
293 parent
::preProcess();
296 public function select() {
299 //add the survey response fields.
300 $this->_addSurveyResponseColumns();
302 $this->_columnHeaders
= array();
303 foreach ($this->_columns
as $tableName => $table) {
304 if (!isset($table['fields'])) {
307 foreach ($table['fields'] as $fieldName => $field) {
308 if (!empty($field['required']) ||
309 !empty($this->_params
['fields'][$fieldName]) ||
310 CRM_Utils_Array
::value('is_required', $field)
313 $fieldsName = CRM_Utils_Array
::value(1, explode('_', $tableName));
315 $this->{"_$fieldsName" . 'Field'} = TRUE;
318 //need to pickup custom data/survey response fields.
319 if ($fieldName == 'survey_response') {
323 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
324 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = CRM_Utils_Array
::value('title', $field);
325 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array
::value('type', $field);
330 $this->_select
= "SELECT " . implode(",\n", $select) . " ";
333 public function from() {
334 $this->_from
= " FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom} ";
335 $activityContacts = CRM_Core_OptionGroup
::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
336 $assigneeID = CRM_Utils_Array
::key('Activity Assignees', $activityContacts);
337 $targetID = CRM_Utils_Array
::key('Activity Targets', $activityContacts);
339 //get the activity table joins.
340 $this->_from
.= " INNER JOIN civicrm_activity_contact civicrm_activity_target ON
341 ( {$this->_aliases['civicrm_contact']}.id = civicrm_activity_target.contact_id AND civicrm_activity_target.record_type_id = {$targetID}) \n";
342 $this->_from
.= " INNER JOIN civicrm_activity {$this->_aliases['civicrm_activity']} ON
343 ( {$this->_aliases['civicrm_activity']}.id = civicrm_activity_target.activity_id )\n";
344 $this->_from
.= " INNER JOIN civicrm_activity_contact activity_contact_civireport ON
345 ( {$this->_aliases['civicrm_activity']}.id = activity_contact_civireport.activity_id AND activity_contact_civireport.record_type_id = {$assigneeID} )\n";
347 //get the address table.
348 $this->_from
.= " LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']} ON
349 {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND {$this->_aliases['civicrm_address']}.is_primary = 1\n";
351 if ($this->_emailField
) {
352 $this->_from
.= "LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']} ON
353 {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND {$this->_aliases['civicrm_email']}.is_primary = 1\n";
356 if ($this->_phoneField
) {
357 $this->_from
.= "LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']} ON
358 {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND {$this->_aliases['civicrm_phone']}.is_primary = 1\n";
361 if ($this->_locationBasedPhoneField
) {
362 foreach ($this->_surveyResponseFields
as $key => $value) {
363 if (substr($key, 0, 5) == 'phone' && !empty($value['location_type_id'])
365 $fName = str_replace('-', '_', $key);
366 $this->_from
.= "LEFT JOIN civicrm_phone " .
367 $this->_aliases
["civicrm_phone_{$fName}"] .
368 " ON {$this->_aliases['civicrm_contact']}.id = " .
369 $this->_aliases
["civicrm_phone_{$fName}"] . ".contact_id AND " .
370 $this->_aliases
["civicrm_phone_{$fName}"] .
371 ".location_type_id = {$value['location_type_id']} AND " .
372 $this->_aliases
["civicrm_phone_{$fName}"] .
373 ".phone_type_id = {$value['phone_type_id']}\n";
379 public function where() {
381 foreach ($this->_columns
as $tableName => $table) {
382 if (array_key_exists('filters', $table)) {
383 foreach ($table['filters'] as $fieldName => $field) {
386 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
387 $relative = CRM_Utils_Array
::value("{$fieldName}_relative", $this->_params
);
388 $from = CRM_Utils_Array
::value("{$fieldName}_from", $this->_params
);
389 $to = CRM_Utils_Array
::value("{$fieldName}_to", $this->_params
);
391 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
394 $op = CRM_Utils_Array
::value("{$fieldName}_op", $this->_params
);
396 $clause = $this->whereClause($field,
398 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
399 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
400 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
405 if (!empty($clause)) {
406 $clauses[] = $clause;
412 //apply survey activity types filter.
413 $surveyActivityTypes = CRM_Campaign_BAO_Survey
::getSurveyActivityType();
414 if (!empty($surveyActivityTypes)) {
415 $clauses[] = "( {$this->_aliases['civicrm_activity']}.activity_type_id IN ( " .
416 implode(' , ', array_keys($surveyActivityTypes)) . ' ) )';
419 // always filter out deleted activities (so contacts that have been released
420 // don't show up in the report).
421 $clauses[] = "( {$this->_aliases['civicrm_activity']}.is_deleted = 0 )";
423 if (empty($clauses)) {
424 $this->_where
= "WHERE ( 1 ) ";
427 $this->_where
= "WHERE " . implode(' AND ', $clauses);
430 if ($this->_aclWhere
) {
431 $this->_where
.= " AND {$this->_aclWhere} ";
435 public function postProcess() {
436 // get the acl clauses built before we assemble the query
437 $this->buildACLClause($this->_aliases
['civicrm_contact']);
439 // get ready with post process params
440 $this->beginPostProcess();
443 $sql = $this->buildQuery();
445 // build array of result based on column headers. This method also allows
446 // modifying column headers before using it to build result set i.e $rows.
448 $this->buildRows($sql, $rows);
450 // format result set.
451 $this->formatDisplay($rows);
453 //call local post process for only print and pdf.
454 //we do need special formatted o/p only when we do have grouping
455 $orderBys = CRM_Utils_Array
::value('order_bys', $this->_params
, array());
456 if (in_array($this->_outputMode
, array(
462 $templateFile = parent
::getTemplateFileName();
463 if (array_key_exists('street_name', $orderBys) ||
464 array_key_exists('street_number', $orderBys)
466 $orderByStreetName = CRM_Utils_Array
::value('street_name', $orderBys);
467 $orderByStreetNum = CRM_Utils_Array
::value('street_number', $orderBys);
470 $dataPerPage = array();
471 $lastStreetName = $lastStreetNum = NULL;
472 foreach ($rows as $row) {
473 //do we need to take new page.
474 if ($orderByStreetName &&
476 CRM_Utils_Array
::value('civicrm_address_street_name', $row))
480 elseif ($orderByStreetNum &&
482 CRM_Utils_Array
::value('civicrm_address_street_number', $row) %
2
488 //get the data per page.
489 $dataPerPage[$pageCnt][] = $row;
490 $lastStreetName = CRM_Utils_Array
::value('civicrm_address_street_name', $row);
491 $lastStreetNum = CRM_Utils_Array
::value('civicrm_address_street_number', $row) %
2;
494 foreach ($dataPerPage as $page) {
495 // assign variables to templates
496 $this->doTemplateAssignment($page);
497 $outPut[] = CRM_Core_Form
::$_template->fetch($templateFile);
501 $this->doTemplateAssignment($rows);
502 $outPut[] = CRM_Core_Form
::$_template->fetch($templateFile);
505 $header = $this->_formValues
['report_header'];
506 $footer = $this->_formValues
['report_footer'];
508 //get the cover sheet.
509 $coverSheet = $this->_surveyCoverSheet();
510 $footerImage = preg_replace('/<\/html>|<\/body>|<\/div>/i', '', $footer);
512 $outPut = $header . $coverSheet .
513 "<div style=\"page-break-after: always\"></div>" .
514 implode($footerImage .
515 "<div style=\"page-break-after: always\"></div>",
519 if ($this->_outputMode
== 'print') {
523 CRM_Utils_PDF_Utils
::html2pdf($outPut, "CiviReport.pdf");
526 CRM_Utils_System
::civiExit();
529 $this->doTemplateAssignment($rows);
530 $this->endPostProcess($rows);
536 * @return bool|mixed|null|string
538 private function _surveyCoverSheet() {
540 $surveyIds = CRM_Utils_Array
::value('survey_id_value', $this->_params
);
541 if (CRM_Utils_System
::isNull($surveyIds)) {
547 $surveyResponseFields = array();
548 foreach ($this->_columns
as $tableName => $values) {
549 if (!is_array($values['fields'])) {
552 foreach ($values['fields'] as $name => $field) {
553 if (!empty($field['isSurveyResponseField'])) {
554 $fldId = substr($name, 7);
555 $fieldIds[$fldId] = $fldId;
556 $title = CRM_Utils_Array
::value('label', $field, $field['title']);
557 $surveyResponseFields[$name] = array(
560 'name' => "{$tableName}_{$name}",
566 //now pickup all options.
567 if (!empty($fieldIds)) {
569 SELECT field.id as id,
572 FROM civicrm_custom_field field
573 INNER JOIN civicrm_option_value val ON ( val.option_group_id = field.option_group_id )
574 WHERE field.id IN (' . implode(' , ', $fieldIds) . ' )
575 Order By val.weight';
576 $field = CRM_Core_DAO
::executeQuery($query);
578 while ($field->fetch()) {
579 $name = "custom_{$field->id}";
580 $surveyResponseFields[$name]['options'][$field->value
] = $field->label
;
584 //get the result values.
586 SELECT survey.id as id,
587 survey.title as title,
590 FROM civicrm_survey survey
591 INNER JOIN civicrm_option_value val ON ( val.option_group_id = survey.result_id )
592 WHERE survey.id IN ( ' . implode(' , ', array_values($surveyIds)) . ' )
593 Order By val.weight';
594 $resultSet = CRM_Core_DAO
::executeQuery($query);
595 $surveyResultFields = array();
596 while ($resultSet->fetch()) {
597 $surveyResultFields[$resultSet->id
]['title'] = $resultSet->title
;
598 $surveyResultFields[$resultSet->id
]['options'][$resultSet->value
] = $resultSet->label
;
601 $this->assign('surveyResultFields', $surveyResultFields);
602 $this->assign('surveyResponseFields', $surveyResponseFields);
604 $templateFile = 'CRM/Report/Form/Campaign/SurveyCoverSheet.tpl';
605 $coverSheet = CRM_Core_Form
::$_template->fetch($templateFile);
611 * Alter display of rows.
613 * Iterate through the rows retrieved via SQL and make changes for display purposes,
614 * such as rendering contacts as links.
617 * Rows generated by SQL, with an array for each row.
619 public function alterDisplay(&$rows) {
621 $this->_formatSurveyResult($rows);
622 $this->_formatSurveyResponseData($rows);
625 foreach ($rows as $rowNum => $row) {
626 // handle state province
627 if (array_key_exists('civicrm_address_state_province_id', $row)) {
628 if ($value = $row['civicrm_address_state_province_id']) {
629 $rows[$rowNum]['civicrm_address_state_province_id'] = CRM_Core_PseudoConstant
::stateProvince($value);
635 if (array_key_exists('civicrm_address_country_id', $row)) {
636 if ($value = $row['civicrm_address_country_id']) {
637 $rows[$rowNum]['civicrm_address_country_id'] = CRM_Core_PseudoConstant
::country($value);
642 // convert display name to links
643 if (array_key_exists('civicrm_contact_sort_name', $row) &&
644 array_key_exists('civicrm_contact_id', $row)
646 $url = CRM_Report_Utils_Report
::getNextUrl('contact/detail',
647 'reset=1&force=1&id_op=eq&id_value=' .
648 $row['civicrm_contact_id'],
649 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
651 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
655 if (array_key_exists('civicrm_activity_contact_contact_id', $row)) {
656 $rows[$rowNum]['civicrm_activity_contact_contact_id'] = CRM_Utils_Array
::value($row['civicrm_activity_contact_contact_id'],
657 CRM_Campaign_BAO_Survey
::getInterviewers()
662 if (array_key_exists('civicrm_activity_survey_id', $row)) {
663 $rows[$rowNum]['civicrm_activity_survey_id'] = CRM_Utils_Array
::value($row['civicrm_activity_survey_id'],
664 CRM_Campaign_BAO_Survey
::getSurveys()
669 // skip looking further in rows, if first row itself doesn't
670 // have the column we need
680 private function _formatSurveyResult(&$rows) {
681 $surveyIds = CRM_Utils_Array
::value('survey_id_value', $this->_params
);
682 if (CRM_Utils_System
::isNull($surveyIds) ||
683 empty($this->_params
['fields']['result']) ||
684 !in_array($this->_outputMode
, array('print', 'pdf'))
689 //swap the survey result label w/ value.
691 SELECT survey.id as id,
694 FROM civicrm_option_value val
695 INNER JOIN civicrm_option_group grp ON ( grp.id = val.option_group_id )
696 INNER JOIN civicrm_survey survey ON ( survey.result_id = grp.id )
697 WHERE survey.id IN (' . implode(' , ', array_values($surveyIds)) . ' )
698 Order By val.weight';
700 $result = CRM_Core_DAO
::executeQuery($query);
701 $resultSet = array();
702 while ($result->fetch()) {
703 $resultSet[$result->id
][$result->value
] = $result->label
;
706 $statusId = CRM_Utils_Array
::value('status_id_value', $this->_params
);
707 $respondentStatus = CRM_Utils_Array
::value($statusId, self
::$_surveyRespondentStatus);
709 $surveyId = CRM_Utils_Array
::value(0, $surveyIds);
710 foreach ($rows as & $row) {
711 if (!empty($row['civicrm_activity_survey_id'])) {
712 $surveyId = $row['civicrm_activity_survey_id'];
714 $result = CRM_Utils_Array
::value($surveyId, $resultSet, array());
715 $resultLabel = CRM_Utils_Array
::value('civicrm_activity_result', $row);
716 if ($respondentStatus == 'Reserved') {
717 $row['civicrm_activity_result'] = implode(' | ', array_keys($result));
719 elseif ($resultLabel) {
720 $resultValue = array_search($resultLabel, $result);
722 $row['civicrm_activity_result'] = $resultValue;
731 private function _formatSurveyResponseData(&$rows) {
732 $surveyIds = CRM_Utils_Array
::value('survey_id_value', $this->_params
);
733 if (CRM_Utils_System
::isNull($surveyIds) ||
734 empty($this->_params
['fields']['survey_response'])
739 $surveyResponseFields = array();
740 $surveyResponseFieldIds = array();
741 foreach ($this->_columns
as $tableName => $values) {
742 if (!is_array($values['fields'])) {
745 foreach ($values['fields'] as $name => $field) {
746 if (!empty($field['isSurveyResponseField'])) {
747 $fldId = substr($name, 7);
748 $surveyResponseFields[$name] = "{$tableName}_{$name}";
749 $surveyResponseFieldIds[$fldId] = $fldId;
754 if (empty($surveyResponseFieldIds)) {
758 $hasResponseData = FALSE;
759 foreach ($surveyResponseFields as $fldName) {
760 foreach ($rows as $row) {
761 if (!empty($row[$fldName])) {
762 $hasResponseData = TRUE;
768 //do check respondent status.
769 $statusId = CRM_Utils_Array
::value('status_id_value', $this->_params
);
770 $respondentStatus = CRM_Utils_Array
::value($statusId, self
::$_surveyRespondentStatus);
772 if (!$hasResponseData &&
773 ($respondentStatus != 'Reserved')
778 //start response data formatting.
787 FROM civicrm_custom_field cf
788 INNER JOIN civicrm_custom_group cg ON ( cg.id = cf.custom_group_id )
789 LEFT JOIN civicrm_option_value ov ON ( cf.option_group_id = ov.option_group_id )
790 WHERE cf.id IN ( ' . implode(' , ', $surveyResponseFieldIds) . ' )
793 $responseFields = array();
794 $fieldValueMap = array();
803 $responseField = CRM_Core_DAO
::executeQuery($query);
804 while ($responseField->fetch()) {
805 $reponseFldName = $responseField->table_name
. '_custom_' .
807 foreach ($properties as $prop) {
808 $responseFields[$reponseFldName][$prop] = $responseField->$prop;
810 if ($responseField->option_group_id
) {
811 //show value for print and pdf.
812 $value = $responseField->label
;
813 if (in_array($this->_outputMode
, array(
817 $value = $responseField->value
;
819 $fieldValueMap[$responseField->option_group_id
][$responseField->value
] = $value;
822 $responseField->free();
824 //actual data formatting.
826 foreach ($rows as & $row) {
827 if (!is_array($row)) {
831 foreach ($row as $name => & $value) {
832 if (!array_key_exists($name, $responseFields)) {
836 if ($respondentStatus == 'Reserved' &&
837 in_array($this->_outputMode
, array('print', 'pdf'))
839 $optGrpId = CRM_Utils_Array
::value('option_group_id', $responseFields[$name]);
840 $options = CRM_Utils_Array
::value($optGrpId, $fieldValueMap, array());
841 $value = implode(' | ', array_keys($options));
844 $value = $this->formatCustomValues($value,
845 $responseFields[$name],
857 private function _addSurveyResponseColumns() {
858 $surveyIds = CRM_Utils_Array
::value('survey_id_value', $this->_params
);
859 if (CRM_Utils_System
::isNull($surveyIds) ||
860 empty($this->_params
['fields']['survey_response'])
865 $responseFields = array();
866 foreach ($surveyIds as $surveyId) {
867 $responseFields +
= CRM_Campaign_BAO_Survey
::getSurveyResponseFields($surveyId);
868 $this->_surveyResponseFields
= $responseFields;
870 foreach ($responseFields as $key => $value) {
871 if (substr($key, 0, 5) == 'phone' && !empty($value['location_type_id'])) {
872 $fName = str_replace('-', '_', $key);
873 $this->_columns
["civicrm_{$fName}"] = array(
874 'dao' => 'CRM_Core_DAO_Phone',
875 'alias' => "phone_civireport_{$fName}",
877 $fName => array_merge($value, array(
878 'is_required' => '1',
879 'alias' => "phone_civireport_{$fName}",
880 'dbAlias' => "phone_civireport_{$fName}.phone",
881 'no_display' => TRUE,
886 $this->_aliases
["civicrm_phone_{$fName}"] = $this->_columns
["civicrm_{$fName}"]['alias'];
887 $this->_locationBasedPhoneField
= TRUE;
890 $responseFieldIds = array();
891 foreach (array_keys($responseFields) as $key) {
892 $cfId = CRM_Core_BAO_CustomField
::getKeyID($key);
894 $responseFieldIds[$cfId] = $cfId;
897 if (empty($responseFieldIds)) {
910 FROM civicrm_custom_group cg
911 INNER JOIN civicrm_custom_field cf ON ( cg.id = cf.custom_group_id )
912 WHERE cf.id IN ( ' . implode(' , ', $responseFieldIds) .
913 ' ) ORDER BY cf.weight';
914 $response = CRM_Core_DAO
::executeQuery($query);
916 while ($response->fetch()) {
917 $resTable = $response->table_name
;
918 $fieldName = "custom_{$response->cfId}";
920 //need to check does these custom data already included.
922 if (!array_key_exists($resTable, $this->_columns
)) {
923 $this->_columns
[$resTable]['dao'] = 'CRM_Contact_DAO_Contact';
924 $this->_columns
[$resTable]['extends'] = $response->extends;
926 if (empty($this->_columns
[$resTable]['alias'])) {
927 $this->_columns
[$resTable]['alias'] = "{$resTable}_survey_response";
929 if (!is_array(CRM_Utils_Array
::value('fields', $this->_columns
[$resTable]))) {
930 $this->_columns
[$resTable]['fields'] = array();
932 if (array_key_exists($fieldName, $this->_columns
[$resTable]['fields'])) {
933 $this->_columns
[$resTable]['fields'][$fieldName]['required'] = TRUE;
934 $this->_columns
[$resTable]['fields'][$fieldName]['isSurveyResponseField'] = TRUE;
938 $title = $responseFields[$fieldName]['title'];
939 if (in_array($this->_outputMode
, array(
943 $title = 'Q' . $fildCnt++
;
946 $fldType = 'CRM_Utils_Type::T_STRING';
947 if ($response->time_format
) {
948 $fldType = CRM_Utils_Type
::T_TIMESTAMP
;
951 'name' => $response->column_name
,
954 'label' => $responseFields[$fieldName]['title'],
955 'dataType' => $response->data_type
,
956 'htmlType' => $response->html_type
,
958 'alias' => ($response->data_type
== 'ContactReference') ?
$this->_columns
[$resTable]['alias'] .
959 '_contact' : $this->_columns
[$resTable]['alias'],
960 'dbAlias' => $this->_columns
[$resTable]['alias'] . '.' .
961 $response->column_name
,
962 'no_display' => TRUE,
963 'isSurveyResponseField' => TRUE,
966 $this->_columns
[$resTable]['fields'][$fieldName] = $field;
967 $this->_aliases
[$resTable] = $this->_columns
[$resTable]['alias'];