File "submit_certification.php"
Full Path: /home/ccipcixf/public_html/miportal/submit_certification.php
File size: 3.5 KB
MIME-type: text/x-php
Charset: utf-8
<?php
session_start();
include("admin/includes/config.php"); // Include your PDO config for the database connection
// Check if the user is logged in
if (!isset($_SESSION['user_acc_id'])) {
die("Unauthorized access");
}
$user_id = $_SESSION['user_acc_id']; // Get the logged-in user's ID
// Define the correct answers
$correct_answers = [
'q1' => 'C',
'q2' => 'B',
'q3' => 'B',
'q4' => 'B',
'q5' => 'C',
'q6' => 'B',
'q7' => 'B',
'q8' => 'C',
'q9' => 'A',
'q10' => 'B'
];
// Initialize score
$score = 0;
// Collect answers from the form and calculate score
$user_answers = [];
foreach ($correct_answers as $question => $correct_answer) {
if (isset($_POST[$question])) {
$user_answers[$question] = $_POST[$question];
if ($_POST[$question] === $correct_answer) {
$score++;
}
} else {
$user_answers[$question] = null; // Record unanswered questions
}
}
// Determine if the user is Qualified
$pass_score = 8; // 80% passing threshold
$status = ($score >= $pass_score) ? 'Qualified' : 'Not Qualified';
try {
// Check if the user has previously attempted the test
$check_query = "SELECT status FROM mystery_shopper_certification WHERE user_id = :user_id LIMIT 1";
$check_stmt = $pdo->prepare($check_query);
$check_stmt->execute(['user_id' => $user_id]);
$existing_attempt = $check_stmt->fetch();
if ($existing_attempt) {
// If the user has a previous attempt, update the existing record
$query = "UPDATE mystery_shopper_certification SET
q1 = :q1, q2 = :q2, q3 = :q3, q4 = :q4, q5 = :q5, q6 = :q6, q7 = :q7,
q8 = :q8, q9 = :q9, q10 = :q10, score = :score, status = :status
WHERE user_id = :user_id";
} else {
// If no previous attempt, insert a new record
$query = "INSERT INTO mystery_shopper_certification
(user_id, q1, q2, q3, q4, q5, q6, q7, q8, q9, q10, score, status)
VALUES
(:user_id, :q1, :q2, :q3, :q4, :q5, :q6, :q7, :q8, :q9, :q10, :score, :status)";
}
$stmt = $pdo->prepare($query);
$stmt->execute([
':user_id' => $user_id,
':q1' => $user_answers['q1'],
':q2' => $user_answers['q2'],
':q3' => $user_answers['q3'],
':q4' => $user_answers['q4'],
':q5' => $user_answers['q5'],
':q6' => $user_answers['q6'],
':q7' => $user_answers['q7'],
':q8' => $user_answers['q8'],
':q9' => $user_answers['q9'],
':q10' => $user_answers['q10'],
':score' => $score,
':status' => $status
]);
// If user is qualified, update their status in `tb_user` to "active"
if ($status === 'Qualified') {
$update_query = "UPDATE tb_user SET `status` = 'active' WHERE user_id = :user_id";
$update_stmt = $pdo->prepare($update_query);
$update_stmt->execute([':user_id' => $user_id]);
}
// Display results
echo "<h3>Test Result</h3>";
echo "<p>Your Score: $score / 10</p>";
echo "<p>Status: $status</p>";
if ($status === 'Qualified') {
echo "<p>Congratulations! You have qualified as a Mystery Shopper.</p>";
} else {
echo "<p>Unfortunately, you did not qualify. Please try again.</p>";
}
} catch (PDOException $e) {
error_log("Database Error: " . $e->getMessage());
die("An error occurred while saving your results. Please try again later.");
}
?>