3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
37 * This class generates form components for processing a survey
40 class CRM_Campaign_Form_Survey_Results
extends CRM_Campaign_Form_Survey
{
44 protected $_reportTitle;
52 CONST NUM_OPTION
= 11;
54 public function preProcess() {
57 $this->_values
= $this->get('values');
58 if (!is_array($this->_values
)) {
59 $this->_values
= array();
60 if ($this->_surveyId
) {
61 $params = array('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";
68 $params = array( 1 => array("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 * This function sets the default values for the form. Note that in edit/view mode
78 * the default values are retrieved from the database
82 * @return array array of default values
85 function setDefaultValues() {
86 $defaults = $this->_values
;
88 // set defaults for weight.
89 for ($i = 1; $i <= self
::NUM_OPTION
; $i++
) {
90 $defaults["option_weight[{$i}]"] = $i;
93 $defaults['create_report'] = 1;
94 if ($this->_reportId
) {
95 $defaults['report_title'] = $this->_reportTitle
;
101 * Function to actually build the form
108 public function buildQuickForm() {
109 $optionGroups = CRM_Campaign_BAO_Survey
::getResultSets();
111 if (empty($optionGroups)) {
112 $optionTypes = array('1' => ts('Create new result set'));
115 $optionTypes = array('1' => ts('Create a new result set'),
116 '2' => ts('Use existing result set'),
120 ts('Select Result Set'),
122 '' => ts('- select -')) +
$optionGroups, FALSE,
123 array('onChange' => 'loadOptionGroup( )')
127 $element = &$this->addRadio('option_type',
128 ts('Survey Responses'),
131 'onclick' => "showOptionSelect();"), '<br/>', TRUE
134 if (empty($optionGroups) ||
!CRM_Utils_Array
::value('result_id', $this->_values
)) {
135 $this->setdefaults(array('option_type' => 1));
137 elseif (CRM_Utils_Array
::value('result_id', $this->_values
)) {
138 $this->setdefaults(array(
140 'option_group_id' => $this->_values
['result_id'],
144 // form fields of Custom Option rows
145 $defaultOption = array();
146 $_showHide = new CRM_Core_ShowHideBlocks('', '');
148 $optionAttributes = CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue');
149 $optionAttributes['label']['size'] = $optionAttributes['value']['size'] = 25;
151 for ($i = 1; $i <= self
::NUM_OPTION
; $i++
) {
152 //the show hide blocks
153 $showBlocks = 'optionField_' . $i;
155 $_showHide->addHide($showBlocks);
156 if ($i == self
::NUM_OPTION
) {
157 $_showHide->addHide('additionalOption');
161 $_showHide->addShow($showBlocks);
164 $this->add('text', 'option_label[' . $i . ']', ts('Label'),
165 $optionAttributes['label']
169 $this->add('text', 'option_value[' . $i . ']', ts('Value'),
170 $optionAttributes['value']
174 $this->add('text', "option_weight[$i]", ts('Order'),
175 $optionAttributes['weight']
178 $this->add('text', 'option_interval[' . $i . ']', ts('Recontact Interval'),
179 CRM_Core_DAO
::getAttribute('CRM_Campaign_DAO_Survey', 'release_frequency')
182 $defaultOption[$i] = $this->createElement('radio', NULL, NULL, NULL, $i);
185 //default option selection
186 $this->addGroup($defaultOption, 'default_option');
188 $_showHide->addToTemplate();
190 $this->addElement('checkbox', 'create_report', ts('Create Report'));
191 $this->addElement('text', 'report_title', ts('Report Title'));
193 if( $this->_reportId
){
194 $this->freeze('create_report');
195 $this->freeze('report_title');
198 $this->addFormRule(array('CRM_Campaign_Form_Survey_Results', 'formRule'), $this);
200 parent
::buildQuickForm();
204 * global validation rules for the form
207 static function formRule($fields, $files, $form) {
210 if (CRM_Utils_Array
::value('option_label', $fields) &&
211 CRM_Utils_Array
::value('option_value', $fields) &&
212 (count(array_filter($fields['option_label'])) == 0) &&
213 (count(array_filter($fields['option_value'])) == 0)
215 $errors['option_label[1]'] = ts('Enter at least one result option.');
218 elseif (!CRM_Utils_Array
::value('option_label', $fields) &&
219 !CRM_Utils_Array
::value('option_value', $fields)
224 if ($fields['option_type'] == 2 &&
225 !CRM_Utils_Array
::value('option_group_id', $fields)
227 $errors['option_group_id'] = ts("Please select a Survey Result Set.");
231 $_flagOption = $_rowError = 0;
232 $_showHide = new CRM_Core_ShowHideBlocks('', '');
234 //capture duplicate Custom option values
235 if (!empty($fields['option_value'])) {
236 $countValue = count($fields['option_value']);
237 $uniqueCount = count(array_unique($fields['option_value']));
239 if ($countValue > $uniqueCount) {
241 while ($start < self
::NUM_OPTION
) {
242 $nextIndex = $start +
1;
244 while ($nextIndex <= self
::NUM_OPTION
) {
245 if ($fields['option_value'][$start] == $fields['option_value'][$nextIndex] &&
246 !empty($fields['option_value'][$nextIndex])
249 $errors['option_value[' . $start . ']'] = ts('Duplicate Option values');
250 $errors['option_value[' . $nextIndex . ']'] = ts('Duplicate Option values');
260 //capture duplicate Custom Option label
261 if (!empty($fields['option_label'])) {
262 $countValue = count($fields['option_label']);
263 $uniqueCount = count(array_unique($fields['option_label']));
265 if ($countValue > $uniqueCount) {
267 while ($start < self
::NUM_OPTION
) {
268 $nextIndex = $start +
1;
270 while ($nextIndex <= self
::NUM_OPTION
) {
271 if ($fields['option_label'][$start] == $fields['option_label'][$nextIndex] && !empty($fields['option_label'][$nextIndex])) {
272 $errors['option_label[' . $start . ']'] = ts('Duplicate Option label');
273 $errors['option_label[' . $nextIndex . ']'] = ts('Duplicate Option label');
283 for ($i = 1; $i <= self
::NUM_OPTION
; $i++
) {
284 if (!$fields['option_label'][$i]) {
285 if ($fields['option_value'][$i]) {
286 $errors['option_label[' . $i . ']'] = ts('Option label cannot be empty');
293 elseif (!strlen(trim($fields['option_value'][$i]))) {
294 if (!$fields['option_value'][$i]) {
295 $errors['option_value[' . $i . ']'] = ts('Option value cannot be empty');
300 if (CRM_Utils_Array
::value($i, $fields['option_interval']) && !CRM_Utils_Rule
::integer($fields['option_interval'][$i])) {
302 $errors['option_interval[' . $i . ']'] = ts('Please enter a valid integer.');
305 $showBlocks = 'optionField_' . $i;
307 $_showHide->addShow($showBlocks);
311 if (!empty($_emptyRow)) {
312 $_showHide->addHide($showBlocks);
315 $_showHide->addShow($showBlocks);
318 if ($i == self
::NUM_OPTION
) {
319 $hideBlock = 'additionalOption';
320 $_showHide->addHide($hideBlock);
323 $_flagOption = $_emptyRow = 0;
325 $_showHide->addToTemplate();
327 return empty($errors) ?
TRUE : $errors;
338 public function postProcess() {
339 // store the submitted values in an array
341 $params = $this->controller
->exportValues($this->_name
);
342 $params['id'] = $this->_surveyId
;
344 $updateResultSet = FALSE;
345 $resultSetOptGrpId = NULL;
346 if ((CRM_Utils_Array
::value('option_type', $params) == 2) &&
347 CRM_Utils_Array
::value('option_group_id', $params)
349 $updateResultSet = TRUE;
350 $resultSetOptGrpId = $params['option_group_id'];
353 $recontactInterval = array();
354 if ($updateResultSet) {
355 $optionValue = new CRM_Core_DAO_OptionValue();
356 $optionValue->option_group_id
= $resultSetOptGrpId;
357 $optionValue->delete();
359 $params['result_id'] = $resultSetOptGrpId;
362 $opGroupName = 'civicrm_survey_' . rand(10, 1000) . '_' . date('YmdHis');
364 $optionGroup = new CRM_Core_DAO_OptionGroup();
365 $optionGroup->name
= $opGroupName;
366 $optionGroup->title
= $this->_values
['title'] . ' Result Set';
367 $optionGroup->is_active
= 1;
368 $optionGroup->save();
370 $params['result_id'] = $optionGroup->id
;
373 foreach ($params['option_value'] as $k => $v) {
374 if (strlen(trim($v))) {
375 $optionValue = new CRM_Core_DAO_OptionValue();
376 $optionValue->option_group_id
= $params['result_id'];
377 $optionValue->label
= $params['option_label'][$k];
378 $optionValue->name
= CRM_Utils_String
::titleToVar($params['option_label'][$k]);
379 $optionValue->value
= trim($v);
380 $optionValue->weight
= $params['option_weight'][$k];
381 $optionValue->is_active
= 1;
383 if (CRM_Utils_Array
::value('default_option', $params) &&
384 $params['default_option'] == $k
386 $optionValue->is_default
= 1;
389 $optionValue->save();
391 // using is_numeric since 0 is a valid value for option_interval
392 if ( is_numeric($params['option_interval'][$k])) {
393 $recontactInterval[$optionValue->label
] = $params['option_interval'][$k];
398 $params['recontact_interval'] = serialize($recontactInterval);
399 $survey = CRM_Campaign_BAO_Survey
::create($params);
401 // create report if required.
402 if ( !$this->_reportId
&& $survey->id
&& CRM_Utils_Array
::value('create_report', $params) ) {
403 $activityStatus = CRM_Core_PseudoConstant
::activityStatus('name');
404 $activityStatus = array_flip($activityStatus);
406 array( 'name' => "survey_{$survey->id}",
407 'title' => $params['report_title'] ?
$params['report_title'] : $this->_values
['title'],
408 'status_id_op' => 'eq',
409 'status_id_value' => $activityStatus['Scheduled'], // reserved status
410 'survey_id_value' => array($survey->id
),
411 'description' => ts('Detailed report for canvassing, phone-banking, walk lists or other surveys.'),
413 //Default value of order by
414 $this->_params
['order_bys'] =
418 'column' => 'sort_name',
422 // for WalkList or default
423 $displayFields = array('id', 'sort_name', 'result', 'street_number','street_name','street_unit','survey_response');
424 if ( CRM_Core_OptionGroup
::getValue('activity_type','WalkList') == $this->_values
['activity_type_id'] ) {
425 $this->_params
['order_bys'] =
429 'column' => 'street_name',
434 'column' => 'street_number_odd_even',
439 'column' => 'street_number',
444 'column' => 'sort_name',
449 elseif ( CRM_Core_OptionGroup
::getValue('activity_type','PhoneBank') == $this->_values
['activity_type_id'] ) {
450 array_push($displayFields, 'phone');
452 elseif ((CRM_Core_OptionGroup
::getValue('activity_type','Survey') == $this->_values
['activity_type_id']) ||
453 (CRM_Core_OptionGroup
::getValue('activity_type','Canvass') == $this->_values
['activity_type_id']) ) {
454 array_push($displayFields, 'phone','city','state_province_id','postal_code','email');
456 foreach($displayFields as $key){
457 $this->_params
['fields'][$key] = 1;
459 $this->_createNew
= TRUE;
460 $this->_id
= CRM_Report_Utils_Report
::getInstanceIDForValue('survey/detail');
461 CRM_Report_Form_Instance
::postProcess($this, FALSE);
463 $query = "SELECT MAX(id) FROM civicrm_report_instance WHERE name = %1";
464 $reportID = CRM_Core_DAO
::singleValueQuery($query, array(1 => array("survey_{$survey->id}",'String')));
466 $url = CRM_Utils_System
::url("civicrm/report/instance/{$reportID}",'reset=1');
467 $status = ts("A Survey Detail Report <a href='%1'>%2</a> has been created.",
468 array(1 => $url, 2 => $this->_params
['title']));
473 // reset status as we don't want status set by Instance::postProcess
474 $session = CRM_Core_Session
::singleton();
475 $session->getStatus(TRUE);
477 CRM_Core_Session
::setStatus($status, ts('Saved'), 'success');
480 parent
::endPostProcess();