require_once '../config/db.php'; require_once '../config/nav.php'; $rel = get_rel_path(); $filter_pos = isset($_GET['pos']) ? (decrypt_param($_GET['pos']) ?: $_GET['pos']) : 'President'; $filter_county = isset($_GET['county']) ? (decrypt_param($_GET['county']) ?: $_GET['county']) : null; $filter_constituency = isset($_GET['constituency']) ? (decrypt_param($_GET['constituency']) ?: $_GET['constituency']) : null; $filter_ward = isset($_GET['ward']) ? (decrypt_param($_GET['ward']) ?: $_GET['ward']) : null; // Aspirant Area Access Control $restricted_county = null; $restricted_constituency = null; $restricted_ward = null; $is_approved_aspirant = false; if (isset($_SESSION['aspirant_id'])) { if (isset($_SESSION['is_approved']) && $_SESSION['is_approved'] == 1) { $is_approved_aspirant = true; $restricted_county = $_SESSION['aspirant_county']; $restricted_constituency = $_SESSION['aspirant_constituency']; $restricted_ward = $_SESSION['aspirant_ward']; } } // Enforce restricted access for approved aspirants if ($is_approved_aspirant) { if ($restricted_ward) { $filter_ward = $restricted_ward; $filter_constituency = $restricted_constituency; $filter_county = $restricted_county; } elseif ($restricted_constituency) { $filter_constituency = $restricted_constituency; $filter_county = $restricted_county; } elseif ($restricted_county) { $filter_county = $restricted_county; } } // Group aspirants by position with filters try { $sql = "SELECT * FROM aspirants WHERE 1=1"; $params = []; if ($filter_pos) { $sql .= " AND position = :pos"; $params['pos'] = $filter_pos; } if ($filter_county) { $sql .= " AND county = :county"; $params['county'] = $filter_county; } if ($filter_constituency) { $sql .= " AND constituency = :constituency"; $params['constituency'] = $filter_constituency; } if ($filter_ward) { $sql .= " AND ward = :ward"; $params['ward'] = $filter_ward; } $sql .= " ORDER BY position ASC, id ASC"; $stmt = $pdo->prepare($sql); $stmt->execute($params); $all_aspirants = $stmt->fetchAll(); // Group aspirants by position AND region for accurate poll comparisons $grouped = []; foreach ($all_aspirants as $a) { // Create a unique key based on position and relevant geographic area $pos = $a['position']; switch ($pos) { case 'President': $region_label = 'National'; $key = 'President'; break; case 'Governor': case 'Senator': case 'Women Rep': $region_label = $a['county'] ?: 'Unknown'; $key = $pos . ' - ' . $region_label; break; case 'MP': $region_label = ($a['constituency'] ?: 'Unknown') . ' (' . ($a['county'] ?: 'Unknown') . ')'; $key = 'MP - ' . $region_label; break; case 'MCA': $region_label = ($a['ward'] ?: 'Unknown') . ', ' . ($a['constituency'] ?: 'Unknown'); $key = 'MCA - ' . $region_label; break; default: $region_label = 'Unknown'; $key = $pos . ' - ' . $region_label; } if (!isset($grouped[$key])) { $grouped[$key] = [ 'position' => $pos, 'region' => $region_label, 'aspirants' => [] ]; } $grouped[$key]['aspirants'][] = $a; } // Get vote counts $votes_stmt = $pdo->query("SELECT aspirant_id, COUNT(*) as count FROM votes GROUP BY aspirant_id"); $vote_data = $votes_stmt->fetchAll(PDO::FETCH_KEY_PAIR); // Identify which races the current user has already voted in (Fingerprint + IP) $device_fp = get_voter_fingerprint(); $ip = $_SERVER['REMOTE_ADDR']; $voted_races = []; $vcheck_stmt = $pdo->prepare("SELECT a.position, a.county, a.constituency, a.ward FROM votes v JOIN aspirants a ON v.aspirant_id = a.id WHERE v.device_fingerprint = ? OR v.voter_ip = ?"); $vcheck_stmt->execute([$device_fp, $ip]); while ($rv = $vcheck_stmt->fetch()) { $pos = $rv['position']; switch ($pos) { case 'President': $vkey = 'President'; break; case 'Governor': case 'Senator': case 'Women Rep': $vkey = $pos . ' - ' . ($rv['county'] ?: 'Unknown'); break; case 'MP': $vkey = 'MP - ' . ($rv['constituency'] ?: 'Unknown') . ' (' . ($rv['county'] ?: 'Unknown') . ')'; break; case 'MCA': $vkey = 'MCA - ' . ($rv['ward'] ?: 'Unknown') . ', ' . ($rv['constituency'] ?: 'Unknown'); break; default: $vkey = $pos . ' - Unknown'; } $voted_races[] = $vkey; } } catch (Exception $e) { // Error handling } // Check if user is registered/approved to see exact votes $can_see_exact_votes = isset($_SESSION['aspirant_id']) || isset($_SESSION['admin_id']); ?> Polls - KenyaDecides