105 lines
3.4 KiB
PHP
105 lines
3.4 KiB
PHP
<?php
|
|
include 'koneksi.php';
|
|
header('Content-Type: application/json');
|
|
|
|
// ===== AUTO-CREATE TABEL JIKA BELUM ADA =====
|
|
$conn->query("
|
|
CREATE TABLE IF NOT EXISTS riwayat_pendidikan (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
penduduk_id INT NOT NULL,
|
|
jenis ENUM('formal','pelatihan') DEFAULT 'formal',
|
|
institusi VARCHAR(150),
|
|
jenjang VARCHAR(100),
|
|
jurusan VARCHAR(100),
|
|
thn_masuk INT,
|
|
thn_lulus INT,
|
|
status ENUM('lulus','sedang_belajar','putus') DEFAULT 'lulus',
|
|
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
FOREIGN KEY (penduduk_id) REFERENCES penduduk(id) ON DELETE CASCADE
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
|
|
");
|
|
|
|
$action = $_GET['action'] ?? $_POST['action'] ?? 'list';
|
|
|
|
// ===== LIST =====
|
|
if ($action === 'list') {
|
|
$result = $conn->query("
|
|
SELECT rp.*,
|
|
p.nama AS penduduk_nama,
|
|
p.nik
|
|
FROM riwayat_pendidikan rp
|
|
JOIN penduduk p ON rp.penduduk_id = p.id
|
|
ORDER BY rp.created_at DESC
|
|
");
|
|
|
|
$rows = [];
|
|
if ($result) {
|
|
while ($r = $result->fetch_assoc()) $rows[] = $r;
|
|
}
|
|
echo json_encode($rows, JSON_UNESCAPED_UNICODE);
|
|
}
|
|
|
|
// ===== SIMPAN =====
|
|
elseif ($action === 'simpan') {
|
|
$pid = (int)($_POST['penduduk_id'] ?? 0);
|
|
$jenis = $_POST['jenis'] ?? 'formal';
|
|
$inst = $_POST['institusi'] ?? '';
|
|
$jenjang = $_POST['jenjang'] ?? '';
|
|
$jurusan = $_POST['jurusan'] ?? '';
|
|
$thn_msk = !empty($_POST['thn_masuk']) ? (int)$_POST['thn_masuk'] : null;
|
|
$thn_lls = !empty($_POST['thn_lulus']) ? (int)$_POST['thn_lulus'] : null;
|
|
$status = $_POST['status'] ?? 'lulus';
|
|
|
|
if (!$pid) {
|
|
echo json_encode(["status" => "error", "msg" => "penduduk_id tidak valid"]);
|
|
exit;
|
|
}
|
|
|
|
$stmt = $conn->prepare("
|
|
INSERT INTO riwayat_pendidikan
|
|
(penduduk_id, jenis, institusi, jenjang, jurusan, thn_masuk, thn_lulus, status)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
");
|
|
$stmt->bind_param("issssiis",
|
|
$pid, $jenis, $inst, $jenjang, $jurusan, $thn_msk, $thn_lls, $status
|
|
);
|
|
|
|
if ($stmt->execute()) {
|
|
$new_id = $conn->insert_id;
|
|
|
|
// Log aktivitas
|
|
$uid = (int)($_POST['user_id'] ?? 0);
|
|
if ($uid) {
|
|
$aksi = 'tambah_pendidikan';
|
|
$tbl = 'riwayat_pendidikan';
|
|
$det = "Tambah $jenis: $jenjang (penduduk ID $pid)";
|
|
$ip = $_SERVER['REMOTE_ADDR'] ?? '';
|
|
$log = $conn->prepare("INSERT INTO log_aktivitas (user_id,aksi,tabel,record_id,detail,ip) VALUES (?,?,?,?,?,?)");
|
|
$log->bind_param("issiiss", $uid, $aksi, $tbl, $new_id, $det, $ip);
|
|
$log->execute();
|
|
}
|
|
|
|
echo json_encode(["status" => "success", "id" => $new_id]);
|
|
} else {
|
|
echo json_encode(["status" => "error", "msg" => $stmt->error]);
|
|
}
|
|
$stmt->close();
|
|
}
|
|
|
|
// ===== HAPUS =====
|
|
elseif ($action === 'hapus') {
|
|
$id = (int)($_POST['id'] ?? 0);
|
|
$stmt = $conn->prepare("DELETE FROM riwayat_pendidikan WHERE id=?");
|
|
$stmt->bind_param("i", $id);
|
|
if ($stmt->execute()) {
|
|
echo json_encode(["status" => "success"]);
|
|
} else {
|
|
echo json_encode(["status" => "error", "msg" => $stmt->error]);
|
|
}
|
|
$stmt->close();
|
|
}
|
|
|
|
else {
|
|
echo json_encode(["status" => "error", "msg" => "Action tidak dikenal"]);
|
|
}
|