<?php

class Thememakers_Entity_Rating {

    public function register() {
        
    }

    public static function set_rate() {
        $post_id = $_REQUEST['post_id'];

        if (self::is_user_can_rate($post_id)) {
            $score = $_REQUEST['score'];
            $rated_users = self::get_rated_users($post_id);
            $user_id = get_current_user_id();
            $user_ip = ThemeMakersHelper::get_user_ip();
            $user_data = array();
            $user_data['user_id'] = $user_id;
            $user_data['user_ip'] = $user_ip;
            $user_data['score'] = $score;
            $rated_users[$user_ip] = $user_data;
            //*****
            $rating = 0;
            $total_user_count = count($rated_users);
            if (!empty($rated_users)) {
                foreach ($rated_users as $value) {
                    $rating+=@$value['score'];
                }
            }

            $rating = round($rating / $total_user_count, 2);

            //*****
            update_post_meta($post_id, "post_rating", $rating);
            update_post_meta($post_id, "post_rated_users", $rated_users);
            //*****

            echo $rating;
        }
        exit;
    }

    public static function get_rating($post_id) {
        return get_post_meta($post_id, "post_rating", true);
    }

    public static function get_users_rating($count = 5) {
        $users_rating = ThemeMakersThemeModel::get_option("users_rating"); //user_id=>score
        $users_rating_last_update = ThemeMakersThemeModel::get_option("users_rating_last_update");
        if (empty($users_rating)) {
            $users_rating = array();
        }
        //*****
        $update_period = 15 * 60; //sec!!!!!!!!!!!!!!!!!!!!!!!!
        $current_time = time();
		
				
       // if ($current_time <= ($users_rating_last_update - $update_period) OR !$users_rating_last_update) {
            $users_rating = array();
            //***
					
            $args = array(
                'numberposts' => -1,
                'post_type' => 'review',
                'post_status' => 'publish');
			
            $posts_array = get_posts($args);

            $posts_rating_tmp = array();
            $posts_rating_tmp_counter = array();

            if (!empty($posts_array)) {
                foreach ($posts_array as $post) {
                    if (!isset($posts_rating_tmp[$post->post_author])) {
                        $posts_rating_tmp[$post->post_author] = 0;
                        $posts_rating_tmp_counter[$post->post_author] = 0;
                    }
                    $rating = self::get_rating($post->ID);
                    $posts_rating_tmp[$post->post_author]+=$rating;
                    $posts_rating_tmp_counter[$post->post_author]+=1;
					
					
                }

                //*****
                if (!empty($posts_rating_tmp)) {
                    foreach ($posts_rating_tmp as $author_id => $score) {
                        $score = round($score / $posts_rating_tmp_counter[$author_id], 2);
                        $users_rating[$author_id] = $score;
						
					
                    }
                }
            }
			
			            
            ThemeMakersThemeModel::update_option('users_rating', $users_rating);
            ThemeMakersThemeModel::update_option('users_rating_last_update', $current_time);
       // }
				
		?>
		
		<?php
			
        return $users_rating;
		
		
    }

    public static function get_rated_users($post_id) {
        $rated_users = get_post_meta($post_id, "post_rated_users", true);
        if (isset($rated_users[0])) {
            unset($rated_users[0]);
        }
        return $rated_users;
    }

    public static function get_max_rating_number() {
        $max_rating = ThemeMakersThemeModel::get_option("max_rating_number");
        if (!$max_rating) {
            $max_rating = 5;
        }

        return $max_rating;
    }

    public static function draw_rating($post_id) {
        $disable_rates = ThemeMakersThemeModel::get_option("disable_rates");
        if ($disable_rates) {
            return;
        }

        $data = array();
        $data['rating'] = self::get_rating($post_id);
        $data['max_rating'] = self::get_max_rating_number();
        $data['post_id'] = $post_id;
        $data['user_can_rate'] = self::is_user_can_rate($post_id);
        return ThemeMakersThemeView::draw_html('rating/draw_rating', $data);
    }

    public static function draw_rating2($post_id) {
        $disable_rates = ThemeMakersThemeModel::get_option("disable_rates");
        if ($disable_rates) {
            return;
        }
        $data = array();
        $data['rating'] = self::get_rating($post_id);
        return ThemeMakersThemeView::draw_html('rating/draw_rating2', $data);
    }

    //***********************

    public static function is_user_can_rate($post_id) {
        $user_id = get_current_user_id();
        $user_ip = ThemeMakersHelper::get_user_ip();
        $is_only_register_user_can_votes = self::is_only_register_user_can_votes();
        //*****
        if ($is_only_register_user_can_votes AND !$user_id) {
            return false;
        }

        $rated_users = self::get_rated_users($post_id);

        if (isset($rated_users[$user_ip])) {
            return false;
        }

        if (!$is_only_register_user_can_votes AND $user_id) {
            //is user with id rated already
            if (!empty($rated_users)) {
                foreach ($rated_users as $user) {
                    if ($user_id == $user['user_id']) {
                        return false;
                    }
                }
            }
        }


        return true;
    }

    public static function is_only_register_user_can_votes() {
        return ThemeMakersThemeModel::get_option("only_register_user_can_votes");
    }

}

