<?php
//=====================================
// Questioner.php (stub version)
//
// Created by M.Miyazaki on 2015.05.24
//=====================================

/***** Configurations *****/

/**
 * HTTP_METHOD
 *
 * Type of HTTP method.
 * Specify "GET" or "POST" as string.
 */
define("HTTP_METHOD", "GET");



/***** Main routine *****/

perform();
exit(0);   // Success execution



/***** Sub routines *****/

/**
 * perform
 *
 * This function is just like 'main' with C-lang.
 * Describe all works here.
 */
function perform()
{
    $request   = getRequest();
    $arguments = getArguments();

    if ($arguments)
    {
        $arguments = json_decode($arguments);
    }

    switch ($request)
    {
        case "getNumberOfQuestions":
            doGetNumberOfQuestions();
            break;
        case "getQuestion":
            doGetQuestion();
            break;
        case "getQuestionById":
            doGetQuestionById($arguments);
            break;
        case "getQuestions":
            doGetQuestions($arguments);
            break;
        case "setQuestion":
            doSetQuestion($arguments);
            break;
        default:
            break;
    }

    return;
}

/**
 * getRequest
 * 
 * Return the type of request.
 * ex) "getQuestion", "getNumberOfQuestions", etc...
 */
function getRequest()
{
    return getAttributeInHttpRequest("request");
}

/**
 * getArguments
 * 
 * Return the arguments in JSON array format.
 * ex) '["3","hoge",["foo","bar"]]', '["124"]', etc...
 */
function getArguments()
{
    return getAttributeInHttpRequest("arguments");
}

/**
 * getAttributeInHttpRequest
 *
 * Return a value of attribute you specify.
 * If it is NOT defined, return null.
 *
 * @param attributeName  Name of attribute.
 * @return Value of attribute you specify.
 */
function getAttributeInHttpRequest($attributeName)
{
    if (!$attributeName) return null;

    $value = null;

    if (HTTP_METHOD == "POST" && array_key_exists($attributeName, $_POST))
    {
        $value = $_POST[$attributeName];
    }
    else if (HTTP_METHOD == "GET" && array_key_exists($attributeName, $_GET))
    {
        $value = $_GET[$attributeName];
    }

    return $value;
}

/**
 * createResponse
 *
 * Create the response string in JSON format.
 *
 * @param request  Name of request.
 * @param data  Response data in JSON format.
 * @return Response string.
 */
function createResponse($request, $data)
{
    return sprintf('{"%s":%s}', $request, $data);
}

/**
 * doGetNumberOfQuestions
 *
 * Output sample of getNumberOfQuestions.
 */
function doGetNumberOfQuestions()
{
    $request = "getNumberOfQuestions";
    $data    = '"124"';
    $output  = createResponse($request, $data);

    print($output);

    return;
}

/**
 * doGetQuestion
 *
 * Output sample of getQuestion.
 */
function doGetQuestion()
{
    $id      = rand(1, 10);
    $request = "getQuestion";
    $data    = '["' . $id . '", "これは質問文です（ID=' . $id . '）", ["正答1"], ["誤答1", "誤答2", "誤答3"]]';
    $output  = createResponse($request, $data);

    print($output);

    return;
}

/**
 * doGetQuestionById
 *
 * Output sample of getQuestionById.
 */
function doGetQuestionById($arguments)
{
    $id      = $arguments[0];
    $request = "getQuestionById";
    $data    = '["' . $id . '", "これは質問文です（ID=' . $id . '）", ["正答1"], ["誤答1", "誤答2", "誤答3"]]';
    $output  = createResponse($request, $data);

    print($output);

    return;
}

/**
 * doGetQuestions
 *
 * Output sample of getQuestions.
 */
function doGetQuestions($arguments)
{
    $number    = $arguments[0];
    $request   = "getQuestions";
    $questions = array();

    for ($index = 0; $index < $number; $index++)
    {
        $id   = rand(1, 10);
        $data = '["' . $id . '", "これは質問文です（ID=' . $id . '）", ["正答1"], ["誤答1", "誤答2", "誤答3"]]';
        array_push($questions, $data);
    }

    $data = "[" . implode(",", $questions) . "]";

    $output  = createResponse($request, $data);

    print($output);

    return;
}

/**
 * doSetQuestion
 *
 * Output sample of setQuestion.
 * (nothing to do...)
 */
function doSetQuestion($arguments)
{
    //$request   = "setQuestion";
    //print($output);

    return;
}

?>