<?php 

class mysteryCalculator
{
	
	protected $db;
	protected $QuestionScores;
	protected $projectName;
	
	
	public function __construct($db,$projectName)
	{
		$this->db = $db;
		$this->projectName = $projectName;
	}

	
	
	/**
	 * With this function we load all scores on question in defined project
	 * 	
	 */
	
	public function LoadQuestionScores() {
		
		$sql = " SELECT a .* , b.q_name, b.tip_sql
					FROM ".DB_WARPIT_WEBCATI.".".$this->projectName."_mystery`AS a
					LEFT JOIN ".DB_WARPIT_WEBCATI.".".$this->projectName." AS b ON a.id_question = b.id ";
		
		$res = $db->SQLexecute($sql);
		while ($zad = $db->fetchAssoc($res) )
		{
			$variableInDatabase = 0;
			
			if ($zad['tip_sql'] == 1) {
				
				$variableInDatabase = $zad['q_name'];
				$this->QuestionScores[$variableInDatabase][$zad['id_answer']] = $zad['sc_value'];
			
			} elseif ($zad['tip_sql'] == 3) {
				
				$variableInDatabase = $zad['q_name']."_".$zad['id_answer'];			
				$this->QuestionScores[$variableInDatabase][$zad['id_answer']] = $zad['sc_value'];
				
			}
			
			if ($variableInDatabase) $this->databaseVariables[] = $variableInDatabase;
			
		}
	
	}

	/**
	 * Enter description here...
	 *
	 * @param record array $rec
	 */
	public function CalculateRecord($rec) {
		
		unset($this->QuestionScores);
		
		foreach ($rec as $key=>$val) {
			
			$ScoreVal = $this->QuestionScores[$key][$val];
			
			if ( $ScoreVal > 0 ) {
				
				$this->calculatedScores['pos'] += $ScoreVal;
				
			} elseif ($ScoreVal < 0) {
				
				$this->calculatedScores['neg'] += $ScoreVal;
				
			}
		}
		
	}
	
	
}
	
?>