Skip to content
This repository has been archived by the owner on May 14, 2023. It is now read-only.

Commit

Permalink
Merge pull request #47 from Borumer/comment-section
Browse files Browse the repository at this point in the history
Ability to comment on questions and answers
  • Loading branch information
varunsingh87 authored Jan 23, 2020
2 parents 72eaeb0 + ab6f492 commit ffb13aa
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 60 deletions.
9 changes: 9 additions & 0 deletions css/profile.css
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,12 @@ div.page-with-form-body > form > p.form-inputs {
background-color: #eeeeee;
}

#question-page-table td.comments {
border-style: solid none solid none;
border-width: 1px;
padding: 2em 0;
}

#question-page-table div.new-comment {
text-align:left;
}
8 changes: 4 additions & 4 deletions css/style.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.sidebar-outer {
position: relative;
position: relative;
}

.none {
Expand Down Expand Up @@ -60,7 +60,7 @@
}

.sidebar-outer + div.col-sm-8 {

}

.fixed-sidebar a {
Expand Down Expand Up @@ -121,7 +121,7 @@
width: 35px;
height: 5px;
background-color: black;
margin: 6px 0;
margin: 6px 0;
}

.t--dark div.navicon div {
Expand Down Expand Up @@ -160,6 +160,7 @@ a.question-tags {

.question-poster {
padding: 5px 6px 7px 7px;
float: right;
}

.question-poster, .question-tags {
Expand Down Expand Up @@ -216,4 +217,3 @@ form input, form textarea {
form input:focus {
outline-color: blue;
}

7 changes: 5 additions & 2 deletions css/toggles.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
table#question-page-table {
padding: 0px;
width: 790px;
border-collapse: separate;
width: 100%;
border-spacing: 10px 50px;
}

Expand All @@ -23,6 +23,10 @@ table#question-page-table tr td div {
text-align: center;
}

#your-answer-ta {
width: 100%;
}

#bio {
margin-bottom: 1em;
width: 80%;
Expand Down Expand Up @@ -137,4 +141,3 @@ td.output {
margin-bottom: 40%;
}
}

Binary file modified images/unavailable.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 27 additions & 12 deletions pages/ajax/answer.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php
<?php

file_exists('../../../mysqli_connect.inc.php') ? require_once('../../../mysqli_connect.inc.php') : require_once('../../../../mysqli_connect.inc.php');

session_start();
require('../includes/helpers.php');

// Validate the form elements
// Define the POST data
$ans = $_POST['answer'];
$ques_id = $_POST['ques_id'];
$counter = $_POST['counter'];
// Check if its okay for the user to answer the question
$q = "SELECT id FROM messages WHERE parent_id = $ques_id AND body = '$ans'";
$r = @mysqli_query($dbc, $q);
Expand All @@ -17,6 +18,21 @@
@mysqli_query($dbc, $q);
}


function getNoAccountButton($way) {
return "\t<button type = 'button' onclick = \"window.location.href = '/Login'\">$way</button>\n";
}

function votedOnQuestion($msg_id, $vote) {
global $dbc;
if (isset($_SESSION['id'])) {
$query = "SELECT vote FROM `user-message-votes` WHERE user_id = {$_SESSION['id']} AND message_id = $msg_id ORDER BY id DESC LIMIT 1"; // Select latest vote for the user for the question
$result = mysqli_query($dbc, $query);
return @mysqli_fetch_array($result, MYSQLI_NUM)[0] == $vote;
}

}

// Return the answer to the question that has just been posted
function getUpArrow() {
global $fillColor;
Expand All @@ -30,9 +46,11 @@ function getDownArrow() {

$ansquery = '
SELECT messages.id AS msg_id, users.id AS usr_id, messages.body AS msg_body, users.profile_picture AS profile, users.first_name AS fn
FROM messages
JOIN users
ON users.id = messages.user_id';
FROM messages
JOIN users
ON users.id = messages.user_id
ORDER BY date_entered DESC
LIMIT 1';
$ansresult = mysqli_query($dbc, $ansquery);
$ansrow = mysqli_fetch_array($ansresult, MYSQLI_ASSOC);
$ans_id = $ansrow['msg_id'];
Expand All @@ -45,15 +63,13 @@ function getDownArrow() {
$noAccountVoteDownBtn = getNoAccountButton($downarrow);

$voteupbtn = isset($_SESSION['id']) ? "\t<button type = 'button' onclick = \"loadXMLDoc('up', {$_SESSION['id']}, {$ansrow['msg_id']}, 'ans-$counter-vote-count')\">$uparrow</button>\n" : $noAccountVoteUpBtn;
$votedownbtn = isset($_SESSION['id']) ? "\t\t<button type = 'button' onclick = \"loadXMLDoc('down', {$_SESSION['id']}, {$ansrow['msg_id']}, 'ans-$counter-vote-count')\">$downarrow</button>\n" : $noAccountVoteDownBtn;
$votedownbtn = isset($_SESSION['id']) ? "\t\t<button type = 'button' onclick = \"loadXMLDoc('down', {$_SESSION['id']}, {$ansrow['msg_id']}, 'ans-$counter-vote-count')\">$downarrow</button>\n" : $noAccountVoteDownBtn;

echo "<tr>";
echo "<td>";
echo $voteupbtn;

$voteCount = $ansrow['votes'] == null ? 0 : $ansrow['votes'];

echo "\t\t<br><div class = 'vote-counter' id = 'ans-$counter-vote-count'>$voteCount</div>";
echo "\t\t<br><div class = 'vote-counter' id = 'ans-$counter-vote-count'>0</div>";
echo $votedownbtn;
echo "</td>";
// Generate query for answers' information
Expand Down Expand Up @@ -81,11 +97,10 @@ function getDownArrow() {
<a href = '/Users/{$ansrow['usr_id']}'>
<img height = '30' src = \"../pages/show_image.php?image={row2['profile']}\">
</a>
</div>
</div>
</td>
</tr>";
/*
*/
?>

6 changes: 3 additions & 3 deletions pages/includes/footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
</ul>
<ul class = "social-media">
<li><a href = "https://twitter.com/BorumInc" class = "twitter social-media-links">
<img
<img
src = "
<?php echo isset($_COOKIE['dark']) && $_COOKIE['dark'] == '1' ? '../images/Twitter_Social_Icon_Circle_Black.svg' : '../images/Twitter_Social_Icon_Circle_White.svg'; ?>"
<?php echo isset($_COOKIE['dark']) && $_COOKIE['dark'] == '1' ? '/images/Twitter_Social_Icon_Circle_Black.svg' : '/images/Twitter_Social_Icon_Circle_White.svg'; ?>"
alt = "Twitter logo in white">
</a></li>
<li><a href = "https://www.youtube.com/channel/UCDDyVIflz8dUhHIb2YmA9hQ" class = "youtube social-media-links"><img src = "/images/youtube_social_circle_dark.png" alt = "Youtube Logo in black"></a></li>
Expand All @@ -30,4 +30,4 @@
<script src = "/scripts/notifications.js"></script>
<script src = "/scripts/search.js"></script>
</body>
</html>
</html>
78 changes: 47 additions & 31 deletions pages/question.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php
file_exists('../../mysqli_connect.inc.php') ? require_once('../../mysqli_connect.inc.php') : require_once('../../../mysqli_connect.inc.php');

// Generate query for question's information
$query = '
SELECT messages.parent_id, users.id AS usr_id, messages.id AS msg_id, messages.subject AS subject, messages.body AS ques_body, users.profile_picture AS ques_profile_pic, users.first_name AS ques_asker, messages.forum_id, topics.name AS topic
FROM messages
LEFT JOIN users
ON messages.user_id = users.id
FROM messages
LEFT JOIN users
ON messages.user_id = users.id
JOIN topics
ON messages.forum_id = topics.id
WHERE messages.id = ' . $_GET['id'];
Expand All @@ -17,12 +17,12 @@
$resultCorr = mysqli_query($dbc, $queryCorr);

$query2 = '
SELECT messages.id AS msg_id, users.id AS usr_id, messages.body AS msg_body, users.profile_picture AS profile, users.first_name AS fn, SUM(`user-message-votes`.vote) AS votes
FROM messages
JOIN users
ON users.id = messages.user_id
LEFT OUTER JOIN `user-message-votes`
ON messages.id = `user-message-votes`.`message_id`
SELECT messages.id AS msg_id, users.id AS usr_id, messages.body AS msg_body, users.profile_picture AS profile, users.first_name AS fn, SUM(`user-message-votes`.vote) AS votes
FROM messages
JOIN users
ON users.id = messages.user_id
LEFT OUTER JOIN `user-message-votes`
ON messages.id = `user-message-votes`.`message_id`
WHERE messages.parent_id = ' . $_GET['id'] . '
GROUP BY messages.id ORDER BY SUM(`user-message-votes`.vote) DESC';
$result2 = mysqli_query($dbc, $query2);
Expand All @@ -32,22 +32,22 @@
$page_title = $row['subject'];
include('includes/header.html');
include('includes/login_functions.inc.php');
?>
?>
<div class = "col-sm-10">
<h1><?php echo $row['subject']; ?></h1>

<table id = "question-page-table">
<tbody>
<tr>
<tr>
<td class = "vote-container">
<!-- PHP Functions -->
<?php
<?php
function votedOnQuestion($msg_id, $vote) {
global $dbc;
if (isset($_SESSION['id'])) {
$query = "SELECT vote FROM `user-message-votes` WHERE user_id = {$_SESSION['id']} AND message_id = $msg_id ORDER BY id DESC LIMIT 1"; // Select latest vote for the user for the question
$result = mysqli_query($dbc, $query);
return @mysqli_fetch_array($result, MYSQLI_NUM)[0] == $vote;
return @mysqli_fetch_array($result, MYSQLI_NUM)[0] == $vote;
}

}
Expand All @@ -68,7 +68,7 @@ function getNoAccountButton($way) {
?>
<?php

$ques_id = $row['msg_id'];
$ques_id = $row['msg_id'];

$fillColor = votedOnQuestion($ques_id, 1) ? 'lightgreen' : 'rgb(221, 221, 221)';
$uparrow = getUpArrow();
Expand All @@ -85,14 +85,21 @@ function getNoAccountButton($way) {
echo "\t\t<div class = 'vote-counter' id = 'ques-vote-count'>{$rowCorr[1]}</div>\n";
echo $votedownbtn;

?>
?>
</td>
<td>
<p id = "ques-body"><?php echo $row['ques_body'] ?></p>
<p id = "ques-body"><?php echo $row['ques_body'] ?></p>
</td>
</tr>
<tr>
<td>
<div class = "question-tags">
<a href = "../Topics/<?php echo $row['topic']; ?>"><?php echo $row['topic'] ?></a>
</div>
</td>
</tr>
<tr class = 'user-profile-container'>
<?php
<?php
if (ISADMIN || (LOGGEDIN && $_SESSION['id'] === $row['usr_id'])) {
$what_to_echo = $ques_id . '/Edit';

Expand All @@ -113,18 +120,28 @@ function getNoAccountButton($way) {
<a href = '<?php echo "/Users/{$row['usr_id']}"; ?>'>
<img height = '30' src = '../pages/show_image.php?image=<?php echo $row['ques_profile_pic']; ?>'>
</a>
</div>
</div>
</td>
</tr>
<?php
$q = "SELECT body, date_written, usr_id FROM comments WHERE msg_id = {$_GET['id']}";
$r = mysqli_query($dbc, $q);
if($r && mysqli_num_rows($r) > 0) {
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
echo "<tr><td></td><td class = 'comments'>{$row['body']}</td></tr>";
}
}
?>
<tr>
<td>
<div class = "question-tags">
<a href = "../Topics/<?php echo $row['topic']; ?>"><?php echo $row['topic'] ?></a>
<td>
<div class = "new-comment">
<input type = "text" size = "50">
<input type = "button" onclick = "addComment(<?php echo $_SESSION['id']; ?>, <?php echo $_GET['id']; ?>)" value = "Add Comment">
</div>
</td>
</tr>

<?php
<?php
$counter = 1;
while ($row2 = mysqli_fetch_array($result2, MYSQLI_ASSOC)) {
$ans_id = $row2['msg_id'];
Expand All @@ -137,14 +154,14 @@ function getNoAccountButton($way) {
$noAccountVoteDownBtn = getNoAccountButton($downarrow);

$voteupbtn = isset($_SESSION['id']) ? "\t<button type = 'button' onclick = \"loadXMLDoc('up', {$_SESSION['id']}, {$row2['msg_id']}, 'ans-$counter-vote-count')\">$uparrow</button>\n" : $noAccountVoteUpBtn;
$votedownbtn = isset($_SESSION['id']) ? "\t\t<button type = 'button' onclick = \"loadXMLDoc('down', {$_SESSION['id']}, {$row2['msg_id']}, 'ans-$counter-vote-count')\">$downarrow</button>\n" : $noAccountVoteDownBtn;
$votedownbtn = isset($_SESSION['id']) ? "\t\t<button type = 'button' onclick = \"loadXMLDoc('down', {$_SESSION['id']}, {$row2['msg_id']}, 'ans-$counter-vote-count')\">$downarrow</button>\n" : $noAccountVoteDownBtn;

echo "<tr>";
echo "<td>";
echo $voteupbtn;

$voteCount = $row2['votes'] == null ? 0 : $row2['votes'];

echo "\t\t<br><div class = 'vote-counter' id = 'ans-$counter-vote-count'>$voteCount</div>";
echo $votedownbtn;
echo "</td>";
Expand Down Expand Up @@ -173,7 +190,7 @@ function getNoAccountButton($way) {
<a href = '/Users/{$row2['usr_id']}'>
<img height = '30' src = \"../pages/show_image.php?image={$row2['profile']}\">
</a>
</div>
</div>
</td>
</tr>";
$counter++;
Expand All @@ -186,7 +203,7 @@ function getNoAccountButton($way) {
<p>
<textarea id = 'your-answer-ta' name = "answer" cols = '125' rows = '20'></textarea>
</p>
<input type = 'button' value = 'Post your Answer' onclick = 'answerQuestion(<?php echo $_GET['id']; ?>, document.getElementById("your-answer-ta").value)'>
<input type = 'button' value = 'Post your Answer' onclick = 'answerQuestion(<?php echo $_GET['id']; ?>, document.getElementById("your-answer-ta").value, <?php echo $counter; ?>)'>
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Handle all form submissions
Expand All @@ -206,7 +223,6 @@ function getNoAccountButton($way) {
}
}

mysqli_close($dbc);
mysqli_close($dbc);
include('includes/footer.html');
?>

Loading

0 comments on commit ffb13aa

Please sign in to comment.