<?php
session_start();
include('db_connection.php');

// Placeholder for teacher ID, e.g., from session
$teacher_id = $_SESSION['teacher_id']; // Replace with actual logic to get teacher_id

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $homework_id = $_POST['homework_id'];
    $student_id = $_POST['student_id'];
    $feedback = $_POST['feedback'];
    $grade = $_POST['grade'];

    // Insert feedback and grade into database
    $sql = "UPDATE student_homework 
            SET feedback = ?, grade = ?, status = 'graded'
            WHERE homework_id = ? AND student_id = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("ssii", $feedback, $grade, $homework_id, $student_id);

    if ($stmt->execute()) {
        $_SESSION['message'] = "Feedback and grade submitted successfully!";
    } else {
        $_SESSION['error'] = "Failed to submit feedback: " . $stmt->error;
    }

    header("Location: feedback_grading.php?homework_id=$homework_id");
    exit();
}

// Close database connection
$conn->close();
?>
