<?php
class Xoriant_Ratings {
private $hostname_conn;
private $database_conn;
private $username_conn;
private $password_conn;
private $conn;
private static $instance;
public function __construct() {
if(self::$instance) {
return self::$instance;
} else {
self::$instance = $this;
$this->hostname_conn = "localhost";
$this->database_conn = "classes";
$this->username_conn = "user";
$this->password_conn = "password";
$this->conn = mysql_connect($this->hostname_conn, $this->username_conn, $this->password_conn);
if(!$this->conn) throw new Exception(mysql_error()." on line number ".__LINE__." of file ".__FILE__);
mysql_select_db($this->database_conn, $this->conn);
}
}
public function display_table_fields($table_name) {
$rs = @mysql_query("select * from ".$table_name." LIMIT 1");
if(!$rs) {
throw new Exception(mysql_error()." on line number ".__LINE__." of file ".__FILE__);
}
$i = 0;
$register_main_arr = array();
while ($i < mysql_num_fields($rs)) {
$meta = mysql_fetch_field($rs, $i);
$register_arr[] = $meta->name;
$i++;
}
return $register_arr;
}
public function phpinsert($table_name,$pk,$postarray) {
$register_arr = array();
$register_arr = $this->display_table_fields($table_name);
$query = "insert into ".$table_name." set ";
foreach($postarray as $key=>$value) {
if(gettype($value)=="array") {
$string = '';
foreach($value as $val) {
if(strlen($val)>0) {
$val = $this->processString($val);
$string .= $val.'|';
}
}
$string = substr($string,0,-1);
if(in_array($key,$register_arr)) {
$query .= $key."='".$string."',";
}
} else {
if(in_array($key,$register_arr)) {
$value = $this->processString($value);
$query .= $key."='".$value."',";
}
}
}
$query = substr($query,0,-1);
$result = @mysql_query($query);
if(!$result) {
throw new Exception(mysql_error()." on line number ".__LINE__." of file ".__FILE__);
}
$uid = mysql_insert_id();
return $uid;
}
public function phpedit($table_name,$pk,$postarray,$uid) {
$register_arr = array();
$register_arr = $this->display_table_fields($table_name);
$query = "update ".$table_name." set ";
foreach($postarray as $key=>$value) {
if(gettype($value)=="array") {
$string = '';
foreach($value as $val) {
if(strlen($val)>0) {
$val = $this->processString($val);
$string .= $val.'|';
}
}
$string = substr($string,0,-1);
if(in_array($key,$register_arr)) {
$query .= $key."='".$string."',";
}
} else {
if(in_array($key,$register_arr)) {
$value = $this->processString($value);
$query .= $key."='".$value."',";
}
}
}
$query = substr($query,0,-1);
$query .= " where ".$pk." = '".$uid."'";
$result = @mysql_query($query);
if(!$result) {
throw new Exception(mysql_error()." on line number ".__LINE__." of file ".__FILE__);
}
return $uid;
}
private function processString($text) {
return addslashes(stripslashes(trim($text)));
}
public function validateData($record) {
if(!isset($record['rating'])) {
throw new Exception("Rating is empty. ");
}
return true;
}
public function checkRatingUniqueIp($record) {
$sql = "SELECT count(*) as cnt FROM xoriant_ratings WHERE item_id = '".$this->processString($record['item_id'])."' and rating_ip = '".$this->processString($record['rating_ip'])."'";
$rs = @mysql_query($sql);
if(!$rs) {
throw new Exception(mysql_error()." on line number ".__LINE__." of file ".__FILE__);
}
$rec = mysql_fetch_array($rs);
$count = $rec['cnt'];
return $count;
}
public function checkRatingUniqueUser($record) {
$sql = "SELECT count(*) as cnt FROM xoriant_ratings WHERE item_id = '".$this->processString($record['item_id'])."' and user_id = '".$this->processString($record['user_id'])."'";
$rs = @mysql_query($sql);
if(!$rs) {
throw new Exception(mysql_error()." on line number ".__LINE__." of file ".__FILE__);
}
$rec = mysql_fetch_array($rs);
$count = $rec['cnt'];
return $count;
}
public function showRating($item_id) {
$sql = "SELECT item_id, AVG(rating) as avg_rating, COUNT(rating) as counter FROM xoriant_ratings WHERE item_id = '".$item_id."' GROUP BY item_id";
$rs = @mysql_query($sql);
if(!$rs) {
throw new Exception(mysql_error()." on line number ".__LINE__." of file ".__FILE__);
}
$arr = mysql_fetch_array($rs);
return $arr;
}
public function showAllRating() {
$sql = "SELECT item_id, AVG(rating) as avg_rating, COUNT(rating) as counter FROM xoriant_ratings GROUP BY item_id";
$rs = @mysql_query($sql);
if(!$rs) {
throw new Exception(mysql_error()." on line number ".__LINE__." of file ".__FILE__);
}
while($arr = mysql_fetch_array($rs)) {
$return[$arr['item_id']] = $arr;
}
return $return;
}
}
?>