<?php
// Start session
session_start();
// Include database configuration
require_once '../../config/database.php';
// Check if user is logged in and has admin or director role
if (!isset($_SESSION['user_id']) || ($_SESSION['role'] !== 'admin' && $_SESSION['role'] !== 'director')) {
echo json_encode(['error' => 'Unauthorized access']);
exit;
}
// Check if question_id is provided
if (!isset($_POST['question_id']) || empty($_POST['question_id'])) {
echo "Invalid question ID";
exit;
}
$question_id = $_POST['question_id'];
// Get question details
$question_query = "SELECT q.*, b.title as bank_title, c.title as course_title
FROM questions q
LEFT JOIN question_banks b ON q.question_bank_id = b.id
LEFT JOIN courses c ON b.course_id = c.id
WHERE q.id = ?";
try {
$stmt = $conn->prepare($question_query);
$stmt->bind_param("i", $question_id);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows === 0) {
echo "Question not found";
exit;
}
$question = $result->fetch_assoc();
} catch (Exception $e) {
echo "Error retrieving question: " . $e->getMessage();
exit;
}
// Get options if it's a choice question
$options = [];
if ($question['question_type'] === 'multiple_choice' || $question['question_type'] === 'true_false') {
try {
$options_query = "SELECT * FROM question_options WHERE question_id = ? ORDER BY id";
$options_stmt = $conn->prepare($options_query);
$options_stmt->bind_param("i", $question_id);
$options_stmt->execute();
$options_result = $options_stmt->get_result();
while ($option = $options_result->fetch_assoc()) {
$options[] = $option;
}
} catch (Exception $e) {
// Handle the case when there's no question_options table or no options
// For older schema that might have used option_a, option_b, etc.
if (isset($question['option_a']) && !empty($question['option_a'])) {
$options[] = ['id' => 'a', 'option_text' => $question['option_a'], 'is_correct' => $question['correct_answer'] == 'a'];
}
if (isset($question['option_b']) && !empty($question['option_b'])) {
$options[] = ['id' => 'b', 'option_text' => $question['option_b'], 'is_correct' => $question['correct_answer'] == 'b'];
}
if (isset($question['option_c']) && !empty($question['option_c'])) {
$options[] = ['id' => 'c', 'option_text' => $question['option_c'], 'is_correct' => $question['correct_answer'] == 'c'];
}
if (isset($question['option_d']) && !empty($question['option_d'])) {
$options[] = ['id' => 'd', 'option_text' => $question['option_d'], 'is_correct' => $question['correct_answer'] == 'd'];
}
}
}
// Determine difficulty field
$difficulty = isset($question['difficulty']) ? $question['difficulty'] :
(isset($question['difficulty_level']) ? $question['difficulty_level'] : 'Unknown');
?>
<div class="question-info">
<div class="mb-3">
<h6 class="font-weight-bold">Question:</h6>
<p><?php echo nl2br(htmlspecialchars($question['question_text'])); ?></p>
</div>
<div class="mb-3">
<h6 class="font-weight-bold">Bank:</h6>
<p><?php echo htmlspecialchars($question['bank_title'] ?? 'Unknown'); ?>
<?php if (!empty($question['course_title'])): ?>
(<?php echo htmlspecialchars($question['course_title']); ?>)
<?php endif; ?>
</p>
</div>
<div class="mb-3">
<h6 class="font-weight-bold">Type:</h6>
<p>
<?php if ($question['question_type'] === 'true_false'): ?>
<span class="badge bg-info">True/False</span>
<?php elseif ($question['question_type'] === 'multiple_choice'): ?>
<span class="badge bg-primary">Multiple Choice</span>
<?php elseif ($question['question_type'] === 'short_answer'): ?>
<span class="badge bg-secondary">Short Answer</span>
<?php elseif ($question['question_type'] === 'essay'): ?>
<span class="badge bg-warning">Essay</span>
<?php else: ?>
<span class="badge bg-secondary"><?php echo ucfirst($question['question_type']); ?></span>
<?php endif; ?>
</p>
</div>
<div class="mb-3">
<h6 class="font-weight-bold">Difficulty:</h6>
<p>
<?php
$difficulty_class = '';
switch ($difficulty) {
case 'easy':
$difficulty_class = 'success';
break;
case 'medium':
$difficulty_class = 'warning';
break;
case 'hard':
$difficulty_class = 'danger';
break;
default:
$difficulty_class = 'secondary';
}
?>
<span class="badge bg-<?php echo $difficulty_class; ?>">
<?php echo ucfirst($difficulty); ?>
</span>
</p>
</div>
<div class="mb-3">
<h6 class="font-weight-bold">Marks:</h6>
<p><?php echo $question['marks']; ?></p>
</div>
<?php if ($question['question_type'] === 'multiple_choice' || $question['question_type'] === 'true_false'): ?>
<div class="mb-3">
<h6 class="font-weight-bold">Options:</h6>
<ul class="list-group">
<?php foreach ($options as $option): ?>
<li class="list-group-item <?php echo $option['is_correct'] ? 'list-group-item-success' : ''; ?>">
<?php echo htmlspecialchars($option['option_text']); ?>
<?php if ($option['is_correct']): ?>
<span class="float-end badge bg-success">Correct</span>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
</div>
<?php elseif ($question['question_type'] === 'short_answer' || $question['question_type'] === 'essay'): ?>
<div class="mb-3">
<h6 class="font-weight-bold">Correct Answer:</h6>
<p><?php echo nl2br(htmlspecialchars($question['correct_answer'] ?? 'Not specified')); ?></p>
</div>
<?php endif; ?>
<?php if (!empty($question['explanation'])): ?>
<div class="mb-3">
<h6 class="font-weight-bold">Explanation:</h6>
<p><?php echo nl2br(htmlspecialchars($question['explanation'])); ?></p>
</div>
<?php endif; ?>
</div>