3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Report_Form_Campaign_SurveyDetails
extends CRM_Report_Form
{
19 protected $_emailField = FALSE;
21 protected $_phoneField = FALSE;
23 protected $_locationBasedPhoneField = FALSE;
25 protected $_summary = NULL;
26 protected $_customGroupGroupBy = FALSE;
27 protected $_customGroupExtends = array(
34 public $_drilldownReport = array('contact/detail' => 'Link to Detail Report');
36 private static $_surveyRespondentStatus;
38 // Survey Question titles are overridden when in print or pdf mode to
40 * say Q1, Q2 instead of the full title - to save space.
43 private $_columnTitleOverrides = [];
50 public function __construct() {
51 //filter options for survey activity status.
52 $responseStatus = array('' => ts('- Any -'));
53 self
::$_surveyRespondentStatus = [];
54 $activityStatus = CRM_Core_PseudoConstant
::activityStatus('name');
55 if ($statusId = array_search('Scheduled', $activityStatus)) {
56 $responseStatus[$statusId] = ts('Reserved');
57 self
::$_surveyRespondentStatus[$statusId] = 'Reserved';
59 if ($statusId = array_search('Completed', $activityStatus)) {
60 $responseStatus[$statusId] = ts('Interviewed');
61 self
::$_surveyRespondentStatus[$statusId] = 'Interviewed';
64 $optionGroups = CRM_Campaign_BAO_Survey
::getResultSets('name');
66 foreach ($optionGroups as $gid => $name) {
69 $value = CRM_Core_OptionGroup
::values($name);
71 $value = array_combine($value, $value);
73 $resultOptions = $resultOptions +
$value;
76 asort($resultOptions);
78 //get all interviewers.
79 $allSurveyInterviewers = CRM_Campaign_BAO_Survey
::getInterviewers();
81 $this->_columns
= array(
82 'civicrm_activity_contact' => array(
83 'dao' => 'CRM_Activity_DAO_ActivityContact',
84 'fields' => array('contact_id' => array('title' => ts('Interviewer Name'))),
86 'contact_id' => array(
87 'name' => 'contact_id',
88 'title' => ts('Interviewer Name'),
89 'type' => CRM_Utils_Type
::T_INT
,
90 'operatorType' => CRM_Report_Form
::OP_SELECT
,
92 '' => ts('- any interviewer -'),
93 ) +
$allSurveyInterviewers,
96 'grouping' => 'survey-interviewer-fields',
98 'civicrm_contact' => array(
99 'dao' => 'CRM_Contact_DAO_Contact',
102 'title' => ts('Contact ID'),
103 'no_display' => TRUE,
106 'sort_name' => array(
107 'title' => ts('Respondent Name'),
113 'sort_name' => array(
114 'title' => ts('Respondent Name'),
115 'operator' => 'like',
118 'grouping' => 'contact-fields',
119 'order_bys' => array(
120 'sort_name' => array(
121 'title' => ts('Respondent Name'),
126 'civicrm_phone' => array(
127 'dao' => 'CRM_Core_DAO_Phone',
131 'title' => ts('Phone'),
134 'grouping' => 'location-fields',
136 'civicrm_email' => array(
137 'dao' => 'CRM_Core_DAO_Email',
141 'title' => ts('Email'),
144 'grouping' => 'location-fields',
146 ) +
$this->getAddressColumns() +
148 'civicrm_activity' => array(
149 'dao' => 'CRM_Activity_DAO_Activity',
150 'alias' => 'survey_activity',
152 'survey_id' => array(
153 'name' => 'source_record_id',
154 'title' => ts('Survey'),
155 'type' => CRM_Utils_Type
::T_INT
,
156 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
157 'options' => CRM_Campaign_BAO_Survey
::getSurveys(),
159 'survey_response' => array(
160 'name' => 'survey_response',
161 'title' => ts('Survey Responses'),
165 'title' => ts('Note'),
171 'title' => ts('Survey Result'),
173 'activity_date_time' => array(
174 'name' => 'activity_date_time',
175 'title' => ts('Date'),
176 'type' => CRM_Utils_Type
::T_DATE + CRM_Utils_Type
::T_TIME
,
180 'survey_id' => array(
181 'name' => 'source_record_id',
182 'title' => ts('Survey'),
183 'type' => CRM_Utils_Type
::T_INT
,
184 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
185 'options' => CRM_Campaign_BAO_Survey
::getSurveys(),
187 'status_id' => array(
188 'name' => 'status_id',
189 'title' => ts('Respondent Status'),
190 'type' => CRM_Utils_Type
::T_INT
,
191 'operatorType' => CRM_Report_Form
::OP_SELECT
,
192 'options' => $responseStatus,
195 'title' => ts('Survey Result'),
196 'type' => CRM_Utils_Type
::T_STRING
,
197 'operatorType' => CRM_Report_Form
::OP_MULTISELECT
,
198 'options' => $resultOptions,
200 'activity_date_time' => array(
201 'title' => ts('Date'),
202 'type' => CRM_Utils_Type
::T_DATE + CRM_Utils_Type
::T_TIME
,
203 'operatorType' => CRM_Report_Form
::OP_DATE
,
206 'grouping' => 'survey-activity-fields',
207 'order_bys' => array(
208 'activity_date_time' => array(
209 'title' => ts('Date'),
214 parent
::__construct();
217 public function preProcess() {
218 parent
::preProcess();
221 public function select() {
224 //add the survey response fields.
225 $this->_addSurveyResponseColumns();
227 $this->_columnHeaders
= [];
228 foreach ($this->_columns
as $tableName => $table) {
229 if (!isset($table['fields'])) {
232 foreach ($table['fields'] as $fieldName => $field) {
233 if (!empty($field['required']) ||
234 !empty($this->_params
['fields'][$fieldName]) ||
235 !empty($field['is_required'])
238 $fieldsName = CRM_Utils_Array
::value(1, explode('_', $tableName));
240 $this->{"_$fieldsName" . 'Field'} = TRUE;
243 //need to pickup custom data/survey response fields.
244 if ($fieldName == 'survey_response') {
248 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
251 $title = $field['title'] ??
NULL;
252 // Check for an override.
253 if (!empty($this->_columnTitleOverrides
["{$tableName}_{$fieldName}"])) {
254 $title = $this->_columnTitleOverrides
["{$tableName}_{$fieldName}"];
256 $this->_columnHeaders
["{$tableName}_{$fieldName}"]['title'] = $title;
258 $this->_selectAliases
[] = "{$tableName}_{$fieldName}";
263 $this->_select
= "SELECT " . implode(",\n", $select) . " ";
266 public function from() {
267 $this->_from
= " FROM civicrm_contact {$this->_aliases['civicrm_contact']} {$this->_aclFrom} ";
268 $activityContacts = CRM_Activity_BAO_ActivityContact
::buildOptions('record_type_id', 'validate');
269 $assigneeID = CRM_Utils_Array
::key('Activity Assignees', $activityContacts);
270 $targetID = CRM_Utils_Array
::key('Activity Targets', $activityContacts);
272 //get the activity table joins.
273 $this->_from
.= " INNER JOIN civicrm_activity_contact civicrm_activity_target ON
274 ( {$this->_aliases['civicrm_contact']}.id = civicrm_activity_target.contact_id AND civicrm_activity_target.record_type_id = {$targetID}) \n";
275 $this->_from
.= " INNER JOIN civicrm_activity {$this->_aliases['civicrm_activity']} ON
276 ( {$this->_aliases['civicrm_activity']}.id = civicrm_activity_target.activity_id )\n";
277 $this->_from
.= " INNER JOIN civicrm_activity_contact activity_contact_civireport ON
278 ( {$this->_aliases['civicrm_activity']}.id = activity_contact_civireport.activity_id AND activity_contact_civireport.record_type_id = {$assigneeID} )\n";
280 $this->joinAddressFromContact();
281 $this->joinPhoneFromContact();
282 $this->joinEmailFromContact();
284 if ($this->_locationBasedPhoneField
) {
285 foreach ($this->_surveyResponseFields
as $key => $value) {
286 if (substr($key, 0, 5) == 'phone' && !empty($value['location_type_id'])
288 $fName = str_replace('-', '_', $key);
289 $this->_from
.= "LEFT JOIN civicrm_phone " .
290 $this->_aliases
["civicrm_phone_{$fName}"] .
291 " ON {$this->_aliases['civicrm_contact']}.id = " .
292 $this->_aliases
["civicrm_phone_{$fName}"] . ".contact_id AND " .
293 $this->_aliases
["civicrm_phone_{$fName}"] .
294 ".location_type_id = {$value['location_type_id']} AND " .
295 $this->_aliases
["civicrm_phone_{$fName}"] .
296 ".phone_type_id = {$value['phone_type_id']}\n";
302 public function where() {
304 foreach ($this->_columns
as $tableName => $table) {
305 if (array_key_exists('filters', $table)) {
306 foreach ($table['filters'] as $fieldName => $field) {
309 if (CRM_Utils_Array
::value('type', $field) & CRM_Utils_Type
::T_DATE
) {
310 $relative = $this->_params
["{$fieldName}_relative"] ??
NULL;
311 $from = $this->_params
["{$fieldName}_from"] ??
NULL;
312 $to = $this->_params
["{$fieldName}_to"] ??
NULL;
314 $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']);
317 $op = $this->_params
["{$fieldName}_op"] ??
NULL;
319 $clause = $this->whereClause($field,
321 CRM_Utils_Array
::value("{$fieldName}_value", $this->_params
),
322 CRM_Utils_Array
::value("{$fieldName}_min", $this->_params
),
323 CRM_Utils_Array
::value("{$fieldName}_max", $this->_params
)
328 if (!empty($clause)) {
329 $clauses[] = $clause;
335 //apply survey activity types filter.
336 $surveyActivityTypes = CRM_Campaign_BAO_Survey
::getSurveyActivityType();
337 if (!empty($surveyActivityTypes)) {
338 $clauses[] = "( {$this->_aliases['civicrm_activity']}.activity_type_id IN ( " .
339 implode(' , ', array_keys($surveyActivityTypes)) . ' ) )';
342 // always filter out deleted activities (so contacts that have been released
343 // don't show up in the report).
344 $clauses[] = "( {$this->_aliases['civicrm_activity']}.is_deleted = 0 )";
346 if (empty($clauses)) {
347 $this->_where
= "WHERE ( 1 ) ";
350 $this->_where
= "WHERE " . implode(' AND ', $clauses);
353 if ($this->_aclWhere
) {
354 $this->_where
.= " AND {$this->_aclWhere} ";
358 public function compileContent() {
359 $coverSheet = $this->_surveyCoverSheet() .
360 "<div style=\"page-break-after: always\"></div>";
361 $templateFile = $this->getHookedTemplateFileName();
363 CRM_Core_Form
::$_template->fetch($templateFile) .
364 CRM_Utils_Array
::value('report_footer', $this->_formValues
);
368 * @return bool|mixed|null|string
370 private function _surveyCoverSheet() {
372 $surveyIds = $this->_params
['survey_id_value'] ??
NULL;
373 if (CRM_Utils_System
::isNull($surveyIds)) {
379 $surveyResponseFields = [];
380 foreach ($this->_columns
as $tableName => $values) {
381 if (!is_array($values['fields'])) {
384 foreach ($values['fields'] as $name => $field) {
385 if (!empty($field['isSurveyResponseField'])) {
386 $fldId = substr($name, 7);
387 $fieldIds[$fldId] = $fldId;
388 $title = CRM_Utils_Array
::value('label', $field, $field['title']);
389 $surveyResponseFields[$name] = array(
392 'name' => "{$tableName}_{$name}",
398 //now pickup all options.
399 if (!empty($fieldIds)) {
401 SELECT field.id as id,
404 FROM civicrm_custom_field field
405 INNER JOIN civicrm_option_value val ON ( val.option_group_id = field.option_group_id )
406 WHERE field.id IN (' . implode(' , ', $fieldIds) . ' )
407 Order By val.weight';
408 $field = CRM_Core_DAO
::executeQuery($query);
410 while ($field->fetch()) {
411 $name = "custom_{$field->id}";
412 $surveyResponseFields[$name]['options'][$field->value
] = $field->label
;
416 //get the result values.
418 SELECT survey.id as id,
419 survey.title as title,
422 FROM civicrm_survey survey
423 INNER JOIN civicrm_option_value val ON ( val.option_group_id = survey.result_id )
424 WHERE survey.id IN ( ' . implode(' , ', array_values($surveyIds)) . ' )
425 Order By val.weight';
426 $resultSet = CRM_Core_DAO
::executeQuery($query);
427 $surveyResultFields = [];
428 while ($resultSet->fetch()) {
429 $surveyResultFields[$resultSet->id
]['title'] = $resultSet->title
;
430 $surveyResultFields[$resultSet->id
]['options'][$resultSet->value
] = $resultSet->label
;
433 $this->assign('surveyResultFields', $surveyResultFields);
434 $this->assign('surveyResponseFields', $surveyResponseFields);
436 $templateFile = 'CRM/Report/Form/Campaign/SurveyCoverSheet.tpl';
437 $coverSheet = CRM_Core_Form
::$_template->fetch($templateFile);
443 * Alter display of rows.
445 * Iterate through the rows retrieved via SQL and make changes for display purposes,
446 * such as rendering contacts as links.
449 * Rows generated by SQL, with an array for each row.
451 public function alterDisplay(&$rows) {
453 $this->_formatSurveyResult($rows);
454 $this->_formatSurveyResponseData($rows);
457 foreach ($rows as $rowNum => $row) {
458 // convert display name to links
459 if (array_key_exists('civicrm_contact_sort_name', $row) &&
460 array_key_exists('civicrm_contact_id', $row)
462 $url = CRM_Report_Utils_Report
::getNextUrl('contact/detail',
463 'reset=1&force=1&id_op=eq&id_value=' .
464 $row['civicrm_contact_id'],
465 $this->_absoluteUrl
, $this->_id
, $this->_drilldownReport
467 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
471 if (array_key_exists('civicrm_activity_contact_contact_id', $row)) {
472 $rows[$rowNum]['civicrm_activity_contact_contact_id'] = CRM_Utils_Array
::value($row['civicrm_activity_contact_contact_id'],
473 CRM_Campaign_BAO_Survey
::getInterviewers()
478 if (array_key_exists('civicrm_activity_survey_id', $row)) {
479 $rows[$rowNum]['civicrm_activity_survey_id'] = CRM_Utils_Array
::value($row['civicrm_activity_survey_id'],
480 CRM_Campaign_BAO_Survey
::getSurveys()
485 $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, NULL, NULL) ?
TRUE : $entryFound;
487 // skip looking further in rows, if first row itself doesn't
488 // have the column we need
498 private function _formatSurveyResult(&$rows) {
499 $surveyIds = $this->_params
['survey_id_value'] ??
NULL;
500 if (CRM_Utils_System
::isNull($surveyIds) ||
501 empty($this->_params
['fields']['result']) ||
502 !in_array($this->_outputMode
, array('print', 'pdf'))
507 //swap the survey result label w/ value.
509 SELECT survey.id as id,
512 FROM civicrm_option_value val
513 INNER JOIN civicrm_option_group grp ON ( grp.id = val.option_group_id )
514 INNER JOIN civicrm_survey survey ON ( survey.result_id = grp.id )
515 WHERE survey.id IN (' . implode(' , ', array_values($surveyIds)) . ' )
516 Order By val.weight';
518 $result = CRM_Core_DAO
::executeQuery($query);
520 while ($result->fetch()) {
521 $resultSet[$result->id
][$result->value
] = $result->label
;
524 $statusId = $this->_params
['status_id_value'] ??
NULL;
525 $respondentStatus = self
::$_surveyRespondentStatus[$statusId] ??
NULL;
527 $surveyId = $surveyIds[0] ??
NULL;
528 foreach ($rows as & $row) {
529 if (!empty($row['civicrm_activity_survey_id'])) {
530 $surveyId = $row['civicrm_activity_survey_id'];
532 $result = CRM_Utils_Array
::value($surveyId, $resultSet, []);
533 $resultLabel = $row['civicrm_activity_result'] ??
NULL;
534 if ($respondentStatus == 'Reserved') {
535 $row['civicrm_activity_result'] = implode(' | ', array_keys($result));
537 elseif ($resultLabel) {
538 $resultValue = array_search($resultLabel, $result);
540 $row['civicrm_activity_result'] = $resultValue;
549 private function _formatSurveyResponseData(&$rows) {
550 $surveyIds = $this->_params
['survey_id_value'] ??
NULL;
551 if (CRM_Utils_System
::isNull($surveyIds) ||
552 empty($this->_params
['fields']['survey_response'])
557 $surveyResponseFields = [];
558 $surveyResponseFieldIds = [];
559 foreach ($this->_columns
as $tableName => $values) {
560 if (!is_array($values['fields'])) {
563 foreach ($values['fields'] as $name => $field) {
564 if (!empty($field['isSurveyResponseField'])) {
565 $fldId = substr($name, 7);
566 $surveyResponseFields[$name] = "{$tableName}_{$name}";
567 $surveyResponseFieldIds[$fldId] = $fldId;
572 if (empty($surveyResponseFieldIds)) {
576 $hasResponseData = FALSE;
577 foreach ($surveyResponseFields as $fldName) {
578 foreach ($rows as $row) {
579 if (!empty($row[$fldName])) {
580 $hasResponseData = TRUE;
586 //do check respondent status.
587 $statusId = $this->_params
['status_id_value'] ??
NULL;
588 $respondentStatus = self
::$_surveyRespondentStatus[$statusId] ??
NULL;
590 if (!$hasResponseData &&
591 ($respondentStatus != 'Reserved')
596 //start response data formatting.
605 FROM civicrm_custom_field cf
606 INNER JOIN civicrm_custom_group cg ON ( cg.id = cf.custom_group_id )
607 LEFT JOIN civicrm_option_value ov ON ( cf.option_group_id = ov.option_group_id )
608 WHERE cf.id IN ( ' . implode(' , ', $surveyResponseFieldIds) . ' )
611 $responseFields = [];
621 $responseField = CRM_Core_DAO
::executeQuery($query);
622 while ($responseField->fetch()) {
623 $reponseFldName = $responseField->table_name
. '_custom_' .
625 foreach ($properties as $prop) {
626 $responseFields[$reponseFldName][$prop] = $responseField->$prop;
628 if ($responseField->option_group_id
) {
629 //show value for print and pdf.
630 $value = $responseField->label
;
631 if (in_array($this->_outputMode
, array(
635 $value = $responseField->value
;
637 $fieldValueMap[$responseField->option_group_id
][$responseField->value
] = $value;
641 //actual data formatting.
643 foreach ($rows as & $row) {
644 if (!is_array($row)) {
648 foreach ($row as $name => & $value) {
649 if (!array_key_exists($name, $responseFields)) {
653 if ($respondentStatus == 'Reserved' &&
654 in_array($this->_outputMode
, array('print', 'pdf'))
656 $optGrpId = $responseFields[$name]['option_group_id'] ??
NULL;
657 $options = CRM_Utils_Array
::value($optGrpId, $fieldValueMap, []);
658 $value = implode(' | ', array_keys($options));
661 $value = CRM_Core_BAO_CustomField
::displayValue($value, $responseFields[$name]['id']);
671 private function _addSurveyResponseColumns() {
672 $surveyIds = $this->_params
['survey_id_value'] ??
NULL;
673 if (CRM_Utils_System
::isNull($surveyIds) ||
674 empty($this->_params
['fields']['survey_response'])
679 $responseFields = [];
680 foreach ($surveyIds as $surveyId) {
681 $responseFields +
= CRM_Campaign_BAO_Survey
::getSurveyResponseFields($surveyId);
682 $this->_surveyResponseFields
= $responseFields;
684 foreach ($responseFields as $key => $value) {
685 if (substr($key, 0, 5) == 'phone' && !empty($value['location_type_id'])) {
686 $fName = str_replace('-', '_', $key);
687 $this->_columns
["civicrm_{$fName}"] = array(
688 'dao' => 'CRM_Core_DAO_Phone',
689 'alias' => "phone_civireport_{$fName}",
691 $fName => array_merge($value, array(
692 'is_required' => '1',
693 'alias' => "phone_civireport_{$fName}",
694 'dbAlias' => "phone_civireport_{$fName}.phone",
695 'no_display' => TRUE,
699 $this->_aliases
["civicrm_phone_{$fName}"] = $this->_columns
["civicrm_{$fName}"]['alias'];
700 $this->_locationBasedPhoneField
= TRUE;
703 $responseFieldIds = [];
704 foreach (array_keys($responseFields) as $key) {
705 $cfId = CRM_Core_BAO_CustomField
::getKeyID($key);
707 $responseFieldIds[$cfId] = $cfId;
710 if (empty($responseFieldIds)) {
723 FROM civicrm_custom_group cg
724 INNER JOIN civicrm_custom_field cf ON ( cg.id = cf.custom_group_id )
725 WHERE cf.id IN ( ' . implode(' , ', $responseFieldIds) .
726 ' ) ORDER BY cf.weight';
727 $response = CRM_Core_DAO
::executeQuery($query);
729 while ($response->fetch()) {
730 $resTable = $response->table_name
;
731 $fieldName = "custom_{$response->cfId}";
733 //need to check does these custom data already included.
735 if (!array_key_exists($resTable, $this->_columns
)) {
736 $this->_columns
[$resTable]['dao'] = 'CRM_Contact_DAO_Contact';
737 $this->_columns
[$resTable]['extends'] = $response->extends;
739 if (empty($this->_columns
[$resTable]['alias'])) {
740 $this->_columns
[$resTable]['alias'] = "{$resTable}_survey_response";
742 if (empty($this->_columns
[$resTable]['fields'])) {
743 $this->_columns
[$resTable]['fields'] = [];
746 if (in_array($this->_outputMode
, ['print', 'pdf'])) {
747 $this->_columnTitleOverrides
["{$resTable}_{$fieldName}"] = 'Q' . $fieldCnt;
751 if (array_key_exists($fieldName, $this->_columns
[$resTable]['fields'])) {
752 $this->_columns
[$resTable]['fields'][$fieldName]['required'] = TRUE;
753 $this->_columns
[$resTable]['fields'][$fieldName]['isSurveyResponseField'] = TRUE;
757 $title = $responseFields[$fieldName]['title'];
758 $fldType = 'CRM_Utils_Type::T_STRING';
759 if ($response->time_format
) {
760 $fldType = CRM_Utils_Type
::T_TIMESTAMP
;
763 'name' => $response->column_name
,
766 'label' => $responseFields[$fieldName]['title'],
767 'dataType' => $response->data_type
,
768 'htmlType' => $response->html_type
,
770 'alias' => ($response->data_type
== 'ContactReference') ?
$this->_columns
[$resTable]['alias'] .
771 '_contact' : $this->_columns
[$resTable]['alias'],
772 'dbAlias' => $this->_columns
[$resTable]['alias'] . '.' .
773 $response->column_name
,
774 'no_display' => TRUE,
775 'isSurveyResponseField' => TRUE,
778 $this->_columns
[$resTable]['fields'][$fieldName] = $field;
779 $this->_aliases
[$resTable] = $this->_columns
[$resTable]['alias'];