<%@ Control Language="C#" %>
<%@ Import Namespace="Kesion.Publics" %>
<%@ Import Namespace="Kesion.BLL" %>
<%@ Import Namespace="Kesion.Cache" %>
<%@ Import Namespace="Kesion.HtmlTags" %>
<%@ Import Namespace="Kesion.APPCode" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="Kesion.Course" %>
<script runat="server" type="text/C#">
	dynamic __user = HttpContext.Current.Items["user"];                //初始化用户信息，
	private static dynamic __info = new System.Dynamic.ExpandoObject();   //初始化当前页信息
	bool isLoadPage = true;  //定义页面是否加载
	public int sjId = 0;
	public int scoreId = 0;
	public int hasXihao = 0;
	public int isRndSj = 0;
	public static string Answer = string.Empty;
	public static string ZGTDFQK = string.Empty;
	public static DataRow dr = null;
	public static DataRow drr = null;
	public static DataTable dtscore = null;
	public static string TJQK = string.Empty;
	protected void Page_Load(object sender, EventArgs e)
	{
		sjId = Utils.StrToInt(KSCMS.S("sjid"));
		initialList();
	}
	private void initialList()
	{
		using (DataTable dtt = DataFactory.GetDataTable("SELECT Top 1 * From KS_SJ Where inputer='" + new TemporaryVar().UserName + "' and id=" + sjId))
		{
			if (dtt.Rows.Count > 0)
			{
				dr = dtt.Rows[0];

				if (dr["dtfs"].ToString() == "3")
				{
					isRndSj = 1;
				}
				#region 成绩段
				int cjdId = Utils.StrToInt(dr["CJDID"].ToString());  //成绩段ID
				if (cjdId != 0)
				{
					DataTable cjdTable = DataFactory.GetDataTable("select * From KS_SJCJDItem Where ItemID=" + cjdId + " order by orderid,id");
					using (DataTable dtd = cjdTable)
					{
						if (dtd.Rows.Count > 0)
						{
							StringBuilder sb = new StringBuilder();
							foreach (DataRow drr in dtd.Rows)
							{
								int num = Utils.StrToInt(DataFactory.GetOnlyValue("select count(1) From KS_SJScore Where SJID=" + sjId + " and realtest=1 and Score>=" + drr["minscore"].ToString() + " and score<=" + drr["maxscore"].ToString()));
								sb.AppendFormat("{0}：{1}人&nbsp;&nbsp;", drr["title"].ToString(), num);
							}
							TJQK = sb.ToString();
						}
					}
				}
				#endregion
			}
			else
			{
				Msg.AlertDoFun("出错啦！", "history.back()");
				isLoadPage = false;
				return;
			}
		}
		dtscore = DataFactory.GetDataTable("select * From KS_SJScore Where SJID=" + sjId);
	
	}
	/// <summary>
	/// 各小题的选题情况
	/// </summary>
	/// <param name="tmType"></param>
	/// <param name="tmid"></param>
	/// <param name="optionNum"></param>
	/// <returns></returns>
	public static string GetXTQK(int tmType, int optionNum, int tmid)
	{
		#region 各小题的选题情况
		int num = 0;
		StringBuilder sb = new StringBuilder();
		switch (tmType)
		{
			case 2:
			case 3:
				for (int i = 1; i <= optionNum; i++)
				{
					num = 0;
					string option = Convert.ToString((char)(i + 64));
					foreach (DataRow dr in dtscore.Rows)
					{
						string userAnswer = Kesion.Exam.Function.GetAnswer(dr["Answer"].ToString(), tmid);
						if (userAnswer.ToLower().IndexOf(option.ToLower()) != -1)
						{
							num++;
						}
					}
					sb.AppendFormat(@"选{0}({1}人) ", option, num > 0 ? "<span style='color:green'>" + num + "</span>" : "0");
				}
				break;
			case 4:
				num = 0;
				foreach (DataRow dr in dtscore.Rows)
				{
					string userAnswer = Kesion.Exam.Function.GetAnswer(dr["Answer"].ToString(), tmid);
					if (userAnswer.ToLower() == "1")
					{
						num++;
					}
				}
				sb.AppendFormat(@"选正确({0}人) ", num > 0 ? "<span style='color:green'>" + num + "</span>" : "0");
				num = 0;
				foreach (DataRow dr in dtscore.Rows)
				{
					string userAnswer = Kesion.Exam.Function.GetAnswer(dr["Answer"].ToString(), tmid);
					if (userAnswer.ToLower() != "1")
					{
						num++;
					}
				}
				sb.AppendFormat(@"选错误({0}人) ", num > 0 ? "<span style='color:green'>" + num + "</span>" : "0");

				break;
		}
		string str = sb.ToString();
		if (!string.IsNullOrEmpty(str))
		{
			str = string.Format(@"<div class=""detail"">考生答案分布： {0}</div>", str);
		}
		return str;
		#endregion
	}
	public static string GetUserAnswerList(int tmType, int tmid, string ckda, string tmfs)
	{
		#region 答案是否正确

		StringBuilder sb = new StringBuilder();

		if (tmType == 1 || dtscore.Rows.Count <= 0)  //主观题无法统计
		{
			sb.AppendLine(@"<td style='text-align:center'>---</td>
                   <td style='text-align:center'>---</td>
                   <td style='text-align:center'>---</td>
                   <td style='text-align:center'>---</td>");
		}
		else
		{
			using (DataTable dt = dtscore)
			{
				if (dt.Rows.Count > 0)
				{
					int zrs = dt.Rows.Count;  //总人数
					int zqrs = 0;   //正确人数
					int cwrs = 0;   //错误人数
					bool istkt = false;  //是否填空题
					foreach (DataRow drr in dt.Rows)
					{
						string userAnswer = Kesion.Exam.Function.GetAnswer(drr["Answer"].ToString(), tmid);
						if (tmType != 5 || (tmType == 5 && ckda.IndexOf("※") == -1))
						{
							if (Kesion.Exam.Function.CheckDa(userAnswer, ckda))
							{
								zqrs++;
							}
							else
							{
								cwrs++;
							}
						}
						else  //填空题多空
						{
							istkt = true;
						}
					}
					if (istkt == false)   //非填空题
					{
						string zqltips = string.Empty;
						if (zqrs == 0 || zrs == 0)
						{
							zqltips = "<font color=brown>0%</font>";
						}
						else
						{
							float zqn = (float)zqrs / zrs * 100;

							if (zqn > 80)
							{
								zqltips = "<font color=green>" + zqn.ToString("f2") + "%</font>";
							}
							else if (zqn <= 50)
							{
								zqltips = "<font color=red><strong>" + zqn.ToString("f2") + "%</strong></font>";
							}
							else
							{
								zqltips = "<font color=brown>" + zqn.ToString("f2") + "%</font>";
							}
						}

						sb.AppendLine(string.Format(@"<td style='text-align:center'><a href='javascript:;' title='查看全部答题的考生' onclick=""showUserList({4},0,-1)"">{0}</a></td>
                           <td style='text-align:center;'><a href='javascript:;' onclick=""showUserList({4},1,-1)"" title='查看答对的考生' style='color:green;'>{1}</a></td>
                           <td style='text-align:center;'><a href='javascript:;' onclick=""showUserList({4},2,-1)"" title='查看答错的考生' style='color:red;'>{2}</a></td>
                           <td style='text-align:center'>{3}</td>", zrs, zqrs, cwrs, zqltips, tmid));
					}
					else
					{
						sb.AppendLine(@"<td colspan=4 >" + gettktfx(ckda, tmid) + "</td>");
					}
				}
			}
		}
		return sb.ToString();
		#endregion
	}

	private static string gettktfx(string ckda, int tmid)
	{
		#region 填空题分析
		var ckarr = ckda.Split('※');
		StringBuilder str = new StringBuilder();
		str.Append(@"<table class='anstable' width='100%' cellspacing='0' cellpadding='0'><tr class='t'><td align=""center"">题号</td><td align=""center"">答题人数</td><td align=""center"">正确人数</td><td align=""center"">错误人数</td><td align=""center"">正确率</td></tr>");
		int k = 0;
		foreach (string s in ckarr)
		{
			int zqrs = 0;
			int cwrs = 0;
			int zrs = dtscore.Rows.Count;
			foreach (DataRow dr in dtscore.Rows)
			{
				string myanswer = Kesion.Exam.Function.GetAnswer(dr["Answer"].ToString(), tmid);
				var useranswers = string.Concat(myanswer, "※※※※※※※※※※※※※※※※※※※※").Split('※');
				if (Kesion.Exam.Function.CheckDa(useranswers[k], ckarr[k]))
				{
					zqrs++;
				}
				else
				{
					cwrs++;
				}
			}
			string zqltips = string.Empty;
			if (zqrs == 0 || zrs == 0)
			{
				zqltips = "<font color=brown>0%</font>";
			}
			else
			{
				float zqn = (float)zqrs / zrs * 100;

				if (zqn > 80)
				{
					zqltips = "<font color=green>" + zqn.ToString("f2") + "%</font>";
				}
				else if (zqn <= 50)
				{
					zqltips = "<font color=red><strong>" + zqn.ToString("f2") + "%</strong></font>";
				}
				else
				{
					zqltips = "<font color=brown>" + zqn.ToString("f2") + "%</font>";
				}
			}
			k++;
			str.AppendFormat(@"<tr class='split'>
                <td align=""center"">第{0}空</td>
                <td align='center'><a href='javascript:;' onclick=""showUserList({4},0,{0})"" title='查看答题的考生' style='color:green;'>{5}人</a></td>
                <td align='center'><a href='javascript:;' onclick=""showUserList({4},1,{0})"" title='查看答对的考生' style='color:green;'>{1}人</a></td>
                <td align='center'><a href='javascript:;' onclick=""showUserList({4},2,{0})"" title='查看答错的考生' style='color:green;'>{2}人</a></td>
                <td align='center' style='color:brown'>{3}</td></tr>", k, zqrs, cwrs, zqltips, tmid, zqrs + cwrs);
		}
		str.AppendLine("</table>");
		return str.ToString();
		#endregion
	}

</script>
<%
    if (!isLoadPage) return;
%>
<div class="t_tybox">

		<script>
			document.title = '卷面分析[<%=dr!=null?dr["title"].ToString():String.Empty %>] ';
			var box = null;
			function openWin(title, url, width, height) {
				if (width == null) width = 760;
				if (height == null) height = 450;
				box = jQuery.dialog.open(url, {
					id: 'topbox', lock: true, title: title, width: width, height: height, close: function () {

					}
				});
			}
			function showUserList(tmid, flag, tkn) {
				openWin('查看题号[' + tmid + ']试题的考生答题详情', 'tcSJDTUserList.aspx?tkn=' + tkn + '&sjid=<%=sjId %>&tmid=' + tmid + '&flag=' + flag);
		   }
		</script>
		<div id="manage_top"> 
			&nbsp;&nbsp;
			卷面分析[<span style="color:Red"><%=dr!=null?dr["title"].ToString():String.Empty %></span>]   
			&nbsp;限制时间：<%=dr!=null?dr["kssj"].ToString():string.Empty %>分钟 
		</div>     
		<style>
		.anstable{border:#E5E5E5 1px solid;padding:1px;width:90%;}
		.anstable split.td{background:#FAFAFA;font-size:12px;padding-left:12px;}
		.anstable .t{background:#DBE9F6;height:28px;line-height:28px;}
		.anstable .t td{font-size:12px;padding-left:12px;padding-right:12px;}
		.border tr.sort td.title_box{ border-bottom:1px solid #eee;}
		.showArea .exam_xiaoti td{ border-bottom:1px solid #eee;}
		.showArea .exam_xiaoti td .anstext{width: 280px;height: 31px;outline: none;line-height: 31px;border: 1px solid #dce2e4;border-radius: 3px;padding-left: 10px;}
		</style>
		<div style="margin:10px"></div>
		<div class="message">
			<div class="ty_title">整体统计情况</div> 
			试卷总分：<span style="color:Green; margin:0 5px;"><%=dr != null ? dr["score"].ToString() : string.Empty%></span>分&nbsp;
			参加人数：<span style="color:Green; margin:0 5px;"><%=Kesion.NET.WebSite.Admin.Exam.Admin_ZGSJTotal.GetEnterExamNum(sjId)%></span>人 已批改<span style="color:Green; margin:0 5px;"><%=Kesion.NET.WebSite.Admin.Exam.Admin_ZGSJTotal.GetExamPGNum(sjId)%></span>份 平均得分<span style="color:Green; margin:0 5px;"><%=Kesion.NET.WebSite.Admin.Exam.Admin_ZGSJTotal.GetAgvScore(sjId)%></span>分
			<%=TJQK%>  
		</div>  
		<div class="blank10"></div>
		
		  <table width="100%" border="0" cellpadding="0" cellspacing="0" class="border">
		

		  <%
					 string sqlStr = string.Empty;
					 if (isRndSj == 1)
					 {
						 sqlStr = "SELECT id as typeid,title as typename,orderid,tmids,fs,num,intro From [KS_SJRndZJ] Where SJID=" + sjId + " order by orderid,id";
					 }
					 else
					 {
						 sqlStr = "SELECT * From [KS_SJTMType] Where SJID=" + sjId + " order by orderid,typeid";
					 }
					 using (DataTable dt = DataFactory.GetDataTable(sqlStr))
					 {
						 if (dt.Rows.Count > 0)
						 {
							 foreach (DataRow drr in dt.Rows)
							 {
								 KSCMS.W(" <TR class=\"CTitle sort topbar\"> <td align=\"center\" nowrap class=\"title_box\" style=\"display:none;\"><span style='cursor:pointer' id='t" + drr["typeid"].ToString() + "' class=\"toggleArea\" onclick=\"showToggle(" + drr["typeid"].ToString() + ");\"></span></td>" +
				 "<td colspan=\"7\" style=\"text-align:left;padding-left:10px;\" class=\"title_box\"><strong>第" + UtilsAutoID.CapitalFigures(Utils.StrToInt(Convert.ToString(drr["orderid"]), 1) - 1) + "大题&nbsp;" + drr["typename"] + "<div style=\"display:none\">" + drr["intro"] + " </div> </strong> </td> </TR>");
								 KSCMS.W(" <tbody id='xt" + drr["typeid"] + "' class=\"showArea\"><tr class=\"title\" align=\"center\" >" +
					 "<td style=\"color:#647171;\" width=\"10%\">题号</td><td width=\"40%\" style=\"color:#647171;\">内容</td><td width=\"10%\" style=\"color:#647171;\">分数</td><td width=\"10%\" style=\"color:#647171;\">答题人数</td><td width=\"10%\" style=\"color:#647171;\">正确人数</td>" +
					" <td width=\"10%\" style=\"color:#647171;\">错误人数</td><td width=\"10%\" style=\"color:#647171;\">正确率</td> </tr>");
								 string typeId = Convert.ToString(drr["TypeID"]);
								 string tmIds = string.Empty;
								 string strSQL = string.Empty;
								 if (isRndSj == 1)
								 {
									 tmIds = Utils.FilterIDs(Convert.ToString(drr["tmids"]));
									 strSQL = "select * from KS_SJTK where tmid in(" + tmIds + ") order by orderid,tmid desc";
								 }
								 else
								 {
									 strSQL = "select * from KS_SJTK where typeid=" + typeId + " order by orderid,tmid desc";
								 }

								 using (DataTable dts = DataFactory.GetDataTable(strSQL))
								 {
									 if (dts.Rows.Count > 0)
									 {
										 foreach (DataRow drs in dts.Rows)
										 {
											 KSCMS.W(" <tr class=\"tdbg exam_xiaoti\"> <td style=\"text-align:center\">" + drs["OrderID"].ToString() + "</td>" +
						  "<td><a href=\"javascript:;\" onClick=\"jQuery('#con" + drs["tmid"] + "').toggle();\" style=\"color: #98a4a4;\">显示/关闭题目内容</a>" +
							  "<div id='con" + drs["tmid"] + "' style=\"display:none; text-align:left; margin-top:10px;\"> <strong>" + drs["title"] + "</strong><br />+ " + Kesion.Exam.Function.ReplaceTKTTags(Utils.replaceBR(Convert.ToString(drs["dajs"].ToString())), 1, 1) +
								 " <div style='height:20px;border-bottom:1px dashed #cccccc;overflow:hidden'></div>" +
									  "<span class=\"tips\"> <div>参考答案： " + Kesion.Exam.Function.GetCKDA(Utils.StrToInt(drs["tmtype"]), Convert.ToString(drs["answer"])) + "</div>" + GetXTQK(Utils.StrToInt(drs["tmType"]), Utils.StrToInt(drs["optionNum"]), Utils.StrToInt(drs["tmid"])) +
										  (string.IsNullOrEmpty(Convert.ToString(drs["dajs"])) ? "" : "答案解释：") + Utils.replaceBR(Convert.ToString(drs["dajs"])) + " </span> </div></div></td>" +
						  "<td align=\"center\" style=\"height:30px;\">" + drs["TMFS"].ToString() + "分</td>" + GetUserAnswerList(Convert.ToInt16(drs["TmType"]), Convert.ToInt32(drs["tmid"]), Convert.ToString(drs["answer"]), Convert.ToString(drs["tmfs"])) + " </tr>");
										 }
									 }
								 }
								 KSCMS.W("</tbody>");
							 }
						 }
						 else
						 {
							 KSCMS.W("<tr class=\"tdbg\"><td class=\"splittd\" style=\"text-align:center\" colspan=\"10\" ></td> </tr> ");
						 }
					 }
		
		 %>
		  </table>

		<div class="noprint">
			<table width="100%" align="center" border="0" cellpadding="0" cellspacing="0" class="CTable"> 
				<tr>
					<td align="right" class="lefttd" height="50"></td>
					<td class="righttd" style="height:50px">
						<div class="cz_btn nofloat">
						  <input type="hidden" name="ZGTDFQK" id="ZGTDFQK" />
						  <input type="hidden" name="TotalScore" id="TotalScore" />         
						  <span><input type="button" class="button" value=" 打印 " onclick="window.print()" /></span>
						
						</div>
					</td>
				</tr>
			</table>
		</div>
		<asp:Panel ID="Panel1" runat="server" Height="30px" CssClass="noprint" Width="100%">
			<script type="text/javascript">
				showtips('<span class="state"><strong>操作说明：</strong><br /></span>系统不能统计主观题的正确率，客观题的正确率已作了统计。');
			</script>
			<div class="blank20"></div>
		</asp:Panel>

</div>    