Path : /home/vishqocm/pcib.in/admin/ajax/
File Upload :
Current File : /home/vishqocm//pcib.in/admin/ajax/get_bank_questions.php

<?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')) {
    die(json_encode(['status' => 'error', 'message' => 'Unauthorized access']));
}

// Check if bank_id and exam_id are provided
if (!isset($_POST['bank_id']) || !is_numeric($_POST['bank_id']) || !isset($_POST['exam_id']) || !is_numeric($_POST['exam_id'])) {
    die('<div class="alert alert-danger">Invalid parameters</div>');
}

$bank_id = $_POST['bank_id'];
$exam_id = $_POST['exam_id'];

// Check if exam_question_maps table exists
$check_table = $conn->query("SHOW TABLES LIKE 'exam_question_maps'");
$exam_question_maps_exists = $check_table->num_rows > 0;

if (!$exam_question_maps_exists) {
    die('<div class="alert alert-danger">exam_question_maps table does not exist. Please run "Fix Database Tables" first.</div>');
}

// Get questions that are not already in the exam
$questions_query = "SELECT q.* FROM questions q 
                   WHERE q.question_bank_id = ? 
                   AND q.id NOT IN (
                       SELECT question_id FROM exam_question_maps WHERE exam_id = ?
                   )
                   ORDER BY q.difficulty, q.id";

try {
    $stmt = $conn->prepare($questions_query);
    $stmt->bind_param("ii", $bank_id, $exam_id);
    $stmt->execute();
    $questions_result = $stmt->get_result();
} catch (Exception $e) {
    die('<div class="alert alert-danger">Error fetching questions: ' . $e->getMessage() . '</div>');
}

// Get question bank info
$bank_query = "SELECT * FROM question_banks WHERE id = ?";
$stmt = $conn->prepare($bank_query);
$stmt->bind_param("i", $bank_id);
$stmt->execute();
$bank_result = $stmt->get_result();
$bank = $bank_result->fetch_assoc();

if (!$bank) {
    die('<div class="alert alert-danger">Question bank not found</div>');
}
?>

<div class="mb-3">
    <h5><?php echo htmlspecialchars($bank['title']); ?> - Available Questions</h5>
    <?php if ($questions_result->num_rows === 0): ?>
        <div class="alert alert-info">
            <i class="fas fa-info-circle"></i> No available questions found in this bank, or all questions are already in the exam.
            <a href="add_question.php?bank_id=<?php echo $bank_id; ?>" target="_blank" class="alert-link">Add new questions</a>
        </div>
    <?php else: ?>
        <div class="alert alert-success">
            <div class="d-flex justify-content-between align-items-center">
                <span><strong><span class="selected-count">0 questions selected</span></strong></span>
                <div>
                    <button type="button" class="btn btn-sm btn-outline-primary" id="select_all">Select All</button>
                    <button type="button" class="btn btn-sm btn-outline-secondary" id="deselect_all">Deselect All</button>
                </div>
            </div>
        </div>
        <div class="table-responsive">
            <table class="table table-bordered" id="availableQuestionsTable" width="100%" cellspacing="0">
                <thead>
                    <tr>
                        <th width="5%">
                            <div class="form-check">
                                <input type="checkbox" class="form-check-input" id="selectAllCheckbox">
                                <label class="form-check-label" for="selectAllCheckbox"></label>
                            </div>
                        </th>
                        <th width="35%">Question</th>
                        <th width="15%">Type</th>
                        <th width="15%">Difficulty</th>
                        <th width="10%">Marks</th>
                        <th width="15%">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php while ($question = $questions_result->fetch_assoc()): ?>
                        <tr>
                            <td>
                                <div class="form-check">
                                    <input type="checkbox" class="form-check-input question-checkbox" 
                                           id="question<?php echo $question['id']; ?>" 
                                           name="selected_questions[]" 
                                           value="<?php echo $question['id']; ?>">
                                    <label class="form-check-label" for="question<?php echo $question['id']; ?>"></label>
                                </div>
                            </td>
                            <td><?php echo htmlspecialchars(substr($question['question_text'], 0, 100)) . (strlen($question['question_text']) > 100 ? '...' : ''); ?></td>
                            <td>
                                <?php 
                                switch ($question['question_type']) {
                                    case 'multiple_choice':
                                        echo 'Multiple Choice';
                                        break;
                                    case 'true_false':
                                        echo 'True/False';
                                        break;
                                    case 'short_answer':
                                        echo 'Short Answer';
                                        break;
                                    case 'essay':
                                        echo 'Essay';
                                        break;
                                    default:
                                        echo ucfirst($question['question_type']);
                                }
                                ?>
                            </td>
                            <td>
                                <?php 
                                $difficulty_class = '';
                                $difficulty_field = isset($question['difficulty']) ? 'difficulty' : 
                                                  (isset($question['difficulty_level']) ? 'difficulty_level' : '');
                                                  
                                if ($difficulty_field && isset($question[$difficulty_field])) {
                                    switch ($question[$difficulty_field]) {
                                        case 'easy':
                                            $difficulty_class = 'success';
                                            break;
                                        case 'medium':
                                            $difficulty_class = 'warning';
                                            break;
                                        case 'hard':
                                            $difficulty_class = 'danger';
                                            break;
                                        default:
                                            $difficulty_class = 'secondary';
                                    }
                                    
                                    echo '<span class="badge bg-'.$difficulty_class.'">';
                                    echo ucfirst($question[$difficulty_field]).'</span>';
                                } else {
                                    echo '<span class="badge bg-secondary">Unknown</span>';
                                }
                                ?>
                            </td>
                            <td><?php echo $question['marks']; ?></td>
                            <td>
                                <button type="button" class="btn btn-info btn-sm view-question" data-id="<?php echo $question['id']; ?>">
                                    <i class="fas fa-eye"></i> View
                                </button>
                            </td>
                        </tr>
                    <?php endwhile; ?>
                </tbody>
            </table>
        </div>
        
        <script>
            // Update the count of selected questions
            $(document).ready(function() {
                $('.question-checkbox').change(function() {
                    var count = $('.question-checkbox:checked').length;
                    $('.selected-count').text(count + ' question' + (count !== 1 ? 's' : '') + ' selected');
                });
            });
        </script>
    <?php endif; ?>
</div>