Skip to content

Commit 4b0b116

Browse files
authored
Merge pull request #111 from VSEphpbb/voters
Show your vote on idea index
2 parents bff8dff + 5c0b00c commit 4b0b116

6 files changed

Lines changed: 55 additions & 19 deletions

File tree

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"type": "phpbb-extension",
44
"description": "phpBB Ideas centre for phpBB 3.2.",
55
"homepage": "https://www.phpbb.com",
6-
"version": "2.2.1",
6+
"version": "2.2.2-dev",
77
"license": "GPL-2.0-only",
88
"authors": [
99
{
@@ -34,13 +34,13 @@
3434
}
3535
],
3636
"require": {
37-
"php": ">=5.4",
37+
"php": ">=5.5",
3838
"composer/installers": "~1.0"
3939
},
4040
"extra": {
4141
"display-name": "phpBB Ideas",
4242
"soft-require": {
43-
"phpbb/phpbb": ">=3.2.1,<3.4"
43+
"phpbb/phpbb": ">=3.2.1"
4444
}
4545
}
4646
}

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,

ext.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ class ext extends \phpbb\extension\base
2525
* The current phpBB version should meet or exceed
2626
* the minimum version required by this extension:
2727
*
28-
* Requires phpBB 3.2.1 due to use of $event->update_subarray();
28+
* Requires phpBB 3.2.1 due to use of $event->update_subarray()
29+
* and PHP 5.5.0 due to use of array_column()
2930
*
3031
* @return bool
3132
* @access public
3233
*/
3334
public function is_enableable()
3435
{
35-
return phpbb_version_compare(PHPBB_VERSION, '3.2.1', '>=');
36+
return phpbb_version_compare(PHPBB_VERSION, '3.2.1', '>=')
37+
&& phpbb_version_compare(PHP_VERSION, '5.5.0', '>=');
3638
}
3739
}

factory/ideas.php

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public function get_ideas($number = 10, $sort = 'date', $sort_direction = 'DESC'
168168
{
169169
$sql = 'SELECT COUNT(i.idea_id) as num_ideas
170170
FROM ' . $this->table_ideas . ' i
171-
INNER JOIN ' . $this->table_topics . " t
171+
INNER JOIN ' . $this->table_topics . " t
172172
ON i.topic_id = t.topic_id
173173
WHERE $where";
174174
$result = $this->db->sql_query($sql);
@@ -183,7 +183,7 @@ public function get_ideas($number = 10, $sort = 'date', $sort_direction = 'DESC'
183183
{
184184
$sql = 'SELECT t.topic_last_post_time, t.topic_status, t.topic_visibility, i.*
185185
FROM ' . $this->table_ideas . ' i
186-
INNER JOIN ' . $this->table_topics . " t
186+
INNER JOIN ' . $this->table_topics . " t
187187
ON i.topic_id = t.topic_id
188188
WHERE $where
189189
ORDER BY " . $this->db->sql_escape($sortby);
@@ -196,12 +196,12 @@ public function get_ideas($number = 10, $sort = 'date', $sort_direction = 'DESC'
196196
((i.idea_votes_up + 1.9208) / (i.idea_votes_up + i.idea_votes_down) -
197197
1.96 * SQRT((i.idea_votes_up * i.idea_votes_down) / (i.idea_votes_up + i.idea_votes_down) + 0.9604) /
198198
(i.idea_votes_up + i.idea_votes_down)) / (1 + 3.8416 / (i.idea_votes_up + i.idea_votes_down))
199-
AS ci_lower_bound
200-
FROM ' . $this->table_ideas . ' i
201-
INNER JOIN ' . $this->table_topics . " t
202-
ON i.topic_id = t.topic_id
203-
WHERE $where
204-
ORDER BY ci_lower_bound " . $this->db->sql_escape($sort_direction);
199+
AS ci_lower_bound
200+
FROM ' . $this->table_ideas . ' i
201+
INNER JOIN ' . $this->table_topics . " t
202+
ON i.topic_id = t.topic_id
203+
WHERE $where
204+
ORDER BY ci_lower_bound " . $this->db->sql_escape($sort_direction);
205205
}
206206

207207
$result = $this->db->sql_query_limit($sql, $number, $start);
@@ -210,16 +210,18 @@ public function get_ideas($number = 10, $sort = 'date', $sort_direction = 'DESC'
210210

211211
if (count($rows))
212212
{
213-
$topic_ids = array_map(function($row) {
214-
return $row['topic_id'];
215-
}, $rows);
213+
$topic_ids = array_column($rows, 'topic_id');
214+
$idea_ids = array_column($rows, 'idea_id');
216215

217216
$topic_tracking_info = get_complete_topic_tracking((int) $this->config['ideas_forum_id'], $topic_ids);
217+
$user_voting_info = $this->get_users_votes($this->user->id, $idea_ids);
218218

219219
foreach ($rows as &$row)
220220
{
221221
$row['read'] = !(isset($topic_tracking_info[$row['topic_id']]) && $row['topic_last_post_time'] > $topic_tracking_info[$row['topic_id']]);
222+
$row['u_voted'] = isset($user_voting_info[$row['idea_id']]) ? (string) $user_voting_info[$row['idea_id']] : '';
222223
}
224+
unset ($row);
223225
}
224226

225227
return $rows;
@@ -721,7 +723,7 @@ public function delete_orphans()
721723
{
722724
// Find any orphans
723725
$sql = 'SELECT idea_id FROM ' . $this->table_ideas . '
724-
WHERE topic_id NOT IN (SELECT t.topic_id
726+
WHERE topic_id NOT IN (SELECT t.topic_id
725727
FROM ' . $this->table_topics . ' t
726728
WHERE t.forum_id = ' . (int) $this->config['ideas_forum_id'] . ')';
727729
$result = $this->db->sql_query($sql);
@@ -827,4 +829,31 @@ protected function profile_url()
827829

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

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>

styles/prosilver/theme/ideas.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ dd.topics {
6666
background-position: -65px 0;
6767
}
6868

69+
.voted {
70+
color: orange !important;
71+
}
72+
6973
/* Ideas Lists Styles */
7074
.view-all {
7175
color: #536482;

0 commit comments

Comments
 (0)