<?php
/*
+--------------------------------------------------------------------------
|   TanyaCMS - Question and Answer Sofware
|   -----------------------------------------------------------------------
|   by fbschool dev team
|   (c) 2011 - 2012 FBSCHOOL
|   http://www.fbschool.com/tanyacms
|   -----------------------------------------------------------------------
|   This is NOT a freeware, use is subject to license terms
|   Support: 13455n@gmail.com
+--------------------------------------------------------------------------
*/


if (!defined('IN_TANYACMS'))
{
	die;
}

class cronlog_class extends ASK_MODEL
{
	public function start()
	{
		$corn_log_file = TEMP_PATH . 'corn_log.php';
	
		if (file_exists($corn_log_file))
		{
			$corn_log = unserialize(file_get_contents($corn_log_file));
		}
		
		if ($corn_log['second'] < time() - 1)
		{
			$call_actions[] = 'second';
			
			$corn_log['second'] = time();
		}
		
		if ($corn_log['half_minute'] < time() - 30)
		{
			$call_actions[] = 'half_minute';
			
			$corn_log['half_minute'] = time();
		}
		
		if ($corn_log['minute'] < time() - 60)
		{
			$call_actions[] = 'minute';
			
			$corn_log['minute'] = time();
		}
		
		if (date('YW', $corn_log['week']) != date('YW', time()))
		{
			$call_actions[] = 'week';
			
			$corn_log['week'] = time();
		}
		else if (date('Y-m-d', $corn_log['day']) != date('Y-m-d', time()))
		{
			$call_actions[] = 'day';
			
			$corn_log['day'] = time();
		}
		else if ($corn_log['hour'] < time() - 3600)
		{
			$call_actions[] = 'hour';
			
			$corn_log['hour'] = time();
		}
		else if ($corn_log['half_hour'] < time() - 1800)
		{
			$call_actions[] = 'half_hour';
			
			$corn_log['half_hour'] = time();
		}
		
		if ($call_actions)
		{
			file_put_contents($corn_log_file, serialize($corn_log));
		}
		
		return $call_actions;
	}
	
	// Performed per second
	public function second($user_id)
	{
		
	}
	
	// Every half a minute execution
	public function half_minute($user_id)
	{
		$this->model('online')->online_active($user_id);
		$this->model('edm')->run_task();
	}
	
	// Every minute
	public function minute($user_id)
	{
		@unlink(TEMP_PATH . 'plugins_table.php');
		@unlink(TEMP_PATH . 'plugins_model.php');
		
		$this->model('reputation')->calculate_by_uid($user_id);
	}
	
	// Performed every half hour
	public function half_hour($user_id)
	{
		
	}
	
	// Hourly
	public function hour($user_id)
	{
		$this->model('cache')->clean_expire();
	}
	
	// Daily executed
	public function day($user_id)
	{
		$this->model('answer')->calc_best_answer();
		$this->model('question')->auto_lock_question();
	}
	
	// Weekly
	public function week($user_id)
	{
		$this->model('system')->clean_break_attach();
	}
}