<?php
/*
*
* -------------------------------------------------------
* CLASSNAME:        login_attempt
* GENERATION DATE:  31.12.2013
* FOR MYSQL TABLE:  login_attempt
* FOR MYSQL DB:     web-school
* -------------------------------------------------------
* CODE GENERATED BY:
* PHP-MYSQL-CLASS GENERATOR
* -------------------------------------------------------
*
*/

include_once("class.database.php");

// **********************
// CLASS DECLARATION
// **********************

class login_attempt
{ // class : begin


// **********************
// ATTRIBUTE DECLARATION
// **********************

var $id;   // KEY ATTR. WITH AUTOINCREMENT

var $userid;   // (normal Attribute)
var $user_key;   // (normal Attribute)
var $date_time;   // (normal Attribute)
var $ip;   // (normal Attribute)

var $database; // Instance of class database


// **********************
// CONSTRUCTOR METHOD
// **********************

function login_attempt()
{

$this->database = new Database();

}


// **********************
// GETTER METHODS
// **********************


function getid()
{
return $this->id;
}

function getuserid()
{
return $this->userid;
}

function getuser_key()
{
return $this->user_key;
}

function getdate_time()
{
return $this->date_time;
}

function getip()
{
return $this->ip;
}

// **********************
// SETTER METHODS
// **********************


function setid($val)
{
$this->id =  $val;
}

function setuserid($val)
{
$this->userid =  $val;
}

function setuser_key($val)
{
$this->user_key =  $val;
}

function setdate_time($val)
{
$this->date_time =  $val;
}

function setip($val)
{
$this->ip =  $val;
}

// **********************
// SELECT METHOD / LOAD
// **********************

function select($id)
{

$sql =  "SELECT * FROM login_attempt WHERE id = $id;";
$result =  $this->database->query($sql);
$result = $this->database->result;
$row = mysql_fetch_object($result);


$this->id = $row->id;

$this->userid = $row->userid;

$this->user_key = $row->user_key;

$this->date_time = $row->date_time;

$this->ip = $row->ip;

}





// **********************
// DELETE
// **********************

function delete($id)
{
$sql = "DELETE FROM login_attempt WHERE id = $id;";
$result = $this->database->query($sql);

}

// **********************
// INSERT
// **********************

function insert()
{
$this->id = ""; // clear key for autoincrement

$sql = "INSERT INTO login_attempt ( userid,user_key,date_time,ip ) VALUES ( '$this->userid','$this->user_key','$this->date_time','$this->ip' )";
$result = $this->database->query($sql);
$this->id = mysql_insert_id($this->database->link);

}

// **********************
// UPDATE
// **********************

function update($id)
{



$sql = " UPDATE login_attempt SET  userid = '$this->userid',user_key = '$this->user_key',date_time = '$this->date_time',ip = '$this->ip' WHERE id = $id ";

$result = $this->database->query($sql);



}
// **********************
// SELECT ALL
// **********************
function selectall($cond='',$limit='',$order)
{
	$sql =  "SELECT * FROM login_attempt ";
	if($cond!='')
		$sql .= " WHERE $cond ";
	
	if($order!='')
		$sql .= " ORDER BY $order ";
		
	if($limit!='')
		$sql .= " LIMIT $limit ";
	
	$sql .=";";
	
	$result =  $this->database->query($sql);
	$result = $this->database->result;
	$this->rows=array();
	while($row=mysql_fetch_object($result))
	{
	$this->rows[] = $row;
	}
	return $this->rows;
}

// **********************
// CUSTOM METHOD / LOAD
// **********************

/*Check attempts*/
function check_attempts($uid,$currentlogin,$previousattempt)
{
	$sql =  "SELECT count(*) total_attempt FROM login_attempt WHERE  ((date_time <= '$currentlogin' AND date_time >= '$previousattempt') AND userid = $uid)  AND user_key = -1 ";
	$result =  $this->database->query($sql);
	$result = $this->database->result;
	$row= mysql_fetch_object($result);
	return $row->total_attempt;
}
/*Check attempts*/

} // class : end

?>