<?php
#  Author       :  Vincent Hayward
#  Date         :  December 2006
#  Project      :  Amgate versie 5
#  Filename     :  class.enquete.php
#  Version      :  1.00
#  Description  :  Amgate Enquete

require_once ('class.stdModule.php');

class AmgateEnquete extends stdModule {
	
	
	function AmgateEnquete() {
		
		$this->stdModule('enquete');
		
		// Available Templates
		$this->_settings->templates->login				= 'login.tpl';
		$this->_settings->templates->enquete_intro		= 'enquete_intro.tpl';
		$this->_settings->templates->enquete_qst		= 'enquete_qst.tpl';
		$this->_settings->templates->enquete_hasvoted	= 'enquete_hasvoted.tpl';
		$this->_settings->templates->enquete_results	= 'enquete_results.tpl';
		
	}
	
	
	function printLogin() {
		
		$this->assignTemplateGlobals();
		$this->_smrt->display($this->_settings->templates->login);
		
		// For DEV purposes $inlog is set on true. So we can skip the inlog part and go straight to the intro
		/*$inlog = true;
		if($inlog = true) {
			$this->_smrt->display($this->_settings->templates->enquete_intro);
		} else {
			$this->_smrt->display($this->_settings->templates->login);
		}*/
	}


	function printEnqueteIntro() {
		
		$this->assignTemplateGlobals();
		$this->_smrt->display($this->_settings->templates->enquete_intro); 
		
	}
	
	/*function printHasVoted() {
		
		$this->assignTemplateGlobals();
		$this->_smrt->display($this->_settings->templates->enquete_hasvoted); 
		
	}*/
	
	function printResults() {
		$this->_smrt->assign('usersanswers', $this->getUserAnswers());		
		$this->_smrt->assign('answers', $this->getAnswers());	
		
		$this->assignTemplateGlobals();
		$this->_smrt->display($this->_settings->templates->enquete_results); 
	}
	
	function printQuestion($qst_id) {
		$this->_smrt->assign('question', $this->getQuestion($qst_id));		
		$this->_smrt->assign('fields', $this->getFields($qst_id));		
		$this->_smrt->assign('max', $this->getMaxQuestions());		
		$this->assignTemplateGlobals();
		$this->_smrt->display($this->_settings->templates->enquete_qst); 
	}
	
	
	function getQuestion($qst_nr) {
		if (is_numeric($qst_nr)) {
			$sql = "SELECT 
						*
					FROM 
						tbl_enq_question
					WHERE qst_nr = ".$qst_nr;
			$rs = $this->_db->query($sql);
			if (!$rs->EOF()) {
				return $rs->row();
			} 
		}
	}
	
	function getUserAnswers() {
		$sql = "SELECT
					ans_fld_id,
					ans_answer,
					usr_id,
					usr_initials,
					usr_firstname,
					usr_familyname,
					usr_email,
					fld_title,
					qst_title
				FROM
					tbl_enq_answer
				LEFT JOIN
					tbl_enq_user
				ON
					ans_usr_id = usr_id
				LEFT JOIN
					tbl_enq_field
				ON
					ans_fld_id = fld_id				
				LEFT JOIN
					tbl_enq_question
				ON
					fld_qst_id = qst_id
				ORDER BY
					usr_id
				ASC";
		$rs = $this->_db->query($sql);
		while($r = $rs->Row()) {
			
			//$usersanswers[$r['usr_id']] = $r;
			$usersanswers[$r['usr_id']]['user']['initials'] = $r['usr_initials'];
			$usersanswers[$r['usr_id']]['user']['firstname'] = $r['usr_firstname'];
			$usersanswers[$r['usr_id']]['user']['familyname'] = $r['usr_familyname'];
			$usersanswers[$r['usr_id']]['user']['email'] = $r['usr_email'];
			if(isset($r['fld_title'])) {
				$usersanswers[$r['usr_id']]['answers'][$r['qst_title']][$r['fld_title']] = $r['ans_answer'];
			} else {
				$usersanswers[$r['usr_id']]['answers'][$r['qst_title']] = $r['ans_answer'];	
			}
			$rs->MoveNext();
		}
		$rs->Close();
		
		return $usersanswers;
	}
	
	function getAnswers() {
		$sql = "SELECT
					ans_fld_id,
					ans_answer,
					fld_title,
					qst_title,
					count(ans_answer) as amount
				FROM
					tbl_enq_answer
				LEFT JOIN
					tbl_enq_field
				ON
					ans_fld_id = fld_id
				LEFT JOIN
					tbl_enq_question
				ON
					fld_qst_id = qst_id
				GROUP BY
					ans_fld_id,
					ans_answer
				ORDER BY
					ans_fld_id
				ASC";
		
		$rs = $this->_db->query($sql);	
		while($r = $rs->Row()) {
				$answers[$r['ans_fld_id']]['qst_title'] = $r['qst_title'];
				$answers[$r['ans_fld_id']]['fld_title'] = $r['fld_title'];
				
				if($r['fld_title']) {
					switch($r['ans_answer']) {
						
						case 4: 
							$r['ans_answer'] = 'uitstekend';
							break; 
							
						case 3:
							$r['ans_answer'] = 'goed';
							break;
													
						case 2:
							$r['ans_answer'] = 'matig';
							break;
							
						case 1: 
							$r['ans_answer'] = 'slecht';
							break;
					}
				}				
				$answers[$r['ans_fld_id']]['answers'][$r['ans_answer']] = $r['amount'];
				$rs->MoveNext();
		}
		$rs->Close();
		return $answers;
	}
	
	function getFields($qst_nr) {
		$fields = array();
		if (is_numeric($qst_nr)) {
			$sql = "SELECT 
						*
					FROM 
						tbl_enq_field
					LEFT JOIN
						tbl_enq_question
					ON 
						fld_qst_id = qst_id
					WHERE 
						qst_nr = ".$qst_nr;
						
			$rs = $this->_db->query($sql);
			while ($r = $rs->Row()) {
				
				if ($r['fld_type'] == 'rdo') {
					$options = explode('||', $r['fld_options']);
					$r['options'] = array_combine(array_map('cleanstring', $options), $options);
				}

				$fields[] = $r;
				
				$rs->moveNext();
			} 
			$rs->Close();
		}
		
		return $fields;
	}
	
	
	function getMaxQuestions() {
		$sql = "SELECT 
						max(qst_nr) as question_nr
					FROM 
						tbl_enq_question";
		$rs = $this->_db->query($sql);
		if (!$rs->EOF()) {
			return $rs->row();
		} 
	}
	
	
	function setAnswer($fld_id, $ans_id) {
		$_SESSION['session_enquete'][$fld_id] = $ans_id;
	}
	
	
	function saveAnswers($usr_id, $answers) {
		if(is_array($answers) && isset($usr_id) && is_numeric($usr_id)) {

			foreach($answers as $fld_id => $answer) {
				$sql = "INSERT INTO tbl_enq_answer VALUES('','$usr_id','$fld_id','$answer')";
				$this->execute($sql);
			}
			session_destroy();
		}
	}
	
	
	function hasVoted($usr_id) {
		if(isset($usr_id) && is_numeric($usr_id)) {
			
			$sql = "SELECT count(ans_usr_id) as aantal FROM tbl_enq_answer WHERE ans_usr_id = ".$usr_id;
			$rs = $this->query($sql);

		}
		return $rs->Row('aantal') > 0;
	}
	
	
	function CheckAuthorization($usr_username, $usr_password) {
		if(isset($usr_username) && isset($usr_password)) {
			
			$sql = "SELECT usr_id FROM tbl_enq_user WHERE usr_username = '".$usr_username."' AND usr_password = '".$usr_password."'";
			$rs = $this->query($sql);
			if (!$rs->EOF()) {
				$usr_id = $rs->Row('usr_id');
			} else {
				$error_msg = "Sorry maar de door u ingevoerde gebruikersnaam en wachtwoord combinatie is onjuist.<br>Probeer het nog eens!";
				$this->_smrt->assign('error_msg', $error_msg);
			}
		}
		return $usr_id;
	}
	
}


if (! function_exists('array_combine')) {
	function array_combine($keys, $values) {
		foreach($keys as $key) $out[$key] = array_shift($values);
		return $out;
	}
}

function cleanstring($str) {
	$str = preg_replace(array('/\W/'), '_', $str);
	return strtolower($str);
}

?>