ordered questionnaire list with conditions * GET ?id= -> single questionnaire in app JSON format * GET ?translations=1 -> all translations merged across all tables */ $tokenRec = require_valid_token(); if ($method !== 'GET') { json_error('METHOD_NOT_ALLOWED', 'Method not allowed', 405); } [$pdo, $tmpDb, $lockFp] = qdb_open(false); $qnID = $_GET['id'] ?? ''; $translations = $_GET['translations'] ?? ''; if ($translations) { $result = []; $rows = $pdo->query(" SELECT q.defaultText AS stringKey, qt.languageCode, qt.text FROM question_translation qt JOIN question q ON q.questionID = qt.questionID ")->fetchAll(PDO::FETCH_ASSOC); foreach ($rows as $r) { $result[$r['languageCode']][$r['stringKey']] = $r['text']; } $rows = $pdo->query(" SELECT ao.defaultText AS stringKey, aot.languageCode, aot.text FROM answer_option_translation aot JOIN answer_option ao ON ao.answerOptionID = aot.answerOptionID ")->fetchAll(PDO::FETCH_ASSOC); foreach ($rows as $r) { $result[$r['languageCode']][$r['stringKey']] = $r['text']; } $rows = $pdo->query("SELECT stringKey, languageCode, text FROM string_translation")->fetchAll(PDO::FETCH_ASSOC); foreach ($rows as $r) { $result[$r['languageCode']][$r['stringKey']] = $r['text']; } qdb_discard($tmpDb, $lockFp); json_success(["translations" => $result]); } if ($qnID) { $qn = $pdo->prepare("SELECT * FROM questionnaire WHERE questionnaireID = :id"); $qn->execute([':id' => $qnID]); $qnRow = $qn->fetch(PDO::FETCH_ASSOC); if (!$qnRow) { qdb_discard($tmpDb, $lockFp); json_error('NOT_FOUND', 'Questionnaire not found', 404); } $stmt = $pdo->prepare(" SELECT questionID, defaultText, type, orderIndex, configJson FROM question WHERE questionnaireID = :id ORDER BY orderIndex "); $stmt->execute([':id' => $qnID]); $dbQuestions = $stmt->fetchAll(PDO::FETCH_ASSOC); $questions = []; foreach ($dbQuestions as $dbQ) { $localId = $dbQ['questionID']; $parts = explode('__', $localId); $shortId = end($parts); $layout = $dbQ['type']; $config = json_decode($dbQ['configJson'], true) ?: []; $q = [ 'id' => $shortId, 'layout' => $layout, 'question' => $dbQ['defaultText'], ]; if (isset($config['textKey'])) $q['textKey'] = $config['textKey']; if (isset($config['textKey1'])) $q['textKey1'] = $config['textKey1']; if (isset($config['textKey2'])) $q['textKey2'] = $config['textKey2']; if (isset($config['hint'])) $q['hint'] = $config['hint']; if (isset($config['hint1'])) $q['hint1'] = $config['hint1']; if (isset($config['hint2'])) $q['hint2'] = $config['hint2']; if (isset($config['symptoms'])) $q['symptoms'] = $config['symptoms']; if (isset($config['range'])) $q['range'] = $config['range']; if (isset($config['constraints'])) $q['constraints'] = $config['constraints']; if (isset($config['minSelection'])) $q['minSelection'] = $config['minSelection']; if ($layout === 'string_spinner' && isset($config['options'])) { $q['options'] = $config['options']; } if ($layout === 'value_spinner' && isset($config['valueOptions'])) { $q['options'] = $config['valueOptions']; } $ao = $pdo->prepare(" SELECT defaultText, points, nextQuestionId FROM answer_option WHERE questionID = :qid ORDER BY orderIndex "); $ao->execute([':qid' => $dbQ['questionID']]); $opts = $ao->fetchAll(PDO::FETCH_ASSOC); if ($opts) { $optionsArr = []; $pointsMap = []; foreach ($opts as $opt) { $o = ['key' => $opt['defaultText']]; if ($opt['nextQuestionId'] !== '') { $o['nextQuestionId'] = $opt['nextQuestionId']; } $optionsArr[] = $o; $pointsMap[$opt['defaultText']] = (int)$opt['points']; } $q['options'] = $optionsArr; $q['pointsMap'] = $pointsMap; } $questions[] = $q; } qdb_discard($tmpDb, $lockFp); json_success(["meta" => ["id" => $qnID], "questions" => $questions]); } // Default: ordered questionnaire list $rows = $pdo->query(" SELECT questionnaireID, name, showPoints, conditionJson FROM questionnaire WHERE state = 'active' ORDER BY orderIndex ")->fetchAll(PDO::FETCH_ASSOC); $list = []; foreach ($rows as $r) { $list[] = [ 'id' => $r['questionnaireID'], 'name' => $r['name'], 'showPoints' => (bool)(int)$r['showPoints'], 'condition' => json_decode($r['conditionJson'], true) ?: new stdClass(), ]; } qdb_discard($tmpDb, $lockFp); json_success($list);