Sindbad~EG File Manager
<?php
require_once '../config/config.php';
// Check if user is logged in and is superuser
if (!isLoggedIn() || !hasRole('superuser')) {
die('Access denied. Superuser required.');
}
$db = new Database();
$conn = $db->getConnection();
echo "<h2>Database Migration - Add Location IDs to Attendance Records</h2>";
try {
// Check if columns already exist
$check_query = "SHOW COLUMNS FROM attendance_records LIKE 'district_id'";
$stmt = $conn->prepare($check_query);
$stmt->execute();
$district_id_exists = $stmt->fetch();
if ($district_id_exists) {
echo "<p style='color: green;'>✓ Migration already completed. District and Assembly ID columns exist.</p>";
} else {
echo "<p>Starting migration...</p>";
// Add new columns
echo "<p>Adding district_id and assembly_id columns...</p>";
$conn->exec("ALTER TABLE attendance_records
ADD COLUMN district_id INT NULL AFTER program_id,
ADD COLUMN assembly_id INT NULL AFTER district_id");
// Add foreign key constraints
echo "<p>Adding foreign key constraints...</p>";
$conn->exec("ALTER TABLE attendance_records
ADD CONSTRAINT fk_attendance_district
FOREIGN KEY (district_id) REFERENCES locations(id) ON DELETE SET NULL,
ADD CONSTRAINT fk_attendance_assembly
FOREIGN KEY (assembly_id) REFERENCES locations(id) ON DELETE SET NULL");
// Add indexes
echo "<p>Adding database indexes...</p>";
$conn->exec("CREATE INDEX idx_attendance_district ON attendance_records(district_id)");
$conn->exec("CREATE INDEX idx_attendance_assembly ON attendance_records(assembly_id)");
// Update existing records
echo "<p>Updating existing attendance records...</p>";
// Update district_id based on district_name
$update_districts = $conn->exec("UPDATE attendance_records ar
SET district_id = (
SELECT l.id FROM locations l
WHERE l.name = ar.district_name AND l.type = 'district'
LIMIT 1
)
WHERE ar.district_name IS NOT NULL AND ar.district_name != ''");
// Update assembly_id based on assembly_name
$update_assemblies = $conn->exec("UPDATE attendance_records ar
SET assembly_id = (
SELECT l.id FROM locations l
WHERE l.name = ar.assembly_name AND l.type = 'assembly'
LIMIT 1
)
WHERE ar.assembly_name IS NOT NULL AND ar.assembly_name != ''");
echo "<p style='color: green;'>✓ Migration completed successfully!</p>";
echo "<p>Updated $update_districts records with district IDs</p>";
echo "<p>Updated $update_assemblies records with assembly IDs</p>";
}
// Show migration results
$stats_query = "SELECT
COUNT(*) as total_records,
COUNT(district_id) as records_with_district_id,
COUNT(assembly_id) as records_with_assembly_id
FROM attendance_records";
$stmt = $conn->prepare($stats_query);
$stmt->execute();
$stats = $stmt->fetch();
echo "<h3>Migration Statistics:</h3>";
echo "<ul>";
echo "<li>Total attendance records: " . $stats['total_records'] . "</li>";
echo "<li>Records with district ID: " . $stats['records_with_district_id'] . "</li>";
echo "<li>Records with assembly ID: " . $stats['records_with_assembly_id'] . "</li>";
echo "</ul>";
// Show locations
$locations_query = "SELECT type, COUNT(*) as count FROM locations WHERE is_active = 1 GROUP BY type";
$stmt = $conn->prepare($locations_query);
$stmt->execute();
$locations_stats = $stmt->fetchAll();
echo "<h3>Available Locations:</h3>";
echo "<ul>";
foreach ($locations_stats as $stat) {
echo "<li>" . ucfirst($stat['type']) . "s: " . $stat['count'] . "</li>";
}
echo "</ul>";
echo "<p><a href='dashboard.php'>← Back to Dashboard</a></p>";
echo "<p><a href='../attendance/form.php?program=1'>→ Test Attendance Form</a></p>";
} catch (Exception $e) {
echo "<p style='color: red;'>Error: " . $e->getMessage() . "</p>";
echo "<p>Please check your database connection and try again.</p>";
}
?>
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists