<?php
// Load config
$config = require 'config.php';
$apiToken = $config['api_token'];
$dataCenter = $config['data_centre'];

// Get the survey ID from POST
$surveyId = $_POST['survey_id'] ?? null;
if (!$surveyId) {
    //$surveyId="SV_cAstEvm4ZrPaqGi";	
    $surveyId="SV_cwKjMqAqGxImjMG";	
    #die("No survey ID provided.");
}

// Build URL
$baseUrl = "https://$dataCenter.qualtrics.com/API/v3";
$questionsUrl = "$baseUrl/survey-definitions/$surveyId/questions";

// Set headers
$headers = [
    "X-API-TOKEN: $apiToken",
    "Content-Type: application/json"
];

// Make request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $questionsUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);

if (curl_errno($ch)) {
    die("Request error: " . curl_error($ch));
}

curl_close($ch);

// Output the raw JSON
//header('Content-Type: application/json');
//echo $response;
//var_dump($response);

$data = json_decode($response, true);
$questions = $data['result']['elements'] ?? [];
echo '<pre>';
print_r(array_slice($questions, 0, 1)); // Show one question object
echo '</pre>';
?>
<!DOCTYPE html>
<html>
<head>
    <title>Survey Questions</title>
</head>
<body>
    <h1>Survey Questions</h1>

    <ul>
        <?php foreach ($questions as $qid => $question): ?>
            <li>
                <strong><?= htmlspecialchars($qid) ?>:</strong>
                <?= htmlspecialchars($question['QuestionText'] ?? '[No question text]') ?>
            </li>
        <?php endforeach; ?>
    </ul>
</body>
</html>
