<?php
// Start session
session_start();
// Check if user has admin privileges
if (!isset($_SESSION['role']) || ($_SESSION['role'] !== 'admin' && $_SESSION['role'] !== 'director')) {
echo json_encode([
'success' => false,
'message' => 'Unauthorized access'
]);
exit;
}
// Include database configuration
require_once '../../admin/database/db_config.php';
// Check if it's a POST request
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
echo json_encode([
'success' => false,
'message' => 'Invalid request method'
]);
exit;
}
// Check if enrollment ID and notes are provided
if (!isset($_POST['enrollment_id']) || empty($_POST['enrollment_id'])) {
echo json_encode([
'success' => false,
'message' => 'Enrollment ID is required'
]);
exit;
}
$enrollment_id = intval($_POST['enrollment_id']);
$admin_notes = isset($_POST['admin_notes']) ? trim($_POST['admin_notes']) : '';
$admin_id = $_SESSION['user_id'];
// Begin transaction
$conn->begin_transaction();
try {
// Get enrollment information to ensure it exists
$query = "SELECT e.id, e.user_id, e.course_id, u.first_name, u.last_name, c.title
FROM enrollments e
JOIN users u ON e.user_id = u.id
JOIN courses c ON e.course_id = c.id
WHERE e.id = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param("i", $enrollment_id);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows === 0) {
throw new Exception('Enrollment not found');
}
$enrollment = $result->fetch_assoc();
// Get application ID if exists
$app_query = "SELECT id FROM enrollment_applications
WHERE user_id = ? AND course_id = ?
LIMIT 1";
$stmt = $conn->prepare($app_query);
$stmt->bind_param("ii", $enrollment['user_id'], $enrollment['course_id']);
$stmt->execute();
$app_result = $stmt->get_result();
$application_id = null;
if ($app_result->num_rows > 0) {
$app = $app_result->fetch_assoc();
$application_id = $app['id'];
}
// Check if the enrollment_notes table exists, if not create it
$check_table_query = "SHOW TABLES LIKE 'enrollment_notes'";
$table_result = $conn->query($check_table_query);
if ($table_result->num_rows === 0) {
$create_table_query = "CREATE TABLE enrollment_notes (
id INT AUTO_INCREMENT PRIMARY KEY,
enrollment_id INT NOT NULL,
application_id INT,
admin_id INT NOT NULL,
notes TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (enrollment_id) REFERENCES enrollments(id) ON DELETE CASCADE,
FOREIGN KEY (admin_id) REFERENCES users(id) ON DELETE SET NULL
)";
$conn->query($create_table_query);
}
// Check if notes already exist for this enrollment
$notes_query = "SELECT id FROM enrollment_notes WHERE enrollment_id = ?";
$stmt = $conn->prepare($notes_query);
$stmt->bind_param("i", $enrollment_id);
$stmt->execute();
$notes_result = $stmt->get_result();
if ($notes_result->num_rows > 0) {
// Update existing notes
$notes_row = $notes_result->fetch_assoc();
$update_query = "UPDATE enrollment_notes
SET notes = ?, admin_id = ?, updated_at = NOW()
WHERE id = ?";
$stmt = $conn->prepare($update_query);
$stmt->bind_param("sii", $admin_notes, $admin_id, $notes_row['id']);
$stmt->execute();
} else {
// Insert new notes
$insert_query = "INSERT INTO enrollment_notes
(enrollment_id, application_id, admin_id, notes)
VALUES (?, ?, ?, ?)";
$stmt = $conn->prepare($insert_query);
$stmt->bind_param("iiis", $enrollment_id, $application_id, $admin_id, $admin_notes);
$stmt->execute();
}
// Log activity
$activity = "Updated admin notes for enrollment #$enrollment_id - Student: " .
$enrollment['first_name'] . ' ' . $enrollment['last_name'] .
", Course: " . $enrollment['title'];
$log_query = "INSERT INTO activities (user_id, activity_type, description, created_at)
VALUES (?, 'update_notes', ?, NOW())";
$stmt = $conn->prepare($log_query);
$stmt->bind_param("is", $admin_id, $activity);
$stmt->execute();
// Commit transaction
$conn->commit();
echo json_encode([
'success' => true,
'message' => 'Admin notes updated successfully'
]);
} catch (Exception $e) {
// Rollback transaction on error
$conn->rollback();
echo json_encode([
'success' => false,
'message' => 'Error: ' . $e->getMessage()
]);
}
?>