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