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