Spaces:
Running
Running
| ```php | |
| session_start(); | |
| include 'db.php'; | |
| if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'admin') { | |
| header("Location: login.php"); | |
| exit(); | |
| } | |
| // Handle CRUD operations | |
| if ($_SERVER['REQUEST_METHOD'] === 'POST') { | |
| if (isset($_POST['add_qa'])) { | |
| $category = mysqli_real_escape_string($conn, $_POST['category']); | |
| $question = mysqli_real_escape_string($conn, $_POST['question']); | |
| $answer = mysqli_real_escape_string($conn, $_POST['answer']); | |
| $sql = "INSERT INTO chatbot_responses (category, user_query, bot_response) | |
| VALUES ('$category', '$question', '$answer')"; | |
| mysqli_query($conn, $sql); | |
| } | |
| elseif (isset($_POST['update_qa'])) { | |
| $id = (int)$_POST['id']; | |
| $category = mysqli_real_escape_string($conn, $_POST['category']); | |
| $question = mysqli_real_escape_string($conn, $_POST['question']); | |
| $answer = mysqli_real_escape_string($conn, $_POST['answer']); | |
| $sql = "UPDATE chatbot_responses | |
| SET category='$category', user_query='$question', bot_response='$answer' | |
| WHERE id=$id"; | |
| mysqli_query($conn, $sql); | |
| } | |
| elseif (isset($_POST['delete_qa'])) { | |
| $id = (int)$_POST['id']; | |
| $sql = "DELETE FROM chatbot_responses WHERE id=$id"; | |
| mysqli_query($conn, $sql); | |
| } | |
| } | |
| // Get all Q&A pairs | |
| $qa_pairs = []; | |
| $sql = "SELECT * FROM chatbot_responses ORDER BY category, user_query"; | |
| $result = mysqli_query($conn, $sql); | |
| while ($row = mysqli_fetch_assoc($result)) { | |
| $qa_pairs[] = $row; | |
| } | |
| // Get all users and their chat history | |
| $users = []; | |
| $sql = "SELECT u.id, u.name, u.email, COUNT(DISTINCT cl.session_id) as session_count | |
| FROM users u | |
| LEFT JOIN chat_logs cl ON u.id = cl.user_id | |
| GROUP BY u.id"; | |
| $result = mysqli_query($conn, $sql); | |
| while ($row = mysqli_fetch_assoc($result)) { | |
| $users[] = $row; | |
| } | |
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <title>Admin Dashboard - AdminBot BrainHub</title> | |
| <link rel="stylesheet" href="style.css"> | |
| <script src="https://cdn.tailwindcss.com"></script> | |
| <script src="https://cdn.jsdelivr.net/npm/feather-icons/dist/feather.min.js"></script> | |
| <script src="components/navbar.js"></script> | |
| <script src="components/sidebar.js"></script> | |
| </head> | |
| <body class="bg-gray-100"> | |
| <custom-navbar></custom-navbar> | |
| <div class="flex"> | |
| <custom-sidebar></custom-sidebar> | |
| <main class="flex-1 p-8"> | |
| <div class="mb-8"> | |
| <h1 class="text-3xl font-bold text-gray-800">Admin Dashboard</h1> | |
| <p class="text-gray-600">Manage chatbot knowledge and user interactions</p> | |
| </div> | |
| <div class="grid grid-cols-1 lg:grid-cols-2 gap-8"> | |
| <!-- Q&A Management Section --> | |
| <div class="bg-white rounded-lg shadow p-6"> | |
| <div class="flex justify-between items-center mb-6"> | |
| <h2 class="text-xl font-semibold">Knowledge Base</h2> | |
| <button onclick="toggleQaForm()" class="bg-blue-500 text-white px-4 py-2 rounded-lg hover:bg-blue-600"> | |
| Add New Q&A | |
| </button> | |
| </div> | |
| <!-- Add/Edit Form (Hidden by default) --> | |
| <div id="qaForm" class="hidden mb-6 p-4 bg-gray-50 rounded-lg"> | |
| <form method="POST"> | |
| <input type="hidden" name="id" id="editId"> | |
| <div class="mb-4"> | |
| <label class="block text-gray-700 mb-2">Category</label> | |
| <input type="text" name="category" id="category" class="w-full p-2 border rounded" required> | |
| </div> | |
| <div class="mb-4"> | |
| <label class="block text-gray-700 mb-2">Question</label> | |
| <input type="text" name="question" id="question" class="w-full p-2 border rounded" required> | |
| </div> | |
| <div class="mb-4"> | |
| <label class="block text-gray-700 mb-2">Answer</label> | |
| <textarea name="answer" id="answer" rows="3" class="w-full p-2 border rounded" required></textarea> | |
| </div> | |
| <div class="flex gap-2"> | |
| <button type="submit" name="add_qa" id="addBtn" class="bg-green-500 text-white px-4 py-2 rounded-lg hover:bg-green-600"> | |
| Add Q&A | |
| </button> | |
| <button type="submit" name="update_qa" id="updateBtn" class="hidden bg-blue-500 text-white px-4 py-2 rounded-lg hover:bg-blue-600"> | |
| Update Q&A | |
| </button> | |
| <button type="button" onclick="toggleQaForm()" class="bg-gray-500 text-white px-4 py-2 rounded-lg hover:bg-gray-600"> | |
| Cancel | |
| </button> | |
| </div> | |
| </form> | |
| </div> | |
| <!-- Q&A List --> | |
| <div class="overflow-x-auto"> | |
| <table class="min-w-full bg-white"> | |
| <thead> | |
| <tr> | |
| <th class="py-2 px-4 border-b">Category</th> | |
| <th class="py-2 px-4 border-b">Question</th> | |
| <th class="py-2 px-4 border-b">Answer</th> | |
| <th class="py-2 px-4 border-b">Actions</th> | |
| </tr> | |
| </thead> | |
| <tbody> | |
| <?php foreach ($qa_pairs as $qa): ?> | |
| <tr> | |
| <td class="py-2 px-4 border-b"><?= htmlspecialchars($qa['category']) ?></td> | |
| <td class="py-2 px-4 border-b"><?= htmlspecialchars($qa['user_query']) ?></td> | |
| <td class="py-2 px-4 border-b"><?= htmlspecialchars($qa['bot_response']) ?></td> | |
| <td class="py-2 px-4 border-b"> | |
| <button onclick="editQa(<?= $qa['id'] ?>, '<?= addslashes($qa['category']) ?>', '<?= addslashes($qa['user_query']) ?>', `<?= addslashes($qa['bot_response']) ?>`)" | |
| class="text-blue-500 hover:text-blue-700 mr-2"> | |
| <i data-feather="edit"></i> | |
| </button> | |
| <form method="POST" class="inline"> | |
| <input type="hidden" name="id" value="<?= $qa['id'] ?>"> | |
| <button type="submit" name="delete_qa" class="text-red-500 hover:text-red-700"> | |
| <i data-feather="trash-2"></i> | |
| </button> | |
| </form> | |
| </td> | |
| </tr> | |
| <?php endforeach; ?> | |
| </tbody> | |
| </table> | |
| </div> | |
| </div> | |
| <!-- User Management Section --> | |
| <div class="bg-white rounded-lg shadow p-6"> | |
| <h2 class="text-xl font-semibold mb-6">User Interactions</h2> | |
| <div class="overflow-x-auto"> | |
| <table class="min-w-full bg-white"> | |
| <thead> | |
| <tr> | |
| <th class="py-2 px-4 border-b">User</th> | |
| <th class="py-2 px-4 border-b">Email</th> | |
| <th class="py-2 px-4 border-b">Chat Sessions</th> | |
| <th class="py-2 px-4 border-b">Actions</th> | |
| </tr> | |
| </thead> | |
| <tbody> | |
| <?php foreach ($users as $user): ?> | |
| <tr> | |
| <td class="py-2 px-4 border-b"><?= htmlspecialchars($user['name']) ?></td> | |
| <td class="py-2 px-4 border-b"><?= htmlspecialchars($user['email']) ?></td> | |
| <td class="py-2 px-4 border-b"><?= $user['session_count'] ?></td> | |
| <td class="py-2 px-4 border-b"> | |
| <a href="user_chats.php?user_id=<?= $user['id'] ?>" class="text-blue-500 hover:text-blue-700"> | |
| <i data-feather="eye"></i> View Chats | |
| </a> | |
| </td> | |
| </tr> | |
| <?php endforeach; ?> | |
| </tbody> | |
| </table> | |
| </div> | |
| </div> | |
| </div> | |
| </main> | |
| </div> | |
| <script> | |
| feather.replace(); | |
| function toggleQaForm() { | |
| const form = document.getElementById('qaForm'); | |
| form.classList.toggle('hidden'); | |
| // Reset form when showing | |
| if (!form.classList.contains('hidden')) { | |
| document.getElementById('editId').value = ''; | |
| document.getElementById('category').value = ''; | |
| document.getElementById('question').value = ''; | |
| document.getElementById('answer').value = ''; | |
| document.getElementById('addBtn').classList.remove('hidden'); | |
| document.getElementById('updateBtn').classList.add('hidden'); | |
| } | |
| } | |
| function editQa(id, category, question, answer) { | |
| toggleQaForm(); | |
| document.getElementById('editId').value = id; | |
| document.getElementById('category').value = category; | |
| document.getElementById('question').value = question; | |
| document.getElementById('answer').value = answer; | |
| document.getElementById('addBtn').classList.add('hidden'); | |
| document.getElementById('updateBtn').classList.remove('hidden'); | |
| } | |
| </script> | |
| <script src="script.js"></script> | |
| </body> | |
| </html> | |
| ``` |