<?php
// Start session
session_start();
// Include database configuration
require_once '../config/database.php';
// Check if user is logged in and has admin role
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'admin') {
header('Location: login.php');
exit;
}
// Check if exam_id and student_exam_id are provided
if (!isset($_GET['exam_id']) || !is_numeric($_GET['exam_id']) ||
!isset($_GET['student_exam_id']) || !is_numeric($_GET['student_exam_id'])) {
header('Location: exam_schedules.php');
exit;
}
$exam_id = $_GET['exam_id'];
$student_exam_id = $_GET['student_exam_id'];
// Get exam details
$exam_query = "SELECT es.*, c.title as course_title
FROM exam_schedules es
JOIN courses c ON es.course_id = c.id
WHERE es.id = ?";
$stmt = $conn->prepare($exam_query);
$stmt->bind_param("i", $exam_id);
$stmt->execute();
$exam_result = $stmt->get_result();
if ($exam_result->num_rows === 0) {
header('Location: exam_schedules.php');
exit;
}
$exam = $exam_result->fetch_assoc();
// Get student exam details
$student_exam_query = "SELECT se.*,
u.id as user_id, u.first_name, u.last_name, u.email, u.id
FROM student_exams se
JOIN users u ON se.user_id = u.id
WHERE se.id = ? AND se.exam_id = ?";
$stmt = $conn->prepare($student_exam_query);
$stmt->bind_param("ii", $student_exam_id, $exam_id);
$stmt->execute();
$student_exam_result = $stmt->get_result();
if ($student_exam_result->num_rows === 0) {
header('Location: exam_results.php?exam_id=' . $exam_id);
exit;
}
$student_exam = $student_exam_result->fetch_assoc();
// Get student answers
$answers_query = "SELECT sa.*, q.question_text, q.question_type, q.marks, q.explanation
FROM student_answers sa
JOIN questions q ON sa.question_id = q.id
WHERE sa.student_exam_id = ?
ORDER BY q.id";
$stmt = $conn->prepare($answers_query);
$stmt->bind_param("i", $student_exam_id);
$stmt->execute();
$answers_result = $stmt->get_result();
$student_answers = [];
while ($answer = $answers_result->fetch_assoc()) {
$student_answers[] = $answer;
}
// Process form submission for grading answers
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['submit_grades'])) {
// Start transaction
$conn->begin_transaction();
try {
$total_score = 0;
$total_possible = 0;
foreach ($_POST['grades'] as $answer_id => $marks_obtained) {
// Update the student answer with marks and feedback
$feedback = $_POST['feedback'][$answer_id] ?? '';
$is_correct = ($marks_obtained > 0) ? 1 : 0;
$update_answer = "UPDATE student_answers
SET marks_obtained = ?, admin_remarks = ?, is_correct = ?
WHERE id = ?";
$stmt = $conn->prepare($update_answer);
$stmt->bind_param("dsis", $marks_obtained, $feedback, $is_correct, $answer_id);
$stmt->execute();
$total_score += $marks_obtained;
$total_possible += $_POST['max_marks'][$answer_id];
}
// Calculate percentage
$percentage = ($total_possible > 0) ? ($total_score / $total_possible) * 100 : 0;
// Update student exam record
$status = ($percentage >= $exam['passing_percentage']) ? 'passed' : 'failed';
$update_exam = "UPDATE student_exams
SET total_score = ?, percentage = ?, status = ?
WHERE id = ?";
$stmt = $conn->prepare($update_exam);
$stmt->bind_param("ddsi", $total_score, $percentage, $status, $student_exam_id);
$stmt->execute();
// Commit transaction
$conn->commit();
$success_message = "Student answers graded successfully.";
// Refresh student exam data
$stmt = $conn->prepare($student_exam_query);
$stmt->bind_param("ii", $student_exam_id, $exam_id);
$stmt->execute();
$student_exam_result = $stmt->get_result();
$student_exam = $student_exam_result->fetch_assoc();
// Refresh answers data
$stmt = $conn->prepare($answers_query);
$stmt->bind_param("i", $student_exam_id);
$stmt->execute();
$answers_result = $stmt->get_result();
$student_answers = [];
while ($answer = $answers_result->fetch_assoc()) {
$student_answers[] = $answer;
}
} catch (Exception $e) {
// Roll back transaction
$conn->rollback();
$error_message = "Error: " . $e->getMessage();
}
}
// Include header
include_once '../includes/header.php';
?>
<div class="container-fluid">
<div class="d-sm-flex align-items-center justify-content-between mb-4">
<h1 class="h3 mb-0 text-gray-800">Student Exam Answers</h1>
<a href="exam_results.php?exam_id=<?php echo $exam_id; ?>" class="btn btn-secondary btn-sm shadow-sm">
<i class="fas fa-arrow-left fa-sm text-white-50"></i> Back to Results
</a>
</div>
<?php if (isset($error_message)): ?>
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<?php echo $error_message; ?>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<?php endif; ?>
<?php if (isset($success_message)): ?>
<div class="alert alert-success alert-dismissible fade show" role="alert">
<?php echo $success_message; ?>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<?php endif; ?>
<!-- Overview Information Card -->
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-primary">Overview</h6>
</div>
<div class="card-body">
<div class="row">
<div class="col-md-6">
<h5>Exam Details</h5>
<table class="table table-bordered">
<tr>
<th width="120">Exam Title</th>
<td><?php echo htmlspecialchars($exam['title']); ?></td>
</tr>
<tr>
<th>Course</th>
<td><?php echo htmlspecialchars($exam['course_title']); ?></td>
</tr>
<tr>
<th>Date</th>
<td><?php echo date('d M, Y', strtotime($exam['exam_date'])); ?></td>
</tr>
<tr>
<th>Passing</th>
<td><?php echo $exam['passing_percentage']; ?>%</td>
</tr>
</table>
</div>
<div class="col-md-6">
<h5>Student Details</h5>
<table class="table table-bordered">
<tr>
<th width="120">Name</th>
<td><?php echo htmlspecialchars($student_exam['first_name'] . ' ' . $student_exam['last_name']); ?></td>
</tr>
<tr>
<th>Student ID</th>
<td><?php echo htmlspecialchars($student_exam['student_id']); ?></td>
</tr>
<tr>
<th>Email</th>
<td><?php echo htmlspecialchars($student_exam['email']); ?></td>
</tr>
<tr>
<th>Status</th>
<td>
<?php
$status_badge_class = '';
switch ($student_exam['status']) {
case 'pending':
$status_badge_class = 'secondary';
break;
case 'in_progress':
$status_badge_class = 'info';
break;
case 'completed':
$status_badge_class = 'primary';
break;
case 'graded':
$status_badge_class = 'warning';
break;
case 'passed':
$status_badge_class = 'success';
break;
case 'failed':
$status_badge_class = 'danger';
break;
default:
$status_badge_class = 'secondary';
}
?>
<span class="badge badge-<?php echo $status_badge_class; ?>">
<?php echo ucfirst(str_replace('_', ' ', $student_exam['status'])); ?>
</span>
<?php if ($student_exam['total_score'] !== null): ?>
<span class="ml-2">
Score: <?php echo $student_exam['total_score']; ?>
(<?php echo number_format($student_exam['percentage'], 2); ?>%)
</span>
<?php endif; ?>
</td>
</tr>
</table>
</div>
</div>
<div class="row mt-3">
<div class="col-md-12">
<div class="alert alert-info">
<strong>Instructions:</strong> Review the student's answers below. For essay and short answer questions,
enter the marks and provide feedback. Multiple choice questions are automatically graded.
</div>
</div>
</div>
</div>
</div>
<!-- Student Answers Form -->
<form method="post" action="">
<?php if (empty($student_answers)): ?>
<div class="alert alert-warning">
<i class="fas fa-exclamation-triangle"></i> No answers found for this student's exam attempt.
</div>
<?php else: ?>
<div class="card shadow mb-4">
<div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
<h6 class="m-0 font-weight-bold text-primary">Student Answers</h6>
<button type="submit" name="submit_grades" class="btn btn-primary btn-sm">
<i class="fas fa-save"></i> Save Grades
</button>
</div>
<div class="card-body">
<?php foreach ($student_answers as $index => $answer): ?>
<div class="question-answer-container mb-4 pb-4 border-bottom">
<div class="row">
<div class="col-md-12">
<h5>Question <?php echo $index + 1; ?></h5>
<div class="question-text mb-3">
<?php echo nl2br(htmlspecialchars($answer['question_text'])); ?>
</div>
<div class="row">
<div class="col-md-8">
<h6 class="font-weight-bold">Student's Answer:</h6>
<?php if ($answer['question_type'] === 'multiple_choice' || $answer['question_type'] === 'true_false'): ?>
<?php
// Get selected option text
$option_query = "SELECT option_text, is_correct FROM question_options WHERE id = ?";
$stmt = $conn->prepare($option_query);
$stmt->bind_param("i", $answer['selected_option_id']);
$stmt->execute();
$option_result = $stmt->get_result();
$option = $option_result->fetch_assoc();
if ($option) {
$badge_class = $option['is_correct'] ? 'success' : 'danger';
echo '<span class="badge badge-' . $badge_class . '">' . htmlspecialchars($option['option_text']) . '</span>';
} else {
echo '<span class="text-muted">No answer provided</span>';
}
?>
<?php elseif ($answer['question_type'] === 'short_answer' || $answer['question_type'] === 'essay'): ?>
<?php if (empty($answer['answer_text'])): ?>
<span class="text-muted">No answer provided</span>
<?php else: ?>
<div class="card">
<div class="card-body bg-light">
<?php echo nl2br(htmlspecialchars($answer['answer_text'])); ?>
</div>
</div>
<?php endif; ?>
<?php endif; ?>
<?php if (!empty($answer['explanation'])): ?>
<div class="mt-3">
<h6 class="font-weight-bold">Explanation:</h6>
<div class="explanation-text">
<?php echo nl2br(htmlspecialchars($answer['explanation'])); ?>
</div>
</div>
<?php endif; ?>
</div>
<div class="col-md-4">
<div class="card bg-light">
<div class="card-body">
<h6 class="font-weight-bold">Grading</h6>
<input type="hidden" name="max_marks[<?php echo $answer['id']; ?>]" value="<?php echo $answer['marks']; ?>">
<?php if ($answer['question_type'] === 'multiple_choice' || $answer['question_type'] === 'true_false'): ?>
<!-- Automatically graded questions -->
<div class="form-group">
<label>Marks:</label>
<input type="number" class="form-control"
name="grades[<?php echo $answer['id']; ?>]"
min="0" max="<?php echo $answer['marks']; ?>" step="0.5"
value="<?php echo $answer['is_correct'] ? $answer['marks'] : 0; ?>"
readonly>
<small class="form-text text-muted">
Max: <?php echo $answer['marks']; ?> marks (Auto-graded)
</small>
</div>
<?php else: ?>
<!-- Manually graded questions -->
<div class="form-group">
<label>Marks:</label>
<input type="number" class="form-control"
name="grades[<?php echo $answer['id']; ?>]"
min="0" max="<?php echo $answer['marks']; ?>" step="0.5"
value="<?php echo $answer['marks_obtained'] ?? 0; ?>">
<small class="form-text text-muted">
Max: <?php echo $answer['marks']; ?> marks
</small>
</div>
<?php endif; ?>
<div class="form-group mb-0">
<label>Feedback:</label>
<textarea class="form-control" name="feedback[<?php echo $answer['id']; ?>]" rows="2"><?php echo htmlspecialchars($answer['admin_remarks'] ?? ''); ?></textarea>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<?php endforeach; ?>
<div class="text-center mt-4">
<button type="submit" name="submit_grades" class="btn btn-primary">
<i class="fas fa-save"></i> Save Grades
</button>
</div>
</div>
</div>
<?php endif; ?>
</form>
</div>
<script>
$(document).ready(function() {
// Enable Bootstrap tooltips
$('[data-toggle="tooltip"]').tooltip();
});
</script>
<?php
// Include footer
include_once '../includes/footer.php';
?>