<?php

/* Qcm Class */

class qcm
{
	public $oDataBase;
	
	//Qcm
	public $iQcmId;
	public $sQcmName;
	private $sAuthor;
	private $sLastModifiedOn;
	private $bValid;
	
	//Questions
	private $asQuestions; //all
	private $asQuestionTitle; // id_question => question
	private $asImages;
	
	//Answers
	private $asAnswerTitle; // id_answer => answer
	private $asRightAnswers;
	private $asAnswerQuestion; // id_question =>> id_answers
	
	//Results
	private $asResults; // id_user => mark
	private $asDates; // id_user => led
	
	public function __construct($oDataBase, $iQcmId=false)
	{
		$this->oDataBase = $oDataBase;
		
		if($iQcmId!==false)
		{
			//Qcm
			$test = array_values(selectRow(QCM_TABLE, array($iQcmId), false, true));
			list($this->iQcmId, $this->sAuthor, $this->sQcmName, $this->bValid, $this->sLastModifiedOn) = $test;
			
			//Questions
			$asQuestions = selectRows(array('from'=>QUESTION_TABLE, 'constraint'=>array('id_'.QCM_TABLE=>$this->iQcmId)));
			pre($asQuestions, '', true);
			foreach($asQuestions as $asRow)
			{
				$this->asQuestionTitle[$asRow['id_'.QUESTION_TABLE]] = $asRow[QUESTION_TABLE];
				$this->asImages[$asRow['id_'.QUESTION_TABLE]] = $asRow['image'];
				$this->asQuestions[$asRow['id_'.QUESTION_TABLE]] = array(QUESTION_TABLE=>$asRow[QUESTION_TABLE], 'image'=>$asRow['image']);
			}
			
			//Answers
			$asAnswers = selectRows(array('from'=>ANSWER_TABLE, 'constraint'=>array('id_'.QCM_TABLE=>$this->iQcmId)));
			foreach($asAnswers as $asRow)
			{
				$this->asAnswerTitle[$asRow['id_'.ANSWER_TABLE]] = $asRow[ANSWER_TABLE];
				$this->asRightAnswers[$asRow['id_'.ANSWER_TABLE]] = $asRow[RIGHT_ANSWER];
				$this->asAnswerQuestion[$asRow['id_'.ANSWER_TABLE]] = $asRow['id_'.QUESTION_TABLE];
				$this->asAnswers[$asRow['id_'.ANSWER_TABLE]] = array(); //TODO
			}
			
			//Results
			$asResults = selectRows(array('from'=>RESULT_TABLE, 'constraint'=>array('id_'.QCM_TABLE=>$this->iQcmId)));
			foreach($asResults as $asRow)
			{
				$this->asResults[$asRow['id_'.USER_TABLE]] = $asRow[RESULT_TABLE];
				$this->asDates[$asRow['id_'.USER_TABLE]] = $asRow['led'];
			}
		}
	}
	
	function getQcm()
	{
		
	}
	
	function getQuestions()
	{
		
	}
	
	function getAnswers($iQuestionId)
	{
		
	}
	
	function cloneQcm()
	{
		$oClonedQcm = new qcm($this->oDataBase);
		
		return $oClonedQcm;
	}
}