<?php
define('THIS_SCRIPT', 'myask');
require_once 'global.php';
include_once $fsetting['manageurl'].'inc/page.class.php';
$lang = load_lang($lang, 'usermanager' );
$stats = $lang['hometitle'];
$arrNavMenu = array(
	0=>array('linktitle'=>$lang['HeaderTitle1'],'linkurl'=>'myask.php')
	,1=>array('linktitle'=>$stats)
);
LoadStyleJs();
PageLoad();
LoadFooter();


function PageLoad(){
	global $userid,$lang,$db,$action;
	global $db,$boardid,$fsetting,$lang,$userinfo,$_DCACHE,$boardmaster,$master,$list,$_DCACHE,$userintegral;
	global $page_size,$page,$total,$page_count,$offset,$p;
	if( intval($userid) == 0 ){
		showmsg($lang['MustLogin']);
		exit;
	} else{
		MainFunc();
	}
}

function MainFunc(){
	global $db,$boardid,$fsetting,$lang,$userinfo,$_DCACHE,$boardmaster,$master,$list,$_DCACHE,$action,$userintegral;
	global $page_size,$page,$total,$page_count,$offset,$p;
	if($action=='all' or $action==''){
		allask(1);
	}
	elseif($action=='overdue'){
		allask(2);
	}
	elseif($action=='allanswer'){
		allask(3);
	}
	elseif($action=='adoptanswer'){
		allask(4);//全部回答
	}
	user_forum_wealth();
	require_once printout('usermanager.navmenu');
	require_once printout('myask');
}

/*
全部问题==》1
过期问题==》2
过期时限用户可以在后台设置
*/
function allask($class){
	global $lang,$dv,$db,$userid,$list,$arraylist;
	global $page_size,$page,$total,$page_count,$offset,$p;
	$overdue=15;//设置过期天数
	$bbsnum=0;
	$list=array();
	if($class==1){
		$total=$db->scalar("select count(1) from {$dv}savvy_topic where postuserid={$userid} and boardid NOT IN (777,444)");
	}
	elseif($class==2){
		$total=$db->scalar("select count(1) from {$dv}savvy_topic where postuserid={$userid} and dateandtime<'".dateAdd('d',-$overdue,gmdate('Y-m-d 23:59:59'), '')."' and boardid NOT IN (777,444)");
	}
	elseif($class==3){
		$total= 0;
		$totallist= array();
		$tablelist= array();
		$result=$db->query("select tablename from {$dv}tablelist");
		while ($tablearr = $db->fetch_array($result)){
			$total_record=$db->scalar("select count(1) from {$tablearr['tablename']} where postuserid={$userid} and forumtype=1 and boardid NOT IN (777,444)");
			$totallist[]= $total_record;
			$total+= $total_record;
			$tablelist[]= $tablearr['tablename'];
		}
	}
	elseif($class==4){//回答被采纳
		$total= 0;
		$totallist= array();
		$tablelist= array();
		$result=$db->query("select tablename from {$dv}tablelist");
		while ($tablearr = $db->fetch_array($result)){
			$total_record=$db->scalar("select count(1) from {$tablearr['tablename']} where postuserid={$userid} and forumtype=1 and ask_solve=1 and boardid NOT IN (777,444)");
			$totallist[]= $total_record;
			$total+= $total_record;
			$tablelist[]= $tablearr['tablename'];
		}		
	}
	$page_size=15;
	if ($page=="" or $page<1)
		$page=1;
	 $page_count=ceil($total/$page_size);
	if ($page>$page_count)
		$page=$page_count;
	$offset=$page;
	if( $offset < 1 ) $offset = 1;
	if($class==1){
		$sql=$db->query("Select /*!40001 SQL_CACHE */ topicid,boardid,title,postusername,postuserid,dateandtime,child,hits,votetotal,lastpost,lastposttime,istop,isvote,isbest,locktopic,expression,topicmode,mode,getmoney,getmoneytype,usetools,issmstopic,hidename,solve,anonymity From {$dv}savvy_topic /*!32312 USE INDEX(board_topic_list) */ Where postuserid={$userid} and boardid NOT IN (777,444) Order By dateandtime Desc", array('absolutePage'=>$offset,'pageSize'=>$page_size));
	}
	elseif($class==2){
		$sql=$db->query("Select /*!40001 SQL_CACHE */ topicid,boardid,title,postusername,postuserid,dateandtime,child,hits,votetotal,lastpost,lastposttime,istop,isvote,isbest,locktopic,expression,topicmode,mode,getmoney,getmoneytype,usetools,issmstopic,hidename,solve,anonymity From {$dv}savvy_topic /*!32312 USE INDEX(board_topic_list) */ Where postuserid={$userid} and dateandtime<'".dateAdd('d',-$overdue,gmdate('Y-m-d 23:59:59'), '')."' and boardid NOT IN (777,444) Order By dateandtime Desc", array('absolutePage'=>$offset,'pageSize'=>$page_size));	
	}
	elseif(($class==3 or $class==4) && ($total > 0)){
		if($class==3){
			$okstr='and ask_solve=0';
		}
		elseif($class==4){
			$okstr='and ask_solve=1';			
		}
		$start= ($page-1)*$page_size;
		$end= $page*$page_size;
		$tempTotal= 0;
		$preTotal= 0;
		for($i=0;$i<count($tablelist);$i++)
		{
			$tempTotal+= $totallist[$i];
			if(($start+1) > $tempTotal)
			{
				$preTotal+= $totallist[$i];
				continue;
			}

			if($end <= $tempTotal){
				if(count($list) > 0)
					$limitstart= 0;
				else
				{
					if($i==0)
						$limitstart= $start;
					else
						$limitstart= $start-$preTotal;
				}
				$limitend= $page_size-count($list);

				$sql=$db->query("select /*!40001 SQL_CACHE */ announceid,parentid,boardid,username,postuserid,topic,body,dateandtime,length,rootid,layer,orders,isbest,ip,expression,locktopic,signflag,emailflag,isagree,isaudit,isupload,postbuyuser,ubblist,getmoney,usetools,getmoneytype,savvy_comment,forumtype,(select child from {$dv}savvy_topic where rootid=topicid) as child,(select title from {$dv}savvy_topic where rootid=topicid) as title from {$tablelist[$i]} where postuserid={$userid} and forumtype=1 {$okstr} and boardid NOT IN (777,444) order by dateandtime DESC limit {$limitstart},{$limitend}");
			}
			else{
				if(count($list) > 0)
					$limitstart= 0;
				else
				{
					if($i==0)
						$limitstart= $start;
					else
						$limitstart= $start-$preTotal;
				}
				$limitend= $page_size-count($list);
				$sql=$db->query("select /*!40001 SQL_CACHE */ announceid,parentid,boardid,username,postuserid,topic,body,dateandtime,length,rootid,layer,orders,isbest,ip,expression,locktopic,signflag,emailflag,isagree,isaudit,isupload,postbuyuser,ubblist,getmoney,usetools,getmoneytype,savvy_comment,forumtype,(select child from {$dv}savvy_topic where rootid=topicid) as child,(select title from {$dv}savvy_topic where rootid=topicid) as title from {$tablelist[$i]} where postuserid={$userid} and forumtype=1 {$okstr} and boardid NOT IN (777,444)  order by dateandtime DESC limit {$limitstart},{$limitend}");
			}

			$preTotal+= $totallist[$i];
			while ($arr=$db->fetch_array($sql)){
				$list[]=$arr;
			}
			$db->free_result();

			if((count($list) >= $page_size) || (count($list) >= $total) || ($end <= $tempTotal))
				break;
		}
	}

	if($class!=3){
		while ($arr=$db->fetch_array($sql)){
			$list[]=$arr;
		}
		$db->free_result();
	}
}


function user_forum_wealth(){
	global $dv,$db,$lang,$userid,$userintegral;
	$userintegral=array();
	$sql=$db->query("select userid,username,answercount,answeradopt,askcount,askdonecount,askdoningcount,asknodonecount,wealthcount,answerwealth,savvy_hortation,offer_pay,anonymity_ask,punishother from {$dv}savvy_integral where userid={$userid}");
	while ($arr=$db->fetch_array($sql)){
		$userintegral[]=$arr;		
	}
	return $userintegral;
	$db->free_result($sql);
}

function together_zero($str){
	if(empty($str)){
		return '0';
	}
	else{
		return $str;
	}	
}

?>