<?php
if (isset($_GET['source'])) { die(highlight_file(__FILE__1)); }
?>
<?php
//  version PHP de quiz1.cgi Chapitre 7 de 
//   Web Applications: Concepts & Real World Design
//   by Craig D. Knuckles, David S. Yuen, wiley, February 2004,
//        http://www.cknuckles.com/webapps/chap07/chap7.html

$SCRIPT_NAME=$_SERVER['SCRIPT_NAME']; 

//### the data source #######################################
$quiz_question = array(
  
"How many hairs are there on Homer Simpson's head?",
  
"What planet is Luke Skywalker from?",
  
"What is the last digit if you multiply 3 by itself a million times?"
);
$quiz_choices = array(
  
"0;1;2;3;4",
  
"Alderaan;Bespin;Corellia;Dantooine;Tatooine",
  
"1;3;5;7;9"
);
$quiz_answer = array(
  
"2",
  
"Tatooine",
  
"1"
);
// ### end data source #######################################

#################################################################
function welcome_page() {
    global 
$SCRIPT_NAME;
     print <<<PAGE
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
     <head>
      <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
      <title>Trivia Quiz</title></head>
  <body>
    <h2>Welcome to the Trivia Quiz</h2>
    <p>You will be given a series of questions.</p>
    <form action="
$SCRIPT_NAME" method="get">
     <p><input type="hidden" name="request" value="begin_quiz"/>
     <input type="submit" value="Begin Quiz"/></p>
    </form>
  </body>
</html>
PAGE;
}

#################################################################
function begin_quiz(){
     print <<<TOP
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
     <head>
      <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
     <title>Trivia Quiz</title></head>
     <body>
      <p>Here is your first question.</p>
TOP;

    
print_question(10);

     print <<<BOTTOM
    </body>
</html>
BOTTOM;
}

#################################################################
function print_question($qnumber$correct){
    global 
$quiz_question,$quiz_choices,$quiz_answer,$SCRIPT_NAME;
  
$index $qnumber-1;
  
$choices preg_split("/;/"$quiz_choices[$index]);
  
  print<<<QUESTION
  <form action = "$SCRIPT_NAME" method="get">
  <p>
$qnumber$quiz_question[$index]</p>
  <p>
QUESTION;
  foreach(
$choices as  $answer) {
     print 
'<input type="radio" name="answer" value="'.$answer.'"/>'.$answer;
  }
  print<<<FORM
  </p>
   <p>
       <input type="hidden" name="qnumber" value="
$qnumber"/>
       <input type="hidden" name="correct" value="
$correct"/>
       <input type="hidden" name="request" value="grade_question"/>
       <input type="submit" value="Submit answer"/>
   </p>
   </form>
FORM;
}


#################################################################
function grade_question() {
    global 
$quiz_question,$quiz_choices,$quiz_answer,$SCRIPT_NAME;
    print<<<TOP
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
     <head>
      <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
    <title>Trivia Quiz</title></head><body>
TOP;

  
$qnumber $_GET["qnumber"];
  
$correct $_GET["correct"];
  
$index $qnumber 1;
 
  if(
$_GET["answer"] == $quiz_answer[$index]) {
     print 
"Your answer of {$_GET['answer']} is CORRECT.<br />\n";
     
$correct++;
  }
  else {
     print 
"Sorry, your answer of {$_GET['answer']} is INCORRECT.<br />\n";
  }
 
  
$qnumber++;     # Either way, the question has been answered.
  
  
if($qnumber count($quiz_question)) {
    print 
"Your final score is $correct correct out of ".($qnumber-1).".\n".
          
"Thank you for playing.<br />\n".
          
"<a href=\"$SCRIPT_NAME?request=begin_quiz\">To play again</a>";
  }
  else {
    print 
"Your score so far is $correct correct out of ".($qnumber-1).".\n".
          
"Here is your next question.\n"
    
print_question($qnumber$correct);
  }
  print<<<BOTTOM
  </body>
</html>
BOTTOM;
}

// ### app logic #############################################

if (isset($_GET["request"])){
    if (
$_GET["request"] == "begin_quiz"begin_quiz();
    else if(
$_GET["request"] == "grade_question"grade_question();
} else 
welcome_page();

// ### end app logic #########################################