Update: 2026-07-12 05:40:28
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
// transit/enrollment/import_roster.php — المشرف يرفع كشف الطلاب (CSV)
|
||||
// POST: file (CSV: student_id, name), semester?, notes?
|
||||
|
||||
require_once __DIR__ . '/../../transit/connect_admin.php';
|
||||
|
||||
if (!isset($_FILES['file']) || $_FILES['file']['error'] !== UPLOAD_ERR_OK) {
|
||||
jsonError('No valid file uploaded', 400);
|
||||
}
|
||||
|
||||
$ext = strtolower(pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION));
|
||||
if (!in_array($ext, ['csv', 'txt'])) jsonError('Only CSV files are supported', 400);
|
||||
|
||||
$semester = filterRequest('semester') ?: date('Y') . '-S1';
|
||||
$notes = filterRequest('notes');
|
||||
|
||||
$rows = [];
|
||||
if (($handle = fopen($_FILES['file']['tmp_name'], 'r')) !== false) {
|
||||
$headers = null;
|
||||
while (($line = fgetcsv($handle, 0, ',')) !== false) {
|
||||
if ($headers === null) { $headers = array_map('strtolower', array_map('trim', $line)); continue; }
|
||||
$row = array_combine($headers, $line);
|
||||
if ($row) $rows[] = $row;
|
||||
}
|
||||
fclose($handle);
|
||||
}
|
||||
|
||||
if (empty($rows)) jsonError('CSV file is empty or invalid', 400);
|
||||
|
||||
$totalRows = count($rows);
|
||||
$transit_con->prepare(
|
||||
"INSERT INTO transit_rosters (org_id, uploaded_by, filename, total_rows, semester, notes)
|
||||
VALUES (?,?,?,?,?,?)"
|
||||
)->execute([$transit_org_id, $transit_admin_id, $_FILES['file']['name'], $totalRows, $semester, $notes]);
|
||||
$rosterId = (int)$transit_con->lastInsertId();
|
||||
|
||||
$insertSt = $transit_con->prepare(
|
||||
"INSERT IGNORE INTO transit_enrollments
|
||||
(org_id, passenger_id, student_id, member_name, verify_method, status, roster_id)
|
||||
VALUES (?,NULL,?,?,'roster_manual','pending',?)"
|
||||
);
|
||||
|
||||
$matchedRows = 0;
|
||||
$newEnroll = 0;
|
||||
|
||||
foreach ($rows as $row) {
|
||||
$sid = trim($row['student_id'] ?? $row['id'] ?? '');
|
||||
$name = trim($row['name'] ?? $row['full_name'] ?? '');
|
||||
if (!$sid) continue;
|
||||
|
||||
$encSid = $encryptionHelper->encryptData($sid);
|
||||
|
||||
$chk = $transit_con->prepare(
|
||||
"SELECT id, passenger_id FROM transit_enrollments WHERE org_id=? AND student_id=? LIMIT 1"
|
||||
);
|
||||
$chk->execute([$transit_org_id, $encSid]);
|
||||
$existing = $chk->fetch();
|
||||
|
||||
if ($existing) {
|
||||
$transit_con->prepare(
|
||||
"UPDATE transit_enrollments SET member_name=?, roster_id=? WHERE id=?"
|
||||
)->execute([$name, $rosterId, $existing['id']]);
|
||||
if ($existing['passenger_id']) $matchedRows++;
|
||||
} else {
|
||||
$insertSt->execute([$transit_org_id, $encSid, $name, $rosterId]);
|
||||
$newEnroll++;
|
||||
}
|
||||
}
|
||||
|
||||
$transit_con->prepare(
|
||||
"UPDATE transit_rosters SET matched_rows=?, new_enrollments=? WHERE id=?"
|
||||
)->execute([$matchedRows, $newEnroll, $rosterId]);
|
||||
|
||||
jsonSuccess([
|
||||
'roster_id' => $rosterId,
|
||||
'total_rows' => $totalRows,
|
||||
'new_enrollments' => $newEnroll,
|
||||
'matched' => $matchedRows,
|
||||
], 'Roster imported successfully');
|
||||
Reference in New Issue
Block a user