
PK 
<?php
header('Content-Type: application/json');
ob_start();
@session_start();
ini_set('allow_url_include', 1);
date_default_timezone_set("Asia/Kolkata");
set_time_limit(600);
ini_set('max_execution_time', 600);
require_once 'admin/includes/settings/PDODB.php';
include 'admin/includes/modules/functions.php';
if (empty($_SESSION['userId'])) {
header("Location: index.php");
exit();
}
try {
$function = new FUNCTIONS();
$questionId = isset($_GET['question_id']) ? (int)$_GET['question_id'] : 0;
// Get all unanswered questions for this user
$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 answered question IDs for this user
$stmt = $db->prepare("SELECT question_id FROM quiz_answers WHERE user_id = ?");
$stmt->execute([$_SESSION['userId']]);
$answeredIds = $stmt->fetchAll(PDO::FETCH_COLUMN);
// Get all active questions
$allQuestions = $function->getQuizQuestion($qid = null, $isActive = 1, $status = 'Active', $orderBy = 'ASC');
// Filter out answered questions
$unansweredQuestions = array_filter($allQuestions, function ($q) use ($answeredIds) {
return !in_array($q['qid'], $answeredIds);
});
$total_question = count($unansweredQuestions);
if (empty($_SESSION['total_question'])) {
$_SESSION['total_question'] = $total_question;
}
if (!empty($_SESSION['total_question']) && $total_question > $_SESSION['total_question']) {
$_SESSION['total_question'] = $total_question;
}
if (empty($unansweredQuestions)) {
throw new Exception('No more questions available');
}
// Reset array keys and get the question at the requested index
$unansweredQuestions = array_values($unansweredQuestions);
if (!isset($unansweredQuestions[$questionId])) {
// If we've reached the end, return the first question (or handle differently)
$question = $unansweredQuestions[0];
$questionId = 0; // Reset index
} else {
$question = $unansweredQuestions[$questionId];
}
$total = (!empty($_SESSION['total_question'])) ? $_SESSION['total_question'] : $total_question;
echo json_encode([
'success' => true,
'question' => $question,
'current' => ($questionId) ? $questionId + 1 : 0,
'total' => $total
]);
} catch (Exception $e) {
echo json_encode([
'success' => false,
'message' => $e->getMessage()
]);
}
?>


PK 99