@@ -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}
0 commit comments