<?php

require_once './include/common.inc.php';

$question = $dsql->getone("SELECT ask.*, mem.userid as username, mem.scores
FROM {$cfg_dbprefix}ask ask left join {$cfg_dbprefix}member mem on mem.ID=ask.uid WHERE ask.id='$id' and status>='0'");

if($question) {
	if($question['status'] == 1){
		$question['dbstatus'] = 1;
		$question['status'] = 'qa_ico_4.gif';
	}elseif($question['expiredtime'] < $timestamp) {
		$question['dbstatus'] = 3;
		$question['status'] = 'qa_ico_4.gif';
		$dsql->setquery("update {$cfg_dbprefix}ask set solvetime=expiredtime, status='3' where id='$id'");
		$dsql->executenonequery();
		$question['solvetime'] = $question['expiredtime'];
	}elseif($question['status'] == 2) {
		$question['dbstatus'] = 2;
		$question['status'] = 'qa_ico_4.gif';
	}else{
		$question['dbstatus'] = 0;
		$question['status'] = 'qa_ico_3.gif';
	}
	$question['content'] = nl2br(ihtmlspecialchars($question['content']));
	$question['extra'] = nl2br(ihtmlspecialchars($question['extra']));
	$question['toendsec'] = $question['expiredtime'] - $timestamp;
	$question['toendday'] = floor($question['toendsec']/86400);
	$question['toendhour'] = floor(($question['toendsec']%86400)/3600);
	$question['dateline'] = gmdate('m-d', $question['dateline'] + ($timeoffset * 3600));
	$question['solvetime'] = gmdate('Y-m-d h:i', $question['solvetime'] + ($timeoffset * 3600));
	$publisher = 0;
	if($question['uid'] == $uid) {
		$publisher = 1;
	}
}else{
	showmsgs('question_nonexistence', '-1');
}

//等级
$honors = array();
$dsql->setquery("Select id, titles, icon, integral From #@__scores order by integral desc");
$dsql->execute();
while($row = $dsql->getarray())
{
	$honors[] = $row;
}
$question['honor'] = '';
foreach($honors as $honor){
	if($honor['integral']<=$question['scores']){
		$question['honor'] = $honor['titles'];
		break;
	}
}


$nav = '<a href="'.$indexname.'">'.$sitename.'</a> '.$symbols.' <a href="browser.php?tid='.$question['tid'].'">'.$question['tidname'].'</a>';
$navtitle = $question['title'];
if($question['tid2']){
	$nav .= ' '.$symbols.' <a href="browser.php?tid2='.$question['tid2'].'">'.$question['tid2name'].'</a>';
	$navtitle .= ' '.$question['tid2name'];
}
$navtitle .= ' '.$question['tidname'].' '.$sitename;

$dsql->Execute('me',"select answer.*,m.scores from {$cfg_dbprefix}askanswer answer left join {$cfg_dbprefix}member m on m.ID=answer.uid where askid='$id' and ifcheck='1'");
$comments = $answers = array();
$first = $goodrateper = $badrateper = $goodrate = $badrate = $ratenum = $commentnum = $answernum = $myanswer = 0;
while($row = $dsql->getarray())
{
	$row['dateline'] = gmdate('m-d h:i', $row['dateline'] + ($timeoffset * 3600));
	$row['dbcontent'] = $row['content'];
	$row['content'] = nl2br(ihtmlspecialchars($row['content']));

	foreach($honors as $honor){
		if($honor['integral'] >= $row['scores']){
			$row['honor'] = $honor['titles'];
			break;
		}else{
			$row['honor'] = $honor['titles'];
		}
	}

	if($row['ifanswer'] == 1)
	{//回答
		if($uid == $row['uid']) {
			$myanswer = 1;
		}
		if($row['id'] == $question['bestanswer']) {
			$digestanswer = $row;
			$ratenum = $row['goodrate'] + $row['badrate'];
			$goodrate = $row['goodrate'];
			$badrate = $row['badrate'];
			$goodrateper = @ceil($goodrate*100/$ratenum);
			$badrateper = 100-$goodrateper;
		}else{
			$answers[] = $row;
			$answernum++;
		}
	}else{//对最佳答案的评价
		if($row['uid'] == $question['uid'] && $first == 0) {
			$publishercomment = $row;
			$first = 1;
		}else{
			$commentnum++;
			$comments[] = $row;
		}
	}
}

//快到期的问题
$query = "select id, tid, tidname, tid2, tid2name, title from {$cfg_dbprefix}ask where status='0' order by expiredtime asc, dateline desc limit 10";
$dsql->Execute('me',$query);
$expiredasks = array();
while($row = $dsql->getarray())
{
	$row['title'] = cn_substr($row['title'],24);
	$expiredasks[] = $row;
}

//会员排行
$query = "select ID, userid, scores from {$cfg_dbprefix}member order by scores desc limit 10";
$dsql->Execute('me',$query);
$topmembers = array();
while($row = $dsql->getarray())
{
	$topmembers[] = $row;
}

$dsql->Close();
include DEDE.'./template/default/question.htm';
?>