updatetest_mn.jsp
上传用户:nbluoke
上传日期:2013-08-09
资源大小:4851k
文件大小:19k
源码类别:

教育系统应用

开发平台:

WORD

  1. <%@ page contentType="text/html; charset=gb2312" language="java"  errorPage="" %>
  2. <%@ page import="java.sql.*"%>
  3. <%@ page import="jinghua.*"%>
  4. <%@ page import="java.text.*"%>
  5. <%@ page import="java.util.*"%>
  6. <script language="javascript">
  7. </script>
  8. <jsp:useBean id="DBCon" class="jinghua.JinghuaConn" scope="session">
  9. </jsp:useBean>
  10. <jsp:useBean id="transString" class="jinghua.TransformString" scope="page">
  11. </jsp:useBean>
  12. <%!
  13. String questTypeID=null;
  14. boolean debug=true;
  15. int subjectID=-1;
  16. String gradeID=null;
  17. int mntestID=-1;
  18. String knowPointID1=null;
  19. String knowPointID2=null;
  20. String knowPointID3=null;
  21. StringBuffer msgsb=new StringBuffer("OK");
  22. String sql="";
  23. String temp="";
  24. int teaID=-1;
  25. int ID=-1;//新提交试题或试卷的ID
  26. Connection con=null;
  27. //生成试题分类编号
  28. public String QuestClassCode(Connection con,int SubjectID,String gradeID,
  29.         String KnowPointID,String testpoint,String QuestType,  String PostHard){
  30.   int num=0;
  31.   StringBuffer bs=new StringBuffer();
  32.   String sql="";
  33.   try{
  34.   Statement stmt=con.createStatement();
  35.   sql="select count(*) as num from question where subjectid="+SubjectID+
  36.             " and knowpointid1="+KnowPointID+
  37.             " and questtype="+ QuestType+
  38.             " and prehard='" +PostHard+"'";
  39.   ResultSet rs=stmt.executeQuery(sql);
  40.   if(rs.next()){
  41.     num=rs.getInt(1);
  42.   }
  43.   num++;
  44.   //格式化第几道题
  45.   DecimalFormat df=new DecimalFormat("00000");
  46.   String numNF=df.format(num);
  47.   //NumberFormat nf = NumberFormat.getNumberInstance ();
  48.  // nf.
  49.   //nf.setMinimumIntegerDigits(5);//整数部分最小的长度不能少于2,如果少了则补另.
  50.   //String numNF=nf.format(num);
  51.   rs.close();
  52.   stmt.close();
  53.   sql="select subject.note as sn, knowpoint.note as kn,grade.note as gn "+
  54.     " from subject,knowpoint,grade "+
  55.       "  where subject.id="+SubjectID+
  56.       " and knowpoint.id="+KnowPointID+" and grade.id="+"'"+gradeID+"'";
  57.   stmt=con.createStatement();
  58.   rs=stmt.executeQuery(sql);
  59.   if (rs.next()){
  60.     String temp;
  61.     temp=rs.getString("sn");
  62.     bs.append(temp);
  63.     bs.append(rs.getString("gn") );
  64. df=new DecimalFormat("00");
  65. temp=df.format(Integer.valueOf(testpoint));
  66.     bs.append(temp);
  67.     bs.append(QuestType);
  68.     bs.append(PostHard);
  69.     bs.append(numNF);
  70.     temp=rs.getString("kn");
  71.     bs.append(temp);
  72.     rs.close();
  73.     stmt.close();
  74.   }
  75.   }catch(Exception se){
  76.     msgsb.append(se.toString());
  77. msgsb.append("<br>"+"SQL="+sql);
  78.   }
  79.   return bs.toString();
  80. }
  81. synchronized public int submitOne(String strHtml,String answer,float mark,String difficulty,Connection con,boolean commit){
  82. //commit=true则需要在一道题提交完后,使用commit来完成提交
  83.     Statement stmt=null;
  84.     strHtml=strHtml.trim();
  85.     int tipPos=-1;
  86.     String tip="";
  87.     //检查格式:
  88.     if(strHtml.startsWith("@@")){
  89.         msgsb.append("<BR>单个试题的数据格式错误,请检查!");
  90.         return -1;
  91.     }else{
  92. //检查是否包含了文件
  93.     }
  94.     //检查答案:
  95.     if(answer.trim()==""){
  96.         msgsb.append("<BR>试题答案为空,请检查!"+"原试题为:<br>"+strHtml);
  97.         return -1;
  98.     }else if (answer.trim().length()!=1){
  99.         msgsb.append("<BR>试题答案长度不为1,请检查!"+"原试题为:<br>"+strHtml);
  100.         return -1;
  101.     }else{
  102.         String strAnswer="ABCDEFGH";
  103.         if(strAnswer.indexOf(answer)<0){
  104.             msgsb.append("<BR>单选试题的答案长度不为“ABCDEFGH”中的字符,请检查!"+"原试题为:<br>"+strHtml);
  105.             return -1;
  106.         }
  107.     }
  108.     //检查难易度
  109.     if(difficulty.trim()==""){
  110.         msgsb.append("<BR>试题难易度为空,请检查!"+"原试题为:<br>"+strHtml);
  111.         return -1;
  112.     }else if (difficulty.trim().length()!=1){
  113.         msgsb.append("<BR>试题难易度长度不为1,请检查!"+"原试题为:<br>"+strHtml);
  114.         return -1;
  115.     }else{
  116.         String strAnswer="ABCDE";
  117.         if(strAnswer.indexOf(difficulty)<0){
  118.             msgsb.append("<BR>单选难易度的答案长度不为“ABCDE”中的字符,请检查!"+"原试题为:<br>"+strHtml);
  119.             return -1;
  120.         }
  121.     }
  122.     tipPos=strHtml.indexOf("#tip");
  123.     if(tipPos>0){
  124.         tip=strHtml.substring(tipPos+4);//取提示
  125.         strHtml=strHtml.substring(0,tipPos);
  126.         tip=Tools.toSql(tip);
  127.         strHtml=Tools.toSql(strHtml);
  128.     }
  129. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  130. String now=df.format(new java.util.Date());//当前日期
  131. answer=answer.toUpperCase();
  132.     int newID=-1;
  133.     if(!strHtml.equals("")){
  134. try{
  135.         answer.toUpperCase();
  136.         difficulty.toUpperCase();
  137. //String QuestClassType=QuestClassCode(con,subjectID, gradeID, knowPointID1,testPointID1,
  138.         //        questTypeID,   difficulty);
  139. String QuestClassType="";
  140.     if(QuestClassType==null){
  141.     msgsb.append(" Error:试题编号生成错误n"+""+QuestClassType+strHtml);
  142.     }
  143. else
  144.     {
  145.     sql="insert into question(QuestID,Mark,Answer,TeacherID,`question`.`Source`, "+
  146.         "PubDate,QuestType,SubjectID,gradenote, "+
  147. "KnowPointID1,KnowPointID2,KnowPointID3,PreHard,Tip) "+ "values("+
  148.     "'"+QuestClassType+"',"+mark+",'"+answer+"',"+teaID+",3,'"+now+"',"+
  149. questTypeID+","+subjectID+","+"'"+gradeID+"'"+","+knowPointID1+","+knowPointID2+","+knowPointID3+
  150. ",'"+difficulty+"','"+tip+"'"+")";
  151.     con.setAutoCommit(!commit);
  152. stmt=con.createStatement();
  153. stmt.executeUpdate(sql);
  154.     stmt.close();
  155.     stmt=con.createStatement();
  156.     ResultSet rs=stmt.executeQuery("select LAST_INSERT_ID() as newid from question");
  157.     if(rs.next()){
  158.     newID=rs.getInt("newID");
  159.                 rs.close();
  160.     stmt.close();
  161.     }
  162. }
  163. }//end try
  164. catch(SQLException se){
  165.     System.out.print(se);
  166.             System.out.print(sql);
  167.             msgsb.append("试题入库出错:SQL="+sql+"n"+se.toString());
  168. }catch(Exception e){
  169.             msgsb.append("试题格式可能出错 strHtml=:"+strHtml);
  170. }
  171. if(newID!=-1 && msgsb.toString().equals("OK")  ) {
  172. try{//修改题干
  173. temp=jinghua.TransformString.TransformSelction(strHtml,newID);
  174. temp=Tools.toSql(temp);
  175.              if(temp==null)
  176.                 throw new Exception("题目没有数据");
  177. stmt=con.createStatement();
  178. sql="update question set QuestText='"+temp+"' where ID="+newID;
  179. stmt.executeUpdate(sql);
  180.             stmt.close();
  181. }
  182. catch(SQLException se){
  183.     System.out.print(se);
  184. msgsb.append("试题入库出错:SQL="+sql+"n"+se.toString());
  185.     }
  186.         catch(Exception se){
  187.     System.out.print(se);
  188. msgsb.append("试题入库出错:"+se.toString());
  189.         }
  190. }//end if
  191.     }else{
  192.         msgsb.append("提交试题出错:试题为空");
  193.     }
  194.     return newID;
  195. }//end submitOne
  196. //@@2. My pain _____apparent the moment I walked into the room. for the first man I met asked
  197. //sympathetically: "Are you feeling all right?"
  198. // A. must be  B. had  C. must have been  D. had to be
  199. //##A ##B  ##C  ##D
  200. //@A@5@E@ :@答案@分数@难度@
  201. synchronized public int submitAll(String strHtml,String Title,Connection con,javax.servlet.jsp.JspWriter out){
  202.         if(debug){
  203.             try{
  204.             out.print("<br> submitAll input:<BR>"+ strHtml);
  205.             }catch(Exception e){}
  206.         }
  207. String [] strArray=strHtml.split("@@");//"@@"为每道试题的分隔符
  208. String [] ansMark;
  209. String temp=null;
  210. StringBuffer bs=new StringBuffer();//试题ID#答案A#分数;
  211. int newID=-1,bin=-1,end=0;
  212. int newtestID=-1;
  213.         int i=-1;
  214. String ans="";
  215. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  216. String now=df.format(new java.util.Date());
  217. //先将所有试题提交
  218. //debug
  219. //out.println(now);
  220. //        out.print("strArray.length="+String.valueOf(strArray.length));
  221. try{
  222.             con.setAutoCommit(false);
  223.             int mark=0;
  224.             for( i=1;i<strArray.length;i++){//提交每一道试题
  225.                 if(debug){
  226.                  out.println("<br>test("+i+"):<br>"+strArray[i]);
  227.                 }
  228.              bin=strArray[i].indexOf("@");
  229.                 end=strArray[i].lastIndexOf("@");
  230.          temp=strArray[i].substring(bin+1,end+1);//答案
  231.                 ansMark=temp.split("@");
  232.                 int tippos=strArray[i].indexOf("#tip");
  233.                 String t1=strArray[i].substring(0,bin)+strArray[i].substring(tippos).trim();
  234.                 //将答案串从试题文本中删除,因为单个试题文本中没有这些信息
  235.                 //提交单个试题
  236.                     mark+=Float.parseFloat(ansMark[1]);
  237.                     try{
  238.                         newID=submitOne(t1,ansMark[0],Float.parseFloat(ansMark[1]),ansMark[2],con,true);
  239.                         if(debug && newID!=-1){
  240.                         out.println("<br>database submit success:newID="+newID);
  241.                     }
  242.                     if(newID!=-1){
  243.                         //试题ID@答案@分数@难易度;     一道试题的格式,以#号结束
  244.                         temp=String.valueOf(newID)+"@"+ansMark[0].trim() +"@"+ansMark[1].trim() +"@"+ansMark[2].trim() +"#";
  245.                      bs.append(temp);
  246.                     }else
  247.                         {
  248.                         throw new Exception("试题格式可能出错");
  249.                         }
  250.                     }catch(IllegalArgumentException ie)
  251.                     {
  252.                         msgsb.append("试题格式可能出错 test("+i+")=:"+t1+temp+"<br>"+ie);
  253.                     }
  254.         }
  255.          //提交试卷
  256.         if(msgsb.toString().equals("OK")){
  257.     try{
  258.          //   sql="insert into test(id,date,testtypeid,paper,title,subid,gradeid,status,teaid,validtime,testnum) values(38,'2003-09-18 14:10:44',1,'117@A@5@A#118@B@6@B#119@C@7@C#120@D@8@E#','',1,1,2,1,3,4)";
  259.          //   con.setAutoCommit(false);
  260.          //   stmt=con.createStatement();
  261.          //   stmt.executeUpdate(sql);
  262.          //   stmt.close();
  263. //
  264.          sql="insert into test(date,testtypeid,paper,title,subid,gradenote,status,teaid,validtime,testnum,"+
  265.                 "knowpointid1,knowpointid2,knowpointid3,mark) "+
  266.             "values('"+now+"',"+"1"+",'"+bs.toString() +"','"+Title+"',"+subjectID+","+"'"+gradeID+"'"+
  267.      ","+"3"+","+teaID+","+"3"+","+(strArray.length-1) +","+
  268.                 knowPointID1+","+knowPointID2+","+knowPointID3+","+mark+")";
  269.          //缺省testtypeid=1:单一的选择题;statue=2:已经发布;status=3为模拟考试
  270.             Statement stmt=con.createStatement();
  271.             stmt.executeUpdate(sql);
  272.             stmt.close();
  273.             if(debug){
  274.                 Tools.debugmsg("<br>insert into test:SQL="+sql,out);
  275.             }
  276.             stmt=con.createStatement();
  277.             ResultSet rs=stmt.executeQuery("select LAST_INSERT_ID() as newid from question");
  278.             if(rs.next()){
  279.                 newtestID=rs.getInt("newID");
  280.                 rs.close();
  281.                 stmt.close();
  282.                 if(newtestID>0){//将这一试卷增加到mn_test表中的相应记录中去
  283.                 if(debug){
  284.                     out.print("<br>mn_testID="+mntestID);
  285.                 }
  286.                 int test_num=-1;
  287.                 String test_paper="";
  288.                 int sub_num=-1;
  289.                 String subs="";
  290. int mntotalmark=0;
  291.                 sql="select * from mn_test where id="+mntestID;
  292.                 try{
  293.                 stmt=con.createStatement();
  294.                 rs=stmt.executeQuery(sql);
  295.                 if(rs.next()){
  296.                     test_num=rs.getInt("test_num");
  297.                     test_paper=rs.getString("test_paper");
  298.                     sub_num=rs.getInt("sub_num");
  299.                     subs=rs.getString("subs");
  300. mntotalmark=rs.getInt("mntotalmark");
  301.                     if(test_paper==null){
  302.                         test_paper="";
  303.                     }
  304.                     if(subs==null){
  305.                         subs="";
  306.                     }
  307.                 }
  308.                 rs.close();
  309.                 stmt.close();
  310.                 if(debug){
  311.                     out.print("<br>mn_test:test_num="+test_num);
  312.                     out.print("<br>mn_test:test_paper="+test_paper);
  313.                     out.print("<br>mn_test:sub_num="+sub_num);
  314.                     out.print("<br>mn_test:subs="+subs);
  315.                 }
  316.                 //add test
  317. if(test_paper.equals("")){
  318.                  test_paper+=String.valueOf(newtestID);//一份试卷以","分隔
  319. }else{
  320.     test_paper+=","+String.valueOf(newtestID);//一份试卷以","分隔
  321. }
  322.                 test_num++;
  323.                 //add sub
  324.                 if(subs.indexOf(String.valueOf(subjectID))<0){//new sub
  325. mntotalmark+=mark;
  326. if(subs.equals("")){
  327. subs+=String.valueOf(subjectID);
  328. }else{
  329.                     subs+=","+String.valueOf(subjectID);
  330. }
  331.                     sub_num++;
  332.                 }
  333.                  if(debug){
  334.                     out.print("<br>mn_test:test_num="+test_num);
  335.                     out.print("<br>mn_test:test_paper="+test_paper);
  336.                     out.print("<br>mn_test:sub_num="+sub_num);
  337.                     out.print("<br>mn_test:subs="+subs);
  338.                 }
  339.                 sql=" update mn_test set testpeople= 0, test_num="+test_num+",test_paper='"+test_paper+"' ,"+
  340.                     " sub_num="+sub_num+",subs='"+subs+"',"+
  341. " mntotalmark="+mntotalmark+
  342.                     " where id="+mntestID;
  343.                 stmt=con.createStatement();
  344.                 stmt.executeUpdate(sql);
  345.                 sql="delete from mn_history where mn_test_id="+mntestID;
  346.                 stmt=con.createStatement();
  347.                 stmt.executeUpdate(sql);
  348.                 }catch(SQLException se){
  349.                     out.print("<BR>update mn_test error: sql="+sql);
  350.                     out.print("<BR>"+se.toString());
  351.                     msgsb.append("update mn_test error: sql="+sql);
  352.                     try{
  353.                 con.rollback();
  354.                     }catch(Exception e1){
  355.                         msgsb.append("<BR>rollbackcbd:SQL="+sql+"n"+e1.toString());
  356.                     }
  357.                 }
  358.                 catch(Exception e){
  359.                     out.print("<BR>update mn_test error: sql="+sql);
  360.                     out.print("<BR>"+e.toString());
  361.                     msgsb.append("update mn_test error: sql="+sql);
  362.                     try{
  363.                 con.rollback();
  364.                     }catch(Exception e1){
  365.                     msgsb.append("<BR>rollbackcbd:SQL="+sql+"n"+e1.toString());
  366.                     }
  367.                 }
  368.             }
  369.             }
  370.             con.commit();
  371.             if(debug){
  372.                 out.print("<br> new test ID is:"+newtestID);
  373.                 stmt=con.createStatement();
  374.                 rs=stmt.executeQuery("select id,paper,title from test where id="+newtestID);
  375.                 if (rs.next()){
  376.                     Tools.debugmsg("<br> the newest paper is:"+rs.getString("paper"),out);
  377.                     Tools.debugmsg("<br> the title of the paper is:"+rs.getString("title"),out);
  378.                 }
  379.                 rs.close();
  380.                 stmt.close();
  381.             }
  382.         }
  383.     catch(SQLException e){
  384.             try{
  385.         con.rollback();
  386.             }catch(Exception e1){
  387.                 msgsb.append("<BR>rollbackcbd:SQL="+sql+"n"+e1.toString());
  388.             }
  389.             msgsb.append("insert Error:SQL="+sql+"n"+e.toString());
  390.     }
  391.         catch(Exception e){
  392.             try{
  393.         con.rollback();
  394.             }catch(Exception e1){
  395.                 msgsb.append("<BR>rollbackcbd:SQL="+sql+"n"+e1.toString());
  396.             }
  397.             msgsb.append("insert Error:SQL="+sql+"n"+e.toString());
  398.     }
  399. }else{
  400.             try{
  401.             con.rollback();
  402.             }catch(Exception e2){
  403.                 msgsb.append("rollback:SQL="+sql+"n"+e2.toString());
  404.             }
  405.             msgsb.append("Single test is error i="+i);
  406. }
  407. }catch(Exception e){
  408.             try{
  409.         con.rollback();
  410.             }catch(Exception e1){
  411.                 msgsb.append("<BR>rollbackcbd:SQL="+sql+"n"+e1.toString());
  412.             }
  413.             msgsb.append("<br>Batch submit Errorn"+
  414.                 "<br>strArray["+i+"]="+strArray[i]+"<br>"+e.toString() );
  415.            Tools.debugmsg("<br>i="+i,out);
  416. }
  417. return newtestID;
  418. }
  419. %>
  420. <html>
  421. <head>
  422. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  423. <title>试题提交</title>
  424. </head>
  425. <body>`
  426. <%
  427. try{
  428. //UN_debug
  429. msgsb.delete(0,msgsb.length() );
  430. msgsb.append("OK");
  431. try{
  432.     String TeacherID=(String)session.getAttribute("teacherID");
  433.     //Tools.showalert("teaID="+TeacherID,out);
  434.     //Tools.showalert("msgdb="+msgsb.toString(),out);
  435.     //Tools.showalert(TeacherID,out);
  436.     teaID=Integer.parseInt(TeacherID);
  437. }catch(Exception e){
  438.     Tools.showalert("您还没有登录,或连接超时,请重新登录!",out);
  439.     response.sendRedirect("../login.jsp");
  440.     msgsb.append("teacherID Errorn");
  441.     Tools.showJS("window.open('../login.jsp','_self')",out);
  442.     //response.sendRedirect("../login.jsp");
  443.     Tools.goback(out);
  444. }
  445. if (msgsb.toString().equals("OK"))
  446. {
  447. questTypeID=""+request.getParameter("QuestType");//试题类型ID
  448. subjectID=Integer.parseInt(request.getParameter("Subject"));//科目ID
  449. gradeID=""+request.getParameter("Grade");//年级ID
  450. mntestID=Integer.parseInt(request.getParameter("mntest"));//模拟考试ID
  451. knowPointID1=""+request.getParameter("KnowPoint1");//知识点ID
  452. knowPointID2=""+request.getParameter("KnowPoint2");//知识点ID
  453. knowPointID3=""+request.getParameter("KnowPoint3");//知识点ID
  454. String strHtml=""+request.getParameter("Content");//获取提交的试题的HTML
  455. //debug
  456. con=DBCon.getConnection();
  457. //
  458. strHtml=Tools.codestring(strHtml);//转换字符集
  459. String group=""+request.getParameter("group");
  460. if(group.equals("single")){//提交单个试题
  461. //ID=submitOne(strHtml,answer,mark,difficulty,con,false);
  462. }else{
  463. if(group.equals("suit") ){
  464.     String Title=""+request.getParameter("suit_title");
  465.             Title=Tools.codestring(Title);
  466.             ID=submitAll(strHtml,Title,con,out);
  467. }
  468. }
  469. %>
  470. <%temp=msgsb.toString();
  471. if(debug){
  472.     out.print("<br><br><br>New Information");
  473.     out.print(temp);
  474. }
  475. if(temp.equals("OK")){
  476. //提交成功
  477.         if(group.equals("single")){//提交单个试题
  478.             Tools.showalert(" 试题提交成功,新的试题ID为:"+ID,out);
  479.             Tools.goback(out);
  480.         }else if(group.equals("suit")){
  481.             Tools.showalert(" 试卷提交成功,新的试卷ID为:"+ID,out);
  482.             Tools.goback(out);
  483.         }
  484. %>
  485. <%
  486. }
  487. else{
  488. System.out.print(msgsb.toString());
  489. %>
  490. <SCRIPT language="javascript">
  491. alert("试题未能提交成功,请检查提交信息或与系统管理员联系");
  492. //history.go(-1);
  493. </SCRIPT>
  494. <%
  495. }
  496. %>
  497. </body>
  498. </html>
  499. <%
  500. }
  501. %>
  502. <%}
  503. catch(Exception e){
  504.     System.out.print(e.toString());
  505. }
  506. finally{
  507.     DBCon.dropConnection();
  508. }
  509. %>