3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
35 * This class generates form components for processing a survey.
37 class CRM_Campaign_Form_Survey_Results
extends CRM_Campaign_Form_Survey
{
41 protected $_reportTitle;
52 const NUM_OPTION
= 11;
54 public function preProcess() {
57 $this->_values
= $this->get('values');
58 if (!is_array($this->_values
)) {
60 if ($this->_surveyId
) {
61 $params = ['id' => $this->_surveyId
];
62 CRM_Campaign_BAO_Survey
::retrieve($params, $this->_values
);
64 $this->set('values', $this->_values
);
67 $query = "SELECT MAX(id) as id, title FROM civicrm_report_instance WHERE name = %1 GROUP BY id";
68 $params = [1 => ["survey_{$this->_surveyId}", 'String']];
69 $result = CRM_Core_DAO
::executeQuery($query, $params);
70 if ($result->fetch()) {
71 $this->_reportId
= $result->id
;
72 $this->_reportTitle
= $result->title
;
77 * Set default values for the form.
79 * Note that in edit/view mode the default values are retrieved from the database.
82 * array of default values
84 public function setDefaultValues() {
85 $defaults = $this->_values
;
87 // set defaults for weight.
88 for ($i = 1; $i <= self
::NUM_OPTION
; $i++
) {
89 $defaults["option_weight[{$i}]"] = $i;
92 $defaults['create_report'] = 1;
93 if ($this->_reportId
) {
94 $defaults['report_title'] = $this->_reportTitle
;
100 * Build the form object.
102 public function buildQuickForm() {
103 $optionGroups = CRM_Campaign_BAO_Survey
::getResultSets();
105 if (empty($optionGroups)) {
106 $optionTypes = ['1' => ts('Create new result set')];
110 '1' => ts('Create new result set'),
111 '2' => ts('Use existing result set'),
115 ts('Select Result Set'),
117 '' => ts('- select -'),
118 ] +
$optionGroups, FALSE,
119 ['onChange' => 'loadOptionGroup( )']
123 $element = &$this->addRadio('option_type',
124 ts('Survey Responses'),
127 'onclick' => "showOptionSelect();",
131 if (empty($optionGroups) ||
empty($this->_values
['result_id'])) {
132 $this->setdefaults(['option_type' => 1]);
134 elseif (!empty($this->_values
['result_id'])) {
137 'option_group_id' => $this->_values
['result_id'],
141 // form fields of Custom Option rows
143 $_showHide = new CRM_Core_ShowHideBlocks('', '');
145 $optionAttributes = CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue');
146 $optionAttributes['label']['size'] = $optionAttributes['value']['size'] = 25;
148 for ($i = 1; $i <= self
::NUM_OPTION
; $i++
) {
149 //the show hide blocks
150 $showBlocks = 'optionField_' . $i;
152 $_showHide->addHide($showBlocks);
153 if ($i == self
::NUM_OPTION
) {
154 $_showHide->addHide('additionalOption');
158 $_showHide->addShow($showBlocks);
161 $this->add('text', 'option_label[' . $i . ']', ts('Label'),
162 $optionAttributes['label']
166 $this->add('text', 'option_value[' . $i . ']', ts('Value'),
167 $optionAttributes['value']
171 $this->add('number', "option_weight[$i]", ts('Order'),
172 $optionAttributes['weight']
175 $this->add('text', 'option_interval[' . $i .
176 ']', ts('Recontact Interval'),
177 CRM_Core_DAO
::getAttribute('CRM_Campaign_DAO_Survey', 'release_frequency')
180 $defaultOption[$i] = $this->createElement('radio', NULL, NULL, NULL, $i);
183 //default option selection
184 $this->addGroup($defaultOption, 'default_option');
186 $_showHide->addToTemplate();
188 $this->addElement('checkbox', 'create_report', ts('Create Report'));
189 $this->addElement('text', 'report_title', ts('Report Title'));
191 if ($this->_reportId
) {
192 $this->freeze('create_report');
193 $this->freeze('report_title');
197 'CRM_Campaign_Form_Survey_Results',
201 parent
::buildQuickForm();
205 * Global validation rules for the form.
213 public static function formRule($fields, $files, $form) {
215 if (!empty($fields['option_label']) && !empty($fields['option_value']) &&
216 (count(array_filter($fields['option_label'])) == 0) &&
217 (count(array_filter($fields['option_value'])) == 0)
219 $errors['option_label[1]'] = ts('Enter at least one result option.');
222 elseif (empty($fields['option_label']) && empty($fields['option_value'])) {
227 $fields['option_type'] == 2 && empty($fields['option_group_id'])
229 $errors['option_group_id'] = ts("Please select a Survey Result Set.");
233 $_flagOption = $_rowError = 0;
234 $_showHide = new CRM_Core_ShowHideBlocks('', '');
236 //capture duplicate Custom option values
237 if (!empty($fields['option_value'])) {
238 $countValue = count($fields['option_value']);
239 $uniqueCount = count(array_unique($fields['option_value']));
241 if ($countValue > $uniqueCount) {
243 while ($start < self
::NUM_OPTION
) {
244 $nextIndex = $start +
1;
246 while ($nextIndex <= self
::NUM_OPTION
) {
247 if ($fields['option_value'][$start] ==
248 $fields['option_value'][$nextIndex] &&
249 !empty($fields['option_value'][$nextIndex])
252 $errors['option_value[' . $start .
253 ']'] = ts('Duplicate Option values');
254 $errors['option_value[' . $nextIndex .
255 ']'] = ts('Duplicate Option values');
265 //capture duplicate Custom Option label
266 if (!empty($fields['option_label'])) {
267 $countValue = count($fields['option_label']);
268 $uniqueCount = count(array_unique($fields['option_label']));
270 if ($countValue > $uniqueCount) {
272 while ($start < self
::NUM_OPTION
) {
273 $nextIndex = $start +
1;
275 while ($nextIndex <= self
::NUM_OPTION
) {
276 if ($fields['option_label'][$start] ==
277 $fields['option_label'][$nextIndex] &&
278 !empty($fields['option_label'][$nextIndex])
280 $errors['option_label[' . $start .
281 ']'] = ts('Duplicate Option label');
282 $errors['option_label[' . $nextIndex .
283 ']'] = ts('Duplicate Option label');
293 for ($i = 1; $i <= self
::NUM_OPTION
; $i++
) {
294 if (!$fields['option_label'][$i]) {
295 if ($fields['option_value'][$i]) {
296 $errors['option_label[' . $i .
297 ']'] = ts('Option label cannot be empty');
304 elseif (!strlen(trim($fields['option_value'][$i]))) {
305 if (!$fields['option_value'][$i]) {
306 $errors['option_value[' . $i .
307 ']'] = ts('Option value cannot be empty');
312 if (!empty($fields['option_interval'][$i]) &&
313 !CRM_Utils_Rule
::integer($fields['option_interval'][$i])
316 $errors['option_interval[' . $i .
317 ']'] = ts('Please enter a valid integer.');
320 $showBlocks = 'optionField_' . $i;
322 $_showHide->addShow($showBlocks);
326 if (!empty($_emptyRow)) {
327 $_showHide->addHide($showBlocks);
330 $_showHide->addShow($showBlocks);
333 if ($i == self
::NUM_OPTION
) {
334 $hideBlock = 'additionalOption';
335 $_showHide->addHide($hideBlock);
338 $_flagOption = $_emptyRow = 0;
340 $_showHide->addToTemplate();
342 return empty($errors) ?
TRUE : $errors;
348 public function postProcess() {
349 // store the submitted values in an array
351 $params = $this->controller
->exportValues($this->_name
);
352 $params['id'] = $this->_surveyId
;
354 $updateResultSet = FALSE;
355 $resultSetOptGrpId = NULL;
356 if ((CRM_Utils_Array
::value('option_type', $params) == 2) &&
357 !empty($params['option_group_id'])
359 $updateResultSet = TRUE;
360 $resultSetOptGrpId = $params['option_group_id'];
363 $recontactInterval = [];
364 if ($updateResultSet) {
365 $optionValue = new CRM_Core_DAO_OptionValue();
366 $optionValue->option_group_id
= $resultSetOptGrpId;
367 $optionValue->delete();
369 $params['result_id'] = $resultSetOptGrpId;
372 $opGroupName = 'civicrm_survey_' . rand(10, 1000) . '_' . date('YmdHis');
374 $optionGroup = new CRM_Core_DAO_OptionGroup();
375 $optionGroup->name
= $opGroupName;
376 $optionGroup->title
= $this->_values
['title'] . ' Result Set';
377 $optionGroup->is_active
= 1;
378 $optionGroup->save();
380 $params['result_id'] = $optionGroup->id
;
383 foreach ($params['option_value'] as $k => $v) {
384 if (strlen(trim($v))) {
385 $optionValue = new CRM_Core_DAO_OptionValue();
386 $optionValue->option_group_id
= $params['result_id'];
387 $optionValue->label
= $params['option_label'][$k];
388 $optionValue->name
= CRM_Utils_String
::titleToVar($params['option_label'][$k]);
389 $optionValue->value
= trim($v);
390 $optionValue->weight
= $params['option_weight'][$k];
391 $optionValue->is_active
= 1;
393 if (!empty($params['default_option']) &&
394 $params['default_option'] == $k
396 $optionValue->is_default
= 1;
399 $optionValue->save();
401 // using is_numeric since 0 is a valid value for option_interval
402 if (is_numeric($params['option_interval'][$k])) {
403 $recontactInterval[$optionValue->label
] = $params['option_interval'][$k];
408 $params['recontact_interval'] = serialize($recontactInterval);
409 $survey = CRM_Campaign_BAO_Survey
::create($params);
411 // create report if required.
412 if (!$this->_reportId
&& $survey->id
&& !empty($params['create_report'])) {
413 $activityStatus = CRM_Core_PseudoConstant
::activityStatus('name');
414 $activityStatus = array_flip($activityStatus);
416 'name' => "survey_{$survey->id}",
417 'title' => $params['report_title'] ?
$params['report_title'] : $this->_values
['title'],
418 'status_id_op' => 'eq',
420 'status_id_value' => $activityStatus['Scheduled'],
421 'survey_id_value' => [$survey->id
],
422 'description' => ts('Detailed report for canvassing, phone-banking, walk lists or other surveys.'),
424 //Default value of order by
425 $this->_params
['order_bys'] = [
427 'column' => 'sort_name',
431 // for WalkList or default
441 if (CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'WalkList') ==
442 $this->_values
['activity_type_id']
444 $this->_params
['order_bys'] = [
446 'column' => 'street_name',
450 'column' => 'street_number_odd_even',
454 'column' => 'street_number',
458 'column' => 'sort_name',
463 elseif (CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'PhoneBank') ==
464 $this->_values
['activity_type_id']
466 array_push($displayFields, 'phone');
468 elseif ((CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Survey') ==
469 $this->_values
['activity_type_id']) ||
470 (CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Canvass') ==
471 $this->_values
['activity_type_id'])
473 array_push($displayFields, 'phone', 'city', 'state_province_id', 'postal_code', 'email');
475 foreach ($displayFields as $key) {
476 $this->_params
['fields'][$key] = 1;
478 $this->_createNew
= TRUE;
479 $this->_id
= CRM_Report_Utils_Report
::getInstanceIDForValue('survey/detail');
480 CRM_Report_Form_Instance
::postProcess($this, FALSE);
482 $query = "SELECT MAX(id) FROM civicrm_report_instance WHERE name = %1";
483 $reportID = CRM_Core_DAO
::singleValueQuery($query, [
485 "survey_{$survey->id}",
490 $url = CRM_Utils_System
::url("civicrm/report/instance/{$reportID}", 'reset=1');
491 $status = ts("A Survey Detail Report <a href='%1'>%2</a> has been created.",
492 [1 => $url, 2 => $this->_params
['title']]);
497 // reset status as we don't want status set by Instance::postProcess
498 $session = CRM_Core_Session
::singleton();
499 $session->getStatus(TRUE);
501 CRM_Core_Session
::setStatus($status, ts('Saved'), 'success');
504 parent
::endPostProcess();