Skip to content

Commit

Permalink
Fixed issue #9140: No error message if data is not inserted on first …
Browse files Browse the repository at this point in the history
…page

Fixed issue #9140: Auto ID is not set properly when using Postgres and deactivating and reactivating the survey
  • Loading branch information
c-schmitz committed Sep 9, 2014
1 parent 716670e commit ea2d73b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 40 deletions.
22 changes: 8 additions & 14 deletions application/controllers/admin/surveyadmin.php
Expand Up @@ -307,26 +307,26 @@ public function deactivate($iSurveyID = null)
$iSurveyID = Yii::app()->request->getPost('sid', $iSurveyID);
$iSurveyID = sanitize_int($iSurveyID);
$clang = $this->getController()->lang;
if (!tableExists('survey_'.$iSurveyID))
{
$_SESSION['flashmessage'] = $clang->gT("Error: Response table does not exist. Survey cannot be deactivated.");
$this->getController()->redirect($this->getController()->createUrl("admin/survey/sa/view/surveyid/{$iSurveyID}"));
}
$date = date('YmdHis'); //'His' adds 24hours+minutes to name to allow multiple deactiviations in a day

if (empty($_POST['ok']))
{
if (!tableExists('survey_'.$iSurveyID))
{
$_SESSION['flashmessage'] = $clang->gT("Error: Response table does not exist. Survey cannot be deactivated.");
$this->getController()->redirect($this->getController()->createUrl("admin/survey/sa/view/surveyid/{$iSurveyID}"));
}
$aData['surveyid'] = $iSurveyID;
$aData['date'] = $date;
$aData['dbprefix'] = Yii::app()->db->tablePrefix;
$aData['step1'] = true;
}
else
{
{
//See if there is a tokens table for this survey
if (tableExists("{{tokens_{$iSurveyID}}}"))
{
if (Yii::app()->db->getDriverName() == 'postgre')
if (Yii::app()->db->getDriverName() == 'pgsql')
{
$deactivateresult = Yii::app()->db->createCommand()->renameTable($toldtable . '_tid_seq', $tnewtable . '_tid_seq');
$setsequence = "ALTER TABLE ".Yii::app()->db->quoteTableName($tnewtable)." ALTER COLUMN tid SET DEFAULT nextval('{{{$tnewtable}}}_tid_seq'::regclass);";
Expand Down Expand Up @@ -362,13 +362,7 @@ public function deactivate($iSurveyID = null)
$survey = Survey::model()->findByAttributes(array('sid' => $iSurveyID));
$survey->autonumber_start = $new_autonumber_start;
$survey->save();
if (Yii::app()->db->getDrivername() == 'postgre')
{
$deactivateresult = Yii::app()->db->createCommand()->renameTable($sOldSurveyTableName . '_id_seq', $sNewSurveyTableName . '_id_seq');
$setsequence = "ALTER TABLE $newtable ALTER COLUMN id SET DEFAULT nextval('{$sNewSurveyTableName}_id_seq'::regclass);";
$deactivateresult = Yii::app()->db->createCommand($setsequence)->execute();
}


$deactivateresult = Yii::app()->db->createCommand()->renameTable($sOldSurveyTableName, $sNewSurveyTableName);

$insertdata = array('active' => 'N');
Expand Down
44 changes: 20 additions & 24 deletions application/helpers/admin/activate_helper.php
Expand Up @@ -394,31 +394,27 @@ function activateSurvey($iSurveyID, $simulate = false)
}

$anquery = "SELECT autonumber_start FROM {{surveys}} WHERE sid={$iSurveyID}";
if ($anresult=Yii::app()->db->createCommand($anquery)->query()->readAll())
{
//if there is an autonumber_start field, start auto numbering here
foreach($anresult as $row)
$iAutoNumberStart=Yii::app()->db->createCommand($anquery)->queryScalar();
//if there is an autonumber_start field, start auto numbering here
if ($iAutoNumberStart!==false && $iAutoNumberStart>0)
{
if (Yii::app()->db->driverName=='mssql' || Yii::app()->db->driverName=='sqlsrv' || Yii::app()->db->driverName=='dblib') {
mssql_drop_primary_index('survey_'.$iSurveyID);
mssql_drop_constraint('id','survey_'.$iSurveyID);
$sQuery = "alter table {{survey_{$iSurveyID}}} drop column id ";
Yii::app()->db->createCommand($sQuery)->execute();
$sQuery = "alter table {{survey_{$iSurveyID}}} add [id] int identity({$iAutoNumberStart},1)";
Yii::app()->db->createCommand($sQuery)->execute();
}
elseif (Yii::app()->db->driverName=='pgsql')
{
if ($row['autonumber_start'] > 0)
{
if (Yii::app()->db->driverName=='mssql' || Yii::app()->db->driverName=='sqlsrv' || Yii::app()->db->driverName=='dblib') {
mssql_drop_primary_index('survey_'.$iSurveyID);
mssql_drop_constraint('id','survey_'.$iSurveyID);
$autonumberquery = "alter table {{survey_{$iSurveyID}}} drop column id ";
Yii::app()->db->createCommand($autonumberquery)->execute();
$autonumberquery = "alter table {{survey_{$iSurveyID}}} add [id] int identity({$row['autonumber_start']},1)";
Yii::app()->db->createCommand($autonumberquery)->execute();
}
elseif (Yii::app()->db->driverName=='pgsql')
{

}
else
{
$autonumberquery = "ALTER TABLE {{survey_{$iSurveyID}}} AUTO_INCREMENT = ".$row['autonumber_start'];
$result = @Yii::app()->db->createCommand($autonumberquery)->execute();
}
}
$sQuery = "SELECT setval(pg_get_serial_sequence('{{survey_{$iSurveyID}}}', 'id'),{$iAutoNumberStart},false);";
$result = @Yii::app()->db->createCommand($sQuery)->execute();

This comment has been minimized.

Copy link
@c-schmitz

c-schmitz Sep 9, 2014

Author Contributor

The next ID is saved in the survey properties on deactivation. Here is the ID restored :-)

This comment has been minimized.

Copy link
@Shnoulle

Shnoulle Sep 9, 2014

Collaborator

OK, then this only happen when deactivate/reactivate survey ? Didi we need to fix it after vv import ?

This comment has been minimized.

Copy link
@c-schmitz

c-schmitz Sep 9, 2014

Author Contributor

No, because usually you import the same IDs with VVimport/export

This comment has been minimized.

Copy link
@Shnoulle

Shnoulle Sep 9, 2014

Collaborator

Yes, but you can set id at 10000 in your vv import. User can update VV import. We have to fix it too. Maybe same for MYSQL and mssql

I retake bug (just tested with export with 1, import with 2).

}
else
{
$sQuery = "ALTER TABLE {{survey_{$iSurveyID}}} AUTO_INCREMENT = {$iAutoNumberStart}";
$result = @Yii::app()->db->createCommand($sQuery)->execute();
}
}

Expand Down
2 changes: 2 additions & 0 deletions application/helpers/expressions/em_manager_helper.php
Expand Up @@ -5205,6 +5205,8 @@ private function _UpdateValuesInDatabase($updatedValues, $finished=false)
else
{
$message .= $this->gT("Unable to insert record into survey table"); // TODO - add SQL error?
echo submitfailed(''); // TODO - report SQL error?
}
//Insert Row for Timings, if needed
if ($this->surveyOptions['savetimings']) {
Expand Down
4 changes: 2 additions & 2 deletions application/views/admin/survey/deactivateSurvey_view.php
Expand Up @@ -34,7 +34,7 @@
</td>
<td>
<?php echo CHtml::form(array("admin/survey/sa/deactivate/surveyid/{$surveyid}/"), 'post'); ?>
<p><input type='submit' value='<?php $clang->eT("Deactivate survey"); ?>' onclick="<?php echo convertGETtoPOST($this->createUrl("admin/survey/sa/deactivate/".$surveyid)."?action=deactivate&amp;ok=Y&amp;sid=$surveyid"); ?>" /></p>
<p><input type='submit' value='<?php $clang->eT("Deactivate survey"); ?>'/></p>
<input type='hidden' value='Y' name='ok' />
</form>
</td>
Expand All @@ -51,7 +51,7 @@
<p>
<?php $clang->eT("The responses to this survey are no longer available using LimeSurvey."); ?></p>
<p>
<?php echo $clang->gT("The responses table has been renamed to: ")." ".$sNewSurveyTableName; ?><br>
<?php echo $clang->gT("The responses table has been renamed to: ")." <b>".$sNewSurveyTableName; ?></b><br>
<?php if (isset($toldtable) && $toldtable)
{
echo $clang->gT("The tokens table associated with this survey has been renamed to: ")." $tnewtable<br>";
Expand Down

1 comment on commit ea2d73b

@Shnoulle
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see that ??? When you have it ?

Activate new survey get the good control :

    "id" serial NOT NULL PRIMARY KEY,

serial is nextval('lime_survey_XXXX_id_seq'::regclass)

Please sign in to comment.