Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * This class provides the functionality to record voter's interview. | |
38 | */ | |
39 | class CRM_Campaign_Form_Task_Interview extends CRM_Campaign_Form_Task { | |
40 | ||
41 | /** | |
100fef9d | 42 | * The title of the group |
6a488035 TO |
43 | * |
44 | * @var string | |
45 | */ | |
46 | protected $_title; | |
47 | ||
48 | /** | |
100fef9d | 49 | * Variable to store redirect path |
6a488035 TO |
50 | */ |
51 | private $_userContext; | |
52 | ||
53 | private $_groupTree; | |
54 | ||
55 | private $_surveyFields; | |
56 | ||
57 | private $_surveyTypeId; | |
58 | ||
59 | private $_interviewerId; | |
60 | ||
61 | private $_surveyActivityIds; | |
62 | ||
63 | private $_votingTab = FALSE; | |
64 | ||
65 | private $_surveyValues; | |
66 | ||
67 | private $_resultOptions; | |
68 | ||
69 | private $_allowAjaxReleaseButton; | |
70 | ||
71 | /** | |
fe482240 | 72 | * Build all the data structures needed to build the form. |
6a488035 TO |
73 | * |
74 | * @return void | |
6a488035 | 75 | */ |
00be9182 | 76 | public function preProcess() { |
6a488035 TO |
77 | $this->_votingTab = $this->get('votingTab'); |
78 | $this->_reserveToInterview = $this->get('reserveToInterview'); | |
79 | if ($this->_reserveToInterview || $this->_votingTab) { | |
80 | //user came from voting tab / reserve form. | |
81 | foreach (array( | |
353ffa53 TO |
82 | 'surveyId', |
83 | 'contactIds', | |
a130e045 | 84 | 'interviewerId', |
353ffa53 | 85 | ) as $fld) { |
6a488035 TO |
86 | $this->{"_$fld"} = $this->get($fld); |
87 | } | |
88 | //get the target voter ids. | |
89 | if ($this->_votingTab) { | |
90 | $this->getVoterIds(); | |
91 | } | |
92 | } | |
93 | else { | |
94 | parent::preProcess(); | |
95 | //get the survey id from user submitted values. | |
96 | $this->_surveyId = CRM_Utils_Array::value('campaign_survey_id', $this->get('formValues')); | |
97 | $this->_interviewerId = CRM_Utils_Array::value('survey_interviewer_id', $this->get('formValues')); | |
98 | } | |
99 | ||
481a74f4 | 100 | if ($this->_surveyId) { |
6a488035 TO |
101 | $params = array('id' => $this->_surveyId); |
102 | CRM_Campaign_BAO_Survey::retrieve($params, $this->_surveyDetails); | |
103 | } | |
104 | ||
94880218 | 105 | $orderClause = FALSE; |
353ffa53 | 106 | $buttonName = $this->controller->getButtonName(); |
481a74f4 | 107 | if ($buttonName == '_qf_Interview_submit_orderBy' && !empty($_POST['order_bys'])) { |
6a488035 TO |
108 | $orderByParams = CRM_Utils_Array::value('order_bys', $_POST); |
109 | } | |
481a74f4 | 110 | elseif (CRM_Core_OptionGroup::getValue('activity_type', 'WalkList') == $this->_surveyDetails['activity_type_id']) { |
acb1052e WA |
111 | $orderByParams |
112 | = array( | |
9d72cede | 113 | 1 => array( |
6a488035 | 114 | 'column' => 'civicrm_address.street_name', |
353ffa53 | 115 | 'order' => 'ASC', |
6a488035 | 116 | ), |
9d72cede | 117 | 2 => array( |
6a488035 TO |
118 | 'column' => 'civicrm_address.street_number%2', |
119 | 'order' => 'ASC', | |
120 | ), | |
9d72cede | 121 | 3 => array( |
6a488035 TO |
122 | 'column' => 'civicrm_address.street_number', |
123 | 'order' => 'ASC', | |
124 | ), | |
9d72cede | 125 | 4 => array( |
6a488035 TO |
126 | 'column' => 'contact_a.sort_name', |
127 | 'order' => 'ASC', | |
128 | ), | |
129 | ); | |
130 | } | |
131 | ||
132 | $orderBy = array(); | |
481a74f4 TO |
133 | if (!empty($orderByParams)) { |
134 | foreach ($orderByParams as $key => $val) { | |
a7488080 | 135 | if (!empty($val['column'])) { |
6a488035 TO |
136 | $orderBy[] = "{$val['column']} {$val['order']}"; |
137 | } | |
138 | } | |
481a74f4 | 139 | if (!empty($orderBy)) { |
6a488035 TO |
140 | $orderClause = "ORDER BY " . implode(', ', $orderBy); |
141 | } | |
142 | } | |
143 | ||
144 | $this->_contactIds = array_unique($this->_contactIds); | |
145 | if (!empty($this->_contactIds) && $orderClause) { | |
146 | $clause = 'contact_a.id IN ( ' . implode(',', $this->_contactIds) . ' ) '; | |
353ffa53 | 147 | $sql = " |
6a488035 TO |
148 | SELECT contact_a.id |
149 | FROM civicrm_contact contact_a | |
150 | LEFT JOIN civicrm_address ON contact_a.id = civicrm_address.contact_id | |
151 | WHERE {$clause} | |
152 | {$orderClause}"; | |
153 | ||
154 | $this->_contactIds = array(); | |
155 | $dao = CRM_Core_DAO::executeQuery($sql); | |
156 | while ($dao->fetch()) { | |
157 | $this->_contactIds[] = $dao->id; | |
158 | } | |
159 | } | |
160 | ||
6a488035 TO |
161 | //get the contact read only fields to display. |
162 | $readOnlyFields = array_merge(array( | |
163 | 'contact_type' => '', | |
164 | 'sort_name' => ts('Name'), | |
165 | )); | |
166 | ||
167 | //get the read only field data. | |
168 | $returnProperties = array_fill_keys(array_keys($readOnlyFields), 1); | |
169 | $returnProperties['contact_sub_type'] = TRUE; | |
170 | ||
171 | //validate all voters for required activity. | |
172 | //get the survey activities for given voters. | |
173 | $this->_surveyActivityIds = CRM_Campaign_BAO_Survey::voterActivityDetails($this->_surveyId, | |
174 | $this->_contactIds, | |
175 | $this->_interviewerId | |
176 | ); | |
177 | $activityStatus = CRM_Core_PseudoConstant::activityStatus('name'); | |
178 | $scheduledStatusId = array_search('Scheduled', $activityStatus); | |
179 | ||
180 | $activityIds = array(); | |
181 | foreach ($this->_contactIds as $key => $voterId) { | |
353ffa53 TO |
182 | $actVals = CRM_Utils_Array::value($voterId, $this->_surveyActivityIds); |
183 | $statusId = CRM_Utils_Array::value('status_id', $actVals); | |
6a488035 TO |
184 | $activityId = CRM_Utils_Array::value('activity_id', $actVals); |
185 | if ($activityId && | |
186 | $statusId && | |
187 | $scheduledStatusId == $statusId | |
188 | ) { | |
189 | $activityIds["activity_id_{$voterId}"] = $activityId; | |
190 | } | |
191 | else { | |
192 | unset($this->_contactIds[$key]); | |
193 | } | |
194 | } | |
195 | ||
196 | //retrieve the contact details. | |
197 | $voterDetails = CRM_Campaign_BAO_Survey::voterDetails($this->_contactIds, $returnProperties); | |
198 | ||
199 | $this->_allowAjaxReleaseButton = FALSE; | |
200 | if ($this->_votingTab && | |
201 | (CRM_Core_Permission::check('manage campaign') || | |
202 | CRM_Core_Permission::check('administer CiviCampaign') || | |
203 | CRM_Core_Permission::check('release campaign contacts') | |
204 | ) | |
205 | ) { | |
206 | $this->_allowAjaxReleaseButton = TRUE; | |
207 | } | |
208 | ||
209 | //validate voter ids across profile. | |
210 | $this->filterVoterIds(); | |
211 | $this->assign('votingTab', $this->_votingTab); | |
212 | $this->assign('componentIds', $this->_contactIds); | |
213 | $this->assign('componentIdsJson', json_encode($this->_contactIds)); | |
214 | $this->assign('voterDetails', $voterDetails); | |
215 | $this->assign('readOnlyFields', $readOnlyFields); | |
216 | $this->assign('interviewerId', $this->_interviewerId); | |
217 | $this->assign('surveyActivityIds', json_encode($activityIds)); | |
218 | $this->assign('allowAjaxReleaseButton', $this->_allowAjaxReleaseButton); | |
219 | ||
220 | //get the survey values. | |
221 | $this->_surveyValues = $this->get('surveyValues'); | |
222 | if (!is_array($this->_surveyValues)) { | |
223 | $this->_surveyValues = array(); | |
224 | if ($this->_surveyId) { | |
225 | $surveyParams = array('id' => $this->_surveyId); | |
226 | CRM_Campaign_BAO_Survey::retrieve($surveyParams, $this->_surveyValues); | |
227 | } | |
228 | $this->set('surveyValues', $this->_surveyValues); | |
229 | } | |
230 | $this->assign('surveyValues', $this->_surveyValues); | |
231 | ||
232 | $result = CRM_Campaign_BAO_Survey::getReportID($this->_surveyId); | |
94880218 | 233 | $this->assign("instanceId", $result); |
6a488035 TO |
234 | |
235 | //get the survey result options. | |
236 | $this->_resultOptions = $this->get('resultOptions'); | |
237 | if (!is_array($this->_resultOptions)) { | |
238 | $this->_resultOptions = array(); | |
239 | if ($resultOptionId = CRM_Utils_Array::value('result_id', $this->_surveyValues)) { | |
240 | $this->_resultOptions = CRM_Core_OptionGroup::valuesByID($resultOptionId); | |
241 | } | |
242 | $this->set('resultOptions', $this->_resultOptions); | |
243 | } | |
244 | ||
245 | //validate the required ids. | |
246 | $this->validateIds(); | |
247 | ||
248 | //append breadcrumb to survey dashboard. | |
249 | if (CRM_Campaign_BAO_Campaign::accessCampaign()) { | |
250 | $url = CRM_Utils_System::url('civicrm/campaign', 'reset=1&subPage=survey'); | |
251 | CRM_Utils_System::appendBreadCrumb(array(array('title' => ts('Survey(s)'), 'url' => $url))); | |
252 | } | |
253 | ||
254 | //set the title. | |
255 | $activityTypes = CRM_Core_PseudoConstant::activityType(FALSE, TRUE, FALSE, 'label', TRUE); | |
256 | $this->_surveyTypeId = CRM_Utils_Array::value('activity_type_id', $this->_surveyValues); | |
257 | CRM_Utils_System::setTitle(ts('Record %1 Responses', array(1 => $activityTypes[$this->_surveyTypeId]))); | |
258 | } | |
259 | ||
00be9182 | 260 | public function validateIds() { |
94880218 | 261 | $required = array( |
353ffa53 | 262 | 'surveyId' => ts('Could not find Survey.'), |
6a488035 TO |
263 | 'interviewerId' => ts('Could not find Interviewer.'), |
264 | 'contactIds' => ts('No respondents are currently reserved for you to interview.'), | |
265 | 'resultOptions' => ts('Oops. It looks like there is no response option configured.'), | |
266 | ); | |
267 | ||
268 | $errorMessages = array(); | |
269 | foreach ($required as $fld => $msg) { | |
270 | if (empty($this->{"_$fld"})) { | |
271 | if (!$this->_votingTab) { | |
272 | CRM_Core_Error::statusBounce($msg); | |
273 | break; | |
274 | } | |
275 | $errorMessages[] = $msg; | |
276 | } | |
277 | } | |
278 | ||
279 | $this->assign('errorMessages', empty($errorMessages) ? FALSE : $errorMessages); | |
280 | } | |
281 | ||
282 | /** | |
fe482240 | 283 | * Build the form object. |
6a488035 | 284 | * |
6a488035 TO |
285 | * |
286 | * @return void | |
287 | */ | |
00be9182 | 288 | public function buildQuickForm() { |
6a488035 TO |
289 | $this->assign('surveyTypeId', $this->_surveyTypeId); |
290 | ||
acb1052e WA |
291 | $options |
292 | = array( | |
353ffa53 TO |
293 | '' => ' - none - ', |
294 | 'civicrm_address.street_name' => 'Street Name', | |
6a488035 | 295 | 'civicrm_address.street_number%2' => 'Odd / Even Street Number', |
353ffa53 TO |
296 | 'civicrm_address.street_number' => 'Street Number', |
297 | 'contact_a.sort_name' => 'Respondent Name', | |
6a488035 TO |
298 | ); |
299 | for ($i = 1; $i < count($options); $i++) { | |
300 | $this->addElement('select', "order_bys[{$i}][column]", ts('Order by Column'), $options); | |
353ffa53 TO |
301 | $this->addElement('select', "order_bys[{$i}][order]", ts('Order by Order'), array( |
302 | 'ASC' => 'Ascending', | |
a130e045 | 303 | 'DESC' => 'Descending', |
353ffa53 | 304 | )); |
6a488035 TO |
305 | } |
306 | ||
307 | //pickup the uf fields. | |
308 | $this->_surveyFields = CRM_Campaign_BAO_Survey::getSurveyResponseFields($this->_surveyId, | |
309 | $this->_surveyTypeId | |
310 | ); | |
311 | ||
312 | foreach ($this->_contactIds as $contactId) { | |
313 | //build the profile fields. | |
314 | foreach ($this->_surveyFields as $name => $field) { | |
9b873358 | 315 | if ($field) { |
6a488035 TO |
316 | CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $contactId); |
317 | } | |
318 | } | |
319 | ||
320 | //build the result field. | |
321 | if (!empty($this->_resultOptions)) { | |
322 | $this->add('select', "field[$contactId][result]", ts('Result'), | |
323 | array( | |
a130e045 | 324 | '' => ts('- select -'), |
353ffa53 TO |
325 | ) + |
326 | array_combine($this->_resultOptions, $this->_resultOptions) | |
6a488035 TO |
327 | ); |
328 | } | |
329 | ||
330 | $this->add('text', "field[{$contactId}][note]", ts('Note')); | |
331 | ||
332 | //need to keep control for release/reserve. | |
333 | if ($this->_allowAjaxReleaseButton) { | |
334 | $this->addElement('hidden', | |
335 | "field[{$contactId}][is_release_or_reserve]", 0, | |
336 | array('id' => "field_{$contactId}_is_release_or_reserve") | |
337 | ); | |
338 | } | |
339 | } | |
340 | $this->assign('surveyFields', empty($this->_surveyFields) ? FALSE : $this->_surveyFields); | |
341 | ||
342 | //no need to get qf buttons. | |
343 | if ($this->_votingTab) { | |
344 | return; | |
345 | } | |
346 | ||
347 | $buttons = array( | |
94880218 | 348 | array( |
353ffa53 | 349 | 'type' => 'cancel', |
6a488035 TO |
350 | 'name' => ts('Done'), |
351 | 'subName' => 'interview', | |
352 | 'isDefault' => TRUE, | |
a130e045 | 353 | ), |
353ffa53 | 354 | ); |
6a488035 TO |
355 | |
356 | $buttons[] = array( | |
357 | 'type' => 'submit', | |
358 | 'name' => ts('Order By >>'), | |
359 | 'subName' => 'orderBy', | |
360 | ); | |
361 | ||
362 | $manageCampaign = CRM_Core_Permission::check('manage campaign'); | |
363 | $adminCampaign = CRM_Core_Permission::check('administer CiviCampaign'); | |
364 | if ($manageCampaign || | |
365 | $adminCampaign || | |
366 | CRM_Core_Permission::check('release campaign contacts') | |
367 | ) { | |
368 | $buttons[] = array( | |
369 | 'type' => 'next', | |
370 | 'name' => ts('Release Respondents >>'), | |
371 | 'subName' => 'interviewToRelease', | |
372 | ); | |
373 | } | |
374 | if ($manageCampaign || | |
375 | $adminCampaign || | |
376 | CRM_Core_Permission::check('reserve campaign contacts') | |
377 | ) { | |
378 | $buttons[] = array( | |
379 | 'type' => 'done', | |
380 | 'name' => ts('Reserve More Respondents >>'), | |
381 | 'subName' => 'interviewToReserve', | |
382 | ); | |
383 | } | |
384 | ||
385 | $this->addButtons($buttons); | |
386 | } | |
387 | ||
388 | /** | |
c490a46a | 389 | * Set default values for the form. |
6a488035 | 390 | * |
6a488035 | 391 | * |
355ba699 | 392 | * @return void |
6a488035 | 393 | */ |
00be9182 | 394 | public function setDefaultValues() { |
6a488035 TO |
395 | //load default data for only contact fields. |
396 | $contactFields = $defaults = array(); | |
397 | foreach ($this->_surveyFields as $name => $field) { | |
398 | $acceptable_types = CRM_Contact_BAO_ContactType::basicTypes(); | |
399 | $acceptable_types[] = 'Contact'; | |
400 | if (in_array($field['field_type'], $acceptable_types)) { | |
401 | $contactFields[$name] = $field; | |
402 | } | |
403 | } | |
404 | if (!empty($contactFields)) { | |
405 | foreach ($this->_contactIds as $contactId) { | |
406 | CRM_Core_BAO_UFGroup::setProfileDefaults($contactId, $contactFields, $defaults, FALSE); | |
407 | } | |
408 | } | |
409 | ||
481a74f4 | 410 | if (CRM_Core_OptionGroup::getValue('activity_type', 'WalkList') == $this->_surveyDetails['activity_type_id']) { |
acb1052e WA |
411 | $defaults['order_bys'] |
412 | = array( | |
9d72cede | 413 | 1 => array( |
6a488035 | 414 | 'column' => 'civicrm_address.street_name', |
353ffa53 | 415 | 'order' => 'ASC', |
6a488035 | 416 | ), |
9d72cede | 417 | 2 => array( |
6a488035 TO |
418 | 'column' => 'civicrm_address.street_number%2', |
419 | 'order' => 'ASC', | |
420 | ), | |
9d72cede | 421 | 3 => array( |
6a488035 TO |
422 | 'column' => 'civicrm_address.street_number', |
423 | 'order' => 'ASC', | |
424 | ), | |
9d72cede | 425 | 4 => array( |
6a488035 TO |
426 | 'column' => 'contact_a.sort_name', |
427 | 'order' => 'ASC', | |
428 | ), | |
429 | ); | |
430 | } | |
431 | else { | |
acb1052e WA |
432 | $defaults['order_bys'] |
433 | = array( | |
a130e045 DG |
434 | 1 => array( |
435 | 'column' => 'contact_a.sort_name', | |
436 | 'order' => 'ASC', | |
437 | ), | |
6a488035 TO |
438 | ); |
439 | } | |
440 | return $defaults; | |
441 | } | |
442 | ||
443 | /** | |
fe482240 | 444 | * Process the form after the input has been submitted and validated. |
6a488035 | 445 | * |
6a488035 | 446 | * |
355ba699 | 447 | * @return void |
6a488035 TO |
448 | */ |
449 | public function postProcess() { | |
450 | $buttonName = $this->controller->getButtonName(); | |
451 | if ($buttonName == '_qf_Interview_done_interviewToReserve') { | |
452 | //hey its time to stop cycle. | |
453 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/survey/search', 'reset=1&op=reserve')); | |
454 | } | |
455 | elseif ($buttonName == '_qf_Interview_next_interviewToRelease') { | |
456 | //get ready to jump to release form. | |
457 | foreach (array( | |
353ffa53 TO |
458 | 'surveyId', |
459 | 'contactIds', | |
a130e045 | 460 | 'interviewerId', |
353ffa53 | 461 | ) as $fld) { |
6a488035 TO |
462 | $this->controller->set($fld, $this->{"_$fld"}); |
463 | } | |
464 | $this->controller->set('interviewToRelease', TRUE); | |
465 | } | |
466 | ||
467 | // vote is done through ajax | |
6a488035 TO |
468 | } |
469 | ||
30c4e065 | 470 | /** |
c490a46a | 471 | * @param array $params |
30c4e065 EM |
472 | * |
473 | * @return mixed | |
474 | */ | |
00be9182 | 475 | public static function registerInterview($params) { |
6a488035 TO |
476 | $activityId = CRM_Utils_Array::value('activity_id', $params); |
477 | $surveyTypeId = CRM_Utils_Array::value('activity_type_id', $params); | |
478 | if (!is_array($params) || !$surveyTypeId || !$activityId) { | |
479 | return FALSE; | |
480 | } | |
481 | ||
482 | static $surveyFields; | |
483 | if (!is_array($surveyFields)) { | |
484 | $surveyFields = CRM_Core_BAO_CustomField::getFields('Activity', | |
485 | FALSE, | |
486 | FALSE, | |
487 | $surveyTypeId, | |
488 | NULL, | |
489 | FALSE, | |
490 | TRUE | |
491 | ); | |
492 | } | |
493 | ||
494 | static $statusId; | |
495 | if (!$statusId) { | |
496 | $statusId = array_search('Completed', CRM_Core_PseudoConstant::activityStatus('name')); | |
497 | } | |
498 | ||
499 | //format custom fields. | |
500 | $customParams = CRM_Core_BAO_CustomField::postProcess($params, | |
501 | $surveyFields, | |
502 | $activityId, | |
503 | 'Activity' | |
504 | ); | |
505 | ||
506 | CRM_Core_BAO_CustomValueTable::store($customParams, 'civicrm_activity', $activityId); | |
507 | ||
508 | //process contact data. | |
509 | $contactParams = $fields = array(); | |
510 | ||
511 | $contactFieldTypes = array_merge(array('Contact'), CRM_Contact_BAO_ContactType::basicTypes()); | |
512 | $responseFields = CRM_Campaign_BAO_Survey::getSurveyResponseFields($params['survey_id']); | |
513 | if (!empty($responseFields)) { | |
514 | foreach ($params as $key => $value) { | |
515 | if (array_key_exists($key, $responseFields)) { | |
516 | if (in_array($responseFields[$key]['field_type'], $contactFieldTypes)) { | |
517 | $fields[$key] = $responseFields[$key]; | |
518 | $contactParams[$key] = $value; | |
519 | if (isset($params["{$key}_id"])) { | |
520 | $contactParams["{$key}_id"] = $params["{$key}_id"]; | |
521 | } | |
522 | } | |
523 | } | |
524 | } | |
525 | } | |
526 | ||
527 | $contactId = CRM_Utils_Array::value('voter_id', $params); | |
528 | if ($contactId && !empty($contactParams)) { | |
529 | CRM_Contact_BAO_Contact::createProfileContact($contactParams, $fields, $contactId); | |
530 | } | |
531 | ||
532 | //update activity record. | |
533 | $activity = new CRM_Activity_DAO_Activity(); | |
534 | $activity->id = $activityId; | |
535 | ||
536 | $activity->selectAdd(); | |
537 | $activity->selectAdd('activity_date_time, status_id, result, subject'); | |
538 | $activity->find(TRUE); | |
539 | $activity->activity_date_time = date('YmdHis'); | |
540 | $activity->status_id = $statusId; | |
541 | ||
a7488080 | 542 | if (!empty($params['activity_date_time'])) { |
5ee01db2 | 543 | $activity->activity_date_time = CRM_Utils_Date::processDate($params['activity_date_time'], $params['activity_date_time_time']); |
6a488035 TO |
544 | } |
545 | ||
546 | $subject = ''; | |
547 | $surveyTitle = CRM_Utils_Array::value('surveyTitle', $params); | |
548 | if ($surveyTitle) { | |
549 | $subject = ts('%1', array(1 => $surveyTitle)); | |
550 | $subject .= ' - '; | |
551 | } | |
552 | $subject .= ts('Respondent Interview'); | |
553 | ||
554 | $activity->subject = $subject; | |
5ee01db2 RN |
555 | $activityParams = array( |
556 | 'details' => 'details', | |
557 | 'result' => 'result', | |
8ef12e64 | 558 | 'engagement_level' => 'activity_engagement_level', |
5ee01db2 RN |
559 | 'subject' => 'activity_subject', |
560 | 'status_id' => 'activity_status_id', | |
8ef12e64 | 561 | 'source_contact_id' => 'source_contact', |
562 | 'location' => 'activity_location', | |
5ee01db2 | 563 | 'campaign_id' => 'activity_campaign_id', |
21dfd5f5 | 564 | 'duration' => 'activity_duration', |
5ee01db2 RN |
565 | ); |
566 | foreach ($activityParams as $key => $field) { | |
a7488080 | 567 | if (!empty($params[$field])) { |
5ee01db2 RN |
568 | $activity->$key = $params[$field]; |
569 | } | |
570 | } | |
8ef12e64 | 571 | |
6a488035 TO |
572 | $activity->save(); |
573 | //really this should use Activity BAO& not be here but refactoring will have to be later | |
574 | //actually the whole ajax call could be done as an api ajax call & post hook would be sorted | |
575 | CRM_Utils_Hook::post('edit', 'Activity', $activity->id, $activity); | |
576 | $activity->free(); | |
577 | ||
578 | return $activityId; | |
579 | } | |
580 | ||
00be9182 | 581 | public function getVoterIds() { |
6a488035 TO |
582 | if (!$this->_interviewerId) { |
583 | $session = CRM_Core_Session::singleton(); | |
584 | $this->_interviewerId = $session->get('userID'); | |
585 | } | |
586 | if (!$this->_surveyId) { | |
587 | // use default survey id | |
353ffa53 TO |
588 | $dao = new CRM_Campaign_DAO_Survey(); |
589 | $dao->is_active = 1; | |
6a488035 TO |
590 | $dao->is_default = 1; |
591 | $dao->find(TRUE); | |
592 | $this->_surveyId = $dao->id; | |
593 | } | |
594 | ||
595 | $this->_contactIds = $this->get('contactIds'); | |
596 | if (!is_array($this->_contactIds)) { | |
597 | //get the survey activities. | |
598 | $activityStatus = CRM_Core_PseudoConstant::activityStatus('name'); | |
599 | $statusIds = array(); | |
600 | if ($statusId = array_search('Scheduled', $activityStatus)) { | |
601 | $statusIds[] = $statusId; | |
602 | } | |
603 | $surveyActivities = CRM_Campaign_BAO_Survey::getSurveyVoterInfo($this->_surveyId, | |
604 | $this->_interviewerId, | |
605 | $statusIds | |
606 | ); | |
607 | $this->_contactIds = array(); | |
5c2ea586 TO |
608 | foreach ($surveyActivities as $val) { |
609 | $this->_contactIds[$val['voter_id']] = $val['voter_id']; | |
94880218 | 610 | } |
6a488035 TO |
611 | $this->set('contactIds', $this->_contactIds); |
612 | } | |
613 | } | |
614 | ||
00be9182 | 615 | public function filterVoterIds() { |
6a488035 TO |
616 | //do the cleanup later on. |
617 | if (!is_array($this->_contactIds)) { | |
618 | return; | |
619 | } | |
620 | ||
621 | $profileId = CRM_Campaign_BAO_Survey::getSurveyProfileId($this->_surveyId); | |
622 | if ($profileId) { | |
623 | $profileType = CRM_Core_BAO_UFField::getProfileType($profileId); | |
624 | if (in_array($profileType, CRM_Contact_BAO_ContactType::basicTypes())) { | |
625 | $voterIdCount = count($this->_contactIds); | |
626 | ||
627 | //create temporary table to store voter ids. | |
628 | $tempTableName = CRM_Core_DAO::createTempTableName('civicrm_survey_respondent'); | |
629 | CRM_Core_DAO::executeQuery("DROP TABLE IF EXISTS {$tempTableName}"); | |
630 | $query = " | |
631 | CREATE TEMPORARY TABLE {$tempTableName} ( | |
632 | id int unsigned NOT NULL AUTO_INCREMENT, | |
8ef12e64 | 633 | survey_contact_id int unsigned NOT NULL, |
6a488035 TO |
634 | PRIMARY KEY ( id ) |
635 | ); | |
636 | "; | |
637 | CRM_Core_DAO::executeQuery($query); | |
638 | $batch = 100; | |
639 | $insertedCount = 0; | |
640 | do { | |
641 | $processIds = $this->_contactIds; | |
642 | $insertIds = array_splice($processIds, $insertedCount, $batch); | |
643 | if (!empty($insertIds)) { | |
8ef12e64 | 644 | $insertSQL = "INSERT IGNORE INTO {$tempTableName}( survey_contact_id ) |
6a488035 TO |
645 | VALUES (" . implode('),(', $insertIds) . ');'; |
646 | CRM_Core_DAO::executeQuery($insertSQL); | |
647 | } | |
648 | $insertedCount += $batch; | |
649 | } while ($insertedCount < $voterIdCount); | |
650 | ||
651 | $query = " | |
652 | SELECT contact.id as id | |
8ef12e64 | 653 | FROM civicrm_contact contact |
6a488035 TO |
654 | INNER JOIN {$tempTableName} ON ( {$tempTableName}.survey_contact_id = contact.id ) |
655 | WHERE contact.contact_type != %1"; | |
656 | $removeContact = CRM_Core_DAO::executeQuery($query, | |
657 | array(1 => array($profileType, 'String')) | |
658 | ); | |
659 | while ($removeContact->fetch()) { | |
660 | unset($this->_contactIds[$removeContact->id]); | |
661 | } | |
662 | } | |
663 | } | |
664 | } | |
96025800 | 665 | |
6a488035 | 666 | } |