<?php
require_once dirname(__FILE__) . '/database.class.php';

class Rating extends Links_Database
{
	var $ratingid        = NULL;
	var $lid             = NULL;
	var $ratinguser      = NULL;
	var $rating          = NULL;
	var $ratinghostname  = NULL;
	var $ratingtimestamp = NULL;

	var $_primary_key = NULL;
	var $_class_name  = NULL;
	var $_rules       = NULL;

	/**
	 * Constructor
	 **/
	function __construct($mydirname)
	{
		$this->_class_name  = strtolower(get_class($this));
		$this->_primary_key = 'ratingid';
		$this->_set_field_rules();

		parent::__construct($mydirname);
	}


	function insert($lid)
	{
		//Clear
	//	unset($_POST['$rating']);// TODO ?
		//SET data
		$this->_get_POST_data();
		$this->lid             = $lid;
		$this->ratinghostname  = getenv("REMOTE_ADDR");
		$this->ratingtimestamp = time();
		$this->ratinguser      = $this->_Links->uid;
		if ($this->_Links->module_config['rating_type'] == 2) {
			$this->rating = 10;
		}

		if ($this->_save()) {
			return TRUE;
		} else {
			return FALSE;
		}
	}

	function _set_field_rules()
	{
		$rules = array();
		$rules['ratingid']        = 'int';
		$rules['lid']             = 'int';
		$rules['ratinguser']      = 'int';
		$rules['rating']          = 'int|_in_rating_rule';
		$rules['ratinghostname']  = 'varchar';
		$rules['ratingtimestamp'] = 'int';
		$this->_rules = $rules;
	}

	function _in_rating_rule($data)
	{
		if ($data === '--') {
			$this->_Links->links_redirect_header($this->_Links->_make_url('rating', 'form', array('lid' => $this->lid)), 3, _MD_NORATING);
			exit;
		}
		if ($data < 0 AND $data > 10) {
			$this->_Links->links_redirect_header($this->_Links->_make_url('rating', 'form', array('lid' => $this->lid)), 3, _MD_DATA_WRONG);
			exit;
		}

		return $data;
	}

/*
CREATE TABLE rating (
  ratingid int(11) unsigned NOT NULL auto_increment,
  lid int(11) unsigned NOT NULL default '0',
  ratinguser int(11) unsigned NOT NULL default '0',
  rating tinyint(3) unsigned NOT NULL default '0',
  ratinghostname varchar(60) NOT NULL default '',
  ratingtimestamp int(10) NOT NULL default '0',
  PRIMARY KEY  (ratingid),
  KEY ratinguser (ratinguser),
  KEY ratinghostname (ratinghostname)
) TYPE=MyISAM;
*/


}
