Skip to content

Commit

Permalink
Fixed issue 06137: Import Excel survey structure leads to database crash
Browse files Browse the repository at this point in the history
<Dev> Added try/catch mechanism for Answers::model()->insertRecords() and Questions::model()->insertRecords()
<Dev> Added descriptive error messages
  • Loading branch information
Andrie de Vries committed May 30, 2012
1 parent 8883bb3 commit 4e48ede
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 12 deletions.
2 changes: 1 addition & 1 deletion application/controllers/admin/surveyadmin.php
Expand Up @@ -941,7 +941,7 @@ public function copy()
unlink($sFullFilepath);
}

if (isset($aImportResults['error']) && $aImportResults['error']) safeDie($aImportResults['error']);
// if (isset($aImportResults['error']) && $aImportResults['error']) safeDie($aImportResults['error']);

if (!$aData['bFailed'])
{
Expand Down
24 changes: 17 additions & 7 deletions application/helpers/admin/import_helper.php
Expand Up @@ -4159,7 +4159,7 @@ function ExcelImportSurvey($sFullFilepath)
$results['error']=false;
$baselang = 'en'; // TODO set proper default

try {
// try {
$data = new Spreadsheet_Excel_Reader($sFullFilepath);
$adata = $data->dumptonamedarray();

Expand Down Expand Up @@ -4260,8 +4260,10 @@ function ExcelImportSurvey($sFullFilepath)
$baselang = $surveyinfo['language']; // the base language
}

$rownumber = 1;
foreach ($adata as $row)
{
$rownumber += 1;
$row = str_replace(chr(0xA0),' ',$row);
switch($row['class'])
{
Expand Down Expand Up @@ -4330,8 +4332,13 @@ function ExcelImportSurvey($sFullFilepath)
$insertdata['question_order'] = $qseq;
}

$newqid = Questions::model()->insertRecords($insertdata) or safeDie ($clang->gT("Error").": Failed to insert question<br />");
$result = Questions::model()->insertRecords($insertdata); //or safeDie ($clang->gT("Error").": Failed to insert question<br />");
if(!$result){
$results['error'][] = $clang->gT("Error")." : ".$clang->gT("Could not insert question").". ".$clang->gT("Excel row number ").$rownumber." (".$qname.")";
break;
}

$newqid = $result;
if (!isset($qinfo[$qname]))
{
$results['questions']++;
Expand Down Expand Up @@ -4474,15 +4481,18 @@ function ExcelImportSurvey($sFullFilepath)
$insertdata['assessment_value'] = (isset($row['relevance']) ? $row['relevance'] : '');
$insertdata['sortorder'] = ++$aseq;

$result = Answers::model()->insertRecords($insertdata) or safeDie("Error: Failed to insert answer<br />");
$result = Answers::model()->insertRecords($insertdata); // or safeDie("Error: Failed to insert answer<br />");
if(!$result){
$results['error'][] = $clang->gT("Error")." : ".$clang->gT("Could not insert answer").". ".$clang->gT("Excel row number ").$rownumber;
}
$results['answers']++;
break;
}

}
}
catch (Exception $e) {
$results['error'] = $e->getMessage();
}
// }
// catch (Exception $e) {
// $results['error'] = $e->getMessage();
// }
return $results;
}
9 changes: 8 additions & 1 deletion application/models/Answers.php
Expand Up @@ -109,7 +109,14 @@ function insertRecords($data)
$ans = new self;
foreach ($data as $k => $v)
$ans->$k = $v;
return $ans->save();
try
{
return $ans->save();
}
catch(Exception $e)
{
return false;
}
}

/**
Expand Down
13 changes: 11 additions & 2 deletions application/models/Questions.php
Expand Up @@ -208,8 +208,17 @@ function insertRecords($data)
$questions = new self;
foreach ($data as $k => $v)
$questions->$k = $v;
if (!$questions->save()) return false;
else return $questions->qid;
// if (!$questions->save()) return false;
// else return $questions->qid;
try
{
$questions->save();
return $questions->qid;
}
catch(Exception $e)
{
return false;
}
}

public static function deleteAllById($questionsIds)
Expand Down
11 changes: 10 additions & 1 deletion application/views/admin/survey/importSurvey_view.php
Expand Up @@ -12,7 +12,16 @@
<?php if (isset($aImportResults['error']) && $aImportResults['error']!=false)
{?>
<div class='warningheader'><?php $clang->eT("Error");?></div><br />
<?php echo $aImportResults['error']; ?><br /><br />
<?php
if(is_array($aImportResults['error']))
{
foreach($aImportResults['error'] as $error)
echo $error."<br/>";
} else
{
echo $aImportResults['error'];
}
?><br /><br />
<input type='submit' value='<?php $clang->eT("Main Admin Screen");?>' onclick="window.open('<?php echo site_url('admin');?>', '_top')" />
<?php } else
{?>
Expand Down

0 comments on commit 4e48ede

Please sign in to comment.