I wrote a script something like this, though not exactly what you want (but not too hard to change it) using PHP and text files.
Take a look here:
http://ci-pro.com/lang/german
It has a list of terms/translations and randomly picks from the list you choose and continues through the end of the list, generating the wrong answers from similar terms (close in the list).
Here's the source code. Sorry it's kinda messy. This was a rough draft and I'm going to make it dymamic (multiple lists, multiple languages) soon, but the concept is sound.
I have a comment for every action that happens in the code, so the code looks VERY busy, but don't let that scare you off-- comments will definitely help. Clearly, you can remove them for the final code. Just an explanation I wrote up.
PHP Code:
<?php
$lang = 'German'; //set the language [purely aesthetic in this case]
ini_set('register_globals','Off'); //be sure that register globals isn't being stupid
session_start(); //start/continue the session
if (isset($_GET['invert'])) { //invert answers?
if (isset($_SESSION['invert'])) { //check if it's set already to invert
unset($_SESSION['invert']); //if so, remove setting, back to normal
}
else { //if not,
$_SESSION['invert']=1; //ok, set to invert
}
}
function getwords($file) { //main function for getting the trivia data, returns array
$words = explode("\n",file_get_contents($file)); //grab each line of the file as an array
foreach ($words as $l) { //for each array item
$l = trim($l); //trim them to get rid of any junk
if ($l[0].$l[1]=='//') { //if it starts with a title (in the form of a comment, like //title)
$cat = substr($l,2); //set the category as this line starting after //
$n=0; //the number in the category array set to 0
}
else if (substr_count($l,'|')===1&&isset($cat)) { //if there is a | in it (splitter) and the category is set
$l = explode('|',$l,2); //split the answer at | and save as l
$out[$cat][$n] = isset($_SESSION['invert'])?array($l[1],$l[0]):array($l[0],$l[1]);
//based on if invert is set or not, save as the output variable's $cat array's $nth item, term|answer or answer|term
$n++; //add to the number in the category
}
}
return $out; //return the final array
}
echo '<html><head><title>Vocabulary Trivia</title></head><body>'; //start output for all versions
if (!isset($_GET['main'])&&(isset($_GET['a'])||isset($_GET['cat']))) { //if the main link was NOT click and an answer or category has been sent via get
if (isset($_GET['cat'])) { //a category was picked, setup triva, etc.
$_SESSION['cat'] = $_GET['cat']; //set the category from the request to the session
$ger1 = getwords('list1.php'); //get the array of questions
$_SESSION['qs'] = $ger1[$_SESSION['cat']]; //set the questions array in session
$_SESSION['qsb'] = $ger1[$_SESSION['cat']]; //set the backup (non-deleting) questions array for making the false answers
}
if (isset($_SESSION['cat'])) { //if the category is set in the session
if (!isset($_SESSION['a'])||!isset($_GET['a'])) { //nothing has been sent, so it must be a new round
$msg = 'Welcome to trivia. Pick the answer that matches the term.'; //welcome/default message
}
else { //ok, answer has been sent--
@$_SESSION['scoren']++; //add to session variable for number of questions answered
if ($_SESSION['a']==$_GET['a']) { //if the sent answer is the same as the answer stored
$msg = 'Correct. <i>'.$_SESSION['q'].'</i> is <i>'.$_SESSION['a'].'</i>.'."\n";
//yes, confirm answer
@$_SESSION['scorec']++; //add to session variable for correct answers
}
else {
$msg = 'No. The correct answer for <i>'.$_SESSION['q'].'</i> is <i>'.$_SESSION['a'].'</i>.'."\n";
//no, and tell answer
}
}
echo $msg.'<br>'."\n"; //echo message
if (isset($_SESSION['scoren'],$_SESSION['scorec'])) { //if the score values are set
echo '<br>You have answered '.$_SESSION['scorec'].'/'.$_SESSION['scoren'].' questions correctly. (Of '.count($_SESSION['qsb']).' total questions.)<br>'."\n"; //show score/status
}
if (!empty($_SESSION['qs'])) { //if there are questions left
$n = mt_rand(0,count($_SESSION['qs'])-1); //get a random number for the question based on the number of questions left
$s = $_SESSION['qs'][$n]; //the answer set (s) is the random number nth item in the array questions
unset ($_SESSION['qs'][$n]); //remove used question from list
sort($_SESSION['qs']); //reorder questions to get rid of any blanks left from removing question
$q = $s[0]; //question is first part of that array
$a = $s[1]; //answer is the second part of that array
echo '<h1>'.$q.'</h1>'; //echo [big] the term/question
$i=3; //remaining answers -- 3 + real answer = 4 total
$as[0]=$a; //set real answer in answers array
$_SESSION['a']=$a; //set answer in session var
$_SESSION['q']=$q; //set question in session var
$max=0; //set the max iterations for the while loop
while ($i>0) { //while the 4 answers aren't yet set
$r = $_SESSION['qsb'][mt_rand(0,count($_SESSION['qsb'])-1)]; //get random answer from question backup array
if (!in_array($r[1],$as)&&!isset($as[$r[0]])) { //if neither the answer nor the term are already in the array
$as[$i]=$r[1]; //set the answer in answers array
$i--; //number of answers remaining goes down
$max++; //add to max counter
if ($max>100) { $i=0; } //force to end if 100 answers doesn't give 4 results
}
}
shuffle($as); //mix up the answers
echo '<div id="answers">'; //start answers div
echo '<form action="?" method="get">'; //start answer form
foreach ($as as $ta) { //for each answer
echo '<input type="submit" name="a" value="'.$ta.'">'; //echo answer button
}
echo '</form></div><br><br>'; //end form/div/etc
echo '<a href="?main">Main</a>'; //main/cancel link
echo '</body></html>'; //end html
die(); //end output (don't show main page below)
}
else { //no more questions
echo 'You have finished all the questions in this category.<br><br>'; //echo text
unset($_SESSION['scoren'],$_SESSION['scorec']); //unset score
}
}
}
unset($_SESSION['qs'],$_SESSION['qs2'],$_SESSION['a'],$_SESSION['cat'],$_SESSION['scoren'],$_SESSION['scorec']);
//unset any quiz variables remaining
$ger1 = getwords('list1.php'); //use getwords (see above) to set questions
echo 'Choose a category below to start:'."\n"; //echo text
foreach ($ger1 as $cat=>$terms) { //foreach set within the text file
echo '<br><a href="?cat='.$cat.'">'.$cat.'</a>'."\n"; //for each category, output the link to that set of questions
}
echo '<br><br><a href="?invert">Switch to '.(isset($_SESSION['invert'])?$lang.' to English':'English to '.$lang).'</a>';
//echo the switch lang>eng or eng>lang link, based on the current settings
echo '</body></html>'; //end html out
?>
And for some example answers:
Code:
//numbers
1|one
2|two
3|three
//questions
True or false?|true
Yes or no?|yes
Is that correct?|no
//German
Hallo|Hello
Guten Tag|Good day
Deutsch|German
The format is quite rigid at the moment. Be sure you have a category set (with the //category marker) BEFORE any lines. Then put each answer/question pair on a single line without any spacing/etc., as question|answer (or inverted, depending on how you end up working with it).
Some customization is certainly possible.
Bookmarks