diff --git a/api/add_voter.php b/api/add_voter.php index bb6e2bf..5145885 100644 --- a/api/add_voter.php +++ b/api/add_voter.php @@ -12,6 +12,7 @@ if ($_SERVER["REQUEST_METHOD"] === "POST") { $password = $_POST["password"] ?? "iloilohns"; $track = $_POST["track"] ?? ""; $grade_level = $_POST["grade_level"] ?? ""; + $section = $_POST["section"] ?? ""; if (!$election_id || !$student_id || !$name || !$email) { die("Missing fields"); @@ -28,9 +29,9 @@ if ($_SERVER["REQUEST_METHOD"] === "POST") { if ($existing) { $user_id = $existing["id"]; - // Update track/grade if needed - $upd = $pdo->prepare("UPDATE users SET track = ?, grade_level = ? WHERE id = ?"); - $upd->execute([$track, $grade_level, $user_id]); + // Update track/grade/section if needed + $upd = $pdo->prepare("UPDATE users SET track = ?, grade_level = ?, section = ? WHERE id = ?"); + $upd->execute([$track, $grade_level, $section, $user_id]); } else { // 1a. Create user in Supabase $supabaseUser = SupabaseAuth::createUser($email, $password); @@ -49,8 +50,8 @@ if ($_SERVER["REQUEST_METHOD"] === "POST") { // Create new user locally $user_id = uuid(); - $stmt = $pdo->prepare("INSERT INTO users (id, supabase_uid, student_id, name, email, track, grade_level, role) VALUES (?, ?, ?, ?, ?, ?, ?, 'Voter')"); - $stmt->execute([$user_id, $supabase_uid, $student_id, $name, $email, $track, $grade_level]); + $stmt = $pdo->prepare("INSERT INTO users (id, supabase_uid, student_id, name, email, track, grade_level, section, role) VALUES (?, ?, ?, ?, ?, ?, ?, ?, 'Voter')"); + $stmt->execute([$user_id, $supabase_uid, $student_id, $name, $email, $track, $grade_level, $section]); } // 2. Assign to election diff --git a/api/import_voters.php b/api/import_voters.php index 2931105..7456764 100644 --- a/api/import_voters.php +++ b/api/import_voters.php @@ -42,6 +42,7 @@ if ($_SERVER["REQUEST_METHOD"] === "POST") { $email = trim($data[2]); $track = trim($data[3]); $grade_level = trim($data[4]); + $section = trim($data[5] ?? ""); if (!$student_id || !$name || !$email) continue; @@ -52,9 +53,9 @@ if ($_SERVER["REQUEST_METHOD"] === "POST") { if ($existing) { $user_id = $existing["id"]; - // Update track/grade if needed - $upd = $pdo->prepare("UPDATE users SET track = ?, grade_level = ? WHERE id = ?"); - $upd->execute([$track, $grade_level, $user_id]); + // Update track/grade/section if needed + $upd = $pdo->prepare("UPDATE users SET track = ?, grade_level = ?, section = ? WHERE id = ?"); + $upd->execute([$track, $grade_level, $section, $user_id]); $updated++; } else { // 1a. Create user in Supabase @@ -75,8 +76,8 @@ if ($_SERVER["REQUEST_METHOD"] === "POST") { // Create new user locally $user_id = uuid(); - $stmt = $pdo->prepare("INSERT INTO users (id, supabase_uid, student_id, name, email, track, grade_level, role) VALUES (?, ?, ?, ?, ?, ?, ?, 'Voter')"); - $stmt->execute([$user_id, $supabase_uid, $student_id, $name, $email, $track, $grade_level]); + $stmt = $pdo->prepare("INSERT INTO users (id, supabase_uid, student_id, name, email, track, grade_level, section, role) VALUES (?, ?, ?, ?, ?, ?, ?, ?, 'Voter')"); + $stmt->execute([$user_id, $supabase_uid, $student_id, $name, $email, $track, $grade_level, $section]); $imported++; } diff --git a/api/update_voter.php b/api/update_voter.php index 53b5a54..7d14d72 100644 --- a/api/update_voter.php +++ b/api/update_voter.php @@ -9,6 +9,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['id'])) { $email = $_POST['email']; $track = $_POST['track']; $gradeLevel = $_POST['grade_level']; + $section = $_POST['section'] ?? ''; $password = $_POST['password'] ?? ''; $pdo = db(); @@ -23,11 +24,11 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['id'])) { SupabaseAuth::updateUserPassword($userRecord['supabase_uid'], $password); } - $stmt = $pdo->prepare("UPDATE users SET name = ?, student_id = ?, email = ?, track = ?, grade_level = ? WHERE id = ?"); - $stmt->execute([$name, $studentId, $email, $track, $gradeLevel, $userId]); + $stmt = $pdo->prepare("UPDATE users SET name = ?, student_id = ?, email = ?, track = ?, grade_level = ?, section = ? WHERE id = ?"); + $stmt->execute([$name, $studentId, $email, $track, $gradeLevel, $section, $userId]); } else { - $stmt = $pdo->prepare("UPDATE users SET name = ?, student_id = ?, email = ?, track = ?, grade_level = ? WHERE id = ?"); - $stmt->execute([$name, $studentId, $email, $track, $gradeLevel, $userId]); + $stmt = $pdo->prepare("UPDATE users SET name = ?, student_id = ?, email = ?, track = ?, grade_level = ?, section = ? WHERE id = ?"); + $stmt->execute([$name, $studentId, $email, $track, $gradeLevel, $section, $userId]); } // Log the action diff --git a/assets/pasted-20260215-214441-d5a201d2.png b/assets/pasted-20260215-214441-d5a201d2.png new file mode 100644 index 0000000..b6524bc Binary files /dev/null and b/assets/pasted-20260215-214441-d5a201d2.png differ diff --git a/assets/pasted-20260215-220308-74df11e7.png b/assets/pasted-20260215-220308-74df11e7.png new file mode 100644 index 0000000..7cca604 Binary files /dev/null and b/assets/pasted-20260215-220308-74df11e7.png differ diff --git a/ballot.php b/ballot.php index 9a4e50c..76dc2c8 100644 --- a/ballot.php +++ b/ballot.php @@ -43,102 +43,115 @@ $endTime = strtotime($election['end_date_and_time']) * 1000;
-
-
- - TIME REMAINING: - 00:00:00 +
+

Cast Your Vote

+

Select one candidate for each position

+
+ +
+
+
Voter Information
+
+ + 00:00:00 +
+
+
+ + +
+
+ + Grade +
+
+ + +
+
+ + +
+
+
+ +
+ + + $pos): ?> +
+
+ +
+ + prepare($sql); + $cStmt->execute($params); + $candidates = $cStmt->fetchAll(); + ?> + + +
+

No candidates available for your track.

+
+ +
+ + + +
+ +
+ + +
+ +
+
+
diff --git a/dashboard.php b/dashboard.php index 679bce3..b352062 100644 --- a/dashboard.php +++ b/dashboard.php @@ -21,7 +21,9 @@ $totalVotes->execute([$electionId]); $totalVotes = $totalVotes->fetchColumn(); // Chart Data: Participation per Grade Level -$gradeStats = $pdo->prepare("SELECT COALESCE(u.grade_level::TEXT, 'Unknown') as label, COUNT(DISTINCT v.voter_id) as count +$driver = $pdo->getAttribute(PDO::ATTR_DRIVER_NAME); +$gradeCol = ($driver === 'pgsql') ? "u.grade_level::TEXT" : "CAST(u.grade_level AS CHAR)"; +$gradeStats = $pdo->prepare("SELECT COALESCE($gradeCol, 'Unknown') as label, COUNT(DISTINCT v.voter_id) as count FROM users u JOIN votes v ON u.id = v.voter_id WHERE v.election_id = ? GROUP BY u.grade_level ORDER BY u.grade_level"); diff --git a/db/mock_data.php b/db/mock_data.php new file mode 100644 index 0000000..1f6188e --- /dev/null +++ b/db/mock_data.php @@ -0,0 +1,137 @@ +getAttribute(PDO::ATTR_DRIVER_NAME); +echo "Connected using driver: $driver\n"; + +try { + $pdo->beginTransaction(); + + echo "Clearing existing data (preserving Admin)...\n"; + + // Order matters for foreign keys + $pdo->exec("DELETE FROM audit_logs"); + $pdo->exec("DELETE FROM votes"); + $pdo->exec("DELETE FROM candidates"); + $pdo->exec("DELETE FROM positions"); + $pdo->exec("DELETE FROM election_assignments"); + $pdo->exec("DELETE FROM elections"); + $pdo->exec("DELETE FROM users WHERE role != 'Admin'"); + + // Ensure admin exists + $admin = $pdo->query("SELECT id FROM users WHERE role = 'Admin' LIMIT 1")->fetch(); + if (!$admin) { + $adminId = generate_uuid(); + $stmt = $pdo->prepare("INSERT INTO users (id, student_id, name, email, password_hash, role, access_level) VALUES (?, ?, ?, ?, ?, ?, ?)"); + $stmt->execute([$adminId, '00-0000', 'Admin User', 'Admin@iloilonhs.edu.ph', password_hash('Testing', PASSWORD_DEFAULT), 'Admin', 4]); + echo "Created default admin.\n"; + } else { + $adminId = $admin['id']; + echo "Preserved existing admin: " . $adminId . "\n"; + } + + echo "Adding mock elections and positions...\n"; + $electionId = generate_uuid(); + $stmt = $pdo->prepare("INSERT INTO elections (id, title, description, status, start_date_and_time, end_date_and_time, created_by) VALUES (?, ?, ?, ?, ?, ?, ?)"); + $stmt->execute([ + $electionId, + 'SSG General Elections 2026', + 'Annual election for Supreme Student Government.', + 'Ongoing', + date('Y-m-d H:i:s', strtotime('-1 day')), + date('Y-m-d H:i:s', strtotime('+7 days')), + $adminId + ]); + + $positions = [ + ['President', 1, 1], + ['Vice President', 1, 2], + ['Secretary', 1, 3], + ['Treasurer', 1, 4] + ]; + $posIds = []; + $stmt = $pdo->prepare("INSERT INTO positions (id, election_id, name, max_votes, sort_order) VALUES (?, ?, ?, ?, ?)"); + foreach ($positions as $p) { + $id = generate_uuid(); + $stmt->execute([$id, $electionId, $p[0], $p[1], $p[2]]); + $posIds[$p[0]] = $id; + } + + echo "Adding mock students (voters)...\n"; + $tracks = ['STEM', 'ABM', 'HUMSS', 'GAS', 'TVL']; + $sections = ['A', 'B', 'C', 'D']; + $voters = []; + $stmt = $pdo->prepare("INSERT INTO users (id, student_id, name, email, password_hash, grade_level, track, section, role) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"); + $assignStmt = $pdo->prepare("INSERT INTO election_assignments (id, election_id, user_id, role_in_election, assigned_by) VALUES (?, ?, ?, ?, ?)"); + + for ($i = 1; $i <= 40; $i++) { + $id = generate_uuid(); + $studentId = sprintf('26-%04d', $i); + $track = $tracks[array_rand($tracks)]; + $grade = rand(11, 12); + $section = $sections[array_rand($sections)]; + + $stmt->execute([ + $id, + $studentId, + "Student $i", + "student$i@iloilonhs.edu.ph", + password_hash('password', PASSWORD_DEFAULT), + $grade, + $track, + $section, + 'Voter' + ]); + + $assignStmt->execute([generate_uuid(), $electionId, $id, 'Voter', $adminId]); + $voters[] = ['id' => $id, 'grade' => $grade, 'track' => $track]; + } + + echo "Adding candidates...\n"; + $candidateIds = []; + foreach ($posIds as $posName => $posId) { + for ($c = 1; $c <= 2; $c++) { + $voter = array_shift($voters); + $userId = $voter['id']; + $candId = generate_uuid(); + + $candStmt = $pdo->prepare("INSERT INTO candidates (id, election_id, position_id, user_id, party_name, approved) VALUES (?, ?, ?, ?, ?, ?)"); + $candStmt->execute([$candId, $electionId, $posId, $userId, ($c == 1 ? 'Alpha Party' : 'Beta Party'), true]); + $candidateIds[$posId][] = $candId; + + $updStmt = $pdo->prepare("UPDATE election_assignments SET role_in_election = 'Candidate' WHERE election_id = ? AND user_id = ?"); + $updStmt->execute([$electionId, $userId]); + } + } + + echo "Generating mock votes...\n"; + $voteStmt = $pdo->prepare("INSERT INTO votes (id, election_id, position_id, candidate_id, voter_id) VALUES (?, ?, ?, ?, ?)"); + foreach ($voters as $v) { + // 85% turnout + if (rand(1, 100) <= 85) { + foreach ($posIds as $posId) { + $candId = $candidateIds[$posId][array_rand($candidateIds[$posId])]; + $voteStmt->execute([generate_uuid(), $electionId, $posId, $candId, $v['id']]); + } + } + } + + $pdo->commit(); + echo "Done! Mock data successfully generated.\n"; + +} catch (Exception $e) { + if ($pdo->inTransaction()) $pdo->rollBack(); + echo "FATAL ERROR: " . $e->getMessage() . "\n"; + exit(1); +} diff --git a/election_history.php b/election_history.php index 90b1870..258ca67 100644 --- a/election_history.php +++ b/election_history.php @@ -235,6 +235,10 @@ $projectDescription = $_SERVER['PROJECT_DESCRIPTION'] ?? 'Online Election System
+ + + GENERATE RESULTS + diff --git a/login.php b/login.php index fb76539..ec97255 100644 --- a/login.php +++ b/login.php @@ -47,7 +47,8 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { $error = 'Invalid Credentials. Please check your UID, Email, and Role.'; } - if ($error && isset($_POST['role'])) { // Redirect back to landing if coming from modal + if ($error && isset($_POST['role']) && str_contains($_SERVER['HTTP_REFERER'] ?? '', 'index.php')) { + // Only redirect back if we actually came from landing page modal header('Location: index.php?error=' . urlencode($error)); exit; } @@ -57,46 +58,113 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { - Login - Online Election System - + + Login - Iloilo National High School + + + + - -