Create remaining item classes
This commit is contained in:
parent
ef941e3cd7
commit
ccc1a23e84
|
@ -0,0 +1,52 @@
|
|||
<?php
|
||||
namespace TestProject;
|
||||
|
||||
/*
|
||||
* Answer class
|
||||
*/
|
||||
class Answer extends Item {
|
||||
private const tb_str = 'answer';
|
||||
|
||||
private $text_str;
|
||||
private $correct_bln;
|
||||
|
||||
/**
|
||||
* Load answer
|
||||
*
|
||||
* @param int $id_int ID of answer 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 answer ID' );
|
||||
}
|
||||
|
||||
$this->id_int = $rows_arr[0]['id'];
|
||||
$this->text_str = $rows_arr[0]['text'];
|
||||
$this->correct_bln = (bool) $rows_arr[0]['correct'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of answer IDs for a question
|
||||
*
|
||||
* @param int $questID_int ID of question to load answers for
|
||||
*
|
||||
* @return int[] Array of answer IDs
|
||||
*/
|
||||
public static function getList( int $questID_int ){
|
||||
$db = DB::getDB();
|
||||
|
||||
$rows_arr = $db->select( self::tb_str, array( 'id' ), array( 'question' => $questID_int ) );
|
||||
|
||||
$id_arr = array();
|
||||
foreach( $rows_arr as $row_arr ){
|
||||
$id_arr[] = $row_arr['id'];
|
||||
}
|
||||
|
||||
return $id_arr;
|
||||
}
|
||||
|
||||
}
|
|
@ -46,7 +46,7 @@ class DB {
|
|||
if ( empty( $fields_arr ) ){
|
||||
$fields_str = '*';
|
||||
} else {
|
||||
foreach( $field_arr as &$field_str ){
|
||||
foreach( $fields_arr as $field_str ){
|
||||
$field_str = '`' . $field_str . '`';
|
||||
}
|
||||
$fields_str = implode( ', ', $fields_arr );
|
||||
|
@ -103,10 +103,10 @@ class DB {
|
|||
|
||||
$fields_str .= '`' . $field_str . '`';
|
||||
|
||||
$var_arr[] = $value;
|
||||
$val_arr[] = $value;
|
||||
}
|
||||
|
||||
$sql_str = 'INSERT INTO `' . $table_str . '` (' . $fields_str . ') VALUES (' . implode( ', ', array_fill( 0, count( $var_arr ), '?' ) ) . ')';
|
||||
$sql_str = 'INSERT INTO `' . $table_str . '` (' . $fields_str . ') VALUES (' . implode( ', ', array_fill( 0, count( $val_arr ), '?' ) ) . ')';
|
||||
|
||||
$stmt = $this->conn->prepare( $sql_str );
|
||||
return $stmt->execute( $val_arr );
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
namespace TestProject;
|
||||
|
||||
/**
|
||||
* Item class
|
||||
*/
|
||||
class Item {
|
||||
protected $id_int;
|
||||
|
||||
/**
|
||||
* Get ID of Item
|
||||
*
|
||||
* @return int ID
|
||||
*/
|
||||
public function getID(){
|
||||
return $this->id_int;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,76 @@
|
|||
<?php
|
||||
namespace TestProject;
|
||||
|
||||
/*
|
||||
* Question class
|
||||
*/
|
||||
class Question extends Item {
|
||||
private const tb_str = 'question';
|
||||
private const resulTb_str = 'question_result';
|
||||
|
||||
private $text_str;
|
||||
private $answers_arr;
|
||||
|
||||
/**
|
||||
* Load question
|
||||
*
|
||||
* @param int $id_int ID of question 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 question ID' );
|
||||
}
|
||||
|
||||
$this->id_int = $rows_arr[0]['id'];
|
||||
$this->text_str = $rows_arr[0]['text'];
|
||||
|
||||
$answerID_arr = Answer::getList( $this->id_int );
|
||||
$this->answers_arr = array();
|
||||
|
||||
foreach( $answerID_arr as $answerID_int ){
|
||||
$this->answers_arr[] = new Answer( $answerID_int );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of question IDs for a quiz
|
||||
*
|
||||
* @param int $quizID_int ID of quiz to load questions for
|
||||
*
|
||||
* @return int[] Array of question IDs
|
||||
*/
|
||||
public static function getList( int $quizID_int ){
|
||||
$db = DB::getDB();
|
||||
|
||||
$rows_arr = $db->select( self::tb_str, array( 'id' ), array( 'quiz' => $quizID_int ) );
|
||||
|
||||
$id_arr = array();
|
||||
foreach( $rows_arr as $row_arr ){
|
||||
$id_arr[] = $row_arr['id'];
|
||||
}
|
||||
|
||||
return $id_arr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get question text
|
||||
*
|
||||
* @return string Question text
|
||||
*/
|
||||
public function getText(){
|
||||
return $this->text_str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Answers for question
|
||||
*
|
||||
* @return TestProject\Answer[] Array of answer objects
|
||||
*/
|
||||
public function getAnswers(){
|
||||
return $this->answers_arr;
|
||||
}
|
||||
}
|
|
@ -4,13 +4,13 @@ namespace TestProject;
|
|||
/*
|
||||
* Quiz class
|
||||
*/
|
||||
class Quiz {
|
||||
class Quiz extends Item {
|
||||
private const tb_str = 'quiz';
|
||||
private const resulTb_str = 'quiz_result';
|
||||
|
||||
private $id_int;
|
||||
private $name_str;
|
||||
private $maxScore_int;
|
||||
private $questionID_arr;
|
||||
|
||||
/**
|
||||
* Load quiz
|
||||
|
@ -28,6 +28,8 @@ class Quiz {
|
|||
$this->id_int = $rows_arr[0]['id'];
|
||||
$this->name_str = $rows_arr[0]['name'];
|
||||
$this->maxScore_int = $rows_arr[0]['max_score'];
|
||||
|
||||
$this->questionID_arr = Question::getList( $this->id_int );
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -0,0 +1,108 @@
|
|||
<?php
|
||||
namespace TestProject;
|
||||
|
||||
/*
|
||||
* User class
|
||||
*/
|
||||
class User extends Item {
|
||||
private const tb_str = 'user';
|
||||
|
||||
private $name_str;
|
||||
private $email_str;
|
||||
|
||||
/**
|
||||
* Load user
|
||||
*
|
||||
* @param int $id_int ID of user 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 user ID' );
|
||||
}
|
||||
|
||||
$this->id_int = $rows_arr[0]['id'];
|
||||
$this->name_str = $rows_arr[0]['name'];
|
||||
$this->email_str = $rows_arr[0]['email'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Find user by email and optionally update name
|
||||
*
|
||||
* @param string $email_str Email to search by
|
||||
* @param string $name_str Name of user to update if needed
|
||||
*
|
||||
* @return TestProject\User User object found or created
|
||||
*/
|
||||
public static function find( string $email_str, string $name_str = '' ){
|
||||
$db = DB::getDB();
|
||||
|
||||
$rows_arr = $db->select( self::tb_str, array( 'id' ), array( 'email' => $email_str ) );
|
||||
|
||||
if ( empty( $rows_arr ) ){
|
||||
$id_int = self::create( $email_str, $name_str );
|
||||
} else {
|
||||
$id_int = $rows_arr[0]['id'];
|
||||
}
|
||||
|
||||
$user = new User( $id_int );
|
||||
|
||||
if ( ! empty( $name_str ) && $user->getName() != $name_str ){
|
||||
$user->updateName( $name_str );
|
||||
}
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new user
|
||||
*
|
||||
* @param string $email_str Email of user
|
||||
* @param string $name_str Name of user
|
||||
*
|
||||
* @return int ID of new user
|
||||
*/
|
||||
private static function create( string $email_str, string $name_str ){
|
||||
$db = DB::getDB();
|
||||
|
||||
$db->insert( self::tb_str, array( 'email' => $email_str, 'name' => $name_str ) );
|
||||
|
||||
$rows_arr = $db->select( self::tb_str, array( 'id' ), array( 'email' => $email_str ) );
|
||||
|
||||
return $rows_arr [0]['id'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Update name of user
|
||||
*
|
||||
* @param string $name New name
|
||||
*/
|
||||
private function updateName( string $name_str ){
|
||||
$db = DB::getDB();
|
||||
|
||||
$db->update( self::tb_str, array( 'name' => $name_str ), array( 'id' => $this->id_int ) );
|
||||
|
||||
$this->name_str = $name_str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get name of User
|
||||
*
|
||||
* @return string Name
|
||||
*/
|
||||
public function getName(){
|
||||
return $this->name_str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get email of User
|
||||
*
|
||||
* @return string Email
|
||||
*/
|
||||
public function getEmail(){
|
||||
return $this->email_str;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue