redirectToStart( 'no-code' ); } if( ( $id = $this->checkID( $_SESSION['room'] ) ) === false ) { $this->redirectToStart( 'invalid' ); } $stmnt = Database::getConnection()->prepare( 'SELECT * FROM rooms WHERE id = :id' ); $stmnt->execute( array( 'id' => $id ) ); return $stmnt->fetch( PDO::FETCH_ASSOC ); } public function getQuestions( $room_id ) { $stmnt = Database::getConnection()->prepare( 'SELECT * FROM questions WHERE room_id = :id' ); $stmnt->execute( array( 'id' => $room_id ) ); $result = $stmnt->fetchAll( PDO::FETCH_ASSOC ); if( $result === false || empty( $result ) ) { return false; } $return = array(); foreach( $result as $index => $value ) { $result[$index]['question'] = substr( $result[$index]['question'], 0, 50 ); $result[$index]['question'] = str_replace( "\n", ' ', $result[$index]['question'] ); $result[$index]['answer'] = substr( $result[$index]['answer'], 0, 50 ); $result[$index]['answer'] = str_replace( "\n", ' ', $result[$index]['answer'] ); } return $result; } private function checkID($room_id) { $stmnt = Database::getConnection()->prepare( 'SELECT id FROM rooms WHERE id = :id' ); $stmnt->execute( array( 'id' => $room_id ) ); $result = $stmnt->fetch( PDO::FETCH_ASSOC ); if( $result === false || empty( $result ) ) { return false; } return $result['id']; } public function addQuestion(int $room_id, string $question) { $stmnt = Database::getConnection()->prepare( 'INSERT INTO questions ( room_id, question, created_by ) VALUES ( :room_id, :question, :created_by )' ); $stmnt->execute( array( 'room_id' => $room_id, 'question' => htmlspecialchars( $question ), 'created_by' => 'Anonymous' ) ); } public function addAnswer(int $question_id, string $answer) { $stmnt = Database::getConnection()->prepare( 'UPDATE questions SET answer = :answer WHERE id = :id' ); $stmnt->execute( array( 'answer' => htmlspecialchars( $answer ), 'id' => $question_id ) ); } public function checkUserIsRoomOwner( array $a_room ) { $stmnt = Database::getConnection()->prepare( 'SELECT id, owner_sid FROM rooms WHERE id = :id' ); $stmnt->execute( array( 'id' => $a_room['id'] ) ); $result = $stmnt->fetch( PDO::FETCH_ASSOC ); if( $result === false || empty( $result ) ) { return false; } if( $result['owner_sid'] === session_id() ) { return true; } return false; } public function getQuestion( int $question_id) { $stmnt = Database::getConnection()->prepare( 'SELECT * FROM questions WHERE id = :id' ); $stmnt->execute( array( 'id' => $question_id ) ); $result = $stmnt->fetch( PDO::FETCH_ASSOC ); if( $result === false || empty( $result ) ) { return false; } $result['question'] = str_replace( "\n", '
', $result['question'] ); return $result; } }