-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathknowledge_document.php
More file actions
184 lines (152 loc) · 6.26 KB
/
Copy pathknowledge_document.php
File metadata and controls
184 lines (152 loc) · 6.26 KB
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
<?php
$pageTitle = "Knowledge Document";
require_once __DIR__ . '/includes/db.php';
require_once __DIR__ . '/includes/header.php';
// h() already exists in your forum helpers; don't redeclare it.
// ---- DB guard ----
if (!isset($conn) || !($conn instanceof mysqli)) {
echo "<div class='container py-4'><div class='alert alert-danger mb-0'>DB connection not available.</div></div>";
require_once __DIR__ . '/includes/footer.php';
exit;
}
$docSlug = trim($_GET['doc'] ?? '');
if ($docSlug === '') {
http_response_code(404);
echo "<div class='container py-4'><div class='alert alert-danger mb-0'>Document not found.</div></div>";
require_once __DIR__ . '/includes/footer.php';
exit;
}
$userRating = 0;
// Fetch document + category (use unique var name to avoid collisions)
$kbDoc = null;
$stmt = $conn->prepare("
SELECT
d.id, d.title, d.slug, d.content, d.created_at, d.updated_at,
c.name AS category_name, c.slug AS category_slug, c.accent_color,
COALESCE(ROUND(AVG(r.rating), 1), 0) AS avg_rating,
COUNT(r.rating) AS rating_count
FROM knowledge_documents d
JOIN knowledge_categories c ON c.id = d.category_id
LEFT JOIN knowledge_ratings r ON r.document_id = d.id
WHERE d.slug = ?
AND d.is_published = 1
AND c.is_active = 1
GROUP BY d.id
LIMIT 1
");
if (!$stmt) {
echo "<div class='container py-4'><div class='alert alert-danger mb-0'>Query prepare failed: " . h($conn->error) . "</div></div>";
require_once __DIR__ . '/includes/footer.php';
exit;
}
$stmt->bind_param("s", $docSlug);
$stmt->execute();
$res = $stmt->get_result();
$kbDoc = $res ? $res->fetch_assoc() : null;
$stmt->close();
if (!$kbDoc) {
http_response_code(404);
echo "<div class='container py-4'><div class='alert alert-danger mb-0'>Document not found.</div></div>";
require_once __DIR__ . '/includes/footer.php';
exit;
}
$userFlagged = false;
if (!empty($currentUser)) {
$uid = (int) $currentUser['id'];
$stmt = $conn->prepare("
SELECT 1
FROM knowledge_flags
WHERE document_id = ? AND user_id = ?
LIMIT 1
");
$stmt->bind_param("ii", $kbDoc['id'], $uid);
$stmt->execute();
$userFlagged = (bool) $stmt->get_result()->fetch_row();
$stmt->close();
}
// Check if user can edit (admin or expert)
$canEdit = false;
if (!empty($currentUser)) {
if ($currentUser['role'] === 'admin' || $currentUser['forum_designation'] === 'Expert') {
$canEdit = true;
}
}
$pageTitle = $kbDoc['title'] . " — Knowledge Base";
?>
<div class="page-content">
<div class="container">
<div class="py-4">
<div class="d-flex align-items-center justify-content-between mb-3">
<a class="btn btn-sm btn-outline-secondary"
href="knowledge_category.php?cat=<?= h($kbDoc['category_slug']) ?>">
← <?= h($kbDoc['category_name']) ?>
</a>
<div class="d-flex gap-2">
<?php if ($canEdit): ?>
<a class="btn btn-sm btn-outline-primary"
href="admin_knowledge_document_edit.php?id=<?= (int) $kbDoc['id'] ?>">
Edit
</a>
<?php endif; ?>
<a class="btn btn-sm btn-outline-secondary" href="knowledge.php">All categories</a>
</div>
</div>
<div class="card shadow-sm" style="border-left:4px solid <?= h($kbDoc['accent_color'] ?? '#adb5bd') ?>;">
<div class="card-body">
<h1 class="h4 mb-2"><?= h($kbDoc['title']) ?></h1>
<div class="small text-muted mb-3">
<?= h(substr((string) $kbDoc['created_at'], 0, 10)) ?>
<?php if (!empty($kbDoc['updated_at'])): ?>
· Updated <?= h(substr((string) $kbDoc['updated_at'], 0, 10)) ?>
<?php endif; ?>
</div>
<!-- CKEditor HTML stored in DB; render as HTML -->
<div class="kb-content">
<?= $kbDoc['content'] ?>
<hr class="mt-5 mb-3">
<p>Is this document high quality? Or does it need improvement? High ranking articles display in the footer, admin notified if improvements required.</p>
<?php if (!empty($currentUser)): ?>
<form method="post" action="/rate_document.php" class="d-inline">
<input type="hidden" name="document_id" value="<?= (int) $kbDoc['id'] ?>">
<input type="hidden" name="csrf_token" value="<?= h($_SESSION['csrf_token']) ?>">
<div class="rating-stars">
<?php for ($i = 1; $i <= 5; $i++): ?>
<button
type="submit"
name="rating"
value="<?= $i ?>"
class="btn btn-link p-0 <?= $userRating === $i ? 'fw-bold' : '' ?>"
title="Rate <?= $i ?> / 5"
>
<?= $i <= $userRating ? '★' : '☆' ?>
</button>
<?php endfor; ?>
</div>
</form>
<?php else: ?>
<div class="small text-muted">
Log in to rate this document
</div>
<?php endif; ?>
<div class="border-top pt-3 mt-3 d-flex justify-content-between align-items-center">
<div class="small text-muted">
Rating: <?= h($kbDoc['avg_rating']) ?> / 5 (<?= (int) $kbDoc['rating_count'] ?>)
</div>
<?php if (!empty($currentUser)): ?>
<form method="post" action="/flag_document.php" class="d-inline">
<input type="hidden" name="document_id" value="<?= (int) $kbDoc['id'] ?>">
<input type="hidden" name="csrf_token" value="<?= h($_SESSION['csrf_token']) ?>">
<button type="submit"
class="btn btn-sm <?= $userFlagged ? 'btn-danger' : 'btn-outline-danger' ?>"
<?= $userFlagged ? 'disabled' : '' ?>>
<?= $userFlagged ? 'Flagged for improvement' : 'Needs improvement' ?>
</button>
</form>
<?php endif; ?>
</div>
</div>
</div>
</div>
</div>
</div>
<?php require_once __DIR__ . '/includes/footer.php'; ?>