diff --git a/application/models/Participant.php b/application/models/Participant.php index 70e2c230f4d..c8fddafefd8 100644 --- a/application/models/Participant.php +++ b/application/models/Participant.php @@ -1621,31 +1621,19 @@ private function writeParticipantsToTokenTable( $successful = 0; $blacklistSkipped = 0; - foreach ($participantIds as $participantId) { - $participant = Yii::app()->db - ->createCommand() - ->select('firstname,lastname,email,language,blacklisted') - ->where('participant_id = :pid') - ->from('{{participants}}') - ->bindParam(":pid", $participantId, PDO::PARAM_INT) - ->queryRow(); + $oParticipants = Participant::model()->findAllByPk($participantIds); + $oTokens = TokenDynamic::model($surveyId)->findAll(); + foreach ($oParticipants as $oParticipant) { if (Yii::app()->getConfig('blockaddingtosurveys') == 'Y' - && $participant['blacklisted'] == 'Y') { + && $oParticipant->blacklisted == 'Y') { $blacklistSkipped++; continue; } - - // Search for matching participant name/email in the survey survey participants table - $matchingParticipant = Yii::app()->db->createCommand()->select('tid')->from($survey->tokensTableName) - ->where('(firstname = :firstname AND lastname = :lastname AND email = :email) OR participant_id = :participant_id') - ->bindParam(":firstname", $participant['firstname'], PDO::PARAM_STR) - ->bindParam(":lastname", $participant['lastname'], PDO::PARAM_STR) - ->bindParam(":email", $participant['email'], PDO::PARAM_STR) - ->bindParam(":participant_id", $participantId, PDO::PARAM_STR) - ->queryAll(); - - if (count($matchingParticipant) > 0) { + $isDuplicate = array_reduce($oTokens, function($carry, $oToken) use ($oParticipant) { + return $carry ? $carry : ($oTokens->tid == $oParticipant->participant_id); + }, false); + if($isDuplicate) { //Participant already exists in survey participants table - don't copy $duplicate++; @@ -1655,14 +1643,14 @@ private function writeParticipantsToTokenTable( if (!empty($newAttributes)) { $numberofattributes = count($addedAttributes); for ($a = 0; $a < $numberofattributes; $a++) { - Participant::model()->updateTokenAttributeValue($surveyId, $participantId, $addedAttributes[$a], $addedAttributeIds[$a]); + Participant::model()->updateTokenAttributeValue($surveyId, $oParticipant->participant_id, $addedAttributes[$a], $addedAttributeIds[$a]); } } //If there are automapped attributes, add those values to the token entry for this participant foreach ($mappedAttributes as $key => $value) { if ($key[10] == 'c') { -//We know it's automapped because the 11th letter is 'c' - Participant::model()->updateTokenAttributeValue($surveyId, $participantId, $value, $key); + //We know it's automapped because the 11th letter is 'c' + Participant::model()->updateTokenAttributeValue($surveyId, $oParticipant->participant_id, $value, $key); } } } @@ -1670,49 +1658,49 @@ private function writeParticipantsToTokenTable( //If there are any manually mapped attributes, add those values to the token entry for this participant foreach ($mappedAttributes as $key => $value) { if ($key[10] != 'c' && $key[9] == '_') { -//It's not an auto field because it's 11th character isn't 'c' - Participant::model()->updateTokenAttributeValue($surveyId, $participantId, $value, $key); + //It's not an auto field because it's 11th character isn't 'c' + Participant::model()->updateTokenAttributeValue($surveyId, $oParticipant->participant_id, $value, $key); } } } if ($options['overwritest'] == "true") { foreach ($mappedAttributes as $key=>$value) { if ((strlen($key) > 8 && $key[10] != 'c' && $key[9] != '_') || strlen($key) < 9) { - Participant::model()->updateTokenAttributeValue($surveyId, $participantId, $value, $key); + Participant::model()->updateTokenAttributeValue($surveyId, $oParticipant->participant_id, $value, $key); } } } } else { //Create a new token entry for this participant $writearray = array( - 'participant_id' => $participantId, - 'firstname' => $participant['firstname'], - 'lastname' => $participant['lastname'], - 'email' => $participant['email'], + 'participant_id' => $oParticipant->participant_id, + 'firstname' => $oParticipant->firstname, + 'lastname' => $oParticipant->lastname, + 'email' => $oParticipant->email, 'emailstatus' => 'OK', - 'language' => isset($participant['language']) ? $participant['language'] : App()->language + 'language' => isset($oParticipant->language) ? $oParticipant->language : App()->language ); - Yii::app()->db - ->createCommand() - ->insert($survey->tokensTableName, $writearray); - - $insertedtokenid = getLastInsertID($survey->tokensTableName); + $insertedtokenid =TokenDynamic::model($surveyId)->insertParticipant($writearray); //Create a survey link for the new token entry - $data = array( - 'participant_id' => $participantId, - 'token_id' => $insertedtokenid, - 'survey_id' => $surveyId, - 'date_created' => date('Y-m-d H:i:s', time())); - Yii::app()->db->createCommand()->insert(SurveyLink::model()->tableName(), $data); - + $oSurveyLink = new SurveyLink; + $oSurveyLink->participant_id = $oParticipant->participant_id; + $oSurveyLink->token_id = $insertedtokenid; + $oSurveyLink->survey_id = $surveyId; + $oSurveyLink->date_created = date('Y-m-d H:i:s', time()); + try { + $oSurveyLink->save(); + } catch (Exception $e) { + throw new Exception(gT("Could not update token attribute value: ".$e->getMessage())); + } + //If there are new attributes created, add those values to the token entry for this participant if (!empty($newAttributes)) { $numberofattributes = count($addedAttributes); for ($a = 0; $a < $numberofattributes; $a++) { try { - Participant::model()->updateTokenAttributeValue($surveyId, $participantId, $addedAttributes[$a], $addedAttributeIds[$a]); + Participant::model()->updateTokenAttributeValue($surveyId, $oParticipant->participant_id, $addedAttributes[$a], $addedAttributeIds[$a]); } catch (Exception $e) { throw new Exception(gT("Could not update token attribute value: ".$e->getMessage())); } @@ -1727,7 +1715,7 @@ private function writeParticipantsToTokenTable( $value = substr($value, 10); } - Participant::model()->updateTokenAttributeValue($surveyId, $participantId, $value, $key); + Participant::model()->updateTokenAttributeValue($surveyId, $oParticipant->participant_id, $value, $key); } catch (Exception $e) { throw new Exception(gT("Could not update token attribute value: ".$e->getMessage())); } diff --git a/application/views/admin/participants/attributeMap_view.php b/application/views/admin/participants/attributeMap_view.php index d2e298d8481..697d39600b3 100644 --- a/application/views/admin/participants/attributeMap_view.php +++ b/application/views/admin/participants/attributeMap_view.php @@ -58,8 +58,8 @@