34760-vm/api.php
Flatlogic Bot d47e350516 v1
2025-10-07 16:17:37 +00:00

64 lines
2.4 KiB
PHP

<?php
header('Content-Type: application/json');
session_start();
require_once 'db/config.php';
// Ensure admin is logged in
if (!isset($_SESSION['user_id']) || !in_array('Admin', $_SESSION['user_roles'])) {
echo json_encode(['error' => 'Unauthorized']);
exit;
}
$action = $_GET['action'] ?? '';
switch ($action) {
case 'submissions_per_survey':
$stmt = db()->query("SELECT s.title, COUNT(fs.id) as submission_count FROM surveys s LEFT JOIN feedback_submissions fs ON s.id = fs.survey_id GROUP BY s.id");
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
$labels = array_column($results, 'title');
$values = array_column($results, 'submission_count');
echo json_encode(['labels' => $labels, 'values' => $values]);
break;
case 'survey_question_analytics':
$surveys_stmt = db()->query("SELECT * FROM surveys");
$surveys = $surveys_stmt->fetchAll(PDO::FETCH_ASSOC);
$response = [];
foreach ($surveys as $survey) {
$questions_stmt = db()->prepare("SELECT * FROM survey_questions WHERE survey_id = ? AND (question_type = 'rating' OR question_type = 'multiple-choice')");
$questions_stmt->execute([$survey['id']]);
$questions = $questions_stmt->fetchAll(PDO::FETCH_ASSOC);
$survey_data = [
'id' => $survey['id'],
'title' => $survey['title'],
'questions' => []
];
foreach ($questions as $question) {
$answers_stmt = db()->prepare("SELECT answer_text, COUNT(*) as count FROM survey_answers WHERE question_id = ? GROUP BY answer_text");
$answers_stmt->execute([$question['id']]);
$answers = $answers_stmt->fetchAll(PDO::FETCH_ASSOC);
$question_data = [
'id' => $question['id'],
'question_text' => $question['question_text'],
'type' => $question['question_type'],
'answers' => [
'labels' => array_column($answers, 'answer_text'),
'values' => array_column($answers, 'count')
]
];
$survey_data['questions'][] = $question_data;
}
$response[] = $survey_data;
}
echo json_encode($response);
break;
default:
echo json_encode(['error' => 'Invalid action']);
break;
}