Generating Survey Responses
We're creating synthetic responses for selection:
isLoggedIn()) { $_SESSION['error_message'] = 'Unauthorized'; redirectTo('projects.php'); exit; } $db = Database::getInstance(); $selection_id = isset($_GET['id']) ? (int)$_GET['id'] : 0; if (!$selection_id) { $_SESSION['error_message'] = 'Invalid selection ID'; redirectTo('projects.php'); exit; } // Get selection details with project information $stmt = $db->prepare(" SELECT s.*, p.id as project_id, p.title as project_title, DATE_FORMAT(s.created_at, '%b %d, %Y %H:%i') as formatted_date FROM selections s JOIN projects p ON s.project_id = p.id WHERE s.id = ? AND p.created_by = ? "); $stmt->bind_param('ii', $selection_id, $_SESSION['user_id']); $stmt->execute(); $selection = $stmt->get_result()->fetch_assoc(); if (!$selection) { $_SESSION['error_message'] = 'Selection not found or access denied'; redirectTo('projects.php'); exit; } // Get project's connected survey $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(); $connected_survey = $stmt->get_result()->fetch_assoc(); if (!$connected_survey) { $_SESSION['error_message'] = 'No survey connected to this project'; redirectTo("manage_project.php?id={$selection['project_id']}"); exit; } // Check if we've already generated responses $stmt = $db->prepare(" SELECT COUNT(*) as count FROM synthetic_responses WHERE selection_id = ? "); $stmt->bind_param('i', $selection_id); $stmt->execute(); $has_responses = $stmt->get_result()->fetch_assoc()['count'] > 0; if ($has_responses) { if (isset($_GET['force']) && $_GET['force'] == 1) { // Delete existing responses if force parameter is provided $stmt = $db->prepare("DELETE FROM synthetic_responses WHERE selection_id = ?"); $stmt->bind_param('i', $selection_id); $stmt->execute(); } else { $_SESSION['error_message'] = 'Responses already generated for this selection. Click here to regenerate.'; redirectTo("manage_project.php?id={$selection['project_id']}"); exit; } } // Initialize progress file $progress_file = sys_get_temp_dir() . '/syndia_progress_' . $selection_id . '.json'; $progress_data = [ 'progress' => 0, 'processed' => 0, 'success' => 0, 'error' => 0, 'total' => 0, 'status' => 'Initializing...', 'completed' => false ]; file_put_contents($progress_file, json_encode($progress_data)); // Start the background process $phpBinary = PHP_BINARY; $scriptPath = dirname(__FILE__) . '/generate_responses_worker.php'; $logPath = dirname(__FILE__) . '/logs'; // Create logs directory if it doesn't exist if (!file_exists($logPath)) { mkdir($logPath, 0777, true); } $logFile = $logPath . '/generate_responses_' . $selection_id . '.log'; $cmd = sprintf( 'nohup %s %s %d > %s 2>&1 & echo $!', escapeshellarg($phpBinary), escapeshellarg($scriptPath), $selection_id, escapeshellarg($logFile) ); error_log("Executing command: $cmd"); $output = []; exec($cmd, $output, $returnVal); error_log("Command execution result - Return value: $returnVal, Output: " . print_r($output, true)); // Create progress tracking page ?>
We're creating synthetic responses for selection: