Path : /home/vishqocm/pcib.in/student/
File Upload :
Current File : /home/vishqocm/pcib.in/student/create_ticket.php

<?php
$pageTitle = "Create Support Ticket";
include_once('includes/header.php');

// Check if user is logged in
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'student') {
    header("Location: ../login.php");
    exit();
}

// Get student ID
$student_id = $_SESSION['user_id'];

// Get student info
$student_query = "SELECT * FROM users WHERE id = ? AND role = 'student'";
$stmt = $conn->prepare($student_query);
$stmt->bind_param("i", $student_id);
$stmt->execute();
$student_result = $stmt->get_result();
$student = $student_result->fetch_assoc();

// Handle form submission
$success_message = "";
$error_message = "";

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['submit_ticket'])) {
    $subject = trim($_POST['subject'] ?? '');
    $category = trim($_POST['category'] ?? '');
    $message = trim($_POST['message'] ?? '');
    $priority = trim($_POST['priority'] ?? 'medium');
    $course_id = isset($_POST['course_id']) && is_numeric($_POST['course_id']) ? (int)$_POST['course_id'] : null;
    
    // Validation
    if (empty($subject) || empty($category) || empty($message)) {
        $error_message = "Please fill in all required fields.";
    } else {
        // Check if we need to create the support tickets table
        $tableExists = $conn->query("SHOW TABLES LIKE 'support_tickets'")->num_rows > 0;
        if (!$tableExists) {
            // Create support tickets table
            $conn->query("
                CREATE TABLE IF NOT EXISTS `support_tickets` (
                  `id` int(11) NOT NULL AUTO_INCREMENT,
                  `ticket_id` varchar(20) NOT NULL,
                  `student_id` int(11) NOT NULL,
                  `subject` varchar(255) NOT NULL,
                  `message` text NOT NULL,
                  `category` varchar(50) NOT NULL,
                  `priority` varchar(20) NOT NULL DEFAULT 'medium',
                  `status` enum('open','in_progress','resolved','closed') NOT NULL DEFAULT 'open',
                  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
                  `updated_at` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
                  `course_id` int(11) DEFAULT NULL,
                  PRIMARY KEY (`id`),
                  UNIQUE KEY `ticket_id` (`ticket_id`)
                ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
            ");
            
            // Create support ticket replies table
            $conn->query("
                CREATE TABLE IF NOT EXISTS `support_ticket_replies` (
                  `id` int(11) NOT NULL AUTO_INCREMENT,
                  `ticket_id` int(11) NOT NULL,
                  `user_type` enum('admin','student') NOT NULL,
                  `user_id` int(11) NOT NULL,
                  `message` text NOT NULL,
                  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
                  PRIMARY KEY (`id`),
                  KEY `ticket_id` (`ticket_id`)
                ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
            ");
        }
        
        // Generate a unique ticket number
        $year = date('Y');
        $month = date('m');
        $day = date('d');
        $random = rand(1000, 9999);
        $ticket_id = "TCK-$year$month$day-$random";
        
        // Insert the support ticket
        $stmt = $conn->prepare("
            INSERT INTO support_tickets 
            (ticket_id, student_id, subject, message, category, priority, status, course_id) 
            VALUES (?, ?, ?, ?, ?, ?, 'open', ?)
        ");
        $stmt->bind_param("sissssi", $ticket_id, $student_id, $subject, $message, $category, $priority, $course_id);
        
        if ($stmt->execute()) {
            $ticket_db_id = $conn->insert_id;
            
            // Add the initial message as a reply
            $stmt = $conn->prepare("
                INSERT INTO support_ticket_replies 
                (ticket_id, user_type, user_id, message) 
                VALUES (?, 'student', ?, ?)
            ");
            $stmt->bind_param("iis", $ticket_db_id, $student_id, $message);
            $stmt->execute();
            
            $success_message = "Your support ticket has been submitted successfully. Ticket #$ticket_id";
            
            // Clear form data
            $subject = $category = $message = $priority = "";
            $course_id = null;
        } else {
            $error_message = "Failed to submit your ticket. Please try again.";
        }
    }
}

// Get student's courses for dropdown
$courses = [];
$course_query = "
    SELECT c.* FROM enrollments e
    JOIN courses c ON e.course_id = c.id
    WHERE e.user_id = ? AND e.status = 'active'
    ORDER BY c.title ASC
";
$stmt = $conn->prepare($course_query);
$stmt->bind_param("i", $student_id);
$stmt->execute();
$course_result = $stmt->get_result();

while ($row = $course_result->fetch_assoc()) {
    $courses[] = $row;
}

// Define categories
$categories = [
    'account' => 'Account Access',
    'payment' => 'Payment Issues',
    'enrollment' => 'Course Enrollment',
    'certificate' => 'Certificate Issues',
    'exam' => 'Exam Related',
    'technical' => 'Technical Support',
    'general' => 'General Information',
    'other' => 'Other'
];

// Define priorities
$priorities = [
    'low' => 'Low Priority',
    'medium' => 'Medium Priority',
    'high' => 'High Priority'
];
?>

<div class="container py-4">
    <div class="row">
        <div class="col-md-8 mx-auto">
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h2 class="h3">Create New Support Ticket</h2>
                <a href="support.php" class="btn btn-outline-secondary">
                    <i class="fas fa-arrow-left me-1"></i> Back to Support
                </a>
            </div>
            
            <?php if (!empty($success_message)): ?>
            <div class="alert alert-success alert-dismissible fade show" role="alert">
                <?php echo $success_message; ?>
                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
            </div>
            <div class="text-center mb-4">
                <a href="support.php" class="btn btn-primary">View My Tickets</a>
            </div>
            <?php endif; ?>
            
            <?php if (!empty($error_message)): ?>
            <div class="alert alert-danger alert-dismissible fade show" role="alert">
                <?php echo $error_message; ?>
                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
            </div>
            <?php endif; ?>
            
            <?php if (empty($success_message)): ?>
            <div class="card">
                <div class="card-body">
                    <form method="post" action="">
                        <div class="mb-3">
                            <label for="subject" class="form-label">Subject <span class="text-danger">*</span></label>
                            <input type="text" class="form-control" id="subject" name="subject" value="<?php echo isset($subject) ? htmlspecialchars($subject) : ''; ?>" required>
                            <div class="form-text">Please provide a brief description of your issue</div>
                        </div>
                        
                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label for="category" class="form-label">Category <span class="text-danger">*</span></label>
                                <select class="form-select" id="category" name="category" required>
                                    <option value="">Select Category</option>
                                    <?php foreach ($categories as $key => $value): ?>
                                    <option value="<?php echo $key; ?>" <?php echo (isset($category) && $category === $key) ? 'selected' : ''; ?>>
                                        <?php echo $value; ?>
                                    </option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                            
                            <div class="col-md-6 mb-3">
                                <label for="priority" class="form-label">Priority <span class="text-danger">*</span></label>
                                <select class="form-select" id="priority" name="priority" required>
                                    <?php foreach ($priorities as $key => $value): ?>
                                    <option value="<?php echo $key; ?>" <?php echo (isset($priority) && $priority === $key) ? 'selected' : ''; ?>>
                                        <?php echo $value; ?>
                                    </option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                        </div>
                        
                        <?php if (count($courses) > 0): ?>
                        <div class="mb-3">
                            <label for="course_id" class="form-label">Related Course (Optional)</label>
                            <select class="form-select" id="course_id" name="course_id">
                                <option value="">Not Related to a Specific Course</option>
                                <?php foreach ($courses as $course): ?>
                                <option value="<?php echo $course['id']; ?>" <?php echo (isset($course_id) && $course_id == $course['id']) ? 'selected' : ''; ?>>
                                    <?php echo htmlspecialchars($course['title']); ?>
                                </option>
                                <?php endforeach; ?>
                            </select>
                            <div class="form-text">Select a course if your issue is related to a specific course</div>
                        </div>
                        <?php endif; ?>
                        
                        <div class="mb-4">
                            <label for="message" class="form-label">Message <span class="text-danger">*</span></label>
                            <textarea class="form-control" id="message" name="message" rows="6" required><?php echo isset($message) ? htmlspecialchars($message) : ''; ?></textarea>
                            <div class="form-text">Please describe your issue in detail. Include any error messages or steps to reproduce the problem.</div>
                        </div>
                        
                        <div class="text-end">
                            <button type="submit" name="submit_ticket" class="btn btn-primary">
                                <i class="fas fa-paper-plane me-1"></i> Submit Ticket
                            </button>
                        </div>
                    </form>
                </div>
            </div>
            
            <div class="card mt-4">
                <div class="card-header bg-light">
                    <h5 class="mb-0">Support Ticket Guidelines</h5>
                </div>
                <div class="card-body">
                    <ul class="mb-0">
                        <li class="mb-2">Be as specific as possible when describing your issue</li>
                        <li class="mb-2">Include any error messages you encountered</li>
                        <li class="mb-2">Mention steps you've already taken to resolve the issue</li>
                        <li class="mb-2">Choose the appropriate category to help route your ticket to the right team</li>
                        <li class="mb-2">Select priority level based on urgency:
                            <ul>
                                <li><strong>Low:</strong> General questions or minor issues</li>
                                <li><strong>Medium:</strong> Issues affecting your experience but with workarounds</li>
                                <li><strong>High:</strong> Critical issues preventing access to essential functions</li>
                            </ul>
                        </li>
                    </ul>
                </div>
            </div>
            <?php endif; ?>
        </div>
    </div>
</div>

<?php include_once('includes/footer.php'); ?>