Check_Auth(); if ($_SESSION['Access_Level'] < 1) { header('Location: Voting_Screen.php'); exit; } $db = db(); // Fetch latest election $election = $db->query("SELECT * FROM Election_History ORDER BY Election_ID DESC LIMIT 1")->fetch(); $electionStatus = $election['Status'] ?? 'Preparing'; $electionId = $election['Election_ID'] ?? 0; // Parse Parties and Positions $electionParties = json_decode($election['Parties'] ?? '[]', true) ?: []; $electionPositionsRaw = json_decode($election['Positions'] ?? '[]', true) ?: []; // Normalize Positions (Ensure they are objects with name and type) $electionPositions = []; $migrationNeeded = false; foreach ($electionPositionsRaw as $p) { if (is_array($p) && isset($p['name'])) { $electionPositions[] = $p; } else { $electionPositions[] = ['name' => (string)$p, 'type' => 'Uniform']; $migrationNeeded = true; } } if ($migrationNeeded && $electionId) { $stmt = $db->prepare("UPDATE Election_History SET Positions = ? WHERE Election_ID = ?"); $stmt->execute([json_encode($electionPositions), $electionId]); } // Handle POST actions for Editors (Preparing Status) if ($_SERVER['REQUEST_METHOD'] === 'POST' && $electionStatus === 'Preparing') { if (isset($_POST['action'])) { $changed = false; $logAction = ""; $logDetails = ""; if ($_POST['action'] === 'add_position' && !empty($_POST['new_position'])) { $newPosName = trim($_POST['new_position']); $newPosType = $_POST['position_type'] ?? 'Uniform'; $exists = false; foreach ($electionPositions as $p) { if ($p['name'] === $newPosName) { $exists = true; break; } } if (!$exists) { $electionPositions[] = ['name' => $newPosName, 'type' => $newPosType]; $changed = true; $logAction = "Add Position"; $logDetails = "Added new position: $newPosName ($newPosType)"; } } elseif ($_POST['action'] === 'remove_position') { $posNameToRemove = $_POST['position']; $electionPositions = array_values(array_filter($electionPositions, fn($p) => $p['name'] !== $posNameToRemove)); $changed = true; $logAction = "Remove Position"; $logDetails = "Removed position: $posNameToRemove"; } elseif ($_POST['action'] === 'add_party' && !empty($_POST['new_party'])) { $newParty = trim($_POST['new_party']); if (!in_array($newParty, $electionParties)) { $electionParties[] = $newParty; $changed = true; $logAction = "Add Party"; $logDetails = "Added new political party: $newParty"; } } elseif ($_POST['action'] === 'remove_party') { $partyToRemove = $_POST['party']; $electionParties = array_values(array_filter($electionParties, fn($p) => $p !== $partyToRemove)); $changed = true; $logAction = "Remove Party"; $logDetails = "Removed political party: $partyToRemove"; } if ($changed) { $stmt = $db->prepare("UPDATE Election_History SET Parties = ?, Positions = ? WHERE Election_ID = ?"); $stmt->execute([json_encode($electionParties), json_encode($electionPositions), $electionId]); // Log to Audit Trail $Auth->Log_Action($_SESSION['User_ID'], $_SESSION['User_Role'], $logAction, $logDetails); header("Location: Candidates.php"); exit; } } } // Fetch all available tracks from Voters table $availableTracks = $db->query("SELECT DISTINCT Track_Cluster FROM Voters WHERE Track_Cluster IS NOT NULL AND Track_Cluster != '' ORDER BY Track_Cluster")->fetchAll(PDO::FETCH_COLUMN); // Filters (for Ongoing/Finished) $search = $_GET['search'] ?? ''; $filterPosition = $_GET['position'] ?? ''; $filterParty = $_GET['party'] ?? ''; $filterGrade = $_GET['grade'] ?? ''; // Fetch Stats $totalCandidates = $db->query("SELECT COUNT(*) FROM Candidates")->fetchColumn(); $positionsCount = count($electionPositions) ?: $db->query("SELECT COUNT(DISTINCT Position) FROM Candidates")->fetchColumn(); $partiesCount = count($electionParties) ?: $db->query("SELECT COUNT(DISTINCT Party) FROM Candidates")->fetchColumn(); // Fetch Breakdowns $positionsBreakdown = $db->query("SELECT Position as label, COUNT(*) as value FROM Candidates GROUP BY Position ORDER BY value DESC")->fetchAll(); $partiesBreakdown = $db->query("SELECT Party as label, COUNT(*) as value FROM Candidates GROUP BY Party ORDER BY value DESC")->fetchAll(); $grades = $db->query("SELECT Grade_Level as label, COUNT(*) as value FROM Candidates GROUP BY Grade_Level ORDER BY Grade_Level ASC")->fetchAll(); // Build Query for List $queryStr = "SELECT * FROM Candidates WHERE 1=1"; $params = []; if ($search) { $queryStr .= " AND (Name LIKE ? OR Email LIKE ? OR Party LIKE ?)"; $params[] = "%$search%"; $params[] = "%$search%"; $params[] = "%$search%"; } if ($filterPosition) { $queryStr .= " AND Position = ?"; $params[] = $filterPosition; } if ($filterParty) { $queryStr .= " AND Party = ?"; $params[] = $filterParty; } if ($filterGrade) { $queryStr .= " AND Grade_Level = ?"; $params[] = $filterGrade; } $queryStr .= " ORDER BY Position ASC, Name ASC"; $stmt = $db->prepare($queryStr); $stmt->execute($params); $candidatesList = $stmt->fetchAll(); ?> Candidate Management | Online School Election System

Candidate Management

Managing SY Election

Define Positions

Define Parties

Register New Candidate

Total Candidates
Unique Positions
Active Parties

Candidates by Position

No data available

Candidates by Party

No data available

Candidates by Grade

No data available

Grade
Candidate Position Party Grade/Track Actions
No candidates found matching your criteria.
|
Grade
Showing of candidates