/**
     * Clear user reputation
     *
     * @param int $uid	User ID
     * @return null
     * @access public
     */
    public function clear_user($uid)
    {
        $this->user->add_lang_ext('pico/reputation', 'reputation_system');
        $is_ajax = $this->request->is_ajax();
        $submit = false;
        $sql_array = array('SELECT' => 'r.*, ut.username AS username_to', 'FROM' => array($this->reputations_table => 'r'), 'LEFT_JOIN' => array(array('FROM' => array(USERS_TABLE => 'ut'), 'ON' => 'r.user_id_to = ut.user_id ')), 'WHERE' => 'r.user_id_to = ' . $uid);
        $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);
        }
        $redirect = $this->helper->route('reputation_details_controller', array('uid' => $uid));
        if ($this->request->is_set_post('cancel')) {
            redirect($redirect);
        }
        $post_ids = array();
        $post_type_id = (int) $this->reputation_manager->get_reputation_type_id('post');
        $sql = 'SELECT reputation_item_id
			FROM ' . $this->reputations_table . "\n\t\t\tWHERE user_id_to = {$uid}\n\t\t\t\tAND reputation_type_id = {$post_type_id}\n\t\t\tGROUP BY reputation_item_id";
        $result = $this->db->sql_query($sql);
        while ($post_row = $this->db->sql_fetchrow($result)) {
            $post_ids[] = $post_row['reputation_item_id'];
        }
        $this->db->sql_freeresult($result);
        $redirect_text = 'RETURN_PAGE';
        if ($this->auth->acl_gets('m_rs_moderate')) {
            if ($is_ajax) {
                $submit = true;
            } else {
                $s_hidden_fields = build_hidden_fields(array('u' => $uid));
                if (confirm_box(true)) {
                    $submit = true;
                } else {
                    confirm_box(false, $this->user->lang('RS_CLEAR_POST_CONFIRM'), $s_hidden_fields);
                }
            }
        } else {
            $message = $this->user->lang('RS_USER_CANNOT_DELETE');
            $json_data = array('error_msg' => $message);
            $this->reputation_manager->response($message, $json_data, $redirect, $redirect_text, $is_ajax);
        }
        if ($submit) {
            try {
                $this->reputation_manager->clear_user_reputation($uid, $row, $post_ids);
            } catch (\pico\reputation\exception\base $e) {
                // Catch exception
                trigger_error($e->get_message($this->user));
            }
            $message = $this->user->lang('RS_CLEARED_USER');
            $json_data = array('clear_user' => true, 'post_ids' => $post_ids, 'poster_id' => $uid, 'user_reputation' => 0, 'post_reputation' => 0, 'reputation_class' => 'neutral');
            $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');
    }
    /**
     * Display the user rating page
     *
     * @param int $uid	User ID taken from the URL
     * @return Symfony\Component\HttpFoundation\Response A Symfony Response object
     * @access public
     */
    public function user($uid)
    {
        $this->user->add_lang_ext('pico/reputation', 'reputation_rating');
        // Define some 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}"));
        }
        if (!$this->config['rs_user_rating'] || !$this->auth->acl_get('u_rs_rate')) {
            $message = $this->user->lang('RS_DISABLED');
            $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);
        }
        $sql = 'SELECT user_id, user_type
			FROM ' . USERS_TABLE . "\n\t\t\tWHERE user_id = {$uid}";
        $result = $this->db->sql_query($sql);
        $row = $this->db->sql_fetchrow($result);
        $this->db->sql_freeresult($result);
        if (!$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);
        }
        // Cancel action
        if ($this->request->is_set_post('cancel')) {
            redirect(append_sid("memberlist.{$this->php_ext}", 'mode=viewprofile&amp;u=' . $uid));
        }
        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}index.{$this->php_ext}");
            $redirect_text = 'RETURN_INDEX';
            $this->reputation_manager->response($message, $json_data, $redirect, $redirect_text, $is_ajax);
        }
        if ($row['user_id'] == $this->user->data['user_id']) {
            $message = $this->user->lang('RS_SELF');
            $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);
        }
        // Disallow rating banned users
        if ($this->user->check_ban($uid, false, false, true)) {
            $message = $this->user->lang('RS_USER_BANNED');
            $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);
        }
        $reputation_type_id = (int) $this->reputation_manager->get_reputation_type_id('user');
        $sql = 'SELECT reputation_id, reputation_time
			FROM ' . $this->reputations_table . "\n\t\t\tWHERE user_id_to = {$uid}\n\t\t\t\tAND user_id_from = {$this->user->data['user_id']}\n\t\t\t\tAND reputation_type_id = {$reputation_type_id}\n\t\t\tORDER by reputation_id DESC";
        $result = $this->db->sql_query($sql);
        $check_user = $this->db->sql_fetchrow($result);
        $this->db->sql_freeresult($result);
        if ($check_user && !$this->config['rs_user_rating_gap']) {
            $message = $this->user->lang('RS_SAME_USER');
            $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);
        }
        if ($this->config['rs_user_rating_gap'] && time() < $check_user['reputation_time'] + $this->config['rs_user_rating_gap'] * 86400) {
            //Inform user how long he has to wait to rate the user
            $next_vote_time = $check_user['reputation_time'] + $this->config['rs_user_rating_gap'] * 86400 - time();
            $next_vote_in = '';
            $next_vote_in .= intval($next_vote_time / 86400) ? intval($next_vote_time / 86400) . ' ' . $this->user->lang('DAYS') . ' ' : '';
            $next_vote_in .= intval($next_vote_time / 3600 % 24) ? intval($next_vote_time / 3600 % 24) . ' ' . $this->user->lang('HOURS') . ' ' : '';
            $next_vote_in .= intval($next_vote_time / 60 % 60) ? intval($next_vote_time / 60 % 60) . ' ' . $this->user->lang('MINUTES') : '';
            $next_vote_in .= intval($next_vote_time) < 60 ? intval($next_vote_time) . ' ' . $this->user->lang('SECONDS') : '';
            $message = $this->user->lang('RS_USER_GAP', $next_vote_in);
            $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);
        }
        if ($this->reputation_manager->prevent_rating($uid)) {
            $message = $this->user->lang('RS_SAME_USER');
            $json_data = array('error_msg' => $message);
            $redirect = append_sid("memberlist.{$this->php_ext}", 'mode=viewprofile&amp;u=' . $uid);
            $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);
        $error = '';
        // 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_USER) && 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);
                }
            }
        }
        // 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 a 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("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);
            }
            $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("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);
            }
            $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
            $startpower = $this->config['rs_negative_point'] ? -$max_voting_allowed : 1;
            for ($i = $max_voting_allowed; $i >= $startpower; $i--) {
                if ($i == 0) {
                    $voting_power_pulldown = '';
                }
                if ($i > 0) {
                    $voting_power_pulldown = '<option value="' . $i . '">' . $this->user->lang('RS_POSITIVE') . ' (+' . $i . ') </option>';
                }
                if ($i < 0 && $this->auth->acl_get('u_rs_rate_negative') && $this->config['rs_negative_point'] && ($this->config['rs_min_rep_negative'] != 0 ? $this->user->data['user_reputation'] >= $this->config['rs_min_rep_negative'] : true)) {
                    $voting_power_pulldown = '<option value="' . $i . '">' . $this->user->lang('RS_NEGATIVE') . ' (' . $i . ') </option>';
                }
                $this->template->assign_block_vars('reputation', array('REPUTATION_POWER' => $voting_power_pulldown));
            }
        } else {
            $rs_power = '<option value="1">' . $this->user->lang('RS_POSITIVE') . '</option>';
            if ($this->auth->acl_get('u_rs_rate_negative') && $this->config['rs_negative_point'] && ($this->config['rs_min_rep_negative'] != 0 ? $this->user->data['user_reputation'] >= $this->config['rs_min_rep_negative'] : true)) {
                $rs_power .= '<option value="-1">' . $this->user->lang('RS_NEGATIVE') . '</option>';
            } else {
                if ($this->config['rs_enable_comment']) {
                    $points = 1;
                } else {
                    $submit = true;
                    $points = 1;
                }
            }
            $this->template->assign_block_vars('reputation', array('REPUTATION_POWER' => $rs_power));
        }
        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('u_rs_rate_negative') && $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' => $uid, 'reputation_type' => 'user', 'reputation_item_id' => $uid, '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);
            }
            // Prepare notification data and notify user
            $notification_data = array('user_id_to' => $uid, 'user_id_from' => $this->user->data['user_id']);
            $this->reputation_manager->add_notification('pico.reputation.notification.type.rate_user', $notification_data);
            $message = $this->user->lang('RS_VOTE_SAVED');
            $json_data = array('user_reputation' => '<strong>' . $this->reputation_manager->get_user_reputation($uid) . '</strong>', 'success_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);
        }
        $this->template->assign_vars(array('ERROR_MSG' => $error, 'S_CONFIRM_ACTION' => $this->helper->route('reputation_user_rating_controller', array('uid' => $uid)), 'S_RS_COMMENT_ENABLE' => $this->config['rs_enable_comment'] ? true : false, 'S_IS_AJAX' => $is_ajax, 'U_RS_REFERER' => $referer));
        return $this->helper->render('rateuser.html', $this->user->lang('RS_USER_RATING'));
    }