36459-vm/admin/add_challenge.php
2025-11-29 17:28:26 +00:00

120 lines
4.5 KiB
PHP

<?php
session_start();
if (!isset($_SESSION['admin_logged_in']) || $_SESSION['admin_logged_in'] !== true) {
header('Location: login.php');
exit;
}
require_once '../db/config.php';
$errors = [];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$title = $_POST['title'] ?? '';
$description = $_POST['description'] ?? '';
$difficulty = $_POST['difficulty'] ?? '';
$learning_style = $_POST['learning_style'] ?? '';
$expected_output = $_POST['expected_output'] ?? '';
$sample_cases_json = $_POST['sample_cases_json'] ?? '';
if (empty($title)) {
$errors[] = 'Title is required';
}
if (empty($description)) {
$errors[] = 'Description is required';
}
if (empty($difficulty)) {
$errors[] = 'Difficulty is required';
}
if (empty($errors)) {
$pdo = db();
$stmt = $pdo->prepare('INSERT INTO challenges (title, description, difficulty, learning_style, expected_output, sample_cases_json) VALUES (?, ?, ?, ?, ?, ?)');
if ($stmt->execute([$title, $description, $difficulty, $learning_style, $expected_output, $sample_cases_json])) {
header('Location: challenges.php?success=true');
exit;
} else {
$errors[] = 'Failed to add challenge. Please try again.';
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Add Challenge</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container">
<a class="navbar-brand" href="index.php">Admin Panel</a>
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link" href="challenges.php">Challenges</a>
</li>
<li class="nav-item">
<a class="nav-link" href="competitions.php">Competitions</a>
</li>
<li class="nav-item">
<a class="nav-link" href="users.php">Users</a>
</li>
</ul>
<ul class="navbar-nav ms-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link" href="logout.php">Logout</a>
</li>
</ul>
</div>
</nav>
<div class="container py-5">
<h1 class="display-4 fw-bold">Add Challenge</h1>
<?php if (!empty($errors)): ?>
<div class="alert alert-danger">
<?php foreach ($errors as $error): ?>
<p><?php echo $error; ?></p>
<?php endforeach; ?>
</div>
<?php endif; ?>
<form action="add_challenge.php" method="post">
<div class="mb-3">
<label for="title" class="form-label">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="5" required></textarea>
</div>
<div class="mb-3">
<label for="difficulty" class="form-label">Difficulty</label>
<select class="form-select" id="difficulty" name="difficulty" required>
<option value="Easy">Easy</option>
<option value="Medium">Medium</option>
<option value="Hard">Hard</option>
</select>
</div>
<div class="mb-3">
<label for="learning_style" class="form-label">Learning Style</label>
<input type="text" class="form-control" id="learning_style" name="learning_style">
</div>
<div class="mb-3">
<label for="expected_output" class="form-label">Expected Output</label>
<textarea class="form-control" id="expected_output" name="expected_output" rows="3"></textarea>
</div>
<div class="mb-3">
<label for="sample_cases_json" class="form-label">Sample Cases (JSON)</label>
<textarea class="form-control" id="sample_cases_json" name="sample_cases_json" rows="5"></textarea>
</div>
<button type="submit" class="btn btn-primary">Add Challenge</button>
</form>
</div>
</body>
</html>