Skip to content

Commit

Permalink
Merge branch 'bug/L41-T48-Participant-can-not-be-copied-to-survey-wit…
Browse files Browse the repository at this point in the history
…h-encryption'
  • Loading branch information
ptelu committed Mar 29, 2021
2 parents 0eb3461 + be5981c commit 2909921
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 63 deletions.
13 changes: 2 additions & 11 deletions application/controllers/admin/participantsaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2486,29 +2486,20 @@ public function addToTokenattmap()
* mapped[attribute_38] = 39
* meaning that an attribute is mapped onto another.
*/
$mappedAttributes = Yii::app()->request->getPost('mapped');
$mappedAttributes = Yii::app()->request->getPost('mapped', []);

/**
* newarr takes values like
* newarr[] = 39
*/
$newAttributes = Yii::app()->request->getPost('newarr');
$newAttributes = Yii::app()->request->getPost('newarr', []);

$options = array();
$options['overwriteauto'] = Yii::app()->request->getPost('overwrite') === 'true';
$options['overwriteman'] = Yii::app()->request->getPost('overwriteman') === 'true';
$options['overwritest'] = Yii::app()->request->getPost('overwritest') === 'true';
$options['createautomap'] = Yii::app()->request->getPost('createautomap') === 'true';

// TODO: Why?
if (empty($newAttributes[0])) {
$newAttributes = array();
}

if (empty($mappedAttributes)) {
$mappedAttributes = array();
}

try {
$response = Participant::model()->copyCPDBAttributesToTokens($surveyId, $participantIds, $mappedAttributes, $newAttributes, $options);
}
Expand Down
3 changes: 2 additions & 1 deletion application/models/LSActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ public function findAllByAttributes($attributes, $condition = '', $params = arra
* @param int $iSurveyId
* @param string $sClassName
* @return array
* TODO: Should be split into seperate functions in the appropiate model or helper class
*/
public function getAllEncryptedAttributes($iSurveyId = 0, $sClassName)
{
Expand Down Expand Up @@ -357,7 +358,7 @@ public static function decryptSingle($value = '')

/**
* Enrypt single value
* @param string $value String value which needs to be decrypted
* @param string $value String value which needs to be encrypted
*/
public static function encryptSingle($value = '')
{
Expand Down
105 changes: 54 additions & 51 deletions application/models/Participant.php
Original file line number Diff line number Diff line change
Expand Up @@ -1675,6 +1675,7 @@ private function writeParticipantsToTokenTable(
$blacklistSkipped++;
continue;
}
$oParticipant->decrypt();
$isDuplicate = array_reduce($oTokens, function ($carry, $oToken) use ($oParticipant) {
return $carry ? $carry : ($oToken->participant_id == $oParticipant->participant_id);
}, false);
Expand Down Expand Up @@ -1723,7 +1724,7 @@ private function writeParticipantsToTokenTable(
$oToken->lastname = $oParticipant->lastname;
$oToken->email = $oParticipant->email;
$oToken->language = $oParticipant->language;
if (!$oToken->save()) {
if (!$oToken->encryptSave(true)) {
throw new Exception(CHtml::errorSummary($oToken));
}
$insertedtokenid = $oToken->tid;
Expand Down Expand Up @@ -1809,10 +1810,10 @@ public function copyCPDBAttributesToTokens($surveyId, array $participantIds, arr
$this->checkColumnDuplicates($surveyId, $newAttributes);

// TODO: Why use two variables for this?
list($addedAttributes, $addedAttributeIds) = $this->createColumnsInTokenTable($surveyId, $newAttributes);
[$addedAttributes, $addedAttributeIds] = $this->createColumnsInTokenTable($surveyId, $newAttributes);

//Write each participant to the survey survey participants table
list($successful, $duplicate, $blacklistSkipped) = $this->writeParticipantsToTokenTable(
[$successful, $duplicate, $blacklistSkipped] = $this->writeParticipantsToTokenTable(
$surveyId,
$participantIds,
$mappedAttributes,
Expand All @@ -1822,13 +1823,13 @@ public function copyCPDBAttributesToTokens($surveyId, array $participantIds, arr
$options
);

$returndata = array(
'success' => $successful,
'duplicate' => $duplicate,
$returndata = [
'success' => $successful,
'duplicate' => $duplicate,
'blacklistskipped' => $blacklistSkipped,
'overwriteauto' => $options['overwriteauto'],
'overwriteman' => $options['overwriteman']
);
'overwriteauto' => $options['overwriteauto'],
'overwriteman' => $options['overwriteman']
];
return $returndata;
}

Expand Down Expand Up @@ -1940,7 +1941,7 @@ public function copyToCentral($surveyid, $aAttributesToBeCreated, $aMapped, $ove
$tokenids = json_decode($tokenid_string, true);
$duplicate = 0;
$sucessfull = 0;
$attid = array(); //Will store the CPDB attribute_id of new or existing attributes keyed by CPDB at
$attid = []; //Will store the CPDB attribute_id of new or existing attributes keyed by CPDB at

$aTokenAttributes = decodeTokenAttributes($survey->attributedescriptions);
$aAutoMapped=$survey->getCPDBMappings();
Expand All @@ -1952,22 +1953,22 @@ public function copyToCentral($surveyid, $aAttributesToBeCreated, $aMapped, $ove
/* $key is the fieldname from the survey participants table (ie "attribute_1")
* $value is the 'friendly name' for the attribute (ie "Gender")
*/
$insertnames = array(
$insertnames = [
'attribute_type' => 'TB',
'visible' => 'Y',
'encrypted' => $aTokenAttributes[$key]['encrypted'],
'defaultname' => $value
);
'visible' => 'Y',
'encrypted' => $aTokenAttributes[$key]['encrypted'],
'defaultname' => $value
];
$oParticipantAttributeNames = new ParticipantAttributeName();
$oParticipantAttributeNames->setAttributes($insertnames, false);
$oParticipantAttributeNames->save(false);
$attid[$key] = $oParticipantAttributeNames->getPrimaryKey();

$insertnameslang = array(
'attribute_id' => $attid[$key],
$insertnameslang = [
'attribute_id' => $attid[$key],
'attribute_name' => urldecode($value),
'lang' => Yii::app()->session['adminlang']
);
'lang' => Yii::app()->session['adminlang']
];
$oParticipantAttributeNamesLang = new ParticipantAttributeNameLang();
$oParticipantAttributeNamesLang->setAttributes($insertnameslang, false);
$oParticipantAttributeNamesLang->save(false);
Expand All @@ -1976,12 +1977,15 @@ public function copyToCentral($surveyid, $aAttributesToBeCreated, $aMapped, $ove

/* Add the participants to the CPDB = Iterate through each $tokenid and create the new CPDB id*/
if (!is_array($tokenids)) {
$tokenids = (array) $tokenids;
$tokenids = (array)$tokenids;
}
foreach ($tokenids as $tid) {
if (is_numeric($tid) && $tid != "") {
/* Get the data for this participant from the tokens table */
$oTokenDynamic = TokenDynamic::model($survey->sid)->findByPk($tid);
if (isset($oTokenDynamic) && $oTokenDynamic) {
$oTokenDynamic->decrypt();
}

/* See if there are any existing CPDB entries that match on firstname,lastname and email */
$participantCriteria = new CDbCriteria();
Expand All @@ -1990,8 +1994,8 @@ public function copyToCentral($surveyid, $aAttributesToBeCreated, $aMapped, $ove
$participantCriteria->addCondition('email = :email');
$participantCriteria->params = [
":firstname" => $oTokenDynamic->firstname,
":lastname" => $oTokenDynamic->lastname,
":email" => $oTokenDynamic->email,
":lastname" => $oTokenDynamic->lastname,
":email" => $oTokenDynamic->email,
];
$existing = Participant::model()->find($participantCriteria);
/* If there is already an existing entry, add to the duplicate count */
Expand All @@ -2006,31 +2010,30 @@ public function copyToCentral($surveyid, $aAttributesToBeCreated, $aMapped, $ove
foreach ($aAutoMapped as $cpdbatt => $tatt) {
Participant::model()->updateAttributeValueToken($surveyid, $existing->participant_id, $cpdbatt, $tatt);
}
}
}
/* If there isn't an existing entry, create one! */ else {
}
} /* If there isn't an existing entry, create one! */ else {
/* Create entry in participants table */
$black = !empty($oTokenDynamic->blacklisted) ? $oTokenDynamic->blacklisted : 'N';
$pid = !empty($oTokenDynamic->participant_id) ? $oTokenDynamic->participant_id : $this->gen_uuid();

$writearray = array(
$writearray = [
'participant_id' => $pid,
'firstname' => $oTokenDynamic->firstname,
'lastname' => $oTokenDynamic->lastname,
'email' => $oTokenDynamic->email,
'language' => $oTokenDynamic->language,
'blacklisted' => $black,
'owner_uid' => Yii::app()->session['loginID'],
'created_by' => Yii::app()->session['loginID'],
'created' => date('Y-m-d H:i:s', time())
);
'firstname' => $oTokenDynamic->firstname,
'lastname' => $oTokenDynamic->lastname,
'email' => $oTokenDynamic->email,
'language' => $oTokenDynamic->language,
'blacklisted' => $black,
'owner_uid' => Yii::app()->session['loginID'],
'created_by' => Yii::app()->session['loginID'],
'created' => date('Y-m-d H:i:s', time())
];
$oParticipant = new Participant();
$oParticipant->setAttributes($writearray, false);
$oParticipant->save(false);
$oParticipant->encryptSave();

//Update survey participants table and insert the new UUID
$oTokenDynamic->participant_id = $pid;
$oTokenDynamic->save(false);
$oTokenDynamic->encryptSave();

/* Now add any new attribute values */
if (!empty($aAttributesToBeCreated)) {
Expand All @@ -2048,12 +2051,12 @@ public function copyToCentral($surveyid, $aAttributesToBeCreated, $aMapped, $ove

/* Create a survey_link */
$oSurveyLink = new SurveyLink();
$data = array(
$data = [
'participant_id' => $pid,
'token_id' => $tid,
'survey_id' => $surveyid,
'date_created' => date('Y-m-d H:i:s', time())
);
'token_id' => $tid,
'survey_id' => $surveyid,
'date_created' => date('Y-m-d H:i:s', time())
];
$oSurveyLink->setAttributes($data, false);
$oSurveyLink->save(false);
}
Expand All @@ -2069,7 +2072,7 @@ public function copyToCentral($surveyid, $aAttributesToBeCreated, $aMapped, $ove
}
Yii::app()->db
->createCommand()
->update('{{surveys}}', array("attributedescriptions" => json_encode($aAttributes)), 'sid = ' . $surveyid);
->update('{{surveys}}', ["attributedescriptions" => json_encode($aAttributes)], 'sid = ' . $surveyid);
}
if (!empty($aMapped)) {
foreach ($aMapped as $cpdbatt => $tatt) {
Expand All @@ -2078,10 +2081,10 @@ public function copyToCentral($surveyid, $aAttributesToBeCreated, $aMapped, $ove
}
Yii::app()->db
->createCommand()
->update('{{surveys}}', array("attributedescriptions" => json_encode($aAttributes)), 'sid = ' . $surveyid);
->update('{{surveys}}', ["attributedescriptions" => json_encode($aAttributes)], 'sid = ' . $surveyid);
}
}
$returndata = array('success' => $sucessfull, 'duplicate' => $duplicate, 'overwriteauto' => $overwriteauto, 'overwriteman' => $overwriteman);
$returndata = ['success' => $sucessfull, 'duplicate' => $duplicate, 'overwriteauto' => $overwriteauto, 'overwriteman' => $overwriteman];
return $returndata;
}

Expand Down Expand Up @@ -2230,13 +2233,13 @@ public static function getParticipantsEncryptionOptions()
if (empty($aOptions)) {
$aOptions = Participant::getDefaultEncryptionOptions();
return $aOptions;
} else {
$aOptionReturn['enabled'] = 'Y';
foreach ($aOptions as $key => $value) {
$aOptionReturn['columns'][$value['defaultname']] = $value['encrypted'];
}
return $aOptionReturn;
}

$aOptionReturn['enabled'] = 'Y';
foreach ($aOptions as $key => $value) {
$aOptionReturn['columns'][$value['defaultname']] = $value['encrypted'];
}
return $aOptionReturn;
}

public static function getDefaultEncryptionOptions()
Expand Down

0 comments on commit 2909921

Please sign in to comment.