<repo_name>abdo2017/php-quiz-app<file_sep>/inc/toasts.php
<?php

$correctToasts=[
  "That was correct, great job!",
  "Awesome work, that was right!",
  "Nice, you got that correct!"
];

$wrongToasts=[
  "Oh no! That was incorrect",
  "Rats! That wasn't quite right",
  "Oops! Wrong answer"
];

function getCorrectToast(){
  global $correctToasts;
  return $correctToasts[rand(0,2)];
}

function getWrongToast(){
  global $wrongToasts;
  return $wrongToasts[rand(0,2)];
}



?>
<file_sep>/inc/generate_questions.php
<?php

//initialize session if it hasn't already been
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

function generateNewQuestionBank(){

  unset($_SESSION['question_bank']);
  $_SESSION["question_bank"] = [];
  $_SESSION["score"] = 0;

  // Loop for required number of questions
  for ($i=1; $i<=10; $i++){
    generateRandomQuestion($i);
  }

}

// Generate random questions
//q stands for question
//a stands for answer, where a1 is the correct one
function generateRandomQuestion($i){

  // Get random numbers to add
  $question1 = rand(10,90); // it's the number 1 actually
  $question2 = rand(10,90); // it's the number 2 actually

  // CALCULATE correct answer
  $answer1 = $question1 + $question2;
  $answer2 = generateWrongAnswer($answer1);

  // Make sure the two are unique answers
  do{
    $answer3 = generateWrongAnswer($answer1);
  } while ($answer2 == $answer3);

// Add question and answer to questions array
  $_SESSION["question_bank"][$i] = [
    "question" => "$question1 + $question2",
    "answers" => [$answer1,$answer2,$answer3]
  ];
}

// Get incorrect answers within 10 numbers either way of correct answer

function generateWrongAnswer($answer1){
  do{
    $deviation = rand(-10,10);
  } while ($deviation == 0);
 
  return $answer1 + $deviation;
}
<file_sep>/play.php
<?php
//initialize session if it hasn't already been
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}
//include quiz code
require_once "inc/quiz.php";
?>

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Math Quiz: Addition</title>
  <link rel="stylesheet" href="css/normalize.css">
  <link rel="stylesheet" href="css/styles.css">
</head>

<body>
  <div class="container">
    <?php if ($questionNumber==11){?>
      <div class="congrats-box box">
        <h2>You got <?php echo $_SESSION["score"]; ?> out of 10!</h2>
        <h1><?php echo getFinalMessage($_SESSION["score"]); ?></h1>
        <form action="play.php" method="post">
          <input type="submit" class="btn" name="answer" value="Play Again!" />
        </form>
      </div>
    <?php } else {?>
      <div class="quiz-box box">
        <div class="toast <?php echo $toastColor ?>"><?php echo $toastMessage; ?></div>
        <p class="breadcrumbs">Question <?php echo $questionNumber ?> of 10</p>
        <p class="quiz">What is <?php echo $questionQuestion; ?>?</p>
        <form action="play.php" method="post">
          <input type="hidden" name="id" value="0" />
          <?php
          //display answers
          foreach($questionAnswers as $answer){ ?>
            <input type="submit" class="btn" name="answer" value="<?php echo $answer ?>" />
          <?php } ?>
        </form>
      </div>
    <?php } ?>
  </div>
</body>

</html>
<file_sep>/inc/quiz.php
<?php
/*
 * PHP Techdegree Project 2: Build a Quiz App in PHP
 *
 * These comments are to help you get started.
 * You may split the file and move the comments around as needed.
 *
 * You will find examples of formating in the index.php script.
 * Make sure you update the index file to use this PHP script, and persist the users answers.
 *
 * For the questions, you may use:
 *  1. PHP array of questions
 *  2. json formated questions
 *  3. auto generate questions
 *
 */

 //initialize session if it hasn't already been
 if (session_status() == PHP_SESSION_NONE) {
     session_start();
 }

 require "generate_questions.php";

// Move question tracker up one
 $_SESSION['question_number']++;

//if we're haven't started a test or completed a test
 if ($_SESSION["question_number"]>=12 || isset($_POST['play'])){
   //start a new game
   generateNewQuestionBank();
   $_SESSION["question_number"] = 1;
   $toastMessage = "Good luck!";
 }


// Keeps track of which questions have been asked
$questionNumber = $_SESSION['question_number'];

if ($questionNumber!=11){
  $questionQuestion = $_SESSION['question_bank'][$questionNumber]["question"];
  $questionAnswers = $_SESSION['question_bank'][$questionNumber]["answers"];
  // Shuffle answer buttons
  shuffle($questionAnswers);
}

//do for all questions but Q1
if ($questionNumber!=1){
  $previousAnswer = $_POST['answer'];

  // Toast correct and incorrect answers
  require "toasts.php";
  if ($previousAnswer == $_SESSION['question_bank'][$questionNumber-1]["answers"][0]){
    $toastColor = "green";
    $toastMessage = getCorrectToast();
    $_SESSION["score"]++;
  } else {
    $toastColor = "red";
    $toastMessage = GetWrongToast();
  }
}

function getFinalMessage($score){
  if ($score==10){
    return "Wow, you got everything correct!";
  } else if ($score >=7){
    return "Great work! You're doing well.";
  } else if ($score >=5){
    return "Just passed! Keep practicing to get better!";
  } else {
    return "Good effort, but you can do better!";
  }
}
