Check_Auth(); if ($_SESSION['Access_Level'] < 1) { header('Location: Voting_Screen.php'); exit; } $db = db(); // Handle New Election Creation if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['create_election'])) { $year = $_POST['election_year'] ?? date('Y'); // Default configuration for a new election $defaultParties = json_encode(['Independent']); $defaultPositions = json_encode([ ['name' => 'President', 'type' => 'Uniform'], ['name' => 'Vice President', 'type' => 'Uniform'], ['name' => 'Secretary', 'type' => 'Uniform'], ['name' => 'Treasurer', 'type' => 'Uniform'] ]); $startDate = date('Y-m-d H:i:s'); $endDate = date('Y-m-d H:i:s', strtotime('+7 days')); try { $stmt = $db->prepare("INSERT INTO Election_History (Year, Parties, Positions, Status, Start_Date, End_Date, Total_Voters) VALUES (?, ?, ?, ?, ?, ?, ?)"); $stmt->execute([$year, $defaultParties, $defaultPositions, 'Preparing', $startDate, $endDate, 0]); $Auth->Log_Action($_SESSION['User_ID'], $_SESSION['User_Role'], 'Create New Election', "Created a new election for School Year $year"); header("Location: Election_Dashboard.php?success=New election created successfully"); exit; } catch (Exception $e) { header("Location: Election_Dashboard.php?error=Error creating election: " . $e->getMessage()); exit; } } // Handle Status Change if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_status'])) { $newStatus = $_POST['new_status']; $electionId = $_POST['election_id']; // Fetch current status $currentElectionStmt = $db->prepare("SELECT * FROM Election_History WHERE Election_ID = ?"); $currentElectionStmt->execute([$electionId]); $electionData = $currentElectionStmt->fetch(); if ($newStatus === 'Finished') { // Save current details to history record $totalVoters = $db->query("SELECT COUNT(*) FROM Voters")->fetchColumn(); $votedCount = $db->query("SELECT COUNT(*) FROM Voters WHERE Has_Voted = 1")->fetchColumn(); // Fetch Candidates and potentially calculate results if there was a voting table $candidates = $db->query("SELECT Name, Position, Party FROM Candidates")->fetchAll(PDO::FETCH_ASSOC); $stmt = $db->prepare("UPDATE Election_History SET Status = ?, Total_Voters = ?, Results = ? WHERE Election_ID = ?"); $stmt->execute([$newStatus, $totalVoters, json_encode($candidates), $electionId]); } else { $stmt = $db->prepare("UPDATE Election_History SET Status = ? WHERE Election_ID = ?"); $stmt->execute([$newStatus, $electionId]); } // Log to Audit Trail $year = $electionData['Year'] ?? 'Unknown'; $oldStatus = $electionData['Status'] ?? 'Unknown'; $Auth->Log_Action($_SESSION['User_ID'], $_SESSION['User_Role'], 'Update Election Status', "Changed SY $year Election status from $oldStatus to $newStatus"); header("Location: Election_Dashboard.php"); exit; } // Fetch Stats $totalVoters = $db->query("SELECT COUNT(*) FROM Voters")->fetchColumn(); $totalCandidates = $db->query("SELECT COUNT(*) FROM Candidates")->fetchColumn(); $totalVotes = $db->query("SELECT COUNT(*) FROM Voters WHERE Has_Voted = 1")->fetchColumn(); // Fetch Active/Preparing/Ongoing Elections $activeElections = $db->query("SELECT * FROM Election_History WHERE Status IN ('Preparing', 'Ongoing', 'Active') ORDER BY Start_Date DESC")->fetchAll(); ?> Election Dashboard | Online School Election System

Election Dashboard

Welcome back! Here's what's happening with the current elections.

Total Voters
Registered Students
Total Candidates
Running for Office
Total Votes Cast
Verified Ballots

Active & Upcoming Elections

Election Title Period Status Quick Actions
No active elections found. Create one now.
School Year Election -