Spaces:
Running
Running
File size: 10,938 Bytes
659d490 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | ```php
<?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>
``` |