Files
postgrachelor-php/Question.php
2017-04-21 08:53:17 +02:00

63 lines
2.3 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 $category_id;
private $category;
private $token;
function __construct($db, $uid, $qid) {
$question_details = $db->query('SELECT "question", "answer0", "answer1", "answer2", "answer3", "Categories_ID"
FROM "Questions" WHERE "ID" = $1', array($qid))['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))['data'][0]['name'];
$this->question = $q;
$this->answers = array($a0, $a1, $a2, $a3);
shuffle($this->answers);
$ca_position = array_search($a0, $this->answers);
$this->category_id = $cid;
$this->category = $c;
$db->query('UPDATE "Games" SET "current_right_answer" = $1, "current_category" = $2 WHERE "ID" = $3',
array($ca_position, $cid, $uid));
$this->token = md5(uniqid(rand(), true));
$db->query('UPDATE "Games" SET "current_token" = $1 WHERE "ID" = $2', array($this->token, $uid));
}
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]['Questions_ID'];
$question = new Question($db, $uid, $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,
'token' => $this->token
);
return $question_object;
}
}