Merge pull request #13373 from MegaphoneJon/core-580-2
[civicrm-core.git] / CRM / Campaign / Form / Survey / Main.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
32 */
33
34/**
3819f101 35 * This class generates form components for processing a survey.
6a488035
TO
36 */
37class CRM_Campaign_Form_Survey_Main extends CRM_Campaign_Form_Survey {
38
39 /* values
e70a7fc0
TO
40 *
41 * @var array
42 */
6a488035
TO
43
44 public $_values;
45
46 /**
fe482240 47 * Context.
6a488035
TO
48 *
49 * @var string
50 */
51 protected $_context;
d5965a37 52
6a488035
TO
53 public function preProcess() {
54 parent::preProcess();
55
edc80cda 56 $this->_context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this);
6a488035
TO
57
58 $this->assign('context', $this->_context);
59
60 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this);
61
62 if ($this->_action & CRM_Core_Action::UPDATE) {
63 CRM_Utils_System::setTitle(ts('Configure Survey') . ' - ' . $this->_surveyTitle);
64 }
65
14c67729
MWMC
66 // Add custom data to form
67 CRM_Custom_Form_CustomData::addToForm($this);
6a488035
TO
68
69 if ($this->_name != 'Petition') {
70 $url = CRM_Utils_System::url('civicrm/campaign', 'reset=1&subPage=survey');
71 CRM_Utils_System::appendBreadCrumb(array(array('title' => ts('Survey Dashboard'), 'url' => $url)));
72 }
73
74 $this->_values = $this->get('values');
75 if (!is_array($this->_values)) {
76 $this->_values = array();
77 if ($this->_surveyId) {
78 $params = array('id' => $this->_surveyId);
79 CRM_Campaign_BAO_Survey::retrieve($params, $this->_values);
80 }
81 $this->set('values', $this->_values);
82 }
83
84 $this->assign('action', $this->_action);
85 $this->assign('surveyId', $this->_surveyId);
6a488035
TO
86 }
87
88 /**
c490a46a 89 * Set default values for the form. Note that in edit/view mode
6a488035
TO
90 * the default values are retrieved from the database
91 *
a6c01b45
CW
92 * @return array
93 * array of default values
6a488035 94 */
00be9182 95 public function setDefaultValues() {
6a488035
TO
96
97 $defaults = $this->_values;
98
99 if ($this->_surveyId) {
100
8cc574cf 101 if (!empty($defaults['result_id']) && !empty($defaults['recontact_interval'])) {
6a488035
TO
102
103 $resultId = $defaults['result_id'];
104 $recontactInterval = unserialize($defaults['recontact_interval']);
105
106 unset($defaults['recontact_interval']);
107 $defaults['option_group_id'] = $resultId;
108 }
109 }
110
111 if (!isset($defaults['is_active'])) {
112 $defaults['is_active'] = 1;
113 }
114
115 $defaultSurveys = CRM_Campaign_BAO_Survey::getSurveys(TRUE, TRUE);
116 if (!isset($defaults['is_default']) && empty($defaultSurveys)) {
117 $defaults['is_default'] = 1;
118 }
119
120 return $defaults;
121 }
122
123 /**
fe482240 124 * Build the form object.
6a488035
TO
125 */
126 public function buildQuickForm() {
6a488035
TO
127 $this->add('text', 'title', ts('Title'), CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey', 'title'), TRUE);
128
6a488035 129 // Activity Type id
4c17373d 130 $this->addSelect('activity_type_id', array('option_url' => 'civicrm/admin/campaign/surveyType'), TRUE);
6a488035 131
f7b32533
CW
132 $this->addEntityRef('campaign_id', ts('Campaign'), [
133 'entity' => 'campaign',
134 'create' => TRUE,
135 ]);
6a488035
TO
136
137 // script / instructions
5d51a2f9 138 $this->add('wysiwyg', 'instructions', ts('Instructions for interviewers'), array('rows' => 5, 'cols' => 40));
6a488035
TO
139
140 // release frequency
9970fd53 141 $this->add('number', 'release_frequency', ts('Release Frequency'), CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey', 'release_frequency'));
6a488035
TO
142
143 $this->addRule('release_frequency', ts('Release Frequency interval should be a positive number.'), 'positiveInteger');
144
145 // max reserved contacts at a time
9970fd53 146 $this->add('number', 'default_number_of_contacts', ts('Maximum reserved at one time'), CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey', 'default_number_of_contacts'));
6a488035
TO
147 $this->addRule('default_number_of_contacts', ts('Maximum reserved at one time should be a positive number'), 'positiveInteger');
148
149 // total reserved per interviewer
9970fd53 150 $this->add('number', 'max_number_of_contacts', ts('Total reserved per interviewer'), CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey', 'max_number_of_contacts'));
6a488035
TO
151 $this->addRule('max_number_of_contacts', ts('Total reserved contacts should be a positive number'), 'positiveInteger');
152
153 // is active ?
154 $this->add('checkbox', 'is_active', ts('Active?'));
155
156 // is default ?
157 $this->add('checkbox', 'is_default', ts('Default?'));
158
159 parent::buildQuickForm();
160 }
161
162 /**
fe482240 163 * Process the form.
6a488035
TO
164 */
165 public function postProcess() {
166 // store the submitted values in an array
167 $params = $this->controller->exportValues($this->_name);
168
169 $session = CRM_Core_Session::singleton();
170
171 $params['last_modified_id'] = $session->get('userID');
172 $params['last_modified_date'] = date('YmdHis');
173
174 if ($this->_surveyId) {
175 $params['id'] = $this->_surveyId;
176 }
177 else {
178 $params['created_id'] = $session->get('userID');
179 $params['created_date'] = date('YmdHis');
180 }
181
182 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, 0);
183 $params['is_default'] = CRM_Utils_Array::value('is_default', $params, 0);
184
14c67729
MWMC
185 $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, $this->getEntityId(), $this->getDefaultEntity());
186
6a488035
TO
187 $survey = CRM_Campaign_BAO_Survey::create($params);
188 $this->_surveyId = $survey->id;
189
a7488080 190 if (!empty($this->_values['result_id'])) {
6a488035 191 $query = "SELECT COUNT(*) FROM civicrm_survey WHERE result_id = %1";
353ffa53 192 $countSurvey = (int) CRM_Core_DAO::singleValueQuery($query,
6a488035 193 array(
353ffa53
TO
194 1 => array(
195 $this->_values['result_id'],
6a488035 196 'Positive',
acb1052e 197 ),
353ffa53 198 )
6a488035
TO
199 );
200 // delete option group if no any survey is using it.
201 if (!$countSurvey) {
202 CRM_Core_BAO_OptionGroup::del($this->_values['result_id']);
203 }
204 }
205
206 parent::endPostProcess();
207 }
96025800 208
6a488035 209}