The PHP part selects HTML files and places them in the select group.. This code fragment uses the preg_grep to select filenames ending in HTMLand creates an optiontag for that file.. Sho
Trang 1</tr>
</table>
</form>
HERE;
} // end showTest
Although the code is long, almost all of it is pure HTML The PHP part selects HTML files and places them in the select group This code fragment uses the preg_grep()
to select filenames ending in HTMLand creates an optiontag for that file
Note that I stripped out the .htmlpart of the filename because I won’t need it
It would complicate some of the code coming up in the takeQuizprogram.
Showing the Edit List
The showEdit()function works a lot like showTest(), listing the system’s master files Although it is often exactly the same as the list of tests, it won’t always be the same; some master files may not have been made into HTML files.
function showEdit(){
// let user select a master file to edit
global $theFiles;
//get only quiz master files
$testFiles = preg_grep(“/mas$/”, $theFiles);
//edit a quiz
print <<<HERE
<form action = “editQuiz.php”
method = “post”>
<table border = 1
width = 400>
<tr>
<td colspan = 2><center>
<h3>Edit a quiz</h3>
</center></td>
</tr>
g r
s o
l u
g in
e r
Trang 2<td>Administrative Password</td>
<td>
<input type = “password”
name = “password”
value = “”>
</td>
</tr>
<tr>
<td>Quiz</td>
<td>
<select name = “editFile”>
<option value = “new”>new quiz</option>
HERE;
foreach ($testFiles as $myFile){
$fileBase = substr($myFile, 0, strlen($myFile) - 4);
print “ <option value = $myFile>$fileBase</option>\n”;
} // end foreach
print <<<HERE
</select>
</td>
</tr>
<tr>
<td colspan = 2><center>
<input type = “submit”
value = “go”>
</center></td>
</tr>
</table>
</form>
HERE;
} // end showEdit
209
Trang 3The showEdit() function is just like showQuiz() but the form points to the editQuiz.phpprogram and the file list is based on those files ending in mas There’s one other subtle but important difference: Look at the code for the select element and see a new quizoption If the user chooses this option, the editQuiz() function won’t try to load a quiz file into memory, but sets up for a new quiz instead.
Showing the Log List
The last segment is meant for the quiz administrator It allows the user with administrator access to view the log of any system quiz This log shows who has taken the test, where and when she took it, and her score When the user clicks the submitbutton associated with this part of the page, the showLog.phpprogram takes over.
function showLog(){
//let user choose from a list of log files
global $theFiles;
print <<<HERE
<form action = “showLog.php”
method = “post”>
<table border = 1
width = 400>
<tr>
<td colspan = 2><center>
<h3>Show a log file</h3>
</td>
</tr>
<tr>
<td>Administrative Password</td>
<td>
<input type = “password”
name = “password”
value = “”>
</td>
</tr>
g r
s o
l u
g in
e r
Trang 4<tr>
<td>Quiz</td>
<td>
<select name = “logFile”>
HERE;
//select only log files
$logFiles = preg_grep(“/log$/”, $theFiles);
foreach ($logFiles as $myFile){
$fileBase = substr($myFile, 0, strlen($myFile) - 4);
print “ <option value = $myFile>$fileBase</option>\n”;
} // end foreach
print <<<HERE
</select>
</td>
</tr>
<tr>
<td colspan = 2><center>
<input type = “submit”
value = “go”>
</td>
</tr>
</table>
</form>
HERE;
} // end showLog
?>
</center>
</body>
</html>
I decided that all log files would end with .log, so the program can easily get a
list of log files to place in the select group.
Trang 5g r
s o
l u
g in
e r
Editing a Test
For simplicity’s sake I decided on a very simple test format The first three lines
of the test file contain the test’s name, the instructor’s e-mail address, and the test’s password The test data itself follows Each problem takes up one line (although it can wrap freely—a line is defined by a carriage return character) The problem has a question followed by four possible answers and the correct answer.
A colon separates each element.
The editQuiz.phpprogram assists the user in creating and editing quizzes It’s a simple program, because the real work happens after the user edits and presses the submitbutton.
Getting Existing Test Data
The first chore of the editQuizprogram is determining which quiz the user is requesting Remember that the value newindicates that the user wants to build
a new test; that value is treated specially Any other value is the foundation of a test filename, so I open the appropriate master file and load its values into the appropriate form elements:
<html>
<head>
<title>Quiz Builder</title>
</head>
<body>
<?
if ($password != “absolute”){
print <<<HERE
<font color = “red” size = +3>
IN THE REAL WORLD
You think question formatting has too many rules? I agree This is a limitation
of the sequential-file access technique that’s storing the data In chapters 8-12, you learn ways that aren’t quite so picky However, this is a relatively easy way
to store your data, so I wrote the program to assist the process as much as is practical You generally want to write your program so the user never has to know the underlying data structure.