Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Is this topic a guide? If so, add ranking functionality
- if ($is_guide)
- {
- $sql = 'SELECT guide_votecount, guide_rating
- FROM ' . GUIDES_TABLE . "
- WHERE guide_id = $topic_id";
- $result = $db->sql_query_limit($sql, 1);
- $guide = $db->sql_fetchrow($result);
- $vote_count = (int) $guide['guide_votecount'];
- $rating = (float) $guide['guide_rating'];
- $db->sql_freeresult($result);
- if ($user->data['is_registered'])
- {
- $sql = 'SELECT vote_value
- FROM ' . GUIDE_VOTES_TABLE . '
- WHERE guide_id = ' . $topic_id . '
- AND vote_user_id = ' . $user->data['user_id'];
- $result = $db->sql_query_limit($sql, 1);
- $row = $db->sql_fetchrow($result);
- $cur_voted_id = (int) $row['vote_value'];
- $db->sql_freeresult($result);
- }
- // Define display and availability
- $s_can_vote = ($auth->acl_get('f_vote', $forum_id));
- $s_display_results = (!$s_can_vote || ($s_can_vote && sizeof($cur_voted_id)) || $view == 'viewpoll') ? true : false;
- if ($update && $s_can_vote)
- {
- // Error handling
- if (!check_form_key('posting') || !is_numeric($voted_id) || intval($voted_id) > 5 || intval($voted_id) < 1 )
- {
- $redirect_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&t=$topic_id" . (($start == 0) ? '' : "&start=$start"));
- meta_refresh(5, $redirect_url);
- if (!sizeof($voted_id))
- {
- $message = 'NO_VOTE_OPTION';
- }
- else if (intval($voted_id) > 5 || intval($voted_id) < 1)
- {
- $message = 'VOTE_OUT_OF_RANGE';
- }
- else
- {
- $message = 'FORM_INVALID';
- }
- $message = $user->lang[$message] . '<br /><br />' . sprintf($user->lang['RETURN_TOPIC'], '<a href="' . $redirect_url . '">', '</a>');
- trigger_error($message);
- }
- // update guide data and insert new vote
- if ($voted_id != $cur_voted_id)
- {
- if ($cur_voted_id)
- {
- $rating = ($rating * $vote_count + $voted_id - $cur_voted_id) / $vote_count;
- }
- else
- {
- $rating = ($rating * $vote_count + $voted_id) / ($vote_count + 1);
- $vote_count++;
- }
- $sql = 'UPDATE ' . GUIDES_TABLE . '
- SET guide_votecount = ' . $vote_count . ', guide_rating = ' . $rating . '
- WHERE guide_id = ' . $topic_id;
- $db->sql_query($sql);
- if ($user->data['is_registered'])
- {
- if (!$cur_voted_id)
- {
- $sql_ary = array(
- 'guide_id' => (int) $topic_id,
- 'vote_value' => (int) $voted_id,
- 'vote_user_id' => (int) $user->data['user_id'],
- 'vote_user_ip' => (string) $user->ip,
- );
- $sql = 'INSERT INTO ' . GUIDE_VOTES_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
- }
- else
- {
- $sql = 'UPDATE ' . GUIDE_VOTES_TABLE . '
- SET vote_value = ' . (int) $voted_id . ", vote_user_ip = '" . (string) $user->ip . "'
- WHERE guide_id = " . $topic_id . '
- AND vote_user_id = ' . (int) $user->data['user_id'];
- }
- $db->sql_query($sql);
- }
- }
- $sql = 'UPDATE ' . TOPICS_TABLE . '
- SET poll_last_vote = ' . time() . "
- WHERE topic_id = $topic_id";
- $db->sql_query($sql);
- $redirect_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&t=$topic_id" . (($start == 0) ? '' : "&start=$start"));
- $message = $user->lang['VOTE_SUBMITTED'] . '<br /><br />' . sprintf($user->lang['RETURN_TOPIC'], '<a href="' . $redirect_url . '">', '</a>');
- if ($request->is_ajax())
- {
- // Filter out invalid options
- $valid_user_votes = array_intersect(array_keys($vote_counts), $voted_id);
- $data = array(
- 'NO_VOTES' => $user->lang['NO_VOTES'],
- 'success' => true,
- 'user_votes' => array_flip($valid_user_votes),
- 'vote_counts' => $vote_counts,
- 'total_votes' => array_sum($vote_counts),
- 'can_vote' => !sizeof($valid_user_votes) || ($auth->acl_get('f_votechg', $forum_id) && $topic_data['poll_vote_change']),
- );
- $json_response = new \phpbb\json_response();
- $json_response->send($data);
- }
- meta_refresh(5, $redirect_url);
- trigger_error($message);
- }
- $template->assign_vars(array(
- 'POLL_OPTION_VOTED' => ($cur_voted_id == $i) ? true : false,
- 'TOTAL_VOTES' => $vote_count,
- 'POLL_LEFT_CAP_IMG' => $user->img('poll_left'),
- 'POLL_RIGHT_CAP_IMG'=> $user->img('poll_right'),
- 'S_CAN_VOTE' => $s_can_vote,
- 'S_DISPLAY_RESULTS' => $s_display_results,
- 'S_POLL_ACTION' => $viewtopic_url,
- 'U_VIEW_RESULTS' => $viewtopic_url . '&view=viewpoll',
- ));
- unset($guide, $voted_id);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement