<?php
/**
 * Post Rating Funxtions & Features.
 *
 * @package UrduPaper
 */

//Create Custom Table For Storing Rating Data
function create_rating_table(){
	global $wpdb;
	$table_name = $wpdb->prefix . "ratings";
	$charset_collate = $wpdb->get_charset_collate();

	$sql = "CREATE TABLE IF NOT EXISTS $table_name (
	  id mediumint(9) NOT NULL AUTO_INCREMENT,
	  user_ip varchar(255) NOT NULL,
	  post_id varchar(255) NOT NULL,
	  rating_value varchar(255) DEFAULT '' NOT NULL,
	  time timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
	  UNIQUE KEY id (id)
	) $charset_collate;";

	require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
	dbDelta( $sql );
}
add_action("after_switch_theme", "create_rating_table");

function get_votes_count($postID,$ratVal) {
	global $wpdb;
	$id = $postID;
	$rat = $ratVal;

	$count = $wpdb->get_var( "SELECT COUNT(*) FROM `{$wpdb->prefix}ratings` WHERE post_id = '".$id."' AND rating_value = '".$rat."' " );
	return $count;
}