Initial import

This commit is contained in:
pongpon pitisuk
2026-06-25 16:49:13 +07:00
commit 72701ad58c
120 changed files with 17590 additions and 0 deletions
+65
View File
@@ -0,0 +1,65 @@
<?php
require_once __DIR__ . '/../includes/functions.php';
requireLogin();
if (isStudent()) {
header('Location: ' . BASE_URL . '/index.php');
exit;
}
$student_id = intval($_POST['student_id'] ?? 0);
requireStudentAccess($student_id);
$course_code = trim($_POST['course_code'] ?? '');
$course_name = trim($_POST['course_name'] ?? '');
$credits = floatval($_POST['credits'] ?? 0);
$lecture = intval($_POST['lecture'] ?? 0);
$practice = intval($_POST['practice'] ?? 0);
$self_study = intval($_POST['self_study'] ?? 0);
$group_id = intval($_POST['group_id'] ?? 0);
$grade = trim($_POST['grade'] ?? '');
$course_type = trim($_POST['course_type'] ?? '');
$semester = trim($_POST['semester'] ?? '');
$academic_year = trim($_POST['academic_year'] ?? '');
$notes = trim($_POST['notes'] ?? '');
$student = getStudent($student_id);
if (!$student || empty($course_code) || empty($course_name) || $credits <= 0) {
header('Location: ' . BASE_URL . '/students/view.php?id=' . $student_id . '&error=invalid_data');
exit;
}
try {
$db = getDB();
// Try to find existing course in the curriculum
$course_id = null;
$curriculum_course = findCourseInCurriculum($student['curriculum_id'], $course_code);
if ($curriculum_course) {
$course_id = $curriculum_course['id'];
} elseif ($group_id > 0) {
// Check if course already exists under selected group
$stmt = $db->prepare("SELECT id FROM courses WHERE group_id = ? AND code = ?");
$stmt->execute([$group_id, $course_code]);
$existing = $stmt->fetch();
if ($existing) {
$course_id = $existing['id'];
} else {
// Create a new course record under the selected group
$stmt = $db->prepare("
INSERT INTO courses (group_id, code, name_th, credits, lecture_hours, practice_hours, self_study_hours, sort_order)
VALUES (?, ?, ?, ?, ?, ?, ?, 0)
");
$stmt->execute([$group_id, $course_code, $course_name, $credits, $lecture, $practice, $self_study]);
$course_id = $db->lastInsertId();
}
}
$stmt = $db->prepare("INSERT INTO student_courses (student_id, course_id, course_code, course_name_th, credits, lecture_hours, practice_hours, self_study_hours, grade, course_type, semester, academic_year, source_type, notes) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'manual', ?)");
$stmt->execute([$student_id, $course_id, $course_code, $course_name, $credits, $lecture, $practice, $self_study, $grade ?: null, $course_type ?: null, $semester ?: null, $academic_year ?: null, $notes]);
header('Location: ' . BASE_URL . '/students/view.php?id=' . $student_id . '&added=1');
} catch (PDOException $e) {
header('Location: ' . BASE_URL . '/students/view.php?id=' . $student_id . '&error=' . urlencode($e->getMessage()));
}
exit;
+20
View File
@@ -0,0 +1,20 @@
<?php
require_once __DIR__ . '/../includes/functions.php';
requireLogin();
if (isStudent()) {
header('Location: ' . BASE_URL . '/index.php');
exit;
}
$id = intval($_GET['id'] ?? 0);
$student_id = intval($_GET['student_id'] ?? 0);
requireStudentAccess($student_id);
if ($id > 0) {
$stmt = getDB()->prepare("DELETE FROM student_courses WHERE id = ?");
$stmt->execute([$id]);
}
header('Location: ' . BASE_URL . '/students/view.php?id=' . $student_id);
exit;
+22
View File
@@ -0,0 +1,22 @@
<?php
require_once __DIR__ . '/../includes/functions.php';
requireLogin();
if (isStudent()) {
header('Location: ' . BASE_URL . '/index.php');
exit;
}
$id = intval($_POST['id'] ?? 0);
$student_id = intval($_POST['student_id'] ?? 0);
$grade = trim($_POST['grade'] ?? '');
requireStudentAccess($student_id);
if ($id > 0) {
$stmt = getDB()->prepare("UPDATE student_courses SET grade = ? WHERE id = ?");
$stmt->execute([$grade, $id]);
}
header('Location: ' . BASE_URL . '/students/view.php?id=' . $student_id);
exit;
+105
View File
@@ -0,0 +1,105 @@
<?php
require_once __DIR__ . '/../includes/functions.php';
requireLogin();
if (isStudent()) {
header('Location: ' . BASE_URL . '/index.php');
exit;
}
spl_autoload_register(function ($class) {
$prefix = 'Smalot\\PdfParser\\';
$base_dir = __DIR__ . '/../vendor/smalot/pdfparser/src/';
if (strncmp($prefix, $class, strlen($prefix)) === 0) {
$file = $base_dir . str_replace('\\', '/', $class) . '.php';
if (file_exists($file)) { require $file; }
}
});
$student_id = intval($_POST['student_id'] ?? 0);
requireStudentAccess($student_id);
$student = getStudent($student_id);
if (!$student || !isset($_FILES['pdf_file']) || $_FILES['pdf_file']['error'] !== UPLOAD_ERR_OK) {
header('Location: ' . BASE_URL . '/students/view.php?id=' . $student_id . '&error=upload_failed');
exit;
}
$upload_dir = __DIR__ . '/../uploads/';
if (!is_dir($upload_dir)) mkdir($upload_dir, 0777, true);
$filename = 'transfer_' . $student_id . '_' . time() . '.pdf';
$filepath = $upload_dir . $filename;
move_uploaded_file($_FILES['pdf_file']['tmp_name'], $filepath);
// Extract text from PDF
$text = extractTextFromPDF($filepath);
if (empty(trim($text))) {
header('Location: ' . BASE_URL . '/students/view.php?id=' . $student_id . '&error=no_text');
exit;
}
// Log the extracted text for debugging
file_put_contents(__DIR__ . '/../uploads/last_extracted_text_' . $student_id . '.txt', $text);
// Parse course data from text
$parsed_courses = parseCoursesFromText($text);
// For each parsed course, try to match with curriculum courses
$db = getDB();
$curriculum_id = $student['curriculum_id'];
// Get all curriculum courses for matching
$curriculum_courses = $db->prepare("
SELECT c.id, c.code, c.name_th, c.credits, cg.name_th AS group_name
FROM courses c
JOIN course_groups cg ON c.group_id = cg.id
WHERE cg.curriculum_id = ?
");
$curriculum_courses->execute([$curriculum_id]);
$curriculum_map = [];
foreach ($curriculum_courses->fetchAll() as $cc) {
$curriculum_map[$cc['code']] = $cc;
}
$added = 0;
$skipped = 0;
$check_stmt = $db->prepare("SELECT COUNT(*) FROM student_courses WHERE student_id = ? AND course_code = ? AND source_type = 'transfer'");
foreach ($parsed_courses as $course) {
$code = $course['code'];
// Only add courses that match the curriculum
if (!isset($curriculum_map[$code])) {
$skipped++;
continue;
}
$cc = $curriculum_map[$code];
// Skip if already added
$check_stmt->execute([$student_id, $code]);
if ($check_stmt->fetchColumn() > 0) {
$skipped++;
continue;
}
// Add the course as transfer credit
$stmt = $db->prepare("INSERT INTO student_courses (student_id, course_id, course_code, course_name_th, credits, lecture_hours, practice_hours, self_study_hours, grade, course_type, source_type, source_file) VALUES (?, ?, ?, ?, ?, ?, ?, ?, 'S', 'เทียบโอนรายวิชา', 'transfer', ?)");
$stmt->execute([
$student_id,
$cc['id'],
$code,
$cc['name_th'],
$cc['credits'],
$course['lecture'],
$course['practice'],
$course['self_study'],
$filename
]);
$added++;
}
$msg = $added > 0 ? "added=$added" : "added=0";
header('Location: ' . BASE_URL . '/students/view.php?id=' . $student_id . '&' . $msg);
exit;
+87
View File
@@ -0,0 +1,87 @@
<?php
require_once __DIR__ . '/../includes/functions.php';
requireLogin();
if (isStudent()) {
header('Location: ' . BASE_URL . '/index.php');
exit;
}
$student_id = intval($_POST['student_id'] ?? 0);
requireStudentAccess($student_id);
$student = getStudent($student_id);
if (!$student || !isset($_POST['pdf_text'])) {
header('Location: ' . BASE_URL . '/students/view.php?id=' . $student_id . '&error=invalid_data');
exit;
}
$text = $_POST['pdf_text'];
if (empty(trim($text))) {
header('Location: ' . BASE_URL . '/students/view.php?id=' . $student_id . '&error=no_text');
exit;
}
// Log the pasted text for debugging
file_put_contents(__DIR__ . '/../uploads/last_pasted_text_' . $student_id . '.txt', $text);
// Parse course data from text
$parsed_courses = parseCoursesFromText($text);
// For each parsed course, try to match with curriculum courses
$db = getDB();
$curriculum_id = $student['curriculum_id'];
// Get all curriculum courses for matching
$curriculum_courses = $db->prepare("
SELECT c.id, c.code, c.name_th, c.credits, cg.name_th AS group_name
FROM courses c
JOIN course_groups cg ON c.group_id = cg.id
WHERE cg.curriculum_id = ?
");
$curriculum_courses->execute([$curriculum_id]);
$curriculum_map = [];
foreach ($curriculum_courses->fetchAll() as $cc) {
$curriculum_map[$cc['code']] = $cc;
}
$added = 0;
$skipped = 0;
$check_stmt = $db->prepare("SELECT COUNT(*) FROM student_courses WHERE student_id = ? AND course_code = ? AND source_type = 'transfer'");
foreach ($parsed_courses as $course) {
$code = $course['code'];
// Only add courses that match the curriculum
if (!isset($curriculum_map[$code])) {
$skipped++;
continue;
}
$cc = $curriculum_map[$code];
// Skip if already added
$check_stmt->execute([$student_id, $code]);
if ($check_stmt->fetchColumn() > 0) {
$skipped++;
continue;
}
// Add the course as transfer credit
$stmt = $db->prepare("INSERT INTO student_courses (student_id, course_id, course_code, course_name_th, credits, lecture_hours, practice_hours, self_study_hours, grade, course_type, source_type, source_file) VALUES (?, ?, ?, ?, ?, ?, ?, ?, 'S', 'เทียบโอนรายวิชา', 'transfer', 'pasted_text')");
$stmt->execute([
$student_id,
$cc['id'],
$code,
$cc['name_th'],
$cc['credits'],
$course['lecture'],
$course['practice'],
$course['self_study']
]);
$added++;
}
$msg = $added > 0 ? "added=$added" : "added=0";
header('Location: ' . BASE_URL . '/students/view.php?id=' . $student_id . '&' . $msg);
exit;