@@ -286,12 +286,7 @@ public function vote(&$idea, $user_id, $value)
286286 }
287287
288288 // Check whether user has already voted - update if they have
289- $ sql = 'SELECT idea_id, vote_value
290- FROM ' . $ this ->table_votes . '
291- WHERE idea_id = ' . (int ) $ idea ['idea_id ' ] . '
292- AND user_id = ' . (int ) $ user_id ;
293- $ this ->db ->sql_query_limit ($ sql , 1 );
294- if ($ row = $ this ->db ->sql_fetchrow ())
289+ if ($ row = $ this ->get_users_vote ($ idea ['idea_id ' ], $ user_id ))
295290 {
296291 if ($ row ['vote_value ' ] != $ value )
297292 {
@@ -370,12 +365,7 @@ public function vote(&$idea, $user_id, $value)
370365 public function remove_vote (&$ idea , $ user_id )
371366 {
372367 // Only change something if user has already voted
373- $ sql = 'SELECT idea_id, vote_value
374- FROM ' . $ this ->table_votes . '
375- WHERE idea_id = ' . (int ) $ idea ['idea_id ' ] . '
376- AND user_id = ' . (int ) $ user_id ;
377- $ this ->db ->sql_query_limit ($ sql , 1 );
378- if ($ row = $ this ->db ->sql_fetchrow ())
368+ if ($ row = $ this ->get_users_vote ($ idea ['idea_id ' ], $ user_id ))
379369 {
380370 $ sql = 'DELETE FROM ' . $ this ->table_votes . '
381371 WHERE idea_id = ' . (int ) $ idea ['idea_id ' ] . '
@@ -430,4 +420,24 @@ public function get_voters($id)
430420
431421 return $ rows ;
432422 }
423+
424+ /**
425+ * Get a user's stored vote value for a given idea
426+ *
427+ * @param int $idea_id The idea id
428+ * @param int $user_id The user id
429+ * @return mixed Array with the row data, false if the row does not exist
430+ */
431+ protected function get_users_vote ($ idea_id , $ user_id )
432+ {
433+ $ sql = 'SELECT idea_id, vote_value
434+ FROM ' . $ this ->table_votes . '
435+ WHERE idea_id = ' . (int ) $ idea_id . '
436+ AND user_id = ' . (int ) $ user_id ;
437+ $ result = $ this ->db ->sql_query_limit ($ sql , 1 );
438+ $ row = $ this ->db ->sql_fetchrow ();
439+ $ this ->db ->sql_freeresult ($ result );
440+
441+ return $ row ;
442+ }
433443}
0 commit comments