I have a Drupal site that currently displays comments in threaded, reverse chronological order. I am trying to change it to work like Reddit/Hacker News, where the threads and comments within each thread are instead ordered by their current score, based on a voting system I've added.
I've found the query used to render the comments right now. As I understand it, I need to find a way to also select the score data for each comment in this query, and then I need to sort the comments in descending order of score while still keeping them in their threads.
Here's the query:
$query = 'SELECT c.cid as cid, c.pid, c.nid, c.subject, c.comment, c.format, c.timestamp, c.name, c.mail, c.homepage, u.uid, u.name AS registered_name, u.picture, u.data, c.score, c.users, c.thread, c.status FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.nid = %d ORDER BY c.thread DESC';
The table I need to join in is called radioactivity and looks like this
@radioactivity
class | id | energy
comment | 1 | 5
comment | 2 | 8
comment | 3 | 27
comment | 4 | 13
The id column in this table syncs up with the cid column in the comments table.
I've tried this to bring in the scoring data:
$query = "SELECT c.cid as cid, c.pid, c.nid, c.subject, c.comment, c.format, c.timestamp, c.name, c.mail, c.homepage, u.uid, u.name AS registered_name, u.picture, u.data, c.score, c.users, c.thread, c.status, r.energy, r.id, r.class FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid INNER JOIN {radioactivity} r ON c.cid = r.id WHERE c.nid = %d AND r.class = 'comment' ORDER BY c.thread DESC";
This query only returns comments that have entries in the radioactivity table. comments that haven't received any votes don't appear in the table at all, so they're not being grabbed. How do I change this so it gets all the comments?
And then how do I change the end of the query so it orders the comments by score while still keeping them within their threads?
Right now comments are sorted based on c.thread. If a thread was structures like this:
comment
-comment
--comment
--comment
-comment
-comment
The c.thread values would be
1
1.1
1.1.1
1.1.2
1.2
1.3
I want to keep this structure, but have the comments within each level of a thread ordered based on their current score, not their c.thread value.
I know this is a lot and it's very detailed, but I thought this the best venue.