public function filterVoterIds() { //do the cleanup later on. if (!is_array($this->_contactIds)) { return; } $profileId = CRM_Campaign_BAO_Survey::getSurveyProfileId($this->_surveyId); if ($profileId) { $profileType = CRM_Core_BAO_UFField::getProfileType($profileId); if (in_array($profileType, CRM_Contact_BAO_ContactType::basicTypes())) { $voterIdCount = count($this->_contactIds); //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 = $this->_contactIds; $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); $query = "\n SELECT contact.id as id\n FROM civicrm_contact contact\nINNER JOIN {$tempTableName} ON ( {$tempTableName}.survey_contact_id = contact.id )\n WHERE contact.contact_type != %1"; $removeContact = CRM_Core_DAO::executeQuery($query, array(1 => array($profileType, 'String'))); while ($removeContact->fetch()) { unset($this->_contactIds[$removeContact->id]); } } } }