<?php
/*
pre_forum_poll
pre_forum_polloption
*/

if($start <= 1){
	truncatetable('forum_poll');
	truncatetable('forum_polloption');
}
$query = $xc->db['source']->query("SELECT max(voteid) FROM {$source_tablepre}vote");
$maxid = $xc->db['source']->fetch_array($query);

$query = "SELECT * FROM {$source_tablepre}vote WHERE voteid BETWEEN $start AND $end";
while ($dvpoll =  $xc->db['source']->fetch_assoc($query)) {
	$dvpoll = array_change_key_case(daddslashes($dvpoll));

	$t_query = $xc->db['source']->fetch_assoc("SELECT topicid FROM {$source_tablepre}topic WHERE pollid=$dvpoll[voteid]");
	$t_topic_arr =  $xc->db['source']->fetch_assoc($t_query);
	$tid = $t_topic_arr['topicid'];

	if(!$dvpoll['voteid']) {
		$xc->db['discuz']->query("UPDATE {$discuz_tablepre}threads SET special=0 WHERE tid='$tid'");
		continue;
	}

	//pre_forum_poll
	//$tid			=	$tid;
	$overt			=	0;
	$multiple		=	$dvpoll['votetype'];
	$maxchoices		=	$multiple ? 0 : 1;
	$visible		=	1;
	$dateline		=	$xc->db['discuz']->result($xc->db['discuz']->query("SELECT dateline FROM {$discuz_tablepre}forum_thread WHERE tid='$tid'"), 0);
	$expiration		=	timetounix($dvpoll['timeout']);
	$pollpreview	=	'';						
	$voters	=	0;						

	//投票选项
	$t_option = explode('|', $dvpoll['vote']);		
	$votenums = explode('|', $dvpoll['votenum']);
	foreach($t_option as $key=>$option) {
		//pre_forum_polloption
		//$polloptionid	=	'';						//自增
		//$tid	=	$tid;
		$votes	=	0;						
		$displayorder	=	0;						
		$polloption	=	$option;				
		$voterids	=	'';						
		$rs2 = $xc->db['source']->execute("SELECT * FROM {$source_tablepre}voteuser WHERE voteid=$dvpoll[voteid] AND (voteoption='$key' OR LEFT(voteoption, 2)='$key,' OR LEFT(voteoption, 3)='$key,' OR CHARINDEX(',$key,', voteoption) OR RIGHT(voteoption, 2)=',$key' OR RIGHT(voteoption, 3)=',$key')");
		$voterids = $comma = '';
		while(!$rs2->EOF) {
			$voterids .= $comma.$rs3->fields[userid]->value;
			$comma = "\t";
			$rs2->Movenext();
		}
		$rs2->close();
		$votes	=	$votenums[$key];		
		
		$field1	=	array('tid','votes','displayorder','polloption','voterids');
		$query1	=	getinsertsql("{$discuz_tablepre}forum_polloption", $field1);
		if (!$xc->db['discuz']->query($query1)) {
			reportlog("无法转换投票选项表 pollid = $pollid tid = $tid，SQL 语句如下 ：<br><textarea rows=\"3\" style=\"width: 100%;\">".$query1."</textarea>");
		}
	}

	$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("无法转换投票主表 pollid = $pollid tid = $tid，SQL 语句如下 ：<br><textarea rows=\"3\" style=\"width: 100%;\">".$query2."</textarea>");
	}

	$totalrows ++;
	$url = 'index.php?mod=dealdata&step='.$step.'&start='.($dvpoll['voteid']+1).'&stay='.$stay.'&totalrows='.$totalrows.'&convertedrows='.$convertedrows.'&sqlfilenum='.$sqlfilenum;
	file_put_contents(XCONVERT_ROOT.'./cache/lock/progress_'.$step.".txt", $url);
}
$end < $maxid ? $converted =1 : '';
?>