Skip to content

Commit

Permalink
Dev: Show error message if attribute already exists at mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
olleharstedt committed Mar 2, 2016
1 parent e9f47bf commit c78fd07
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
8 changes: 7 additions & 1 deletion application/controllers/admin/participantsaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -1601,9 +1601,15 @@ function addToTokenattmap()
{
$response = Participant::model()->copyCPDBAttributesToTokens($surveyId, $participantIds, $mappedAttributes, $newAttributes, $options);
}
// This exception carries error messages
catch (CPDBException $e)
{
echo $e->getMessage();
return;
}
catch (Exception $e)
{
printf("Error: Could not copy attributes to tokens: file %s, line %s; %s", $e->getFile(), $e->getLine(), $e->getMessage());
printf(gT("Error: Could not copy attributes to tokens: file %s, line %s; %s"), $e->getFile(), $e->getLine(), $e->getMessage());
return;
}

Expand Down
49 changes: 47 additions & 2 deletions application/models/Participant.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
*
*/

/**
* Specific exception for our purpose
* Used to spit out error messages if mapping attributes doesn't work.
*/
class CPDBException extends Exception {}

/**
* This is the model class for table "{{participants}}".
*
Expand Down Expand Up @@ -1001,6 +1007,42 @@ private function updateTokenFieldProperties($surveyId, array $mappedAttributes)
}
}

/**
* Check for column duplicates from CPDB to token attributes
* Throws error message if an attribute already exists; otherwise false.
*
* @param int $surveyId
* @param string[] $newAttributes Array of CPDB attributes ids like ['42', '32', ...]
* @return boolean
* @throws CPDBException with error message
*/
private function checkColumnDuplicates($surveyId, array $newAttributes)
{
$tokenTableSchema = Yii::app()->db
->schema
->getTable("{{tokens_$surveyId}}");


foreach ($tokenTableSchema->columns as $columnName => $columnObject)
{
if (strpos($columnName, 'attribute_') !== false)
{
$id = substr($columnName, 10);
if (in_array($id, $newAttributes))
{
$name = ParticipantAttributeName::model()->getAttributeName($id, $_SESSION['adminlang']);
if (empty($name)) {
$name = array('attribute_name' => '[Found no name]');
}
throw new CPDBException(sprintf(gT("Token attribute already exists: %s"), $name['attribute_name']));
}
}
}

return false;

}

/**
* Create new "fields"? in which table?
*
Expand Down Expand Up @@ -1095,7 +1137,7 @@ private function createColumnsInTokenTable($surveyId, array $newAttributes)
}
Yii::app()->db->schema->getTable("{{tokens_$surveyId}}", true); // Refresh schema cache just

return [$addedAttributes, $addedAttributeIds];
return array($addedAttributes, $addedAttributeIds);
}

/**
Expand Down Expand Up @@ -1292,6 +1334,9 @@ public function copyCPDBAttributesToTokens($surveyId, array $participantIds, arr
//$mappedAttributes[$id] = $columnName; // $name is 'attribute_1', which will clash with postgres
//}

// Check for duplicates. Will throw CPDBException if duplicate is found.
$this->checkColumnDuplicates($surveyId, $newAttributes);

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

Expand Down Expand Up @@ -1330,7 +1375,7 @@ function updateTokenAttributeValue($surveyId, $participantId, $participantAttrib

if (intval($participantAttributeId) === 0) // OBS: intval returns 0 at fail, but also at intval("0"). lolphp.
{
throw new InvalidArgumentException(sprintf('$participantAttributeId has to be an integer. Given: %s (%s)', gettype($participantAttributeId), $participantAttributeId));
throw new InvalidArgumentException(sprintf(gT('$participantAttributeId has to be an integer. Given: %s (%s)'), gettype($participantAttributeId), $participantAttributeId));
}

//Get the value from the participant_attribute field
Expand Down

0 comments on commit c78fd07

Please sign in to comment.