added prototype dev settings with test import and db wipe
This commit is contained in:
87
common.php
87
common.php
@ -467,6 +467,93 @@ function qdb_option_key(array $aoRow): string {
|
||||
return qdb_is_stable_key($dt) ? $dt : '';
|
||||
}
|
||||
|
||||
/** Human-readable text for glass-scale answers stored as JSON on the parent question row. */
|
||||
function qdb_format_glass_answer_json(?string $raw): string
|
||||
{
|
||||
if ($raw === null || trim($raw) === '') {
|
||||
return '';
|
||||
}
|
||||
$decoded = json_decode($raw, true);
|
||||
if (!is_array($decoded)) {
|
||||
return $raw;
|
||||
}
|
||||
$parts = [];
|
||||
foreach ($decoded as $key => $val) {
|
||||
$val = trim((string)$val);
|
||||
if ($val === '') {
|
||||
continue;
|
||||
}
|
||||
$parts[] = $key . ': ' . $val;
|
||||
}
|
||||
return implode('; ', $parts);
|
||||
}
|
||||
|
||||
/**
|
||||
* Map glass symptom string keys to parent questionID for one questionnaire.
|
||||
*
|
||||
* @return array<string, string> symptomKey => parentQuestionID
|
||||
*/
|
||||
function qdb_glass_symptom_parent_map(PDO $pdo, string $qnID): array
|
||||
{
|
||||
$stmt = $pdo->prepare(
|
||||
"SELECT questionID, configJson FROM question WHERE questionnaireID = :qn AND type = 'glass_scale_question'"
|
||||
);
|
||||
$stmt->execute([':qn' => $qnID]);
|
||||
$map = [];
|
||||
foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
|
||||
$cfg = json_decode($row['configJson'] ?? '{}', true) ?: [];
|
||||
foreach ($cfg['symptoms'] ?? [] as $symptomKey) {
|
||||
$sk = trim((string)$symptomKey);
|
||||
if ($sk !== '') {
|
||||
$map[$sk] = $row['questionID'];
|
||||
}
|
||||
}
|
||||
}
|
||||
return $map;
|
||||
}
|
||||
|
||||
/** Merge symptom => label selections into JSON for one glass-scale parent question. */
|
||||
function qdb_merge_glass_symptom_json(?string $existingJson, array $symptomLabels): string
|
||||
{
|
||||
$data = [];
|
||||
if ($existingJson !== null && trim($existingJson) !== '') {
|
||||
$decoded = json_decode($existingJson, true);
|
||||
if (is_array($decoded)) {
|
||||
$data = $decoded;
|
||||
}
|
||||
}
|
||||
foreach ($symptomLabels as $key => $label) {
|
||||
$k = trim((string)$key);
|
||||
$v = trim((string)$label);
|
||||
if ($k !== '' && $v !== '') {
|
||||
$data[$k] = $v;
|
||||
}
|
||||
}
|
||||
return json_encode($data, JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
|
||||
/** Format one client_answer row for results table / CSV export. */
|
||||
function qdb_format_client_answer_display(array $question, ?array $answerRow, array $optionTextMap): string
|
||||
{
|
||||
if (!$answerRow) {
|
||||
return '';
|
||||
}
|
||||
if (($question['type'] ?? '') === 'glass_scale_question') {
|
||||
return qdb_format_glass_answer_json($answerRow['freeTextValue'] ?? null);
|
||||
}
|
||||
$aoid = $answerRow['answerOptionID'] ?? null;
|
||||
if ($aoid && isset($optionTextMap[$aoid])) {
|
||||
return (string)$optionTextMap[$aoid];
|
||||
}
|
||||
if (!empty($answerRow['freeTextValue'])) {
|
||||
return (string)$answerRow['freeTextValue'];
|
||||
}
|
||||
if (isset($answerRow['numericValue']) && $answerRow['numericValue'] !== null && $answerRow['numericValue'] !== '') {
|
||||
return (string)$answerRow['numericValue'];
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
function qdb_note_before_key(string $questionKey): string {
|
||||
return $questionKey . '_note_before';
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user