<?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');
}

if(!(is_readable(realpath(dirname(__FILE__)).'/include/pollobjectsarc.inc'))) {
	die('Error - Could not open or read file '.realpath(dirname(__FILE__)).'/include/pollobjectsarc.inc');
} else {
	include_once(realpath(dirname(__FILE__)).'/include/pollobjectsarc.inc');
}

if(!(is_readable(realpath(dirname(__FILE__)).'/include/pagination.inc'))) {
	die('Error - Could not open or read file '.realpath(dirname(__FILE__)).'/include/pagination.inc');
} else {
	include_once(realpath(dirname(__FILE__)).'/include/pagination.inc');
}

checkUserAndRedirectToLogin();

if(!(is_readable(realpath(dirname(__FILE__)).'/include/filterbuttons.inc'))) {
	die('Error - Could not open or read file '.realpath(dirname(__FILE__)).'/include/filterbuttons.inc');
} else {
	include_once(realpath(dirname(__FILE__)).'/include/filterbuttons.inc');
}

$cn = db_connect();
$lng = ( isset($_REQUEST['lang']) ? 'lang='.$_REQUEST['lang'] : '');
$lngid = db_getLanguageID($cn, getLanguage());
$lngshort = db_getLanguageShortByID($cn, $lngid);
$contlng = $lng;
$contlngid = $lngid;
if (getSetting('portal','useRights',0) == 1){
	$contlng = 'lang='.db_getLanguageShortByID($cn, $_SESSION['user_idlanguagecontent']);
	$contlngid = $_SESSION['user_idlanguagecontent'];
}

$usr_arr = GetUserIDs();
if ( GetSetting('portal','useRights',0) == 0 ){
	if( !IsUserLogin() ){
		header("Location: ".getCurPageURLWO()."/businessmodel.php");
		ob_end_flush();
		exit;
	}
} else {
	if( !IsUserLogin() ){
		header("Location: ".getCurPageURLWO()."/login.php?returl=adminpollobject.php");
		ob_end_flush();
		exit;
	}
	$cn = db_connect();
	if (!isUserAdmin() && !db_isUserAdminToPolls($cn, $usr_arr)){ 
		header("Location: ".getCurPageURLWO()."/personal.php");
		ob_end_flush();
		exit;
	}
}

$id = 0;

if (!isset($_REQUEST['skpollingobject']) || !isset($_REQUEST['skpolltype'])) {
        header("Location: ".getCurPageURLWO()."/pagenotfound.php?url=".getCurPageWithEncodeParams());
        ob_end_flush();
        $oguid = null;
        exit;
}

$pollingobject = db_getPollingObject($cn, $_REQUEST['skpollingobject']);
if (!is_array($pollingobject) || sizeof($pollingobject) == 0 || sizeof($pollingobject[0]) == 0) {
	header("Location: ".getCurPageURLWO()."/pagenotfound.php?url=".getCurPageWithEncodeParams());
	ob_end_flush();
	$oguid = null;
	exit;
}

$onpage = getSetting('page', 'polls.pagesize', 20);
$default_order_column = getCookieForPage('order_column','user_name'); 
$default_order_val = getCookieForPage('order_val','asc');

$url = getCurPageURL();
$urlFromCookies = getCurPageURL();
if (getCookieForPage('foldergroup') !== NULL && !isset($_REQUEST['foldergroup'])) {
	$urlFromCookies = urlReplaceParam($urlFromCookies,'foldergroup', getCookieForPage('foldergroup'));
}

if (!isset($_REQUEST['answers-has']) && !isset($_REQUEST['answers-hasnot'])) {
	if (getCookieForPage('answers-hasnot') !== NULL)
		$urlFromCookies = urlReplaceParam($urlFromCookies,'answers-hasnot', '');
	if (getCookieForPage('answers-has') !== NULL)
		$urlFromCookies = urlReplaceParam($urlFromCookies,'answers-has', '');
	if (getCookieForPage('answers-has') === NULL && getCookieForPage('answers-hasnot') === NULL) {
		$urlFromCookies = urlReplaceParam($urlFromCookies,'answers-hasnot', '');
		$urlFromCookies = urlReplaceParam($urlFromCookies,'answers-has', '');
	}
	if( $urlFromCookies !== getCurPageURL() ){
		header("Location: ".$urlFromCookies);
		ob_end_flush();
		exit;
	}
}

// url parse and build
$order_column = isset( $_GET[ "order_column" ] ) ? $_GET[ "order_column" ] : $default_order_column;
$order_val = isset( $_GET[ "order_val" ] ) ? $_GET[ "order_val" ] : $default_order_val;
setCookieForPage("order_column", $order_column);
setCookieForPage("order_val", $order_val);
$page = isset( $_GET[ "page" ] ) ? $_GET[ "page" ] : 1;
setCookieForPage('answers-has', (isset($_REQUEST['answers-has']) ? 'true' : NULL));
setCookieForPage('answers-hasnot', (isset($_REQUEST['answers-hasnot']) ? 'true'  : NULL));

$objectName = db_getObjectName($cn, $pollingobject[0]['o_guid'], $contlngid, $pollingobject[0]['skbranch']);
$reportName = ''.$pollingobject[0]['r_guid'];
if ($pollingobject[0]['o_guid'] !== $pollingobject[0]['r_guid']) {
	$reportName = db_getReportName($cn, $pollingobject[0]['r_guid'], $contlngid, $pollingobject[0]['skbranch']);
}

$objectfullname = $objectName.' ('.$reportName.')';

?>
<!doctype html>
<html>
<head>
        <title><?php echo renderTitle();?></title>
        <? 
        renderCommonMeta(renderTitle()); 
        renderCommonCSS(); 
        ?>
</head>
<body>
        <div id="" style="overflow: hidden;">
			<section class="bread-crumbs" style="padding-bottom: 10px;">
				<div id="box-input-container" >			
						<div class="box-period-caption" style="margin-top: 9px;margin-bottom: 10px;">
							<?php echo ''.db_getPolltypeNameBySK($cn, $_REQUEST['skpolltype'], $contlngid).': '; if (isset($pollingobject[0]['skproject']) && $pollingobject[0]['skproject'] !== '0') { echo db_getProjectName($cn, $pollingobject[0]['skproject'], $contlngid);} else { echo db_getBranchName($cn, $pollingobject[0]['skbranch'], $contlngid);} ?> \ <span class="box-poll-bread-crumbs-objectfullname-span"> <?php echo $objectfullname;?></span> 
						</div>
						<div class="box-group-caption" style="margin-top: 5px;margin-bottom: 10px;">&nbsp;
							<span>
								<?php 
									echo buildFilterButton(getResourceByLang("title.poll.filter.answers.has"), 'answers-has', isset($_REQUEST['answers-has']),  $order_column, $order_val, $page  );
									echo buildFilterButton(getResourceByLang("title.poll.filter.answers.hasnot"), 'answers-hasnot', isset($_REQUEST['answers-hasnot']),  $order_column, $order_val, $page );
								?>
							</span>
						</div>
				</div>
			</section>
		</div>
		<div style="overflow: auto;height: calc(100% - 65px);"> <!--63840-->			
            <table class="tbldata self-binding-width" cellspacing="0">
				<tr>
					<th style="width: 35%;" valign="top" class="<? if( $order_column == "user_name" ) echo "tbldata-cell-sortable" ?> tblinput-cell-name"><? echo buildSortColumnTitle( getResourceByLang('field.poll.user'), ( $order_column == "user_name" ? true : false ), "user_name", $order_val, '' ) ?></th>
					<th style="width: 15%;" valign="top" class="<? if( $order_column == "answer_value" ) echo "tbldata-cell-sortable" ?> tblinput-cell-name"><? echo buildSortColumnTitle( getResourceByLang('field.poll.answer'), ( $order_column == "answer_value" ? true : false ), "answer_value", $order_val, '' ) ?></th>
					<th style="width: 50%;" valign="top" class="tblinput-cell-name" style="width:50%"><? echo getResourceByLang('field.poll.comment'); ?></th>
                </tr>
                        <?php 
							$answers = NULL;
							if (!isset($_REQUEST['answers-has']) && isset($_REQUEST['answers-hasnot']))
								$answers = 'hasnot';
							if (!isset($_REQUEST['answers-hasnot']) && isset($_REQUEST['answers-has']))
								$answers = 'has';
							$answers = db_getPolltypeQuestionAnswersExpectedWithNames($cn, $_REQUEST[ "skpollingobject" ], $_REQUEST[ "skpolltype" ], $contlngid, $order_column, $order_val, $answers );
							$total = sizeof($answers) == 0 ? 1 : sizeof($answers);
							$totalpages = ceil($total/$onpage); 
							$totalpages = $totalpages == 0 ? 1 : $totalpages;
							$idpolltype = db_getIDPollTypeBySK($cn, $_REQUEST[ "skpolltype" ]);
							for($j=(($page - 1) * $onpage); $j< ( ($page * $onpage) <= $total ? ($page * $onpage) : $total); $j++){
								if (!isset($answers[ $j ])){
									break;
								}
                                echo "<tr>";
                                echo "<td valign='top'>";
								echo "<a href='adminpolbyrespondent.php?iduser=".$answers[ $j ][ "iduser" ]."&idpolltype=".$idpolltype."&".
									(isset($pollingobject[0]['skproject']) && $pollingobject[0]['skproject'] > 0 ? "skproject=".$pollingobject[0]['skproject'] : "skbranch=".$pollingobject[0]['skbranch'])."' target='_top'>".$answers[ $j ]['user_name']."</a>"; //63977
                                echo "</td>";
                                echo "<td valign='top'>";
                                echo $answers[ $j ]['answer_value']."<br/>".$answers[ $j ]['uav_tmst']; //add time 68883
                                echo "</td>";
                                echo "<td valign='top'>";
                                echo $answers[ $j ]['uav_body'];
                                echo "</td>";
                                echo "</tr>";
							}
						
			?>      
                </table>    
<?php if (!(null === $totalpages || $totalpages == 0) && $totalpages > 1) {?>
				<section id="box-paginator" class="box-paginator-table">
				<?php echo_panel_page($totalpages, $page, $url, FALSE);?>
				</section>
<?php }?>				                              
        </div>
        <?php renderFooterTemplate(); ?>
</body>
</html>
<?php
db_disconnect($cn);
ob_end_flush(); ?>
