Skip to content

Commit

Permalink
Fixed issue #12605: Redirect bug when creating dummy participant with…
Browse files Browse the repository at this point in the history
… mandatory attribute
  • Loading branch information
dominikvitt committed Sep 3, 2018
1 parent c4621ec commit dff62fe
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 31 deletions.
69 changes: 44 additions & 25 deletions application/controllers/admin/tokens.php
Expand Up @@ -878,43 +878,44 @@ public function addDummies($iSurveyId, $subaction = '')

//Fix up dates and match to database format
if (trim(Yii::app()->request->getPost('validfrom')) == '') {
$_POST['validfrom'] = null;
$aData['validfrom'] = null;
} else {
$datetimeobj = new Date_Time_Converter(trim(Yii::app()->request->getPost('validfrom')), $dateformatdetails['phpdate'].' H:i');
$_POST['validfrom'] = $datetimeobj->convert('Y-m-d H:i:s');
$aData['validfrom'] = $datetimeobj->convert('Y-m-d H:i:s');
}
if (trim(Yii::app()->request->getPost('validuntil')) == '') {
$_POST['validuntil'] = null;
$aData['validuntil'] = null;
} else {
$datetimeobj = new Date_Time_Converter(trim(Yii::app()->request->getPost('validuntil')), $dateformatdetails['phpdate'].' H:i');
$_POST['validuntil'] = $datetimeobj->convert('Y-m-d H:i:s');
$aData['validuntil'] = $datetimeobj->convert('Y-m-d H:i:s');
}

$aData = array('firstname' => flattenText(Yii::app()->request->getPost('firstname')),
'lastname' => flattenText(Yii::app()->request->getPost('lastname')),
'email' => flattenText(Yii::app()->request->getPost('email')),
'token' => '',
'language' => sanitize_languagecode(Yii::app()->request->getPost('language')),
'sent' => 'N',
'remindersent' => 'N',
'completed' => 'N',
'usesleft' => flattenText(Yii::app()->request->getPost('usesleft')),
'validfrom' => Yii::app()->request->getPost('validfrom'),
'validuntil' => Yii::app()->request->getPost('validuntil'));
$aData['firstname'] = flattenText(Yii::app()->request->getPost('firstname'));
$aData['lastname'] = flattenText(Yii::app()->request->getPost('lastname'));
$aData['email'] = flattenText(Yii::app()->request->getPost('email'));
$aData['token'] = '';
$aData['language'] = sanitize_languagecode(Yii::app()->request->getPost('language'));
$aData['sent'] = 'N';
$aData['remindersent'] = 'N';
$aData['completed'] = 'N';
$aData['usesleft'] = flattenText(Yii::app()->request->getPost('usesleft'));
$aData['amount'] = Yii::app()->request->getPost('amount');
$aData['tokenlength'] = Yii::app()->request->getPost('tokenlen');

// add attributes
$cntAttributeErrors = 0;
$attrfieldnames = getTokenFieldsAndNames($iSurveyId, true);
foreach ($attrfieldnames as $attr_name => $desc) {
$value = flattenText(Yii::app()->request->getPost($attr_name));
if ($desc['mandatory'] == 'Y' && trim($value) == '') {
Yii::app()->setFlashMessage(sprintf(gT('%s cannot be left empty'), $desc['description']), 'error');
$this->getController()->refresh();
$cntAttributeErrors+=1;
}
$aData[$attr_name] = $value;
}

$amount = (int) Yii::app()->request->getPost('amount');
$iTokenLength = (int) Yii::app()->request->getPost('tokenlen');
$aData['amount'] = (int) Yii::app()->request->getPost('amount');
$aData['tokenlength'] = (int) Yii::app()->request->getPost('tokenlen');

// Fill an array with all existing tokens
$existingtokens = array();
Expand All @@ -931,7 +932,7 @@ public function addDummies($iSurveyId, $subaction = '')

$invalidtokencount = 0;
$newDummyToken = 0;
while ($newDummyToken < $amount && $invalidtokencount < 50) {
while ($newDummyToken < $aData['amount'] && $invalidtokencount < 50) {
$token = Token::create($iSurveyId);
$token->setAttributes($aData, false);

Expand All @@ -941,7 +942,7 @@ public function addDummies($iSurveyId, $subaction = '')

$attempts = 0;
do {
$token->token = Token::generateRandomToken($iTokenLength);
$token->token = Token::generateRandomToken($aData['tokenlength']);
$attempts++;
} while (isset($existingtokens[$token->token]) && $attempts < 50);

Expand All @@ -955,7 +956,11 @@ public function addDummies($iSurveyId, $subaction = '')
}
$aData['thissurvey'] = getSurveyInfo($iSurveyId);
$aData['surveyid'] = $iSurveyId;
if (!$invalidtokencount) {
if ($cntAttributeErrors > 0){ // attribute validation errors
$aData['dateformatdetails'] = getDateFormatData(Yii::app()->session['dateformat'], App()->language);
$aData['aAttributeFields'] = getParticipantAttributes($iSurveyId);
$this->_renderWrappedTemplate('token', array('dummytokenform'), $aData);
} elseif (!$invalidtokencount) {
$aData['success'] = true;
Yii::app()->session['flashmessage'] = gT("New dummy participants were added.");
//admin/tokens/sa/browse/surveyid/652779//
Expand All @@ -969,17 +974,31 @@ public function addDummies($iSurveyId, $subaction = '')
."\n<input type='button' value='"
. gT("Browse participants")."' onclick=\"window.open('".$this->getController()->createUrl("admin/tokens/sa/browse/surveyid/$iSurveyId")."', '_top')\" />\n"
);
$this->_renderWrappedTemplate('token', array('message' => $message), $aData);
}

$this->_renderWrappedTemplate('token', array('message' => $message), $aData);


} else {
$iTokenLength = !empty(Token::model($iSurveyId)->survey->tokenlength) ? Token::model($iSurveyId)->survey->tokenlength : 15;
// default values
$aData['firstname'] = '';
$aData['lastname'] = '';
$aData['email'] = '';
$aData['token'] = '';
$aData['language'] = Survey::model()->findByPk($iSurveyId)->language;
$aData['sent'] = 'N';
$aData['remindersent'] = 'N';
$aData['completed'] = 'N';
$aData['usesleft'] = 1;
$aData['validfrom'] = null;
$aData['validuntil'] = null;
$aData['amount'] = 100;
$aData['tokenlength'] = !empty(Token::model($iSurveyId)->survey->tokenlength) ? Token::model($iSurveyId)->survey->tokenlength : 15;


$thissurvey = getSurveyInfo($iSurveyId);
$aData['thissurvey'] = $thissurvey;
$aData['surveyid'] = $iSurveyId;
$aData['tokenlength'] = $iTokenLength;

$aData['dateformatdetails'] = getDateFormatData(Yii::app()->session['dateformat'], App()->language);
$aData['aAttributeFields'] = getParticipantAttributes($iSurveyId);
$this->_renderWrappedTemplate('token', array('dummytokenform'), $aData);
Expand Down
23 changes: 17 additions & 6 deletions application/views/admin/token/dummytokenform.php
Expand Up @@ -23,7 +23,7 @@
<div class="form-group">
<label class=" control-label" for='amount'><?php eT("Number of participants:"); ?></label>
<div class="">
<input class='form-control' type='text' size='20' id='amount' name='amount' value="100" />
<input class='form-control' type='text' size='20' id='amount' name='amount' value="<?php echo $amount; ?>" />
</div>
</div>

Expand All @@ -39,42 +39,53 @@
<div class="form-group">
<label class=" control-label" for='firstname'><?php eT("First name"); ?>:</label>
<div class="">
<input class='form-control' type='text' size='30' id='firstname' name='firstname' value="" />
<input class='form-control' type='text' size='30' id='firstname' name='firstname' value="<?php echo $firstname; ?>" />
</div>
</div>

<!-- Last name -->
<div class="form-group">
<label class=" control-label" for='lastname'><?php eT("Last name"); ?>:</label>
<div class="">
<input class='form-control' type='text' size='30' id='lastname' name='lastname' value="" />
<input class='form-control' type='text' size='30' id='lastname' name='lastname' value="<?php echo $lastname; ?>" />
</div>
</div>

<!-- Email -->
<div class="form-group">
<label class=" control-label" for='email'><?php eT("Email address:"); ?></label>
<div class="">
<input class='form-control' type='email' maxlength='320' size='50' id='email' name='email' value="" />
<input class='form-control' type='email' maxlength='320' size='50' id='email' name='email' value="<?php echo $email; ?>" />
</div>
</div>

<!-- Language -->
<div class="form-group">
<label class=" control-label" for='language'><?php eT("Language"); ?>:</label>
<div class="">
<?php echo languageDropdownClean($surveyid, Survey::model()->findByPk($surveyid)->language); ?>
<?php echo languageDropdownClean($surveyid, $language); ?>
</div>
</div>

<!-- Uses left -->
<div class="form-group">
<label class=" control-label" for='usesleft'><?php eT("Uses left:"); ?></label>
<div class="">
<input class='form-control' type='text' size='20' id='usesleft' name='usesleft' value="1" />
<input class='form-control' type='text' size='20' id='usesleft' name='usesleft' value="<?php echo $usesleft; ?>" />
</div>
</div>

<?php
if (isset($validfrom) && $validfrom != 'N')
{
$validfrom = convertToGlobalSettingFormat($validfrom, true);
}

if (isset($validuntil) && $validuntil != 'N')
{
$validuntil = convertToGlobalSettingFormat($validuntil, true);
}
?>
<!-- Validity -->
<div class="form-group">
<label class=" control-label" for='validfrom'><?php eT("Valid from"); ?>:</label>
Expand Down

0 comments on commit dff62fe

Please sign in to comment.