Skip to content

Commit 3c26d22

Browse files
committed
Get user’s votes
1 parent bff8dff commit 3c26d22

3 files changed

Lines changed: 33 additions & 2 deletions

File tree

controller/base.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ protected function assign_template_block_vars($block, $rows)
123123
'READ' => $row['read'],
124124
'VOTES_UP' => $row['idea_votes_up'],
125125
'VOTES_DOWN' => $row['idea_votes_down'],
126+
'USER_VOTED' => $row['u_voted'],
126127
'POINTS' => $row['idea_votes_up'] - $row['idea_votes_down'],
127128
'STATUS' => $row['idea_status'], // for status icons (not currently implemented)
128129
'LOCKED' => $row['topic_status'] == ITEM_LOCKED,

factory/ideas.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,15 @@ public function get_ideas($number = 10, $sort = 'date', $sort_direction = 'DESC'
214214
return $row['topic_id'];
215215
}, $rows);
216216

217+
$idea_ids = array_column($rows, 'idea_id');
218+
217219
$topic_tracking_info = get_complete_topic_tracking((int) $this->config['ideas_forum_id'], $topic_ids);
220+
$user_voting_info = $this->get_users_votes($this->user->id, $idea_ids);
218221

219222
foreach ($rows as &$row)
220223
{
221224
$row['read'] = !(isset($topic_tracking_info[$row['topic_id']]) && $row['topic_last_post_time'] > $topic_tracking_info[$row['topic_id']]);
225+
$row['u_voted'] = isset($user_voting_info[$row['idea_id']]) ? (string) $user_voting_info[$row['idea_id']] : '';
222226
}
223227
}
224228

@@ -827,4 +831,30 @@ protected function profile_url()
827831

828832
return $this->profile_url;
829833
}
834+
835+
/**
836+
* Get the ideas a user voted on from a group of ideas
837+
*
838+
* @param int $user_id The user's id
839+
* @param array $ids An array of idea identifiers
840+
* @return array An array of ideas the user voted on with their vote result, or empty otherwise.
841+
* example: [idea_id => vote_result]
842+
* [1 => 1, 2 => 0] Voted up idea 1, voted down idea 2
843+
*/
844+
protected function get_users_votes($user_id, $ids = [])
845+
{
846+
$results = [];
847+
$sql = 'SELECT idea_id, vote_value
848+
FROM ' . $this->table_votes . '
849+
WHERE user_id = ' . (int) $user_id . '
850+
AND ' . $this->db->sql_in_set('idea_id', $ids, false, true);
851+
$result = $this->db->sql_query($sql);
852+
while ($row = $this->db->sql_fetchrow($result))
853+
{
854+
$results[$row['idea_id']] = $row['vote_value'];
855+
}
856+
$this->db->sql_freeresult($result);
857+
858+
return $results;
859+
}
830860
}

styles/prosilver/template/index_list.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
</dt>
1818

1919
<dd class="topics">
20-
<div class="minivoteup"><span>{{ idea.VOTES_UP }}</span></div>
21-
<div class="minivotedown"><span>{{ idea.VOTES_DOWN }}</span></div>
20+
<div class="minivoteup{% if idea.USER_VOTED == '1' %} voted{% endif %}"><span>{{ idea.VOTES_UP }}</span></div>
21+
<div class="minivotedown{% if idea.USER_VOTED == '0' %} voted{% endif %}"><span>{{ idea.VOTES_DOWN }}</span></div>
2222
</dd>
2323
</dl>
2424
</li>

0 commit comments

Comments
 (0)