Merge pull request #2539 from PalanteJon/CRM-14223
[civicrm-core.git] / CRM / Campaign / Form / Task / Reserve.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
32 * $Id$
33 *
34 */
35
36 /**
37 * This class provides the functionality to add contacts for
38 * voter reservation.
39 */
40 class CRM_Campaign_Form_Task_Reserve extends CRM_Campaign_Form_Task {
41
42 /**
43 * survet id`
44 *
45 * @var int
46 */
47 protected $_surveyId;
48
49 /**
50 * interviewer id
51 *
52 * @var int
53 */
54 protected $_interviewerId;
55
56 /**
57 * survey details
58 *
59 * @var object
60 */
61 protected $_surveyDetails;
62
63 /**
64 * number of voters
65 *
66 * @var int
67 */
68 protected $_numVoters;
69
70 /**
71 * build all the data structures needed to build the form
72 *
73 * @return void
74 * @access public
75 */
76 function preProcess() {
77 parent::preProcess();
78
79 //get the survey id from user submitted values.
80 $this->_surveyId = $this->get('surveyId');
81 $this->_interviewerId = $this->get('interviewerId');
82 if (!$this->_surveyId) {
83 CRM_Core_Error::statusBounce(ts("Could not find Survey Id."));
84 }
85 if (!$this->_interviewerId) {
86 CRM_Core_Error::statusBounce(ts("Missing Interviewer contact."));
87 }
88 if (!is_array($this->_contactIds) || empty($this->_contactIds)) {
89 CRM_Core_Error::statusBounce(ts("Could not find contacts for reservation."));
90 }
91
92 $params = array('id' => $this->_surveyId);
93 CRM_Campaign_BAO_Survey::retrieve($params, $this->_surveyDetails);
94
95 //get the survey activities.
96 $activityStatus = CRM_Core_PseudoConstant::activityStatus('name');
97 $statusIds = array();
98 foreach (array(
99 'Scheduled') as $name) {
100 if ($statusId = array_search($name, $activityStatus)) {
101 $statusIds[] = $statusId;
102 }
103 }
104
105 // these are the activities count that are linked to the current
106 // interviewer and current survey and not the list of ALL survey activities
107 $this->_numVoters = CRM_Campaign_BAO_Survey::getSurveyActivities($this->_surveyId,
108 $this->_interviewerId,
109 $statusIds,
110 NULL,
111 TRUE
112 );
113 //validate the selected survey.
114 $this->validateSurvey();
115 $this->assign('surveyTitle', $this->_surveyDetails['title']);
116 $this->assign('activityType', $this->_surveyDetails['activity_type_id']);
117 $this->assign('surveyId', $this->_surveyId);
118
119 //append breadcrumb to survey dashboard.
120 if (CRM_Campaign_BAO_Campaign::accessCampaign()) {
121 $url = CRM_Utils_System::url('civicrm/campaign', 'reset=1&subPage=survey');
122 CRM_Utils_System::appendBreadCrumb(array(array('title' => ts('Survey(s)'), 'url' => $url)));
123 }
124
125 //set the title.
126 CRM_Utils_System::setTitle(ts('Reserve Respondents'));
127 }
128
129 function validateSurvey() {
130 $errorMsg = NULL;
131 $maxVoters = CRM_Utils_Array::value('max_number_of_contacts', $this->_surveyDetails);
132 if ($maxVoters) {
133 if ($maxVoters <= $this->_numVoters) {
134 $errorMsg = ts('The maximum number of contacts is already reserved for this interviewer.');
135 }
136 elseif (count($this->_contactIds) > ($maxVoters - $this->_numVoters)) {
137 $errorMsg = ts('You can reserve a maximum of %1 contact(s) at a time for this survey.',
138 array(1 => $maxVoters - $this->_numVoters)
139 );
140 }
141 }
142
143 $defaultNum = CRM_Utils_Array::value('default_number_of_contacts', $this->_surveyDetails);
144 if (!$errorMsg && $defaultNum && (count($this->_contactIds) > $defaultNum)) {
145 $errorMsg = ts('You can reserve a maximum of %1 contact(s) at a time for this survey.',
146 array(1 => $defaultNum)
147 );
148 }
149
150 if ($errorMsg) {
151 CRM_Core_Error::statusBounce($errorMsg);
152 }
153 }
154
155 /**
156 * Build the form
157 *
158 * @access public
159 *
160 * @return void
161 */
162 function buildQuickForm() {
163 // allow to add contact to either new or existing group.
164 $this->addElement('text', 'ActivityType', ts('Activity Type'));
165 $this->addElement('text', 'newGroupName', ts('Name for new group'));
166 $this->addElement('text', 'newGroupDesc', ts('Description of new group'));
167 $groups = CRM_Core_PseudoConstant::group();
168 $hasExistingGroups = FALSE;
169 if (is_array($groups) && !empty($groups)) {
170 $hasExistingGroups = TRUE;
171 $this->addElement('select', 'groups', ts('Add respondent(s) to existing group(s)'),
172 $groups, array('multiple' => "multiple", 'size' => 5)
173 );
174 }
175 $this->assign('hasExistingGroups', $hasExistingGroups);
176
177 $buttons = array(
178 array('type' => 'done',
179 'name' => ts('Reserve'),
180 'subName' => 'reserve',
181 'isDefault' => TRUE,
182 ));
183
184 if (CRM_Core_Permission::check('manage campaign') ||
185 CRM_Core_Permission::check('administer CiviCampaign') ||
186 CRM_Core_Permission::check('interview campaign contacts')
187 ) {
188 $buttons[] = array(
189 'type' => 'next',
190 'name' => ts('Reserve and Interview'),
191 'subName' => 'reserveToInterview',
192 );
193 }
194 $buttons[] = array(
195 'type' => 'back',
196 'name' => ts('Cancel'),
197 );
198
199 $this->addButtons($buttons);
200 $this->addFormRule(array('CRM_Campaign_Form_Task_Reserve', 'formRule'), $this);
201 }
202
203 /**
204 * global validation rules for the form
205 *
206 * @param array $fields posted values of the form
207 *
208 * @return array list of errors to be posted back to the form
209 * @static
210 * @access public
211 */
212 static function formRule($fields, $files, $self) {
213 $errors = array();
214 $invalidGroupName = FALSE;
215 if (!empty($fields['newGroupName'])) {
216 $title = trim($fields['newGroupName']);
217 $name = CRM_Utils_String::titleToVar($title);
218 $query = 'select count(*) from civicrm_group where name like %1 OR title like %2';
219 $grpCnt = CRM_Core_DAO::singleValueQuery($query, array(1 => array($name, 'String'),
220 2 => array($title, 'String'),
221 ));
222 if ($grpCnt) {
223 $invalidGroupName = TRUE;
224 $errors['newGroupName'] = ts('Group \'%1\' already exists.', array(1 => $fields['newGroupName']));
225 }
226 }
227 $self->assign('invalidGroupName', $invalidGroupName);
228
229 return empty($errors) ? TRUE : $errors;
230 }
231
232 /**
233 * process the form after the input has been submitted and validated
234 *
235 * @access public
236 *
237 * @return void
238 */
239 public function postProcess() {
240 //add reservation.
241 $countVoters = 0;
242 $maxVoters = CRM_Utils_Array::value('max_number_of_contacts', $this->_surveyDetails);
243 $activityStatus = CRM_Core_PseudoConstant::activityStatus('name');
244 $statusHeld = array_search('Scheduled', $activityStatus);
245
246 $reservedVoterIds = array();
247 foreach ($this->_contactIds as $cid) {
248 $subject = ts('%1', array(1 => $this->_surveyDetails['title'])) . ' - ' . ts('Respondent Reservation');
249 $session = CRM_Core_Session::singleton();
250 $activityParams = array('source_contact_id' => $session->get('userID'),
251 'assignee_contact_id' => array($this->_interviewerId),
252 'target_contact_id' => array($cid),
253 'source_record_id' => $this->_surveyId,
254 'activity_type_id' => $this->_surveyDetails['activity_type_id'],
255 'subject' => $subject,
256 'activity_date_time' => date('YmdHis'),
257 'status_id' => $statusHeld,
258 'skipRecentView' => 1,
259 'campaign_id' => CRM_Utils_Array::value('campaign_id', $this->_surveyDetails),
260 );
261 $activity = CRM_Activity_BAO_Activity::create($activityParams);
262 if ($activity->id) {
263 $countVoters++;
264 $reservedVoterIds[$cid] = $cid;
265 }
266 if ($maxVoters && ($maxVoters <= ($this->_numVoters + $countVoters))) {
267 break;
268 }
269 }
270
271 //add reserved voters to groups.
272 $groupAdditions = $this->_addRespondentToGroup($reservedVoterIds);
273
274 // Success message
275 if ($countVoters > 0) {
276 $status = '<p>' . ts("%1 Contact(s) have been reserved.", array(1 => $countVoters)) . '</p>';
277 if ($groupAdditions) {
278 $status .= '<p>' . ts('Respondent(s) has been added to %1 group(s).',
279 array(1 => implode(', ', $groupAdditions))
280 ) . '</p>';
281 }
282 CRM_Core_Session::setStatus($status, ts('Reservation Added'), 'success');
283 }
284 // Error message
285 if (count($this->_contactIds) > $countVoters) {
286 CRM_Core_Session::setStatus(ts('Reservation did not add for %1 contact(s).',
287 array(1 => (count($this->_contactIds) - $countVoters))
288 ), ts('Notice'));
289 }
290
291 //get ready to jump to voter interview form.
292 $buttonName = $this->controller->getButtonName();
293 if (!empty($reservedVoterIds) &&
294 $buttonName == '_qf_Reserve_next_reserveToInterview'
295 ) {
296 $this->controller->set('surveyId', $this->_surveyId);
297 $this->controller->set('contactIds', $reservedVoterIds);
298 $this->controller->set('interviewerId', $this->_interviewerId);
299 $this->controller->set('reserveToInterview', TRUE);
300 }
301 }
302
303 private function _addRespondentToGroup($contactIds) {
304 $groupAdditions = array();
305 if (empty($contactIds)) {
306 return $groupAdditions;
307 }
308
309 $params = $this->controller->exportValues($this->_name);
310 $groups = CRM_Utils_Array::value('groups', $params, array());
311 $newGroupName = CRM_Utils_Array::value('newGroupName', $params);
312 $newGroupDesc = CRM_Utils_Array::value('newGroupDesc', $params);
313
314 $newGroupId = NULL;
315 //create new group.
316 if ($newGroupName) {
317 $grpParams = array(
318 'title' => $newGroupName,
319 'description' => $newGroupDesc,
320 'is_active' => TRUE,
321 );
322 $group = CRM_Contact_BAO_Group::create($grpParams);
323 $groups[] = $newGroupId = $group->id;
324 }
325
326 //add the respondents to groups.
327 if (is_array($groups)) {
328 $existingGroups = CRM_Core_PseudoConstant::group();
329 foreach ($groups as $groupId) {
330 $addCount = CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIds, $groupId);
331 $totalCount = CRM_Utils_Array::value(1, $addCount);
332 if ($groupId == $newGroupId) {
333 $name = $newGroupName;
334 $new = TRUE;
335 }
336 else {
337 $name = $existingGroups[$groupId];
338 $new = FALSE;
339 }
340 if ($totalCount) {
341 $url = CRM_Utils_System::url('civicrm/group/search',
342 'reset=1&force=1&context=smog&gid=' . $groupId
343 );
344 $groupAdditions[] = '<a href="' . $url . '">' . $name . '</a>';
345 }
346 }
347 }
348
349 return $groupAdditions;
350 }
351 }
352