V1rus Private
User / IP
:
216.73.217.108
Host / Server
:
190.92.174.125 / aerosofthealthcare.com
System
:
Linux s3739.bom1.stableserver.net 4.18.0-513.24.1.lve.2.el8.x86_64 #1 SMP Fri May 24 12:42:50 UTC 2024 x86_64
Cmd
|
Upload
|
Mass Deface
|
Create
|
Sym
:
/
home
/
aerosoft
/
public_html
/
exam
/
Viewing: import_questions.php
<?php include 'config.php'; if (!isset($_SESSION['admin_id'])) { header("Location: login.php"); exit(); } if ($_SERVER['REQUEST_METHOD'] == 'POST') { $response = ['success' => false, 'message' => '']; try { if (!isset($_FILES['excel_file']) || $_FILES['excel_file']['error'] != UPLOAD_ERR_OK) { throw new Exception('Please select a valid file.'); } $category_id = $_POST['category_id']; $default_marks = $_POST['default_marks'] ?? 1; // Validate category $stmt = $pdo->prepare("SELECT id FROM categories WHERE id = ?"); $stmt->execute([$category_id]); if (!$stmt->fetch()) { throw new Exception('Invalid category selected.'); } $file = $_FILES['excel_file']['tmp_name']; $file_type = $_FILES['excel_file']['type']; // Handle CSV files if (strpos($file_type, 'csv') !== false || pathinfo($_FILES['excel_file']['name'], PATHINFO_EXTENSION) === 'csv') { $handle = fopen($file, 'r'); $headers = fgetcsv($handle); // Validate headers $required_headers = ['question_text', 'option_a', 'option_b', 'option_c', 'option_d', 'correct_option']; foreach ($required_headers as $header) { if (!in_array($header, $headers)) { throw new Exception("Missing required column: $header"); } } $imported = 0; $skipped = 0; while (($data = fgetcsv($handle)) !== FALSE) { if (count($data) < 6) continue; // Skip incomplete rows $question_text = trim($data[array_search('question_text', $headers)]); $option_a = trim($data[array_search('option_a', $headers)]); $option_b = trim($data[array_search('option_b', $headers)]); $option_c = trim($data[array_search('option_c', $headers)]); $option_d = trim($data[array_search('option_d', $headers)]); $correct_option = strtolower(trim($data[array_search('correct_option', $headers)])); $marks = isset($data[array_search('marks', $headers)]) ? intval(trim($data[array_search('marks', $headers)])) : $default_marks; // Validate data if (empty($question_text) || empty($option_a) || empty($option_b) || empty($correct_option)) { $skipped++; continue; } if (!in_array($correct_option, ['a', 'b', 'c', 'd'])) { $skipped++; continue; } // Prepare options array $options = [ 'a' => $option_a, 'b' => $option_b, 'c' => $option_c, 'd' => $option_d ]; // Insert question $stmt = $pdo->prepare("INSERT INTO questions (category_id, question_text, options, correct_option, marks) VALUES (?, ?, ?, ?, ?)"); $stmt->execute([ $category_id, $question_text, json_encode($options), $correct_option, $marks ]); $imported++; } fclose($handle); $response['success'] = true; $response['message'] = "Successfully imported $imported questions. $skipped rows skipped."; } else { throw new Exception('Please upload a CSV file. Excel file support coming soon.'); } } catch (Exception $e) { $response['message'] = $e->getMessage(); } header('Content-Type: application/json'); echo json_encode($response); exit(); } ?>
Coded With 💗 by
HanzOFC