<?php
!function_exists('html') && exit('ERR');

$paperType=array("1"=>"單選題","2"=>"多選題","3"=>"判斷題","4"=>"填空題","5"=>"排序題","6"=>"計算題","7"=>"簡答題","8"=>"問答題","9"=>"作文題");

//創建題目
if($job=="make"&&$Apower[exam_title])
{
	$sort_fid=$Guidedb->Select("{$pre}exam_sort","fid");

	$ifsharedb[1]=' checked ';
	
	require("head.php");
	require("template/exam_title/menu.htm");
	require("template/exam_title/make.htm");
	require("foot.php");
}
//創建題目
elseif($action=="make"&&$Apower[exam_title])
{
	if(!$fid)
	{
		showerr("請選擇分類");
	}
	$db->query("INSERT INTO `{$pre}exam_title` ( `fid`, `type`, `question`, `config`, `answer`, `uid`, `username`, `ifshare`) VALUES ('$fid','$ctype','$atc_question','$atc_config','$atc_answer','$userdb[uid]','$userdb[username]','$atc_ifshare')");
 	jump("創建succeed,繼續創建",$FROMURL,1);
}

//列出所有題目
elseif($job=="list"&&$Apower[exam_title])
{
	$sort_fid=$Guidedb->Select("{$pre}exam_sort","fid",$fid,"index.php?lfj=$lfj&job=$job");

	if($fid){
		$SQL=" WHERE A.fid='$fid' ";
	}else{
		$SQL=" WHERE 1 ";
	}

	$select_exam="<select name='form_id'>";
	$query = $db->query("SELECT * FROM `{$pre}exam_form` A $SQL");
	while($rs = $db->fetch_array($query)){
		$select_exam.="<option value='$rs[id]'>$rs[name]</option>";
	}
	$select_exam.="</select>";

	
	if($page<1){
		$page=1;
	}
	$rows=50;
	$min=($page-1)*$rows;

	$query = $db->query("SELECT A.*,S.name AS fname FROM `{$pre}exam_title` A LEFT JOIN `{$pre}exam_sort` S ON A.fid=S.fid $SQL ORDER BY A.id DESC LIMIT $min,$rows");
	while($rs = $db->fetch_array($query)){
		$listdb[]=$rs;
	}
	
	$showpage=getpage("`{$pre}exam_title` A","$SQL","index.php?lfj=exam_title&job=list&fid=$fid","$rows");

	require("head.php");
	require("template/exam_title/menu.htm");
	require("template/exam_title/list.htm");
	require("foot.php");
}

//批量添加題目到試卷
elseif($action=="list"&&$Apower[exam_title])
{
	if(!$listdb){
		showerr("請選擇一項");
	}
	foreach( $listdb AS $key=>$value){
		if($ctype=='del'){
			$db->query("DELETE FROM `{$pre}exam_title` WHERE id='$value'");
		}else{
			//批量添加題目到試卷
			if(!$form_id){
				showerr("請選擇一個試卷");
			}
			$rs=$db->get_one("SELECT * FROM `{$pre}exam_form_element` WHERE title_id='$value' AND `form_id`='$form_id'");
			if(!$rs){
				$db->query("INSERT INTO `{$pre}exam_form_element` ( `form_id`, `title_id` ) VALUES ( '$form_id','$value' )");
			}
		}
	}
	refreshto($FROMURL,"操作succeed");
}

//刪除題目
elseif($action=="delete"&&$Apower[exam_title])
{
	$db->query(" DELETE FROM `{$pre}exam_title` WHERE id='$id' ");
	$db->query(" DELETE FROM `{$pre}exam_form_element` WHERE title_id='$id' ");
	$db->query(" DELETE FROM `{$pre}exam_student_title` WHERE title_id='$id' ");
	refreshto($FROMURL,"刪除succeed");
}

//修改題目
elseif($job=="edit"&&$Apower[exam_title])
{

	$rsdb=$db->get_one("SELECT * FROM {$pre}exam_title WHERE id='$id'");
	$sort_fid=$Guidedb->Select("{$pre}exam_sort","fid",$rsdb[fid]);
	$ifsharedb[$rsdb[ifshare]]=' checked ';

	require("head.php");
	require("template/exam_title/menu.htm");
	require("template/exam_title/make.htm");
	require("foot.php");
}
//修改題目
elseif($action=='edit'&&$Apower[exam_title])
{
	$db->query("UPDATE `{$pre}exam_title` SET fid='$fid',type='$ctype',question='$atc_question',config='$atc_config',answer='$atc_answer',ifshare='$atc_ifshare' WHERE id='$id'");
	refreshto($FROMURL,"修改succeed");
}

?>