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