<?php
include "classes/DatabaseConnection.php";

// return JSON string of the request and results for getting word_test 
echo '{"request_type":"get_word_test"';

$error = FALSE;
$error_text = "";

if ($_GET["word_id"]) {
    $i_word_id = $_GET["word_id"];
    echo ',"parm_word_id":"' . $i_word_id . '"';
} else {
    $error = TRUE;
    $error_text = "Mandatory parameter word_id not supplied";
}

if ($_GET["language_id"]) {
    $i_language_id = $_GET["language_id"];
    echo ',"parm_language_id":"' . $i_language_id . '"';
} else {
    $i_language_id = '1';
    echo ',"parm_language_id":"dft=1"';
}
if ($_GET["number_of_tests"]) {
    $i_number_of_tests = $_GET["number_of_tests"];
    echo ',"parm_number_of_tests":"' . $i_number_of_tests . '"';
} else {
    $i_number_of_tests = 8;
    echo ',"parm_number_of_tests":"dft=8"';
}

if ($error) {
    echo ',"error_found":"' . $error_text . '"}';
    exit;
}
// get db connection
$db_conn = DatabaseConnection::getConnection();
echo ',"db_status":"' . $db_conn->status . '"';

// read correct meanings for given word
$query = "SELECT * FROM v_meanings m WHERE m.from_word_id = $i_word_id and language_id = $i_language_id";

$result = $db_conn->connection->query($query);
$count = 0;
$parts_of_speech = "0";
$correct_meaning_ids = "";
$correct_words = "";
if ($result->num_rows == 0) {
    // not found
    echo ',"error_found":"no meanings found for word with id ' . $i_word_id . '"}';
    exit;
} else {
    echo ',"correct_meanings":[';
    $separator = '';
    foreach ($result as $row) {
        echo $separator . '{"word":"' . $row['to_word'] . '","meaning_id":"' . $row['meaning_id'] . '","type":"correct"}';
        // store parts of speech of correct meanings
        $parts_of_speech .= "," . $row['part_of_speech_id'];
        // store meaning ids of correct meanings
        $correct_meaning_ids .= $separator . $row['meaning_id'];
        // store words of correct meanings
        $correct_words .= $separator . "'" . $row['to_word'] . "'";
        $separator = ',';
        $count++;
    }
    $result->close();
    echo ']'; // close array
}

// get any previous errors for this word (there may not be any)
$query = "select * FROM v_word_test_errors WHERE wrong_word_id = $i_word_id";

$result = $db_conn->connection->query($query);
if ($result->num_rows == 0) {
    // not found
    echo ',"incorrect_meanings":[]';
} else {
    echo ',"incorrect_meanings":[';
    $separator = '';
    foreach ($result as $row) {
        echo $separator . '{"word":"' . $row['to_word'] . '","meaning_id":"' . $row['meaning_id'] . '","type":"incorrect"}';
        $separator = ',';
        $count++;
        if ($count == $i_number_of_tests) {
            break;
        }
    }
    $result->close();
    echo ']'; // close array
}

// get random words to make up multiple choice (filter on relevant parts of speech) and omit any that are correct to avoid correct words being counted as errors
if ($count < $i_number_of_tests) {
    $x = $i_number_of_tests - $count;
    $query = "SELECT * FROM v_meanings where language_id = $i_language_id and part_of_speech_id in ($parts_of_speech) and to_word not in($correct_words) order by rand() limit $x";
    
    $result = $db_conn->connection->query($query);
    if ($result->num_rows == 0) {
        // not found
        echo ',"random_meanings":[]';
    } else {
        echo ',"random_meanings":[';
        $separator = '';
        foreach ($result as $row) {
            echo $separator . '{"word":"' . $row['to_word'] . '","meaning_id":"' . $row['meaning_id'] . '","type":"random"}';
            $separator = ',';
            $count++;
        }
        $result->close();
        echo ']'; // close array
    }
}

// close request object
echo '}';
?>