<?php
/*
转换投票
pre_forum_poll
pre_forum_polloption
*/
if($start <= 1){
	truncatetable('forum_poll');
	truncatetable('forum_polloption');
}

$maxtid = $xc->db['source']->result($xc->db['source']->query("SELECT max(tid) FROM {$source_tablepre}polls"), 0);
if($start < $maxtid){
	$converted = 1;
}

$query = $xc->db['source']->query("SELECT * FROM {$source_tablepre}polls WHERE tid >= $start AND tid <$start + $rpp") or dexit();
while ($pwpoll 	=	$xc->db['source']->fetch_array($query)) {
	$pwpoll		=	daddslashes($pwpoll);
	$t_option	=	unserialize(stripslashes($pwpoll['voteopts']));
	//pre_forum_poll
	$tid			=	$pwpoll['tid'];
	$overt			=	0;						//是否公开投票参与人
	$multiple		=	$pwpoll['multiple'];
	$visible		=	$pwpoll['previewable'];
	$maxchoices		=	$pwpoll['mostvotes'];
	$dateline		=	$xc->db['discuz']->result($xc->db['discuz']->query("SELECT dateline FROM {$discuz_tablepre}forum_thread WHERE tid='$tid'"), 0);
	$expiration		=	$pwpoll['timelimit'] ? ($pwpoll['timelimit'] * 86400 + $dateline) : 0;
	$pollpreview	=	'';						//选项内容前两项预览
	$voters			=	0;						//投票人数
	if(!is_array($t_option)){
		$xc->db['discuz']->query("UPDATE {$discuz_tablepre}forum_thread SET special=0  WHERE tid='$tid'");
		continue;
	}
	foreach($t_option as $key=>$option) {
		//pre_forum_polloption
		$votes			=	$option[1];				//票数
		$displayorder	=	0;						//显示顺序
		$polloption		=	addslashes($option[0]);				//显示内容
		$voterids		=	'';						//投票人id，是一个文本
		$uid_query	=	$xc->db['source']->query("SELECT uid FROM {$source_tablepre}voter WHERE tid='$tid' and vote='$key'");
		while($uid = $xc->db['source']->fetch_array($uid_query)) {
			$voterids	.=	"\t".$uid['uid'];
		}
		$voters			=	$voters+$option[1];		//投票总人数

		$field1			=	array('tid','votes','displayorder','polloption','voterids');
		$query1			=	getinsertsql("{$discuz_tablepre}forum_polloption", $field1);
		if ($xc->db['discuz']->query($query1)) {
		} else {
			reportlog("无法转换投票选项表 tid = $tid，SQL 语句如下 ：<br><textarea rows=\"3\" style=\"width: 100%;\">".$query1."</textarea>");
			continue;
		}
	}

	$field2	=	array('tid','overt','multiple','visible','maxchoices','expiration','pollpreview','voters');
	$query2	=	getinsertsql("{$discuz_tablepre}forum_poll", $field2);

	if ($xc->db['discuz']->query($query2)) {
		$convertedrows ++;
	} else {
		reportlog("无法转换投票主表 tid = $tid，SQL 语句如下 ：<br><textarea rows=\"3\" style=\"width: 100%;\">".$query2."</textarea>");
	}
	$converted = 1;
	$totalrows ++;
	$url = 'index.php?mod=dealdata&step='.$step.'&start='.($pwpoll['tid']+1).'&stay='.$stay.'&totalrows='.$totalrows.'&convertedrows='.$convertedrows.'&sqlfilenum='.$sqlfilenum;
	file_put_contents(XCONVERT_ROOT.'./cache/lock/progress_'.$step.".txt", $url);
}
?>