57 lines
2.0 KiB
PHP
57 lines
2.0 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Created by PhpStorm.
|
|
* User: leanderschmedemann
|
|
* Date: 01/04/2017
|
|
* Time: 10:08
|
|
*/
|
|
|
|
require_once('includes.inc.php');
|
|
|
|
class Question
|
|
{
|
|
private $question;
|
|
private $answers;
|
|
private $correct_answer_position;
|
|
private $category_id;
|
|
private $category;
|
|
|
|
function __construct($db, $id) {
|
|
$question_details = $db->query('SELECT "question", "answer0", "answer1", "answer2", "answer3", "Categories_ID"
|
|
FROM "Questions" WHERE "ID" = $1', array($id))['data'][0];
|
|
$q = $question_details['question'];
|
|
$a0 = $question_details['answer0'];
|
|
$a1 = $question_details['answer1'];
|
|
$a2 = $question_details['answer2'];
|
|
$a3 = $question_details['answer3'];
|
|
$cid = $question_details['Categories_ID'];
|
|
$c = $db->query('SELECT "name" FROM "Categories" WHERE "ID" = $1', array($cid));
|
|
$this->question = $q;
|
|
$this->answers = array('a0' => $a0, 'a1' => $a1, 'a2' => $a2, 'a3' => $a3);
|
|
shuffle($this->answers);
|
|
$this->correct_answer_position = array_search('a0', $this->answers);
|
|
$this->category_id = $cid;
|
|
$this->category = $c;
|
|
}
|
|
|
|
public static function get_next_question($db, $uid) {
|
|
$qid = $db->query('SELECT "Questions_ID" FROM "GamesQuestions" WHERE "Games_ID" = $1 LIMIT 1', array($uid))['data'][0]['ID'];
|
|
$question = new Question($db, $qid);
|
|
$db->query('DELETE FROM "GamesQuestions" WHERE "Games_ID" = $1 AND "Questions_ID" = $2', array($uid, $qid));
|
|
$question_object = $question->get_question_object();
|
|
return $question_object;
|
|
}
|
|
|
|
// Create the question message for API.
|
|
function get_question_object() {
|
|
$question_object = array(
|
|
'categoryID' => $this->category_id,
|
|
'categoryName' => $this->category,
|
|
'question' => $this->question,
|
|
'answers' => [$this->answers['a0'], $this->answers['a1'], $this->answers['a2'], $this->answers['a3']]
|
|
);
|
|
return $question_object;
|
|
}
|
|
|
|
} |