more comment fixes
[civicrm-core.git] / CRM / Campaign / Form / Survey / Main.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
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
6e62b28c
TM
53 /**
54 * Explicitly declare the entity api name.
55 */
56 public function getDefaultEntity() {
57 return 'Survey';
58 }
6a488035
TO
59
60 public function preProcess() {
61 parent::preProcess();
62
63 $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this);
64
65 $this->assign('context', $this->_context);
66
67 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this);
68
69 if ($this->_action & CRM_Core_Action::UPDATE) {
70 CRM_Utils_System::setTitle(ts('Configure Survey') . ' - ' . $this->_surveyTitle);
71 }
72
6a488035 73 // when custom data is included in this page
a7488080 74 if (!empty($_POST['hidden_custom'])) {
6a488035
TO
75 CRM_Custom_Form_CustomData::preProcess($this);
76 CRM_Custom_Form_CustomData::buildQuickForm($this);
77 }
78
79 if ($this->_name != 'Petition') {
80 $url = CRM_Utils_System::url('civicrm/campaign', 'reset=1&subPage=survey');
81 CRM_Utils_System::appendBreadCrumb(array(array('title' => ts('Survey Dashboard'), 'url' => $url)));
82 }
83
84 $this->_values = $this->get('values');
85 if (!is_array($this->_values)) {
86 $this->_values = array();
87 if ($this->_surveyId) {
88 $params = array('id' => $this->_surveyId);
89 CRM_Campaign_BAO_Survey::retrieve($params, $this->_values);
90 }
91 $this->set('values', $this->_values);
92 }
93
94 $this->assign('action', $this->_action);
95 $this->assign('surveyId', $this->_surveyId);
96 // for custom data
97 $this->assign('entityID', $this->_surveyId);
98 }
99
100 /**
c490a46a 101 * Set default values for the form. Note that in edit/view mode
6a488035
TO
102 * the default values are retrieved from the database
103 *
a6c01b45
CW
104 * @return array
105 * array of default values
6a488035 106 */
00be9182 107 public function setDefaultValues() {
6a488035
TO
108
109 $defaults = $this->_values;
110
111 if ($this->_surveyId) {
112
8cc574cf 113 if (!empty($defaults['result_id']) && !empty($defaults['recontact_interval'])) {
6a488035
TO
114
115 $resultId = $defaults['result_id'];
116 $recontactInterval = unserialize($defaults['recontact_interval']);
117
118 unset($defaults['recontact_interval']);
119 $defaults['option_group_id'] = $resultId;
120 }
121 }
122
123 if (!isset($defaults['is_active'])) {
124 $defaults['is_active'] = 1;
125 }
126
127 $defaultSurveys = CRM_Campaign_BAO_Survey::getSurveys(TRUE, TRUE);
128 if (!isset($defaults['is_default']) && empty($defaultSurveys)) {
129 $defaults['is_default'] = 1;
130 }
131
132 return $defaults;
133 }
134
135 /**
fe482240 136 * Build the form object.
6a488035
TO
137 */
138 public function buildQuickForm() {
6a488035
TO
139
140 $this->add('text', 'title', ts('Title'), CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey', 'title'), TRUE);
141
142 $surveyActivityTypes = CRM_Campaign_BAO_Survey::getSurveyActivityType();
143 // Activity Type id
4c17373d 144 $this->addSelect('activity_type_id', array('option_url' => 'civicrm/admin/campaign/surveyType'), TRUE);
6a488035
TO
145
146 // Campaign id
147 $campaigns = CRM_Campaign_BAO_Campaign::getCampaigns(CRM_Utils_Array::value('campaign_id', $this->_values));
148 $this->add('select', 'campaign_id', ts('Campaign'), array('' => ts('- select -')) + $campaigns);
149
150 // script / instructions
5d51a2f9 151 $this->add('wysiwyg', 'instructions', ts('Instructions for interviewers'), array('rows' => 5, 'cols' => 40));
6a488035
TO
152
153 // release frequency
e71ee30d 154 $this->add('text', 'release_frequency', ts('Release Frequency'), CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey', 'release_frequency'));
6a488035
TO
155
156 $this->addRule('release_frequency', ts('Release Frequency interval should be a positive number.'), 'positiveInteger');
157
158 // max reserved contacts at a time
159 $this->add('text', 'default_number_of_contacts', ts('Maximum reserved at one time'), CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey', 'default_number_of_contacts'));
160 $this->addRule('default_number_of_contacts', ts('Maximum reserved at one time should be a positive number'), 'positiveInteger');
161
162 // total reserved per interviewer
163 $this->add('text', 'max_number_of_contacts', ts('Total reserved per interviewer'), CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey', 'max_number_of_contacts'));
164 $this->addRule('max_number_of_contacts', ts('Total reserved contacts should be a positive number'), 'positiveInteger');
165
166 // is active ?
167 $this->add('checkbox', 'is_active', ts('Active?'));
168
169 // is default ?
170 $this->add('checkbox', 'is_default', ts('Default?'));
171
172 parent::buildQuickForm();
173 }
174
175 /**
fe482240 176 * Process the form.
6a488035
TO
177 */
178 public function postProcess() {
179 // store the submitted values in an array
180 $params = $this->controller->exportValues($this->_name);
181
182 $session = CRM_Core_Session::singleton();
183
184 $params['last_modified_id'] = $session->get('userID');
185 $params['last_modified_date'] = date('YmdHis');
186
187 if ($this->_surveyId) {
188 $params['id'] = $this->_surveyId;
189 }
190 else {
191 $params['created_id'] = $session->get('userID');
192 $params['created_date'] = date('YmdHis');
193 }
194
195 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, 0);
196 $params['is_default'] = CRM_Utils_Array::value('is_default', $params, 0);
197
198 $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params,
6a488035
TO
199 $this->_surveyId,
200 'Survey'
201 );
202 $survey = CRM_Campaign_BAO_Survey::create($params);
203 $this->_surveyId = $survey->id;
204
a7488080 205 if (!empty($this->_values['result_id'])) {
6a488035 206 $query = "SELECT COUNT(*) FROM civicrm_survey WHERE result_id = %1";
353ffa53 207 $countSurvey = (int) CRM_Core_DAO::singleValueQuery($query,
6a488035 208 array(
353ffa53
TO
209 1 => array(
210 $this->_values['result_id'],
6a488035 211 'Positive',
acb1052e 212 ),
353ffa53 213 )
6a488035
TO
214 );
215 // delete option group if no any survey is using it.
216 if (!$countSurvey) {
217 CRM_Core_BAO_OptionGroup::del($this->_values['result_id']);
218 }
219 }
220
221 parent::endPostProcess();
222 }
96025800 223
6a488035 224}