<?php
class Statistic extends PhonalisaRecord
{
		
	const StatisticTargetTypeQueue = 0;	
	const StatisticTargetTypeGroup = 1;	
	const StatisticTargetTypeGroupOutbound = 2;	
	const StatisticTargetTypeGroupUser = 3;	
	const StatisticTargetTypeAgentInbound = 4;	
	const StatisticTargetTypeAgentOutbound = 5;
	const StatisticTargetTypeSystemOverview = 6;
	
	const StatisticTimeRangeTypeDay = 0;
	const StatisticTimeRangeTypeWeek = 1;
	const StatisticTimeRangeTypeMonth = 2;
	const StatisticTimeRangeTypeYear = 3;
	
	
	const TimeRangeType = 0;
	const Triggered = 1;
	const Running = 2;
	const Finished = 3;
	const NeedToTriggerAgain = 4;
	const Failed = 5;
	
	public static function readable($type,$returnTranslationTag = FALSE ) {
	    switch ($type) {
	    
	      case self::StatisticTimeRangeTypeDay :
		return $returnTranslationTag ? 'STATISTIC_RANGE_TYPE_DAY' :__('STATISTIC_RANGE_TYPE_DAY');
	      break;
	      case self::StatisticTimeRangeTypeWeek :
		return $returnTranslationTag ? 'STATISTIC_RANGE_TYPE_WEEK' :__('STATISTIC_RANGE_TYPE_WEEK');
	      break;
	      case self::StatisticTimeRangeTypeMonth :
		return $returnTranslationTag ? 'STATISTIC_RANGE_TYPE_MONTH' :__('STATISTIC_RANGE_TYPE_MONTH');
	      break;
	      case self::StatisticTimeRangeTypeYear :
		return $returnTranslationTag ? 'STATISTIC_RANGE_TYPE_YEAR' :__('STATISTIC_RANGE_TYPE_YEAR');
	      break;
	      default : 
		return 'not defined';
	    
	    }
	
	}
	
	public $time_range_type;
	public $target_type;
	public $target_id;
	public $year;
	public $month;
	public $week;
	public $day;
	
	public $counter_success;
	public $counter_missed;
	public $counter_missed_timeout;
	public $counter_missed_no_agents;
	
	public static function myAttributes() {
		return array(
				'time_range_type'=>'time_range_type',
				'target_type'=>'target_type',
				'target_id'=>'target_id',
				'year'=>'year',
				'month'=>'month',
				'week'=>'week',
				'day' => 'day',
				'counter_missed' => 'counter_missed',
				'counter_missed_timeout' => 'counter_missed_timeout',
				'counter_missed_no_agents' => 'counter_missed_no_agents',
				'counter_success' => 'counter_success'
		);
	}
	
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Job the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
	
	public function getAllMissedCalls() {
		return (int) $this->counter_missed + (int) $this->counter_missed_no_agents + (int) $this->counter_missed_timeout;
	}
	
	/**
	 * @param string $hour 0-23 set this for getting all missed calls per hour (including timeout and no agent counters)
	 */
	public function getAllMissedCallsForHour($hour) {
		$counter = 0;
		$counterKeys = array(); // all keys containing counters for missed calls
		$counterKeys[] = "counter_missed_".$hour;
		$counterKeys[] = "counter_missed_timeout_".$hour;
		$counterKeys[] = "counter_missed_no_agents_".$hour;
		
		foreach ($counterKeys as $counterKey) {
			for ($i=0; $i<4; $i++) { // loop quarter hours
				$counter += (int)$this->valueForKey($counterKey."_".$i);
			}
		}
		
		return $counter;
	}
	
	/**
	 * @param string $hour 0-23 set this for getting all success calls per hour
	 */
	public function getAllSuccessCallsForHour($hour) {
		$counter = 0;
		$counterKeys = array(); // all keys containing counters for success calls, currently only one.
		$counterKeys[] = "counter_success_".$hour;
		
		foreach ($counterKeys as $counterKey) {
			for ($i=0; $i<4; $i++) { // loop quarter hours
				$counter += (int)$this->valueForKey($counterKey."_".$i);
			}
		}
		return $counter;
	}
	
	
	
}