Skip to content

Commit

Permalink
Fixed issue #7175: Problems with attributes not being copied when cop…
Browse files Browse the repository at this point in the history
…ying from tokens to participants database
  • Loading branch information
c-schmitz committed Feb 20, 2013
1 parent c7d2ec8 commit 0a539b3
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 88 deletions.
2 changes: 1 addition & 1 deletion application/controllers/admin/participantsaction.php
Expand Up @@ -1482,7 +1482,7 @@ function addToCentral()
$overwriteman = Yii::app()->request->getPost('overwriteman');
$createautomap = Yii::app()->request->getPost('createautomap');

$response = Participants::model()->copyToCentral(Yii::app()->request->getPost('surveyid'), $newarr, $mapped, $overwriteauto, $overwriteman);
$response = Participants::model()->copyToCentral(Yii::app()->request->getPost('surveyid'), $newarr, $mapped, $overwriteauto, $overwriteman, $createautomap);
$clang = $this->getController()->lang;

printf($clang->gT("%s participants have been copied to the central participants table"), $response['success']);
Expand Down
4 changes: 2 additions & 2 deletions application/models/ParticipantAttributeNames.php
Expand Up @@ -360,8 +360,8 @@ function editParticipantAttributeValue($data)
Yii::app()->db->createCommand()
->update('{{participant_attribute}}',
$data,
'participant_id = :participant_id AND attribute_id = :attribute_id',
array(':participant_id' => $data['participant_id'], ':attribute_id'=>$data['attribute_id']));
'participant_id = :participant_id2 AND attribute_id = :attribute_id2',
array(':participant_id2' => $data['participant_id'], ':attribute_id2'=>$data['attribute_id']));
}

}
Expand Down
178 changes: 93 additions & 85 deletions application/models/Participants.php
Expand Up @@ -11,7 +11,7 @@
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*
* $Id$
* $Id$
*/

/**
Expand All @@ -29,18 +29,18 @@
class Participants extends CActiveRecord
{

/**
* Returns the static model of Settings table
*
* @static
* @access public
/**
* Returns the static model of Settings table
*
* @static
* @access public
* @param string $class
* @return Participants
*/
public static function model($class = __CLASS__)
{
return parent::model($class);
}
* @return Participants
*/
public static function model($class = __CLASS__)
{
return parent::model($class);
}

/**
* @return string the associated database table name
Expand Down Expand Up @@ -360,31 +360,31 @@ function getSurveyCount($participant_id)

function deleteParticipant($rows)
{
/* This function deletes the participant from the participants table,
references in the survey_links table (but not in matching tokens tables)
and then all the participants attributes. */
/* This function deletes the participant from the participants table,
references in the survey_links table (but not in matching tokens tables)
and then all the participants attributes. */

// Converting the comma seperated id's to an array to delete multiple rows
// Converting the comma seperated id's to an array to delete multiple rows
$rowid = explode(",", $rows);
foreach ($rowid as $row)
{
Yii::app()->db->createCommand()->delete(Participants::model()->tableName(), array('in', 'participant_id', $row));
Yii::app()->db->createCommand()->delete(Survey_links::model()->tableName(), array('in', 'participant_id', $row));
Yii::app()->db->createCommand()->delete(Participant_attribute::model()->tableName(), array('in', 'participant_id', $row));
Yii::app()->db->createCommand()->delete(Survey_links::model()->tableName(), array('in', 'participant_id', $row));
Yii::app()->db->createCommand()->delete(Participant_attribute::model()->tableName(), array('in', 'participant_id', $row));
}
}

function deleteParticipantToken($rows)
{
/* This function deletes the participant from the participants table,
the participant from any tokens table they're in (using the survey_links table to find them)
and then all the participants attributes. */
/* This function deletes the participant from the participants table,
the participant from any tokens table they're in (using the survey_links table to find them)
and then all the participants attributes. */
$rowid = explode(",", $rows);
foreach ($rowid as $row)
{
$tokens = Yii::app()->db->createCommand()->select('*')->from('{{survey_links}}')->where('participant_id = :pid')->bindParam(":pid", $row, PDO::PARAM_INT)->queryAll();

foreach ($tokens as $key => $value)
foreach ($tokens as $key => $value)
{
$tokentable='{{tokens_'.intval($value['survey_id']).'}}';

Expand All @@ -393,69 +393,69 @@ function deleteParticipantToken($rows)
Yii::app()->db->createCommand()
->delete('{{tokens_' . intval($value['survey_id']) . '}}', 'participant_id = :pid',array(':pid'=>$row)); // Deletes matching token table entries
//Yii::app()->db->createCommand()->delete(Tokens::model()->tableName(), array('in', 'participant_id', $row));
}
}
}
Yii::app()->db->createCommand()->delete(Participants::model()->tableName(), array('in', 'participant_id', $row));
Yii::app()->db->createCommand()->delete(Survey_links::model()->tableName(), array('in', 'participant_id', $row));
Yii::app()->db->createCommand()->delete(Participant_attribute::model()->tableName(), array('in', 'participant_id', $row));
Yii::app()->db->createCommand()->delete(Participants::model()->tableName(), array('in', 'participant_id', $row));
Yii::app()->db->createCommand()->delete(Survey_links::model()->tableName(), array('in', 'participant_id', $row));
Yii::app()->db->createCommand()->delete(Participant_attribute::model()->tableName(), array('in', 'participant_id', $row));

}
}

function deleteParticipantTokenAnswer($rows)
{
/* This function deletes the participant from the participants table,
the participant from any tokens table they're in (using the survey_links table to find them),
all responses in surveys they've been linked to,
and then all the participants attributes. */
$rowid = explode(",", $rows);
foreach ($rowid as $row)
{
$tokens = Yii::app()->db->createCommand()
function deleteParticipantTokenAnswer($rows)
{
/* This function deletes the participant from the participants table,
the participant from any tokens table they're in (using the survey_links table to find them),
all responses in surveys they've been linked to,
and then all the participants attributes. */
$rowid = explode(",", $rows);
foreach ($rowid as $row)
{
$tokens = Yii::app()->db->createCommand()
->select('*')
->from('{{survey_links}}')
->where('participant_id = :row')
->bindParam(":row", $row, PDO::PARAM_INT)
->queryAll();

foreach ($tokens as $key => $value)
{
foreach ($tokens as $key => $value)
{
$tokentable='{{tokens_'.intval($value['survey_id']).'}}';
if (Yii::app()->db->schema->getTable($tokentable))
{
$tokenid = Yii::app()->db->createCommand()
if (Yii::app()->db->schema->getTable($tokentable))
{
$tokenid = Yii::app()->db->createCommand()
->select('token')
->from('{{tokens_' . intval($value['survey_id']) . '}}')
->where('participant_id = :pid')
->bindParam(":pid", $value['participant_id'], PDO::PARAM_INT)
->queryAll();
$token = $tokenid[0];
$token = $tokenid[0];
$surveytable='{{survey_'.intval($value['survey_id']).'}}';
if ($datas=Yii::app()->db->schema->getTable($surveytable))
{
if (!empty($token['token']) && isset($datas->columns['token'])) //Make sure we have a token value, and that tokens are used to link to the survey
{
$gettoken = Yii::app()->db->createCommand()
if ($datas=Yii::app()->db->schema->getTable($surveytable))
{
if (!empty($token['token']) && isset($datas->columns['token'])) //Make sure we have a token value, and that tokens are used to link to the survey
{
$gettoken = Yii::app()->db->createCommand()
->select('*')
->from('{{survey_' . intval($value['survey_id']) . '}}')
->where('token = :token')
->bindParam(":token", $token['token'], PDO::PARAM_STR)
->queryAll();
$gettoken = $gettoken[0];
Yii::app()->db->createCommand()
$gettoken = $gettoken[0];
Yii::app()->db->createCommand()
->delete('{{survey_' . intval($value['survey_id']) . '}}', 'token = :token')
->bindParam(":token", $gettoken['token'], PDO::PARAM_STR); // Deletes matching responses from surveys
}
}
Yii::app()->db->createCommand()
}
}
Yii::app()->db->createCommand()
->delete('{{tokens_' . intval($value['survey_id']) . '}}', 'participant_id = :pid' , array(':pid'=>$value['participant_id'])); // Deletes matching token table entries
}
}
Yii::app()->db->createCommand()->delete(Participants::model()->tableName(), array('in', 'participant_id', $row));
Yii::app()->db->createCommand()->delete(Survey_links::model()->tableName(), array('in', 'participant_id', $row));
Yii::app()->db->createCommand()->delete(Participant_attribute::model()->tableName(), array('in', 'participant_id', $row));
}
}
}
}
Yii::app()->db->createCommand()->delete(Participants::model()->tableName(), array('in', 'participant_id', $row));
Yii::app()->db->createCommand()->delete(Survey_links::model()->tableName(), array('in', 'participant_id', $row));
Yii::app()->db->createCommand()->delete(Participant_attribute::model()->tableName(), array('in', 'participant_id', $row));
}
}

/*
* Function builds a select query for searches through participants using the $condition field passed
Expand All @@ -472,8 +472,8 @@ function deleteParticipantTokenAnswer($rows)
* */
function getParticipantsSearchMultiple($condition, $page, $limit)
{
//http://localhost/limesurvey_yii/admin/participants/getParticipantsResults_json/search/email||contains||gov||and||firstname||contains||AL
//First contains fieldname, second contains method, third contains value, fourth contains BOOLEAN SQL and, or
//http://localhost/limesurvey_yii/admin/participants/getParticipantsResults_json/search/email||contains||gov||and||firstname||contains||AL
//First contains fieldname, second contains method, third contains value, fourth contains BOOLEAN SQL and, or

//As we iterate through the conditions we build up the $command query by adding conditions to it
//
Expand Down Expand Up @@ -646,7 +646,7 @@ function getParticipantsSearchMultiple($condition, $page, $limit)
}
}

if ($page == 0 && $limit == 0)
if ($page == 0 && $limit == 0)
{
$arr = Participants::model()->findAll($command);
$data = array();
Expand Down Expand Up @@ -683,8 +683,8 @@ function getParticipantsSearchMultiple($condition, $page, $limit)
*/
function getParticipantsSearchMultipleCondition($condition)
{
//http://localhost/limesurvey_yii/admin/participants/getParticipantsResults_json/search/email||contains||gov||and||firstname||contains||AL
//First contains fieldname, second contains method, third contains value, fourth contains BOOLEAN SQL and, or
//http://localhost/limesurvey_yii/admin/participants/getParticipantsResults_json/search/email||contains||gov||and||firstname||contains||AL
//First contains fieldname, second contains method, third contains value, fourth contains BOOLEAN SQL and, or

//As we iterate through the conditions we build up the $command query by adding conditions to it
//
Expand Down Expand Up @@ -854,6 +854,7 @@ function getParticipantSharedAll()
* */
function copytosurveyatt($surveyid, $mapped, $newcreate, $participantid, $overwriteauto=false, $overwriteman=false, $overwritest=false, $createautomap=true)
{
debugbreak();
Yii::app()->loadHelper('common');
$duplicate = 0;
$sucessfull = 0;
Expand Down Expand Up @@ -1191,7 +1192,7 @@ function updateAttributeValueToken($surveyId, $participantId, $participantAttrib
*
* @param int $surveyid The id of the survey, used to find the appropriate tokens table
* @param array $newarr An array containing the names of token attributes that have to be created in the cpdb
* @param array $mapped An array containing the names of token attributes that are to be mapped to an existing cpdb attribute
* @param array $aMapped An array containing the names of token attributes that are to be mapped to an existing cpdb attribute
* @param bool $overwriteauto If true, overwrites existing automatically mapped attribute values (where token fieldname=attribute_cpdb_n)
* @param bool $overwriteman If true, overwrites manually mapped attribute values (where token fieldname=attribute_n)
* @param bool $createautomap If true, updates manuall mapped token fields to fieldname=attribute_cpdb_n from fieldname=attribute_n, s in future mapping is automatic
Expand All @@ -1201,12 +1202,13 @@ function updateAttributeValueToken($surveyId, $participantId, $participantAttrib
* @return array An array contaning list of successful and list of failed ids
*/

function copyToCentral($surveyid, $newarr, $mapped, $overwriteauto=false, $overwriteman=false, $createautomap=true)
function copyToCentral($surveyid, $newarr, $aMapped, $overwriteauto=false, $overwriteman=false, $createautomap=true)
{
$tokenid = Yii::app()->session['participantid']; //List of token_id's to add to participants table
$duplicate = 0;
$sucessfull = 0;
$writearray = array();
$aAutoMapped=array();
$attid = array(); //Will store the CPDB attribute_id of new or existing attributes keyed by CPDB at
$pid = "";

Expand All @@ -1227,7 +1229,7 @@ function copyToCentral($surveyid, $newarr, $mapped, $overwriteauto=false, $overw
if ($value[10] == 'c') /* This is going to cause a problem one day! It's deciding that an item is an automatically mapped because the 10th letter is "c"*/
{
$autoattid = substr($value, 15);
$mapped[$autoattid] = $value;
$aAutoMapped[$autoattid] = $value;
}
}

Expand Down Expand Up @@ -1283,23 +1285,25 @@ function copyToCentral($surveyid, $newarr, $mapped, $overwriteauto=false, $overw
{
$duplicate++;
//HERE is where we can add "overwrite" feature to update attribute values for existing participants
if($overwriteauto == "true") {
if (!empty($newarr))
if($overwriteauto == "true" && !empty($newarr))
{
foreach ($newarr as $key => $value)
{
foreach ($newarr as $key => $value)
{
Participants::model()->updateAttributeValueToken($surveyid, $query[0]['participant_id'], $attid[$key], $key);
}
Participants::model()->updateAttributeValueToken($surveyid, $query[0]['participant_id'], $attid[$key], $key);
}
}
if($overwriteman == "true") {
/* Now add mapped attribute values */
if (!empty($mapped))
if($overwriteman == "true" && !empty($aMapped))
{
foreach ($aMapped as $cpdbatt => $tatt)
{
foreach ($mapped as $cpdbatt => $tatt)
{
Participants::model()->updateAttributeValueToken($surveyid, $query[0]['participant_id'], $cpdbatt, $tatt);
}
Participants::model()->updateAttributeValueToken($surveyid, $query[0]['participant_id'], $cpdbatt, $tatt);
}
}
if($overwriteauto == "true" && !empty($aAutoMapped))
{
foreach ($aAutoMapped as $cpdbatt => $tatt)
{
Participants::model()->updateAttributeValueToken($surveyid, $query[0]['participant_id'], $cpdbatt, $tatt);
}
}
}
Expand Down Expand Up @@ -1334,9 +1338,13 @@ function copyToCentral($surveyid, $newarr, $mapped, $overwriteauto=false, $overw
}
}
/* Now add mapped attribute values */
if (!empty($mapped))
if (!empty($aMapped))
{
foreach ($mapped as $cpdbatt => $tatt)
foreach ($aMapped as $cpdbatt => $tatt)
{
Participants::model()->updateAttributeValueToken($surveyid,$pid,$cpdbatt,$tatt);
}
foreach ($aAutoMapped as $cpdbatt => $tatt)
{
Participants::model()->updateAttributeValueToken($surveyid,$pid,$cpdbatt,$tatt);
}
Expand Down Expand Up @@ -1392,9 +1400,9 @@ function copyToCentral($surveyid, $newarr, $mapped, $overwriteauto=false, $overw
'sid = '.$surveyid); //load description in the surveys table
}
}
if (!empty($mapped))
if (!empty($aMapped))
{
foreach ($mapped as $cpdbatt => $tatt)
foreach ($aMapped as $cpdbatt => $tatt)
{
if ($tatt[10] != 'c' && $createautomap=="true") //This attribute is not already mapped
{
Expand Down Expand Up @@ -1433,7 +1441,7 @@ function copyToCentral($surveyid, $newarr, $mapped, $overwriteauto=false, $overw
return $returndata;
}

/*
/*
* The purpose of this function is to check for duplicate in participants
*/

Expand Down

0 comments on commit 0a539b3

Please sign in to comment.