question id
* $ia[1] => fieldname
* $ia[2] => title
* $ia[3] => question text
* $ia[4] => type -- text, radio, select, array, etc
* $ia[5] => group id
* $ia[6] => mandatory Y || N
* $ia[7] => conditions ??
*
* $conditions element structure
* $condition[n][0] => question id
* $condition[n][1] => question with value to evaluate
* $condition[n][2] => internal field name of element [1]
* $condition[n][3] => value to be evaluated on answers labeled. *NEW* tittle of questions to evaluate.
* $condition[n][4] => type of question
* $condition[n][5] => equal to [2], but concatenated in this time (why the same value 2 times?)
* $condition[n][6] => method used to evaluate *NEW*
* $condition[n][7] => scenario *NEW BY R.L.J. van den Burg*
*/
function retrieveConditionInfo($ia)
{
//This function returns an array containing all related conditions
//for a question - the array contains the fields from the conditions table
global $dbprefix, $connect;
if ($ia[7] == "Y")
{ //DEVELOP CONDITIONS ARRAY FOR THIS QUESTION
$cquery = "SELECT {$dbprefix}conditions.qid, "
."{$dbprefix}conditions.scenario, "
."{$dbprefix}conditions.cqid, "
."{$dbprefix}conditions.cfieldname, "
."{$dbprefix}conditions.value, "
."{$dbprefix}questions.type, "
."{$dbprefix}questions.sid, "
."{$dbprefix}questions.gid, "
."{$dbprefix}conditions.method "
."FROM {$dbprefix}conditions, "
."{$dbprefix}questions "
."WHERE {$dbprefix}conditions.cqid={$dbprefix}questions.qid "
."AND {$dbprefix}conditions.qid=$ia[0] "
."AND {$dbprefix}questions.language='".$_SESSION['s_lang']."' "
."ORDER BY {$dbprefix}conditions.scenario, "
."{$dbprefix}conditions.cqid, "
."{$dbprefix}conditions.cfieldname";
$cresult = db_execute_assoc($cquery) or safe_die ("OOPS $cquery ".$connect->ErrorMsg()); //Checked
while ($crow = $cresult->FetchRow())
{
$conditions[] = array ($crow['qid'],
$crow['cqid'],
$crow['cfieldname'],
$crow['value'],
$crow['type'],
$crow['sid']."X".$crow['gid']."X".$crow['cqid'],
$crow['method'],
$crow['scenario']);
}
return $conditions;
}
else
{
return null;
}
}
function create_mandatorylist($ia)
{
//Checks current question and returns required mandatory arrays if required
if ($ia[6] == 'Y')
{
switch($ia[4])
{
case 'R':
$thismandatory = setman_ranking($ia);
break;
case 'M':
$thismandatory = setman_questionandcode($ia);
break;
case 'J':
case 'P':
case 'Q':
case 'K':
case 'A':
case 'B':
case 'C':
case 'E':
case 'F':
case 'H':
$thismandatory = setman_questionandcode($ia);
break;
case ':':
case ';':
$thismandatory = setman_multiflex($ia);
break;
case '1':
$thismandatory = setman_questionandcode_multiscale($ia);
break;
case 'X':
//Do nothing - boilerplate questions CANNOT be mandatory
break;
default:
$thismandatory = setman_normal($ia);
}
if ($ia[7] != 'Y' && isset($thismandatory)) //Question is not conditional - addto mandatory arrays
{
$mandatory=$thismandatory;
}
if ($ia[7] == 'Y' && isset($thismandatory)) //Question IS conditional - add to conmandatory arrays
{
$conmandatory=$thismandatory;
}
}
if (isset($mandatory))
{
return array($mandatory, null);
}
elseif (isset($conmandatory))
{
return array(null, $conmandatory);
}
else
{
return array(null, null);
}
}
function setman_normal($ia)
{
$mandatorys[]=$ia[1];
$mandatoryfns[]=$ia[1];
return array($mandatorys, $mandatoryfns);
}
function setman_ranking($ia)
{
global $dbprefix, $connect;
$ansquery = "SELECT * FROM {$dbprefix}answers WHERE qid={$ia[0]} AND language='".$_SESSION['s_lang']."' ORDER BY sortorder, answer";
$ansresult = $connect->Execute($ansquery); //Checked
$anscount = $ansresult->RecordCount();
$qidattributes=getQuestionAttributes($ia[0]);
if ($ma=arraySearchByKey("max_answers", $qidattributes, "attribute", 1)) {
$max_answers = $ma['value'];
}
else
{
$max_answers = $anscount;
}
for ($i=1; $i<=$max_answers; $i++)
{
$mandatorys[]=$ia[1].$i;
$mandatoryfns[]=$ia[1];
}
return array($mandatorys, $mandatoryfns);
}
function setman_questionandcode($ia)
{
global $dbprefix, $connect;
$qquery = "SELECT other FROM {$dbprefix}questions WHERE qid=".$ia[0]." AND language='".$_SESSION['s_lang']."'";
$qresult = db_execute_assoc($qquery); //Checked
while ($qrow = $qresult->FetchRow()) {$other = $qrow['other'];}
$ansquery = "SELECT * FROM {$dbprefix}answers WHERE qid={$ia[0]} AND language='".$_SESSION['s_lang']."' ORDER BY sortorder, answer";
$ansresult = db_execute_assoc($ansquery); //Checked
while ($ansrow = $ansresult->FetchRow())
{
$mandatorys[]=$ia[1].$ansrow['code'];
$mandatoryfns[]=$ia[1];
}
if ($other == "Y" and ($ia[4]=="!" or $ia[4]=="L" or $ia[4]=="M" or $ia[4]=="P"))
{
$mandatorys[]=$ia[1]."other";
$mandatoryfns[]=$ia[1];
}
return array($mandatorys, $mandatoryfns);
}
function setman_multiflex($ia)
{
//The point of these functions (setman) is to return an array containing two arrays.
// The first ($mandatorys) is an array containing question, so they can all be checked
// The second ($mandatoryfns) is an arry containing the fieldnames of every question
// What's the difference? The difference arises from multiple option questions, and came
// about when trying to distinguish between answering just one option (which satisfies
// the mandatory requirement, and answering them all). The "mandatorys" input contains the
// actual specific response items that could be filled in.. ie: in a multiple option
// question, there will be a unique one for every possible answer. The "mandatoryfns" array
// contains the generic question fieldname for the question as a whole (it will be repeated
// for multiple option qeustions, but won't contain unique items.
global $dbprefix, $connect;
$qq="SELECT lid FROM {$dbprefix}questions WHERE qid={$ia[0]}";
$qr=db_execute_assoc($qq);
while($qd=$qr->FetchRow())
{
$lid=$qd['lid'];
}
$ansquery = "SELECT * FROM {$dbprefix}answers WHERE qid={$ia[0]} AND language='".$_SESSION['s_lang']."' ORDER BY sortorder, answer";
$ansresult = db_execute_assoc($ansquery);
$ans2query = "SELECT * FROM {$dbprefix}labels WHERE lid={$lid} AND language='".$_SESSION['s_lang']."' ORDER BY sortorder, title";
$ans2result = db_execute_assoc($ans2query);
while ($ans2row=$ans2result->FetchRow())
{
$lset[]=$ans2row;
}
$qidattributes=getQuestionAttributes($ia[0]);
while ($ansrow = $ansresult->FetchRow())
{
//Don't add to mandatory list if the row is filtered out with the array_filter option
if ($htmltbody=arraySearchByKey("array_filter", $qidattributes, "attribute", 1))
{
//This particular one may not be mandatory if it's hidden
$selected = getArrayFiltersForQuestion($ia[0]);
if (!in_array($ansrow['code'],$selected))
{
//This one's hidden, so don't add it to the mandatory list
}
else
{
//This one's not hidden. so add it to the mandatory list
}
} else { //There is no array_filter option, so we should definitely add to the mandatory list here!
foreach($lset as $ls)
{
$mandatorys[]=$ia[1].$ansrow['code']."_".$ls['code'];
$mandatoryfns[]=$ia[1];
}
}
}
return array($mandatorys, $mandatoryfns);
}
function setman_questionandcode_multiscale($ia)
{
global $dbprefix, $connect;
$qquery = "SELECT other FROM {$dbprefix}questions WHERE qid=".$ia[0]." AND language='".$_SESSION['s_lang']."'";
$qresult = db_execute_assoc($qquery); //Checked
while ($qrow = $qresult->FetchRow()) {$other = $qrow['other'];}
$ansquery = "SELECT * FROM {$dbprefix}answers WHERE qid={$ia[0]} AND language='".$_SESSION['s_lang']."' ORDER BY sortorder, answer";
$ansresult = db_execute_assoc($ansquery); //Checked
$lquery = "SELECT q.qid FROM {$dbprefix}labels l, {$dbprefix}questions q WHERE l.lid = q.lid AND q.qid=".$ia[0]." AND l.language='".$_SESSION['s_lang']."' AND q.language='".$_SESSION['s_lang']."'";
$labelsresult = db_execute_assoc($lquery); //Checked
$labelscount = $labelsresult->RowCount();
$lquery1 = "SELECT q.qid FROM {$dbprefix}labels l, {$dbprefix}questions q WHERE l.lid = q.lid1 AND q.qid=".$ia[0]." AND l.language='".$_SESSION['s_lang']."' AND q.language='".$_SESSION['s_lang']."'";
$labelsresult1 = db_execute_assoc($lquery1); //Checked
$labelscount1 = $labelsresult1->RowCount();
while ($ansrow = $ansresult->FetchRow())
{
if ($labelscount > 0)
{
$mandatorys[]=$ia[1].$ansrow['code']."#0";
$mandatoryfns[]=$ia[1];
}
else
{
$mandatorys[]=$ia[1].$ansrow['code'];
$mandatoryfns[]=$ia[1];
}
// second label set
if ($labelscount1 > 0)
{
$mandatorys[]=$ia[1].$ansrow['code']."#1";
$mandatoryfns[]=$ia[1];
}
else
{
$mandatorys[]=$ia[1].$ansrow['code'];
$mandatoryfns[]=$ia[1];
}
}
if ($other == "Y" and ($ia[4]=="!" or $ia[4]=="L" or $ia[4]=="M" or $ia[4]=="P" or $ia[4]=="1"))
{
$mandatorys[]=$ia[1]."other";
$mandatoryfns[]=$ia[1];
}
return array($mandatorys, $mandatoryfns);
}
function retrieveAnswers($ia, $notanswered=null, $notvalidated=null)
{
//This function returns an array containing the "question/answer" html display
//and a list of the question/answer fieldnames associated. It is called from
//question.php, group.php or survey.php
//globalise required config variables
global $dbprefix, $shownoanswer, $clang; //These are from the config-defaults.php file
//-----
global $thissurvey, $gl; //These are set by index.php
global $connect;
//DISPLAY
$display = $ia[7];
//QUESTION NAME
$name = $ia[0];
$qtitle=$ia[3];
//Replace INSERTANS statements with previously provided answers;
while (strpos($qtitle, "{INSERTANS:") !== false)
{
$replace=substr($qtitle, strpos($qtitle, "{INSERTANS:"), strpos($qtitle, "}", strpos($qtitle, "{INSERTANS:"))-strpos($qtitle, "{INSERTANS:")+1);
$replace2=substr($replace, 11, strpos($replace, "}", strpos($replace, "{INSERTANS:"))-11);
$replace3=retrieve_Answer($replace2);
$qtitle=str_replace($replace, $replace3, $qtitle);
} //while
//GET HELP
$hquery="SELECT help FROM {$dbprefix}questions WHERE qid=$ia[0] AND language='".$_SESSION['s_lang']."'";
$hresult=db_execute_num($hquery) or safe_die($connect->ErrorMsg()); //Checked
$help="";
while ($hrow=$hresult->FetchRow()) {$help=$hrow[0];}
//A bit of housekeeping to stop PHP Notices
$answer = "";
if (!isset($_SESSION[$ia[1]])) {$_SESSION[$ia[1]] = "";}
$qidattributes=getQuestionAttributes($ia[0]);
//echo "
";print_r($qidattributes);echo "
";
//Create the question/answer html
switch ($ia[4])
{
case 'X': //BOILERPLATE QUESTION
$values = do_boilerplate($ia);
break;
case '5': //5 POINT CHOICE radio-buttons
$values = do_5pointchoice($ia);
break;
case 'D': //DATE
$values = do_date($ia);
break;
case 'Z': //LIST Flexible drop-down/radio-button list
$values = do_list_flexible_radio($ia);
if (!$displaycols=arraySearchByKey('hide_tip', $qidattributes, 'attribute', 1))
{
$qtitle .= " \n"
. $clang->gT('Choose one of the following answers').'';
}
break;
case 'L': //LIST drop-down/radio-button list
$values = do_list_radio($ia);
if (!$displaycols=arraySearchByKey('hide_tip', $qidattributes, 'attribute', 1))
{
$qtitle .= " \n"
. $clang->gT('Choose one of the following answers').'';
}
break;
case 'W': //List - dropdown
$values=do_list_flexible_dropdown($ia);
if (!$displaycols=arraySearchByKey('hide_tip', $qidattributes, 'attribute', 1))
{
$qtitle .= " \n"
. $clang->gT('Choose one of the following answers').'';
}
break;
case '!': //List - dropdown
$values=do_list_dropdown($ia);
if (!$displaycols=arraySearchByKey('hide_tip', $qidattributes, 'attribute', 1))
{
$qtitle .= " \n"
. $clang->gT('Choose one of the following answers').'';
}
break;
case 'O': //LIST WITH COMMENT drop-down/radio-button list + textarea
$values=do_listwithcomment($ia);
if (count($values[1]) > 1 && !$displaycols=arraySearchByKey('hide_tip', $qidattributes, 'attribute', 1))
{
$qtitle .= " \n"
. $clang->gT('Choose one of the following answers').'';
}
break;
case 'R': //RANKING STYLE
$values=do_ranking($ia);
if (count($values[1]) > 1 && !$displaycols=arraySearchByKey('hide_tip', $qidattributes, 'attribute', 1))
{
if ($minansw=arraySearchByKey("min_answers", $qidattributes, "attribute", 1))
{
$qtitle .= " \n"
. sprintf($clang->gT("Rank at least %d items"), $minansw['value'])."";
}
}
break;
case 'M': //MULTIPLE OPTIONS checkbox
$values=do_multiplechoice($ia);
if (count($values[1]) > 1 && !$displaycols=arraySearchByKey('hide_tip', $qidattributes, 'attribute', 1))
{
$maxansw=arraySearchByKey("max_answers", $qidattributes, "attribute", 1);
$minansw=arraySearchByKey("min_answers", $qidattributes, "attribute", 1);
if (!($maxansw || $minansw))
{
$qtitle .= " \n"
. $clang->gT('Check any that apply').'';
}
else
{
if ($maxansw && $minansw)
{
$qtitle .= " \n"
. sprintf($clang->gT("Check between %d and %d answers"), $minansw['value'], $maxansw['value'])."";
} elseif ($maxansw)
{
$qtitle .= " \n"
. sprintf($clang->gT("Check at most %d answers"), $maxansw['value'])."";
} else
{
$qtitle .= " \n"
. sprintf($clang->gT("Check at least %d answers"), $minansw['value'])."";
}
}
}
break;
case 'I': //Language Question
$values=do_language($ia);
if (count($values[1]) > 1)
{
$qtitle .= " \n"
. $clang->gT('Choose your language').'';
}
break;
case 'P': //MULTIPLE OPTIONS WITH COMMENTS checkbox + text
$values=do_multiplechoice_withcomments($ia);
if (count($values[1]) > 1 && !$displaycols=arraySearchByKey('hide_tip', $qidattributes, 'attribute', 1))
{
$maxansw=arraySearchByKey("max_answers", $qidattributes, "attribute", 1);
$minansw=arraySearchByKey("min_answers", $qidattributes, "attribute", 1);
if (!($maxansw || $minansw))
{
$qtitle .= " \n"
. $clang->gT('Check any that apply').'';
}
else
{
if ($maxansw && $minansw)
{
$qtitle .= " \n"
. sprintf($clang->gT("Check between %d and %d answers"), $minansw['value'], $maxansw['value'])."";
} elseif ($maxansw)
{
$qtitle .= " \n"
. sprintf($clang->gT("Check at most %d answers"), $maxansw['value'])."";
} else
{
$qtitle .= " \n"
. sprintf($clang->gT("Check at least %d answers"), $minansw['value'])."";
}
}
}
break;
case 'Q': //MULTIPLE SHORT TEXT
$values=do_multipleshorttext($ia);
break;
case 'K': //MULTIPLE NUMERICAL QUESTION
$values=do_multiplenumeric($ia);
break;
case 'N': //NUMERICAL QUESTION TYPE
$values=do_numerical($ia);
break;
case 'S': //SHORT FREE TEXT
$values=do_shortfreetext($ia);
break;
case 'T': //LONG FREE TEXT
$values=do_longfreetext($ia);
break;
case 'U': //HUGE FREE TEXT
$values=do_hugefreetext($ia);
break;
case 'Y': //YES/NO radio-buttons
$values=do_yesno($ia);
break;
case 'G': //GENDER drop-down list
$values=do_gender($ia);
break;
case 'A': //ARRAY (5 POINT CHOICE) radio-buttons
$values=do_array_5point($ia);
break;
case 'B': //ARRAY (10 POINT CHOICE) radio-buttons
$values=do_array_10point($ia);
break;
case 'C': //ARRAY (YES/UNCERTAIN/NO) radio-buttons
$values=do_array_yesnouncertain($ia);
break;
case 'E': //ARRAY (Increase/Same/Decrease) radio-buttons
$values=do_array_increasesamedecrease($ia);
break;
case 'F': //ARRAY (Flexible) - Row Format
$values=do_array_flexible($ia);
break;
case 'H': //ARRAY (Flexible) - Column Format
$values=do_array_flexiblecolumns($ia);
break;
// case '^': //SLIDER CONTROL
// $values=do_slider($ia);
// break;
case ':': //ARRAY (Multi Flexi) 1 to 10
$values=do_array_multiflexi($ia);
break;
case ';': //ARRAY (Multi Flexi) Text
$values=do_array_multitext($ia); //It's like the "5th element" movie, come to life
break;
case '1': //Array (Flexible Labels) dual scale
$values=do_array_flexible_dual($ia);
break;
} //End Switch
if (isset($values)) //Break apart $values array returned from switch
{
//$answer is the html code to be printed
//$inputnames is an array containing the names of each input field
list($answer, $inputnames)=$values;
}
$answer .= "\n\t\t\t'.$clang->gT('*').''.$qtitle;
}
//If this question is mandatory but wasn't answered in the last page
//add a message HIGHLIGHTING the question
$qtitle .= mandatory_message($ia);
$qtitle .= validation_message($ia);
$qanda=array($qtitle, $answer, $help, $display, $name, $ia[2], $gl[0], $ia[1]);
//New Return
return array($qanda, $inputnames);
}
function validation_message($ia)
{
//This function checks to see if this question requires validation and
//that validation has not been met.
global $notvalidated, $dbprefix, $connect, $clang;
$qtitle="";
if (isset($notvalidated) && is_array($notvalidated)) //ADD WARNINGS TO QUESTIONS IF THEY ARE NOT VALID
{
global $validationpopup, $popup;
if (in_array($ia[1], $notvalidated))
{
$help='';
$helpselect="SELECT help\n"
."FROM {$dbprefix}questions\n"
."WHERE qid={$ia[0]} AND language='".$_SESSION['s_lang']."'";
$helpresult=db_execute_assoc($helpselect) or safe_die($helpselect.' '.$connect->ErrorMsg()); //Checked
while ($helprow=$helpresult->FetchRow())
{
$help=' '.$helprow['help'].'';
}
$qtitle .= ' '.$clang->gT('This question must be answered correctly.').' '.$help.'
';
}
}
return $qtitle;
}
function mandatory_message($ia)
{
//This function checks to see if this question is mandatory and
//is being re-displayed because it wasn't answered. It returns
global $notanswered, $clang, $dbprefix;
$qtitle="";
if (isset($notanswered) && is_array($notanswered)) //ADD WARNINGS TO QUESTIONS IF THEY WERE MANDATORY BUT NOT ANSWERED
{
global $mandatorypopup, $popup;
if (in_array($ia[1], $notanswered))
{
$qtitle .= " ".$clang->gT('This question is mandatory').'.';
switch($ia[4])
{
case 'A':
case 'B':
case 'C':
case 'Q':
case 'K':
case 'F':
case 'J':
case 'H':
case ':':
$qtitle .= " \n".$clang->gT('Please complete all parts').'.';
break;
case '1':
$qtitle .= " \n".$clang->gT('Please check the items').'.';
break;
case 'R':
$qtitle .= " \n".$clang->gT('Please rank all items').'.';
break;
case 'M':
case 'P':
$qtitle .= ' '.$clang->gT('Please check at least one item.').'.';
$qquery = "SELECT other FROM {$dbprefix}questions WHERE qid=".$ia[0];
$qresult = db_execute_assoc($qquery); //Checked
$qrow = $qresult->FetchRow();
if ($qrow['other']=='Y')
{
$qidattributes=getQuestionAttributes($ia[0]);
if ($othertexts=arraySearchByKey('other_replace_text', $qidattributes, 'attribute', 1))
{
$othertext=$clang->gT($othertexts['value']);
}
else
{
$othertext=$clang->gT('Other');
}
$qtitle .= " \n".sprintf($clang->gT("If you choose '%s' you must provide a description."), $othertext);
}
break;
} // end switch
$qtitle .= " \n";
}
}
return $qtitle;
}
function mandatory_popup($ia, $notanswered=null)
{
//This sets the mandatory popup message to show if required
//Called from question.php, group.php or survey.php
if ($notanswered === null) {unset($notanswered);}
if (isset($notanswered) && is_array($notanswered)) //ADD WARNINGS TO QUESTIONS IF THEY WERE MANDATORY BUT NOT ANSWERED
{
global $mandatorypopup, $popup, $clang;
//POPUP WARNING
if (!isset($mandatorypopup) && ($ia[4] == 'T' || $ia[4] == 'S' || $ia[4] == 'U'))
{
$popup="\n";
$mandatorypopup="Y";
}else
{
$popup="\n";
$mandatorypopup="N";
}
return array($mandatorypopup, $popup);
}
else
{
return false;
}
}
function validation_popup($ia, $notvalidated=null)
{
//This sets the validation popup message to show if required
//Called from question.php, group.php or survey.php
if ($notvalidated === null) {unset($notvalidated);}
$qtitle="";
if (isset($notvalidated) && is_array($notvalidated)) //ADD WARNINGS TO QUESTIONS IF THEY ARE NOT VALID
{
global $validationpopup, $vpopup, $clang;
//POPUP WARNING
if (!isset($validationpopup))
{
$vpopup="\n";
$validationpopup="Y";
}
return array($validationpopup, $vpopup);
}
else
{
return false;
}
}
// ==================================================================
// setting constants for 'checked' and 'selected' inputs
define('CHECKED' , ' checked="checked"' , true);
define('SELECTED' , ' selected="selected"' , true);
// ==================================================================
// QUESTION METHODS =================================================
function do_boilerplate($ia)
{
$answer = '
';
$inputnames[]=$ia[1];
return array($answer, $inputnames);
}
// ---------------------------------------------------------------
function do_5pointchoice($ia)
{
global $shownoanswer, $clang;
$answer = "\n
\n";
for ($fp=1; $fp<=5; $fp++)
{
$answer .= "\t
\n\t\t\n\t\t\n\t
\n";
}
if ($ia[6] != "Y" && $shownoanswer == 1) // Add "No Answer" option if question is not mandatory
{
$answer .= "\t