gT("Save your responses so far"))))
{
$backok="Y";
}
else
{
$backok="N";
}
//Now, we check mandatory questions if necessary
//CHECK IF ALL CONDITIONAL MANDATORY QUESTIONS THAT APPLY HAVE BEEN ANSWERED
$notanswered=addtoarray_single(checkmandatorys($move,$backok),checkconditionalmandatorys($move,$backok));
//CHECK PREGS
$notvalidated=checkpregs($move,$backok);
//CHECK QUOTA
if ($thissurvey['active'] == "Y")
{
check_quota('enforce',$surveyid);
}
//SEE IF $surveyid EXISTS ####################################################################
if ($surveyexists <1)
{
sendcacheheaders();
doHeader();
//SURVEY DOES NOT EXIST. POLITELY EXIT.
echo templatereplace(file_get_contents("$thistpl/startpage.pstpl"));
echo "\t
\n";
echo "\t".$clang->gT("Sorry. There is no matching survey.")."
\n";
echo templatereplace(file_get_contents("$thistpl/endpage.pstpl"));
doFooter();
exit;
}
//RUN THIS IF THIS IS THE FIRST TIME
if (!isset($_SESSION['step']) || !$_SESSION['step'])
{
$totalquestions = buildsurveysession();
sendcacheheaders();
doHeader();
echo templatereplace(file_get_contents("$thistpl/startpage.pstpl"));
echo "\n\n";
echo templatereplace(file_get_contents("$thistpl/endpage.pstpl"));
doFooter();
exit;
}
//******************************************************************************************************
//PRESENT SURVEY
//******************************************************************************************************
//GET GROUP DETAILS
if ($_SESSION['step'] == "0") {$currentquestion=$_SESSION['step'];}
else {$currentquestion=$_SESSION['step']-1;}
$ia=$_SESSION['fieldarray'][$currentquestion];
list($newgroup, $gid, $groupname, $groupdescription, $gl)=checkIfNewGroup($ia);
// MANAGE CONDITIONAL QUESTIONS
$conditionforthisquestion=$ia[7];
$questionsSkipped=0;
while ($conditionforthisquestion == "Y") //IF CONDITIONAL, CHECK IF CONDITIONS ARE MET
{
$cquery="SELECT distinct cqid FROM {$dbprefix}conditions WHERE qid={$ia[0]}";
$cresult=db_execute_assoc($cquery) or safe_die("Couldn't count cqids
$cquery
".$connect->ErrorMsg()); //Checked
$cqidcount=$cresult->RecordCount();
$cqidmatches=0;
while ($crows=$cresult->FetchRow())//Go through each condition for this current question
{
//Check if the condition is multiple type
$ccquery="SELECT type FROM {$dbprefix}questions WHERE qid={$crows['cqid']} AND language='".$_SESSION['s_lang']."' ";
$ccresult=db_execute_assoc($ccquery) or safe_die ("Coudn't get type from questions
$ccquery
".$connect->ErrorMsg()); //Checked
while($ccrows=$ccresult->FetchRow())
{
$thistype=$ccrows['type'];
}
// In case thistype = Q or K, then multiple conditions are ANDed
// and thus must match
// ==> increase $cqidcount to the number of conditions
// avoiding the 'distinct' keyword in the SQL above
// (which is used for type M or P questions whose conditions
// are ORed)
if ($thistype =="Q" || $thistype =="K")
{
$cquery2="SELECT cqid FROM {$dbprefix}conditions WHERE qid={$ia[0]} AND cqid={$crows['cqid']}";
$cresult2=db_execute_assoc($cquery2) or safe_die("Couldn't count cqids
$cquery
".$connect->ErrorMsg()); //Checked
$cqidcount2=$cresult2->RecordCount();
$cqidcount += $cqidcount2 - 1; // substract 1 as it has been already counted once by $cquery
}
$cqquery = "SELECT cfieldname, value, cqid, method FROM {$dbprefix}conditions WHERE qid={$ia[0]} AND cqid={$crows['cqid']}";
$cqresult = db_execute_assoc($cqquery) or safe_die("Couldn't get conditions for this question/cqid
$cquery
".$connect->ErrorMsg()); //Checked
$amatchhasbeenfound="N";
while ($cqrows=$cqresult->FetchRow()) //Check each condition
{
$currentcqid=$cqrows['cqid'];
$conditionfieldname=$cqrows['cfieldname'];
if (!$cqrows['value'] || $cqrows['value'] == ' ')
{
$conditionvalue="NULL";
}
else
{
$conditionvalue=$cqrows['value'];
}
if ($thistype == "M" || $thistype == "P") //Adjust conditionfieldname for multiple option type questions
{
$conditionfieldname .= $conditionvalue;
$conditionvalue = "Y";
}
// If condition value is @SIDXGIDXQID[aid]@ field
if (ereg('^@([0-9]+X[0-9]+X[^@]+)@',$conditionvalue, $targetconditionfieldname))
{
$conditionvalue = $_SESSION[$targetconditionfieldname[1]];
}
if (trim($cqrows['method'])=='') {$cqrows['method']='==';}
if (!isset($_SESSION[$conditionfieldname]) ||
$_SESSION[$conditionfieldname] == '' ||
$_SESSION[$conditionfieldname] == ' ')
{
if($thistype == "K")
{
$currentvalue = 0;
} else {
$currentvalue="NULL";
}
}
else
{
$currentvalue=$_SESSION[$conditionfieldname];
}
if ( $cqrows['method'] != 'RX')
{
if (eval('if ($currentvalue'. $cqrows['method'].'$conditionvalue) return true; else return false;'))
{
$amatchhasbeenfound="Y";
}
}
else
{
if (ereg($conditionvalue,$currentvalue))
{
$amatchhasbeenfound="Y";
}
}
if ( ($thistype =="Q" || $thistype =="K") &&
$amatchhasbeenfound=="Y")
{
// For type Q/K questions each match is counted
// because this is an AND condition
$cqidmatches++;
// then we reset matchfound switch in order
// to check the next condition
$amatchhasbeenfound="N";
}
}
if ($amatchhasbeenfound == "Y" &&
($thistype !="Q" && $thistype !="K") )
{
// For all other question type than Q and K,
// conditions on same Question are ORed (type M or P)
// so increment counter at least one of the conditions matches
$cqidmatches++;
}
}
if ($cqidmatches == $cqidcount)
{
//a match has been found in ALL distinct cqids. The question WILL be displayed
$conditionforthisquestion="N";
}
else
{
//matches have not been found in ALL distinct cqids. The question WILL NOT be displayed
$questionsSkipped++;
if (returnglobal('move') == "movenext")
{
$currentquestion++;
if(isset($_SESSION['fieldarray'][$currentquestion]))
{
$ia=$_SESSION['fieldarray'][$currentquestion];
}
if ($_SESSION['step']>=$_SESSION['totalsteps'])
{
$move="movesubmit";
submitanswer(); // complete this answer (submitdate)
break;
}
$_SESSION['step']++;
foreach ($_SESSION['grouplist'] as $gl)
{
if ($gl[0] == $ia[5])
{
$gid=$gl[0];
$groupname=$gl[1];
$groupdescription=$gl[2];
if (auto_unescape($_POST['lastgroupname']) != strip_tags($groupname) && $groupdescription) {$newgroup = "Y";} else {$newgroup == "N";}
}
}
}
elseif (returnglobal('move') == "moveprev")
{
$currentquestion--;
$ia=$_SESSION['fieldarray'][$currentquestion];
$_SESSION['step']--;
}
$conditionforthisquestion=$ia[7];
}
}
//SUBMIT
if ((isset($move) && $move == "movesubmit") && (!isset($notanswered) || !$notanswered) && (!isset($notvalidated) || !$notvalidated ))
{
if ($thissurvey['refurl'] == "Y")
{
if (!in_array("refurl", $_SESSION['insertarray'])) //Only add this if it doesn't already exist
{
$_SESSION['insertarray'][] = "refurl";
}
// $_SESSION['refurl'] = $_SESSION['refurl'];
}
//COMMIT CHANGES TO DATABASE
if ($thissurvey['active'] != "Y")
{
sendcacheheaders();
doHeader();
echo templatereplace(file_get_contents("$thistpl/startpage.pstpl"));
//Check for assessments
$assessments = doAssessment($surveyid);
if ($assessments)
{
echo templatereplace(file_get_contents("$thistpl/assessment.pstpl"));
}
$completed = "
".$clang->gT("Did Not Save")."
\n\n";
$completed .= $clang->gT("Your survey responses have not been recorded. This survey is not yet active.")."
\n";
$completed .= "".$clang->gT("Clear Responses")."
\n";
}
else
{
if ($thissurvey['usecookie'] == "Y" && $tokensexist != 1) //don't use cookies if tokens are being used
{
$cookiename="PHPSID".returnglobal('sid')."STATUS";
setcookie("$cookiename", "COMPLETE", time() + 31536000);
}
$content='';
$content .= templatereplace(file_get_contents("$thistpl/startpage.pstpl"));
//Check for assessments
$assessments = doAssessment($surveyid);
if ($assessments)
{
$content .= templatereplace(file_get_contents("$thistpl/assessment.pstpl"));
}
$completed = "
"
.$clang->gT("Thank you")."
\n\n"
.$clang->gT("Your survey responses have been recorded.")."
\n"
.""
.$clang->gT("Close this Window")."
\n";
// Link to Print Answer Preview **********
if ($thissurvey['printanswers']=='Y')
{
$completed .= "
"
.""
.$clang->gT("Click here to print your answers.")
."
\n";
}
//*****************************************
//Update the token if needed and send a confirmation email
if (isset($clienttoken) && $clienttoken)
{
submittokens();
}
//Send notification to survey administrator //Thanks to Jeff Clement http://jclement.ca
if ($thissurvey['sendnotification'] > 0 && $thissurvey['adminemail'])
{
sendsubmitnotification($thissurvey['sendnotification']);
}
$_SESSION['finished']=true;
$_SESSION['sid']=$surveyid;
if (isset($thissurvey['autoredirect']) && $thissurvey['autoredirect'] == "Y" && $thissurvey['url'])
{
//Automatically redirect the page to the "url" setting for the survey
$url = $thissurvey['url'];
$url=str_replace("{SAVEDID}",$saved_id, $url); // to activate the SAVEDID in the END URL
$url=str_replace("{TOKEN}",$clienttoken, $url); // to activate the TOKEN in the END URL
$url=str_replace("{SID}", $surveyid, $url); // to activate the SID in the RND URL
header("Location: {$url}");
}
doHeader();
if (isset($content)) {echo $content;}
}
echo templatereplace(file_get_contents("$thistpl/completed.pstpl"));
echo "\n
\n";
echo templatereplace(file_get_contents("$thistpl/endpage.pstpl"));
doFooter();
if($thissurvey['printanswers'] != 'Y' && $thissurvey['usecookie'] != 'Y' && $tokensexist !=1)
{
session_unset();
session_destroy();
}
exit;
}
if ($questionsSkipped == 0 && $newgroup == "Y" && isset($move) && $move == "moveprev" && (isset($_POST['grpdesc']) && $_POST['grpdesc']=="Y")) //a small trick to manage moving backwards from a group description
{
//This does not work properly in all instances.
$currentquestion++;
$ia=$_SESSION['fieldarray'][$currentquestion];
$_SESSION['step']++;
}
list($newgroup, $gid, $groupname, $groupdescription, $gl)=checkIfNewGroup($ia);
//Check if current page is for group description only
$bIsGroupDescrPage = false;
if ($newgroup == "Y" && $groupdescription && (isset($move) && $move != "moveprev"))
{
// This is a group description page
// - $ia contains next question description,
// but his question is not displayed, it is only used to know current group
// - in this case answers' inputnames mustn't be added to filednames hidden input
$bIsGroupDescrPage = true;
}
require_once("qanda.php");
$mandatorys=array();
$mandatoryfns=array();
$conmandatorys=array();
$conmandatoryfns=array();
$conditions=array();
$inputnames=array();
list($plus_qanda, $plus_inputnames)=retrieveAnswers($ia);
if ($plus_qanda)
{
$qanda[]=$plus_qanda;
}
if ($plus_inputnames && !$bIsGroupDescrPage)
{
// Add answers' inputnames to $inputnames unless this is a group description page
$inputnames = addtoarray_single($inputnames, $plus_inputnames);
}
//Display the "mandatory" popup if necessary
if (isset($notanswered) && $notanswered!=false)
{
list($mandatorypopup, $popup)=mandatory_popup($ia, $notanswered);
}
//Display the "validation" popup if necessary
if (isset($notvalidated))
{
list($validationpopup, $vpopup)=validation_popup($ia, $notvalidated);
}
//Get list of mandatory questions
list($plusman, $pluscon)=create_mandatorylist($ia);
if ($plusman !== null)
{
list($plus_man, $plus_manfns)=$plusman;
$mandatorys=addtoarray_single($mandatorys, $plus_man);
$mandatoryfns=addtoarray_single($mandatoryfns, $plus_manfns);
}
if ($pluscon !== null)
{
list($plus_conman, $plus_conmanfns)=$pluscon;
$conmandatorys=addtoarray_single($conmandatorys, $plus_conman);
$conmandatoryfns=addtoarray_single($conmandatoryfns, $plus_conmanfns);
}
//Build an array containing the conditions that apply for this page
$plus_conditions=retrieveConditionInfo($ia); //Returns false if no conditions
if ($plus_conditions)
{
$conditions = addtoarray_single($conditions, $plus_conditions);
}
//------------------------END DEVELOPMENT OF QUESTION
$percentcomplete = makegraph($_SESSION['step'], $_SESSION['totalsteps']);
//READ TEMPLATES, INSERT DATA AND PRESENT PAGE
sendcacheheaders();
doHeader();
if (isset($popup)) {echo $popup;}
if (isset($vpopup)) {echo $vpopup;}
echo templatereplace(file_get_contents("$thistpl/startpage.pstpl"));
echo "\n\n";
//foreach(file("$thistpl/endpage.pstpl") as $op)
//{
// echo templatereplace($op);
//}
echo templatereplace(file_get_contents("$thistpl/endpage.pstpl"));
doFooter();
function checkIfNewGroup($ia)
{
foreach ($_SESSION['grouplist'] as $gl)
{
if ($gl[0] == $ia[5])
{
$gid=$gl[0];
$groupname=$gl[1];
$groupdescription=$gl[2];
if (isset($_POST['lastgroupname']) && auto_unescape($_POST['lastgroupname']) != strip_tags($groupname) && $groupdescription)
{
$newgroup = "Y";
}
else
{
$newgroup = "N";
}
if (!isset($_POST['lastgroupname'])) {$newgroup="Y";}
}
}
return array($newgroup, $gid, $groupname, $groupdescription, $gl);
}
?>