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