/** * Get the list of voters (respondents, yet to record survey response) for a survey * * @param array $params (reference ) input parameters * * @return array (reference ) contact_id * @static void * @access public */ function &civicrm_api3_survey_voter_get($params) { civicrm_api3_verify_one_mandatory($params, 'CRM_Campaign_BAO_Survey', array('survey_id', 'id')); if (array_key_exists('status_id', $params)) { $status_id = $params['status_id']; } else { $status_id = null; } $surveyID = empty($params['survey_id']) ? $params['id'] : $params['survey_id']; require_once 'CRM/Campaign/BAO/Survey.php'; $survey = new CRM_Campaign_BAO_Survey(); $voters = $survey->getSurveyVoterInfo($surveyID); require_once 'CRM/Contact/BAO/Contact.php'; foreach ($voters as $key => $voterArray) { $voters[$key]['voter_name'] = CRM_Contact_BAO_Contact::displayName($voterArray['voter_id']); } return civicrm_api3_create_success($voters, $params); }
/** * @param array $params * * @return array */ public static function voterClause($params) { $voterClause = array(); $fromClause = $whereClause = NULL; if (!is_array($params) || empty($params)) { return $voterClause; } $surveyId = CRM_Utils_Array::value('campaign_survey_id', $params); $searchVoterFor = CRM_Utils_Array::value('campaign_search_voter_for', $params); //get the survey activities. $activityStatus = CRM_Core_PseudoConstant::activityStatus('name'); $status = array('Scheduled'); if ($searchVoterFor == 'reserve') { $status[] = 'Completed'; } $completedStatusId = NULL; foreach ($status as $name) { if ($statusId = array_search($name, $activityStatus)) { $statusIds[] = $statusId; if ($name == 'Completed') { $completedStatusId = $statusId; } } } $voterActValues = CRM_Campaign_BAO_Survey::getSurveyVoterInfo($surveyId, NULL, $statusIds); if (!empty($voterActValues)) { $operator = 'IN'; $voterIds = array_keys($voterActValues); if ($searchVoterFor == 'reserve') { $operator = 'NOT IN'; //filter out recontact survey contacts. $recontactInterval = CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Survey', $surveyId, 'recontact_interval'); $recontactInterval = unserialize($recontactInterval); if ($surveyId && is_array($recontactInterval) && !empty($recontactInterval)) { $voterIds = array(); foreach ($voterActValues as $values) { $numOfDays = CRM_Utils_Array::value($values['result'], $recontactInterval); if ($numOfDays && $values['status_id'] == $completedStatusId) { $recontactIntSeconds = $numOfDays * 24 * 3600; $actDateTimeSeconds = CRM_Utils_Date::unixTime($values['activity_date_time']); $totalSeconds = $recontactIntSeconds + $actDateTimeSeconds; //don't consider completed survey activity //unless it fulfill recontact interval criteria. if ($totalSeconds <= time()) { continue; } } $voterIds[$values['voter_id']] = $values['voter_id']; } } } //lets dump these ids in tmp table and //use appropriate join depend on operator. if (!empty($voterIds)) { $voterIdCount = count($voterIds); //create temporary table to store voter ids. $tempTableName = CRM_Core_DAO::createTempTableName('civicrm_survey_respondent'); CRM_Core_DAO::executeQuery("DROP TEMPORARY TABLE IF EXISTS {$tempTableName}"); $query = "\n CREATE TEMPORARY TABLE {$tempTableName} (\n id int unsigned NOT NULL AUTO_INCREMENT,\n survey_contact_id int unsigned NOT NULL,\n PRIMARY KEY ( id )\n);\n"; CRM_Core_DAO::executeQuery($query); $batch = 100; $insertedCount = 0; do { $processIds = $voterIds; $insertIds = array_splice($processIds, $insertedCount, $batch); if (!empty($insertIds)) { $insertSQL = "INSERT IGNORE INTO {$tempTableName}( survey_contact_id )\n VALUES (" . implode('),(', $insertIds) . ');'; CRM_Core_DAO::executeQuery($insertSQL); } $insertedCount += $batch; } while ($insertedCount < $voterIdCount); if ($operator == 'IN') { $fromClause = " INNER JOIN {$tempTableName} ON ( {$tempTableName}.survey_contact_id = contact_a.id )"; } else { $fromClause = " LEFT JOIN {$tempTableName} ON ( {$tempTableName}.survey_contact_id = contact_a.id )"; $whereClause = "( {$tempTableName}.survey_contact_id IS NULL )"; } } } $voterClause = array('fromClause' => $fromClause, 'whereClause' => $whereClause); return $voterClause; }
function getVoterIds() { if (!$this->_interviewerId) { $session = CRM_Core_Session::singleton(); $this->_interviewerId = $session->get('userID'); } if (!$this->_surveyId) { // use default survey id require_once 'CRM/Campaign/DAO/Survey.php'; $dao = new CRM_Campaign_DAO_Survey(); $dao->is_active = 1; $dao->is_default = 1; $dao->find(true); $this->_surveyId = $dao->id; } $this->_contactIds = $this->get('contactIds'); if (!is_array($this->_contactIds)) { //get the survey activities. require_once 'CRM/Core/PseudoConstant.php'; $activityStatus = CRM_Core_PseudoConstant::activityStatus('name'); $statusIds = array(); if ($statusId = array_search('Scheduled', $activityStatus)) { $statusIds[] = $statusId; } require_once 'CRM/Campaign/BAO/Survey.php'; $surveyActivities = CRM_Campaign_BAO_Survey::getSurveyVoterInfo($this->_surveyId, $this->_interviewerId, $statusIds); $this->_contactIds = array(); foreach ($surveyActivities as $val) { $this->_contactIds[$val['voter_id']] = $val['voter_id']; } $this->set('contactIds', $this->_contactIds); } }
function voterClause($params) { $voterClause = null; if (!is_array($params) || empty($params)) { return $voterClause; } $surveyId = CRM_Utils_Array::value('campaign_survey_id', $params); $interviewerId = CRM_Utils_Array::value('survey_interviewer_id', $params); $searchVoterFor = CRM_Utils_Array::value('campaign_search_voter_for', $params); //get the survey activities. require_once 'CRM/Core/PseudoConstant.php'; $activityStatus = CRM_Core_PseudoConstant::activityStatus('name'); $status = array('Scheduled'); if ($searchVoterFor == 'reserve') { $status[] = 'Completed'; } $completedStatusId = null; foreach ($status as $name) { if ($statusId = array_search($name, $activityStatus)) { $statusIds[] = $statusId; if ($name == 'Completed') { $completedStatusId = $statusId; } } } require_once 'CRM/Campaign/BAO/Survey.php'; $voterActValues = CRM_Campaign_BAO_Survey::getSurveyVoterInfo($surveyId, null, $statusIds); if (!empty($voterActValues)) { $operator = 'IN'; $voterIds = array_keys($voterActValues); if ($searchVoterFor == 'reserve') { $operator = 'NOT IN'; //filter out recontact survey contacts. $recontactInterval = CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Survey', $surveyId, 'recontact_interval'); $recontactInterval = unserialize($recontactInterval); if ($surveyId && is_array($recontactInterval) && !empty($recontactInterval)) { $voterIds = array(); foreach ($voterActValues as $values) { $numOfDays = CRM_Utils_Array::value($values['result'], $recontactInterval); if ($numOfDays && $values['status_id'] == $completedStatusId) { $recontactIntSeconds = $numOfDays * 24 * 3600; $actDateTimeSeconds = CRM_Utils_Date::unixTime($values['activity_date_time']); $totalSeconds = $recontactIntSeconds + $actDateTimeSeconds; //don't consider completed survey activity //unless it fulfill recontact interval criteria. if ($totalSeconds <= time()) { continue; } } $voterIds[$values['voter_id']] = $values['voter_id']; } } } if (!empty($voterIds)) { $voterClause = "( contact_a.id {$operator} ( " . implode(', ', $voterIds) . ' ) )'; } } return $voterClause; }