Create initial quiz class and start page

This commit is contained in:
Mark Wane 2019-10-05 13:50:34 +01:00
parent b62302dd4e
commit ef941e3cd7
2 changed files with 97 additions and 3 deletions

64
classes/quiz.class.php Normal file
View file

@ -0,0 +1,64 @@
<?php
namespace TestProject;
/*
* Quiz class
*/
class Quiz {
private const tb_str = 'quiz';
private const resulTb_str = 'quiz_result';
private $id_int;
private $name_str;
private $maxScore_int;
/**
* Load quiz
*
* @param int $id_int ID of quiz to load
*/
public function __construct( int $id_int ){
$db = DB::getDB();
$rows_arr = $db->select( self::tb_str, array(), array( 'id' => $id_int ) );
if ( empty( $rows_arr ) ){
throw new \RuntimeException( 'Invalid quiz ID' );
}
$this->id_int = $rows_arr[0]['id'];
$this->name_str = $rows_arr[0]['name'];
$this->maxScore_int = $rows_arr[0]['max_score'];
}
/**
* Get a list of quiz IDs and names
*
* @return string[] Array of quiz names keyed by id
*/
public static function getList(){
$db = DB::getDB();
$quiz_arr = $db->select( self::tb_str, array( 'id', 'name' ) );
return $quiz_arr;
}
/**
* Get name of Quiz
*
* @return string Name
*/
public function getName(){
return $this->name_str;
}
/**
* Get Maximum possible Score of quiz
*
* @return int Max score
*/
public function getMaxScore(){
return $this->maxScore_int;
}
}

View file

@ -1,5 +1,8 @@
<?php
use TestProject\Quiz;
require_once 'init.php';
$quizzes_arr = Quiz::getList();
?>
<!doctype html>
<html lang=en_GB>
@ -12,11 +15,38 @@ require_once 'init.php';
<body>
<div class="container">
<header class="row">
<div class="col col-md-6 offset-md-3">
<h1>Basic PHP Quiz</h1>
<div class="col">
<h1 class="text-center">Basic PHP Quiz</h1>
</div>
</header>
<main class="row">
<main>
<form method="POST" action="quiz.php">
<div class="form-row">
<div class="form-group col-md-6">
<label for="name">Name</label>
<input class="form-control" id="name" name="name" type="text" placeholder="<?php echo $_SESSION['name'] ?? ''; ?>" required>
</div>
<div class="form-group col-md-6">
<label for="email">Email</label>
<input class="form-control" id="email" name="email" type="email" placeholder="<?php echo $_SESSION['email'] ?? ''; ?>" required>
</div>
</div>
<div class="form-row">
<div class="form-group col">
<label for="quiz">Select quiz</label>
<select class="form-control" id="quiz" name="quiz">
<?php foreach( $quizzes_arr as $quiz_arr ){
echo '<option value="' . $quiz_arr['id'] . '">' . $quiz_arr['name'] . '</option>';
} ?>
</select>
</div>
</div>
<div class="form-row">
<div class="col">
<button type="submit" class="btn btn-primary">Start</button>
</div>
</div>
</form>
</main>
</div>
</body>