164 lines
6.7 KiB
PHP
164 lines
6.7 KiB
PHP
<?php
|
|
ini_set('display_errors', 1);
|
|
error_reporting(E_ALL);
|
|
|
|
require_once 'db/config.php';
|
|
require_once 'includes/header.php';
|
|
|
|
// Block access if not logged in or not a psychologist
|
|
if (!isset($_SESSION["user_id"]) || !isset($_SESSION["role"]) || $_SESSION["role"] !== 'psychologist') {
|
|
header("Location: login.php");
|
|
exit();
|
|
}
|
|
|
|
$message = '';
|
|
$error = '';
|
|
|
|
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
|
$title = trim($_POST['title'] ?? '');
|
|
$description = trim($_POST['description'] ?? '');
|
|
$is_public = isset($_POST['is_public']) ? 1 : 0;
|
|
$questions = $_POST['questions'] ?? [];
|
|
$user_id = $_SESSION['user_id'];
|
|
|
|
if (empty($title) || empty($description) || empty($questions)) {
|
|
$error = "Please fill out the title, description, and add at least one question.";
|
|
} else {
|
|
$pdo = db();
|
|
if ($pdo) {
|
|
try {
|
|
$pdo->beginTransaction();
|
|
|
|
// Insert test
|
|
$stmt = $pdo->prepare("INSERT INTO tests (user_id, title, description, is_public) VALUES (?, ?, ?, ?)");
|
|
$stmt->execute([$user_id, $title, $description, $is_public]);
|
|
$test_id = $pdo->lastInsertId();
|
|
|
|
// Insert questions and options
|
|
foreach ($questions as $q_data) {
|
|
$question_text = trim($q_data['text'] ?? '');
|
|
if (empty($question_text)) continue;
|
|
|
|
$stmt = $pdo->prepare("INSERT INTO questions (test_id, question_text) VALUES (?, ?)");
|
|
$stmt->execute([$test_id, $question_text]);
|
|
$question_id = $pdo->lastInsertId();
|
|
|
|
$options = $q_data['options'] ?? [];
|
|
$correct_option_index = isset($q_data['correct']) ? intval($q_data['correct']) : -1;
|
|
|
|
foreach ($options as $index => $option_text) {
|
|
$option_text = trim($option_text);
|
|
if(empty($option_text)) continue;
|
|
|
|
$is_correct = ($index === $correct_option_index);
|
|
$stmt = $pdo->prepare("INSERT INTO options (question_id, option_text, is_correct) VALUES (?, ?, ?)");
|
|
$stmt->execute([$question_id, $option_text, $is_correct]);
|
|
}
|
|
}
|
|
|
|
$pdo->commit();
|
|
$message = "Test created successfully!";
|
|
} catch (PDOException $e) {
|
|
$pdo->rollBack();
|
|
$error = "Database error: " . $e->getMessage();
|
|
}
|
|
} else {
|
|
$error = "Could not connect to the database.";
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
|
|
<main class="container py-5">
|
|
<div class="row justify-content-center">
|
|
<div class="col-md-8">
|
|
<h2 class="mb-4">Create a New Test</h2>
|
|
|
|
<?php if ($message): ?>
|
|
<div class="alert alert-success"><?php echo $message; ?></div>
|
|
<?php endif; ?>
|
|
<?php if ($error): ?>
|
|
<div class="alert alert-danger"><?php echo $error; ?></div>
|
|
<?php endif; ?>
|
|
|
|
<form action="create_test.php" method="POST" id="create-test-form">
|
|
<div class="mb-3">
|
|
<label for="title" class="form-label">Test Title</label>
|
|
<input type="text" class="form-control" id="title" name="title" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="description" class="form-label">Description</label>
|
|
<textarea class="form-control" id="description" name="description" rows="3" required></textarea>
|
|
</div>
|
|
<div class="mb-3 form-check">
|
|
<input type="checkbox" class="form-check-input" id="is_public" name="is_public" value="1" checked>
|
|
<label class="form-check-label" for="is_public">Make this test public</label>
|
|
</div>
|
|
|
|
<hr class="my-4">
|
|
|
|
<h4>Questions</h4>
|
|
<div id="questions-container">
|
|
<!-- Questions will be added here dynamically -->
|
|
</div>
|
|
<button type="button" class="btn btn-secondary mt-2" id="add-question">Add Question</button>
|
|
|
|
<hr class="my-4">
|
|
|
|
<button type="submit" class="btn btn-primary">Create Test</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</main>
|
|
|
|
<script>
|
|
document.addEventListener('DOMContentLoaded', function () {
|
|
const questionsContainer = document.getElementById('questions-container');
|
|
const addQuestionBtn = document.getElementById('add-question');
|
|
let questionCounter = 0;
|
|
|
|
addQuestionBtn.addEventListener('click', function () {
|
|
questionCounter++;
|
|
const questionId = `q_${questionCounter}`;
|
|
|
|
const questionBlock = document.createElement('div');
|
|
questionBlock.className = 'question-block border p-3 mb-3';
|
|
questionBlock.innerHTML = `
|
|
<div class="mb-2">
|
|
<label for="${questionId}_text" class="form-label fw-bold">Question ${questionCounter}</label>
|
|
<input type="text" class="form-control" id="${questionId}_text" name="questions[${questionCounter}][text]" required>
|
|
</div>
|
|
<div class="options-container ms-3">
|
|
<label class="form-label">Options</label>
|
|
<!-- Options will be added here -->
|
|
</div>
|
|
<button type="button" class="btn btn-sm btn-outline-secondary ms-3 mt-2 add-option-btn">Add Option</button>
|
|
`;
|
|
questionsContainer.appendChild(questionBlock);
|
|
});
|
|
|
|
questionsContainer.addEventListener('click', function(e) {
|
|
if (e.target && e.target.classList.contains('add-option-btn')) {
|
|
const questionBlock = e.target.closest('.question-block');
|
|
const questionIndex = Array.from(questionsContainer.children).indexOf(questionBlock);
|
|
const optionsContainer = questionBlock.querySelector('.options-container');
|
|
const optionCounter = optionsContainer.querySelectorAll('.input-group').length;
|
|
|
|
const optionBlock = document.createElement('div');
|
|
optionBlock.className = 'input-group input-group-sm mb-2';
|
|
optionBlock.innerHTML = `
|
|
<div class="input-group-text">
|
|
<input class="form-check-input mt-0" type="radio" value="${optionCounter}" name="questions[${questionIndex + 1}][correct]" required>
|
|
</div>
|
|
<input type="text" class="form-control" name="questions[${questionIndex + 1}][options][]" required>
|
|
`;
|
|
optionsContainer.appendChild(optionBlock);
|
|
}
|
|
});
|
|
});
|
|
</script>
|
|
|
|
<?php require_once 'includes/footer.php'; // Assuming you might have a footer ?>
|
|
</body>
|
|
</html>
|