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