<?php

class CriteriumValue extends AppModel {
	
	var $useTable = 'criterium_value';
	var $id = 'id';
	var $primaryKey = 'id';
	var $order = 'ORDER BY time DESC';
	
	const GOOGLE_PAGERANK = 1;
	const ALEXA_RANK = 2;
	const W3C_VALIDITY = 3;
	
	var $belongsTo = array(
		'Site'=>array(
			'className' => 'Site',
            'foreignKey' => 'site'
		),
		'Criterium'=>array(
			'className' => 'Criterium',
            'foreignKey' => 'criterium'
		),
	);
	
	function afterFind($results)
	{
		/*$fp = FirePHP::getInstance(true);
		$fp->log($results, 'results');*/
		
		foreach ($results as $key => &$criteriumValue) 
		{
			if (isset($criteriumValue['CriteriumValue']['criterium'])) {
				$criteriumValue['CriteriumValue'] = $this->modifyValue($criteriumValue['CriteriumValue']);
			}
			else 
			{
				if (is_array($criteriumValue))
				{
					foreach ($criteriumValue as $value)
					{
						if (isset($value['criterium'])) {
							$value = $this->modifyValue($value);
						}
					}
				}
			}
		}
		
		
		return $results;
	}
	
	function modifyValue($criteriumValue)
	{
		switch ($criteriumValue['criterium'])
		{
			case self::ALEXA_RANK : 
				$criteriumValue['value'] = ceil(10 - log10($criteriumValue['value']));
				break;
			case self::W3C_VALIDITY : 
				$criteriumValue['value'] = ceil(10 - log($criteriumValue['value']));
				break;
		}
		return $criteriumValue;
	}
	
	
}

?>