Please enter your name.";
}
if ($_POST['email'] == "") {
$errors['email']="Please enter your email.";
}
}
function display_errors() {
global $errors;
foreach ($errors as $values) {
echo $values, " ";
}
}
// Selecting the assignments
function display_assignment() {
echo <<\n
When you are ready, you click on your name below to start the midterm. The clock starts ticking. A timestamp will be recorded. When you submit the test, another timestamp will be recorded. This is to make sure that you do the test within 2.5 hours.
HTML;
} //Selecting the assignments
// Get the data and display the form
function display_form() {
// files for the questions. the first element is not used. We use index starting with 1 for the array.
// 5 files for the midterm: q1 to q5
$theFile = array('a.txt','q1.txt', 'q2.txt', 'q3.txt', 'q4.txt', 'q5.txt');
//echo "index = " .$arr. " ";
//$first = substr($arr, 0, 1); // get the first character of the name which is for the filename index
//$name = substr($arr, 1); // get the name of the student
//$file = "data/".$theFile[$first]; // location of the file is in the data folder e.g. data/q1.txt
$file = "data/q1sample.txt"; // location of the file is in the data folder e.g. data/q1.txt
//echo $file;
if ($_POST[timestamp] == "")
{
$today = date("F j, Y, g:i a");
}
else
{
$today = $_POST[timestamp];
}
echo "
You started: ";
echo $today;
$now = date("F j, Y, g:i a");
echo " Now: ";
echo $now;
echo "
";
echo <<
HTML;
// counter for the question
$Qcounter = 0;
// open the file for reading
if ($fh = fopen("$file", 'rb')) {
// loop through the file by reading 1000 bytes or one line
// whichever comes first with each iteration.
// The data being read is broken into an array
// using the | to indicate the separate elements.
while ($line = fgetcsv($fh, 1000, "|")) {
$count = count($line); // get the size of the array
$Qcounter++; // increment counter for the question
$theName = 'Q'.$Qcounter; // name for the menu
$size = $count-1; // size for the menu
echo "";
}
// close the file
fclose($fh);
}
echo "";
echo <<
HTML;
} // end display the form
// Process Form to display the answers selected by the user
function process_form() {
// files for the answers. The first element is not used. We use index starting with 1 for the array.
// 5 files for the midterm: m1 to m5
$theAns = array('r.txt','m1.txt', 'm2.txt', 'm3.txt', 'm4.txt', 'm5.txt');
$ans = "data/m1sample.txt"; // location of the answer file in data folder, e.g. m1.txt
$userAnswer = array(); // store the user answers
$userAnswer[0] = 0;
// get the answers by the users with names of the menus beginning with Q like Q1, Q2, etc.
$i = 0;
foreach ($_POST as $key => $value) {
$i++; // start the index with 1
$question = substr($key, 0, 1); // get the first character of the name
if ($question == 'Q') {
$userAnswer[$i] = $value; // store the answer selected by the user
}
else {
$i = 0;
}
}
// get the answers from the file and store them in $line
// open the file for reading
if ($fh = fopen("$ans", 'rb')) {
// loop through the file by reading 1000 bytes or one line
// whichever comes first with each iteration.
// The data being read is broken into an array
// using the | to indicate the separate elements.
$line = fgetcsv($fh, 1000, "|");
}
// close the file
fclose($fh);
// check the results
$wrong = 0; // number of mistakes
$results = array(); // store the user's answers and correct
// loop through the two arrays to compare the user answers against the correct answers
for ($key = 1, $size = count($userAnswer); $key < $size; $key++) {
if ($userAnswer[$key] != $line[$key]) {
$wrong++;
$results[$key] = $line[$key];
}
else {
$results[$key] = "correct";
}
}
$total = 30; // there are 30 questions
$point = $total - $wrong;
echo "
You started: ";
echo $_POST[timestamp];
$now = date("F j, Y, g:i a");
echo " Now: ";
echo $now;
echo "
";
echo <<
Sample Midterm Results
EOF;
if ($wrong > 0) {
echo "
Good effort $_POST[name] You made $wrong mistakes. There are $total questions. Each question is worth 1 point. You earned $point points.
";
}
else {
echo "
Good job $_POST[name] You made $wrong mistake. There are $total questions. Each question is worth 1 point. You earned $point points.
If you are not satisfied with the results, you can try again until you are ready. So
here.
EOF;
// send the user answers and the corrected results for emailing
for ($key = 1, $size = count($results); $key <= $size; $key++) {
$question = "Q" . $key;
// $answer = "R" . $key;
echo "";
// echo "";
}
echo "";
} // end Process Form
// Email the results
function email_results($arr) {
$today = date("F j, Y, g:i a");
// files for the answers. The first element is not used. We use index starting with 1 for the array.
// 5 files for the midterm: m1 to m5
$theAns = array('r.txt','m1.txt', 'm2.txt', 'm3.txt', 'm4.txt', 'm5.txt');
$first = substr($arr, 0, 1); // get the first character of the name which is for the filename index
$ans = "data/".$theAns[$first]; // location of the answer file in data folder, e.g. m1.txt
$userAnswer = array(); // store the user answers
$userAnswer[0] = 0;
// get the answers by the users with names of the menus beginning with Q like Q1, Q2, etc.
$i = 0;
foreach ($_POST as $key => $value) {
$i++; // start the index with 1
$question = substr($key, 0, 1); // get the first character of the name
if ($question == 'Q') {
$userAnswer[$i] = $value; // store the answer selected by the user
}
else {
$i = 0;
}
}
// get the answers from the file and store them in $line
// open the file for reading
if ($fh = fopen("$ans", 'rb')) {
// loop through the file by reading 1000 bytes or one line
// whichever comes first with each iteration.
// The data being read is broken into an array
// using the | to indicate the separate elements.
$line = fgetcsv($fh, 1000, "|");
}
// close the file
fclose($fh);
$total = $_POST[point]+$_POST[wrong];
$tome ='sohann@fhda.edu';
$fromme = 'From:sohann@fhda.edu';
$subject = $_POST[name] . " Midterm " . $_POST[point] . " pts " . $_POST[timestamp] . " " . $today;
$to ="{$_POST[email]}\r\n";
$from = "From:{$_POST['email']}\r\n";
$subject2 = "Thanks " . $_POST[name] . " for Midterm ";
$textmsg = "Started: $_POST[timestamp] and finished: $today\n";
$textmsg .= "name: $_POST[name]\n";
$textmsg .= "# of mistakes: $_POST[wrong]\n";
$textmsg .= "points earned: $_POST[point]\n\n";
$textmsg .= "Remote Address: $_POST[remoteaddr]\n";
$textmsg .= "Remote Host: $_POST[remotehost]\n";
$textmsg .= "Remote User: $_POST[remoteuser]\n\n";
$textmsg .= "Here are the results (question#=>user answer=>correct answer):\n\n";
// get the results from the arrays that store the user's answers and the correct answers
for ($key = 1, $size = count($line); $key < $size; $key++) {
$textmsg .= "$key =>\n";
$textmsg .= " user => $userAnswer[$key]\n"; // store the answer selected by the user
$textmsg .= " answer => $line[$key]\n"; // store the correct answer
}
$textmsg = wordwrap($textmsg, 80);
// Send email to me
mail($tome, $subject, $textmsg, $from);
$message = "Started: $_POST[timestamp] and finished: $today\n\n";
$message .= "Thank you $_POST[name] for submitting your results.\n";
$message .= "You made $_POST[wrong] mistake(s). Each question is worth 1 point.\n";
$message .= "There are $total questions. So you earn $_POST[point] points.\n\n";
$message .= "Here are your answers:\n\n";
// get the answers by the users with names of the menus beginning with Q like Q1, Q2, etc.
$i = 0;
foreach ($_POST as $key => $value) {
$i++; // start the index with 1
$question = substr($key, 0, 1); // get the first character of the name
if ($question == 'Q') {
$message .= "$i =>$value\n"; // store the answer selected by the user
}
elseif ($question == 'R') {
$i--; // decrease the counter as each counter has 2 answers
}
else {
$i = 0;
}
}
$message = wordwrap($message, 80);
// Send email to the user
mail($to, $subject2, $message, $fromme);
/*
if (mail('hannso@gmail.com', $subject2, $message, $fromme)) {
echo 'mail() Success!' . " \n";
}
else {
echo 'mail() Failure!' . " \n";
}
*/
// Display a thank you note
echo "
";
echo "Started: $_POST[timestamp] and finished: $today";
echo "
";
echo <<Midterm Test Results
Thank you $_POST[name] for submitting your results.
You made $_POST[wrong] mistake(s). Each question is worth 1 point. There are $total questions. So you earn $_POST[point] points.