Extension:Quizzes/Quiz.php
From MediaWiki.org
The script is part of the Quizzes extension, go to Extension:Quizzes for full details on how to install this extension.
<?php #apd_set_pprof_trace(); # Main wiki script; see design.doc # $wgRequestTime = microtime(); unset( $IP ); @ini_set( 'allow_url_fopen', 0 ); # For security... if( !file_exists( '../LocalSettings.php' ) ) { if ( file_exists( '../config/LocalSettings.php' ) ) { die( "To complete the installation, move <tt>config/LocalSettings.php</tt> to the parent directory.\n" ); } else { die( "You'll have to <a href='config/index.php'>set the wiki up</a> first!" ); } } # Valid web server entry point, enable includes. # Please don't move this line to includes/Defines.php. This line essentially defines # a valid entry point. If you put it in includes/Defines.php, then any script that includes # it becomes an entry point, thereby defeating its purpose. define( 'MEDIAWIKI', true ); require_once( '../includes/Defines.php' ); require_once( '../LocalSettings.php' ); require_once( '../includes/Setup.php' ); wfProfileIn( 'main-misc-setup' ); OutputPage::setEncodings(); # Not really used yet # Debug statement for user levels # print_r($wgUser); # If the user is not logged in, the Namespace:title of the article must be in # the Read array in order for the user to see it. (We have to check here to # catch special pages etc. We check again in Article::view()) if ( !is_null( $wgTitle ) && !$wgTitle->userCanRead() ) { $wgOut->loginToUse(); $wgOut->output(); exit; } if ($wgUser->getID() != 0) { $wgOut->setArticleFlag( false ); $wgTitle = Title::makeTitle( NS_SPECIAL, "Custom" ); $wgOut->setArticleRelated( false ); $wgOut->setRobotPolicy( "noindex,follow" ); //////////////////////////////////////////////////////////// // // xmlQuiz v1.1 - a simple quiz script // //////////////////////////////////////////////////////////// // // This script allows you to quiz users on any number of // questions and calculate the score. // // See readme.txt for more information. // // Author: Jon Thomas <http://www.fromthedesk.com/> // Last Modified: 12/18/2005 // // You may freely use, modify, and distribute this script. // //////////////////////////////////////////////////////////// // // SET VARIABLES // // name of XML file which contains your quiz data $xmlFile = "Quiz.xml"; // // GET QUIZ DATA // // get XML data $data = implode("", file($xmlFile)); // create XML parser $parser = xml_parser_create(); // set parser options xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); // parse XML data into arrays xml_parse_into_struct($parser, $data, $values, $tags); // free parser xml_parser_free($parser); // // STRUCTURE XML DATA INTO ARRAY // // set counter variable for to-be-created questions array $questionNo = 0; // cycle through parsed XML data to look for text and answer tags foreach ($values as $key=>$val) { // a TITLE tag if ($val[tag] == "TITLE") { $wTitle = $val[value]; } // save value to "questions" array if this is a TEXT tag if ($val[tag] == "TEXT") { $questions[$questionNo]['text'] = $val[value]; } // save value to "questions" array if this is a CHOICES tag if ($val[tag] == "CHOICES") { $questions[$questionNo]['choices'] = $val[value]; } // save value to "questions" array if this is an ANSWER tag if ($val[tag] == "ANSWER") { $questions[$questionNo]['answer'] = $val[value]; // increment question counter variable $questionNo++; } } // // IMPORT POST VARIABLES // import_request_variables("p", "post_"); // // PRINT FIRST QUESTION // if (!isset($post_answers)) { $wtext .= "== Question 1 of " . ($questionNo) . ": ==\n\n"; $wtext .= "<b>" . $questions[0]['text'] . "</b>\n"; $text .= "<form action=\"$PHP_SELF\" method=\"post\">\n"; // split choices into "choices" array $choices = explode(", ", $questions[0]['choices']); // print text field if there are no choices if (count($choices) == 1) { $text .= "<input type=\"text\" name=\"answers[0]\" size=10>\n"; } // print radio fields if there are multiple choices else { // print a radio button for each choice for ($i = 0; $i < count($choices); $i++) { $text .= "<input type=\"radio\" name=\"answers[0]\" value=\"" . $choices[$i] . "\"> " . $choices[$i] . "<br>\n"; } } $text .= "<input type=\"submit\" value=\"Next Question\">\n"; $text .= "</form>\n"; } // // PRINT NEXT QUESTION // elseif (count($questions) > count($post_answers)) { // get number of next question $nextQuestion = count($post_answers); // print question $wtext .= "== Question " . ($nextQuestion + 1) . " of " . ($questionNo) . " ==\n\n"; $wtext .= "<b>" . $questions[$nextQuestion]['text'] . "</b>\n"; $text .= "<form action=\"$PHP_SELF\" method=\"post\">\n"; // print answers to previous questions as hidden form fields for ($i = 0; $i < count($post_answers); $i++) { $text .= "<input type=\"hidden\" name=\"answers[$i]\" value=\"$post_answers[$i]\">\n"; } // split choices into "choices" array $choices = explode(", ", $questions[$nextQuestion]['choices']); // print text field if there are no choices if (count($choices) == 1) { $text .= "<input type=\"text\" name=\"answers[$nextQuestion]\" size=10>\n"; } // print radio fields if there are multiple choices else { // print a radio button for each choice for ($i = 0; $i < count($choices); $i++) { $text .= "<input type=\"radio\" name=\"answers[$nextQuestion]\" value=\"" . $choices[$i] . "\">" . $choices[$i] . "<br>\n"; } } // print appropriate button label if (count($questions) == count($post_answers) + 1) { $text .= "<input type=\"submit\" value=\"Calculate Score\">\n"; } else { $text .= "<input type=\"submit\" value=\"Next Question\">\n"; } $text .= "</form>\n"; } // // CALCULATE AND PRINT SCORE // else { // get number of questions $noQuestions = count($questions); // get number of correct answers for ($i = 0; $i < $noQuestions; $i++) { // increment "noCorrectAnswers" variable if user has correct answer if ($questions[$i]['answer'] == $post_answers[$i]) { $noCorrectAnswers++; } } // calculate score $score = ($noCorrectAnswers / $noQuestions) * 100; // round score to nearest whole precentage point $score = round($score); // print score $wtext .= "__NOTOC__\n== Quiz Results ==\n"; $wtext .= "=== You have scored $score% on this quiz.===\n"; if ($noCorrectAnswers == 0) { $text .= "<p>You answered no questions correctly. <a href=" . $PHP_SELF . ">Try again.</a></p>"; $text .= "<p>Return to the <a href=\"/wiki/index.php\">Main Page</a></p>"; } if ($noCorrectAnswers == 1) { $text .= "<p>You answered 1 out of $noQuestions questions correctly. <a href=" . $PHP_SELF . ">Try again.</a></p>"; $text .= "<p>Return to the <a href=\"/wiki/index.php\">Main Page</a></p>"; } if ($noCorrectAnswers > 1 && $noCorrectAnswers < $noQuestions) { $text .= "<p>You answered $noCorrectAnswers out of $noQuestions questions correctly. <a href=" . $PHP_SELF . ">Try again.</a></p>"; $text .= "<p>Return to the <a href=\"/wiki/index.php\">Main Page</a></p>"; } if ($noCorrectAnswers == $noQuestions) { $text .= "<p>You answered all questions correctly!</p>"; $text .= "<p>Return to the <a href=\"/wiki/index.php\">Main Page</a></p>"; } for ($i = 0; $i < $noQuestions; $i++) { // print question $wtext .= "== Question " . ($i + 1) . " ==\n\n"; $wtext .= "<b>" . $questions[$i]['text'] . "</b>\n"; // split choices into "choices" array $choices = explode(", ", $questions[$i]['choices']); // print radio fields if there are multiple choices // print text field if there are no choices if (count($choices) == 1) { if ($post_answers[$i] == $questions[$i]['answer']) { $wtext .= "<b>" . $post_answers[$i]. "</b> <font color=blue><b>CORRECT</b></font><br>\n"; } else { $wtext .= "<b>" . $post_answers[$i]. "</b> <font color=red><b>INCORRECT</b></font><br>\n"; } } else { $wtext .= "\n"; // print a radio button for each choice for ($x = 0; $x < count($choices); $x++) { if (($questions[$i]['answer'] == $post_answers[$i]) && ($post_answers[$i] == $choices[$x])) { $wtext .= " <b> " . $choices[$x] . "</b> <font color=blue><b>CORRECT</b></font><br>\n"; } else { if ($post_answers[$i] == $choices[$x]) { $wtext .= " <b> " . $choices[$x] . "</b> <font color=red><b>INCORRECT</b></font><br>\n"; } else { $wtext .= " " . $choices[$x] . "<br>\n"; } } } $wtext .= "</ul>\n"; } } } $wgOut->setPageTitle($wTitle); $wgOut->addWikiText( "<br clear=all>\n" ); $wgOut->addWikiText( $wtext ); $wgOut->addWikiText( "<br clear=all>\n" ); $wgOut->addHTML( $text ); $wgOut->output(); exit; } else { $wgOut->loginToUse(); $wgOut->output(); exit; }
by Smcnaught (04:35, 23 June 2006 (UTC)) - I am also available on irc.chekmate.org #MediaWiki
