1 ? "Sorry, try again." : "If you would like to leave a comment, please select the most correct statement from each of the three groups.";
$form_html = "
\n";
$form_html .= "

\n";
$form_html .= "
$form_greeting
\n";
$form_html .= "
\n";
$form_html .= "\n";
$form_html .= "\n";
$form_html .= "
\n";
return $form_html;
}
function create_form($true_source, $false_source)
{
// Load true and false sentences, and make sure false sentences are actually false
$true_sentences = parse_sentences($true_source);
$false_sentences = parse_sentences($false_source);
$false_sentences = array_diff($false_sentences, $true_sentences);
// Randomize order of arrays
srand(microtime() * 1000000);
shuffle($true_sentences);
shuffle($false_sentences);
// Add 3 true and six false sentences, but make sure they are at least 5 words long
$questions = array();
$answers = array();
while(count($questions) < 3 && count($true_sentences) > 0) {
$sentence = array_pop($true_sentences);
trim($sentence);
if (str_word_count($sentence) > 4) {
array_push($questions, $sentence);
array_push($answers, $sentence);
}
}
// TODO: What if there are not three sentences?
while(count($questions) < 9 && count($false_sentences) > 0) {
$sentence = array_pop($false_sentences);
$sentence = trim($sentence);
if (str_word_count($sentence) > 4)
array_push($questions, $sentence);
}
// Randomize order of questions
shuffle($questions);
// Create the form from the array of sentences
$form_greeting = $_SESSION['attempts'] > 1 ? "Sorry, try again." : "If you would like to leave a comment, please select three sentences that appeared in this post.";
$form_html = "\n";
$form_html .= "

\n";
$form_html .= "
$form_greeting
\n";
$form_html .= "
\n";
$form_html .= "\n";
$form_html .= "\n";
$form_html .= "
\n";
return $form_html;
}
function parse_sentences($html_source)
{
$body_tags = $html_source->getElementsByTagName("body");
$body_tag = $body_tags->item(0);
$body_text = strip_tags($body_tag->nodeValue, "");
$sentences = split("\n|([\.?!]\s*)", $body_text);
return $sentences;
}
?>