<?php
include "classes/DatabaseConnection.php";
// return JSON string of the request and results for list of words
echo '{"request_type":"get_words_for_test"';
if ($_GET["language"]) {
    $language = $_GET["language"];
    echo ',"parm_language":"' . $language . '"';
} else {
    $language = 2; // default Greek
    echo ',"parm_language":"dft-' . $language . '"';
}

if ($_GET["limit"]) {
    $limit = $_GET["limit"];
    echo ',"parm_limit":"' . $limit . '"';
} else {
    $limit = 10; // default 10
    echo ',"parm_limit":"dft-' . $limit . '"';
}
// read words from database
$db_conn = DatabaseConnection::getConnection();

$query = "SELECT rw.* FROM v_random_words rw INNER JOIN v_word_test_stats wts ON rw.from_word_id = wts.from_word_id WHERE language_id = $language ORDER BY latest_date LIMIT $limit";

$result = $db_conn->connection->query($query);

// initialise variables
$first_time = TRUE;

// start array of words 
echo ',"words":[';

foreach ($result as $row) {
    if (!$first_time) {
        echo ',';
    }
    // set up details of word
    echo '{"type":"word","word_id":"' . $row['from_word_id'] . '","word":"' . $row['from_word'] . '"}';
    $first_time = FALSE;
}

// close array of words and request object
echo ']}';
?>