Extension:Quizzes/Quiz.php

The script is part of the Quizzes extension, go to Quizzes for full details on how to install this extension.

config/LocalSettings.php to the parent directory.\n" ); } else { die( "You'll have to set the wiki up first!" ); } }

define( 'MEDIAWIKI', true );
 * 1) Valid web server entry point, enable includes.
 * 2) Please don't move this line to includes/Defines.php. This line essentially defines
 * 3) a valid entry point. If you put it in includes/Defines.php, then any script that includes
 * 4) it becomes an entry point, thereby defeating its purpose.

require_once( '../includes/Defines.php' ); require_once( '../LocalSettings.php' ); require_once( '../includes/Setup.php' );

wfProfileIn( 'main-misc-setup' ); OutputPage::setEncodings; # Not really used yet


 * 1) Debug statement for user levels
 * 2) print_r($wgUser);

if ( !is_null( $wgTitle ) && !$wgTitle->userCanRead ) { $wgOut->loginToUse; $wgOut->output; exit; }
 * 1) If the user is not logged in, the Namespace:title of the article must be in
 * 2) the Read array in order for the user to see it. (We have to check here to
 * 3) catch special pages etc. We check again in Article::view)

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  // 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 .= "'''". $questions[0]['text']. "'''\n"; $text .= "\n";

// split choices into "choices" array $choices = explode(", ", $questions[0]['choices']);

// print text field if there are no choices if (count($choices) == 1) { $text .= "\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 .= " ". $choices[$i]. " \n"; }       }

$text .= "\n"; $text .= " \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 .= "'''". $questions[$nextQuestion]['text']. "'''\n"; $text .= "\n";

// print answers to previous questions as hidden form fields for ($i = 0; $i < count($post_answers); $i++) { $text .= "\n"; }

// split choices into "choices" array $choices = explode(", ", $questions[$nextQuestion]['choices']);

// print text field if there are no choices if (count($choices) == 1) { $text .= "\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 .= "". $choices[$i]. " \n"; }       }

// print appropriate button label if (count($questions) == count($post_answers) + 1) { $text .= "\n"; }       else { $text .= "\n"; }

$text .= " \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 .= "\n== Quiz Results ==\n"; $wtext .= "=== You have scored $score% on this quiz.===\n";

if ($noCorrectAnswers == 0) { $text .= " You answered no questions correctly. Try again.</a> "; $text .= " Return to the <a href=\"/wiki/index.php\">Main Page</a> "; }

if ($noCorrectAnswers == 1) { $text .= " You answered 1 out of $noQuestions questions correctly. <a href=". $PHP_SELF. ">Try again.</a> "; $text .= " Return to the <a href=\"/wiki/index.php\">Main Page</a> "; }

if ($noCorrectAnswers > 1 && $noCorrectAnswers < $noQuestions) { $text .= " You answered $noCorrectAnswers out of $noQuestions questions correctly. <a href=". $PHP_SELF. ">Try again.</a> "; $text .= " Return to the <a href=\"/wiki/index.php\">Main Page</a> "; }

if ($noCorrectAnswers == $noQuestions) { $text .= " You answered all questions correctly! "; $text .= " Return to the <a href=\"/wiki/index.php\">Main Page</a> ";

}

for ($i = 0; $i < $noQuestions; $i++) {

// print question $wtext .= "== Question ". ($i + 1). " ==\n\n"; $wtext .= "'''". $questions[$i]['text']. "'''\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 .= "'''". $post_answers[$i]. " CORRECT''' \n"; } else { $wtext .= "'''". $post_answers[$i]. " INCORRECT''' \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 .= " ''' ". $choices[$x]. " CORRECT''' \n"; } else { if ($post_answers[$i] == $choices[$x]) { $wtext .= " ''' ". $choices[$x]. " INCORRECT''' \n"; } else { $wtext .= " ". $choices[$x]. " \n"; }

}       }                $wtext .= "</ul>\n"; } }

}

$wgOut->setPageTitle($wTitle);

$wgOut->addWikiText( " \n" ); $wgOut->addWikiText( $wtext ); $wgOut->addWikiText( " \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