<?php
// 提交用户签到结果
require_once ("../user_include.inc");

ErrorLogger::doOutput("Koala...ajax_user_examination.php....Start.", 0);

//文章ID
$ueid = ParamUtil::getRequestNumber("ueid", 0);//user_examination_mst.id
$eid = ParamUtil::getRequestNumber("eid", 0);//examination_mst.id

$index = ParamUtil::getRequestNumber("index", 0);
$did = ParamUtil::getRequestNumber("did", 0);//examination_dat.id
$point = ParamUtil::getRequestNumber("point", 0);//改题目的分值
$is_last = ParamUtil::getRequestNumber("is_last", 0);//当前题目的是否为最后一题
$right_answer = ParamUtil::getRequestNumber("right_answer", 0);//当前题目正确答案
$user_answer = ParamUtil::getRequestNumber("user_answer", 0);//当前题目的用户选择
$use_time = ParamUtil::getRequestNumber("use_time", 0);//当前为止用户答题耗时

ErrorLogger::doOutput("Koala...ajax_user_examination.php....ueid=" . $ueid, 0);
ErrorLogger::doOutput("Koala...ajax_user_examination.php....eid=" . $eid, 0);
ErrorLogger::doOutput("Koala...ajax_user_examination.php....index=" . $index, 0);
ErrorLogger::doOutput("Koala...ajax_user_examination.php....did=" . $did, 0);
ErrorLogger::doOutput("Koala...ajax_user_examination.php....point=" . $point, 0);
ErrorLogger::doOutput("Koala...ajax_user_examination.php....is_last=" . $is_last, 0);
ErrorLogger::doOutput("Koala...ajax_user_examination.php....right_answer=" . $right_answer, 0);
ErrorLogger::doOutput("Koala...ajax_user_examination.php....user_answer=" . $user_answer, 0);

$ta = new TemplateAction();
if(!$ta->isLogin()) {
	exit;
}

//更新用户已经消耗的时间/答题分数/错误题数/正确题数
$user_examination_mst = UserExaminationMst::getById($ueid);
$user_examination_mst->use_time = $use_time;

//当前答题数据保存
if($did > 0 && $right_answer > 0 && $user_answer > 0) {
	//避免连击重复提交
	$param = array();
	$param["user_id"] = $ta->user->id;
	$param["user_examination_mst_id"] = $ueid;
	$param["examination_dat_id"] = $did;
	$param["delete_flg"] = false;
	$user_examination_dat_count	= UserExaminationDat::getListCount($param);
	if($user_examination_dat_count < 1) {
		$user_examination_dat = new UserExaminationDat();
		$user_examination_dat->user_id = $ta->user->id;
		$user_examination_dat->user_examination_mst_id = $ueid;
		$user_examination_dat->examination_mst_id = $eid;
		$user_examination_dat->examination_dat_id = $did;
		$user_examination_dat->user_answer = $user_answer;
		
		if($right_answer == $user_answer) {
			$user_examination_dat->is_right = true;
			$user_examination_mst->right_count++;
			$user_examination_mst->point += $point;
		} else {
			$user_examination_dat->is_right = false;
			$user_examination_mst->wrong_count++;
		}
		$user_examination_dat->save();
	}
}
//试卷状态保存
$user_examination_mst->save();

//最后一题交卷处理
if($did > 0 && $right_answer > 0 && $user_answer > 0 && $is_last==1) {
	//用户试卷状态修改
	$param = array();
	$param["user_id"] = $ta->user->id;
	$param["user_examination_mst_id"] = $ueid;
	$param["examination_dat_id"] = $did;
	$param["delete_flg"] = false;
	$user_examination_dat_list	= UserExaminationDat::getList($param);
	
	$user_examination_mst = UserExaminationMst::getById($ueid);
	$user_examination_mst->finish_time = date("Y-m-d H:i:s");
	$user_examination_mst->status = "FINISH";
	$user_examination_mst->use_time = $use_time;
	$user_examination_mst->save();
	//返回结束
	responseFINISH($user_examination_mst);
}

//获取下一题数据
$examination_dat = null;
$param = array();
$param["examination_mst_id"] = $eid;
$param["delete_flg"] = false;
$examination_dat_list = ExaminationDat::getList($param, "category_id asc, display_order", "asc", $index, 2);
if(!empty($examination_dat_list)) {
	$examination_dat = $examination_dat_list[0];
	if(count($examination_dat_list) > 1) {
		$examination_dat->is_last = "0";
	} else {
		$examination_dat->is_last = "1";
	}
	//如果为听力类的题目，替换为播放地址
	if(!empty($examination_dat->mp3)) {
		$file_name = $examination_dat->mp3;
		$play_url = "http://" . SCHOOL_DOMAIN . "/subject_mp3.php?file_name=" . $file_name;
		$examination_dat->mp3 = $play_url;
	}
}

ErrorLogger::doOutput("Koala...ajax_user_examination.php....End.", 0);

responseOK($examination_dat);


function responseNG($data) {
	$result = array("status"=>"NG", "data"=>$data);
	header("Access-Control-Allow-Origin: *");
	print json_encode($result);
	exit;
}
function responseOK($data) {
	$result = array("status"=>"OK", "data"=>$data);
	header("Access-Control-Allow-Origin: *");
	print json_encode($result);
	exit;
}
function responseFINISH($data) {
	$result = array("status"=>"FINISH", "data"=>$data);
	header("Access-Control-Allow-Origin: *");
	print json_encode($result);
	exit;
}
?>