isLoggedIn()) { die('Unauthorized'); } $db = Database::getInstance(); $selection_id = isset($_GET['selection_id']) ? (int)$_GET['selection_id'] : 29; // Default to 29 echo "

Debug: Synthetic Responses for Selection $selection_id

"; // Check responses in synthetic_responses table echo "

1. Synthetic Responses Table

"; $stmt = $db->prepare(" SELECT sr.*, sq.question_text FROM synthetic_responses sr LEFT JOIN survey_questions sq ON sr.question_id = sq.id WHERE sr.selection_id = ? ORDER BY sr.panelist_id, sr.question_id LIMIT 20 "); $stmt->bind_param('i', $selection_id); $stmt->execute(); $result = $stmt->get_result(); if ($result->num_rows > 0) { echo ""; echo ""; while ($row = $result->fetch_assoc()) { echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; } echo "
IDPanelist IDQuestion IDResponse TextQuestionCreated
" . htmlspecialchars($row['id']) . "" . htmlspecialchars($row['panelist_id']) . "" . htmlspecialchars($row['question_id']) . "" . htmlspecialchars(substr($row['response_text'], 0, 100)) . "..." . htmlspecialchars(substr($row['question_text'] ?? 'N/A', 0, 50)) . "..." . htmlspecialchars($row['created_at']) . "
"; // Count total responses $count_stmt = $db->prepare("SELECT COUNT(*) as total FROM synthetic_responses WHERE selection_id = ?"); $count_stmt->bind_param('i', $selection_id); $count_stmt->execute(); $total = $count_stmt->get_result()->fetch_assoc()['total']; echo "

Total responses found: $total

"; // Count "No response provided" entries $no_response_stmt = $db->prepare("SELECT COUNT(*) as no_response FROM synthetic_responses WHERE selection_id = ? AND response_text = 'No response provided'"); $no_response_stmt->bind_param('i', $selection_id); $no_response_stmt->execute(); $no_response_count = $no_response_stmt->get_result()->fetch_assoc()['no_response']; echo "

\"No response provided\" entries: $no_response_count

"; if ($no_response_count > 0) { echo "

โš ๏ธ Warning: Found $no_response_count placeholder responses that need to be regenerated

"; } } else { echo "

No responses found in synthetic_responses table for selection $selection_id

"; } // Check selection details echo "

2. Selection Details

"; $stmt = $db->prepare(" SELECT s.*, p.title as project_title FROM selections s JOIN projects p ON s.project_id = p.id WHERE s.id = ? "); $stmt->bind_param('i', $selection_id); $stmt->execute(); $selection = $stmt->get_result()->fetch_assoc(); if ($selection) { echo "

Selection: " . htmlspecialchars($selection['name']) . "

"; echo "

Project: " . htmlspecialchars($selection['project_title']) . "

"; echo "

Sample Size: " . htmlspecialchars($selection['sample_size']) . "

"; } else { echo "

Selection not found!

"; } // Check connected survey echo "

3. Connected Survey

"; if (isset($selection['project_id'])) { $stmt = $db->prepare(" SELECT s.id as survey_id, s.title as survey_title FROM project_surveys ps JOIN surveys s ON ps.survey_id = s.id WHERE ps.project_id = ? LIMIT 1 "); $stmt->bind_param('i', $selection['project_id']); $stmt->execute(); $survey = $stmt->get_result()->fetch_assoc(); if ($survey) { echo "

Survey: " . htmlspecialchars($survey['survey_title']) . " (ID: " . $survey['survey_id'] . ")

"; // Count questions $q_stmt = $db->prepare(" SELECT COUNT(*) as question_count FROM survey_questions WHERE survey_id = ? AND question_type NOT IN ('section_header', 'descriptive_text', 'page_break') "); $q_stmt->bind_param('i', $survey['survey_id']); $q_stmt->execute(); $q_count = $q_stmt->get_result()->fetch_assoc()['question_count']; echo "

Answerable Questions: $q_count

"; // Show first few questions $q_stmt = $db->prepare(" SELECT id, question_text, question_type FROM survey_questions WHERE survey_id = ? AND question_type NOT IN ('section_header', 'descriptive_text', 'page_break') ORDER BY question_order ASC LIMIT 5 "); $q_stmt->bind_param('i', $survey['survey_id']); $q_stmt->execute(); $sample_questions = $q_stmt->get_result(); echo "

Sample Questions:

"; echo "
    "; while ($q = $sample_questions->fetch_assoc()) { echo "
  1. " . htmlspecialchars(substr($q['question_text'], 0, 100)) . "... (" . $q['question_type'] . ")
  2. "; } echo "
"; } else { echo "

No survey connected!

"; } } // Check selection members echo "

4. Selection Members

"; $stmt = $db->prepare(" SELECT COUNT(*) as member_count FROM selection_members WHERE selection_id = ? "); $stmt->bind_param('i', $selection_id); $stmt->execute(); $member_count = $stmt->get_result()->fetch_assoc()['member_count']; echo "

Total Members: $member_count

"; // Show sample members $stmt = $db->prepare(" SELECT sm.panelist_id FROM selection_members sm WHERE sm.selection_id = ? LIMIT 5 "); $stmt->bind_param('i', $selection_id); $stmt->execute(); $members = $stmt->get_result(); echo "

Sample Member IDs: "; $member_ids = []; while ($member = $members->fetch_assoc()) { $member_ids[] = $member['panelist_id']; } echo implode(', ', $member_ids) . "

"; // Check if responses exist for these specific members if (!empty($member_ids)) { echo "

5. Response Check for Sample Members

"; foreach (array_slice($member_ids, 0, 3) as $panelist_id) { $resp_stmt = $db->prepare(" SELECT COUNT(*) as resp_count FROM synthetic_responses WHERE selection_id = ? AND panelist_id = ? "); $resp_stmt->bind_param('is', $selection_id, $panelist_id); $resp_stmt->execute(); $resp_count = $resp_stmt->get_result()->fetch_assoc()['resp_count']; echo "

Member $panelist_id: $resp_count responses

"; // Show actual responses for this member if ($resp_count > 0) { $sample_resp_stmt = $db->prepare(" SELECT sr.response_text, sq.question_text FROM synthetic_responses sr LEFT JOIN survey_questions sq ON sr.question_id = sq.id WHERE sr.selection_id = ? AND sr.panelist_id = ? LIMIT 3 "); $sample_resp_stmt->bind_param('is', $selection_id, $panelist_id); $sample_resp_stmt->execute(); $sample_responses = $sample_resp_stmt->get_result(); echo ""; } } } // Check for recent generation activity echo "

6. Recent Generation Activity

"; $progress_file = sys_get_temp_dir() . '/syndia_progress_' . $selection_id . '.json'; if (file_exists($progress_file)) { $progress_data = json_decode(file_get_contents($progress_file), true); echo "

Last Generation Status: " . htmlspecialchars($progress_data['status'] ?? 'Unknown') . "

"; echo "

Progress: " . htmlspecialchars($progress_data['progress'] ?? 0) . "%

"; echo "

Timestamp: " . date('Y-m-d H:i:s', $progress_data['timestamp'] ?? 0) . "

"; } else { echo "

No recent progress file found.

"; } // Summary and recommendations echo "

7. Summary & Recommendations

"; if ($total_responses > 0) { if ($no_response_count > ($total_responses * 0.5)) { echo "
"; echo "

๐Ÿšจ Issue Found

"; echo "

More than 50% of responses are placeholders (\"No response provided\"). This indicates the GPT response parsing failed.

"; echo "

Action needed: Re-generate responses with the improved worker script.

"; echo "
"; } else if ($no_response_count > 0) { echo "
"; echo "

โš ๏ธ Partial Issue

"; echo "

Some responses are placeholders. The generation partially worked but could be improved.

"; echo "

Suggestion: Consider re-generating for better results.

"; echo "
"; } else { echo "
"; echo "

โœ… Looks Good

"; echo "

All responses appear to contain actual content. Export should work properly.

"; echo "
"; } } else { echo "
"; echo "

โŒ No Responses Found

"; echo "

No responses found in database. Need to generate responses first.

"; echo "
"; } echo "
"; echo "
"; echo "

Quick Actions:

"; echo "

"; echo "๐Ÿงช Test Single Response "; echo "๐Ÿ”„ Re-generate All "; echo "๐Ÿ“Š Try Export"; echo "

"; echo "
"; echo "

Debug completed at " . date('Y-m-d H:i:s') . "

"; ?>