/**
     * Delete reputation page/action
     *
     * @param int $rid	Reputation ID taken from the URL
     * @return null
     * @access public
     */
    public function delete($rid)
    {
        $this->user->add_lang_ext('pico/reputation', 'reputation_system');
        $is_ajax = $this->request->is_ajax();
        $submit = false;
        $post_type_id = (int) $this->reputation_manager->get_reputation_type_id('post');
        $sql_array = array('SELECT' => 'r.*, rt.reputation_type_name, p.post_id, uf.username AS username_from, ut.username AS username_to', 'FROM' => array($this->reputations_table => 'r', $this->reputation_types_table => 'rt'), 'LEFT_JOIN' => array(array('FROM' => array(POSTS_TABLE => 'p'), 'ON' => 'p.post_id = r.reputation_item_id
						AND r.reputation_type_id = ' . $post_type_id), array('FROM' => array(USERS_TABLE => 'uf'), 'ON' => 'r.user_id_from = uf.user_id '), array('FROM' => array(USERS_TABLE => 'ut'), 'ON' => 'r.user_id_to = ut.user_id ')), 'WHERE' => 'r.reputation_id = ' . $rid);
        $sql = $this->db->sql_build_query('SELECT', $sql_array);
        $result = $this->db->sql_query($sql);
        $row = $this->db->sql_fetchrow($result);
        $this->db->sql_freeresult($result);
        //We couldn't find this reputation. May be it was deleted meanwhile?
        if (empty($row)) {
            $message = $this->user->lang('RS_NO_REPUTATION');
            $json_data = array('error_msg' => $message);
            $redirect = append_sid("{$this->root_path}index.{$this->php_ext}");
            $redirect_text = 'RETURN_INDEX';
            $this->reputation_manager->response($message, $json_data, $redirect, $redirect_text, $is_ajax);
        }
        if ($this->request->is_set_post('cancel')) {
            redirect($this->helper->route('reputation_details_controller', array('uid' => $row['user_id_to'])));
        }
        if ($this->auth->acl_gets('m_rs_moderate') || $row['user_id_from'] == $this->user->data['user_id'] && $this->auth->acl_get('u_rs_delete')) {
            if ($is_ajax) {
                $submit = true;
            } else {
                $s_hidden_fields = build_hidden_fields(array('r' => $rid));
                if (confirm_box(true)) {
                    $submit = true;
                } else {
                    confirm_box(false, $this->user->lang('RS_REPUTATION_DELETE_CONFIRM'), $s_hidden_fields);
                }
            }
        } else {
            $message = $this->user->lang('RS_USER_CANNOT_DELETE');
            $json_data = array('error_msg' => $message);
            $redirect = $this->helper->route('reputation_details_controller', array('uid' => $row['user_id_to']));
            $redirect_text = 'RETURN_PAGE';
            $this->reputation_manager->response($message, $json_data, $redirect, $redirect_text, $is_ajax);
        }
        if ($submit) {
            try {
                $this->reputation_manager->delete_reputation($row);
            } catch (\pico\reputation\exception\base $e) {
                // Catch exception
                trigger_error($e->get_message($this->user));
            }
            $user_reputation = $this->reputation_manager->get_user_reputation($row['user_id_to']);
            $message = $this->user->lang('RS_POINTS_DELETED');
            $json_data = array('rid' => $rid, 'user_reputation' => $user_reputation);
            if (isset($row['post_id'])) {
                $post_reputation = $this->reputation_manager->get_post_reputation($row['post_id']);
                $json_data = array_merge($json_data, array('poster_id' => $row['user_id_to'], 'post_id' => $row['post_id'], 'post_reputation' => $post_reputation, 'reputation_class' => $this->reputation_helper->reputation_class($post_reputation), 'own_vote' => $row['user_id_from'] == $this->user->data['user_id'] ? true : false));
            }
            $redirect = $this->helper->route('reputation_details_controller', array('uid' => $row['user_id_to']));
            $redirect_text = 'RETURN_PAGE';
            $this->reputation_manager->response($message, $json_data, $redirect, $redirect_text, $is_ajax);
        }
    }
    /**
     * User details controller
     *
     * @param int $uid			User ID taken from the URL
     * @param string $sort_key	Sort key: id|username|time|point|action (default: id)
     * @param string $sort_dir	Sort direction: dsc|asc (descending|ascending) (default: dsc)
     * @return Symfony\Component\HttpFoundation\Response A Symfony Response object
     * @access public
     */
    public function userdetails($uid, $sort_key, $sort_dir)
    {
        $this->user->add_lang_ext('pico/reputation', array('reputation_system', 'reputation_rating'));
        $is_ajax = $this->request->is_ajax();
        $referer = $this->symfony_request->get('_referer');
        if (empty($this->config['rs_enable'])) {
            if ($is_ajax) {
                $json_response = new \phpbb\json_response();
                $json_data = array('error_msg' => $this->user->lang('RS_DISABLED'));
                $json_response->send($json_data);
            }
            redirect(append_sid("{$this->root_path}index.{$this->php_ext}"));
        }
        $sql = 'SELECT user_id, username, user_colour
			FROM ' . USERS_TABLE . '
			WHERE user_type <> 2
				AND user_id =' . (int) $uid;
        $result = $this->db->sql_query($sql);
        $user_row = $this->db->sql_fetchrow($result);
        $this->db->sql_freeresult($result);
        if (empty($user_row)) {
            $message = $this->user->lang('RS_NO_USER_ID');
            $json_data = array('error_msg' => $message);
            $redirect = append_sid("{$this->root_path}index.{$this->php_ext}");
            $redirect_text = 'RETURN_INDEX';
            $this->reputation_manager->response($message, $json_data, $redirect, $redirect_text, $is_ajax);
        }
        if (!$this->auth->acl_get('u_rs_view')) {
            $message = $this->user->lang('RS_VIEW_DISALLOWED');
            $json_data = array('error_msg' => $message);
            $redirect = append_sid("memberlist.{$this->php_ext}", 'mode=viewprofile&amp;u=' . $uid);
            $redirect_text = 'RETURN_PAGE';
            $this->reputation_manager->response($message, $json_data, $redirect, $redirect_text, $is_ajax);
        }
        $sort_key_sql = array('username' => 'u.username_clean', 'time' => 'r.reputation_time', 'point' => 'r.reputation_points', 'action' => 'rt.reputation_type_name', 'id' => 'r.reputation_id');
        // Sql order depends on sort key
        $order_by = $sort_key_sql[$sort_key] . ' ' . ($sort_dir == 'dsc' ? 'DESC' : 'ASC');
        $reputation_type_id = (int) $this->reputation_manager->get_reputation_type_id('post');
        $sql_array = array('SELECT' => 'r.*, rt.reputation_type_name, u.username, u.user_colour, u.user_avatar, u.user_avatar_type, u.user_avatar_width, u.user_avatar_height, p.post_id, p.forum_id, p.post_subject', 'FROM' => array($this->reputations_table => 'r', $this->reputation_types_table => 'rt'), 'LEFT_JOIN' => array(array('FROM' => array(USERS_TABLE => 'u'), 'ON' => 'u.user_id = r.user_id_from'), array('FROM' => array(POSTS_TABLE => 'p'), 'ON' => 'p.post_id = r.reputation_item_id
						AND r.reputation_type_id = ' . $reputation_type_id)), 'WHERE' => 'r.user_id_to = ' . $uid . '
				AND r.reputation_type_id = rt.reputation_type_id', 'ORDER_BY' => $order_by);
        $sql = $this->db->sql_build_query('SELECT', $sql_array);
        $result = $this->db->sql_query($sql);
        while ($row = $this->db->sql_fetchrow($result)) {
            $this->template->assign_block_vars('reputation', array('ID' => $row['reputation_id'], 'USERNAME' => get_username_string('full', $row['user_id_from'], $row['username'], $row['user_colour']), 'ACTION' => $this->user->lang('RS_' . strtoupper($row['reputation_type_name']) . '_RATING'), 'AVATAR' => phpbb_get_user_avatar($row), 'TIME' => $this->user->format_date($row['reputation_time']), 'COMMENT' => $row['reputation_comment'], 'POINTS' => $row['reputation_points'], 'POINTS_CLASS' => $this->reputation_helper->reputation_class($row['reputation_points']), 'POINTS_TITLE' => $this->user->lang('RS_POINTS_TITLE', $row['reputation_points']), 'U_DELETE' => $this->helper->route('reputation_delete_controller', array('rid' => $row['reputation_id'])), 'S_COMMENT' => !empty($row['reputation_comment']), 'S_DELETE' => $this->auth->acl_get('m_rs_moderate') || $row['user_id_from'] == $this->user->data['user_id'] && $this->auth->acl_get('u_rs_delete') ? true : false));
            // Generate post url
            $this->reputation_manager->generate_post_link($row);
        }
        $this->db->sql_freeresult($result);
        $this->template->assign_vars(array('USER_ID' => $uid, 'U_USER_DETAILS' => $this->helper->route('reputation_details_controller', array('uid' => $uid)), 'U_SORT_USERNAME' => $this->helper->route('reputation_user_details_controller', array('uid' => $uid, 'sort_key' => 'username', 'sort_dir' => $sort_key == 'username' && $sort_dir == 'asc' ? 'dsc' : 'asc')), 'U_SORT_TIME' => $this->helper->route('reputation_user_details_controller', array('uid' => $uid, 'sort_key' => 'time', 'sort_dir' => $sort_key == 'time' && $sort_dir == 'asc' ? 'dsc' : 'asc')), 'U_SORT_POINT' => $this->helper->route('reputation_user_details_controller', array('uid' => $uid, 'sort_key' => 'point', 'sort_dir' => $sort_key == 'point' && $sort_dir == 'asc' ? 'dsc' : 'asc')), 'U_SORT_ACTION' => $this->helper->route('reputation_user_details_controller', array('uid' => $uid, 'sort_key' => 'action', 'sort_dir' => $sort_key == 'action' && $sort_dir == 'asc' ? 'dsc' : 'asc')), 'U_CLEAR' => $this->helper->route('reputation_clear_user_controller', array('uid' => $uid)), 'U_REPUTATION_REFERER' => $referer, 'L_RS_USER_REPUTATION' => $this->user->lang('RS_USER_REPUTATION', get_username_string('username', $user_row['user_id'], $user_row['username'], $user_row['user_colour'])), 'S_RS_AVATAR' => $this->config['rs_display_avatar'] ? true : false, 'S_RS_COMMENT' => $this->config['rs_enable_comment'] ? true : false, 'S_RS_POINTS_IMG' => $this->config['rs_point_type'] ? true : false, 'S_CLEAR' => $this->auth->acl_gets('m_rs_moderate') ? true : false, 'S_IS_AJAX' => $is_ajax ? true : false));
        return $this->helper->render('userdetails.html');
    }
 /**
  * Add post row data for displaying reputation
  *
  * @param object $event The event object
  * @return null
  * @access public
  */
 public function post_row_reputation($event)
 {
     if ($this->config['rs_enable'] && $this->config['rs_post_rating']) {
         $row = $event['row'];
         $user_poster_data = $event['user_poster_data'];
         $post_row = $event['post_row'];
         $post_id = $row['post_id'];
         $poster_id = $event['poster_id'];
         // Check post status and return its status as class
         //	Is it own post, rated good or rated bad?
         if ($this->user->data['user_id'] == $poster_id) {
             $post_vote_class = 'own';
         } else {
             $post_vote_class = $row['user_voted'] ? $row['reputation_points'] > 0 ? 'rated_good' : 'rated_bad' : '';
         }
         $post_row = array_merge($post_row, array('S_VIEW_REPUTATION' => $this->auth->acl_get('u_rs_view') ? true : false, 'S_RATE_POST' => $this->auth->acl_get('f_rs_rate', $row['forum_id']) && $this->auth->acl_get('u_rs_rate_post') && $poster_id != ANONYMOUS ? true : false, 'S_RATE_POST_NEGATIVE' => $this->auth->acl_get('f_rs_rate_negative', $row['forum_id']) && $this->config['rs_negative_point'] ? true : false, 'RS_RATE_POST_NEGATIVE' => $row['user_voted'] ? $this->user->lang('RS_POST_RATED') : $this->user->lang('RS_RATE_POST_NEGATIVE'), 'RS_RATE_POST_POSITIVE' => $row['user_voted'] ? $this->user->lang('RS_POST_RATED') : $this->user->lang('RS_RATE_POST_POSITIVE'), 'U_RATE_POST_POSITIVE' => $this->helper->route('reputation_post_rating_controller', array('mode' => 'positive', 'post_id' => $post_id)), 'U_RATE_POST_NEGATIVE' => $this->helper->route('reputation_post_rating_controller', array('mode' => 'negative', 'post_id' => $post_id)), 'U_VIEW_POST_REPUTATION' => $this->helper->route('reputation_post_details_controller', array('post_id' => $post_id)), 'U_VIEW_USER_REPUTATION' => $this->helper->route('reputation_user_details_controller', array('uid' => $poster_id)), 'POST_REPUTATION' => $row['post_reputation'], 'POST_REPUTATION_CLASS' => $this->reputation_helper->reputation_class($row['post_reputation']), 'POST_VOTE_CLASS' => $post_vote_class, 'USER_REPUTATION' => $user_poster_data['reputation']));
         $event['post_row'] = $post_row;
     }
 }
    /**
     * Display the post rating page
     *
     * @param string $mode		Mode taken from the URL 
     * @param int $post_id		Post ID taken from the URL
     * @return Symfony\Component\HttpFoundation\Response A Symfony Response object
     * @access public
     */
    public function post($mode, $post_id)
    {
        $this->user->add_lang_ext('pico/reputation', 'reputation_rating');
        // Define basic variables
        $error = '';
        $is_ajax = $this->request->is_ajax();
        $referer = $this->symfony_request->get('_referer');
        if (empty($this->config['rs_enable'])) {
            if ($is_ajax) {
                $json_response = new \phpbb\json_response();
                $json_data = array('error_msg' => $this->user->lang('RS_DISABLED'));
                $json_response->send($json_data);
            }
            redirect(append_sid("{$this->root_path}index.{$this->php_ext}"));
        }
        $reputation_type_id = (int) $this->reputation_manager->get_reputation_type_id('post');
        $sql_array = array('SELECT' => 'p.forum_id, p.poster_id, p.post_subject, u.user_type, u.user_reputation, f.reputation_enabled, r.reputation_id, r.reputation_points', 'FROM' => array(POSTS_TABLE => 'p', USERS_TABLE => 'u', FORUMS_TABLE => 'f'), 'LEFT_JOIN' => array(array('FROM' => array($this->reputations_table => 'r'), 'ON' => 'p.post_id = r.reputation_item_id
						AND r.reputation_type_id = ' . $reputation_type_id . '
						AND r.user_id_from = ' . $this->user->data['user_id'])), 'WHERE' => 'p.post_id = ' . $post_id . '
				AND p.poster_id = u.user_id
				AND p.forum_id = f.forum_id');
        $sql = $this->db->sql_build_query('SELECT', $sql_array);
        $result = $this->db->sql_query($sql);
        $row = $this->db->sql_fetchrow($result);
        $this->db->sql_freeresult($result);
        // We couldn't find this post. May be it was deleted while user voted?
        if (!$row) {
            $message = $this->user->lang('RS_NO_POST');
            $json_data = array('error_msg' => $message);
            $redirect = append_sid("{$this->root_path}index.{$this->php_ext}");
            $redirect_text = 'RETURN_INDEX';
            $this->reputation_manager->response($message, $json_data, $redirect, $redirect_text, $is_ajax);
        }
        // Cancel action
        if ($this->request->is_set_post('cancel')) {
            redirect(append_sid("{$this->root_path}viewtopic.{$this->php_ext}", 'f=' . $row['forum_id'] . '&amp;p=' . $post_id) . '#p' . $post_id);
        }
        // Fire error if post rating is disabled
        if (!$this->config['rs_post_rating'] || !$this->config['rs_negative_point'] && $mode == 'negative' || !$row['reputation_enabled']) {
            $message = $this->user->lang('RS_DISABLED');
            $json_data = array('error_msg' => $message);
            $redirect = append_sid("{$this->root_path}viewtopic.{$this->php_ext}", 'f=' . $row['forum_id'] . '&amp;p=' . $post_id) . '#p' . $post_id;
            $redirect_text = 'RETURN_TOPIC';
            $this->reputation_manager->response($message, $json_data, $redirect, $redirect_text, $is_ajax);
        }
        // No anonymous voting is allowed
        if ($row['user_type'] == USER_IGNORE) {
            $message = $this->user->lang('RS_USER_ANONYMOUS');
            $json_data = array('error_msg' => $message);
            $redirect = append_sid("{$this->root_path}viewtopic.{$this->php_ext}", 'f=' . $row['forum_id'] . '&amp;p=' . $post_id) . '#p' . $post_id;
            $redirect_text = 'RETURN_TOPIC';
            $this->reputation_manager->response($message, $json_data, $redirect, $redirect_text, $is_ajax);
        }
        // You cannot rate your own post
        if ($row['poster_id'] == $this->user->data['user_id']) {
            $message = $this->user->lang('RS_SELF');
            $json_data = array('error_msg' => $message);
            $redirect = append_sid("{$this->root_path}viewtopic.{$this->php_ext}", 'f=' . $row['forum_id'] . '&amp;p=' . $post_id) . '#p' . $post_id;
            $redirect_text = 'RETURN_TOPIC';
            $this->reputation_manager->response($message, $json_data, $redirect, $redirect_text, $is_ajax);
        }
        // Don't allow to rate same post
        if ($row['reputation_id']) {
            $message = $this->user->lang('RS_SAME_POST', $row['reputation_points']);
            $json_data = array('error_msg' => $message);
            $redirect = append_sid("{$this->root_path}viewtopic.{$this->php_ext}", 'f=' . $row['forum_id'] . '&amp;p=' . $post_id) . '#p' . $post_id;
            $redirect_text = 'RETURN_TOPIC';
            $this->reputation_manager->response($message, $json_data, $redirect, $redirect_text, $is_ajax);
        }
        // Check if user is allowed to vote
        if (!$this->auth->acl_get('f_rs_rate', $row['forum_id']) || !$this->auth->acl_get('f_rs_rate_negative', $row['forum_id']) && $mode == 'negative' || !$this->auth->acl_get('u_rs_rate_post')) {
            $message = $this->user->lang('RS_USER_DISABLED');
            $json_data = array('error_msg' => $message);
            $redirect = append_sid("{$this->root_path}viewtopic.{$this->php_ext}", 'f=' . $row['forum_id'] . '&amp;p=' . $post_id) . '#p' . $post_id;
            $redirect_text = 'RETURN_TOPIC';
            $this->reputation_manager->response($message, $json_data, $redirect, $redirect_text, $is_ajax);
        }
        //Check if user reputation is enough to give negative points
        if ($this->config['rs_min_rep_negative'] && $this->user->data['user_reputation'] < $this->config['rs_min_rep_negative'] && $mode == 'negative') {
            $message = $this->user->lang('RS_USER_NEGATIVE', $this->config['rs_min_rep_negative']);
            $json_data = array('error_msg' => $message);
            $redirect = append_sid("{$this->root_path}viewtopic.{$this->php_ext}", 'f=' . $row['forum_id'] . '&amp;p=' . $post_id) . '#p' . $post_id;
            $redirect_text = 'RETURN_TOPIC';
            $this->reputation_manager->response($message, $json_data, $redirect, $redirect_text, $is_ajax);
        }
        // Anti-abuse behaviour
        if (!empty($this->config['rs_anti_time']) && !empty($this->config['rs_anti_post'])) {
            $anti_time = time() - $this->config['rs_anti_time'] * 3600;
            $sql_and = !$this->config['rs_anti_method'] ? 'AND user_id_to = ' . $row['poster_id'] : '';
            $sql = 'SELECT COUNT(reputation_id) AS reputation_per_day
				FROM ' . $this->reputations_table . '
				WHERE user_id_from = ' . $this->user->data['user_id'] . '
					' . $sql_and . '
					AND reputation_type_id = ' . $reputation_type_id . '
					AND reputation_time > ' . $anti_time;
            $result = $this->db->sql_query($sql);
            $anti_row = $this->db->sql_fetchrow($result);
            $this->db->sql_freeresult($result);
            if ($anti_row['reputation_per_day'] >= $this->config['rs_anti_post']) {
                $message = $this->user->lang('RS_ANTISPAM_INFO');
                $json_data = array('error_msg' => $message);
                $redirect = append_sid("{$this->root_path}viewtopic.{$this->php_ext}", 'f=' . $row['forum_id'] . '&amp;p=' . $post_id) . '#p' . $post_id;
                $redirect_text = 'RETURN_TOPIC';
                $this->reputation_manager->response($message, $json_data, $redirect, $redirect_text, $is_ajax);
            }
        }
        // Disallow rating banned users
        if ($this->user->check_ban($row['poster_id'], false, false, true)) {
            $message = $this->user->lang('RS_USER_BANNED');
            $json_data = array('error_msg' => $message);
            $redirect = append_sid("{$this->root_path}viewtopic.{$this->php_ext}", 'f=' . $row['forum_id'] . '&amp;p=' . $post_id) . '#p' . $post_id;
            $redirect_text = 'RETURN_TOPIC';
            $this->reputation_manager->response($message, $json_data, $redirect, $redirect_text, $is_ajax);
        }
        // Prevent overrating one user by another
        if ($this->reputation_manager->prevent_rating($row['poster_id'])) {
            $message = $this->user->lang('RS_ANTISPAM_INFO');
            $json_data = array('error_msg' => $message);
            $redirect = append_sid("{$this->root_path}viewtopic.{$this->php_ext}", 'f=' . $row['forum_id'] . '&amp;p=' . $post_id) . '#p' . $post_id;
            $redirect_text = 'RETURN_TOPIC';
            $this->reputation_manager->response($message, $json_data, $redirect, $redirect_text, $is_ajax);
        }
        // Request variables
        $points = $this->request->variable('points', '');
        $comment = $this->request->variable('comment', '', true);
        // Submit vote
        $submit = false;
        if ($this->request->is_set_post('submit_vote')) {
            $submit = true;
        }
        // The comment
        if ($submit && $this->config['rs_enable_comment']) {
            // The comment is too long
            if (strlen($comment) > $this->config['rs_comment_max_chars']) {
                $submit = false;
                $error = $this->user->lang('RS_COMMENT_TOO_LONG', strlen($comment), $this->config['rs_comment_max_chars']);
                if ($is_ajax) {
                    $json_response = new \phpbb\json_response();
                    $json_data = array('comment_error' => $error);
                    $json_response->send($json_data);
                }
            }
            // Force the comment
            if (($this->config['rs_force_comment'] == self::RS_COMMENT_BOTH || $this->config['rs_force_comment'] == self::RS_COMMENT_POST) && empty($comment)) {
                $submit = false;
                $error = $this->user->lang('RS_NO_COMMENT');
                if ($is_ajax) {
                    $json_response = new \phpbb\json_response();
                    $json_data = array('comment_error' => $error);
                    $json_response->send($json_data);
                }
            }
        }
        // Sumbit vote when the comment and the reputation power are disabled
        if (!$this->config['rs_enable_comment'] && !$this->config['rs_enable_power']) {
            $submit = true;
            $points = $mode == 'negative' ? '-1' : '1';
        }
        // Get reputation power
        if ($this->config['rs_enable_power']) {
            $voting_power_pulldown = '';
            // Get details on user voting - how much power was used
            $used_power = $this->reputation_power->used($this->user->data['user_id']);
            // Calculate how much maximum power the user has
            $max_voting_power = $this->reputation_power->get($this->user->data['user_posts'], $this->user->data['user_regdate'], $this->user->data['user_reputation'], $this->user->data['user_warnings'], $this->user->data['group_id']);
            if ($max_voting_power < 1) {
                $message = $this->user->lang('RS_NO_POWER');
                $json_data = array('error_msg' => $message);
                $redirect = append_sid("{$this->root_path}viewtopic.{$this->php_ext}", 'f=' . $row['forum_id'] . '&amp;p=' . $post_id) . '#p' . $post_id;
                $redirect_text = 'RETURN_TOPIC';
                $this->reputation_manager->response($message, $json_data, $redirect, $redirect_text, $is_ajax);
            }
            $voting_power_left = $max_voting_power - $used_power;
            // Don't allow to vote more than set in ACP per 1 vote
            $max_voting_allowed = $this->config['rs_power_renewal'] ? min($max_voting_power, $voting_power_left) : $max_voting_power;
            // If now voting power left - fire error and exit
            if ($voting_power_left <= 0 && $this->config['rs_power_renewal']) {
                $message = $this->user->lang('RS_NO_POWER_LEFT', $max_voting_power);
                $json_data = array('error_msg' => $message);
                $redirect = append_sid("{$this->root_path}viewtopic.{$this->php_ext}", 'f=' . $row['forum_id'] . '&amp;p=' . $post_id) . '#p' . $post_id;
                $redirect_text = 'RETURN_TOPIC';
                $this->reputation_manager->response($message, $json_data, $redirect, $redirect_text, $is_ajax);
            }
            $this->template->assign_vars(array('RS_POWER_POINTS_LEFT' => $this->config['rs_power_renewal'] ? $this->user->lang('RS_VOTE_POWER_LEFT_OF_MAX', $voting_power_left, $max_voting_power, $max_voting_allowed) : '', 'RS_POWER_PROGRESS_EMPTY' => $this->config['rs_power_renewal'] && $max_voting_power ? round(($max_voting_power - $voting_power_left) / $max_voting_power * 100, 0) : ''));
            //Preparing HTML for voting by manual spending of user power
            for ($i = 1; $i <= $max_voting_allowed; ++$i) {
                if ($mode == 'negative') {
                    $voting_power_pulldown = '<option value="-' . $i . '">' . $this->user->lang('RS_NEGATIVE') . ' (-' . $i . ') </option>';
                } else {
                    $voting_power_pulldown = '<option value="' . $i . '">' . $this->user->lang('RS_POSITIVE') . ' (+' . $i . ')</option>';
                }
                $this->template->assign_block_vars('reputation', array('REPUTATION_POWER' => $voting_power_pulldown));
            }
        } else {
            $points = $mode == 'negative' ? '-1' : '1';
        }
        // Save vote
        if ($submit) {
            // Prevent cheater to break the forum permissions to give negative points or give more points than they can
            if (!$this->auth->acl_get('f_rs_rate_negative', $row['forum_id']) && $points < 0 || $points < 0 && $this->config['rs_min_rep_negative'] && $this->user->data['user_reputation'] < $this->config['rs_min_rep_negative'] || $this->config['rs_enable_power'] && ($points > $max_voting_allowed || $points < -$max_voting_allowed)) {
                $submit = false;
                $error = $this->user->lang('RS_USER_CANNOT_RATE');
                if ($is_ajax) {
                    $json_response = new \phpbb\json_response();
                    $json_data = array('comment_error' => $error);
                    $json_response->send($json_data);
                }
            }
        }
        if (!empty($error)) {
            $submit = false;
        }
        if ($submit) {
            $data = array('user_id_from' => $this->user->data['user_id'], 'user_id_to' => $row['poster_id'], 'reputation_type' => 'post', 'reputation_item_id' => $post_id, 'reputation_points' => $points, 'reputation_comment' => $comment);
            try {
                $this->reputation_manager->store_reputation($data);
            } catch (\pico\reputation\exception\base $e) {
                // Catch exception
                $error = $e->get_message($this->user);
            }
            // Notification data
            $notification_data = array('user_id_to' => $row['poster_id'], 'user_id_from' => $this->user->data['user_id'], 'post_id' => $post_id, 'post_subject' => $row['post_subject']);
            $notification_type = $points > 0 ? 'pico.reputation.notification.type.rate_post_positive' : 'pico.reputation.notification.type.rate_post_negative';
            $this->reputation_manager->add_notification($notification_type, $notification_data);
            // Get post reputation
            $post_reputation = $this->reputation_manager->get_post_reputation($post_id);
            $message = $this->user->lang('RS_VOTE_SAVED');
            $json_data = array('post_id' => $post_id, 'poster_id' => $row['poster_id'], 'post_reputation' => $post_reputation, 'user_reputation' => $this->reputation_manager->get_user_reputation($row['poster_id']), 'reputation_class' => $this->reputation_helper->reputation_class($post_reputation), 'reputation_vote' => $points > 0 ? 'rated_good' : 'rated_bad', 'success_msg' => $message);
            $redirect = append_sid("{$this->root_path}viewtopic.{$this->php_ext}", 'f=' . $row['forum_id'] . '&amp;p=' . $post_id) . '#p' . $post_id;
            $redirect_text = 'RETURN_TOPIC';
            $this->reputation_manager->response($message, $json_data, $redirect, $redirect_text, $is_ajax);
        }
        $this->template->assign_vars(array('ERROR_MSG' => $error, 'S_CONFIRM_ACTION' => $this->helper->route('reputation_post_rating_controller', array('mode' => $mode, 'post_id' => $post_id)), 'S_ERROR' => !empty($error) ? true : false, 'S_RS_COMMENT_ENABLE' => $this->config['rs_enable_comment'] ? true : false, 'S_RS_POWER_ENABLE' => $this->config['rs_enable_power'] ? true : false, 'S_IS_AJAX' => $is_ajax, 'U_RS_REFERER' => $referer));
        return $this->helper->render('ratepost.html', $this->user->lang('RS_POST_RATING'));
    }