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

教育系统应用

开发平台:

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