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 | */ | |
52 | private $_userContext; | |
53 | ||
54 | private $_groupTree; | |
55 | ||
56 | private $_surveyFields; | |
57 | ||
58 | private $_surveyTypeId; | |
59 | ||
60 | private $_interviewerId; | |
61 | ||
62 | private $_surveyActivityIds; | |
63 | ||
64 | private $_votingTab = FALSE; | |
65 | ||
66 | private $_surveyValues; | |
67 | ||
68 | private $_resultOptions; | |
69 | ||
70 | private $_allowAjaxReleaseButton; | |
71 | ||
72 | /** | |
100fef9d | 73 | * Build all the data structures needed to build the form |
6a488035 TO |
74 | * |
75 | * @return void | |
76 | * @access public | |
77 | */ | |
78 | function preProcess() { | |
79 | $this->_votingTab = $this->get('votingTab'); | |
80 | $this->_reserveToInterview = $this->get('reserveToInterview'); | |
81 | if ($this->_reserveToInterview || $this->_votingTab) { | |
82 | //user came from voting tab / reserve form. | |
83 | foreach (array( | |
84 | 'surveyId', 'contactIds', 'interviewerId') as $fld) { | |
85 | $this->{"_$fld"} = $this->get($fld); | |
86 | } | |
87 | //get the target voter ids. | |
88 | if ($this->_votingTab) { | |
89 | $this->getVoterIds(); | |
90 | } | |
91 | } | |
92 | else { | |
93 | parent::preProcess(); | |
94 | //get the survey id from user submitted values. | |
95 | $this->_surveyId = CRM_Utils_Array::value('campaign_survey_id', $this->get('formValues')); | |
96 | $this->_interviewerId = CRM_Utils_Array::value('survey_interviewer_id', $this->get('formValues')); | |
97 | } | |
98 | ||
99 | if ( $this->_surveyId ) { | |
100 | $params = array('id' => $this->_surveyId); | |
101 | CRM_Campaign_BAO_Survey::retrieve($params, $this->_surveyDetails); | |
102 | } | |
103 | ||
104 | $orderClause = false; | |
105 | $buttonName = $this->controller->getButtonName(); | |
8cc574cf | 106 | if ( $buttonName == '_qf_Interview_submit_orderBy' && !empty($_POST['order_bys'])) { |
6a488035 TO |
107 | $orderByParams = CRM_Utils_Array::value('order_bys', $_POST); |
108 | } | |
109 | elseif ( CRM_Core_OptionGroup::getValue('activity_type','WalkList') == $this->_surveyDetails['activity_type_id'] ) { | |
110 | $orderByParams = | |
111 | array( | |
9d72cede | 112 | 1 => array( |
6a488035 TO |
113 | 'column' => 'civicrm_address.street_name', |
114 | 'order' => 'ASC', | |
115 | ), | |
9d72cede | 116 | 2 => array( |
6a488035 TO |
117 | 'column' => 'civicrm_address.street_number%2', |
118 | 'order' => 'ASC', | |
119 | ), | |
9d72cede | 120 | 3 => array( |
6a488035 TO |
121 | 'column' => 'civicrm_address.street_number', |
122 | 'order' => 'ASC', | |
123 | ), | |
9d72cede | 124 | 4 => array( |
6a488035 TO |
125 | 'column' => 'contact_a.sort_name', |
126 | 'order' => 'ASC', | |
127 | ), | |
128 | ); | |
129 | } | |
130 | ||
131 | $orderBy = array(); | |
132 | if ( !empty($orderByParams) ) { | |
133 | foreach ( $orderByParams as $key => $val ) { | |
a7488080 | 134 | if (!empty($val['column'])) { |
6a488035 TO |
135 | $orderBy[] = "{$val['column']} {$val['order']}"; |
136 | } | |
137 | } | |
138 | if ( !empty($orderBy) ) { | |
139 | $orderClause = "ORDER BY " . implode(', ', $orderBy); | |
140 | } | |
141 | } | |
142 | ||
143 | $this->_contactIds = array_unique($this->_contactIds); | |
144 | if (!empty($this->_contactIds) && $orderClause) { | |
145 | $clause = 'contact_a.id IN ( ' . implode(',', $this->_contactIds) . ' ) '; | |
146 | $sql = " | |
147 | SELECT contact_a.id | |
148 | FROM civicrm_contact contact_a | |
149 | LEFT JOIN civicrm_address ON contact_a.id = civicrm_address.contact_id | |
150 | WHERE {$clause} | |
151 | {$orderClause}"; | |
152 | ||
153 | $this->_contactIds = array(); | |
154 | $dao = CRM_Core_DAO::executeQuery($sql); | |
155 | while ($dao->fetch()) { | |
156 | $this->_contactIds[] = $dao->id; | |
157 | } | |
158 | } | |
159 | ||
160 | ||
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) { | |
182 | $actVals = CRM_Utils_Array::value($voterId, $this->_surveyActivityIds); | |
183 | $statusId = CRM_Utils_Array::value('status_id', $actVals); | |
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); | |
233 | $this->assign("instanceId",$result); | |
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 | ||
260 | function validateIds() { | |
261 | $required = array('surveyId' => ts('Could not find Survey.'), | |
262 | 'interviewerId' => ts('Could not find Interviewer.'), | |
263 | 'contactIds' => ts('No respondents are currently reserved for you to interview.'), | |
264 | 'resultOptions' => ts('Oops. It looks like there is no response option configured.'), | |
265 | ); | |
266 | ||
267 | $errorMessages = array(); | |
268 | foreach ($required as $fld => $msg) { | |
269 | if (empty($this->{"_$fld"})) { | |
270 | if (!$this->_votingTab) { | |
271 | CRM_Core_Error::statusBounce($msg); | |
272 | break; | |
273 | } | |
274 | $errorMessages[] = $msg; | |
275 | } | |
276 | } | |
277 | ||
278 | $this->assign('errorMessages', empty($errorMessages) ? FALSE : $errorMessages); | |
279 | } | |
280 | ||
281 | /** | |
c490a46a | 282 | * Build the form object |
6a488035 TO |
283 | * |
284 | * @access public | |
285 | * | |
286 | * @return void | |
287 | */ | |
288 | function buildQuickForm() { | |
289 | $this->assign('surveyTypeId', $this->_surveyTypeId); | |
290 | ||
291 | $options = | |
292 | array('' => ' - none - ', | |
293 | 'civicrm_address.street_name' => 'Street Name', | |
294 | 'civicrm_address.street_number%2' => 'Odd / Even Street Number', | |
295 | 'civicrm_address.street_number' => 'Street Number', | |
296 | 'contact_a.sort_name' => 'Respondent Name', | |
297 | ); | |
298 | for ($i = 1; $i < count($options); $i++) { | |
299 | $this->addElement('select', "order_bys[{$i}][column]", ts('Order by Column'), $options); | |
300 | $this->addElement('select', "order_bys[{$i}][order]", ts('Order by Order'), array('ASC' => 'Ascending', 'DESC' => 'Descending')); | |
301 | } | |
302 | ||
303 | //pickup the uf fields. | |
304 | $this->_surveyFields = CRM_Campaign_BAO_Survey::getSurveyResponseFields($this->_surveyId, | |
305 | $this->_surveyTypeId | |
306 | ); | |
307 | ||
308 | foreach ($this->_contactIds as $contactId) { | |
309 | //build the profile fields. | |
310 | foreach ($this->_surveyFields as $name => $field) { | |
311 | if ($field){ | |
312 | CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $contactId); | |
313 | } | |
314 | } | |
315 | ||
316 | //build the result field. | |
317 | if (!empty($this->_resultOptions)) { | |
318 | $this->add('select', "field[$contactId][result]", ts('Result'), | |
319 | array( | |
320 | '' => ts('- select -')) + | |
321 | array_combine($this->_resultOptions, $this->_resultOptions) | |
322 | ); | |
323 | } | |
324 | ||
325 | $this->add('text', "field[{$contactId}][note]", ts('Note')); | |
326 | ||
327 | //need to keep control for release/reserve. | |
328 | if ($this->_allowAjaxReleaseButton) { | |
329 | $this->addElement('hidden', | |
330 | "field[{$contactId}][is_release_or_reserve]", 0, | |
331 | array('id' => "field_{$contactId}_is_release_or_reserve") | |
332 | ); | |
333 | } | |
334 | } | |
335 | $this->assign('surveyFields', empty($this->_surveyFields) ? FALSE : $this->_surveyFields); | |
336 | ||
337 | //no need to get qf buttons. | |
338 | if ($this->_votingTab) { | |
339 | return; | |
340 | } | |
341 | ||
342 | $buttons = array( | |
343 | array('type' => 'cancel', | |
344 | 'name' => ts('Done'), | |
345 | 'subName' => 'interview', | |
346 | 'isDefault' => TRUE, | |
347 | )); | |
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 TO |
383 | * |
384 | * @access public | |
385 | * | |
355ba699 | 386 | * @return void |
6a488035 TO |
387 | */ |
388 | function setDefaultValues() { | |
389 | //load default data for only contact fields. | |
390 | $contactFields = $defaults = array(); | |
391 | foreach ($this->_surveyFields as $name => $field) { | |
392 | $acceptable_types = CRM_Contact_BAO_ContactType::basicTypes(); | |
393 | $acceptable_types[] = 'Contact'; | |
394 | if (in_array($field['field_type'], $acceptable_types)) { | |
395 | $contactFields[$name] = $field; | |
396 | } | |
397 | } | |
398 | if (!empty($contactFields)) { | |
399 | foreach ($this->_contactIds as $contactId) { | |
400 | CRM_Core_BAO_UFGroup::setProfileDefaults($contactId, $contactFields, $defaults, FALSE); | |
401 | } | |
402 | } | |
403 | ||
404 | if ( CRM_Core_OptionGroup::getValue('activity_type','WalkList') == $this->_surveyDetails['activity_type_id'] ) { | |
405 | $defaults['order_bys'] = | |
406 | array( | |
9d72cede | 407 | 1 => array( |
6a488035 TO |
408 | 'column' => 'civicrm_address.street_name', |
409 | 'order' => 'ASC', | |
410 | ), | |
9d72cede | 411 | 2 => array( |
6a488035 TO |
412 | 'column' => 'civicrm_address.street_number%2', |
413 | 'order' => 'ASC', | |
414 | ), | |
9d72cede | 415 | 3 => array( |
6a488035 TO |
416 | 'column' => 'civicrm_address.street_number', |
417 | 'order' => 'ASC', | |
418 | ), | |
9d72cede | 419 | 4 => array( |
6a488035 TO |
420 | 'column' => 'contact_a.sort_name', |
421 | 'order' => 'ASC', | |
422 | ), | |
423 | ); | |
424 | } | |
425 | else { | |
426 | $defaults['order_bys'] = | |
427 | array( | |
428 | 1 => | |
429 | array( | |
430 | 'column' => 'contact_a.sort_name', | |
431 | 'order' => 'ASC', | |
432 | ), | |
433 | ); | |
434 | } | |
435 | return $defaults; | |
436 | } | |
437 | ||
438 | /** | |
100fef9d | 439 | * Process the form after the input has been submitted and validated |
6a488035 TO |
440 | * |
441 | * @access public | |
442 | * | |
355ba699 | 443 | * @return void |
6a488035 TO |
444 | */ |
445 | public function postProcess() { | |
446 | $buttonName = $this->controller->getButtonName(); | |
447 | if ($buttonName == '_qf_Interview_done_interviewToReserve') { | |
448 | //hey its time to stop cycle. | |
449 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/survey/search', 'reset=1&op=reserve')); | |
450 | } | |
451 | elseif ($buttonName == '_qf_Interview_next_interviewToRelease') { | |
452 | //get ready to jump to release form. | |
453 | foreach (array( | |
454 | 'surveyId', 'contactIds', 'interviewerId') as $fld) { | |
455 | $this->controller->set($fld, $this->{"_$fld"}); | |
456 | } | |
457 | $this->controller->set('interviewToRelease', TRUE); | |
458 | } | |
459 | ||
460 | // vote is done through ajax | |
461 | return; | |
462 | } | |
463 | ||
30c4e065 | 464 | /** |
c490a46a | 465 | * @param array $params |
30c4e065 EM |
466 | * |
467 | * @return mixed | |
468 | */ | |
6a488035 TO |
469 | static function registerInterview($params) { |
470 | $activityId = CRM_Utils_Array::value('activity_id', $params); | |
471 | $surveyTypeId = CRM_Utils_Array::value('activity_type_id', $params); | |
472 | if (!is_array($params) || !$surveyTypeId || !$activityId) { | |
473 | return FALSE; | |
474 | } | |
475 | ||
476 | static $surveyFields; | |
477 | if (!is_array($surveyFields)) { | |
478 | $surveyFields = CRM_Core_BAO_CustomField::getFields('Activity', | |
479 | FALSE, | |
480 | FALSE, | |
481 | $surveyTypeId, | |
482 | NULL, | |
483 | FALSE, | |
484 | TRUE | |
485 | ); | |
486 | } | |
487 | ||
488 | static $statusId; | |
489 | if (!$statusId) { | |
490 | $statusId = array_search('Completed', CRM_Core_PseudoConstant::activityStatus('name')); | |
491 | } | |
492 | ||
493 | //format custom fields. | |
494 | $customParams = CRM_Core_BAO_CustomField::postProcess($params, | |
495 | $surveyFields, | |
496 | $activityId, | |
497 | 'Activity' | |
498 | ); | |
499 | ||
500 | CRM_Core_BAO_CustomValueTable::store($customParams, 'civicrm_activity', $activityId); | |
501 | ||
502 | //process contact data. | |
503 | $contactParams = $fields = array(); | |
504 | ||
505 | $contactFieldTypes = array_merge(array('Contact'), CRM_Contact_BAO_ContactType::basicTypes()); | |
506 | $responseFields = CRM_Campaign_BAO_Survey::getSurveyResponseFields($params['survey_id']); | |
507 | if (!empty($responseFields)) { | |
508 | foreach ($params as $key => $value) { | |
509 | if (array_key_exists($key, $responseFields)) { | |
510 | if (in_array($responseFields[$key]['field_type'], $contactFieldTypes)) { | |
511 | $fields[$key] = $responseFields[$key]; | |
512 | $contactParams[$key] = $value; | |
513 | if (isset($params["{$key}_id"])) { | |
514 | $contactParams["{$key}_id"] = $params["{$key}_id"]; | |
515 | } | |
516 | } | |
517 | } | |
518 | } | |
519 | } | |
520 | ||
521 | $contactId = CRM_Utils_Array::value('voter_id', $params); | |
522 | if ($contactId && !empty($contactParams)) { | |
523 | CRM_Contact_BAO_Contact::createProfileContact($contactParams, $fields, $contactId); | |
524 | } | |
525 | ||
526 | //update activity record. | |
527 | $activity = new CRM_Activity_DAO_Activity(); | |
528 | $activity->id = $activityId; | |
529 | ||
530 | $activity->selectAdd(); | |
531 | $activity->selectAdd('activity_date_time, status_id, result, subject'); | |
532 | $activity->find(TRUE); | |
533 | $activity->activity_date_time = date('YmdHis'); | |
534 | $activity->status_id = $statusId; | |
535 | ||
a7488080 | 536 | if (!empty($params['activity_date_time'])) { |
5ee01db2 | 537 | $activity->activity_date_time = CRM_Utils_Date::processDate($params['activity_date_time'], $params['activity_date_time_time']); |
6a488035 TO |
538 | } |
539 | ||
540 | $subject = ''; | |
541 | $surveyTitle = CRM_Utils_Array::value('surveyTitle', $params); | |
542 | if ($surveyTitle) { | |
543 | $subject = ts('%1', array(1 => $surveyTitle)); | |
544 | $subject .= ' - '; | |
545 | } | |
546 | $subject .= ts('Respondent Interview'); | |
547 | ||
548 | $activity->subject = $subject; | |
5ee01db2 RN |
549 | $activityParams = array( |
550 | 'details' => 'details', | |
551 | 'result' => 'result', | |
8ef12e64 | 552 | 'engagement_level' => 'activity_engagement_level', |
5ee01db2 RN |
553 | 'subject' => 'activity_subject', |
554 | 'status_id' => 'activity_status_id', | |
8ef12e64 | 555 | 'source_contact_id' => 'source_contact', |
556 | 'location' => 'activity_location', | |
5ee01db2 RN |
557 | 'campaign_id' => 'activity_campaign_id', |
558 | 'duration' => 'activity_duration' | |
559 | ); | |
560 | foreach ($activityParams as $key => $field) { | |
a7488080 | 561 | if (!empty($params[$field])) { |
5ee01db2 RN |
562 | $activity->$key = $params[$field]; |
563 | } | |
564 | } | |
8ef12e64 | 565 | |
6a488035 TO |
566 | $activity->save(); |
567 | //really this should use Activity BAO& not be here but refactoring will have to be later | |
568 | //actually the whole ajax call could be done as an api ajax call & post hook would be sorted | |
569 | CRM_Utils_Hook::post('edit', 'Activity', $activity->id, $activity); | |
570 | $activity->free(); | |
571 | ||
572 | return $activityId; | |
573 | } | |
574 | ||
575 | function getVoterIds() { | |
576 | if (!$this->_interviewerId) { | |
577 | $session = CRM_Core_Session::singleton(); | |
578 | $this->_interviewerId = $session->get('userID'); | |
579 | } | |
580 | if (!$this->_surveyId) { | |
581 | // use default survey id | |
582 | $dao = new CRM_Campaign_DAO_Survey(); | |
583 | $dao->is_active = 1; | |
584 | $dao->is_default = 1; | |
585 | $dao->find(TRUE); | |
586 | $this->_surveyId = $dao->id; | |
587 | } | |
588 | ||
589 | $this->_contactIds = $this->get('contactIds'); | |
590 | if (!is_array($this->_contactIds)) { | |
591 | //get the survey activities. | |
592 | $activityStatus = CRM_Core_PseudoConstant::activityStatus('name'); | |
593 | $statusIds = array(); | |
594 | if ($statusId = array_search('Scheduled', $activityStatus)) { | |
595 | $statusIds[] = $statusId; | |
596 | } | |
597 | $surveyActivities = CRM_Campaign_BAO_Survey::getSurveyVoterInfo($this->_surveyId, | |
598 | $this->_interviewerId, | |
599 | $statusIds | |
600 | ); | |
601 | $this->_contactIds = array(); | |
602 | foreach ($surveyActivities as $val) $this->_contactIds[$val['voter_id']] = $val['voter_id']; | |
603 | $this->set('contactIds', $this->_contactIds); | |
604 | } | |
605 | } | |
606 | ||
607 | function filterVoterIds() { | |
608 | //do the cleanup later on. | |
609 | if (!is_array($this->_contactIds)) { | |
610 | return; | |
611 | } | |
612 | ||
613 | $profileId = CRM_Campaign_BAO_Survey::getSurveyProfileId($this->_surveyId); | |
614 | if ($profileId) { | |
615 | $profileType = CRM_Core_BAO_UFField::getProfileType($profileId); | |
616 | if (in_array($profileType, CRM_Contact_BAO_ContactType::basicTypes())) { | |
617 | $voterIdCount = count($this->_contactIds); | |
618 | ||
619 | //create temporary table to store voter ids. | |
620 | $tempTableName = CRM_Core_DAO::createTempTableName('civicrm_survey_respondent'); | |
621 | CRM_Core_DAO::executeQuery("DROP TABLE IF EXISTS {$tempTableName}"); | |
622 | $query = " | |
623 | CREATE TEMPORARY TABLE {$tempTableName} ( | |
624 | id int unsigned NOT NULL AUTO_INCREMENT, | |
8ef12e64 | 625 | survey_contact_id int unsigned NOT NULL, |
6a488035 TO |
626 | PRIMARY KEY ( id ) |
627 | ); | |
628 | "; | |
629 | CRM_Core_DAO::executeQuery($query); | |
630 | $batch = 100; | |
631 | $insertedCount = 0; | |
632 | do { | |
633 | $processIds = $this->_contactIds; | |
634 | $insertIds = array_splice($processIds, $insertedCount, $batch); | |
635 | if (!empty($insertIds)) { | |
8ef12e64 | 636 | $insertSQL = "INSERT IGNORE INTO {$tempTableName}( survey_contact_id ) |
6a488035 TO |
637 | VALUES (" . implode('),(', $insertIds) . ');'; |
638 | CRM_Core_DAO::executeQuery($insertSQL); | |
639 | } | |
640 | $insertedCount += $batch; | |
641 | } while ($insertedCount < $voterIdCount); | |
642 | ||
643 | $query = " | |
644 | SELECT contact.id as id | |
8ef12e64 | 645 | FROM civicrm_contact contact |
6a488035 TO |
646 | INNER JOIN {$tempTableName} ON ( {$tempTableName}.survey_contact_id = contact.id ) |
647 | WHERE contact.contact_type != %1"; | |
648 | $removeContact = CRM_Core_DAO::executeQuery($query, | |
649 | array(1 => array($profileType, 'String')) | |
650 | ); | |
651 | while ($removeContact->fetch()) { | |
652 | unset($this->_contactIds[$removeContact->id]); | |
653 | } | |
654 | } | |
655 | } | |
656 | } | |
657 | } | |
658 |