";
}
if ($action == "editquestion" || $action=="addquestion")
{
$adding=($action=="addquestion");
$questlangs = GetAdditionalLanguagesFromSurveyID($surveyid);
$baselang = GetBaseLanguageFromSurveyID($surveyid);
$questlangs[] = $baselang;
$questlangs = array_flip($questlangs);
if (!$adding)
{
$egquery = "SELECT * FROM ".db_table_name('questions')." WHERE sid=$surveyid AND gid=$gid AND qid=$qid";
$egresult = db_execute_assoc($egquery);
while ($esrow = $egresult->FetchRow())
{
if(!array_key_exists($esrow['language'], $questlangs)) // Language Exists, BUT ITS NOT ON THE SURVEY ANYMORE.
{
$egquery = "DELETE FROM ".db_table_name('questions')." WHERE sid='{$surveyid}' AND gid='{$gid}' AND qid='{$qid}' AND language='".$esrow['language']."'";
$egresultD = $connect->Execute($egquery);
} else {
$questlangs[$esrow['language']] = 99;
}
if ($esrow['language'] == $baselang)
{
$basesettings = array('question_order' => $esrow['question_order'],
'other' => $esrow['other'],
'mandatory' => $esrow['mandatory'],
'type' => $esrow['type'],
'title' => $esrow['title'],
'preg' => $esrow['preg'],
'question' => $esrow['question'],
'help' => $esrow['help']);
}
}
if ($egresult==false or $egresult->RecordCount()==0)
{
safe_die('Invalid question id');
}
while (list($key,$value) = each($questlangs))
{
if ($value != 99)
{
db_switchIDInsert('questions',true);
$egquery = "INSERT INTO ".db_table_name('questions')." (qid, sid, gid, type, title, question, preg, help, other, mandatory, question_order, language)"
." VALUES ('{$qid}','{$surveyid}', '{$gid}', '{$basesettings['type']}', '{$basesettings['title']}',"
." '{$basesettings['question']}', '{$basesettings['preg']}', '{$basesettings['help']}', '{$basesettings['other']}', '{$basesettings['mandatory']}', '{$basesettings['question_order']}','{$key}')";
$egresult = $connect->Execute($egquery);
db_switchIDInsert('questions',false);
}
}
$eqquery = "SELECT * FROM {$dbprefix}questions WHERE sid=$surveyid AND gid=$gid AND qid=$qid AND language='{$baselang}'";
$eqresult = db_execute_assoc($eqquery);
}
$js_admin_includes[] = '../scripts/jquery/jquery.dd.js';
$css_admin_includes[] = '../scripts/jquery/dd.css';
$editquestion = PrepareEditorScript();
$qtypelist=getqtypelist('','array');
$qDescToCode = 'qDescToCode = {';
$qCodeToInfo = 'qCodeToInfo = {';
foreach ($qtypelist as $qtype=>$qdesc){
$qDescToCode .= " '{$qdesc['description']}' : '{$qtype}', \n";
$qCodeToInfo .= " '{$qtype}' : '".json_encode($qdesc)."', \n";
}
$qTypeOutput = "$qDescToCode 'null':'null' }; \n $qCodeToInfo 'null':'null' };";
$editquestion .= "\n
";
if (!$adding) {$editquestion .=$clang->gT("Edit question");} else {$editquestion .=$clang->gT("Add a new question");};
$editquestion .= "
\n";
if (!$adding)
{
$eqrow = $eqresult->FetchRow(); // there should be only one datarow, therefore we don't need a 'while' construct here.
// Todo: handler in case that record is not found
}
else
{
$eqrow['language']=$baselang;
$eqrow['title']='';
$eqrow['question']='';
$eqrow['help']='';
$eqrow['type']='T';
$eqrow['lid']=0;
$eqrow['lid1']=0;
$eqrow['gid']=$gid;
$eqrow['other']='N';
$eqrow['mandatory']='N';
$eqrow['preg']='';
}
$editquestion .= "
\n";
if ($adding)
{
// Import dialogue
if (bHasSurveyPermission($surveyid,'surveycontent','import'))
{
$editquestion .= "
".$clang->gT("...or import a question")."
\n"
. "\t\n\n";
}
$editquestion .= "\n";
}
$editquestion .= questionjavascript($eqrow['type']);
}
//Constructing the interface here...
if($action == "orderquestions")
{
if (isset($_POST['questionordermethod']))
{
switch($_POST['questionordermethod'])
{
// Pressing the Up button
case 'up':
$newsortorder=$postsortorder-1;
$oldsortorder=$postsortorder;
$cdquery = "UPDATE ".db_table_name('questions')." SET question_order=-1 WHERE gid=$gid AND question_order=$newsortorder";
$cdresult=$connect->Execute($cdquery) or safe_die($connect->ErrorMsg());
$cdquery = "UPDATE ".db_table_name('questions')." SET question_order=$newsortorder WHERE gid=$gid AND question_order=$oldsortorder";
$cdresult=$connect->Execute($cdquery) or safe_die($connect->ErrorMsg());
$cdquery = "UPDATE ".db_table_name('questions')." SET question_order='$oldsortorder' WHERE gid=$gid AND question_order=-1";
$cdresult=$connect->Execute($cdquery) or safe_die($connect->ErrorMsg());
break;
// Pressing the Down button
case 'down':
$newsortorder=$postsortorder+1;
$oldsortorder=$postsortorder;
$cdquery = "UPDATE ".db_table_name('questions')." SET question_order=-1 WHERE gid=$gid AND question_order=$newsortorder";
$cdresult=$connect->Execute($cdquery) or safe_die($connect->ErrorMsg());
$cdquery = "UPDATE ".db_table_name('questions')." SET question_order='$newsortorder' WHERE gid=$gid AND question_order=$oldsortorder";
$cdresult=$connect->Execute($cdquery) or safe_die($connect->ErrorMsg());
$cdquery = "UPDATE ".db_table_name('questions')." SET question_order=$oldsortorder WHERE gid=$gid AND question_order=-1";
$cdresult=$connect->Execute($cdquery) or safe_die($connect->ErrorMsg());
break;
}
}
if ((!empty($_POST['questionmovefrom']) || (isset($_POST['questionmovefrom']) && $_POST['questionmovefrom'] == '0')) && (!empty($_POST['questionmoveto']) || (isset($_POST['questionmoveto']) && $_POST['questionmoveto'] == '0')))
{
$newpos=(int)$_POST['questionmoveto'];
$oldpos=(int)$_POST['questionmovefrom'];
if($newpos > $oldpos)
{
//Move the question we're changing out of the way
$cdquery = "UPDATE ".db_table_name('questions')." SET question_order=-1 WHERE gid=$gid AND question_order=$oldpos";
$cdresult=$connect->Execute($cdquery) or safe_die($connect->ErrorMsg());
//Move all question_orders that are less than the newpos down one
$cdquery = "UPDATE ".db_table_name('questions')." SET question_order=question_order-1 WHERE gid=$gid AND question_order > $oldpos AND question_order <= $newpos";
$cdresult=$connect->Execute($cdquery) or safe_die($connect->ErrorMsg());
//Renumber the question we're changing
$cdquery = "UPDATE ".db_table_name('questions')." SET question_order=$newpos WHERE gid=$gid AND question_order=-1";
$cdresult=$connect->Execute($cdquery) or safe_die($connect->ErrorMsg());
}
if(($newpos+1) < $oldpos)
{
//echo "Newpos $newpos, Oldpos $oldpos";
//Move the question we're changing out of the way
$cdquery = "UPDATE ".db_table_name('questions')." SET question_order=-1 WHERE gid=$gid AND question_order=$oldpos";
$cdresult=$connect->Execute($cdquery) or safe_die($connect->ErrorMsg());
//Move all question_orders that are later than the newpos up one
$cdquery = "UPDATE ".db_table_name('questions')." SET question_order=question_order+1 WHERE gid=$gid AND question_order > $newpos AND question_order <= $oldpos";
$cdresult=$connect->Execute($cdquery) or safe_die($connect->ErrorMsg());
//Renumber the question we're changing
$cdquery = "UPDATE ".db_table_name('questions')." SET question_order=".($newpos+1)." WHERE gid=$gid AND question_order=-1";
$cdresult=$connect->Execute($cdquery) or safe_die($connect->ErrorMsg());
}
}
//Get the questions for this group
$baselang = GetBaseLanguageFromSurveyID($surveyid);
$oqquery = "SELECT * FROM ".db_table_name('questions')." WHERE sid=$surveyid AND gid=$gid AND language='".$baselang."' and parent_qid=0 order by question_order" ;
$oqresult = db_execute_assoc($oqquery);
$orderquestions = "
".$clang->gT("Change Question Order")."
";
$questioncount = $oqresult->RecordCount();
$oqarray = $oqresult->GetArray();
$minioqarray=$oqarray;
// Get the condition dependecy array for all questions in this array and group
$questdepsarray = GetQuestDepsForConditions($surveyid,$gid);
if (!is_null($questdepsarray))
{
$orderquestions .= "
".$clang->gT("Warning").": ".$clang->gT("Current group is using conditional questions")."
".$clang->gT("Re-ordering questions in this group is restricted to ensure that questions on which conditions are based aren't reordered after questions having the conditions set")."
".$clang->gT("See the conditions marked on the following questions").":