Skip to content

Commit

Permalink
Fixed issue: Small nuisances in the cpdb
Browse files Browse the repository at this point in the history
  • Loading branch information
lacrioque committed May 29, 2018
1 parent 1f5bb13 commit d68cb78
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 52 deletions.
78 changes: 33 additions & 45 deletions application/models/Participant.php
Expand Up @@ -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++;

Expand All @@ -1655,64 +1643,64 @@ 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);
}
}
}
if ($options['overwriteman'] == "true") {
//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()));
}
Expand All @@ -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()));
}
Expand Down
4 changes: 2 additions & 2 deletions application/views/admin/participants/attributeMap_view.php
Expand Up @@ -58,8 +58,8 @@
<div class='col-sm-4'>
<div id="newcreated" class="panel panel-primary <?php echo $columnstyle ?>">
<div class="panel-heading"><?php eT("Survey participant attributes to create"); ?></div>
<div class='panel-body'>
<div class="newcreate" id="sortable" style ="">
<div class='panel-body' style="height:100%;">
<div class="newcreate" id="sortable" style ="height:100%;">
</div>
</div>
</div>
Expand Down
10 changes: 5 additions & 5 deletions assets/scripts/admin/participantpanel.js
Expand Up @@ -170,7 +170,7 @@ LS.CPDB = (function() {
return false;
});

$('.action_participant_editModal').on('click', function(e){
$('#list_central_participants').on('click', '.action_participant_editModal', function(e){
e.preventDefault();
var data = {modalTarget: 'editparticipant', 'participant_id' : $(this).closest('tr').data('participant_id')};
//url, data, idString, actionButtonClass, formId, gridViewId
Expand All @@ -186,7 +186,7 @@ LS.CPDB = (function() {
});
});

$('.action_participant_deleteModal').on('click', function(e) {
$('#list_central_participants').on('click', '.action_participant_deleteModal', function(e) {
e.preventDefault();
var data = {modalTarget: 'showdeleteparticipant', 'participant_id' : $(this).closest('tr').data('participant_id')};
//url, data, idString, actionButtonClass, formId, gridViewId
Expand All @@ -198,7 +198,7 @@ LS.CPDB = (function() {
'list_central_participants'
);
});
$('.action_participant_infoModal').on('click', function(e) {
$('#list_central_participants').on('click', '.action_participant_infoModal', function(e) {
e.preventDefault();
var data = {
modalTarget: 'showparticipantsurveys',
Expand All @@ -213,7 +213,7 @@ LS.CPDB = (function() {
'list_central_participants'
);
});
$('.action_participant_shareParticipant').on('click', function(e) {
$('#list_central_participants').on('click', '.action_participant_shareParticipant', function(e) {
e.preventDefault();
var data = {modalTarget: 'shareparticipant', 'participant_id' : $(this).closest('tr').data('participant_id')};
//url, data, idString, actionButtonClass, formId, gridViewId
Expand Down Expand Up @@ -248,7 +248,7 @@ LS.CPDB = (function() {
/**
* Small icon, add participant to a survey
*/
$('.action_participant_addToSurvey').on('click', function(e) {
$('#list_central_participants').on('click', '.action_participant_addToSurvey', function(e) {
var data = {
modalTarget: 'addToSurvey',
participant_id: $(this).closest('tr').data('participant_id')
Expand Down

0 comments on commit d68cb78

Please sign in to comment.