isLoggedIn()) { throw new Exception('Unauthorized'); } $projectId = isset($_GET['project_id']) ? (int)$_GET['project_id'] : null; if (!$projectId) { throw new Exception('Project ID is required'); } $db = Database::getInstance(); // Check if the project exists and user has access $stmt = $db->prepare("SELECT id FROM projects WHERE id = ? AND created_by = ?"); $stmt->bind_param('ii', $projectId, $_SESSION['user_id']); $stmt->execute(); if ($stmt->get_result()->num_rows == 0) { throw new Exception('Project not found or access denied'); } // Get surveys that are connected to this project with additional data $sql = "SELECT ps.id as connection_id, s.*, (SELECT COUNT(DISTINCT q.id) FROM survey_questions q WHERE q.survey_id = s.id) as questions_count, (SELECT COUNT(DISTINCT t.id) FROM survey_tokens t WHERE t.survey_id = s.id) as tokens_count, (SELECT COUNT(DISTINCT t.id) FROM survey_tokens t WHERE t.survey_id = s.id AND t.is_completed = 1) as responses_count FROM project_surveys ps JOIN surveys s ON ps.survey_id = s.id WHERE ps.project_id = ? ORDER BY ps.created_at DESC"; $stmt = $db->prepare($sql); $stmt->bind_param('i', $projectId); $stmt->execute(); $result = $stmt->get_result(); $surveys = []; while ($row = $result->fetch_assoc()) { $surveys[] = $row; } echo json_encode([ 'success' => true, 'surveys' => $surveys ]); } catch (Exception $e) { http_response_code(500); echo json_encode([ 'success' => false, 'message' => $e->getMessage() ]); } ?>