Skip to content

Commit

Permalink
Fixed issue #11886: lsq file bug import
Browse files Browse the repository at this point in the history
Dev: validate('title') and get the new title for another row
Dev: create a model function (to be used in survey and group import too
  • Loading branch information
Shnoulle committed Nov 10, 2016
1 parent 89cfd16 commit 3fbe4b2
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 22 deletions.
2 changes: 1 addition & 1 deletion application/controllers/admin/questions.php
Expand Up @@ -235,7 +235,7 @@ public function import()
href="'.$this->getController()->createUrl('admin/survey/sa/listquestions/surveyid/').'/'.$surveyid.'">'
.gT("Return to question list").'</a></p>';
$this->_renderWrappedTemplate('super', 'messagebox', array('title'=>gT('Error'), 'message'=>$message));
die();
App()->end();
}

unlink($sFullFilepath);
Expand Down
37 changes: 19 additions & 18 deletions application/helpers/admin/import_helper.php
Expand Up @@ -410,14 +410,7 @@ function XMLImportQuestion($sFullFilePath, $iNewSID, $newgid, $options=array('au
{
$insertdata[(string)$key]=(string)$value;
}
if ($options['autorename'])
{
// Check if code already exists
while (Question::model()->countByAttributes(array('title'=>$insertdata['title'],'sid'=>$iNewSID))>0)
{
$insertdata['title']=randomChars(5);
}
}

$iOldSID=$insertdata['sid'];
$insertdata['sid']=$iNewSID;
$insertdata['gid']=$newgid;
Expand All @@ -434,21 +427,29 @@ function XMLImportQuestion($sFullFilePath, $iNewSID, $newgid, $options=array('au
$insertdata['qid']=$aQIDReplacements[$oldqid];
}

$ques = new Question;
if ($insertdata)
XSSFilterArray($insertdata);
foreach ($insertdata as $k => $v)
$ques->$k = $v;
$result = $ques->save();
if (!$result)
$oQuestion = new Question('import');
$oQuestion->setAttributes($insertdata, false);
if(!$oQuestion->validate(array('title')) && $options['autorename']){
if(isset($sNewTitle)){
$oQuestion->title=$sNewTitle;
}else{
$sOldTitle=$oQuestion->title;
$oQuestion->title=$sNewTitle=$oQuestion->getNewTitle();
if(!$sNewTitle){
$results['fatalerror'] = CHtml::errorSummary($oQuestion,gT("The question could not be imported for the following reasons:"));
return $results;
}
$results['importwarnings'][] = sprintf(gT("Question code %s was updated to %s."),$sOldTitle,$sNewTitle);
}
}
if (!$oQuestion->save())
{
$results['fatalerror'] = CHtml::errorSummary($ques,gT("The question could not be imported for the following reasons:"));
$results['fatalerror'] = CHtml::errorSummary($oQuestion,gT("The question could not be imported for the following reasons:"));
return $results;
}
if (!isset($aQIDReplacements[$oldqid]))
{
$newqid=getLastInsertID($ques->tableName());
$aQIDReplacements[$oldqid]=$newqid; // add old and new qid to the mapping array
$newqid=$aQIDReplacements[$oldqid]=$oQuestion->qid;
}
}

Expand Down
42 changes: 41 additions & 1 deletion application/models/Question.php
Expand Up @@ -829,7 +829,46 @@ public function getOtherIcon()
return $sIcon;
}


/**
* Get an new title/code for a question
* @return string|null : new title, null if impossible
*/
public function getNewTitle()
{
$sOldTitle=$this->title;
if($this->validate(array('title'))){
return $sOldTitle;
}
/* Maybe it's an old invalid title : try to fix it */
$sNewTitle=preg_replace("/[^A-Za-z0-9]/", '', $sOldTitle);
if (is_numeric(substr($sNewTitle,0,1)))
{
$sNewTitle='q' . $sNewTitle;
}
/* Maybe there are another question with same title try to fix it 10 times */
$attempts = 0;
while (!$this->validate(array('title')))
{
if (!isset($index))
{
$index = 0;
$rand = mt_rand(0, 1024);
}
else
{
$index++;
}
$sNewTitle='r' . $rand . 'q' . $index;
$this->title = $sNewTitle;
$attempts++;
if ($attempts > 10)
{
$this->addError('title', 'Failed to resolve question code problems after 10 attempts.');
return null;
}
}
return $sNewTitle;
}

public function search()
{
Expand Down Expand Up @@ -959,4 +998,5 @@ public static function getNumberOfQuestions($surveyid)
." AND language='".$_SESSION['survey_'.$surveyid]['s_lang']."'"
." AND parent_qid=0")->read();
}

}
15 changes: 13 additions & 2 deletions application/views/admin/survey/Question/import_view.php
Expand Up @@ -20,13 +20,24 @@
<li><?php echo gT("Label sets") . ": " . $aImportResults['labelsets'] . " (" . $aImportResults['labels'] ?>)</li>
<?php endif;?>
<li><?php echo gT("Question attributes:") . $aImportResults['question_attributes'] ?></li>
</ul>
</ul>
</p>
<?php if (!empty($aImportResults['importwarnings'])): ?>
<h2 class="warning"><?php eT("Warnings");?>:</h2>
<ul class="list-unstyled">
<?php
foreach ($aImportResults['importwarnings'] as $warning)
{ ?>
<li><?php echo $warning; ?></li>
<?php
} ?>
</ul>
<?php endif; ?>
<p class="text-info"><?php eT("Question import is complete.") ?></p>
<p>
<a href="<?php echo $this->createUrl('admin/questions/sa/view/surveyid/' . $surveyid . '/gid/' . $gid . '/qid/' . $aImportResults['newqid']) ?>" class="btn btn-default btn-lg" /><?php eT("Go to question") ?></a>
</p>
</div>
</div>
</div>
</div>
</div>

0 comments on commit 3fbe4b2

Please sign in to comment.