<?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 data is submitted via POST
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
echo json_encode(['success' => false, 'message' => 'Invalid request method']);
exit;
}
// Check if enrollment ID is provided
if (!isset($_POST['enrollment_id']) || empty($_POST['enrollment_id'])) {
echo json_encode(['success' => false, 'message' => 'Invalid enrollment ID']);
exit;
}
$enrollment_id = intval($_POST['enrollment_id']);
$admin_notes = trim($_POST['admin_notes'] ?? '');
// Begin transaction
$conn->begin_transaction();
try {
// Get enrollment details
$query = "SELECT user_id, course_id FROM enrollments WHERE 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();
// Check if there's an application record
$app_query = "SELECT id FROM enrollment_applications
WHERE user_id = ? AND course_id = ? AND status = 'completed'
ORDER BY updated_at DESC LIMIT 1";
$stmt = $conn->prepare($app_query);
$stmt->bind_param("ii", $enrollment['user_id'], $enrollment['course_id']);
$stmt->execute();
$app_result = $stmt->get_result();
if ($app_result->num_rows > 0) {
// Update application notes
$app_id = $app_result->fetch_assoc()['id'];
$update_app_query = "UPDATE enrollment_applications SET admin_notes = ? WHERE id = ?";
$stmt = $conn->prepare($update_app_query);
$stmt->bind_param("si", $admin_notes, $app_id);
if (!$stmt->execute()) {
throw new Exception('Failed to update application notes: ' . $conn->error);
}
}
// Update enrollment notes table (create if it doesn't exist)
$create_notes_table = "CREATE TABLE IF NOT EXISTS enrollment_notes (
id INT(11) NOT NULL AUTO_INCREMENT,
enrollment_id INT(11) NOT NULL,
admin_notes TEXT DEFAULT NULL,
admin_id INT(11) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id),
KEY enrollment_id (enrollment_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";
if (!$conn->query($create_notes_table)) {
throw new Exception('Failed to create notes table: ' . $conn->error);
}
// Check if entry exists
$check_query = "SELECT id FROM enrollment_notes WHERE enrollment_id = ?";
$stmt = $conn->prepare($check_query);
$stmt->bind_param("i", $enrollment_id);
$stmt->execute();
$check_result = $stmt->get_result();
if ($check_result->num_rows > 0) {
// Update existing notes
$notes_id = $check_result->fetch_assoc()['id'];
$update_query = "UPDATE enrollment_notes SET admin_notes = ?, admin_id = ?, created_at = NOW() WHERE id = ?";
$stmt = $conn->prepare($update_query);
$stmt->bind_param("sii", $admin_notes, $_SESSION['user_id'], $notes_id);
} else {
// Insert new notes
$insert_query = "INSERT INTO enrollment_notes (enrollment_id, admin_notes, admin_id) VALUES (?, ?, ?)";
$stmt = $conn->prepare($insert_query);
$stmt->bind_param("isi", $enrollment_id, $admin_notes, $_SESSION['user_id']);
}
if (!$stmt->execute()) {
throw new Exception('Failed to update enrollment notes: ' . $conn->error);
}
// Log activity
try {
$log_query = "INSERT INTO activities (user_id, user_type, activity_type, activity_description, created_at)
VALUES (?, 'admin', 'enrollment_note', ?, NOW())";
$stmt = $conn->prepare($log_query);
$activity_description = "Updated notes for enrollment #" . $enrollment_id;
$stmt->bind_param("is", $_SESSION['user_id'], $activity_description);
$stmt->execute();
} catch (Exception $e) {
// Just log error but don't fail the transaction
error_log('Failed to log activity: ' . $e->getMessage());
}
// Commit transaction
$conn->commit();
echo json_encode(['success' => true, 'message' => 'Notes updated successfully']);
} catch (Exception $e) {
// Rollback transaction
$conn->rollback();
echo json_encode(['success' => false, 'message' => $e->getMessage()]);
}
?>