Advertisement
Guest User

lawl

a guest
Mar 2nd, 2015
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.45 KB | None | 0 0
  1. // Is this topic a guide? If so, add ranking functionality
  2. if ($is_guide)
  3. {  
  4.     $sql = 'SELECT guide_votecount, guide_rating
  5.             FROM ' . GUIDES_TABLE . "
  6.             WHERE guide_id = $topic_id";
  7.    
  8.     $result = $db->sql_query_limit($sql, 1);
  9.     $guide = $db->sql_fetchrow($result);
  10.     $vote_count = (int) $guide['guide_votecount'];
  11.     $rating = (float) $guide['guide_rating'];
  12.    
  13.     $db->sql_freeresult($result);  
  14.    
  15.     if ($user->data['is_registered'])
  16.     {
  17.         $sql = 'SELECT vote_value
  18.                 FROM ' . GUIDE_VOTES_TABLE . '
  19.                 WHERE guide_id = ' . $topic_id . '
  20.                 AND vote_user_id = ' . $user->data['user_id'];
  21.        
  22.        
  23.         $result = $db->sql_query_limit($sql, 1);
  24.         $row = $db->sql_fetchrow($result);
  25.         $cur_voted_id = (int) $row['vote_value'];
  26.         $db->sql_freeresult($result);
  27.     }
  28.    
  29.     // Define display and availability
  30.     $s_can_vote = ($auth->acl_get('f_vote', $forum_id));
  31.     $s_display_results = (!$s_can_vote || ($s_can_vote && sizeof($cur_voted_id)) || $view == 'viewpoll') ? true : false;
  32.    
  33.     if ($update && $s_can_vote)
  34.     {
  35.         // Error handling
  36.         if (!check_form_key('posting') || !is_numeric($voted_id) || intval($voted_id) > 5 || intval($voted_id) < 1 )
  37.         {
  38.             $redirect_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id" . (($start == 0) ? '' : "&amp;start=$start"));
  39.    
  40.             meta_refresh(5, $redirect_url);
  41.             if (!sizeof($voted_id))
  42.             {
  43.                 $message = 'NO_VOTE_OPTION';
  44.             }
  45.             else if (intval($voted_id) > 5 || intval($voted_id) < 1)
  46.             {
  47.                 $message = 'VOTE_OUT_OF_RANGE';
  48.             }
  49.             else
  50.             {
  51.                 $message = 'FORM_INVALID';
  52.             }
  53.    
  54.             $message = $user->lang[$message] . '<br /><br />' . sprintf($user->lang['RETURN_TOPIC'], '<a href="' . $redirect_url . '">', '</a>');
  55.             trigger_error($message);
  56.         }
  57.    
  58.         // update guide data and insert new vote
  59.         if ($voted_id != $cur_voted_id)
  60.         {
  61.             if ($cur_voted_id)
  62.             {
  63.                 $rating = ($rating * $vote_count + $voted_id - $cur_voted_id) / $vote_count;
  64.             }
  65.             else
  66.             {
  67.                 $rating = ($rating * $vote_count + $voted_id) / ($vote_count + 1);
  68.                 $vote_count++;
  69.             }
  70.            
  71.             $sql = 'UPDATE ' . GUIDES_TABLE . '
  72.                 SET guide_votecount = ' . $vote_count . ', guide_rating = ' . $rating . '
  73.                 WHERE guide_id = ' . $topic_id;
  74.             $db->sql_query($sql);
  75.        
  76.             if ($user->data['is_registered'])
  77.             {
  78.                 if (!$cur_voted_id)
  79.                 {
  80.                     $sql_ary = array(
  81.                         'guide_id'          => (int) $topic_id,
  82.                         'vote_value'        => (int) $voted_id,
  83.                         'vote_user_id'      => (int) $user->data['user_id'],
  84.                         'vote_user_ip'      => (string) $user->ip,
  85.                     );
  86.                     $sql = 'INSERT INTO ' . GUIDE_VOTES_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
  87.                 }
  88.                 else
  89.                 {
  90.                     $sql = 'UPDATE ' . GUIDE_VOTES_TABLE . '
  91.                         SET vote_value = ' . (int) $voted_id . ", vote_user_ip = '" . (string) $user->ip . "'
  92.                         WHERE guide_id = " . $topic_id . '
  93.                             AND vote_user_id = ' . (int) $user->data['user_id'];
  94.                 }
  95.                 $db->sql_query($sql);
  96.             }
  97.         }
  98.    
  99.         $sql = 'UPDATE ' . TOPICS_TABLE . '
  100.             SET poll_last_vote = ' . time() . "
  101.                 WHERE topic_id = $topic_id";
  102.         $db->sql_query($sql);
  103.    
  104.         $redirect_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id" . (($start == 0) ? '' : "&amp;start=$start"));
  105.         $message = $user->lang['VOTE_SUBMITTED'] . '<br /><br />' . sprintf($user->lang['RETURN_TOPIC'], '<a href="' . $redirect_url . '">', '</a>');
  106.    
  107.         if ($request->is_ajax())
  108.         {
  109.             // Filter out invalid options
  110.             $valid_user_votes = array_intersect(array_keys($vote_counts), $voted_id);
  111.    
  112.             $data = array(
  113.                     'NO_VOTES'          => $user->lang['NO_VOTES'],
  114.                     'success'           => true,
  115.                     'user_votes'        => array_flip($valid_user_votes),
  116.                     'vote_counts'       => $vote_counts,
  117.                     'total_votes'       => array_sum($vote_counts),
  118.                     'can_vote'          => !sizeof($valid_user_votes) || ($auth->acl_get('f_votechg', $forum_id) && $topic_data['poll_vote_change']),
  119.             );
  120.             $json_response = new \phpbb\json_response();
  121.             $json_response->send($data);
  122.         }
  123.    
  124.         meta_refresh(5, $redirect_url);
  125.         trigger_error($message);
  126.     }
  127.            
  128.     $template->assign_vars(array(
  129.         'POLL_OPTION_VOTED' => ($cur_voted_id == $i) ? true : false,
  130.            
  131.         'TOTAL_VOTES'       => $vote_count,
  132.         'POLL_LEFT_CAP_IMG' => $user->img('poll_left'),
  133.         'POLL_RIGHT_CAP_IMG'=> $user->img('poll_right'),
  134.  
  135.         'S_CAN_VOTE'        => $s_can_vote,
  136.         'S_DISPLAY_RESULTS' => $s_display_results,
  137.         'S_POLL_ACTION'     => $viewtopic_url,
  138.  
  139.         'U_VIEW_RESULTS'    => $viewtopic_url . '&amp;view=viewpoll',
  140.     ));
  141.    
  142.     unset($guide, $voted_id);
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement