<?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;
}
// Process form submission
$success_message = '';
$error_message = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['save_settings'])) {
try {
// Start transaction
$conn->begin_transaction();
// Process each setting
$settings = [
'default_passing_percentage' => $_POST['default_passing_percentage'],
'default_duration_minutes' => $_POST['default_duration_minutes'],
'randomize_questions' => isset($_POST['randomize_questions']) ? 1 : 0,
'randomize_options' => isset($_POST['randomize_options']) ? 1 : 0,
'show_results_immediately' => isset($_POST['show_results_immediately']) ? 1 : 0,
'allow_exam_retake' => isset($_POST['allow_exam_retake']) ? 1 : 0,
'max_retake_attempts' => $_POST['max_retake_attempts'],
'retake_waiting_period_days' => $_POST['retake_waiting_period_days'],
'block_exam_navigation' => isset($_POST['block_exam_navigation']) ? 1 : 0,
'exam_result_certificate' => isset($_POST['exam_result_certificate']) ? 1 : 0
];
foreach ($settings as $key => $value) {
// Check if setting exists
$check_query = "SELECT id FROM exam_settings WHERE setting_key = ?";
$stmt = $conn->prepare($check_query);
$stmt->bind_param("s", $key);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
// Update existing setting
$update_query = "UPDATE exam_settings SET setting_value = ? WHERE setting_key = ?";
$stmt = $conn->prepare($update_query);
$stmt->bind_param("ss", $value, $key);
$stmt->execute();
} else {
// Insert new setting
$insert_query = "INSERT INTO exam_settings (setting_key, setting_value) VALUES (?, ?)";
$stmt = $conn->prepare($insert_query);
$stmt->bind_param("ss", $key, $value);
$stmt->execute();
}
}
// Commit transaction
$conn->commit();
$success_message = "Exam settings updated successfully.";
} catch (Exception $e) {
// Roll back transaction
$conn->rollback();
$error_message = "Error updating settings: " . $e->getMessage();
}
}
// Get current settings
$settings = [];
$settings_query = "SELECT * FROM exam_settings";
$settings_result = $conn->query($settings_query);
if ($settings_result && $settings_result->num_rows > 0) {
while ($row = $settings_result->fetch_assoc()) {
$settings[$row['setting_key']] = $row['setting_value'];
}
}
// Set default values if not present
$default_settings = [
'default_passing_percentage' => 40,
'default_duration_minutes' => 60,
'randomize_questions' => 1,
'randomize_options' => 1,
'show_results_immediately' => 1,
'allow_exam_retake' => 1,
'max_retake_attempts' => 3,
'retake_waiting_period_days' => 7,
'block_exam_navigation' => 0,
'exam_result_certificate' => 1
];
foreach ($default_settings as $key => $value) {
if (!isset($settings[$key])) {
$settings[$key] = $value;
}
}
// 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">Exam System Settings</h1>
<a href="dashboard.php" class="btn btn-primary btn-sm shadow-sm">
<i class="fas fa-arrow-left fa-sm text-white-50"></i> Back to Exam Dashboard
</a>
</div>
<?php if ($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; ?>
<?php if ($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; ?>
<form method="post" action="">
<div class="row">
<div class="col-lg-6">
<!-- General Settings -->
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-primary">General Exam Settings</h6>
</div>
<div class="card-body">
<div class="form-group">
<label for="default_passing_percentage">Default Passing Percentage</label>
<div class="input-group">
<input type="number" class="form-control" id="default_passing_percentage"
name="default_passing_percentage" min="1" max="100"
value="<?php echo $settings['default_passing_percentage']; ?>">
<div class="input-group-append">
<span class="input-group-text">%</span>
</div>
</div>
<small class="form-text text-muted">
This is the default passing percentage for all exams. This can be overridden for individual exams.
</small>
</div>
<div class="form-group">
<label for="default_duration_minutes">Default Exam Duration</label>
<div class="input-group">
<input type="number" class="form-control" id="default_duration_minutes"
name="default_duration_minutes" min="1"
value="<?php echo $settings['default_duration_minutes']; ?>">
<div class="input-group-append">
<span class="input-group-text">minutes</span>
</div>
</div>
<small class="form-text text-muted">
This is the default duration for all exams. This can be overridden for individual exams.
</small>
</div>
<div class="form-group">
<div class="custom-control custom-switch">
<input type="checkbox" class="custom-control-input" id="randomize_questions"
name="randomize_questions" <?php echo $settings['randomize_questions'] ? 'checked' : ''; ?>>
<label class="custom-control-label" for="randomize_questions">Randomize Questions</label>
</div>
<small class="form-text text-muted">
If enabled, the order of questions will be randomized for each student.
</small>
</div>
<div class="form-group">
<div class="custom-control custom-switch">
<input type="checkbox" class="custom-control-input" id="randomize_options"
name="randomize_options" <?php echo $settings['randomize_options'] ? 'checked' : ''; ?>>
<label class="custom-control-label" for="randomize_options">Randomize Multiple Choice Options</label>
</div>
<small class="form-text text-muted">
If enabled, the order of options in multiple choice questions will be randomized for each student.
</small>
</div>
<div class="form-group">
<div class="custom-control custom-switch">
<input type="checkbox" class="custom-control-input" id="block_exam_navigation"
name="block_exam_navigation" <?php echo $settings['block_exam_navigation'] ? 'checked' : ''; ?>>
<label class="custom-control-label" for="block_exam_navigation">Block Navigation During Exam</label>
</div>
<small class="form-text text-muted">
If enabled, students cannot navigate away from the exam page once they start the exam.
</small>
</div>
</div>
</div>
</div>
<div class="col-lg-6">
<!-- Results and Retakes -->
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-primary">Results and Retakes</h6>
</div>
<div class="card-body">
<div class="form-group">
<div class="custom-control custom-switch">
<input type="checkbox" class="custom-control-input" id="show_results_immediately"
name="show_results_immediately" <?php echo $settings['show_results_immediately'] ? 'checked' : ''; ?>>
<label class="custom-control-label" for="show_results_immediately">Show Results Immediately</label>
</div>
<small class="form-text text-muted">
If enabled, students will see their results immediately after completing the exam.
If disabled, results will be shown only after manual grading by admin.
</small>
</div>
<div class="form-group">
<div class="custom-control custom-switch">
<input type="checkbox" class="custom-control-input" id="allow_exam_retake"
name="allow_exam_retake" <?php echo $settings['allow_exam_retake'] ? 'checked' : ''; ?>>
<label class="custom-control-label" for="allow_exam_retake">Allow Exam Retakes</label>
</div>
<small class="form-text text-muted">
If enabled, students can retake exams if they fail.
</small>
</div>
<div class="form-group">
<label for="max_retake_attempts">Maximum Retake Attempts</label>
<input type="number" class="form-control" id="max_retake_attempts"
name="max_retake_attempts" min="1"
value="<?php echo $settings['max_retake_attempts']; ?>">
<small class="form-text text-muted">
The maximum number of times a student can retake an exam after failing.
</small>
</div>
<div class="form-group">
<label for="retake_waiting_period_days">Retake Waiting Period</label>
<div class="input-group">
<input type="number" class="form-control" id="retake_waiting_period_days"
name="retake_waiting_period_days" min="0"
value="<?php echo $settings['retake_waiting_period_days']; ?>">
<div class="input-group-append">
<span class="input-group-text">days</span>
</div>
</div>
<small class="form-text text-muted">
The number of days a student must wait before retaking an exam. Set to 0 for immediate retakes.
</small>
</div>
<div class="form-group">
<div class="custom-control custom-switch">
<input type="checkbox" class="custom-control-input" id="exam_result_certificate"
name="exam_result_certificate" <?php echo $settings['exam_result_certificate'] ? 'checked' : ''; ?>>
<label class="custom-control-label" for="exam_result_certificate">Generate Certificates for Passed Exams</label>
</div>
<small class="form-text text-muted">
If enabled, a certificate will be generated when a student passes an exam.
</small>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-12">
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-primary">Database Maintenance</h6>
</div>
<div class="card-body">
<p>
Make sure the required tables for the exam system exist in your database. If not, click the button below to create them.
</p>
<a href="setup_tables.php" class="btn btn-warning">
<i class="fas fa-database"></i> Check & Create Exam Tables
</a>
</div>
</div>
</div>
</div>
<div class="text-center mb-4">
<button type="submit" name="save_settings" class="btn btn-primary btn-lg">
<i class="fas fa-save"></i> Save Settings
</button>
</div>
</form>
</div>
<?php
// Include footer
include_once 'includes/footer.php';
?>