Taking a Quiz The point of all this work is to have a set of quizzes your users can take, so it’s good to have a program to present the quizzes.. Actually, since the quizzes are saved as
Trang 1function buildHTML(){
global $quizName, $quizData;
$htData = <<<HERE
<html>
<head>
<title>$quizName</title>
</head>
<body>
HERE;
//get the quiz data
$problems = split(“\n”, $quizData);
$htData = <<<HERE
<center>
<h1>$quizName</h1>
</center>
<form action = “gradeQuiz.php”
method = “post”>
Name
<input type = “text”
name = “student”>
<ol>
HERE;
$questionNumber = 1;
foreach ($problems as $currentProblem){
list($question, $answerA, $answerB, $answerC, $answerD, $correct) = split (“:”, $currentProblem);
$htData = <<<HERE
<li>
$question
<ol type = “A”>
<li>
<input type = “radio”
name = “quest[$questionNumber]”
218
g r
s o
l u
g in
e r
Trang 2value = “A”>
$answerA
</li>
<li>
<input type = “radio”
name = “quest[$questionNumber]”
value = “B”>
$answerB
</li>
<li>
<input type = “radio”
name = “quest[$questionNumber]”
value = “C”>
$answerC
</li>
<li>
<input type = “radio”
name = “quest[$questionNumber]”
value = “D”>
$answerD
</li>
</ol>
</li>
HERE;
$questionNumber++;
} // end foreach
$htData = <<<HERE
</ol>
<input type = “hidden”
name = “quizName”
value = “$quizName”>
<input type = “submit”
value = “submit quiz”>
219
Trang 3HERE;
print $htData;
return $htData;
} // end buildHTML
?>
</body>
</html>
problem contains a list of values, which is separated into a series of scalar
question’s information Take a careful look at the code for the radio buttons Recall from your HTML experience or Appendix A that radio buttons that operate
as a group should all have the same name I did this by calling them all
quest[$questionNumber] The $questionNumbervariable contains the current ques-tion number, and this value is interpolated before the HTML code is written
button
Taking a Quiz
The point of all this work is to have a set of quizzes your users can take, so it’s good to have a program to present the quizzes Actually, since the quizzes are saved as HTML pages, you could simply provide a link to a quiz and be done with
it, but I wanted a little more security I wanted the ability to store my quiz files
so people don’t take a quiz until I know it’s ready (I don’t release the password until I’m ready for people to take the quiz.) Also, I can easily turn “off” a quiz by simply changing its password
The takeQuizpage’s only real job is to check the user’s password against the indi-cated test’s password and allow access to the quiz if appropriate
220
g r
s o
l u
g in
e r
Trang 4//takeQuiz.php
//given a quiz file, prints out that quiz
//get the password from the file
$masterFile = $takeFile “.mas”;
$fp = fopen($masterFile, “r”);
//the password is the third line, so get the first two lines, but ignore them
$dummy = fgets($fp);
$dummy = fgets($fp);
$magicWord = fgets($fp);
$magicWord = rtrim($magicWord);
fclose($fp);
if ($password == $magicWord){
$htmlFile = $takeFile “.html”;
//print out the page if the user got the password right
readFile($htmlFile);
} else {
print <<<HERE
<font color = “red”
size = +3>
Incorrect Password.<br>
You must have a password in order to take this quiz
</font>
HERE;
} // end if
?>
The password associated with a test is stored in the test file, so once I know which
test the user wants to take, I can open that file and extract the password The
password is stored in the file’s third line The only way to get to it with a
sequen-tial access file is to load the first two lines into a dummy variable and then load
the quiz HTML page to the browser If not, I send a message indicating the
pass-word was incorrect
221
Trang 5This is a dandy place to set up a little more security I keep a log file of every access in a production version of this system, so I know if somebody has been try-ing to get at my system 1,000 times from the same machine within a second (a sure sign of some kind of automated attack) or other mischief I can also check
to see that later on when a page has been submitted, it comes from the same com-puter that requested the file in the first place I can also check the request and submission times to reject quizzes that have been out longer than my time limit.
Grading the Quiz
One advantage of this kind of system is the potential for instantaneous feedback
pro-gram automatically grades the quiz and stores a results log for the administrator
Opening the Files
The gradeQuizprogram, like all the programs in this system, relies on files to do all its important work In this case, the program uses the master file to get the answer key for the quiz and writes to a log file
<?
print <<<HERE
<html>
<head>
<title>Grade for $quizName, $student</title>
</head>
<body>
</body>
<h1>Grade for $quizName, $student</h1>
HERE;
//open up the correct master file for reading
$fileBase = str_replace(“ “, “_”, $quizName);
$masFile = $fileBase “.mas”;
$msfp = fopen($masFile, “r”);
$logFile = $fileBase “.log”;
T R I C K
222
g r
s o
l u
g in
e r