125 lines
4.4 KiB
PHP
125 lines
4.4 KiB
PHP
<?php
|
|
|
|
require_once __DIR__ . '/../lib/submissions.php';
|
|
|
|
$tokenRec = require_valid_token_web();
|
|
|
|
if ($method !== 'GET') {
|
|
json_error('METHOD_NOT_ALLOWED', 'Method not allowed', 405);
|
|
}
|
|
|
|
if (!empty($_GET['bundle'])) {
|
|
require_role(['admin', 'supervisor'], $tokenRec);
|
|
try {
|
|
$opened = qdb_open_read_or_fail();
|
|
[$pdo, $tmpDb, $lockFp] = $opened;
|
|
$bundle = qdb_export_all_questionnaires_bundle($pdo);
|
|
qdb_discard($tmpDb, $lockFp);
|
|
|
|
$filename = 'questionnaires_bundle_' . date('Y-m-d_His') . '.json';
|
|
header('Content-Type: application/json; charset=UTF-8');
|
|
header('Content-Disposition: attachment; filename="' . $filename . '"');
|
|
echo json_encode($bundle, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
|
|
exit;
|
|
} catch (Throwable $e) {
|
|
if (isset($tmpDb, $lockFp)) {
|
|
qdb_discard($tmpDb, $lockFp);
|
|
}
|
|
if (function_exists('qdb_api_log_note_exception')) {
|
|
qdb_api_log_note_exception($e);
|
|
}
|
|
require_once __DIR__ . '/../lib/errors.php';
|
|
error_log('export bundle: ' . $e->getMessage());
|
|
json_error('SERVER_ERROR', qdb_public_error_message($e, 'Export'), 500);
|
|
}
|
|
}
|
|
|
|
if (!empty($_GET['exportAll'])) {
|
|
require_role(['admin'], $tokenRec);
|
|
try {
|
|
$opened = qdb_open_read_or_fail();
|
|
[$pdo, $tmpDb, $lockFp] = $opened;
|
|
$allVersions = !empty($_GET['allVersions']);
|
|
$zipPath = qdb_build_server_export_zip($pdo, $tokenRec, $allVersions);
|
|
qdb_discard($tmpDb, $lockFp);
|
|
|
|
$label = $allVersions ? 'all_versions' : 'current';
|
|
$filename = 'responses_export_' . $label . '_' . date('Y-m-d_His') . '.zip';
|
|
|
|
header('Content-Type: application/zip');
|
|
header('Content-Disposition: attachment; filename="' . $filename . '"');
|
|
header('Content-Length: ' . (string)filesize($zipPath));
|
|
readfile($zipPath);
|
|
@unlink($zipPath);
|
|
exit;
|
|
} catch (Throwable $e) {
|
|
if (isset($tmpDb, $lockFp)) {
|
|
qdb_discard($tmpDb, $lockFp);
|
|
}
|
|
if (function_exists('qdb_api_log_note_exception')) {
|
|
qdb_api_log_note_exception($e);
|
|
}
|
|
require_once __DIR__ . '/../lib/errors.php';
|
|
error_log('export exportAll: ' . $e->getMessage());
|
|
json_error('SERVER_ERROR', qdb_public_error_message($e, 'Export'), 500);
|
|
}
|
|
}
|
|
|
|
$qnID = $_GET['questionnaireID'] ?? '';
|
|
if (!$qnID) {
|
|
json_error('MISSING_PARAM', 'questionnaireID query param required', 400);
|
|
}
|
|
|
|
$allVersions = !empty($_GET['allVersions']);
|
|
|
|
try {
|
|
$opened = qdb_open_read_or_fail();
|
|
[$pdo, $tmpDb, $lockFp] = $opened;
|
|
|
|
$qn = $pdo->prepare('SELECT * FROM questionnaire WHERE questionnaireID = :id');
|
|
$qn->execute([':id' => $qnID]);
|
|
$questionnaire = $qn->fetch(PDO::FETCH_ASSOC);
|
|
if (!$questionnaire) {
|
|
qdb_discard($tmpDb, $lockFp);
|
|
json_error('NOT_FOUND', 'Questionnaire not found', 404);
|
|
}
|
|
|
|
$ctx = qdb_export_questionnaire_context($pdo, $qnID);
|
|
$questions = $ctx['questions'];
|
|
$resultColumns = $ctx['resultColumns'];
|
|
$optionTextMap = $ctx['optionTextMap'];
|
|
|
|
if ($allVersions) {
|
|
$rows = qdb_export_all_versions_rows($pdo, $qnID, $questions, $resultColumns, $optionTextMap, $tokenRec);
|
|
qdb_discard($tmpDb, $lockFp);
|
|
|
|
$safeName = qdb_export_safe_basename($questionnaire['name']);
|
|
$filename = $safeName . '_all_versions_' . date('Y-m-d') . '.csv';
|
|
|
|
header('Content-Type: text/csv; charset=UTF-8');
|
|
header('Content-Disposition: attachment; filename="' . $filename . '"');
|
|
echo qdb_export_rows_to_csv_string($rows, qdb_export_all_versions_csv_fallback_header($resultColumns));
|
|
exit;
|
|
}
|
|
|
|
$rows = qdb_export_current_rows($pdo, $qnID, $questions, $resultColumns, $optionTextMap, $tokenRec);
|
|
qdb_discard($tmpDb, $lockFp);
|
|
|
|
$safeName = qdb_export_safe_basename($questionnaire['name']);
|
|
$filename = $safeName . '_v' . $questionnaire['version'] . '.csv';
|
|
|
|
header('Content-Type: text/csv; charset=UTF-8');
|
|
header('Content-Disposition: attachment; filename="' . $filename . '"');
|
|
echo qdb_export_rows_to_csv_string($rows, qdb_export_current_csv_fallback_header($resultColumns));
|
|
} catch (Throwable $e) {
|
|
if (isset($tmpDb, $lockFp)) {
|
|
qdb_discard($tmpDb, $lockFp);
|
|
}
|
|
if (function_exists('qdb_api_log_note_exception')) {
|
|
qdb_api_log_note_exception($e);
|
|
}
|
|
require_once __DIR__ . '/../lib/errors.php';
|
|
error_log('export csv: ' . $e->getMessage());
|
|
json_error('SERVER_ERROR', qdb_public_error_message($e, 'Export'), 500);
|
|
}
|