History

Nulla risus nulla, volutpat vel fringilla eget, auctor a magna. Proin quis sapien sit amet metus ultricies rutrum. Ut tincidunt libero vel lectus scelerisque sit amet accumsan odio facilisis. Morbi vel magna nisi. Praesent egestas erat in felis cursus eget vestibulum nibh blandit. Phasellus pellentesque luctus aliquam. Mauris ultrices, justo sed tempor feugiat, est urna porta nulla, eu blandit tellus nulla at justo. Mauris posuere tortor vitae magna rhoncus a egestas lorem scelerisque.

Take a quiz!

// Last Modified: 2009-10-31 // // Only the purchaser may use and modify this script. // The author will customize this script for $25/hour with // a $10 minimum. // //////////////////////////////////////////////////////////// // // SET VARIABLES // // name of XML file which contains your quiz data $xml_file = "quiz.xml"; // QUESTION ORDER: // "file" for order of questions as they appear in "xml_file" above // "random" for random order of questions $question_order = "file"; // CHECK ANSWERS AFTER EACH QUESTION: // 0 for "no"; 1 for "yes" $check_answers_immediately = 1; // CASE-SENSITIVE ANSWERS: // 0 for "no"; 1 for "yes" $case_sensitive = 0; // PRINT EXPLANATIONS AFTER EACH QUESTION // "0" for no explanations // "1" for explanations only with incorrect answers // "2" for explanations with correct and incorrect answers $explanations = 2; // AUTOMATICALLY GENERATE MULTIPLE CHOICES FOR QUESTIONS: // 0 for "no"; 1 for "yes" $multiple_choice = 0; // number of choices to be generated (not including correct answer) $no_multiple_choices = 3; // USER REPORT: // "score_only" for score only // "corrected_quiz" for questions, multiple-choice answers, user's answer, correct answer, and explanation // "user_statistics" for user's answer, correct answer, and percentage of users who answered correctly $report_type = "corrected_quiz"; // PRINT IMAGES IN REPORT: // 0 for "no"; 1 for "yes" $show_images = 0; // PRINT LINKS TO URLS, AUDIO, AND VIDEO IN REPORT: // 0 for "no"; 1 for "yes" $media_links = 1; // // GET QUIZ DATA FROM XML FILE // // create new SimpleXML object from XML file $quiz = simplexml_load_file($xml_file); // // IMPORT POST VARIABLES // import_request_variables("p", "post_"); // // ERROR-CHECKING // // check "multiple_choice" setting for error if ($multiple_choice) { // get number of questions in quiz $no_questions_in_quiz = count($quiz); // "multiple_choice" setting will not work if there are not enough questions in the quiz if ($no_questions_in_quiz < $no_multiple_choices) { echo "

Error: You have configured xmlQuiz to generate $no_multiple_choices choices per question, but there are only $no_questions_in_quiz questions in the $xml_file. Lower the number of choices per question or increase the number of questions.

"; } } // // FUNCTIONS // // get number for random answer function get_random_answer($multiple_choices, $question_no_in_file) { global $quiz; // get a random number corresponding to "quiz" object $random_no = rand(0, count($quiz) - 1); // try again if this random number is repetitive or matches the correct answer if (in_array($quiz->question[$random_no]->answer, $multiple_choices) || $random_no == $question_no_in_file) { return get_random_answer($multiple_choices, $question_no_in_file); } // return random number else { return $random_no; } } // print question function print_question($question_no_in_quiz, $question_no_in_file) { global $quiz, $question_order, $post_answers, $multiple_choice, $no_multiple_choices; // print question number echo "

Question #$question_no_in_quiz of " . count($quiz) . "

\n"; // print hyperlink if ($quiz->question[$question_no_in_file]->url) { echo "

URL: question[$question_no_in_file]->url . "\">" . $quiz->question[$question_no_in_file]->url . "

\n"; } // print question image if ($quiz->question[$question_no_in_file]->image) { echo "

\"imagequestion[$question_no_in_file]->image . "\" />

\n"; } // print question audio if ($quiz->question[$question_no_in_file]->audio) { echo "

\n"; echo "question[$question_no_in_file]->audio . "\" />\n"; echo "

Listen to the audio file: question[$question_no_in_file]->audio . "\">" . $quiz->question[$question_no_in_file]->audio . "

.\n"; echo "

\n"; } // print question video if ($quiz->question[$question_no_in_file]->video) { echo "

question[$question_no_in_file]->video) . "\">\n"; echo "question[$question_no_in_file]->video) . "\" />\n"; echo "

Watch the video: question[$question_no_in_file]->video) . "\">" . htmlspecialchars($quiz->question[$question_no_in_file]->video) . "

.\n"; echo "

\n"; } // print question echo "

" . $quiz->question[$question_no_in_file]->text . "

\n"; // print answering form echo "
\n"; echo "
\n"; echo "\n"; echo "\n"; // print answers to previous questions as hidden form fields if (isset($post_answers)) { foreach ($post_answers as $key => $value) { echo "\n"; } } // print multiple choices if ($quiz->question[$question_no_in_file]->choices) { foreach ($quiz->question[$question_no_in_file]->choices->choice as $choice) { echo " " . $choice . "
\n"; } } // automatically generate multiple choices elseif ($multiple_choice) { // create "multiple_choices" array $multiple_choices = array(); // get required number of (incorrect) answers for ($n = 0; $n < $no_multiple_choices; $n++) { // get random answer $random_no = get_random_answer($multiple_choices, $question_no_in_file); // add to "multiple_choices" array $multiple_choices[$n] = $quiz->question[$random_no]->answer; } // add correct answer to array $multiple_choices[] = $quiz->question[$question_no_in_file]->answer; // print multiple choices for ($n = 0; $n <= $no_multiple_choices; $n++) { echo " " . $multiple_choices[$n] . "
\n"; } } // print text field else { echo "\n"; } // print submit button with appropriate label if ($question_no_in_quiz == count($quiz)) { echo "\n"; } else { echo "\n"; } echo "
\n"; echo "
\n"; } // check answer and print message function check_answer($answers, $question_no_in_file) { global $quiz, $explanations; // print message for correct answer if (($case_sensitive && ($answers[$question_no_in_file] == $quiz->question[$question_no_in_file]->answer)) || (!$case_sensitive && (strtolower($answers[$question_no_in_file]) == strtolower($quiz->question[$question_no_in_file]->answer)))) { echo "

Correct!"; // print explanation of correct answer if ($explanations == 2 && $quiz->question[$question_no_in_file]->explanation) { echo "

\n"; if ($quiz->question[$question_no_in_file]->answerimage) { echo "

\"answerquestion[$question_no_in_file]->answerimage . "\" />

\n " ; }; echo "

\n

" . $quiz->question[$question_no_in_file]->explanation; } echo "

\n"; } // print message for incorrect answer else { echo "

Sorry, the correct answer is " . $quiz->question[$question_no_in_file]->answer . "."; // print explanation of correct answer if ($explanations >= 1 && $quiz->question[$question_no_in_file]->explanation) { echo "

\n"; if ($quiz->question[$question_no_in_file]->answerimage) { echo "

\"answerquestion[$question_no_in_file]->answerimage . "\" />

\n " ; }; echo "

\n

" . $quiz->question[$question_no_in_file]->explanation; } echo "

\n"; } } if (!isset($post_next_question) && !isset($post_calculate_score)) { // print first question in file if ($question_order == "file") { print_question(1, 0); } // print random question if ($question_order == "random") { // create "questions" array for ($i = 0; $i < count($quiz); $i++) { $questions[$i + 1] = $i; } // get random question number $question_no = array_rand($questions); print_question(1, $questions[$question_no]); } } // // PRINT NEXT QUESTION // if (isset($post_next_question)) { // make "post_question_no_in_file" an integer settype($post_question_no_in_file, "integer"); // set answer of unanswered question to "Unanswered" if ($post_answers[$post_question_no_in_file] == "") { $post_answers[$post_question_no_in_file] = "Unanswered"; } // check previous question now if ($check_answers_immediately) { check_answer($post_answers, $post_question_no_in_file); } // increment "post_question_no_in_quiz" $post_question_no_in_quiz++; // print next question in file if ($question_order == "file") { print_question($post_question_no_in_quiz, $post_question_no_in_quiz - 1); } // print another random question if ($question_order == "random") { // create "questions" array from which a random question will be selected for ($i = 0; $i < count($quiz); $i++) { $questions[$i + 1] = $i; } // get keys from "post_answers" array $answered_questions = array_keys($post_answers); // remove answered questions from "questions" array for ($i = 0; $i < count($answered_questions); $i++) { // look for an element containing this question in "questions" array $key = array_search($answered_questions[$i], $questions); // remove this element from "questions" array if ($key != null) { unset($questions[$key]); } } // get random question number $question_no = array_rand($questions); print_question($post_question_no_in_quiz, $questions[$question_no]); } } // // CALCULATE AND PRINT SCORE // if (isset($post_calculate_score)) { // make "post_question_no_in_file" an integer settype($post_question_no_in_file, "integer"); // set answer of unanswered questions to "Unanswered" if ($post_answers[$post_question_no_in_file] == "") { $post_answers[$post_question_no_in_file] = "Unanswered"; } // check last question now if ($check_answers_immediately) { check_answer($post_answers, $post_question_no_in_file); } // get number of questions in quiz $no_questions = count($quiz); // get number of correct answers and adjust tally in "xml_file" for ($i = 0; $i < $no_questions; $i++) { // increment "no_correct_answers" variable if user has correct answer if (($case_sensitive && ($post_answers[$i] == $quiz->question[$i]->answer)) || (!$case_sensitive && (strtolower($post_answers[$i]) == strtolower($quiz->question[$i]->answer)))) { $no_correct_answers++; // increment correct tally $quiz->question[$i]->tally->correct = $quiz->question[$i]->tally->correct + 1; } // increment incorrect tally else { $quiz->question[$i]->tally->incorrect = $quiz->question[$i]->tally->incorrect + 1; } } // update XML file with new tallies $fp_xml_file = fopen($xml_file, "w"); fwrite($fp_xml_file, $quiz->asXML()); fclose($fp_xml_file); // calculate score $score = ($no_correct_answers / $no_questions) * 100; // round score to nearest whole precentage point $score = round($score); // print score echo "

Score: $score%

\n"; // print appropriate comment if ($no_correct_answers == 0) { echo "

You answered no questions correctly. Try again.

\n"; } if ($no_correct_answers == 1) { echo "

You answered 1 out of $no_questions questions correctly. Try again.

\n"; } if ($no_correct_answers > 1 && $no_correct_answers < $no_questions) { echo "

You answered $no_correct_answers out of $no_questions questions correctly. Try again.

\n"; } if ($no_correct_answers == $no_questions) { echo "

You answered all questions correctly!

\n"; } // // PRINT CORRECTED QUIZ // if ($report_type == "corrected_quiz") { echo "

Corrected Quiz

\n"; echo "
    \n"; // print each question with multiple-choice answers for ($n = 0; $n < count($post_answers); $n++) { echo "
  1. \n"; echo "

    " . $quiz->question[$n]->text . "

    \n"; // print correct answer with image if ($show_images && $quiz->question[$n]->image) { echo "

    \"imagequestion[$n]->image . "\" />

    \n"; } // print question with media links elseif ($media_links) { if ($quiz->question[$n]->url) { echo "

    URL: question[$n]->url) . "\">" . htmlspecialchars($quiz->question[$n]->url) . "

    \n"; } if ($quiz->question[$n]->audio) { echo "

    Audio: question[$n]->audio) . "\">" . htmlspecialchars($quiz->question[$n]->audio) . "

    \n"; } if ($quiz->question[$n]->video) { echo "

    Video: question[$n]->video) . "\">" . htmlspecialchars($quiz->question[$n]->video) . "

    \n"; } } // print multiple-choice question if ($quiz->question[$n]->choices) { echo "
      \n"; // evaluate each multiple-choice answer foreach ($quiz->question[$n]->choices->choice as $choice) { // highlight correct answer if ($choice == (string)$quiz->question[$n]->answer) { echo "
    1. " . $choice . "
    2. \n"; } // highlight user's answer elseif ($choice == $post_answers[$n]) { echo "
    3. " . $choice . "
    4. \n"; } else { echo "
    5. " . $choice . "
    6. \n"; } } echo "
    \n"; // mark answer as correct if (($case_sensitive && ($post_answers[$n] == $quiz->question[$n]->answer)) || (!$case_sensitive && (strtolower($post_answers[$n]) == strtolower($quiz->question[$n]->answer)))) { echo "

    Correct!

    \n"; } // mark question as unanswered elseif ($post_answers[$n] == "Unanswered") { echo "

    Unanswered.

    \n"; } // mark answer as incorrect else { echo "

    Incorrect!"; // print explanation if ($quiz->question[$n]->explanation) { echo "

    " . $quiz->question[$n]->explanation; } echo "

    \n"; } } // print short-answer question else { // mark question as correct if ($post_answers[$n] == $quiz->question[$n]->answer) { echo "

    " . $post_answers[$n] . "

    \n"; echo "

    Correct!

    \n"; } // mark question as unanswered elseif ($post_answers[$n] == "Unanswered") { echo "

    Unanswered. The correct answer is " . $quiz->question[$n]->answer . "."; // print explanation if ($quiz->question[$n]->explanation) { echo "

    " . $quiz->question[$n]->explanation; } echo "

    \n"; } // mark question as incorrect else { echo "

    " . $post_answers[$n] . "

    \n"; echo "

    Incorrect! The correct answer is " . $quiz->question[$n]->answer . "."; // print explanation if ($quiz->question[$n]->explanation) { echo "

    " . $quiz->question[$n]->explanation; } echo "

    \n"; } } echo "
  2. \n"; } echo "
\n"; } // // PRINT USER STATISTICS // if ($report_type == "user_statistics") { // print questions with user's answers and correct answers echo "\n"; echo "\n"; echo "\n"; echo "\t\n"; echo "\t\n"; echo "\t\n"; echo "\t\n"; echo "\n"; // get total number of answers $total_answers = $quiz->question[0]->tally->correct + $quiz->question[0]->tally->incorrect; // print each question in a new table row for ($n = 0; $n < count($post_answers); $n++) { echo "\n"; // print question with image if ($show_images && $quiz->question[$n]->image) { echo "\t\n"; } // print question with media links elseif ($media_links) { echo "\t\n"; } // print question without image else { echo "\t\n"; } // print user's answer if (($case_sensitive && ($post_answers[$n] == $quiz->question[$n]->answer)) || (!$case_sensitive && (strtolower($post_answers[$n]) == strtolower($quiz->question[$n]->answer)))) { echo "\t\n"; } else { echo "\t\n"; } // print correct answer echo "\t\n"; // get percent correct if ($quiz->question[$n]->tally->correct > 0) { $percent_correct = ($quiz->question[$n]->tally->correct / $total_answers) * 100; // round percent correct to nearest whole precentage point $percent_correct = round($percent_correct); } else { $percent_correct = 0; } // print percent correct echo "\t\n"; echo "\n"; } echo "
User Statistics
QuestionYour AnswerCorrect Answer% who Answered this Question Correctly
\n"; echo "

" . $quiz->question[$n]->text . "

\n"; echo "

\"imagequestion[$n]->image . "\" />

\n"; echo "
\n"; echo "

" . $quiz->question[$n]->text . "

\n"; if ($quiz->question[$n]->url) { echo "

URL: question[$n]->url) . "\">" . htmlspecialchars($quiz->question[$n]->url) . "

\n"; } if ($quiz->question[$n]->audio) { echo "

Audio: question[$n]->audio) . "\">" . htmlspecialchars($quiz->question[$n]->audio) . "

\n"; } if ($quiz->question[$n]->video) { echo "

Video: question[$n]->video) . "\">" . htmlspecialchars($quiz->question[$n]->video) . "

\n"; } echo "
" . $quiz->question[$n]->text . "" . $post_answers[$n] . "" . $post_answers[$n] . "" . $quiz->question[$n]->answer . "" . $percent_correct . "%
\n"; } } ?>