diff --git a/classes/answer.class.php b/classes/answer.class.php new file mode 100644 index 0000000..8e33e01 --- /dev/null +++ b/classes/answer.class.php @@ -0,0 +1,52 @@ +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; + } + +} diff --git a/classes/db.class.php b/classes/db.class.php index dc0be37..ef6dfbd 100644 --- a/classes/db.class.php +++ b/classes/db.class.php @@ -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 ); diff --git a/classes/item.class.php b/classes/item.class.php new file mode 100644 index 0000000..2664a10 --- /dev/null +++ b/classes/item.class.php @@ -0,0 +1,18 @@ +id_int; + } +} diff --git a/classes/question.class.php b/classes/question.class.php new file mode 100644 index 0000000..bbf4ed9 --- /dev/null +++ b/classes/question.class.php @@ -0,0 +1,76 @@ +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; + } +} diff --git a/classes/quiz.class.php b/classes/quiz.class.php index 44971e0..fe20274 100644 --- a/classes/quiz.class.php +++ b/classes/quiz.class.php @@ -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 ); } /** diff --git a/classes/user.class.php b/classes/user.class.php new file mode 100644 index 0000000..7d7e387 --- /dev/null +++ b/classes/user.class.php @@ -0,0 +1,108 @@ +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; + } +} diff --git a/init.php b/init.php index 528ccdd..4a63920 100644 --- a/init.php +++ b/init.php @@ -1,6 +1,8 @@