📄 qfunctions.php
字号:
<?php
function getquestions( $admin, $currentquestion, $questioncount, $masurvey )
{
global $HTTP_POST_VARS;
global $phpQlang;
$sid = $masurvey['SID'];
$preview = $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" );
$foundpb = $questioncount < $cquestion ? 1 : 0;
while ( $maquery = mysql_fetch_array( $mqquery ) )
{
if ( $maquery['Type'] == "pagebreak" )
{
if ( $foundpb )
{
break;
}
else
{
++$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;
}
else
{
$answervalidity = storeanswer( $maquestion, $masurvey );
if ( !$answervalidity[0] )
{
break;
}
if ( $maquestion['Type'] == "checkbox" || $maquestion['Type'] == "selectmult" )
{
$l = 0;
for ( ; $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'];
continue;
break;
}
}
}
else if ( $prevcq )
{
$cquestion = $prevcq;
}
if ( $masurvey['AdminNotes'] && $cquestion == 1 )
{
$contents .= "<TR><TD>".$masurvey['AdminNotes']."<BR><BR></TD></TR>\n";
}
if ( $questioncount <= $validthru )
{
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'] );
}
}
else if ( $questioncount < $cquestion )
{
$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(
$overallvalidity,
$nextcq,
$contents
);
}
function validanswerchoices( $qanswer )
{
$validanswers = split( "\\|\\|\\|", $qanswer );
$i = 0;
for ( ; $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;
global $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 == "" && $required )
{
$answervalidity = false;
$message = $phpQlang['EmptyAnswer'];
}
if ( !$answervalidity )
{
break;
}
$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 );
$i = 0;
for ( ; $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] ) )
{
break;
}
$cmsg = $choose[1] == 1 ? $phpQlang['ExactCount1'] : $phpQlang['ExactCount'];
break;
case "l" :
if ( !( $answercount < $choose[1] ) )
{
break;
}
$cmsg = $choose[1] == 1 ? $phpQlang['AtLeastCount1'] : $phpQlang['AtLeastCount'];
break;
case "m" :
if ( $choose[1] < $answercount )
{
break;
}
$cmsg = $choose[1] == 1 ? $phpQlang['AtMostCount1'] : $phpQlang['AtMostCount'];
}
if ( $cmsg != "" )
{
$answervalidity = false;
$message = sprintf( $cmsg, $choose[1] );
}
}
}
$choices = split( "\\|\\|\\|", $qanswer );
$k = 0;
for ( ; $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 );
}
}
}
}
else if ( substr( $subid, 0, 1 ) == "O" && $answervar3 != "" )
{
$answervalidity = false;
$message = sprintf( $phpQlang['SelectTextfield'], $optiontext );
}
}
$tempanswer = substr( $tempanswer, 2 );
if ( !( $answervalidity && !$invalidselect ) )
{
break;
}
$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;
$message = $phpQlang['InvalidAnswer'];
}
}
$choices = split( "\\|\\|\\|", $qanswer );
$k = 0;
for ( ; $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 == "select" )
{
$optiontext = protecthtml( $optiontext );
}
$answervar3 = $HTTP_POST_VARS["Q".$qid.$subid];
if ( $uanswer == $subid )
{
if ( $qtype == "select" && $qparts[4] == "N" )
{
if ( $required )
{
$answervalidity = false;
$message = sprintf( $phpQlang['UnselectableOption'], $optiontext );
}
else
{
$invalidselect = true;
}
}
else
{
$tempanswer = $uanswer;
if ( substr( $subid, 0, 1 ) == "O" )
{
if ( $answervar3 != "" )
{
$tempanswer .= "|".$answervar3;
}
else
{
$answervalidity = false;
$message = sprintf( $phpQlang['CompleteTextfield'], $optiontext );
}
}
}
}
else if ( substr( $subid, 0, 1 ) == "O" && $answervar3 != "" )
{
$answervalidity = false;
$message = sprintf( $phpQlang['SelectTextfield'], $optiontext );
}
}
if ( !( $answervalidity && !$invalidselect ) )
{
break;
}
$contents = $tempanswer;
}
if ( !$answervalidity && $message )
{
$contents = "<TR><TD>";
$contents .= "<TABLE CELLPADDING=2 CELLSPACING=0 BORDER=0><TR>";
$contents .= "<TD STYLE=\"{$errorstyle}\">{$message}</TD>";
$contents .= "</TR></TABLE>";
$contents .= "</TD></TR>";
}
return array(
$answervalidity,
$contents
);
}
function printquestion( $maquestion, $masurvey )
{
global $phpQlang;
$question = freehtml( $maquestion['Question'] );
$questionstyle = $maquestion['QuestionStyle'] ? $maquestion['QuestionStyle'] : $masurvey['QuestionStyle'];
$required = $maquestion['Required'] == "n" ? false : true;
$autonumb = $masurvey['QuestionNumb'];
$mqqnum = mysql_query( "select COUNT(*) as qnum from phpQQuestion WHERE SID=\"".protectmysql( $maquestion['SID'] )."\" and SortOrder<\"".protectmysql( $maquestion['SortOrder'] )."\" and Type != 'pagebreak' AND Type != 'htmlcontent'" );
$maqnum = mysql_fetch_array( $mqqnum );
$contents = "<TR><TD>\n";
if ( $maquestion['Instructions'] )
{
$contents .= $maquestion['Instructions'];
}
if ( $maquestion['Type'] != "htmlcontent" )
{
$contents .= "<TABLE CELLPADDING=2 CELLSPACING=0 BORDER=0><TR>";
if ( $autonumb == "y" )
{
if ( $required == "y" )
{
$contents .= "<TD VALIGN=top><B>".$phpQlang['*']."</B></TD>\n";
}
else
{
$contents .= "<TD> </TD>\n";
}
$contents .= ( "<TD STYLE=\"{$questionstyle}\" ALIGN=right VALIGN=top><B>".( $maqnum['qnum'] + 1 ) ).".</B></TD>\n";
}
$contents .= "<TD WIDTH=100% STYLE=\"{$questionstyle}\">".$question."</TD>\n";
$contents .= "</TR></TABLE>\n";
}
$contents .= "</TD></TR>\n";
return $contents;
}
function printanswers( $maquestion, $masurvey, $uidanswer, $print = 0 )
{
global $HTTP_POST_VARS;
$qid = $maquestion['QID'];
$type = $maquestion['Type'];
$indent = $masurvey['Indent'];
$questionstyle = $maquestion['QuestionStyle'] ? $maquestion['QuestionStyle'] : $masurvey['QuestionStyle'];
$answerinputstyle = $maquestion['AnswerInputStyle'] ? $maquestion['AnswerInputStyle'] : $masurvey['AnswerInputStyle'];
$answertextstyle = $maquestion['AnswerTextStyle'] ? $maquestion['AnswerTextStyle'] : $masurvey['AnswerTextStyle'];
$answerstyle = $maquestion['AnswerStyle'] ? $maquestion['AnswerStyle'] : $masurvey['AnswerStyle'];
$attributes = $maquestion['Attributes'];
$answer = protecthtml( $maquestion['Answer'] );
$defaultanswer = $maquestion['DefaultAnswer'];
$colnumb = $maquestion['ColNumb'];
$mult = $maquestion['Mult'];
$sortorder = $maquestion['SortOrder'];
$required = $maquestion['Required'] == "n" ? false : true;
$contents .= "<TR><TD>\n";
switch ( $type )
{
case "radio" :
$contents .= "<TABLE CELLPADDING=2 CELLSPACING=0 BORDER=0>\n";
$contents .= "<TR><TD WIDTH={$indent}></TD><TD>\n";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -