| Server IP : 158.247.231.215 / Your IP : 216.73.216.10 Web Server : Apache/2.4.41 (Ubuntu) System : Linux CTMS 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64 User : www-data ( 33) PHP Version : 8.0.30 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /mnt/blockstorage/ctms/api/custom-api/ |
Upload File : |
<?php
/**
* Get User Quiz Attempts API
* GET /api/custom-api/user-quiz-attempts.php
* Requires: Authorization header with Bearer token
*/
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, OPTIONS');
header("Access-Control-Allow-Headers: X-Requested-With, Content-Type, Authorization");
header('Content-Type: application/json');
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
http_response_code(200);
exit();
}
// Load WordPress
require_once('/mnt/blockstorage/ctms/wp-load.php');
require_once('auth-helper.php');
if ($_SERVER['REQUEST_METHOD'] !== 'GET') {
send_error_response('Only GET requests are allowed', 405);
}
// Verify JWT token
$user_id = verify_jwt_token();
if (!$user_id) {
send_error_response('User not authenticated', 401);
}
global $wpdb;
$quiz_attempts_table = $wpdb->prefix . 'quiz_attempts';
$quiz_attempt_data_table = $wpdb->prefix . 'quiz_attempt_data';
$users_table = $wpdb->prefix . 'users';
$quizzes_table = $wpdb->prefix . 'quizzes';
$questions_table = $wpdb->prefix . 'quiz_questions';
// Check if tables exist
$table_exists = $wpdb->get_var("SHOW TABLES LIKE '$quiz_attempts_table'");
if (!$table_exists) {
send_error_response('Quiz attempts table not found', 404);
}
// Retrieve all quiz attempts for the user
$quiz_attempts = $wpdb->get_results($wpdb->prepare("
SELECT
ua.id AS attempt_id,
u.display_name AS student_name,
ua.score,
ua.datetime,
c.name AS class_name,
q.title AS quiz_title
FROM $quiz_attempts_table ua
INNER JOIN $quizzes_table q ON ua.quiz_id = q.id
INNER JOIN $users_table u ON ua.student_id = u.ID
INNER JOIN {$wpdb->prefix}class_enrollment ce ON ua.student_id = ce.student_id
INNER JOIN {$wpdb->prefix}classes c ON ce.class_id = c.id
WHERE ua.student_id = %d
ORDER BY ua.datetime DESC
", $user_id));
if (empty($quiz_attempts)) {
send_json_response(array(
'success' => true,
'quiz_attempts' => array(),
'total_count' => 0,
), 200);
}
// Format the results
$formatted_attempts = array();
foreach ($quiz_attempts as $attempt) {
$attempt_id = $attempt->attempt_id;
// Get detailed results for this attempt
$details_results = $wpdb->get_results($wpdb->prepare("
SELECT qq.title AS question_title, qad.answer, qad.is_correct
FROM $quiz_attempt_data_table qad
INNER JOIN $questions_table qq ON qad.question_id = qq.id
WHERE qad.quiz_attempt_id = %d
", $attempt_id));
$correct_answers = 0;
$total_questions = count($details_results);
$question_details = array();
foreach ($details_results as $detail) {
if ($detail->is_correct) {
$correct_answers++;
}
$question_details[] = array(
'question' => $detail->question_title,
'answer' => $detail->answer,
'is_correct' => (bool) $detail->is_correct,
);
}
$percentage = $total_questions > 0 ? round(($correct_answers / $total_questions) * 100, 2) : 0;
$formatted_attempts[] = array(
'attempt_id' => (int) $attempt->attempt_id,
'name' => $attempt->student_name,
'class' => $attempt->class_name,
'quiz_title' => $attempt->quiz_title,
'score' => $attempt->score,
'date' => $attempt->datetime,
'percentage' => $percentage,
'correct_answers' => $correct_answers,
'total_questions' => $total_questions,
'details' => $question_details,
);
}
send_json_response(array(
'success' => true,
'quiz_attempts' => $formatted_attempts,
'total_count' => count($formatted_attempts),
), 200);
?>