From 33fc880bd4cfe2a95014eec9a3178897b4d27ecf Mon Sep 17 00:00:00 2001 From: Olle Haerstedt Date: Mon, 29 Feb 2016 13:17:12 +0100 Subject: [PATCH] Fixed issue #10623: When exporting CPDB to token, automatic mappings should be pre-mapped --- .../controllers/admin/participantsaction.php | 51 ++++++++++++++----- .../admin/participants/attributeMap_view.php | 13 ++++- scripts/admin/attributeMap.js | 2 + 3 files changed, 52 insertions(+), 14 deletions(-) diff --git a/application/controllers/admin/participantsaction.php b/application/controllers/admin/participantsaction.php index ed621105166..7a3a1e0176d 100644 --- a/application/controllers/admin/participantsaction.php +++ b/application/controllers/admin/participantsaction.php @@ -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 + $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)) { @@ -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, @@ -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); diff --git a/application/views/admin/participants/attributeMap_view.php b/application/views/admin/participants/attributeMap_view.php index 682432f1921..159f873e09b 100644 --- a/application/views/admin/participants/attributeMap_view.php +++ b/application/views/admin/participants/attributeMap_view.php @@ -74,9 +74,18 @@
$value) + foreach ($selectedtokenattribute as $id => $name) { - echo "
" . $value . "
"; + if (isset($automaticallyMappedAttributes[$id])) + { + $autoAttr = $automaticallyMappedAttributes[$id]; + echo "
" . $name . "
"; + echo "
" . $autoAttr['cpdbAttribute']['attribute_name'] . "
"; + } + else + { + echo "
" . $name . "
"; + } } ?>
diff --git a/scripts/admin/attributeMap.js b/scripts/admin/attributeMap.js index d4eaf71ed6d..e6d5f5e1b02 100644 --- a/scripts/admin/attributeMap.js +++ b/scripts/admin/attributeMap.js @@ -162,4 +162,6 @@ $(document).ready(function(){ $('#attribute-map-participant-modal').modal(); }); }); + + tokencurrentarray = $('.tokenatt').sortable('toArray'); });