Skip to content

Commit

Permalink
New feature: ability to overwrite/create attribute values for existin…
Browse files Browse the repository at this point in the history
…g participants when importing from a CSV file
  • Loading branch information
jcleeland committed Jul 15, 2012
1 parent e0d1c8e commit df43bb5
Show file tree
Hide file tree
Showing 12 changed files with 120 additions and 34 deletions.
30 changes: 26 additions & 4 deletions application/controllers/admin/participantsaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ function displayParticipants()
// if user is superadmin, all survey names
$urlSearch=Yii::app()->request->getQuery('searchurl');
$urlSearch=!empty($urlSearch) ? "getParticipantsResults_json/search/$urlSearch" : "getParticipants_json";
//echo $urlSearch; die();

if (Yii::app()->session['USER_RIGHT_SUPERADMIN'])
{
$aSurveyNames = Surveys_languagesettings::model()->with('survey', 'owner')->findAll('surveyls_language=:lang', array(':lang'=>$lang));
Expand All @@ -123,7 +123,6 @@ function displayParticipants()
$tSurveyNames=array();
foreach($aSurveyNames as $row)
{
//echo $row['surveyls_survey_id']."<br />";
$bTokenExists = tableExists('{{tokens_' . $row['surveyls_survey_id'] . '}}');
if ($bTokenExists) //If tokens table exists
{
Expand Down Expand Up @@ -1433,13 +1432,15 @@ function uploadCSV()
$mappedarray = Yii::app()->request->getPost('mappedarray');
$sFilePath = Yii::app()->request->getPost('fullfilepath');
$filterblankemails = Yii::app()->request->getPost('filterbea');
$overwrite = Yii::app()->request->getPost('overwrite');
$errorinupload = "";
$tokenlistarray = file($sFilePath);
$recordcount = 0;
$mandatory = 0;
$mincriteria = 0;
$imported = 0;
$dupcount = 0;
$overwritten = 0;
$duplicatelist = array();
$invalidemaillist = array();
$invalidformatlist = array();
Expand Down Expand Up @@ -1556,10 +1557,29 @@ function uploadCSV()
//HACK - converting into SQL instead of doing an array search
$aData = "firstname = '".mysql_real_escape_string($writearray['firstname'])."' AND lastname = '".mysql_real_escape_string($writearray['lastname'])."' AND email = '".mysql_real_escape_string($writearray['email'])."' AND owner_uid = '".Yii::app()->session['loginID']."'";
//End of HACK
$aData = Participants::model()->checkforDuplicate($aData);
if ($aData == true) {
$aData = Participants::model()->checkforDuplicate($aData, "participant_id");
if ($aData !== false) {
$thisduplicate = 1;
$dupcount++;
if($overwrite=="true")
{
//Although this person already exists, we want to update the mapped attribute values
if (!empty($mappedarray)) {
//The mapped array contains the attributes we are
//saving in this import
foreach ($mappedarray as $attid => $attname) {
if (!empty($attname)) {
$bData = array('participant_id' => $aData,
'attribute_id' => $attid,
'value' => $writearray[$attname]);
Participant_attribute::model()->updateParticipantAttributeValue($bData);
} else {
//If the value is empty, don't write the value
}
}
}
$overwritten++;
}
}
if ($thisduplicate == 1) {
$dupfound = true;
Expand Down Expand Up @@ -1641,6 +1661,7 @@ function uploadCSV()
}
$recordcount++;
}

unlink($sFilePath);
$clang = $this->getController()->lang;
$aData = array();
Expand All @@ -1655,6 +1676,7 @@ function uploadCSV()
$aData['invalidattribute'] = $invalidattribute;
$aData['mandatory'] = $mandatory;
$aData['invalidparticipantid'] = $invalidparticipantid;
$aData['overwritten'] = $overwritten;
$this->getController()->render('/admin/participants/uploadSummary_view', $aData);
}

Expand Down
4 changes: 3 additions & 1 deletion application/models/ParticipantAttributeNames.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,9 @@ function storeAttributeCSV($data)
//updates the attribute values in participant_attribute_values
function saveAttributeValue($data)
{
Yii::app()->db->createCommand()->update('{{participant_attribute_values}}', $data, "attribute_id = :attribute_id AND value_id = :value_id", array(":attribute_id" => $data['attribute_id'], ":value_id" => $data['value_id'])); //->bindParam(":attribute_id", $data['attribute_id'], PDO::PARAM_INT)->bindParam(":value_id", $data['value_id'], PDO::PARAM_INT);
Yii::app()->db->createCommand()
->update('{{participant_attribute_values}}', $data, "attribute_id = :attribute_id AND value_id = :value_id", array(":attribute_id" => $data['attribute_id'], ":value_id" => $data['value_id']));
//->bindParam(":attribute_id", $data['attribute_id'], PDO::PARAM_INT)->bindParam(":value_id", $data['value_id'], PDO::PARAM_INT);
}

function saveAttributeVisible($attid,$visiblecondition)
Expand Down
12 changes: 12 additions & 0 deletions application/models/Participant_attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ function getAttributeInfo($participantid)
{
return self::model()->findAllByAttributes(array('participant_id' => $participantid));
}
function updateParticipantAttributeValue($data)
{
$query = Yii::app()->db->createCommand()->select('*')->where('participant_id="'.$data['participant_id'].'" AND attribute_id = '.$data['attribute_id'])->from('{{participant_attribute}}')->queryAll();
if (count($query) > 0)
{
Yii::app()->db->createCommand()
->update('{{participant_attribute}}', $data, 'participant_id = "'.$data['participant_id'].'" AND attribute_id = '.$data['attribute_id']);
} else {
Yii::app()->db->createCommand()
->insert('{{participant_attribute}}', $data);
}
}

}

Expand Down
7 changes: 4 additions & 3 deletions application/models/Participants.php
Original file line number Diff line number Diff line change
Expand Up @@ -1909,12 +1909,13 @@ function copyToCentral($surveyid, $newarr, $mapped, $overwriteauto=false, $overw
* The purpose of this function is to check for duplicate in participants
*/

function checkforDuplicate($fields)
function checkforDuplicate($fields, $output="bool")
{
$query = Yii::app()->db->createCommand()->select('*')->where($fields)->from('{{participants}}')->queryAll();
$query = Yii::app()->db->createCommand()->select('participant_id')->where($fields)->from('{{participants}}')->queryAll();
if (count($query) > 0)
{
return true;
if($output=="bool") {return true;}
return $query[0][$output];
}
else
{
Expand Down
3 changes: 3 additions & 0 deletions application/views/admin/participants/attributeMapCSV_view.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@
}
?>
</ul>
<div class='explanation'>
<input type='checkbox' id='overwrite' name='overwrite' /> <label for='overwrite'><?php $clang->eT("Overwrite existing token attribute values if a duplicate participant is found?") ?></label>
</div>
</div>
</ul>
</div>
Expand Down
20 changes: 12 additions & 8 deletions application/views/admin/participants/uploadSummary_view.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,32 @@
}
else
{
$uploadSummary .= "<div class='warningheader'>" . $clang->gT("Failed to create token entries") . "</div>";
$uploadSummary .= "<div class='warningheader'>" . $clang->gT("No new participants were created") . "</div>";
}
if (!empty($recordcount))
{
$uploadSummary .= "<ul style='margin-left: 105px'><li>" . sprintf($clang->gT("%s records in CSV"), $recordcount) . "</li>";
$uploadSummary .= "<ul><li style='width: 80%'>" . sprintf($clang->gT("%s records found in CSV file"), $recordcount) . "</li>";
}
if (!empty($mandatory))
{
$uploadSummary .= "<li>" . sprintf($clang->gT("%s records have empty mandatory fields"), $mandatory) . "</li>";
$uploadSummary .= "<li style='width: 80%'>" . sprintf($clang->gT("%s records have empty mandatory fields"), $mandatory) . "</li>";
}
$uploadSummary .= "<li>" . sprintf($clang->gT("%s records met minumum requirements"), $mincriteria) . "</li>";
$uploadSummary .= "<li>" . sprintf($clang->gT("%s records imported"), $imported) . "</li></ul>";
$uploadSummary .= "<li style='width: 80%'>" . sprintf($clang->gT("%s records met minumum requirements"), $mincriteria) . "</li>";
$uploadSummary .= "<li style='width: 80%'>" . sprintf($clang->gT("%s new participants were created"), $imported) . "</li>";
if($overwritten > 0) {
$uploadSummary .= "<li style='width: 80%'>".sprintf($clang->gT("%s records were duplicate but had attributes updated"), $overwritten)."</li>";
}
$uploadSummary .="</ul>";
if (count($duplicatelist) > 0 || count($invalidemaillist) > 0 || count($invalidattribute) > 0)
{
$uploadSummary .= "<div class='warningheader'>" . $clang->gT('Warnings') . "</div><ul>";
if (!empty($duplicatelist) && (count($duplicatelist) > 0))
{
$uploadSummary .= "<li style='width: 400px'>" . sprintf($clang->gT("%s duplicate records removed"), count($duplicatelist));
$uploadSummary .= "<div class='badtokenlist' id='duplicateslist' ><ul>";
$uploadSummary .= "<li style='width: 80%'>" . sprintf($clang->gT("%s duplicate entries not created"), count($duplicatelist));
$uploadSummary .= "<div class='badtokenlist' id='duplicateslist'><ul>";
foreach ($duplicatelist as $data)
{
$uploadSummary .= "<li>" . $data . "</li>";
$uploadSummary .= "<li style='width: 95%; margin-left: 0px'>" . $data . "</li>";
}
$uploadSummary .= "</ul></div></li>";
}
Expand Down
17 changes: 12 additions & 5 deletions scripts/admin/attributeMapCSV.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ $(document).ready(function() {
$('#centralattribute').css({'height' : height-200});
$('#csvattribute').css({'height' : height-200});
$('#newcreated').css({'height' : height-200});
if($("#overwrite").is(':checked')) {var attoverwrite=true;} else {var attoverwrite=false;}
$("#overwrite").click(function(){
if($("#overwrite").is(':checked')) {attoverwrite=true;} else {attoverwrite=false;}
});
//The original fieldnames bucket
$(".csvatt").sortable({
connectWith:".cpdbatt,.newcreate",
Expand Down Expand Up @@ -42,11 +46,13 @@ $(document).ready(function() {
$(ui.sender).sortable('cancel');
} else {
$('ul.cpdbatt > li:nth-child('+cpdbattpos+')').css("color","white");
$('ul.cpdbatt > li:nth-child('+csvpos+')').css("color","white");
$("#"+cpdbattid).css("background-color","#328639");
$("#"+cpdbattid).css("border-color","#FFFFFF");
$("#"+csvattid).css("border-color","#FFFFFF");
$("#"+csvattid).css("background-color","#328639");
$('ul.cpdbatt > li:nth-child('+cpdbattpos+')').css("border-bottom","0");
$('ul.cpdbatt > li:nth-child('+csvpos+')').css("color","white");
$('ul.cpdbatt > li:nth-child('+csvpos+')').css("margin-top","-5px");
$('ul.cpdbatt > li:nth-child('+csvpos+')').css("border-top","0");
$('ul.cpdbatt > li:nth-child('+csvpos+')').css("min-height","20px");
$('ul.cpdbatt > li:nth-child('+csvpos+')').css("background-color","#328639");
$("#"+cpdbattid).css("background-color","#328639");
}
}
});
Expand Down Expand Up @@ -113,6 +119,7 @@ $(document).ready(function() {
fullfilepath : thefilepath,
newarray : newcurrentarray,
mappedarray : mappedarray,
overwrite : attoverwrite,
filterbea : filterblankemails
}, function(msg){
$('#processing').parent().find("button").each(function() {
Expand Down
13 changes: 13 additions & 0 deletions styles/blobblueish/attributeMap.css
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,16 @@ li{
background:#696565;
color: white;
}
.explanation {
font-size: 0.8em;
width: 75%;
margin-left: auto;
margin-right: auto;
text-align: left;
padding: 5px 0px 5px 15px;
}
.explanation input {
position: relative;
margin-left: -20px;
top: 3px;
}
13 changes: 13 additions & 0 deletions styles/blobblueish/attributeMapCSV.css
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,16 @@ li{
background:#696565;
color: white;
}
.explanation {
font-size: 0.8em;
width: 75%;
margin-left: auto;
margin-right: auto;
text-align: left;
padding: 5px 0px 5px 15px;
}
.explanation input {
position: relative;
margin-left: -20px;
top: 3px;
}
3 changes: 2 additions & 1 deletion styles/gringegreen/adminstyle.css
Original file line number Diff line number Diff line change
Expand Up @@ -794,11 +794,12 @@ ul.assessmentscope li label:first-child {
}

.badtokenlist {
width: 400px;
width: 95%;
background-color: #FFF;
border: 1px solid #CCC;
height: 50px;
overflow: auto;
overflow-y: scroll;
text-align: left;
margin-bottom: 0px;
font-size: 8pt
Expand Down
1 change: 1 addition & 0 deletions styles/gringegreen/attributeMap.css
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ html, body{
min-height: 100px;
padding-bottom: 50px;
}

#cpdbatt {
min-height: 100px;
}
Expand Down
31 changes: 19 additions & 12 deletions styles/gringegreen/attributeMapCSV.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,19 @@ html, body{
margin-top: 10px;
}

.tokenatt li{
border-color: #696565;
border-style:solid;
border-width:1px;
padding : 4px;
}

.newcreate, .csvatt, .cpdbatt {
min-height: 100px;
padding-bottom: 50px;
}

.cpdbatt {
margin-top: -5px;
}

.cpdbatt li {
margin-top: 10px;
border-bottom: 0px;
min-height: 35px;
}

.newcreate, .csvatt, .cpdbatt {
min-height: 100px;
padding-bottom: 50px;
}

#newcreated, #csvattribute, #centralattribute{
Expand Down Expand Up @@ -86,3 +80,16 @@ li{
background:#696565;
color: white;
}
.explanation {
font-size: 0.8em;
width: 75%;
margin-left: auto;
margin-right: auto;
text-align: left;
padding: 5px 0px 5px 15px;
}
.explanation input {
position: relative;
margin-left: -20px;
top: 3px;
}

0 comments on commit df43bb5

Please sign in to comment.