179 lines
5.7 KiB
PHP
179 lines
5.7 KiB
PHP
<?php
|
|
|
|
class QuestionnaireRepo
|
|
{
|
|
/**
|
|
* @return list<array{
|
|
* questionnaireID: string,
|
|
* name: string,
|
|
* version: string,
|
|
* state: string,
|
|
* orderIndex: int,
|
|
* showPoints: int,
|
|
* conditionJson: string,
|
|
* questionCount: int
|
|
* }>
|
|
*/
|
|
public static function listAll(PDO $pdo): array
|
|
{
|
|
$rows = $pdo->query("
|
|
SELECT q.questionnaireID, q.name, q.version, q.state,
|
|
q.orderIndex, q.showPoints, q.conditionJson,
|
|
COUNT(qu.questionID) AS questionCount
|
|
FROM questionnaire q
|
|
LEFT JOIN question qu ON qu.questionnaireID = q.questionnaireID
|
|
GROUP BY q.questionnaireID
|
|
ORDER BY q.orderIndex, q.name
|
|
")->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
foreach ($rows as &$r) {
|
|
$r['orderIndex'] = (int) $r['orderIndex'];
|
|
$r['showPoints'] = (int) $r['showPoints'];
|
|
$r['conditionJson'] = $r['conditionJson'] ?: '{}';
|
|
$r['questionCount'] = (int) $r['questionCount'];
|
|
}
|
|
unset($r);
|
|
|
|
return $rows;
|
|
}
|
|
|
|
/**
|
|
* @return list<array{
|
|
* id: string,
|
|
* name: string,
|
|
* showPoints: bool,
|
|
* condition: array|stdClass
|
|
* }>
|
|
*/
|
|
public static function listActive(PDO $pdo): array
|
|
{
|
|
$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(),
|
|
];
|
|
}
|
|
|
|
return $list;
|
|
}
|
|
|
|
/**
|
|
* @return array<string, mixed>|null
|
|
*/
|
|
public static function findById(PDO $pdo, string $id): ?array
|
|
{
|
|
$stmt = $pdo->prepare('SELECT * FROM questionnaire WHERE questionnaireID = :id');
|
|
$stmt->execute([':id' => $id]);
|
|
$row = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
return $row ?: null;
|
|
}
|
|
|
|
public static function create(
|
|
PDO $pdo,
|
|
string $id,
|
|
string $name,
|
|
string $version,
|
|
string $state,
|
|
int $orderIndex,
|
|
int $showPoints,
|
|
string $conditionJson
|
|
): void {
|
|
$stmt = $pdo->prepare('
|
|
INSERT INTO questionnaire (questionnaireID, name, version, state, orderIndex, showPoints, conditionJson)
|
|
VALUES (:id, :n, :v, :s, :o, :sp, :cj)
|
|
');
|
|
$stmt->execute([
|
|
':id' => $id,
|
|
':n' => $name,
|
|
':v' => $version,
|
|
':s' => $state,
|
|
':o' => $orderIndex,
|
|
':sp' => $showPoints,
|
|
':cj' => $conditionJson,
|
|
]);
|
|
}
|
|
|
|
public static function update(
|
|
PDO $pdo,
|
|
string $id,
|
|
string $name,
|
|
string $version,
|
|
string $state,
|
|
int $orderIndex,
|
|
int $showPoints,
|
|
string $conditionJson
|
|
): void {
|
|
$stmt = $pdo->prepare('
|
|
UPDATE questionnaire
|
|
SET name = :n, version = :v, state = :s,
|
|
orderIndex = :o, showPoints = :sp, conditionJson = :cj
|
|
WHERE questionnaireID = :id
|
|
');
|
|
$stmt->execute([
|
|
':n' => $name,
|
|
':v' => $version,
|
|
':s' => $state,
|
|
':o' => $orderIndex,
|
|
':sp' => $showPoints,
|
|
':cj' => $conditionJson,
|
|
':id' => $id,
|
|
]);
|
|
}
|
|
|
|
public static function delete(PDO $pdo, string $id): void
|
|
{
|
|
$pdo->exec('PRAGMA foreign_keys = OFF;');
|
|
try {
|
|
$pdo->beginTransaction();
|
|
|
|
$qIds = $pdo->prepare('SELECT questionID FROM question WHERE questionnaireID = :id');
|
|
$qIds->execute([':id' => $id]);
|
|
$questionIDs = $qIds->fetchAll(PDO::FETCH_COLUMN);
|
|
|
|
foreach ($questionIDs as $qid) {
|
|
$aoIds = $pdo->prepare('SELECT answerOptionID FROM answer_option WHERE questionID = :qid');
|
|
$aoIds->execute([':qid' => $qid]);
|
|
$optionIDs = $aoIds->fetchAll(PDO::FETCH_COLUMN);
|
|
|
|
foreach ($optionIDs as $aoid) {
|
|
$delAot = $pdo->prepare('DELETE FROM answer_option_translation WHERE answerOptionID = :id');
|
|
$delAot->execute([':id' => $aoid]);
|
|
}
|
|
|
|
$pdo->prepare('DELETE FROM answer_option WHERE questionID = :qid')->execute([':qid' => $qid]);
|
|
$pdo->prepare('DELETE FROM question_translation WHERE questionID = :qid')->execute([':qid' => $qid]);
|
|
$pdo->prepare('DELETE FROM client_answer WHERE questionID = :qid')->execute([':qid' => $qid]);
|
|
}
|
|
|
|
$pdo->prepare('DELETE FROM question WHERE questionnaireID = :id')->execute([':id' => $id]);
|
|
$pdo->prepare('DELETE FROM completed_questionnaire WHERE questionnaireID = :id')->execute([':id' => $id]);
|
|
$pdo->prepare('DELETE FROM questionnaire WHERE questionnaireID = :id')->execute([':id' => $id]);
|
|
|
|
$pdo->commit();
|
|
} catch (Throwable $e) {
|
|
if ($pdo->inTransaction()) {
|
|
$pdo->rollBack();
|
|
}
|
|
throw $e;
|
|
} finally {
|
|
$pdo->exec('PRAGMA foreign_keys = ON;');
|
|
}
|
|
}
|
|
|
|
public static function nextOrderIndex(PDO $pdo): int
|
|
{
|
|
return (int) $pdo->query('SELECT COALESCE(MAX(orderIndex),0)+1 FROM questionnaire')->fetchColumn();
|
|
}
|
|
}
|