From ae2a46cd5c33738b621bcd23360d8c4db7402830 Mon Sep 17 00:00:00 2001 From: Sam Mousa Date: Mon, 21 Oct 2013 09:33:35 +0200 Subject: [PATCH] Dev Fixed issue where a survey with duplicated question codes would not be imported. Dev Question codes must now start with a letter and contain only alphanumeric characters. --- application/helpers/admin/import_helper.php | 35 ++++++++++++++++++++- application/models/Question.php | 9 ++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/application/helpers/admin/import_helper.php b/application/helpers/admin/import_helper.php index ac2e1b9dd78..20b66bebf5d 100644 --- a/application/helpers/admin/import_helper.php +++ b/application/helpers/admin/import_helper.php @@ -3679,7 +3679,40 @@ function XMLImportSurvey($sFullFilepath,$sXMLdata=NULL,$sNewSurveyName=NULL,$iDe } if ($insertdata) XSSFilterArray($insertdata); - $newsqid =Question::model()->insertRecords($insertdata) or safeDie($clang->gT("Error").": Failed to insert data [5]
"); + $question = new Question(); + $question->setAttributes($insertdata, false); + $attempts = 0; + while (!$question->validate(array('title'))) + { + if (is_numeric($question->title)) + { + $question->title = 'q' . $question->title; + } + else + { + if (!isset($index)) + { + $index = 0; + $rand = mt_rand(0, 1024); + } + else + { + $index++; + } + $question->title = 'r' . $rand . 'q' . $index; + } + $attempts++; + if ($attempts > 10) + { + safeDie($clang->gT("Error").": Failed to resolve question code problems after 10 attempts.
"); + } + } + if (!$question->save()) + { + safeDie($clang->gT("Error while saving: "). print_r($question->errors, true)); + } + $newsqid = $question->qid; + //$newsqid =Question::model()->insertRecords($insertdata) or safeDie($clang->gT("Error").": Failed to insert data [5]
"); if (!isset($insertdata['qid'])) { $aQIDReplacements[$oldsqid]=$newsqid; // add old and new qid to the mapping array diff --git a/application/models/Question.php b/application/models/Question.php index 1e84eb05347..54f1735ce1d 100644 --- a/application/models/Question.php +++ b/application/models/Question.php @@ -88,6 +88,15 @@ public function rules() 'message'=>'{attribute} "{value}" is already in use.'), array('language','length', 'min' => 2, 'max'=>20),// in array languages ? array('title,question,help','LSYii_Validators'), + array('title', 'unique', 'caseSensitive'=>true, 'criteria'=>array( + 'condition' => 'language=:language AND sid=:sid', + 'params' => array( + ':language' => $this->language, + ':sid' => $this->sid + ) + ), + 'message' => 'Question codes must be unique.'), + array('title', 'match', 'pattern' => '/[a-z,A-Z][[:alnum:]]+/', 'message' => 'Question codes must start with a letter and may only contain alphanumeric characters.'), array('other', 'in','range'=>array('Y','N'), 'allowEmpty'=>true), array('mandatory', 'in','range'=>array('Y','N'), 'allowEmpty'=>true), array('question_order','numerical', 'integerOnly'=>true,'allowEmpty'=>true),