<?php
ob_start();
if(!(is_readable(realpath(dirname(__FILE__).'/../').'/include/includesroutines.inc'))) {
	die('Error - Could not open or read file '.realpath(dirname(__FILE__).'/../').'/include/includesroutines.inc');
} else {
	include_once(realpath(dirname(__FILE__).'/../').'/include/includesroutines.inc');
}
$cn = db_connect();
$lng = ( isset($_REQUEST['lang']) ? 'lang='.$_REQUEST['lang'] : '');
$lngid = db_getLanguageID($cn, getLanguage());
$contlng = 'lang='.db_getLanguageShortByID($cn, $_SESSION['user_idlanguagecontent']);
$contlngid = $_SESSION['user_idlanguagecontent'];
session_write_close();
$usr_arr = GetUserIDs();
if (!isUserAdmin() && !db_isUserAdminToPolls($cn, $usr_arr)){ 
	ob_end_flush();
	exit;
}

if (!isset($_REQUEST['skpollingobject']) || !isset($_REQUEST['skpolltype'])){ 
	ob_end_flush();
	exit;
}

$urlWO = getCurPageURLWO('service');
$skpollingobject = $_REQUEST['skpollingobject'];
$skpolltype = $_REQUEST['skpolltype'];
$answersvals = [];
$answers = db_getPolltypeQuestionAnswers($cn, $skpollingobject, $skpolltype );
if (is_array($answers) && sizeof($answers) > 0){
	for($j=0; $j<sizeof($answers); $j++){
		$question = db_getPolltypeQuestion($cn, $skpollingobject, $skpolltype, $answers[ $j ]['iduser'], $contlngid );
		$ansval = '';
		if (is_array($question) && sizeof($question) > 0 ){
			if ($question[0]['idpolltypequestiontype'] == 1){
				$ansval = $question[0]['rft_columnname_text'];
			} else if ($question[0]['idpolltypequestiontype'] == 2 && sizeof($answers[$j]) > 0 && $answers[$j]['uav_value'] !== NULL && $answers[$j]['uav_value'] > 0  ){
				$ansval = db_getQuestionSelectValueByID($cn, $question[0]['skpolltypequestion'], $answers[$j]['uav_value'], $contlngid );
			}
			if (strlen($ansval) > 0) {
				array_push($answersvals,$ansval);
			}
		}
	}
}
							
foreach(array_count_values($answersvals) as $key=>$val){
	echo '<nobr>'.$key.': '.$val."</nobr><br/>";
}
echo "<div ".(sizeof($answersvals) == 0 ? "style='margin-top: 0px;'" : "")." href='dialog-thorough-form' class='dialog-thorough dialog-html-show-link admin-pollingobject-answerdetails-link' title='";
getResource("title.thorough");
echo "'  dialogbuttons='visible' dialoghref='pollobjectanswersbyuserdetails.php?skpollingobject=".$skpollingobject.'&skpolltype='.$skpolltype."' >";
getResource("title.thorough");
echo "</div><br/>";
if (sizeof($answersvals) > 0){
	echo "<a href='".$urlWO."pollobjectanswersbyuserdetailsprint.php?skpollingobject=".$skpollingobject.'&skpolltype='.$skpolltype."' target='_blank' title='";
	getResource("button.Print");
	echo "'>";
	getResource("button.Print");
	echo "</a>";
}
db_disconnect($cn);
ob_end_flush();
?>