<?php
// Set your API key and data center
$apiToken = 'GZjoFiLmb2j62s8AHmMWKN25BZjGBhsU5ez4cwjn';
$dataCenter = 'fra1'; // Your data center, e.g., 'fra1'

// Set the endpoint URL
$baseUrl = "https://$dataCenter.qualtrics.com/API/v3/surveys";

// Initialize cURL session
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $baseUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    "X-API-TOKEN: $apiToken",
    "Content-Type: application/json"
]);

// Execute the request
$response = curl_exec($ch);

// Check for cURL errors
if(curl_errno($ch)){
    die('Request Error: ' . curl_error($ch));
}

curl_close($ch);

// Decode the response
$result = json_decode($response, true);
$surveys = $result['result']['elements'] ?? [];



?>
<!DOCTYPE html>
<html>
<head>
    <title>Select a Qualtrics Survey</title>
</head>
<body>
    <h1>Select a Survey</h1>

    <p>This tool has been created by Peter Edmond working for <a href="https://www.telospartners.com/">Telos Partners</a> as part of the OPL Data Analysis Project. Whilst it *SHOULD* behave as expected, it has been know for lightning to strike, computers to explode, and pigs to fly (allegedly, if thrown out of an aircraft?). There may be other interesting side effects? If this software does something unexpected, then it should be addressed to Telos Partners <a href="https://www.chiark.greenend.org.uk/~sgtatham/bugs.html">AFTER READING AND UNDERSTANDING how to report bugs effectively</a>.
    </p>
    <p>In order to be able to access the survey data, you must use this form to export the data from <a href="https://telospartners.eu.qualtrics.com/login">Qualtrics</a>. This is done via an API key. Normally the survey will be immediately visible by name in the drop down, however under certain circumstances this might not work, such as if it has the same name as another survey, or the survey has not been shared with the account that is associated with the API.
    </p>

    <p>The ability to check whether a surveyId is accessible for exporting can be checked directly via the surveyId. The form allowing direct entry of the surveyID lso carries out a full export of the data associated with the requested surveyId (provided that surveyId data can be accessed). The template survey is surveyId SV_cwKjMqAqGxImjMG if you wish to test a value. The surveyId is of the form SV_************** and can be found by looking at the survey web page path when looking at the survey in the Qualtrics interface.
    </p>

    <p>NOTE - Once you have pressed submit, your browser will appear to freeze. This is because there is LOADS of work going on in the background to authenticate, identify all the answers that have been provided to this survey, and download them. Once the available answer sets have been downloaded, you will get a messy page showing the surveyId, the total number of COMPLETED AND SUBMITTED response sets (responseId). Once the page with the Response IDs has appeared, then the data has been fully exported, and is <a href="./reportTemplate.html">available for analysis.</a></p>

    <p>The export process OVER-WRITES any previous results for a particular survey. This means that that if data is deleted in the Qualtrics interface, then that data set will be lost. Additionally, by importing the data, and additional completed surveys will be added to the data set and be available for analysis. Carrying out the import process effectively aims to sychronise the data for analysis with that from the Qualtrics survey software.</p>


    <?php if (empty($surveys)): ?>
        <p>No surveys found or an error occurred.</p><br><br><hr><br>

	<pre><?php echo print_r($result); ?></pre>
	<pre><?php


	?></pre>
    <?php else: ?>
        <form method="POST" action="getData.php">
            <label for="survey_id">Choose a survey to download data and produce reports:</label>
            <select name="survey_id" id="survey_id">
                <?php foreach ($surveys as $survey): ?>
                    <option value="<?= htmlspecialchars($survey['id']) ?>">
                        <?= htmlspecialchars($survey['name']) ?>
                    </option>
                <?php endforeach; ?>
            </select>
            <br><br>
            <input type="submit" value="Submit">
        </form>
        <br>
        <form method="POST" action="getData.php">
            <label for="survey_id">.....OR enter a surveyId to download data and produce reports:</label>
            <input name="survey_id" id="survey_id">
            <br><br>
            <input type="submit" value="Submit">
        </form>
    <?php endif; ?>
</body>
</html>


