<?php

/**
 * @class history
 * @brief Manage, Display history of activity
 * @author Vincent Lemoine
 * @date 29/04/2009
 * @modified Xavier Perrissoud
 * @date 01/05/2009
 * @version 0.1
 *
 * This class manages the history of the differents actions made on a hosting and/or an user account
*/
class history {

	private $history_records = null;
	
	/**
	 * @brief Save current action to history
	 * @param message_key -> message of action
	 * @param user_id -> the user id of action
	 * @return boolean
	 *
	 * Add an action record in the history list for a given user.
	*/
	public function add($message_key, $user_id) 
	{
	
		$message_key = $_SESSION['database']->clearString($message_key);
		$user_id = $_SESSION['database']->clearString($user_id);
		
		// hosting_id ŕ récuperer
		$hosting_id = 1;
		$ip = $_SERVER['REMOTE_ADDR'];

		$req = "INSERT INTO history
			SET 
				date = NOW(),
				user_id = '$user_id',
				hosting_id = '$hosting_id',
				ip = '$ip',
				message_key = '$message_key'";
		$_SESSION['database']->execRequest($req);
		// Update cache if needed
		if ( !is_null($this->history_records) ) {
			$this->history_records++;
		}
		return true;
			
	} // End of actiontoHistory
	

	/**
	 * @brief List all history actions with a given clause
	 * @param start -> Record of starting listing
	 * @param extract -> Number of records to extract
     * @param clause -> SQL clause for listing
	 * @return array with information (nul if empty)
	*/
	private function listHistoryByClause($start = NULL, $extract = NULL, $clause = NULL) 
	{
	
		$lang = new lang();	

		if ( !is_null($start) and !is_null($extract) ) {
			$limit = "LIMIT $start, $extract";
		} else $limit = NULL;
	
		if ( !is_null($clause) ) {
			$clause = "WHERE $clause";
		}
		
		$req = "SELECT 
				h.message_key AS action,
				i.base_name AS hosting,
				h.ip AS ip,
				UNIX_TIMESTAMP(h.date) AS date
			FROM history AS h
			LEFT JOIN hostings AS i
			ON i.id = h.hosting_id 
			$clause $limit";
		$result = $_SESSION['database']->fetchObject($req);

		foreach ( $result as $key => $value ) 
		{
			$result[$key]->action = $lang->getWordFromLangFile($result[$key]->action);
		}
		return $result;
	} // End of listHistoryByClause
	
	
	/**
	 * @brief List all history actions for the current user
	 * @param start -> Record of starting listing
	 * @param extract -> Number of records to extract
	 * @return array with information (nul if empty)
	*/
	public function userListHistory($start = NULL, $extract = NULL) 
	{
	
		$lang = new lang();

		if ( !is_null($start) and !is_null($extract) ) {
			$limit = " LIMIT $start, $extract";
		} else $limit = NULL;
		
		$user_id = $_SESSION['user']->information_user->userid;
		
		$result = $this->listHistoryByClause($start,$extract, "h.user_id='$user_id'");
		
		$time = $lang->userGetTimeFormat();
		$date = $lang->userGetDateFormat();

		foreach ( $result as $key => $value ) 
		{
			$result[$key]->date = date("$date $time" , $result[$key]->date);
		}
		return $result;
	} // End of userListHistory
	
	/**
	 * @brief Get number of all history records for the current user
	 * @param None
	 * @return number of history records registered for the user
	 *
	 * Get the total of all actions recorded in the history list for the current user.<br />
	 * This value is got the first time from the database (generating a SQL request) and is then stocked in a cache variable, so, other calls to this method won't generate another SQL request.
	*/
	public function userCountHistoryRecords() 
	{
		// Try to get the value from the cache
		if( !is_null($this->history_records) ) {
			return $this->history_records;
		}
		
		$user_id = $_SESSION['user']->information_user->userid;
		$req = "SELECT COUNT(id) AS total FROM history WHERE user_id='$user_id'";
		$result = $_SESSION['database']->fetchObject($req);
		$this->history_records = $result[0]->total;
		
		return $this->history_records;
	}
	
	/**
	 * @brief Get number of history records pages, regarding the total count of records and the count of items to be shown per page
	 * @param None
	 * @return number of pages availables
	 *
	 * Calculate the total pages needed to show all the history actions records for the current user.
	*/
	public function userCountTotalPages() 
	{
		$items_count = $this->userCountHistoryRecords();
		$pages_count = intval($items_count / RECORD_BY_PAGE);
		if ( ($items_count % RECORD_BY_PAGE) != 0 ) {
			$pages_count++;
		}
		return $pages_count;
	}

} // End of class
