<?php
require("global.php");

$paperType=array("1"=>"單選題","2"=>"多選題","3"=>"判斷題","4"=>"填空題","5"=>"排序題","6"=>"計算題","7"=>"簡答題","8"=>"問答題","9"=>"作文題");
$numDB=array("零","一","二","三","四","五","六","七","八","九","十","十一","十二","十三","十四");
$letterDB=array("a","b","c","d","e","f","g");

$rsdb=$db->get_one("SELECT F.*,S.name AS fname FROM {$pre}exam_form F LEFT JOIN {$pre}exam_sort S ON F.fid=S.fid WHERE F.id='$id'");
if(!$rsdb){
	showerr("數據不存在!");
}

$config=@unserialize($rsdb[config]);

//SEO
$titleDB[title]		= "$rsdb[name] - $rsdb[fname] - $titleDB[title]";

//提交試卷
if($action=="postAnswer")
{
	//試卷的話,必須要先登錄
	if($rsdb[type]==1){
		if(!$lfjuid){
			showerr("請先登錄!");
		}else{
			@extract($db->get_one("SELECT COUNT(*) AS NUM FROM {$pre}exam_student WHERE form_id='$id' AND student_uid='$lfjuid'"));
			if($NUM>0){
				showerr("你已經提交過了,請不要重復提交");
			}
		}
	}

	//獲取每種題型的分數是多少
	$query = $db->query("SELECT COUNT(A.type) AS num,A.type FROM `{$pre}exam_form_element` E LEFT JOIN {$pre}exam_title A ON E.title_id=A.id WHERE E.form_id='$id' GROUP BY A.type");
	while($rs = $db->fetch_array($query)){
		$_L[$rs[type]]=$config[fendb][$rs[type]]/$rs[num];
	}
	
	//插入用戶資料.證實已考試,游客的話,只考慮是參加調查表單
	$student_name=$lfjid?$lfjid:$onlineip;
	$db->query("INSERT INTO `{$pre}exam_student` (`student_uid`, `form_id`, `student_name`,posttime) VALUES ('$lfjuid','$id','$student_name','$timestamp')");
	$student_id=$db->insert_id();
	
	$total_fen=$total_num=0;
	$query = $db->query("SELECT A.* FROM `{$pre}exam_form_element` E LEFT JOIN `{$pre}exam_title` A ON E.title_id=A.id WHERE E.form_id='$id' ");
	while($rs = $db->fetch_array($query)){
		if( $answerdb[$rs[id]]!='' ){
			
			//多選題
			if(is_array($answerdb[$rs[id]])){
				$answer=implode("\n",$answerdb[$rs[id]]);
			}else{
				$answer=$answerdb[$rs[id]];
			}

			$fen=0;
			//對于試卷類型的題目,這種類型的題目可以直接得出分數,
			if( $rsdb[type]==1 && ereg("^(1|2|3|4|5|6)$",$rs[type]) ){
				//填空題要特別處理
				if($rs[type]==4){
					//每答對一個答案都要給分
					preg_match_all("/<<<(.*?)>>>/is",$rs[question],$array);
					$each_fen=$_L[$rs[type]]/count($array[1]);
					foreach($array[1] AS $key=>$value){
						if(trim($value)==trim($answerdb[$rs[id]][$key])){
							$fen+=$each_fen;
						}
					}					
				}elseif($rs[type]==2){
					//多選題中,設置答案的時候.排序不能打亂,只選中部分的給一半分,一旦有錯.不給分
					if(trim($rs[answer])==implode(",",$answerdb[$rs[id]])){
						$fen=$_L[$rs[type]];
					}elseif($answerdb[$rs[id]]){
						$fen=$_L[$rs[type]]/2;
						$answer_array=explode(",",$rs[answer]);
						foreach( $answerdb[$rs[id]] AS $value){
							if(!in_array($value,$answer_array)){
								$fen=0;							//一旦有錯.不給分
							}
						}
					}
				}elseif(trim($answer)==trim($rs[answer])){
					$fen=$_L[$rs[type]];
				}
				if($fen){
					$total_num++;
					$total_fen+=$fen;
				}
			}

			//防止有不安全代碼
			$answer = preg_replace('/javascript/i','java script',$answer);
			$answer = preg_replace('/<iframe ([^<>]+)>/i','&lt;iframe \\1>',$answer);
			$db->query("INSERT INTO `{$pre}exam_student_title` ( `student_id`, `title_id`, `form_id`, `answer`, `fen` ) VALUES ('$student_id','$rs[id]','$id','$answer','$fen' )");
		}
	}
	if($total_fen){
		$db->query("UPDATE `{$pre}exam_student` SET total_fen='$total_fen' WHERE student_id='$student_id'");
	}
	if($rsdb[type]==2){
		refreshto("$webdb[www_url]/","謝謝你參與本次活動調查",30);
	}else{
		refreshto("$webdb[www_url]/","你在本次考查中,共答對{$total_num}題,所得總分是:{$total_fen}分",60);
	}		
}

unset($listdb);
$query = $db->query("SELECT T.* FROM `{$pre}exam_form_element` E LEFT JOIN `{$pre}exam_title` T ON E.title_id=T.id WHERE E.form_id='$id' ORDER BY E.list DESC,E.element_id ASC ");
while($rs = $db->fetch_array($query)){
	//處理有些題庫已被刪除
	if(!$rs[id]){
		continue;
	}
	$rs[showcontent]='';
	//單選題與判斷題
	if($rs[type]==1||$rs[type]==3){
		$detail=explode("\r\n",$rs[config]);
		foreach( $detail AS $key=>$value){
			if($value===''){
				continue;
			}
			$black=strlen($value)>20?'<br>':'&nbsp;&nbsp;&nbsp;';
			$rs[showcontent].="<input type='radio' name='answerdb[$rs[id]]' value='$value'  style='border:0px;'> {$letterDB[$key]}、{$value} $black";
		}
	//多選題
	}elseif($rs[type]==2){
		$detail=explode("\r\n",$rs[config]);
		foreach( $detail AS $key=>$value){
			if($value===''){
				continue;
			}
			$black=strlen($value)>20?'<br>':'&nbsp;&nbsp;&nbsp;';
			$rs[showcontent].="<input type='checkbox' name='answerdb[{$rs[id]}][]' value='$value' style='border:0px;'> {$letterDB[$key]}、{$value} $black";
		}
	//填空題
	}elseif($rs[type]==4){
		$rs[question]=preg_replace("/<<<(.*?)>>>/is","<input type='text' name='answerdb[$rs[id]][]' style='border:0px;border-bottom:1px solid #ccc;width:70px;'>",$rs[question]);
	//排序題
	}elseif($rs[type]==5){
		$detail=explode("\r\n",$rs[config]);
		foreach( $detail AS $key=>$value){
			if($value===''){
				continue;
			}
			$rs[showcontent].="{$letterDB[$key]}、$value<BR>";
		}
		$rs[showcontent].="答:<input type='text' name='answerdb[$rs[id]]' style='border:0px;border-bottom:1px solid #ccc;width:270px;'>";
	//簡答題
	}elseif($rs[type]==7){
		$rs[showcontent].="答:<input type='text' name='answerdb[$rs[id]]' style='border:0px;border-bottom:1px solid #ccc;width:370px;'>";
	//問答題,作文題
	}elseif($rs[type]==8||$rs[type]==9){
		$rs[showcontent].="答:<textarea name='answerdb[$rs[id]]' cols='65' rows='10'></textarea>";
	//其它題型,如計算題
	}else{
		$rs[showcontent].="答:<input type='text' name='answerdb[$rs[id]]' style='border:0px;border-bottom:1px solid #ccc;width:170px;'>";
	}
	$listdb[$rs[type]][]=$rs;
}
ksort($listdb);

require(PHP168_PATH."inc/head.php");
require(html("exam_write"));
require(PHP168_PATH."inc/foot.php");
?>