online-faq/model/FaqModel.class.inc.php
2020-10-04 15:31:53 +02:00

129 lines
4.0 KiB
PHP

<?php
class FaqModel
{
public function redirectToStart($string)
{
header( "Location: ?c=default&a=index&rsn=$string" );
exit();
}
public function getRoom( )
{
if( !isset( $_SESSION['room'] ) )
{
$this->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 )
{
$end_question = '';
$end_answer = '';
if( strlen( $result[$index]['question'] ) > 50 )
{
$end_question = '...';
}
if( strlen( $result[$index]['answer'] ) > 50 )
{
$end_answer = '...';
}
$result[$index]['question'] = substr( $result[$index]['question'], 0, 50 ) . $end_question;
$result[$index]['question'] = str_replace( "\n", ' ', $result[$index]['question'] );
$result[$index]['answer'] = substr( $result[$index]['answer'], 0, 50 ) . $end_answer;
$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'];
}
/**
* @param int $room_id
* @param string $question
* @param string $nickname
*/
public function addQuestion(int $room_id, string $question, string $nickname )
{
$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' => $nickname ) );
}
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;
}
$secret = empty( $_COOKIE['owner_room_' . $a_room['id'] ] ) ? null : $_COOKIE['owner_room_' . $a_room['id'] ];
if( $result['owner_sid'] === $secret )
{
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", '<br>', $result['question'] );
return $result;
}
}