<%@page contentType="text/html;charset=GBK"%>
<%@page import="java.sql.*"%>
<jsp:useBean id="conn" scope="page" class="test.ConnOracle"/>
<%@ include file="/public/checkvalidstudent.jsp"%>
<%!
    ResultSet rs     = null;                      // 执行Sql语句返回的结果集
	String sql = "";
	String paperid = "";
	String student_answer[] = new String[100];
	String paper_answer[]  = new String[100];
	int correct_question = 0;                     // 记录共答对了多少道题
	int score = 0;                                // 总分
%>
<%
    // session paperid的值在exam2.jsp中设置
	paperid = (String) session.getAttribute("paperid");
	if(paperid == null) {
	    out.println("读取试卷信息错误！");
		return;
	}
	sql = "select * from test_paper_info where PAPER_ID=" + session.getAttribute("paperid");
	rs = conn.executeQuery( sql );

	int answer_index = 0;
	String tmpbuffer[] = new String[4];        // 为了处理多选题，而使用的一个字符串数组

	correct_question=0;
	while(rs.next()) {
		// 对单选题的处理
		student_answer[answer_index] = "";
		if(rs.getInt("selectid") == 1) {
		    student_answer[answer_index] = request.getParameter("" + rs.getInt("questionid") );
		}else {  // 对多选题的处理
		    tmpbuffer = request.getParameterValues("" + rs.getInt("questionid") );
			if(tmpbuffer!=null) {
				for(int i=0; i<tmpbuffer.length; i++) {
					if(tmpbuffer[i] != null)
					   student_answer[answer_index] += tmpbuffer[i];
				}
			}
		}
		paper_answer[answer_index] = rs.getString("answer");
		if(student_answer[answer_index]!=null && student_answer[answer_index].equals(paper_answer[answer_index]))
			correct_question++;
		answer_index++;
	}
	score = (int) (correct_question * 100 / answer_index);

// 下面将学生考试结果保存到test_result表中
   sql = "insert into test_result (testid,studentid,score,starttime,classid) values (";
   sql+= "test_seq_result.nextval," + (String)(session.getAttribute("studentid")) ;
   // 下面用到的classid在页面exam2.jsp中赋值
   sql+= "," + score + ",sysdate,'" + (String)(session.getAttribute("classid")) + "')";
   conn.executeUpdate( sql );
%>
<html>
<head>
  <title>入学测试得分</title>
  <link rel="stylesheet" href="../public/style.css">
</head>
	
<body bgcolor="#FFFFFF" background="../images/bg3.gif">
<center>
<br><br><br><br><br><br><br><br><br>
<table class=dbt2 width=640 cellSpacing=1 cellPadding=3 border=0>
  <tr class=dbt3>
    <td colspan=2 align="center">入学测试得分</td>
  </tr>
 
  <tr class=dbt4>
    <td width="18%">id号：</td>
    <td width="82%" align="center">
	<%
	if(session.getAttribute("studentid") != null)
	    out.println(session.getAttribute("studentid"));
	%>
	</td>
  </tr>
  <tr class=dbt4>
    <td width="18%">姓名：</td>
    <td width="82%" align="center">
	<%
	if(session.getAttribute("studentname") != null)
	    out.println(session.getAttribute("studentname"));
	%>
	</td>
  </tr>
  <tr class=dbt4>
    <td width="18%">考试用时：</td>
    <td width="82%" align="center">
	<%
	if(request.getParameter("totaltime")!=null)
	    out.println(request.getParameter("totaltime"));
	%>分钟</td>
  </tr>
  <tr class=dbt4>
    <td width="18%">考试成绩：</td>
    <td width="82%" align="center">答对<%=correct_question%>道题，分数为<%=score%>分</td>
  </tr>
  <tr class=dbt4>
    <td align="center" colspan="2">&nbsp;谢谢你参加此次考试！</td>
  </tr>

  <tr class=dbt4>
    <td align="center" colspan="2">【<a href="javascript:window.close()">关闭窗口</a>】</td>
  </tr>
</table>
</center>
</body>
</html>
