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"; $answer_key = array(); $question_index = 0; $group_index = 1; foreach ($question_groups as $question_group) { $correct_answer = $question_group[0]; shuffle($question_group); $form_html .= " Group $group_index:
\n"; $group_index += 1; foreach ($question_group as $answer) { $question = "sentence$question_index"; $answer_key[$question] = $answer == $correct_answer ? $question : FALSE; $form_html .= " $answer
\n"; $question_index++; } } $_SESSION['answer_key'] = $answer_key; $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"; $answer_key = array(); for ($question_index = 0; $question_index < count($questions); $question_index++) { $question = "sentence$question_index"; $answer = $questions[$question_index]; $answer_key[$question] = in_array($answer, $answers, TRUE) ? $question : FALSE; $form_html .= " $answer
\n"; } $_SESSION['answer_key'] = $answer_key; $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; } ?>