<?php

class Question {
    public $con;

    public function __construct($conn){
        $this->con = $conn;
    }

    public function getById($id){
        $quest = array();
        $sql = "SELECT * FROM questions WHERE id = $id";

        $result = $this->con->query($sql);

        if ($result->num_rows > 0) {
            // output data of each row
            while($row = $result->fetch_assoc()){
                array_push($quest, $row);
            }
            return json_encode($quest);
        }
    }

    public function getAllQuestions($limit = null){
        $quest = array();
        if($limit){
            $sql = "SELECT * FROM questions ORDER BY id DESC LIMIT $limit";
        }else{
            $sql = "SELECT * FROM questions";
        }

        $result = $this->con->query($sql);

        if ($result->num_rows > 0) {
            // output data of each row
            while($row = $result->fetch_assoc()){
                array_push($quest, $row);
            }
            return json_encode($quest);
        }
    }

    public function getAllQuestionsWithModifier($modifier = null){
        $quest = array();

        if($modifier && $modifier === 'today'){
            $sql = "SELECT * FROM questions WHERE date LIKE '%".date('Y-%-d')."%'";
        }elseif($modifier && $modifier === 'week'){
            $sql = "SELECT * FROM questions WHERE WEEKOFYEAR(date) = WEEKOFYEAR(NOW())";
        }elseif($modifier && $modifier === 'month'){
            $sql = "SELECT * FROM questions WHERE date LIKE '%".date('Y-m-%')."%'";
        }else{
            return "Modifier is Required";
        }

        $result = $this->con->query($sql);

        if ($result->num_rows > 0) {
            // output data of each row
            while($row = $result->fetch_assoc()){
                array_push($quest, $row);
            }
            return json_encode($quest);
        }
        // return $result;
    }
}