📄 qfunctions.php
字号:
<?
################################################################################
# phpQuestionnaire Version 2.2 #
# Copyright 2003-2005 chumpsoft, inc. March 28, 2005 #
################################################################################
# SCRiPTMAFiA 2005 - THE DiRTY HANDS ON YOUR SCRiPTS #
################################################################################
function GetQuestions ($admin, $currentquestion, $questioncount, $masurvey) {
global $HTTP_POST_VARS, $phpQlang;
$sid = $masurvey["SID"];
$preview = ($GLOBALS["action"] == "preview" && $admin) ? 1 : 0;
$skip = ($preview && isset($HTTP_POST_VARS["skip"])) ? 1 : 0;
$cquestion = ($currentquestion == 'done') ? ($questioncount + 1) : $currentquestion;
$pagebreak = false;
$overallvalidity = true;
$nextcq = 0;
$contents = '';
$prevcq = 0;
$squestion = 0;
if ($preview) {
$mqquery = mysql_query("select SortOrder,Type from phpQQuestion WHERE SID = \"".ProtectMySQL($sid)."\" and SortOrder < \"".ProtectMySQL($cquestion)."\" order by SortOrder desc");
# This ensures that if submit was pressed, we don't backtrack 2 pagebreaks.
$foundpb = ($cquestion > $questioncount) ? 1 : 0;
while ($maquery = mysql_fetch_array($mqquery)) {
if ($maquery["Type"] == 'pagebreak') { if ($foundpb) { break; } $foundpb++; }
else { $squestion = $maquery["SortOrder"]; }
$i++;
}
}
$validthru = 0;
$hiddenfields = '';
if (!$skip) {
$mqquestion = mysql_query("select * from phpQQuestion where SID = \"".ProtectMySQL($sid)."\" and SortOrder < \"".ProtectMySQL($cquestion)."\" and SortOrder >= \"".ProtectMySQL($squestion)."\" order by SortOrder");
while ($maquestion = mysql_fetch_array($mqquestion)) {
$qid = $maquestion["QID"];
$prevcq = !$prevcq ? $maquestion["SortOrder"] : $prevcq;
if ($maquestion["Type"] == "pagebreak") { $prevcq = 0; continue; }
$answervalidity = StoreAnswer($maquestion, $masurvey);
if ($answervalidity[0]) {
if ($maquestion["Type"] == 'checkbox' || $maquestion["Type"] == 'selectmult') {
for ($l=0; $l<sizeof($HTTP_POST_VARS["Q".$qid]); $l++) {
$value = $HTTP_POST_VARS["Q".$qid][$l];
$hiddenfields .= "<INPUT TYPE=hidden name=\"Q" . $qid . "[]\" value=\"" . ProtectHTML($value) . "\">\n";
if ($HTTP_POST_VARS["Q".$qid.$value] != '') {
$hiddenfields .= "<INPUT TYPE=hidden name=\"Q".$qid.$value."\" value=\"" . ProtectHTML($HTTP_POST_VARS["Q".$qid.$value]) . "\">\n";
}
}
}
else {
$value = $HTTP_POST_VARS["Q".$qid];
$hiddenfields .= "<input type=hidden name=\"Q" . $qid . "\" value=\"" . ProtectHTML($value) . "\">\n";
if (($maquestion["Type"] == 'radio' || $maquestion["Type"] == 'select') && $HTTP_POST_VARS["Q".$qid.$value] != '') {
$hiddenfields .= "<INPUT TYPE=hidden name=\"Q".$qid.$value."\" value=\"" . ProtectHTML($HTTP_POST_VARS["Q".$qid.$value]) . "\">\n";
}
}
$validthru = $maquestion["SortOrder"];
}
else {
break;
}
}
}
if ($prevcq) { $cquestion = $prevcq; }
if ($masurvey["AdminNotes"] && $cquestion == 1) {
$contents .= "<TR><TD>" . $masurvey["AdminNotes"] . "<BR><BR></TD></TR>\n";
}
if ($validthru >= $questioncount) { return array(1, 'complete', ''); }
$pagecount = mysql_fetch_array(mysql_query("select count(*) from phpQQuestion where SID = \"".ProtectMySQL($sid)."\" and Type='pagebreak'"));
$currentpage = mysql_fetch_array(mysql_query("select count(*) from phpQQuestion where SID = \"".ProtectMySQL($sid)."\" and SortOrder < \"".ProtectMySQL($cquestion)."\" and Type='pagebreak'"));
if ($pagecount[0]) {
$GLOBALS["PageNumber"] = sprintf($phpQlang["PageNumber"], $currentpage[0] + 1, $pagecount[0] + 1);
}
$totalerrors = 0;
$contents .= '::PAGEERRORS::';
while (!$pagebreak) {
$mqquestion = mysql_query("select * from phpQQuestion where SID = \"".ProtectMySQL($sid)."\" and SortOrder = \"".ProtectMySQL($cquestion)."\"");
if ($maquestion = mysql_fetch_array($mqquestion)) {
if ($maquestion["Type"] == "pagebreak") {
$pagebreak = true;
$nextcq = $cquestion+1;
$contents .= PrintSubmit($phpQlang["Next"], $masurvey, $preview);
}
else {
if (!$skip) {
$answered = $HTTP_POST_VARS["Q".$maquestion["QID"]."Answered"];
if ($answered) {
$answervalidity = StoreAnswer($maquestion, $masurvey);
if (!$answervalidity[0]) {
$contents .= $answervalidity[1];
$totalerrors++;
$overallvalidity = false;
}
}
else {
$overallvalidity = false;
}
}
$contents .= PrintQuestion($maquestion, $masurvey);
$contents .= PrintAnswers($maquestion, $masurvey, 0);
$hiddenfields .= PrintHiddenVars($maquestion["QID"]);
}
}
elseif ($cquestion > $questioncount) {
$pagebreak = true;
$nextcq = "done";
$contents .= PrintSubmit($phpQlang["Submit"], $masurvey, $preview);
}
$cquestion++;
}
if ($totalerrors) {
$errorstyle = $masurvey["ErrorStyle"];
$estring = $totalerrors == 1 ? 'error' : 'errors';
$errtext = sprintf($phpQlang['PleaseCorrect'], sprintf($phpQlang[$estring], $totalerrors));
$contents = str_replace('::PAGEERRORS::', "<TR><TD STYLE=\"$errorstyle\">$errtext</TD></TR><TR><TD> </TD></TR>", $contents);
}
else {
$contents = str_replace('::PAGEERRORS::', '', $contents);
}
$contents .= $hiddenfields;
$contents .= "<input type=hidden name=\"cq\" value=\"$nextcq\">\n";
# Return Array
# 0 - True if every answer is valid otherwise false
# 1 - The first question count in the survey
# 2 - The content output of the survey
return array($overallvalidity, $nextcq, $contents);
}
function ValidAnswerChoices ($qanswer) {
$validanswers = split("\|\|\|", $qanswer);
for ($i=0; $i<sizeof($validanswers); $i++) {
$tempchoice = split("\|\|", $validanswers[$i]);
$validanswers[$i] = (substr($tempchoice[2],0,11) == "Textfield:(") ? "O".$tempchoice[1] : "N".$tempchoice[1];
}
return $validanswers;
}
function StoreAnswer ($maquestion, $masurvey) {
global $HTTP_POST_VARS, $phpQlang;
$message = '';
$answervalidity = true;
$invalidselect = false;
$qid = $maquestion["QID"];
$errorstyle = $masurvey["ErrorStyle"];
$required = ($maquestion["Required"] == "n") ? false : true;
$contents = '';
$qtype = $maquestion["Type"];
$qanswer = $maquestion["Answer"];
$uanswer = $HTTP_POST_VARS["Q".$qid];
switch ($qtype) {
case "textfield":
case "textarea":
if ($uanswer == "") {
if ($required) {
$answervalidity = false;
$message = $phpQlang["EmptyAnswer"];
}
}
if ($answervalidity) {
$contents = $uanswer;
}
break;
case "selectmult":
case "checkbox":
$tempanswer = "";
if (!is_array($uanswer) || count($uanswer) == 0) {
if ($required) {
$answervalidity = false;
$message = $phpQlang["EmptyAnswer"];
}
}
else {
$validanswers = ValidAnswerChoices($qanswer);
for ($i=0; $i<sizeof($uanswer); $i++) {
if (!in_array($uanswer[$i], $validanswers)) {
$answervalidity = false;
$message = $phpQlang["InvalidAnswer"];
}
}
if ($answervalidity && $maquestion["Choose"]) {
$choose = split(":", $maquestion["Choose"]);
$answercount = sizeof($uanswer);
$cmsg = '';
switch ($choose[0]) {
case "e":
if ($answercount != $choose[1]) {
$cmsg = ($choose[1] == 1) ? $phpQlang["ExactCount1"] : $phpQlang["ExactCount"];
}
break;
case "l":
if ($answercount < $choose[1]) {
$cmsg = ($choose[1] == 1) ? $phpQlang["AtLeastCount1"] : $phpQlang["AtLeastCount"];
}
break;
case "m":
if ($answercount > $choose[1]) {
$cmsg = ($choose[1] == 1) ? $phpQlang["AtMostCount1"] : $phpQlang["AtMostCount"];
}
break;
}
if ($cmsg != '') {
$answervalidity = false;
$message = sprintf($cmsg, $choose[1]);
}
}
}
$choices = split("\|\|\|", $qanswer);
for ($k=0; $k<sizeof($choices); $k++) {
$qparts = split("\|\|", $choices[$k]);
$subid = "N".$qparts[1];
if (substr($qparts[2],0,11) == "Textfield:(") {
$subid = "O".$qparts[1];
$optiontext = substr($qparts[2],11,-1);
}
else {
$optiontext = $qparts[2];
}
if ($qtype == "selectmult") { $optiontext = ProtectHTML($optiontext); }
$answervar3 = $HTTP_POST_VARS["Q".$qid.$subid];
if (is_array($uanswer) && in_array($subid, $uanswer)) {
if ($qtype == "selectmult" && $qparts[4] == "N") {
if ($required) {
$answervalidity = false;
$message = sprintf($phpQlang["UnselectableOption"], $optiontext);
}
else {
$invalidselect = true;
}
}
else {
$tempanswer .= "||".$subid;
if (substr($subid,0,1) == "O") {
if ($answervar3 != '') {
$tempanswer .= "|".$answervar3;
}
else {
$answervalidity = false;
$message = sprintf($phpQlang["CompleteTextfield"], $optiontext);
}
}
}
}
elseif (substr($subid,0,1) == "O" && $answervar3 != '') {
$answervalidity = false;
$message = sprintf($phpQlang["SelectTextfield"], $optiontext);
}
}
$tempanswer = substr($tempanswer,2);
if ($answervalidity && !$invalidselect) {
$contents = $tempanswer;
}
break;
case "radio":
case "select":
if ($uanswer == "") {
if ($required) {
$answervalidity = false;
$message = $phpQlang["EmptyAnswer"];
}
}
else {
$validanswers = ValidAnswerChoices($qanswer);
if (!in_array($uanswer, $validanswers)) {
$answervalidity = false;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -