<?PHP
	// definitions for Word_Test_Stat class: word test statistics
class Word_Test_Stat {
	var $id;
	var $user_id;
	var $meaning_id;
	var $correct_answers;
	var $date_added;
	var $date_updated;
	var $status;

	// constructor for Word_Test_Stat 
	function __construct ($user_id, $meaning_id) {
		$this->id = "";
		$this->user_id = $user_id;
		$this->meaning_id = $meaning_id;
		$this->correct_answers = 0;
		$this->date_added = "";
		$this->date_updated = "";
		$this->status = "";
	}

	// use database row to set object's values
	function setValues ($row) {
		$this->id = $row['id'];
		$this->user_id = $row['user_id'];
		$this->meaning_id = $row['meaning_id'];
		$this->correct_answers = $row['correct_answers'];
		$this->date_added = $row['date_added'];
		$this->date_updated = $row['date_updated'];
		$this->status = "read from db";
	}

	// create json string (allows this object to be created in JavaScript)
	function toJson () {
		$json = '{"type":"Word_Test_Stat"';
		$json .= ',"id":"' . $this->id . '"';
		$json .= ',"user_id":"' . $this->user_id . '"';
		$json .= ',"meaning_id":"' . $this->meaning_id . '"';
		$json .= ',"correct_answers":"' . $this->correct_answers . '"';
		$json .= ',"date_added":"' . $this->date_added . '"';
		$json .= ',"date_updated":"' . $this->date_updated . '"';
		$json .= ',"status":"' . $this->status . '"';
		$json .= '}';
		return $json;
	}

	// uses id to retrieve from db
	function getById ($db_link) {

		$db_sql = "SELECT * FROM word_test_stats WHERE id = " . $this->id;
		$result = $db_link->connection->query($db_sql);
		if ( $result ) {
			$row = $result->fetch_array();
			if ($row) {
				$this->setValues($row);
				
			} else {
				$this->status = "Row not found on word_test_stats table with key: id=" . $this->id;
			}
		} else {
			$this->status = "MySql error: " . $db_link->connection->error;
		}
	}

	// store object on database, optionally refresh object from database (caters for database generated values)
	function store ($db_link, $refresh = true) {

		// check minimum info supplied
		if (empty($this->user_id) || empty($this->meaning_id)) {
			$this->status = "Insufficient key information supplied: user_id and meaning_id required";
			return; // skip out
		}
		$db_sql = "SELECT * FROM word_test_stats WHERE user_id = " . $this->user_id . " and meaning_id = " . $this->meaning_id;
        if($result = $db_link->connection->query($db_sql)){
            if($result->num_rows > 0){
     			$row = $result->fetch_array();
				//$this->is_on_db = true;
				$this->id = $row['id'];
				$this->correct_answers = $row['correct_answers'];
                // Free result set
                $result->free();
        		// timestamp
    			$setClause = "date_updated = NOW()";
    			// increase correct answers
    			$this->correct_answers = $this->correct_answers + 1;
    			$setClause .= ", correct_answers = " . $this->correct_answers;

     			$db_sql = "UPDATE word_test_stats SET " . $setClause . " WHERE id=" . $this->id;
                if($result = $db_link->connection->query($db_sql)){
    				$this->status = "Updated OK!";
    				$this->getById($db_link); //refresh values not updated from interface
    			} else {
    				$this->status = "Update error: " . $db_sql . $db_link->connection->error;
    			}

            } else{
        		$columns = "user_id";
        		$values = $this->user_id;
        
        		$columns .= ", meaning_id";
        		$values .= ", " . $this->meaning_id;
        
        		$columns .= ", correct_answers";
        		$values .= ", 1";
        
        		$db_sql = "INSERT INTO word_test_stats (" . $columns . ") VALUES(" . $values . ")";
        
                if($result = $db_link->connection->query($db_sql)){
        			$this->id = $db_link->connection->insert_id;
        			// reread from db to get all values (some can be populated on INSERT)
        			$this->getById($db_link);
        			$this->status = "New word test stat added!";

        		} else {
        			$this->status = "Insert error: " . $db_sql . ", Db error: " . $db_link->connection->error;
        	    }
            }
        }
	}
}

?>
