<commit_before><?php
		
namespace Pair;

class Session extends ActiveRecord {

	/**
	 * Property that binds db field id.
	 * @var string
	 */
	protected $id;

	/**
	 * Property that binds db field id_user.
	 * @var int
	 */
	protected $idUser;

	/**
	 * Property that binds db field start_time.
	 * @var DateTime
	 */
	protected $startTime;

	/**
	 * Property that binds db field timezone_offset.
	 * @var float
	 */
	protected $timezoneOffset;

	/**
	 * Property that binds db field timezone_name.
	 * @var string
	 */
	protected $timezoneName;

	/**
	 * Property that binds db field former_user_id.
	 *
	 * @var int
	 */
	protected $formerUserId;

	/**
	 * Name of related db table.
	 * @var string
	 */
	const TABLE_NAME = 'sessions';
		
	/**
	 * Name of primary key db field.
	 * @var string
	 */
	const TABLE_KEY = 'id';
	
	/**
	 * Method called by constructor just after having populated the object.
	 */
	protected function init() {
			
		$this->bindAsDatetime('startTime');
		
		$this->bindAsFloat('timezoneOffset');

		$this->bindAsInteger('idUser', 'formerUserId');
			
	}

	/**
	 * Returns array with matching object property name on related db fields.
	 *
	 * @return	array
	 */
	protected static function getBinds() {
		
		$varFields = array (
			'id'				=> 'id',
			'idUser'			=> 'id_user',
			'startTime'			=> 'start_time',
			'timezoneOffset'	=> 'timezone_offset',
			'timezoneName'		=> 'timezone_name',
			'formerUserId'		=> 'former_user_id'
		);
		
		return $varFields;
		
	}

	/**
	 * Only one session is allowed, deletes previous for this user.
	 * 
	 * @see ActiveRecord::beforeCreate()
	 */
	public function beforeCreate() {

		// $this->db->exec('DELETE FROM `sessions` WHERE `id_user` = ?', [$this->idUser]);

	}
	
	/**
	 * Extends timeout updating startTime of this session, based on user’s time zone.
	 */
	public function extendTimeout() {

		// converts to current time zone
		$dateTime  = new \DateTime();
		$startTime = Utilities::convertToDbDatetime($dateTime);
		
		$this->db->exec('UPDATE `sessions` SET `start_time` = ? WHERE `id` = ?', array($startTime, $this->id));
		
	}
	
	/**
	 * Deletes expired sessions from database, based on sessionTime param and user’s time zone.
	 *
	 * @param	int		Session time in minutes.
	 */
	public static function cleanOlderThan($sessionTime) {

		$db = Database::getInstance();

		// converts to current time zone
		$dateTime  = new \DateTime();
		$startTime = $dateTime->format('Y-m-d H:i:s');

		$query = 'DELETE FROM `sessions` WHERE `start_time` < DATE_SUB(?, INTERVAL '. (int)$sessionTime .' MINUTE)';
		$db->exec($query, [$startTime]);

	}

	/**
	 * Checks if a Session object is expired after sessionTime passed as parameter.
	 * 
	 * @param	int		Session time in minutes.
	 * 
	 * @return	bool
	 */
	public function isExpired($sessionTime) {
		
		if (is_null($this->startTime)) {
			return TRUE;
		}

		// creates expiring date subtracting sessionTime interval
		$expiring = new \DateTime(NULL, new \DateTimeZone(BASE_TIMEZONE));
		$expiring->sub(new \DateInterval('PT' . (int)$sessionTime . 'M'));
		
		return ($this->startTime < $expiring);

	}
	
	/**
	 * Return the User object of this Session, if exists.
	 * 
	 * @return	User|NULL 
	 */
	public function getUser(): ?User {

		if (!$this->idUser) return NULL;

		$user = new User($this->idUser);

		return ($user->isLoaded() ? $user : NULL);

	}

	public static function getCurrent(): Session
	{
		return new Session(session_id());
	}
	
}<commit_msg>New functions for Session<commit_after><?php
		
namespace Pair;

class Session extends ActiveRecord {

	/**
	 * Property that binds db field id.
	 * @var string
	 */
	protected $id;

	/**
	 * Property that binds db field id_user.
	 * @var int
	 */
	protected $idUser;

	/**
	 * Property that binds db field start_time.
	 * @var DateTime
	 */
	protected $startTime;

	/**
	 * Property that binds db field timezone_offset.
	 * @var float
	 */
	protected $timezoneOffset;

	/**
	 * Property that binds db field timezone_name.
	 * @var string
	 */
	protected $timezoneName;

	/**
	 * Property that binds db field former_user_id.
	 *
	 * @var int
	 */
	protected $formerUserId;

	/**
	 * Name of related db table.
	 * @var string
	 */
	const TABLE_NAME = 'sessions';
		
	/**
	 * Name of primary key db field.
	 * @var string
	 */
	const TABLE_KEY = 'id';
	
	/**
	 * Method called by constructor just after having populated the object.
	 */
	protected function init() {
			
		$this->bindAsDatetime('startTime');
		
		$this->bindAsFloat('timezoneOffset');

		$this->bindAsInteger('idUser', 'formerUserId');
			
	}

	/**
	 * Returns array with matching object property name on related db fields.
	 *
	 * @return	array
	 */
	protected static function getBinds() {
		
		$varFields = array (
			'id'				=> 'id',
			'idUser'			=> 'id_user',
			'startTime'			=> 'start_time',
			'timezoneOffset'	=> 'timezone_offset',
			'timezoneName'		=> 'timezone_name',
			'formerUserId'		=> 'former_user_id'
		);
		
		return $varFields;
		
	}

	/**
	 * Only one session is allowed, deletes previous for this user.
	 * 
	 * @see ActiveRecord::beforeCreate()
	 */
	public function beforeCreate() {

		// $this->db->exec('DELETE FROM `sessions` WHERE `id_user` = ?', [$this->idUser]);

	}
	
	/**
	 * Extends timeout updating startTime of this session, based on user’s time zone.
	 */
	public function extendTimeout() {

		// converts to current time zone
		$dateTime  = new \DateTime();
		$startTime = Utilities::convertToDbDatetime($dateTime);
		
		$this->db->exec('UPDATE `sessions` SET `start_time` = ? WHERE `id` = ?', array($startTime, $this->id));
		
	}
	
	/**
	 * Deletes expired sessions from database, based on sessionTime param and user’s time zone.
	 *
	 * @param	int		Session time in minutes.
	 */
	public static function cleanOlderThan($sessionTime) {

		$db = Database::getInstance();

		// converts to current time zone
		$dateTime  = new \DateTime();
		$startTime = $dateTime->format('Y-m-d H:i:s');

		$query = 'DELETE FROM `sessions` WHERE `start_time` < DATE_SUB(?, INTERVAL '. (int)$sessionTime .' MINUTE)';
		$db->exec($query, [$startTime]);

	}

	/**
	 * Checks if a Session object is expired after sessionTime passed as parameter.
	 * 
	 * @param	int		Session time in minutes.
	 * 
	 * @return	bool
	 */
	public function isExpired($sessionTime) {
		
		if (is_null($this->startTime)) {
			return TRUE;
		}

		// creates expiring date subtracting sessionTime interval
		$expiring = new \DateTime(NULL, new \DateTimeZone(BASE_TIMEZONE));
		$expiring->sub(new \DateInterval('PT' . (int)$sessionTime . 'M'));
		
		return ($this->startTime < $expiring);

	}
	
	/**
	 * Return the User object of this Session, if exists.
	 * 
	 * @return	User|NULL 
	 */
	public function getUser(): ?User {

		if (!$this->idUser) return NULL;

		$user = new User($this->idUser);

		return ($user->isLoaded() ? $user : NULL);

	}

	/**
	 * Returns true if the session has a former user
	 *
	 * @return boolean
	 */
	public function hasFormerUser(): bool
	{
		return !in_array($this->formerUserId,  [null, '']);
	}

	/**
	 * Returns the former user associated to the session, if present
	 *
	 * @return User|null
	 */
	public function getFormerUser(): ?User
	{
		$user = new User($this->formerUserId);
		return $user->isLoaded() ? $user : null;
	}

	/**
	 * Returns the current session object
	 *
	 * @return Session
	 */
	public static function getCurrent(): Session
	{
		return new Session(session_id());
	}
	
}
{'fraction_non_alphanumeric': 0.14366121683194855, 'fraction_numerical': 0.0, 'mean_word_length': 3.1646205357142856, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5347014', 'n_tokens_mistral': 2704, 'n_tokens_neox': 2325, 'n_words': 689}