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 | require_once 'CRM/Campaign/DAO/Survey.php'; | |
4c6ce474 EM |
36 | |
37 | /** | |
38 | * Class CRM_Campaign_BAO_Survey | |
39 | */ | |
6a488035 TO |
40 | class CRM_Campaign_BAO_Survey extends CRM_Campaign_DAO_Survey { |
41 | ||
42 | /** | |
43 | * Takes a bunch of params that are needed to match certain criteria and | |
44 | * retrieves the relevant objects. Typically the valid params are only | |
45 | * campaign_id. | |
46 | * | |
7aaf6db0 TO |
47 | * @param array $params |
48 | * (reference ) an assoc array of name/value pairs. | |
49 | * @param array $defaults | |
50 | * (reference ) an assoc array to hold the flattened values. | |
6a488035 TO |
51 | */ |
52 | ||
53 | /** | |
54 | * The action links that we need to display for the browse screen | |
55 | * | |
c490a46a | 56 | * @param array $params |
77b97be7 EM |
57 | * @param $defaults |
58 | * | |
c490a46a | 59 | * @return CRM_Campaign_DAO_Survey|null |
6a488035 | 60 | */ |
00be9182 | 61 | public static function retrieve(&$params, &$defaults) { |
6a488035 TO |
62 | $dao = new CRM_Campaign_DAO_Survey(); |
63 | ||
64 | $dao->copyValues($params); | |
65 | ||
66 | if ($dao->find(TRUE)) { | |
67 | CRM_Core_DAO::storeValues($dao, $defaults); | |
68 | return $dao; | |
69 | } | |
70 | return NULL; | |
71 | } | |
72 | ||
73 | /** | |
100fef9d | 74 | * Takes an associative array and creates a Survey object |
6a488035 TO |
75 | * |
76 | * the function extract all the params it needs to initialize the create a | |
77 | * survey object. | |
78 | * | |
79 | * | |
c490a46a | 80 | * @param array $params |
77b97be7 | 81 | * |
16b10e64 | 82 | * @return CRM_Survey_DAO_Survey |
6a488035 | 83 | */ |
00be9182 | 84 | public static function create(&$params) { |
6a488035 | 85 | if (empty($params)) { |
5c2ea586 | 86 | return FALSE; |
6a488035 TO |
87 | } |
88 | ||
a7488080 | 89 | if (!empty($params['is_default'])) { |
6a488035 TO |
90 | $query = "UPDATE civicrm_survey SET is_default = 0"; |
91 | CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray); | |
92 | } | |
93 | ||
94 | if (!(CRM_Utils_Array::value('id', $params))) { | |
95 | ||
96 | if (!(CRM_Utils_Array::value('created_id', $params))) { | |
97 | $session = CRM_Core_Session::singleton(); | |
98 | $params['created_id'] = $session->get('userID'); | |
99 | } | |
100 | if (!(CRM_Utils_Array::value('created_date', $params))) { | |
101 | $params['created_date'] = date('YmdHis'); | |
102 | } | |
103 | } | |
104 | ||
105 | $dao = new CRM_Campaign_DAO_Survey(); | |
106 | $dao->copyValues($params); | |
107 | $dao->save(); | |
108 | ||
a7488080 | 109 | if (!empty($params['custom']) && |
6a488035 TO |
110 | is_array($params['custom']) |
111 | ) { | |
112 | CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_survey', $dao->id); | |
113 | } | |
114 | return $dao; | |
115 | } | |
116 | ||
117 | /** | |
100fef9d | 118 | * Retrieve surveys for dashboard. |
6a488035 | 119 | * |
6a488035 | 120 | */ |
00be9182 | 121 | public static function getSurveySummary($params = array(), $onlyCount = FALSE) { |
6a488035 TO |
122 | //build the limit and order clause. |
123 | $limitClause = $orderByClause = $lookupTableJoins = NULL; | |
124 | if (!$onlyCount) { | |
125 | $sortParams = array( | |
126 | 'sort' => 'created_date', | |
127 | 'offset' => 0, | |
128 | 'rowCount' => 10, | |
129 | 'sortOrder' => 'desc', | |
130 | ); | |
131 | foreach ($sortParams as $name => $default) { | |
a7488080 | 132 | if (!empty($params[$name])) { |
6a488035 TO |
133 | $sortParams[$name] = $params[$name]; |
134 | } | |
135 | } | |
136 | ||
137 | //need to lookup tables. | |
138 | $orderOnSurveyTable = TRUE; | |
139 | if ($sortParams['sort'] == 'campaign') { | |
140 | $orderOnSurveyTable = FALSE; | |
141 | $lookupTableJoins = ' | |
142 | LEFT JOIN civicrm_campaign campaign ON ( campaign.id = survey.campaign_id )'; | |
143 | $orderByClause = "ORDER BY campaign.title {$sortParams['sortOrder']}"; | |
144 | } | |
145 | elseif ($sortParams['sort'] == 'activity_type') { | |
146 | $orderOnSurveyTable = FALSE; | |
147 | $lookupTableJoins = " | |
91da6cd5 | 148 | LEFT JOIN civicrm_option_value activity_type ON ( activity_type.value = survey.activity_type_id |
6a488035 TO |
149 | OR survey.activity_type_id IS NULL ) |
150 | INNER JOIN civicrm_option_group grp ON ( activity_type.option_group_id = grp.id AND grp.name = 'activity_type' )"; | |
151 | $orderByClause = "ORDER BY activity_type.label {$sortParams['sortOrder']}"; | |
152 | } | |
153 | elseif ($sortParams['sort'] == 'isActive') { | |
154 | $sortParams['sort'] = 'is_active'; | |
155 | } | |
156 | if ($orderOnSurveyTable) { | |
157 | $orderByClause = "ORDER BY survey.{$sortParams['sort']} {$sortParams['sortOrder']}"; | |
158 | } | |
159 | $limitClause = "LIMIT {$sortParams['offset']}, {$sortParams['rowCount']}"; | |
160 | } | |
161 | ||
162 | //build the where clause. | |
163 | $queryParams = $where = array(); | |
164 | ||
165 | //we only have activity type as a | |
166 | //difference between survey and petition. | |
167 | $petitionTypeID = CRM_Core_OptionGroup::getValue('activity_type', 'petition', 'name'); | |
168 | if ($petitionTypeID) { | |
169 | $where[] = "( survey.activity_type_id != %1 )"; | |
170 | $queryParams[1] = array($petitionTypeID, 'Positive'); | |
171 | } | |
172 | ||
a7488080 | 173 | if (!empty($params['title'])) { |
6a488035 TO |
174 | $where[] = "( survey.title LIKE %2 )"; |
175 | $queryParams[2] = array('%' . trim($params['title']) . '%', 'String'); | |
176 | } | |
a7488080 | 177 | if (!empty($params['campaign_id'])) { |
6a488035 TO |
178 | $where[] = '( survey.campaign_id = %3 )'; |
179 | $queryParams[3] = array($params['campaign_id'], 'Positive'); | |
180 | } | |
a7488080 | 181 | if (!empty($params['activity_type_id'])) { |
6a488035 TO |
182 | $typeId = $params['activity_type_id']; |
183 | if (is_array($params['activity_type_id'])) { | |
184 | $typeId = implode(' , ', $params['activity_type_id']); | |
185 | } | |
186 | $where[] = "( survey.activity_type_id IN ( {$typeId} ) )"; | |
187 | } | |
188 | $whereClause = NULL; | |
189 | if (!empty($where)) { | |
190 | $whereClause = ' WHERE ' . implode(" \nAND ", $where); | |
191 | } | |
192 | ||
193 | $selectClause = ' | |
194 | SELECT survey.id as id, | |
195 | survey.title as title, | |
196 | survey.is_active as is_active, | |
197 | survey.result_id as result_id, | |
198 | survey.is_default as is_default, | |
199 | survey.campaign_id as campaign_id, | |
200 | survey.activity_type_id as activity_type_id, | |
201 | survey.release_frequency as release_frequency, | |
202 | survey.max_number_of_contacts as max_number_of_contacts, | |
203 | survey.default_number_of_contacts as default_number_of_contacts'; | |
204 | if ($onlyCount) { | |
205 | $selectClause = 'SELECT COUNT(*)'; | |
206 | } | |
207 | $fromClause = 'FROM civicrm_survey survey'; | |
208 | ||
209 | $query = "{$selectClause} {$fromClause} {$lookupTableJoins} {$whereClause} {$orderByClause} {$limitClause}"; | |
210 | ||
211 | //return only count. | |
212 | if ($onlyCount) { | |
5c2ea586 | 213 | return (int) CRM_Core_DAO::singleValueQuery($query, $queryParams); |
6a488035 TO |
214 | } |
215 | ||
216 | $surveys = array(); | |
217 | $properties = array( | |
353ffa53 TO |
218 | 'id', |
219 | 'title', | |
220 | 'campaign_id', | |
221 | 'is_active', | |
222 | 'is_default', | |
223 | 'result_id', | |
224 | 'activity_type_id', | |
225 | 'release_frequency', | |
226 | 'max_number_of_contacts', | |
227 | 'default_number_of_contacts', | |
6a488035 TO |
228 | ); |
229 | ||
230 | $survey = CRM_Core_DAO::executeQuery($query, $queryParams); | |
231 | while ($survey->fetch()) { | |
232 | foreach ($properties as $property) { | |
233 | $surveys[$survey->id][$property] = $survey->$property; | |
234 | } | |
235 | } | |
236 | ||
237 | return $surveys; | |
238 | } | |
239 | ||
240 | /** | |
241 | * Get the survey count. | |
242 | * | |
6a488035 | 243 | */ |
00be9182 | 244 | public static function getSurveyCount() { |
5c2ea586 | 245 | return (int) CRM_Core_DAO::singleValueQuery('SELECT COUNT(*) FROM civicrm_survey'); |
6a488035 TO |
246 | } |
247 | ||
248 | /** | |
100fef9d | 249 | * Get Surveys |
6a488035 | 250 | * |
7aaf6db0 TO |
251 | * @param bool $onlyActive |
252 | * Retrieve only active surveys. | |
253 | * @param bool $onlyDefault | |
254 | * Retrieve only default survey. | |
255 | * @param bool $forceAll | |
256 | * Retrieve all surveys. | |
257 | * @param bool $includePetition | |
258 | * Include or exclude petitions. | |
6a488035 | 259 | * |
6a488035 | 260 | */ |
5c2ea586 | 261 | public static function getSurveys($onlyActive = TRUE, $onlyDefault = FALSE, $forceAll = FALSE, $includePetition = FALSE) { |
6a488035 | 262 | $cacheKey = 0; |
9231464f | 263 | $cacheKeyParams = array('onlyActive', 'onlyDefault', 'forceAll', 'includePetition'); |
6a488035 TO |
264 | foreach ($cacheKeyParams as $param) { |
265 | $cacheParam = $$param; | |
266 | if (!$cacheParam) { | |
267 | $cacheParam = 0; | |
268 | } | |
269 | $cacheKey .= '_' . $cacheParam; | |
270 | } | |
271 | ||
272 | static $surveys; | |
273 | ||
274 | if (!isset($surveys[$cacheKey])) { | |
9231464f KJ |
275 | if (!$includePetition) { |
276 | //we only have activity type as a | |
277 | //difference between survey and petition. | |
278 | $petitionTypeID = CRM_Core_OptionGroup::getValue('activity_type', 'petition', 'name'); | |
6a488035 | 279 | |
9231464f KJ |
280 | $where = array(); |
281 | if ($petitionTypeID) { | |
282 | $where[] = "( survey.activity_type_id != {$petitionTypeID} )"; | |
283 | } | |
6a488035 TO |
284 | } |
285 | if (!$forceAll && $onlyActive) { | |
286 | $where[] = '( survey.is_active = 1 )'; | |
287 | } | |
288 | if (!$forceAll && $onlyDefault) { | |
289 | $where[] = '( survey.is_default = 1 )'; | |
290 | } | |
291 | $whereClause = implode(' AND ', $where); | |
292 | ||
293 | $query = " | |
294 | SELECT survey.id as id, | |
295 | survey.title as title | |
296 | FROM civicrm_survey as survey | |
297 | WHERE {$whereClause}"; | |
298 | $surveys[$cacheKey] = array(); | |
299 | $survey = CRM_Core_DAO::executeQuery($query); | |
300 | while ($survey->fetch()) { | |
301 | $surveys[$cacheKey][$survey->id] = $survey->title; | |
302 | } | |
303 | } | |
304 | ||
305 | return $surveys[$cacheKey]; | |
306 | } | |
307 | ||
308 | /** | |
100fef9d | 309 | * Get Surveys activity types |
6a488035 TO |
310 | * |
311 | * | |
6a488035 | 312 | */ |
5c2ea586 TO |
313 | static function getSurveyActivityType( |
314 | $returnColumn = 'label', | |
6a488035 TO |
315 | $includePetitionActivityType = FALSE |
316 | ) { | |
317 | static $activityTypes; | |
318 | $cacheKey = "{$returnColumn}_{$includePetitionActivityType}"; | |
319 | ||
320 | if (!isset($activityTypes[$cacheKey])) { | |
321 | $activityTypes = array(); | |
322 | $campaignCompId = CRM_Core_Component::getComponentID('CiviCampaign'); | |
323 | if ($campaignCompId) { | |
324 | $condition = " AND v.component_id={$campaignCompId}"; | |
325 | if (!$includePetitionActivityType) { | |
326 | $condition .= " AND v.name != 'Petition'"; | |
327 | } | |
328 | $activityTypes[$cacheKey] = CRM_Core_OptionGroup::values('activity_type', | |
329 | FALSE, FALSE, FALSE, | |
330 | $condition, | |
331 | $returnColumn | |
332 | ); | |
333 | } | |
334 | } | |
7b60d5b9 | 335 | if (!empty($activityTypes[$cacheKey])) { |
336 | return $activityTypes[$cacheKey]; | |
337 | } | |
338 | else { | |
339 | return; | |
340 | } | |
6a488035 TO |
341 | } |
342 | ||
343 | /** | |
100fef9d | 344 | * Get Surveys custom groups |
6a488035 | 345 | * |
5a4f6742 CW |
346 | * @param array $surveyTypes |
347 | * an array of survey type id. | |
6a488035 | 348 | * |
77b97be7 | 349 | * @return array |
6a488035 | 350 | */ |
00be9182 | 351 | public static function getSurveyCustomGroups($surveyTypes = array()) { |
6a488035 TO |
352 | $customGroups = array(); |
353 | if (!is_array($surveyTypes)) { | |
354 | $surveyTypes = array($surveyTypes); | |
355 | } | |
356 | ||
357 | if (!empty($surveyTypes)) { | |
358 | $activityTypes = array_flip($surveyTypes); | |
359 | } | |
360 | else { | |
361 | $activityTypes = self::getSurveyActivityType(); | |
362 | } | |
363 | ||
364 | if (!empty($activityTypes)) { | |
365 | $extendSubType = implode('[[:>:]]|[[:<:]]', array_keys($activityTypes)); | |
366 | ||
367 | $query = "SELECT cg.id, cg.name, cg.title, cg.extends_entity_column_value | |
368 | FROM civicrm_custom_group cg | |
369 | WHERE cg.is_active = 1 AND cg.extends_entity_column_value REGEXP '[[:<:]]{$extendSubType}[[:>:]]'"; | |
370 | ||
371 | $dao = CRM_Core_DAO::executeQuery($query); | |
372 | while ($dao->fetch()) { | |
373 | $customGroups[$dao->id]['id'] = $dao->id; | |
374 | $customGroups[$dao->id]['name'] = $dao->name; | |
375 | $customGroups[$dao->id]['title'] = $dao->title; | |
376 | $customGroups[$dao->id]['extends'] = $dao->extends_entity_column_value; | |
377 | } | |
378 | } | |
379 | ||
380 | return $customGroups; | |
381 | } | |
382 | ||
383 | /** | |
100fef9d | 384 | * Update the is_active flag in the db |
6a488035 | 385 | * |
7aaf6db0 TO |
386 | * @param int $id |
387 | * Id of the database record. | |
388 | * @param bool $is_active | |
389 | * Value we want to set the is_active field. | |
6a488035 | 390 | * |
a6c01b45 CW |
391 | * @return Object |
392 | * DAO object on sucess, null otherwise | |
6a488035 | 393 | */ |
00be9182 | 394 | public static function setIsActive($id, $is_active) { |
6a488035 TO |
395 | return CRM_Core_DAO::setFieldValue('CRM_Campaign_DAO_Survey', $id, 'is_active', $is_active); |
396 | } | |
397 | ||
398 | /** | |
100fef9d | 399 | * Delete the survey |
6a488035 | 400 | * |
7aaf6db0 TO |
401 | * @param int $id |
402 | * Survey id. | |
6a488035 | 403 | * |
77b97be7 | 404 | * @return mixed|null |
6a488035 | 405 | */ |
00be9182 | 406 | public static function del($id) { |
6a488035 TO |
407 | if (!$id) { |
408 | return NULL; | |
409 | } | |
410 | $reportId = CRM_Campaign_BAO_Survey::getReportID($id); | |
9b873358 | 411 | if ($reportId) { |
0b25329b | 412 | CRM_Report_BAO_ReportInstance::del($reportId); |
6a488035 TO |
413 | } |
414 | $dao = new CRM_Campaign_DAO_Survey(); | |
415 | $dao->id = $id; | |
416 | return $dao->delete(); | |
417 | } | |
418 | ||
419 | /** | |
420 | * This function retrieve contact information. | |
421 | * | |
c490a46a | 422 | * @param array $voterIds |
7aaf6db0 TO |
423 | * @param array $returnProperties |
424 | * An array of return elements. | |
6a488035 | 425 | * |
a6c01b45 CW |
426 | * @return array |
427 | * array of contact info. | |
6a488035 | 428 | */ |
00be9182 | 429 | public static function voterDetails($voterIds, $returnProperties = array()) { |
6a488035 TO |
430 | $voterDetails = array(); |
431 | if (!is_array($voterIds) || empty($voterIds)) { | |
432 | return $voterDetails; | |
433 | } | |
434 | ||
435 | if (empty($returnProperties)) { | |
436 | $autocompleteContactSearch = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, | |
437 | 'contact_autocomplete_options' | |
438 | ); | |
439 | $returnProperties = array_fill_keys(array_merge(array( | |
353ffa53 TO |
440 | 'contact_type', |
441 | 'contact_sub_type', | |
442 | 'sort_name', | |
443 | ), | |
444 | array_keys($autocompleteContactSearch) | |
445 | ), 1); | |
6a488035 TO |
446 | } |
447 | ||
448 | $select = $from = array(); | |
449 | foreach ($returnProperties as $property => $ignore) { | |
450 | $value = (in_array($property, array( | |
353ffa53 TO |
451 | 'city', |
452 | 'street_address' | |
453 | ))) ? 'address' : $property; | |
6a488035 TO |
454 | switch ($property) { |
455 | case 'sort_name': | |
456 | case 'contact_type': | |
457 | case 'contact_sub_type': | |
458 | $select[] = "$property as $property"; | |
459 | $from['contact'] = 'civicrm_contact contact'; | |
460 | break; | |
461 | ||
462 | case 'email': | |
463 | case 'phone': | |
464 | case 'city': | |
465 | case 'street_address': | |
466 | $select[] = "$property as $property"; | |
467 | $from[$value] = "LEFT JOIN civicrm_{$value} {$value} ON ( contact.id = {$value}.contact_id AND {$value}.is_primary = 1 ) "; | |
468 | break; | |
469 | ||
470 | case 'country': | |
471 | case 'state_province': | |
472 | $select[] = "{$property}.name as $property"; | |
473 | if (!in_array('address', $from)) { | |
474 | $from['address'] = 'LEFT JOIN civicrm_address address ON ( contact.id = address.contact_id AND address.is_primary = 1) '; | |
475 | } | |
476 | $from[$value] = " LEFT JOIN civicrm_{$value} {$value} ON ( address.{$value}_id = {$value}.id ) "; | |
477 | break; | |
478 | } | |
479 | } | |
480 | ||
481 | //finally retrieve contact details. | |
482 | if (!empty($select) && !empty($from)) { | |
353ffa53 | 483 | $fromClause = implode(' ', $from); |
6a488035 | 484 | $selectClause = implode(', ', $select); |
353ffa53 | 485 | $whereClause = "contact.id IN (" . implode(',', $voterIds) . ')'; |
6a488035 TO |
486 | |
487 | $query = " | |
91da6cd5 | 488 | SELECT contact.id as contactId, $selectClause |
6a488035 TO |
489 | FROM $fromClause |
490 | WHERE $whereClause | |
491 | Group By contact.id"; | |
492 | ||
493 | $contact = CRM_Core_DAO::executeQuery($query); | |
494 | while ($contact->fetch()) { | |
495 | $voterDetails[$contact->contactId]['contact_id'] = $contact->contactId; | |
496 | foreach ($returnProperties as $property => $ignore) { | |
497 | $voterDetails[$contact->contactId][$property] = $contact->$property; | |
498 | } | |
5c2ea586 | 499 | $image = CRM_Contact_BAO_Contact_Utils::getImage($contact->contact_sub_type ? $contact->contact_sub_type : $contact->contact_type, |
6a488035 TO |
500 | FALSE, |
501 | $contact->contactId | |
502 | ); | |
503 | $voterDetails[$contact->contactId]['contact_type'] = $image; | |
504 | } | |
505 | $contact->free(); | |
506 | } | |
507 | ||
508 | return $voterDetails; | |
509 | } | |
510 | ||
511 | /** | |
512 | * This function retrieve survey related activities w/ for give voter ids. | |
513 | * | |
7aaf6db0 TO |
514 | * @param int $surveyId |
515 | * Survey id. | |
516 | * @param array $voterIds | |
517 | * VoterIds. | |
6a488035 | 518 | * |
100fef9d | 519 | * @param int $interviewerId |
2a6da8d7 EM |
520 | * @param array $statusIds |
521 | * | |
a6c01b45 CW |
522 | * @return array |
523 | * array of survey activity. | |
6a488035 | 524 | */ |
5c2ea586 TO |
525 | static function voterActivityDetails( |
526 | $surveyId, $voterIds, $interviewerId = NULL, | |
353ffa53 | 527 | $statusIds = array()) { |
6a488035 TO |
528 | $activityDetails = array(); |
529 | if (!$surveyId || | |
530 | !is_array($voterIds) || empty($voterIds) | |
531 | ) { | |
532 | return $activityDetails; | |
533 | } | |
534 | ||
535 | $whereClause = NULL; | |
536 | if (is_array($statusIds) && !empty($statusIds)) { | |
537 | $whereClause = ' AND ( activity.status_id IN ( ' . implode(',', array_values($statusIds)) . ' ) )'; | |
538 | } | |
539 | ||
6a488035 | 540 | $targetContactIds = ' ( ' . implode(',', $voterIds) . ' ) '; |
e7e657f0 | 541 | $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); |
9e74e3ce | 542 | $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts); |
543 | $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); | |
8ef12e64 | 544 | |
64e716d7 | 545 | $params[1] = array($surveyId, 'Integer'); |
91da6cd5 DL |
546 | $query = " |
547 | SELECT activity.id, activity.status_id, | |
baf8a079 | 548 | activityTarget.contact_id as voter_id, |
549 | activityAssignment.contact_id as interviewer_id | |
6a488035 | 550 | FROM civicrm_activity activity |
91da6cd5 | 551 | INNER JOIN civicrm_activity_contact activityTarget |
9e74e3ce | 552 | ON ( activityTarget.activity_id = activity.id AND activityTarget.record_type_id = $targetID ) |
91da6cd5 | 553 | INNER JOIN civicrm_activity_contact activityAssignment |
9e74e3ce | 554 | ON ( activityAssignment.activity_id = activity.id AND activityAssignment.record_type_id = $assigneeID ) |
6a488035 | 555 | WHERE activity.source_record_id = %1 |
64e716d7 | 556 | AND ( activity.is_deleted IS NULL OR activity.is_deleted = 0 ) "; |
22e263ad | 557 | if (!empty($interviewerId)) { |
64e716d7 JM |
558 | $query .= "AND activityAssignment.contact_id = %2 "; |
559 | $params[2] = array($interviewerId, 'Integer'); | |
560 | } | |
561 | $query .= "AND activityTarget.contact_id IN {$targetContactIds} | |
6a488035 | 562 | $whereClause"; |
77b97be7 | 563 | $activity = CRM_Core_DAO::executeQuery($query, $params); |
6a488035 TO |
564 | while ($activity->fetch()) { |
565 | $activityDetails[$activity->voter_id] = array( | |
566 | 'voter_id' => $activity->voter_id, | |
567 | 'status_id' => $activity->status_id, | |
568 | 'activity_id' => $activity->id, | |
569 | 'interviewer_id' => $activity->interviewer_id, | |
570 | ); | |
571 | } | |
572 | ||
573 | return $activityDetails; | |
574 | } | |
575 | ||
576 | /** | |
577 | * This function retrieve survey related activities. | |
578 | * | |
7aaf6db0 | 579 | * @param int $surveyId |
100fef9d | 580 | * @param int $interviewerId |
f220017a CW |
581 | * @param array $statusIds |
582 | * @param array $voterIds | |
2a6da8d7 | 583 | * @param bool $onlyCount |
f220017a CW |
584 | * |
585 | * @return array | |
586 | * An array of survey activity. | |
6a488035 | 587 | */ |
5c2ea586 TO |
588 | static function getSurveyActivities( |
589 | $surveyId, | |
6a488035 | 590 | $interviewerId = NULL, |
5c2ea586 TO |
591 | $statusIds = NULL, |
592 | $voterIds = NULL, | |
593 | $onlyCount = FALSE | |
6a488035 TO |
594 | ) { |
595 | $activities = array(); | |
596 | $surveyActivityCount = 0; | |
597 | if (!$surveyId) { | |
598 | return ($onlyCount) ? 0 : $activities; | |
599 | } | |
600 | ||
601 | $where = array(); | |
602 | if (!empty($statusIds)) { | |
603 | $where[] = '( activity.status_id IN ( ' . implode(',', array_values($statusIds)) . ' ) )'; | |
604 | } | |
605 | ||
606 | if ($interviewerId) { | |
5161d8be | 607 | $where[] = "( activityAssignment.contact_id = $interviewerId )"; |
6a488035 TO |
608 | } |
609 | ||
610 | if (!empty($voterIds)) { | |
5161d8be | 611 | $where[] = "( activityTarget.contact_id IN ( " . implode(',', $voterIds) . " ) )"; |
6a488035 TO |
612 | } |
613 | ||
614 | $whereClause = NULL; | |
615 | if (!empty($where)) { | |
616 | $whereClause = ' AND ( ' . implode(' AND ', $where) . ' )'; | |
617 | } | |
618 | ||
619 | $actTypeId = CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Survey', $surveyId, 'activity_type_id'); | |
620 | if (!$actTypeId) { | |
621 | return $activities; | |
622 | } | |
623 | ||
624 | if ($onlyCount) { | |
625 | $select = "SELECT count(activity.id)"; | |
626 | } | |
627 | else { | |
628 | $select = " | |
91da6cd5 | 629 | SELECT activity.id, activity.status_id, |
5161d8be | 630 | activityTarget.contact_id as voter_id, |
631 | activityAssignment.contact_id as interviewer_id, | |
6a488035 TO |
632 | activity.result as result, |
633 | activity.activity_date_time as activity_date_time, | |
634 | contact_a.display_name as voter_name"; | |
635 | } | |
636 | ||
e7e657f0 | 637 | $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); |
9e74e3ce | 638 | $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts); |
639 | $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); | |
640 | ||
6a488035 TO |
641 | $query = " |
642 | $select | |
643 | FROM civicrm_activity activity | |
91da6cd5 | 644 | INNER JOIN civicrm_activity_contact activityTarget |
9e74e3ce | 645 | ON ( activityTarget.activity_id = activity.id AND activityTarget.record_type_id = $targetID ) |
91da6cd5 | 646 | INNER JOIN civicrm_activity_contact activityAssignment |
9e74e3ce | 647 | ON ( activityAssignment.activity_id = activity.id AND activityAssignment.record_type_id = $assigneeID ) |
91da6cd5 | 648 | INNER JOIN civicrm_contact contact_a ON ( activityTarget.contact_id = contact_a.id ) |
6a488035 TO |
649 | WHERE activity.source_record_id = %1 |
650 | AND activity.activity_type_id = %2 | |
651 | AND ( activity.is_deleted IS NULL OR activity.is_deleted = 0 ) | |
652 | $whereClause"; | |
653 | ||
5c2ea586 | 654 | $params = array( |
353ffa53 | 655 | 1 => array($surveyId, 'Integer'), |
6a488035 TO |
656 | 2 => array($actTypeId, 'Integer'), |
657 | ); | |
658 | ||
659 | if ($onlyCount) { | |
660 | $dbCount = CRM_Core_DAO::singleValueQuery($query, $params); | |
661 | return ($dbCount) ? $dbCount : 0; | |
662 | } | |
663 | ||
664 | $activity = CRM_Core_DAO::executeQuery($query, $params); | |
665 | ||
666 | while ($activity->fetch()) { | |
667 | $activities[$activity->id] = array( | |
668 | 'id' => $activity->id, | |
669 | 'voter_id' => $activity->voter_id, | |
670 | 'voter_name' => $activity->voter_name, | |
671 | 'status_id' => $activity->status_id, | |
672 | 'interviewer_id' => $activity->interviewer_id, | |
673 | 'result' => $activity->result, | |
21dfd5f5 | 674 | 'activity_date_time' => $activity->activity_date_time, |
6a488035 TO |
675 | ); |
676 | } | |
677 | ||
678 | return $activities; | |
679 | } | |
680 | ||
681 | /** | |
dc195289 | 682 | * Retrieve survey voter information. |
6a488035 | 683 | * |
7aaf6db0 TO |
684 | * @param int $surveyId |
685 | * Survey id. | |
686 | * @param int $interviewerId | |
687 | * Interviewer id. | |
688 | * @param array $statusIds | |
689 | * Survey status ids. | |
6a488035 | 690 | * |
dc195289 CW |
691 | * @return array |
692 | * Survey related contact ids. | |
6a488035 | 693 | */ |
5c2ea586 | 694 | public static function getSurveyVoterInfo($surveyId, $interviewerId = NULL, $statusIds = array()) { |
6a488035 TO |
695 | $voterIds = array(); |
696 | if (!$surveyId) { | |
697 | return $voterIds; | |
698 | } | |
699 | ||
700 | $cacheKey = $surveyId; | |
701 | if ($interviewerId) { | |
702 | $cacheKey .= "_{$interviewerId}"; | |
703 | } | |
704 | if (is_array($statusIds) && !empty($statusIds)) { | |
705 | $cacheKey = "{$cacheKey}_" . implode('_', $statusIds); | |
706 | } | |
707 | ||
708 | static $contactIds = array(); | |
709 | if (!isset($contactIds[$cacheKey])) { | |
710 | $activities = self::getSurveyActivities($surveyId, $interviewerId, $statusIds); | |
711 | foreach ($activities as $values) { | |
712 | $voterIds[$values['voter_id']] = $values; | |
713 | } | |
714 | $contactIds[$cacheKey] = $voterIds; | |
715 | } | |
716 | ||
717 | return $contactIds[$cacheKey]; | |
718 | } | |
719 | ||
720 | /** | |
91da6cd5 | 721 | * This function retrieve all option groups which are created as a result set |
6a488035 | 722 | * |
2a6da8d7 | 723 | * @param string $valueColumnName |
a6c01b45 CW |
724 | * @return array |
725 | * an array of option groups. | |
6a488035 | 726 | */ |
5c2ea586 | 727 | public static function getResultSets($valueColumnName = 'title') { |
6a488035 TO |
728 | $resultSets = array(); |
729 | $valueColumnName = CRM_Utils_Type::escape($valueColumnName, 'String'); | |
730 | ||
353ffa53 TO |
731 | $query = "SELECT id, {$valueColumnName} FROM civicrm_option_group WHERE name LIKE 'civicrm_survey_%' AND is_active=1"; |
732 | $dao = CRM_Core_DAO::executeQuery($query); | |
6a488035 TO |
733 | while ($dao->fetch()) { |
734 | $resultSets[$dao->id] = $dao->$valueColumnName; | |
735 | } | |
736 | ||
737 | return $resultSets; | |
738 | } | |
739 | ||
740 | /** | |
dc195289 | 741 | * check survey activity. |
6a488035 | 742 | * |
7aaf6db0 TO |
743 | * @param int $activityId |
744 | * Activity id. | |
a6c01b45 | 745 | * @return boolean |
6a488035 | 746 | */ |
00be9182 | 747 | public static function isSurveyActivity($activityId) { |
6a488035 TO |
748 | $isSurveyActivity = FALSE; |
749 | if (!$activityId) { | |
750 | return $isSurveyActivity; | |
751 | } | |
752 | ||
753 | $activity = new CRM_Activity_DAO_Activity(); | |
754 | $activity->id = $activityId; | |
755 | $activity->selectAdd('source_record_id, activity_type_id'); | |
756 | if ($activity->find(TRUE) && | |
757 | $activity->source_record_id | |
758 | ) { | |
759 | $surveyActTypes = self::getSurveyActivityType(); | |
760 | if (array_key_exists($activity->activity_type_id, $surveyActTypes)) { | |
761 | $isSurveyActivity = TRUE; | |
762 | } | |
763 | } | |
764 | ||
765 | return $isSurveyActivity; | |
766 | } | |
767 | ||
768 | /** | |
769 | * This function retrive all response options of survey | |
770 | * | |
7aaf6db0 TO |
771 | * @param int $surveyId |
772 | * Survey id. | |
a6c01b45 CW |
773 | * @return array |
774 | * an array of option values | |
6a488035 | 775 | */ |
00be9182 | 776 | public static function getResponsesOptions($surveyId) { |
6a488035 TO |
777 | $responseOptions = array(); |
778 | if (!$surveyId) { | |
779 | return $responseOptions; | |
780 | } | |
781 | ||
782 | $resultId = CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Survey', $surveyId, 'result_id'); | |
783 | if ($resultId) { | |
784 | $responseOptions = CRM_Core_OptionGroup::valuesByID($resultId); | |
785 | } | |
786 | ||
787 | return $responseOptions; | |
788 | } | |
789 | ||
790 | /** | |
791 | * This function return all voter links with respecting permissions | |
792 | * | |
100fef9d | 793 | * @param int $surveyId |
2a6da8d7 EM |
794 | * @param bool $enclosedInUL |
795 | * @param string $extraULName | |
72b3a70c CW |
796 | * @return array|string |
797 | * $url array of permissioned links | |
6a488035 | 798 | */ |
00be9182 | 799 | public static function buildPermissionLinks($surveyId, $enclosedInUL = FALSE, $extraULName = 'more') { |
6a488035 TO |
800 | $menuLinks = array(); |
801 | if (!$surveyId) { | |
802 | return $menuLinks; | |
803 | } | |
804 | ||
805 | static $voterLinks = array(); | |
806 | if (empty($voterLinks)) { | |
807 | $permissioned = FALSE; | |
808 | if (CRM_Core_Permission::check('manage campaign') || | |
809 | CRM_Core_Permission::check('administer CiviCampaign') | |
810 | ) { | |
811 | $permissioned = TRUE; | |
812 | } | |
813 | ||
814 | if ($permissioned || CRM_Core_Permission::check("reserve campaign contacts")) { | |
815 | $voterLinks['reserve'] = array( | |
816 | 'name' => 'reserve', | |
817 | 'url' => 'civicrm/survey/search', | |
2db64250 | 818 | 'qs' => 'sid=%%id%%&reset=1&op=reserve', |
6a488035 TO |
819 | 'title' => ts('Reserve Respondents'), |
820 | ); | |
821 | } | |
822 | if ($permissioned || CRM_Core_Permission::check("interview campaign contacts")) { | |
823 | $voterLinks['release'] = array( | |
824 | 'name' => 'interview', | |
825 | 'url' => 'civicrm/survey/search', | |
826 | 'qs' => 'sid=%%id%%&reset=1&op=interview&force=1', | |
827 | 'title' => ts('Interview Respondents'), | |
828 | ); | |
829 | } | |
830 | if ($permissioned || CRM_Core_Permission::check("release campaign contacts")) { | |
831 | $voterLinks['interview'] = array( | |
832 | 'name' => 'release', | |
833 | 'url' => 'civicrm/survey/search', | |
834 | 'qs' => 'sid=%%id%%&reset=1&op=release&force=1', | |
835 | 'title' => ts('Release Respondents'), | |
836 | ); | |
837 | } | |
838 | } | |
839 | ||
840 | if (CRM_Core_Permission::check('access CiviReport')) { | |
841 | $reportID = self::getReportID($surveyId); | |
842 | if ($reportID) { | |
91da6cd5 | 843 | $voterLinks['report'] = |
6a488035 | 844 | array( |
353ffa53 TO |
845 | 'name' => 'report', |
846 | 'url' => "civicrm/report/instance/{$reportID}", | |
847 | 'qs' => 'reset=1', | |
848 | 'title' => ts('View Survey Report'), | |
849 | ); | |
6a488035 TO |
850 | } |
851 | } | |
852 | ||
853 | $ids = array('id' => $surveyId); | |
854 | foreach ($voterLinks as $link) { | |
a7488080 | 855 | if (!empty($link['qs']) && |
6a488035 TO |
856 | !CRM_Utils_System::isNull($link['qs']) |
857 | ) { | |
858 | $urlPath = CRM_Utils_System::url(CRM_Core_Action::replace($link['url'], $ids), | |
859 | CRM_Core_Action::replace($link['qs'], $ids) | |
860 | ); | |
a1c7d42f | 861 | $menuLinks[] = sprintf('<a href="%s" class="action-item crm-hover-button" title="%s">%s</a>', |
6a488035 TO |
862 | $urlPath, |
863 | CRM_Utils_Array::value('title', $link), | |
864 | $link['title'] | |
865 | ); | |
866 | } | |
867 | } | |
868 | if ($enclosedInUL) { | |
869 | $extraLinksName = strtolower($extraULName); | |
870 | $allLinks = ''; | |
871 | CRM_Utils_String::append($allLinks, '</li><li>', $menuLinks); | |
872 | $allLinks = "$extraULName <ul id='panel_{$extraLinksName}_xx' class='panel'><li>{$allLinks}</li></ul>"; | |
a1c7d42f | 873 | $menuLinks = "<span class='btn-slide crm-hover-button' id={$extraLinksName}_xx>{$allLinks}</span>"; |
6a488035 TO |
874 | } |
875 | ||
876 | return $menuLinks; | |
877 | } | |
878 | ||
879 | /** | |
100fef9d | 880 | * Retrieve survey associated profile id. |
6a488035 TO |
881 | */ |
882 | public static function getSurveyProfileId($surveyId) { | |
883 | if (!$surveyId) { | |
884 | return NULL; | |
885 | } | |
886 | ||
887 | static $ufIds = array(); | |
888 | if (!array_key_exists($surveyId, $ufIds)) { | |
889 | //get the profile id. | |
890 | $ufJoinParams = array( | |
891 | 'entity_id' => $surveyId, | |
892 | 'entity_table' => 'civicrm_survey', | |
893 | 'module' => 'CiviCampaign', | |
894 | ); | |
91da6cd5 | 895 | |
6a488035 TO |
896 | list($first, $second) = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams); |
897 | ||
898 | if ($first) { | |
91da6cd5 | 899 | $ufIds[$surveyId] = array($first); |
6a488035 TO |
900 | } |
901 | if ($second) { | |
91da6cd5 | 902 | $ufIds[$surveyId][] = array_shift($second); |
6a488035 TO |
903 | } |
904 | } | |
905 | ||
47dd4836 | 906 | return CRM_Utils_Array::value($surveyId, $ufIds); |
6a488035 TO |
907 | } |
908 | ||
30c4e065 | 909 | /** |
100fef9d | 910 | * @param int $surveyId |
30c4e065 EM |
911 | * |
912 | * @return mixed | |
913 | */ | |
6a488035 TO |
914 | public Static function getReportID($surveyId) { |
915 | static $reportIds = array(); | |
916 | ||
917 | if (!array_key_exists($surveyId, $reportIds)) { | |
918 | $query = "SELECT MAX(id) as id FROM civicrm_report_instance WHERE name = %1"; | |
5c2ea586 | 919 | $reportID = CRM_Core_DAO::singleValueQuery($query, array(1 => array("survey_{$surveyId}", 'String'))); |
6a488035 TO |
920 | $reportIds[$surveyId] = $reportID; |
921 | } | |
922 | return $reportIds[$surveyId]; | |
923 | } | |
924 | ||
925 | /** | |
100fef9d | 926 | * Decides the contact type for given survey. |
6a488035 TO |
927 | */ |
928 | public static function getSurveyContactType($surveyId) { | |
929 | $contactType = NULL; | |
930 | ||
931 | //apply filter of profile type on search. | |
932 | $profileId = self::getSurveyProfileId($surveyId); | |
933 | if ($profileId) { | |
934 | $profileType = CRM_Core_BAO_UFField::getProfileType($profileId); | |
935 | if (in_array($profileType, CRM_Contact_BAO_ContactType::basicTypes())) { | |
936 | $contactType = $profileType; | |
937 | } | |
938 | } | |
939 | ||
940 | return $contactType; | |
941 | } | |
942 | ||
943 | /** | |
100fef9d | 944 | * Get survey supportable profile types |
6a488035 TO |
945 | */ |
946 | public static function surveyProfileTypes() { | |
947 | static $profileTypes; | |
948 | ||
949 | if (!isset($profileTypes)) { | |
950 | $profileTypes = array_merge(array('Activity', 'Contact'), CRM_Contact_BAO_ContactType::basicTypes()); | |
5c2ea586 | 951 | $profileTypes = array_diff($profileTypes, array('Organization', 'Household')); |
6a488035 TO |
952 | } |
953 | ||
954 | return $profileTypes; | |
955 | } | |
956 | ||
957 | /** | |
958 | * Get the valid survey response fields those | |
959 | * are configured with profile and custom fields. | |
960 | * | |
7aaf6db0 TO |
961 | * @param int $surveyId |
962 | * Survey id. | |
963 | * @param int $surveyTypeId | |
964 | * Survey activity type id. | |
6a488035 | 965 | * |
a6c01b45 CW |
966 | * @return array |
967 | * an array of valid survey response fields. | |
6a488035 TO |
968 | */ |
969 | public static function getSurveyResponseFields($surveyId, $surveyTypeId = NULL) { | |
970 | if (empty($surveyId)) { | |
971 | return array(); | |
972 | } | |
973 | ||
974 | static $responseFields; | |
975 | $cacheKey = "{$surveyId}_{$surveyTypeId}"; | |
976 | ||
977 | if (isset($responseFields[$cacheKey])) { | |
978 | return $responseFields[$cacheKey]; | |
979 | } | |
980 | ||
981 | $responseFields[$cacheKey] = array(); | |
982 | ||
983 | $profileId = self::getSurveyProfileId($surveyId); | |
984 | ||
985 | if (!$profileId) { | |
986 | return $responseFields; | |
987 | } | |
988 | ||
989 | if (!$surveyTypeId) { | |
990 | $surveyTypeId = CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Survey', $surveyId, 'activity_type_id'); | |
991 | } | |
992 | ||
993 | $profileFields = CRM_Core_BAO_UFGroup::getFields($profileId, | |
5e5141ea | 994 | FALSE, CRM_Core_Action::VIEW, NULL, NULL, FALSE, NULL, FALSE, NULL, CRM_Core_Permission::CREATE, 'field_name', TRUE |
6a488035 TO |
995 | ); |
996 | ||
997 | //don't load these fields in grid. | |
998 | $removeFields = array('File', 'RichTextEditor'); | |
999 | ||
1000 | $supportableFieldTypes = self::surveyProfileTypes(); | |
1001 | ||
1002 | // get custom fields of type survey | |
1003 | $customFields = CRM_Core_BAO_CustomField::getFields('Activity', FALSE, FALSE, $surveyTypeId); | |
1004 | ||
1005 | foreach ($profileFields as $name => $field) { | |
1006 | //get only contact and activity fields. | |
1007 | //later stage we might going to consider contact type also. | |
4e9ab75a RN |
1008 | if (in_array($field['field_type'], $supportableFieldTypes)) { |
1009 | // we should allow all supported custom data for survey | |
1010 | // In case of activity, allow normal activity and with subtype survey, | |
1011 | // suppress custom data of other activity types | |
1012 | if (CRM_Core_BAO_CustomField::getKeyID($name)) { | |
1013 | if (!in_array($field['html_type'], $removeFields)) { | |
1014 | if ($field['field_type'] != 'Activity') { | |
1015 | $responseFields[$cacheKey][$name] = $field; | |
1016 | } | |
1017 | elseif (array_key_exists(CRM_Core_BAO_CustomField::getKeyID($name), $customFields)) { | |
1018 | $responseFields[$cacheKey][$name] = $field; | |
1019 | } | |
1020 | } | |
6a488035 | 1021 | } |
4e9ab75a | 1022 | else { |
6a488035 TO |
1023 | $responseFields[$cacheKey][$name] = $field; |
1024 | } | |
1025 | } | |
6a488035 TO |
1026 | } |
1027 | ||
1028 | return $responseFields[$cacheKey]; | |
1029 | } | |
1030 | ||
1031 | /** | |
1032 | * Get all interviewers of surveys. | |
1033 | * | |
a6c01b45 CW |
1034 | * @return array |
1035 | * an array of valid survey response fields. | |
6a488035 TO |
1036 | */ |
1037 | public static function getInterviewers() { | |
1038 | static $interviewers; | |
1039 | ||
1040 | if (isset($interviewers)) { | |
1041 | return $interviewers; | |
1042 | } | |
1043 | ||
1044 | $whereClause = NULL; | |
1045 | $activityTypes = self::getSurveyActivityType(); | |
1046 | if (!empty($activityTypes)) { | |
1047 | $whereClause = ' WHERE survey.activity_type_id IN ( ' . implode(' , ', array_keys($activityTypes)) . ' )'; | |
1048 | } | |
1049 | ||
1050 | $interviewers = array(); | |
e7e657f0 | 1051 | $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); |
9e74e3ce | 1052 | $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts); |
6a488035 TO |
1053 | |
1054 | $query = " | |
91da6cd5 | 1055 | SELECT contact.id as id, |
6a488035 | 1056 | contact.sort_name as sort_name |
91da6cd5 | 1057 | FROM civicrm_contact contact |
9e74e3ce | 1058 | INNER JOIN civicrm_activity_contact assignment ON ( assignment.contact_id = contact.id AND record_type_id = $assigneeID ) |
6a488035 TO |
1059 | INNER JOIN civicrm_activity activity ON ( activity.id = assignment.activity_id ) |
1060 | INNER JOIN civicrm_survey survey ON ( activity.source_record_id = survey.id ) | |
1061 | {$whereClause}"; | |
1062 | ||
1063 | $interviewer = CRM_Core_DAO::executeQuery($query); | |
1064 | while ($interviewer->fetch()) { | |
1065 | $interviewers[$interviewer->id] = $interviewer->sort_name; | |
1066 | } | |
1067 | ||
1068 | return $interviewers; | |
1069 | } | |
1070 | ||
1071 | /** | |
1072 | * Check and update the survey respondents. | |
1073 | * | |
c490a46a | 1074 | * @param array $params |
dd244018 | 1075 | * |
a6c01b45 CW |
1076 | * @return array |
1077 | * success message | |
6a488035 | 1078 | */ |
7b60d5b9 | 1079 | public static function releaseRespondent($params) { |
6a488035 TO |
1080 | $activityStatus = CRM_Core_PseudoConstant::activityStatus('name'); |
1081 | $reserveStatusId = array_search('Scheduled', $activityStatus); | |
1082 | $surveyActivityTypes = CRM_Campaign_BAO_Survey::getSurveyActivityType(); | |
7b60d5b9 | 1083 | if (!empty($surveyActivityTypes) && is_array($surveyActivityTypes)) { |
1084 | $surveyActivityTypesIds = array_keys($surveyActivityTypes); | |
1085 | } | |
6a488035 TO |
1086 | |
1087 | //retrieve all survey activities related to reserve action. | |
1088 | $releasedCount = 0; | |
1089 | if ($reserveStatusId && !empty($surveyActivityTypesIds)) { | |
1090 | $query = ' | |
91da6cd5 | 1091 | SELECT activity.id as id, |
6a488035 TO |
1092 | activity.activity_date_time as activity_date_time, |
1093 | survey.id as surveyId, | |
1094 | survey.release_frequency as release_frequency | |
1095 | FROM civicrm_activity activity | |
91da6cd5 DL |
1096 | INNER JOIN civicrm_survey survey ON ( survey.id = activity.source_record_id ) |
1097 | WHERE activity.is_deleted = 0 | |
1098 | AND activity.status_id = %1 | |
6a488035 TO |
1099 | AND activity.activity_type_id IN ( ' . implode(', ', $surveyActivityTypesIds) . ' )'; |
1100 | $activity = CRM_Core_DAO::executeQuery($query, array(1 => array($reserveStatusId, 'Positive'))); | |
1101 | $releasedIds = array(); | |
1102 | while ($activity->fetch()) { | |
1103 | if (!$activity->release_frequency) { | |
1104 | continue; | |
1105 | } | |
1106 | $reservedSeconds = CRM_Utils_Date::unixTime($activity->activity_date_time); | |
1107 | $releasedSeconds = $activity->release_frequency * 24 * 3600; | |
1108 | $totalReservedSeconds = $reservedSeconds + $releasedSeconds; | |
1109 | if ($totalReservedSeconds < time()) { | |
1110 | $releasedIds[$activity->id] = $activity->id; | |
1111 | } | |
1112 | } | |
1113 | ||
1114 | //released respondent. | |
1115 | if (!empty($releasedIds)) { | |
1116 | $query = ' | |
1117 | UPDATE civicrm_activity | |
1118 | SET is_deleted = 1 | |
1119 | WHERE id IN ( ' . implode(', ', $releasedIds) . ' )'; | |
1120 | CRM_Core_DAO::executeQuery($query); | |
1121 | $releasedCount = count($releasedIds); | |
1122 | } | |
1123 | } | |
1124 | ||
1125 | $rtnMsg = array( | |
1126 | 'is_error' => 0, | |
1127 | 'messages' => "Number of respondents released = {$releasedCount}", | |
1128 | ); | |
1129 | ||
1130 | return $rtnMsg; | |
1131 | } | |
a2015e41 CW |
1132 | |
1133 | /** | |
1134 | * Get options for a given field. | |
1135 | * @see CRM_Core_DAO::buildOptions | |
1136 | * | |
7aaf6db0 | 1137 | * @param string $fieldName |
353ffa53 TO |
1138 | * @param string $context : @see CRM_Core_DAO::buildOptionsContext |
1139 | * @param array $props : whatever is known about this dao object | |
a2015e41 CW |
1140 | * |
1141 | * @return array|bool | |
1142 | */ | |
1143 | public static function buildOptions($fieldName, $context = NULL, $props = array()) { | |
1144 | $params = array(); | |
1145 | // Special logic for fields whose options depend on context or properties | |
1146 | switch ($fieldName) { | |
1147 | case 'activity_type_id': | |
1148 | $campaignCompId = CRM_Core_Component::getComponentID('CiviCampaign'); | |
1149 | if ($campaignCompId) { | |
1150 | $params['condition'] = array("component_id={$campaignCompId}", "v.name != 'Petition'"); | |
1151 | } | |
1152 | break; | |
1153 | } | |
1154 | return CRM_Core_PseudoConstant::get(__CLASS__, $fieldName, $params, $context); | |
1155 | } | |
6a488035 | 1156 | } |