<?php
#  Author       :  Vincent Hayward
#  Date         :  Augustus 2006
#  Project      :  Oral-B Poetsweek
#  Filename     :  class.conp0100.php
#  Version      :  1.00
#  Description  :  Oral-B Nationale Poetsweek Quiz & Poll

require_once('class.stdModule.php');

class conp extends stdModule {
	
	function conp() {
		
		$this->stdModule('conp0100');
		
		// Available Templates
		$this->_settings->templates->quiz			= 'quiz.tpl';
		$this->_settings->templates->quiz_results	= 'quiz_results.tpl';
		$this->_settings->templates->poll			= 'poll.tpl';
		$this->_settings->templates->poll_results	= 'poll_results.tpl';
	}
	
	function printPoll($qst_id) {
		$this->_smrt->assign('poll', $this->getPoll($qst_id));				
		$this->_smrt->assign('options', $this->getOptions($qst_id));
		$this->assignTemplateGlobals();
		$this->_smrt->display($this->_settings->templates->poll); 
	}
	
	function getOptions($qst_id, $order='') {
		$sql_order = ($order == 'votecount') ? 'opt_votecount DESC' : 'opt_order';
		if (is_numeric($qst_id)) {
			$sql = "SELECT 
						*
					FROM 
						tbl_poll_options
					WHERE opt_qst_id = ".$qst_id."					 
					ORDER BY ".$sql_order;
			$rs = $this->_db->query($sql);
			while ($r = $rs->Row()) {
				$return[] = $r;
				$rs->moveNext();
			}
			$rs->Close();
			return $return;
		}
	}
	
	function getPoll($qst_id) {
		if (is_numeric($qst_id)) {
			$sql = "SELECT 
						*
					FROM 
						tbl_poll_questions
					WHERE qst_id = ".$qst_id."
					AND 
						qst_active = '1'";
			$rs = $this->_db->query($sql);
			if (!$rs->EOF()) {
				return $rs->row();
			} 
		}
	}
	
	function doVote($qst_id, $opt_id) {
		if (is_numeric($qst_id) && is_numeric($opt_id)) {
			
			if (!$this->hasVoted($qst_id)) {
				$_COOKIE['conp0100_vote'] .= '['.$qst_id.']';
				if (!setcookie('conp0100_vote',$_COOKIE['conp0100_vote'], time() + 2592000, '/')) {
					return false;
				}

				$this->_db->execute("UPDATE tbl_poll_options SET opt_votecount = opt_votecount + 1 WHERE opt_id = ".$opt_id);
				$this->_db->execute("UPDATE tbl_poll_questions SET qst_votecount = qst_votecount + 1 WHERE qst_id = ".$qst_id);
				
				return true;
			}
		}

		return false;
	}
	
	function hasVoted($qst_id) {
		$voted = (strpos($_COOKIE['conp0100_vote'], '['.$qst_id.']') !== false);
		return $voted;
	}
	
	function printResult($qst_id) {
		$this->_smrt->assign('poll', $this->getPoll($qst_id));		
		$this->_smrt->assign('options', $this->getOptions($qst_id, 'votecount'));				
		$this->assignTemplateGlobals();
		$this->_smrt->display($this->_settings->templates->poll_results); 
	}
	
	function printQuiz($qiz_id, $qst_order=1) {
		if (!is_numeric($qst_order)) $qst_order = 1;
		
		// Zoek id van de vraag op
		$sql = "SELECT qst_id FROM tbl_quiz_question WHERE qst_qiz_id = ".$qiz_id ." AND qst_order = ".$qst_order;
		$rs = $this->_db->query($sql);
		$qst_id = $rs->Row('qst_id');
		$rs->Close();
		
		$rs = $this->_db->query("SELECT max(qst_order) as max_questions FROM tbl_quiz_question WHERE qst_qiz_id = ".$qiz_id." GROUP BY qst_qiz_id");
		$max_questions = $rs->Row('max_questions');
		$rs->Close();
		
		$this->_smrt->assign('qiz_id', $qiz_id);
		$this->_smrt->assign('max_questions', $max_questions);
		$this->_smrt->assign('question', $this->getQuestion($qst_id));				
		$this->assignTemplateGlobals();
		$this->_smrt->display($this->_settings->templates->quiz); 
	}
	
	function getQuestion($qst_id) {

		if(is_numeric($qst_id)) {
			$rs = $this->_db->query("SELECT * FROM tbl_quiz_question WHERE qst_id = ".$qst_id);
			$question = $rs->Row();
			$rs->Close();
			$question['answers'] = array();
			
			$rs = $this->_db->query("SELECT * FROM tbl_quiz_answer WHERE ans_qst_id = ".$qst_id ." ORDER BY ans_order");
			while ($r = $rs->Row()) {
				$question['answers'][$r['ans_id']] = $r['ans_title'];
				$rs->moveNext();
			}
			$rs->Close();
			return $question;
		}
	}
	
	function saveAnswer($qiz_id, $qst_id, $ans_id) {
		$_SESSION['session_conp_quiz_answer'][$qiz_id][$qst_id] = $ans_id;
	}
	
	function printQuizResult($qiz_id) {
		$sql = "SELECT SUM(ans_points) as score FROM tbl_quiz_answer WHERE ans_id IN (".implode(',',$_SESSION['session_conp_quiz_answer'][$qiz_id]).")";
		$rs = $this->_db->query($sql);
		$score = $rs->Row('score');
		$rs->Close();
		
		$sql = "SELECT * FROM tbl_quiz_score WHERE scr_pts_min <= ".$score." AND scr_pts_max >= ".$score." AND scr_qiz_id = ".$qiz_id;
		$rs = $this->_db->query($sql);
		$result = $rs->Row();
		$rs->Close();
		
		if($qiz_id == 1 || $qiz_id == 4) {
			$limit = 'LIMIT 4';
		} else {
			$limit = 'LIMIT 5';
		} 
		
		$sql = "SELECT qst.qst_id, qst.qst_tip_order, qst.qst_tip_title, qst.qst_tip_text FROM tbl_quiz_question qst LEFT JOIN tbl_quiz_answer ans ON qst.qst_id = ans.ans_qst_id WHERE ans.ans_id IN (".implode(',',$_SESSION['session_conp_quiz_answer'][$qiz_id]).") AND ans.ans_points IN (0,1) ORDER BY qst.qst_tip_order ".$limit; 
		$rs = $this->_db->query($sql);
		while ($r = $rs->Row()) {
			$tips[] = $r;
			$rs->moveNext();
		}
		$rs->Close();
				
		$sql = "SELECT qst.qst_id, qst.qst_tip_order, qst.qst_tip_title, ans.ans_points FROM tbl_quiz_question qst LEFT JOIN tbl_quiz_answer ans ON qst.qst_id = ans.ans_qst_id WHERE ans.ans_id IN (".implode(',',$_SESSION['session_conp_quiz_answer'][$qiz_id]).") AND qst_qiz_id = ".$qiz_id." AND qst_title = 'Wat voor soort tandenborstel gebruik je meestal?' ORDER BY qst.qst_tip_order"; 
		$rs = $this->_db->query($sql);
		while ($r = $rs->Row()) {
			$tips[] = $r;
			$rs->moveNext();
		}
		$rs->Close();
			
		$this->assignTemplateGlobals();
		$this->_smrt->assign('score', $score);
		$this->_smrt->assign('result', $result);
		$this->_smrt->assign('tips', $tips);
		$this->_smrt->display($this->_settings->templates->quiz_results);
		$_SESSION['session_conp_quiz_answer'] = '';
	}
}

?>