Skip to content

Commit

Permalink
Fixed issue #10623: When exporting CPDB to token, automatic mappings …
Browse files Browse the repository at this point in the history
…should be pre-mapped
  • Loading branch information
olleharstedt committed Feb 29, 2016
1 parent 33a0f24 commit 33fc880
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 14 deletions.
51 changes: 39 additions & 12 deletions application/controllers/admin/participantsaction.php
Expand Up @@ -1613,26 +1613,26 @@ function attributeMap()
$redirect = Yii::app()->request->getPost('redirect');
$count = Yii::app()->request->getPost('count');
$iParticipantId = Yii::app()->request->getPost('participant_id');
$attributes = ParticipantAttributeName::model()->getCPDBAttributes();
$tokenattributefieldnames = getTokenFieldsAndNames($iSurveyId, TRUE);
$CPDBAttributes = ParticipantAttributeName::model()->getCPDBAttributes();
$tokenAttributes = getTokenFieldsAndNames($iSurveyId, TRUE);

$selectedattribute = array(); //List of existing attribute fields that are not mapped
$selectedcentralattribute = array(); //List of attributes that haven't already been mapped
$alreadymappedattid = array(); //List of fields already mapped to this tokens table
$alreadymappedattname = array();
$selectedattribute = []; //List of existing attribute fields that are not mapped

This comment has been minimized.

Copy link
@Shnoulle

Shnoulle Feb 29, 2016

Collaborator

PHP 5.4 only :) there are a pull request acceped some day ago :)

$selectedcentralattribute = []; //List of attributes that haven't already been mapped
$alreadymappedattid = []; //List of fields already mapped to this tokens table
$alreadymappedattname = [];

foreach ($tokenattributefieldnames as $key => $value)
foreach ($tokenAttributes as $attributeId => $attribute) // attributeId like 'attribute_1'
{
if (is_numeric($key[10])) //Assumes that if the 11th character is a number, it must be a token-table created attribute
if (is_numeric($attributeId[10])) //Assumes that if the 11th character is a number, it must be a token-table created attribute
{
$selectedattribute[$key] = $value['description'];
$selectedattribute[$attributeId] = $attribute['description'];
}
else
{
array_push($alreadymappedattid, substr($key, 15));
array_push($alreadymappedattid, substr($attributeId, 15));
}
}
foreach ($attributes as $row)
foreach ($CPDBAttributes as $row)
{
if (!in_array($row['attribute_id'], $alreadymappedattid))
{
Expand All @@ -1644,10 +1644,37 @@ function attributeMap()
}
}

// Check for automatic mappings
// TODO: Maybe do this with SQL instead?
$automaticallyMappedAttributes = [];
foreach ($tokenAttributes as $attributeId => $tokenAttribute) // attributeId like 'attribute_1'
{
if ($tokenAttribute['cpdbmap'] !== '')
{
foreach ($CPDBAttributes as $CPDBAttribute)
{
if ($CPDBAttribute['attribute_id'] === intval($tokenAttribute['cpdbmap']))
{
$automaticallyMappedAttributes[$attributeId] = [
'tokenAttributeId' => $attributeId,
'tokenAttribute' => $tokenAttribute,
'cpdbAttribute' => $CPDBAttribute
];
}
}
}
}
// Remove automatic mappings from CPDB list (they should only be in right-most list)
foreach ($automaticallyMappedAttributes as $autoAttr)
{
unset($selectedcentralattribute[$autoAttr['cpdbAttribute']['attribute_id']]);
}

$aData = array(
'selectedcentralattribute' => $selectedcentralattribute,
'selectedtokenattribute' => $selectedattribute,
'alreadymappedattributename' => $alreadymappedattname,
'automaticallyMappedAttributes' => $automaticallyMappedAttributes,
'survey_id' => $iSurveyId,
'redirect' => $redirect,
'participant_id' => $iParticipantId,
Expand All @@ -1656,7 +1683,7 @@ function attributeMap()

if (count($selectedcentralattribute) === 0)
{
Yii::app()->setFlashMessage(gT("There are no unmapped attributes"), 'warning');
Yii::app()->setFlashMessage(gT("There are no unmapped attributes"), 'info');
}

$this->_renderWrappedTemplate('participants', 'attributeMap', $aData);
Expand Down
13 changes: 11 additions & 2 deletions application/views/admin/participants/attributeMap_view.php
Expand Up @@ -74,9 +74,18 @@
<div class='panel-body'>
<div class="tokenatt ui-sortable">
<?php
foreach ($selectedtokenattribute as $key => $value)
foreach ($selectedtokenattribute as $id => $name)
{
echo "<div class='panel panel-default' id='t_" . $key . "'><div class='panel-body'>" . $value . "</div></div>";
if (isset($automaticallyMappedAttributes[$id]))
{
$autoAttr = $automaticallyMappedAttributes[$id];
echo "<div class='panel panel-default' style='opacity: 1; top: 0px; color: rgb(255, 255, 255); border-top-width: 0px; background-color: rgb(105, 101, 101);' id='t_" . $id . "'><div class='panel-body'>" . $name . "</div></div>";
echo "<div class='panel panel-default' style='opacity: 1; top: 0px; color: rgb(255, 255, 255); border-top-width: 0px; background-color: rgb(105, 101, 101);' id='c_" . $autoAttr['cpdbAttribute']['attribute_id'] . "'><div class='panel-body'>" . $autoAttr['cpdbAttribute']['attribute_name'] . "</div></div>";
}
else
{
echo "<div class='panel panel-default' id='t_" . $id . "'><div class='panel-body'>" . $name . "</div></div>";
}
}
?>
</div>
Expand Down
2 changes: 2 additions & 0 deletions scripts/admin/attributeMap.js
Expand Up @@ -162,4 +162,6 @@ $(document).ready(function(){
$('#attribute-map-participant-modal').modal();
});
});

tokencurrentarray = $('.tokenatt').sortable('toArray');
});

0 comments on commit 33fc880

Please sign in to comment.