Skip to content

Commit

Permalink
Fixed issue #6440 : Adding a question allways put at end of group
Browse files Browse the repository at this point in the history
Fixed issue : Delete a question rewrites question order by qid
Dev: remove fixSortOrderQuestions, use Questions::model()->updateQuestionOrder
  • Loading branch information
Shnoulle committed Aug 8, 2012
1 parent 71d2e8c commit 68aced0
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 39 deletions.
36 changes: 10 additions & 26 deletions application/controllers/admin/database.php
Expand Up @@ -373,18 +373,16 @@ function index($sa = null)
{
if (!Yii::app()->request->getPost('lid') || Yii::app()->request->getPost('lid') == '') {$_POST['lid']="0";}
if (!Yii::app()->request->getPost('lid1') || Yii::app()->request->getPost('lid1') == '') {$_POST['lid1']="0";}
if (Yii::app()->request->getPost('questionposition'))
if (Yii::app()->request->getPost('questionposition',"")!="")
{
$question_order=(sanitize_int(Yii::app()->request->getPost('questionposition')));
$question_order= intval(Yii::app()->request->getPost('questionposition'));
//Need to renumber all questions on or after this
$cdquery = "UPDATE {{questions}} SET question_order=question_order+1 WHERE gid=:gid AND question_order >= :order";

$cdresult=Yii::app()->db->createCommand($cdquery)->bindValues(array(':gid'=>$gid, ':order'=>$question_order))->query();
} else {
$question_order=(getMaxQuestionOrder($gid,$surveyid));
$question_order++;
}

$_POST['title'] = html_entity_decode(Yii::app()->request->getPost('title'), ENT_QUOTES, "UTF-8");
$_POST['question_'.$baselang] = html_entity_decode(Yii::app()->request->getPost('question_'.$baselang), ENT_QUOTES, "UTF-8");
$_POST['help_'.$baselang] = html_entity_decode(Yii::app()->request->getPost('help_'.$baselang), ENT_QUOTES, "UTF-8");
Expand All @@ -404,7 +402,6 @@ function index($sa = null)
$_POST['help_'.$baselang]=fixCKeditorText(Yii::app()->request->getPost('help_'.$baselang));
}

$data = array();
$data = array(
'sid' => $surveyid,
'gid' => $gid,
Expand All @@ -419,18 +416,9 @@ function index($sa = null)
'question_order' => $question_order,
'language' => $baselang
);

$question = new Questions;
foreach ($data as $k => $v)
$question->$k = $v;
$result = $question->save();

// Checked
// Get the last inserted questionid for other languages
$qid=$question->qid;

$qid=Questions::model()->insertRecords($data);
// Add other languages
if ($result)
if ($qid)
{
$addlangs = Survey::model()->findByPk($surveyid)->additionalLanguages;
foreach ($addlangs as $alang)
Expand All @@ -451,13 +439,10 @@ function index($sa = null)
'question_order' => $question_order,
'language' => $alang
);
$ques = new Questions;
foreach ($data as $k => $v)
$ques->$k = $v;
$result2 = $ques->save();
$langqid=Questions::model()->insertRecords($data);

// Checked */
if (!$result2)
if (!$langqid)
{
$databaseoutput .= "<script type=\"text/javascript\">\n<!--\n alert(\"".sprintf($clang->gT("Question in language %s could not be created.","js"),$alang)."\\n\")\n //-->\n</script>\n";
}
Expand All @@ -466,7 +451,7 @@ function index($sa = null)
}


if (!$result)
if (!$qid)
{
$databaseoutput .= "<script type=\"text/javascript\">\n<!--\n alert(\"".$clang->gT("Question could not be created.","js")."\\n\")\n //-->\n</script>\n";

Expand Down Expand Up @@ -538,8 +523,7 @@ function index($sa = null)
}
}
}

fixSortOrderQuestions($gid, $surveyid);
Questions::model()->updateQuestionOrder($gid, $surveyid);
Yii::app()->session['flashmessage'] = $clang->gT("Question was successfully added.");

}
Expand Down Expand Up @@ -781,8 +765,8 @@ function index($sa = null)
{
Questions::model()->updateAll(array('gid'=>$gid), 'qid=:qid and parent_qid>0', array(':qid'=>$qid));
// if the group has changed then fix the sortorder of old and new group
fixSortOrderQuestions($oldgid, $surveyid);
fixSortOrderQuestions($gid, $surveyid);
Questions::model()->updateQuestionOrder($oldgid, $surveyid);
Questions::model()->updateQuestionOrder($gid, $surveyid);
// If some questions have conditions set on this question's answers
// then change the cfieldname accordingly
fixMovedQuestionConditions($qid, $oldgid, $gid);
Expand Down
2 changes: 1 addition & 1 deletion application/controllers/admin/question.php
Expand Up @@ -885,7 +885,7 @@ public function delete($surveyid, $gid, $qid)
Defaultvalues::model()->deleteAllByAttributes(array('qid' => $qid));
Quota_members::model()->deleteAllByAttributes(array('qid' => $qid));

Questions::updateSortOrder($gid, $surveyid);
Questions::updateQuestionOrder($gid, $surveyid);

$qid = "";
$postqid = "";
Expand Down
2 changes: 1 addition & 1 deletion application/controllers/admin/remotecontrol.php
Expand Up @@ -1577,7 +1577,7 @@ public function set_question_properties($sSessionKey, $iQuestionID, $aQuestionDa
try
{
$bSaveResult=$oQuestion->save(); // save the change to database
fixSortOrderQuestions($oQuestion->gid, $oQuestion->sid);
Questions::model()->updateQuestionOrder($oQuestion->gid, $oQuestion->sid);
$aResult[$sFieldName]=$bSaveResult;
//unset fields that failed
if (!$bSaveResult)
Expand Down
4 changes: 2 additions & 2 deletions application/helpers/admin/import_helper.php
Expand Up @@ -674,7 +674,7 @@ function CSVImportGroup($sFullFilepath, $iNewSID)
$gres = Yii::app()->db->createCommand($gquery)->query();
foreach ($gres->readAll() as $grow)
{
fixSortOrderQuestions($grow['gid'], $iNewSID);
Questions::model()->updateQuestionOrder($grow['gid'], $iNewSID);
}
}

Expand Down Expand Up @@ -3123,7 +3123,7 @@ function CSVImportSurvey($sFullFilepath,$iDesiredSurveyId=NULL,$bTranslateLinks=
$gres = Yii::app()->db->createCommand($gquery)->query();
foreach ($gres->readAll() as $grow)
{
fixSortOrderQuestions($grow['gid'], $iNewSID);
Questions::model()->updateQuestionOrder($grow['gid'], $iNewSID);
}

//We've built two arrays along the way - one containing the old SID, GID and QIDs - and their NEW equivalents
Expand Down
5 changes: 3 additions & 2 deletions application/helpers/common_helper.php
Expand Up @@ -1585,10 +1585,11 @@ function fixSortOrderAnswers($qid,$surveyid=null) //Function rewrites the sortor

/**
* This function rewrites the sortorder for questions inside the named group
*
* REMOVED the 2012-08-08 : replaced by Questions::model()->updateQuestionOrder
* @param integer $groupid the group id
* @param integer $surveyid the survey id
*/
/**
function fixSortOrderQuestions($groupid, $surveyid) //Function rewrites the sortorder for questions
{
$gid = sanitize_int($groupid);
Expand All @@ -1604,7 +1605,7 @@ function fixSortOrderQuestions($groupid, $surveyid) //Function rewrites the sort
$p++;
}
}

*/

function shiftOrderQuestions($sid,$gid,$shiftvalue) //Function shifts the sortorder for questions
{
Expand Down
23 changes: 16 additions & 7 deletions application/models/Questions.php
Expand Up @@ -90,7 +90,7 @@ public function rules()


/**
* Fixes sort order for questions in a group
* Rewrites sort order for questions in a group
*
* @static
* @access public
Expand All @@ -109,7 +109,15 @@ public static function updateSortOrder($gid, $surveyid)
$p++;
}
}

/**
* Fixe sort order for questions in a group
*
* @static
* @access public
* @param int $gid
* @param int $surveyid
* @return void
*/
function updateQuestionOrder($gid,$language,$position=0)
{
$data=Yii::app()->db->createCommand()->select('qid')
Expand Down Expand Up @@ -239,19 +247,20 @@ function getQuestionsWithSubQuestions($iSurveyID, $sLanguage, $sCondition = FALS
function insertRecords($data)
{
$questions = new self;
foreach ($data as $k => $v)
foreach ($data as $k => $v){
$questions->$k = $v;
}
// if (!$questions->save()) return false;
// else return $questions->qid;
try
{
$questions->save();
return $questions->qid;
$questions->save();
return $questions->qid;
}
catch(Exception $e)
{
return false;
}
return false;
}
}

public static function deleteAllById($questionsIds)
Expand Down

0 comments on commit 68aced0

Please sign in to comment.