
PK 
<?php
ob_start();
header('Content-Type: application/json');
error_reporting(E_ALL);
ini_set('display_errors', 0);
ini_set('log_errors', 1);
//ini_set('error_log', 'quiz_errors.log');
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
// Validate session
if (!isset($_SESSION['userId'])) {
echo json_encode(['success' => false, 'message' => 'User not logged in']);
exit;
}
// Get and validate input
$input = json_decode(file_get_contents('php://input'), true);
if (json_last_error() !== JSON_ERROR_NONE) {
echo json_encode(['success' => false, 'message' => 'Invalid JSON input']);
exit;
}
$questionId = $input['question_id'] ?? null;
$userAnswer = isset($input['user_answer']) ? trim($input['user_answer']) : null;
if (!$questionId || $userAnswer === null || $userAnswer === '') {
echo json_encode(['success' => false, 'message' => 'Invalid input data']);
exit;
}
try {
// Create PDO connection
$dsn = 'mysql:host=localhost;dbname=questend_techconjiffyfilms;charset=utf8';
$db = new PDO($dsn, 'questend_Usrjiffyfilms', 'hZy_VO*$VoWVFi+=;3M?FWazvx,=le=U');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Get question data
$query = "SELECT qid, question, ans1, ans2, ans3, ans4, correct_answer
FROM quiz_questions
WHERE qid = ?";
$stmt = $db->prepare($query);
$stmt->execute([$questionId]);
$question = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$question) {
throw new Exception("Question not found in database");
}
// Normalize user answer (accept both 'A' or full answer text)
$userAnswerLetter = null;
$normalizedUserAnswer = strtoupper(trim($userAnswer));
// Check if user provided a letter (A-D)
if (in_array($normalizedUserAnswer, ['A','B','C','D'])) {
$userAnswerLetter = $normalizedUserAnswer;
}
// Otherwise check if answer text matches any option
else {
$answerOptions = [
'A' => trim($question['ans1']),
'B' => trim($question['ans2']),
'C' => trim($question['ans3']),
'D' => trim($question['ans4'])
];
foreach ($answerOptions as $letter => $text) {
if (strcasecmp(trim($userAnswer), $text) === 0) {
$userAnswerLetter = $letter;
break;
}
}
}
// Validate answer format
if (!$userAnswerLetter) {
throw new Exception("Invalid answer format");
}
// Compare with correct answer (which is stored as A-D)
$isCorrect = ($userAnswerLetter === trim($question['correct_answer']));
// Save the answer attempt
$insertQuery = "INSERT INTO quiz_answers
(user_id, question_id, user_answer, answer, is_correct)
VALUES (?, ?, ?, ?, ?)";
$stmt = $db->prepare($insertQuery);
$stmt->execute([
$_SESSION['userId'],
$questionId,
$userAnswer,
$question['correct_answer'],
$isCorrect ? 1 : 0
]);
// Update user points if correct
if ($isCorrect) {
$updateQuery = "UPDATE users SET points = points + 2 WHERE id = ?";
$stmt = $db->prepare($updateQuery);
$stmt->execute([$_SESSION['userId']]);
}
// Prepare response
$response = [
'success' => true,
'is_correct' => $isCorrect,
'correct_answer' => $question['correct_answer'], // Returns 'A','B','C', or 'D'
'correct_answer_text' => $question['ans'.ord($question['correct_answer']) - 64], // Returns full text
'user_answer' => $userAnswer,
'user_answer_letter' => $userAnswerLetter
];
} catch (PDOException $e) {
error_log("Database Error: " . $e->getMessage());
$response = [
'success' => false,
'message' => 'Database error occurred',
'error' => $e->getMessage()
];
} catch (Exception $e) {
error_log("General Error: " . $e->getMessage());
$response = [
'success' => false,
'message' => 'Error processing your answer',
'error' => $e->getMessage()
];
}
ob_end_clean();
echo json_encode($response);
exit;


PK 99