updatetest_free.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.             //source=1为自由测试
  148.     sql="insert into question(QuestID,Mark,Answer,TeacherID, `question`.`Source`,"+
  149.         "PubDate,QuestType,SubjectID,gradenote, "+
  150. "KnowPointID1,KnowPointID2,KnowPointID3,PreHard,Tip) "+ "values("+
  151.     "'"+QuestClassType+"',"+mark+",'"+answer+"',"+teaID+",1,'"+now+"',"+
  152. questTypeID+","+subjectID+","+"'"+gradeID+"'"+","+knowPointID1+","+knowPointID2+","+knowPointID3+
  153. ",'"+difficulty+"','"+tip+"'"+")";
  154.     con.setAutoCommit(!commit);
  155. stmt=con.createStatement();
  156. stmt.executeUpdate(sql);
  157.     stmt.close();
  158.     stmt=con.createStatement();
  159.     ResultSet rs=stmt.executeQuery("select LAST_INSERT_ID() as newid from question");
  160.     if(rs.next()){
  161.     newID=rs.getInt("newID");
  162.                 rs.close();
  163.     stmt.close();
  164.     }
  165. }
  166. }//end try
  167. catch(SQLException se){
  168.     System.out.print(se);
  169.             System.out.print(sql);
  170.             msgsb.append("试题入库出错:SQL="+sql+"n"+se.toString());
  171. }catch(Exception e){
  172.             msgsb.append("试题格式可能出错 strHtml=:"+strHtml);
  173. }
  174. if(newID!=-1 && msgsb.toString().equals("OK")  ) {
  175. try{//修改题干
  176. temp=jinghua.TransformString.TransformSelction(strHtml,newID);
  177. temp=Tools.toSql(temp);
  178.             if(temp==null)
  179.                 throw new Exception("题目没有数据");
  180. stmt=con.createStatement();
  181. sql="update question set QuestText='"+temp+"' where ID="+newID;
  182. stmt.executeUpdate(sql);
  183.             stmt.close();
  184. }
  185. catch(SQLException se){
  186.     System.out.print(se);
  187. msgsb.append("试题入库出错:SQL="+sql+"n"+se.toString());
  188.     }
  189.         catch(Exception se){
  190.     System.out.print(se);
  191. msgsb.append("试题入库出错:"+se.toString());
  192.         }
  193. }//end if
  194.     }else{
  195.         msgsb.append("提交试题出错:试题为空");
  196.     }
  197.     return newID;
  198. }//end submitOne
  199. //@@2. My pain _____apparent the moment I walked into the room. for the first man I met asked
  200. //sympathetically: "Are you feeling all right?"
  201. // A. must be  B. had  C. must have been  D. had to be
  202. //##A ##B  ##C  ##D
  203. //@A@5@E@ :@答案@分数@难度@
  204. synchronized public int submitAll(String strHtml,String Title,Connection con,javax.servlet.jsp.JspWriter out){
  205.         if(debug){
  206.             try{
  207.             out.print("<br> submitAll input:<BR>"+ strHtml);
  208.             }catch(Exception e){}
  209.         }
  210. String [] strArray=strHtml.split("@@");
  211. String [] ansMark;
  212. String temp=null;
  213. StringBuffer bs=new StringBuffer();//试题ID#答案A#分数;
  214. int newID=-1,bin=-1,end=0;
  215. int newtestID=-1;
  216.         int i=-1;
  217. String ans="";
  218. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  219. String now=df.format(new java.util.Date());
  220. //先将所有试题提交
  221. //debug
  222. //out.println(now);
  223. //        out.print("strArray.length="+String.valueOf(strArray.length));
  224. //        if (debug){
  225. //            for(i=0;i<strArray.length;i++){
  226. //                Tools.debugmsg("<br>strArray["+i+"]="+strArray[i]+"<br>",out);
  227. //            }
  228. //        }
  229. try{
  230.             con.setAutoCommit(false);
  231.             int mark=0;
  232.             for( i=1;i<strArray.length;i++){//提交每一道试题
  233.                 if(debug){
  234.                  out.println("<br>test("+i+"):<br>"+strArray[i]);
  235.                 }
  236.              bin=strArray[i].indexOf("@");
  237.                 end=strArray[i].lastIndexOf("@");
  238.          temp=strArray[i].substring(bin+1,end+1);//答案
  239.                 ansMark=temp.split("@");
  240.                 int tippos=strArray[i].indexOf("#tip");
  241.                 String t1=strArray[i].substring(0,bin)+strArray[i].substring(tippos).trim();
  242.                 //将答案串从试题文本中删除,因为单个试题文本中没有这些信息
  243.                 //提交单个试题
  244.                 mark+=Float.parseFloat(ansMark[1]);
  245.                 try{
  246.                     newID=submitOne(t1,ansMark[0],Float.parseFloat(ansMark[1]),ansMark[2],con,true);
  247.                     if(debug && newID!=-1){
  248.                         out.println("<br>database submit success:newID="+newID);
  249.                     }
  250.                     if(newID!=-1){
  251.                         //试题ID@答案@分数@难易度;     一道试题的格式,以#号结束
  252.                         temp=String.valueOf(newID)+"@"+ansMark[0].trim() +"@"+ansMark[1].trim() +"@"+ansMark[2].trim() +"#";
  253.                      bs.append(temp);
  254.                     }else
  255.                         {
  256.                         throw new Exception("试题格式可能出错");
  257.                         }
  258.                 }catch(IllegalArgumentException ie)
  259.                     {
  260.                         msgsb.append("试题格式可能出错 test("+i+")=:"+t1+temp+"<br>"+ie);
  261.                         out.print("<br>"+msgsb.toString());
  262.                     }
  263.         }
  264.          //提交试卷
  265.         if(msgsb.toString().equals("OK")){
  266.     try{
  267.          //   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)";
  268.          //   con.setAutoCommit(false);
  269.          //   stmt=con.createStatement();
  270.          //   stmt.executeUpdate(sql);
  271.          //   stmt.close();
  272. //status=4:自由组题
  273.          sql="insert into test(date,testtypeid,paper,title,subid,gradenote,status,teaid,validtime,testnum,"+
  274.                 "knowpointid1,knowpointid2,knowpointid3,mark) "+
  275.             "values('"+now+"',"+"1"+",'"+bs.toString() +"','"+Title+"',"+subjectID+","+"'"+gradeID+"'"+
  276.      ","+"4"+","+teaID+","+"3"+","+(strArray.length-1) +","+
  277.                 knowPointID1+","+knowPointID2+","+knowPointID3+","+mark+")";
  278.          //缺省testtypeid=1:单一的选择题;statue=2:已经发布;validtiem=3:有效期为3年
  279.             Statement stmt=con.createStatement();
  280.             stmt.executeUpdate(sql);
  281.             stmt.close();
  282.             if(debug){
  283.                 Tools.debugmsg("<br>insert into test:SQL="+sql,out);
  284.             }
  285.             stmt=con.createStatement();
  286.             ResultSet rs=stmt.executeQuery("select LAST_INSERT_ID() as newid from question");
  287.             if(rs.next()){
  288.                 newtestID=rs.getInt("newID");
  289.                 rs.close();
  290.                 stmt.close();
  291.             }
  292.             con.commit();
  293.             if(debug){
  294.                 out.print("<br> new test ID is:"+newtestID);
  295.                 stmt=con.createStatement();
  296.                 rs=stmt.executeQuery("select id,paper,title from test where id="+newtestID);
  297.                 if (rs.next()){
  298.                     Tools.debugmsg("<br> the newest paper is:"+rs.getString("paper"),out);
  299.                     Tools.debugmsg("<br> the title of the paper is:"+rs.getString("title"),out);
  300.                 }
  301.                 rs.close();
  302.                 stmt.close();
  303.             }
  304.         }
  305.     catch(SQLException e){
  306.             try{
  307.         con.rollback();
  308.             }catch(Exception e1){
  309.                 msgsb.append("<BR>rollbackcbd:SQL="+sql+"n"+e1.toString());
  310.             }
  311.             msgsb.append("insert Error:SQL="+sql+"n"+e.toString());
  312.     }
  313. }else{
  314.             try{
  315.             con.rollback();
  316.             }catch(Exception e2){
  317.                 msgsb.append("rollback:SQL="+sql+"n"+e2.toString());
  318.             }
  319.             msgsb.append("Single test is error i="+i);
  320. }
  321. }catch(Exception e){
  322.             try{
  323.         con.rollback();
  324.             }catch(Exception e1){
  325.                 msgsb.append("<BR>rollbackcbd:SQL="+sql+"n"+e1.toString());
  326.             }
  327.             msgsb.append("<br>Batch submit Errorn"+
  328.                 "<br>strArray["+i+"]="+strArray[i]+"<br>"+e.toString() );
  329.            Tools.debugmsg("<br>i="+i,out);
  330. }
  331. return newtestID;
  332. }
  333. %>
  334. <html>
  335. <head>
  336. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  337. <title>试题提交</title>
  338. </head>
  339. <body>`
  340. <%
  341. try{
  342. //UN_debug
  343. msgsb.delete(0,msgsb.length() );
  344. msgsb.append("OK");
  345. try{
  346.     String TeacherID=(String)session.getAttribute("teacherID");
  347.     //Tools.showalert("teaID="+TeacherID,out);
  348.     //Tools.showalert("msgdb="+msgsb.toString(),out);
  349.     //Tools.showalert(TeacherID,out);
  350.     teaID=Integer.parseInt(TeacherID);
  351. }catch(Exception e){
  352.     Tools.showalert("您还没有登录,或连接超时,请重新登录!",out);
  353.     //response.sendRedirect("../login.jsp");
  354.     msgsb.append("teacherID Errorn");
  355.     Tools.showJS("window.open('../login.jsp','_self')",out);
  356.     //response.sendRedirect("../login.jsp");
  357. //    Tools.goback(out);
  358. }
  359. if (msgsb.toString().equals("OK"))
  360. {
  361. questTypeID=""+request.getParameter("QuestType");//试题类型ID
  362. subjectID=Integer.parseInt(request.getParameter("Subject"));//科目ID
  363. gradeID=""+request.getParameter("Grade");//年级ID
  364. //difficulty=""+request.getParameter("Difficulty");//难度
  365. knowPointID1=""+request.getParameter("KnowPoint1");//知识点ID
  366. knowPointID2=""+request.getParameter("KnowPoint2");//知识点ID
  367. knowPointID3=""+request.getParameter("KnowPoint3");//知识点ID
  368. //answer=""+request.getParameter("Answer");//答案
  369. //answer=Tools.codestring(answer);
  370. float mark=0;//Float.parseFloat(request.getParameter("Mark"));//分数
  371. String strHtml=""+request.getParameter("Content");//获取提交的试题的HTML
  372. //debug
  373. con=DBCon.getConnection();
  374. //
  375. strHtml=Tools.codestring(strHtml);//转换字符集
  376. String group=""+request.getParameter("group");
  377. if(group.equals("single")){//提交单个试题
  378. ID=submitOne(strHtml,answer,mark,difficulty,con,false);
  379. }else{
  380. if(group.equals("suit") ){
  381.     String Title=""+request.getParameter("suit_title");
  382.             Title=Tools.codestring(Title);
  383.             ID=submitAll(strHtml,Title,con,out);
  384. }
  385. }
  386. %>
  387. <%temp=msgsb.toString();
  388. if(debug){
  389.     out.print("<br><br><br>New Information");
  390.     out.print(temp);
  391. }
  392. if(temp.equals("OK")){
  393. //提交成功
  394.         if(group.equals("single")){//提交单个试题
  395.             Tools.showalert(" 试题提交成功,新的试题ID为:"+ID,out);
  396.             Tools.goback(out);
  397.         }else if(group.equals("suit")){
  398.             Tools.showalert(" 试卷提交成功,新的试卷ID为:"+ID,out);
  399.             Tools.goback(out);
  400.         }
  401. %>
  402. <%
  403. }
  404. else{
  405. System.out.print(msgsb.toString());
  406. %>
  407. <SCRIPT language="javascript">
  408. alert("试题未能提交成功,请检查提交信息或与系统管理员联系");
  409. //history.go(-1);
  410. </SCRIPT>
  411. <%
  412. }
  413. %>
  414. </body>
  415. </html>
  416. <%
  417. }
  418. %>
  419. <%}
  420. catch(Exception e){
  421.     System.out.print(e.toString());
  422. }
  423. finally{
  424.     DBCon.dropConnection();
  425. }
  426. %>