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
19 * This class generates form components for processing a survey.
21 class CRM_Campaign_Form_Survey_Results
extends CRM_Campaign_Form_Survey
{
25 protected $_reportTitle;
36 const NUM_OPTION
= 11;
38 public function preProcess() {
41 $this->_values
= $this->get('values');
42 if (!is_array($this->_values
)) {
44 if ($this->_surveyId
) {
45 $params = ['id' => $this->_surveyId
];
46 CRM_Campaign_BAO_Survey
::retrieve($params, $this->_values
);
48 $this->set('values', $this->_values
);
51 $query = "SELECT MAX(id) as id, title FROM civicrm_report_instance WHERE name = %1 GROUP BY id";
52 $params = [1 => ["survey_{$this->_surveyId}", 'String']];
53 $result = CRM_Core_DAO
::executeQuery($query, $params);
54 if ($result->fetch()) {
55 $this->_reportId
= $result->id
;
56 $this->_reportTitle
= $result->title
;
61 * Set default values for the form.
63 * Note that in edit/view mode the default values are retrieved from the database.
66 * array of default values
68 public function setDefaultValues() {
69 $defaults = $this->_values
;
71 // set defaults for weight.
72 for ($i = 1; $i <= self
::NUM_OPTION
; $i++
) {
73 $defaults["option_weight[{$i}]"] = $i;
76 $defaults['create_report'] = 1;
77 if ($this->_reportId
) {
78 $defaults['report_title'] = $this->_reportTitle
;
84 * Build the form object.
86 public function buildQuickForm() {
87 $optionGroups = CRM_Campaign_BAO_Survey
::getResultSets();
89 if (empty($optionGroups)) {
90 $optionTypes = ['1' => ts('Create new result set')];
94 '1' => ts('Create new result set'),
95 '2' => ts('Use existing result set'),
99 ts('Select Result Set'),
101 '' => ts('- select -'),
102 ] +
$optionGroups, FALSE,
103 ['onChange' => 'loadOptionGroup( )']
107 $element = &$this->addRadio('option_type',
108 ts('Survey Responses'),
111 'onclick' => "showOptionSelect();",
115 if (empty($optionGroups) ||
empty($this->_values
['result_id'])) {
116 $this->setdefaults(['option_type' => 1]);
118 elseif (!empty($this->_values
['result_id'])) {
121 'option_group_id' => $this->_values
['result_id'],
125 // form fields of Custom Option rows
127 $_showHide = new CRM_Core_ShowHideBlocks('', '');
129 $optionAttributes = CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue');
130 $optionAttributes['label']['size'] = $optionAttributes['value']['size'] = 25;
132 for ($i = 1; $i <= self
::NUM_OPTION
; $i++
) {
133 //the show hide blocks
134 $showBlocks = 'optionField_' . $i;
136 $_showHide->addHide($showBlocks);
137 if ($i == self
::NUM_OPTION
) {
138 $_showHide->addHide('additionalOption');
142 $_showHide->addShow($showBlocks);
145 $this->add('text', 'option_label[' . $i . ']', ts('Label'),
146 $optionAttributes['label']
150 $this->add('text', 'option_value[' . $i . ']', ts('Value'),
151 $optionAttributes['value']
155 $this->add('number', "option_weight[$i]", ts('Order'),
156 $optionAttributes['weight']
159 $this->add('text', 'option_interval[' . $i .
160 ']', ts('Recontact Interval'),
161 CRM_Core_DAO
::getAttribute('CRM_Campaign_DAO_Survey', 'release_frequency')
164 $defaultOption[$i] = $this->createElement('radio', NULL, NULL, NULL, $i);
167 //default option selection
168 $this->addGroup($defaultOption, 'default_option');
170 $_showHide->addToTemplate();
172 $this->addElement('checkbox', 'create_report', ts('Create Report'));
173 $this->addElement('text', 'report_title', ts('Report Title'));
175 if ($this->_reportId
) {
176 $this->freeze('create_report');
177 $this->freeze('report_title');
181 'CRM_Campaign_Form_Survey_Results',
185 parent
::buildQuickForm();
189 * Global validation rules for the form.
197 public static function formRule($fields, $files, $form) {
199 if (!empty($fields['option_label']) && !empty($fields['option_value']) &&
200 (count(array_filter($fields['option_label'])) == 0) &&
201 (count(array_filter($fields['option_value'])) == 0)
203 $errors['option_label[1]'] = ts('Enter at least one result option.');
206 elseif (empty($fields['option_label']) && empty($fields['option_value'])) {
211 $fields['option_type'] == 2 && empty($fields['option_group_id'])
213 $errors['option_group_id'] = ts("Please select a Survey Result Set.");
217 $_flagOption = $_rowError = 0;
218 $_showHide = new CRM_Core_ShowHideBlocks('', '');
220 //capture duplicate Custom option values
221 if (!empty($fields['option_value'])) {
222 $countValue = count($fields['option_value']);
223 $uniqueCount = count(array_unique($fields['option_value']));
225 if ($countValue > $uniqueCount) {
227 while ($start < self
::NUM_OPTION
) {
228 $nextIndex = $start +
1;
230 while ($nextIndex <= self
::NUM_OPTION
) {
231 if ($fields['option_value'][$start] ==
232 $fields['option_value'][$nextIndex] &&
233 !empty($fields['option_value'][$nextIndex])
236 $errors['option_value[' . $start .
237 ']'] = ts('Duplicate Option values');
238 $errors['option_value[' . $nextIndex .
239 ']'] = ts('Duplicate Option values');
249 //capture duplicate Custom Option label
250 if (!empty($fields['option_label'])) {
251 $countValue = count($fields['option_label']);
252 $uniqueCount = count(array_unique($fields['option_label']));
254 if ($countValue > $uniqueCount) {
256 while ($start < self
::NUM_OPTION
) {
257 $nextIndex = $start +
1;
259 while ($nextIndex <= self
::NUM_OPTION
) {
260 if ($fields['option_label'][$start] ==
261 $fields['option_label'][$nextIndex] &&
262 !empty($fields['option_label'][$nextIndex])
264 $errors['option_label[' . $start .
265 ']'] = ts('Duplicate Option label');
266 $errors['option_label[' . $nextIndex .
267 ']'] = ts('Duplicate Option label');
277 for ($i = 1; $i <= self
::NUM_OPTION
; $i++
) {
278 if (!$fields['option_label'][$i]) {
279 if ($fields['option_value'][$i]) {
280 $errors['option_label[' . $i .
281 ']'] = ts('Option label cannot be empty');
288 elseif (!strlen(trim($fields['option_value'][$i]))) {
289 if (!$fields['option_value'][$i]) {
290 $errors['option_value[' . $i .
291 ']'] = ts('Option value cannot be empty');
296 if (!empty($fields['option_interval'][$i]) &&
297 !CRM_Utils_Rule
::integer($fields['option_interval'][$i])
300 $errors['option_interval[' . $i .
301 ']'] = ts('Please enter a valid integer.');
304 $showBlocks = 'optionField_' . $i;
306 $_showHide->addShow($showBlocks);
310 if (!empty($_emptyRow)) {
311 $_showHide->addHide($showBlocks);
314 $_showHide->addShow($showBlocks);
317 if ($i == self
::NUM_OPTION
) {
318 $hideBlock = 'additionalOption';
319 $_showHide->addHide($hideBlock);
322 $_flagOption = $_emptyRow = 0;
324 $_showHide->addToTemplate();
326 return empty($errors) ?
TRUE : $errors;
332 public function postProcess() {
333 // store the submitted values in an array
335 $params = $this->controller
->exportValues($this->_name
);
336 $params['id'] = $this->_surveyId
;
338 $updateResultSet = FALSE;
339 $resultSetOptGrpId = NULL;
340 if ((CRM_Utils_Array
::value('option_type', $params) == 2) &&
341 !empty($params['option_group_id'])
343 $updateResultSet = TRUE;
344 $resultSetOptGrpId = $params['option_group_id'];
347 $recontactInterval = [];
348 if ($updateResultSet) {
349 $optionValue = new CRM_Core_DAO_OptionValue();
350 $optionValue->option_group_id
= $resultSetOptGrpId;
351 $optionValue->delete();
353 $params['result_id'] = $resultSetOptGrpId;
356 $opGroupName = 'civicrm_survey_' . rand(10, 1000) . '_' . date('YmdHis');
358 $optionGroup = new CRM_Core_DAO_OptionGroup();
359 $optionGroup->name
= $opGroupName;
360 $optionGroup->title
= $this->_values
['title'] . ' Result Set';
361 $optionGroup->is_active
= 1;
362 $optionGroup->save();
364 $params['result_id'] = $optionGroup->id
;
367 foreach ($params['option_value'] as $k => $v) {
368 if (strlen(trim($v))) {
369 $optionValue = new CRM_Core_DAO_OptionValue();
370 $optionValue->option_group_id
= $params['result_id'];
371 $optionValue->label
= $params['option_label'][$k];
372 $optionValue->name
= CRM_Utils_String
::titleToVar($params['option_label'][$k]);
373 $optionValue->value
= trim($v);
374 $optionValue->weight
= $params['option_weight'][$k];
375 $optionValue->is_active
= 1;
377 if (!empty($params['default_option']) &&
378 $params['default_option'] == $k
380 $optionValue->is_default
= 1;
383 $optionValue->save();
385 // using is_numeric since 0 is a valid value for option_interval
386 if (is_numeric($params['option_interval'][$k])) {
387 $recontactInterval[$optionValue->label
] = $params['option_interval'][$k];
392 $params['recontact_interval'] = serialize($recontactInterval);
393 $survey = CRM_Campaign_BAO_Survey
::create($params);
395 // create report if required.
396 if (!$this->_reportId
&& $survey->id
&& !empty($params['create_report'])) {
397 $activityStatus = CRM_Core_PseudoConstant
::activityStatus('name');
398 $activityStatus = array_flip($activityStatus);
400 'name' => "survey_{$survey->id}",
401 'title' => $params['report_title'] ?
$params['report_title'] : $this->_values
['title'],
402 'status_id_op' => 'eq',
404 'status_id_value' => $activityStatus['Scheduled'],
405 'survey_id_value' => [$survey->id
],
406 'description' => ts('Detailed report for canvassing, phone-banking, walk lists or other surveys.'),
408 //Default value of order by
409 $this->_params
['order_bys'] = [
411 'column' => 'sort_name',
415 // for WalkList or default
425 if (CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'WalkList') ==
426 $this->_values
['activity_type_id']
428 $this->_params
['order_bys'] = [
430 'column' => 'street_name',
434 'column' => 'street_number_odd_even',
438 'column' => 'street_number',
442 'column' => 'sort_name',
447 elseif (CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'PhoneBank') ==
448 $this->_values
['activity_type_id']
450 array_push($displayFields, 'phone');
452 elseif ((CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Survey') ==
453 $this->_values
['activity_type_id']) ||
454 (CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Canvass') ==
455 $this->_values
['activity_type_id'])
457 array_push($displayFields, 'phone', 'city', 'state_province_id', 'postal_code', 'email');
459 foreach ($displayFields as $key) {
460 $this->_params
['fields'][$key] = 1;
462 $this->_createNew
= TRUE;
463 $this->_id
= CRM_Report_Utils_Report
::getInstanceIDForValue('survey/detail');
464 CRM_Report_Form_Instance
::setDefaultValues($this, $this->_defaults
);
465 $this->_params
= array_merge($this->_params
, $this->_defaults
);
466 CRM_Report_Form_Instance
::postProcess($this, FALSE);
468 $query = "SELECT MAX(id) FROM civicrm_report_instance WHERE name = %1";
469 $reportID = CRM_Core_DAO
::singleValueQuery($query, [
471 "survey_{$survey->id}",
476 $url = CRM_Utils_System
::url("civicrm/report/instance/{$reportID}", 'reset=1');
477 $status = ts("A Survey Detail Report <a href='%1'>%2</a> has been created.",
478 [1 => $url, 2 => $this->_params
['title']]);
483 // reset status as we don't want status set by Instance::postProcess
484 $session = CRM_Core_Session
::singleton();
485 $session->getStatus(TRUE);
487 CRM_Core_Session
::setStatus($status, ts('Saved'), 'success');
490 parent
::endPostProcess();