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

教育系统应用

开发平台:

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. <jsp:useBean id="DBCon" class="jinghua.JinghuaConn" scope="session">
  7. </jsp:useBean>
  8. <%!
  9. boolean debug=false;
  10. String questTypeID=null;
  11. int subjectID=-1;
  12. String gradeID=null;
  13. String difficulty=null;
  14. String knowPointID1=null;
  15. String knowPointID2=null;
  16. String knowPointID3=null;
  17. String answer=null;
  18. StringBuffer msgsb=new StringBuffer("OK");
  19. Statement stmt=null;
  20. ResultSet rs=null;
  21. String sql="";
  22. String temp="";
  23. int teaID=-1;
  24. int ID=-1;//新提交试题或试卷的ID
  25. Connection con=null;
  26. class Test{
  27.     long id;
  28.     String name;
  29.     String grade;
  30.     String time;
  31.     float mark;
  32.     String paper;
  33. int testnum;
  34.     public String toString(){
  35.         String temp="";
  36.         temp="<br> id="+id+"<br> name="+name;
  37.         return temp;
  38.    }
  39. };
  40. synchronized public int submitOne(String strHtml,String answer,float mark,String difficulty,Connection con,boolean commit){
  41. //commit=true则需要在一道题提交完后,使用commit
  42.     strHtml=strHtml.trim();
  43.     int tipPos=-1;
  44.     String tip="";
  45.     //检查格式:
  46.     if(strHtml.startsWith("@@")){
  47.         msgsb.append("<BR>单个试题的数据格式错误,请检查!");
  48.         return -1;
  49.     }else{
  50. //检查是否包含了文件
  51. if((strHtml.indexOf("src=")>0) || (strHtml.indexOf("SRC=")>0)){
  52. msgsb.append("<BR>单个试题的数据格式错误,可能包含有图片,请检查!");
  53.          return -1;
  54. }
  55.     }
  56.     //检查答案:
  57.     if(answer.trim()==""){
  58.         msgsb.append("<BR>试题答案为空,请检查!"+"原试题为:<br>"+strHtml);
  59.         return -1;
  60.     }else if (answer.trim().length()!=1){
  61.         msgsb.append("<BR>试题答案长度不为1,请检查!"+"原试题为:<br>"+strHtml);
  62.         return -1;
  63.     }else{
  64.         String strAnswer="ABCDEFGH";
  65.         if(strAnswer.indexOf(answer)<0){
  66.             msgsb.append("<BR>单选试题的答案长度不为“ABCDEFGH”中的字符,请检查!"+"原试题为:<br>"+strHtml);
  67.             return -1;
  68.         }
  69.     }
  70.     //检查难易度
  71.     if(difficulty.trim()==""){
  72.         msgsb.append("<BR>试题难易度为空,请检查!"+"原试题为:<br>"+strHtml);
  73.         return -1;
  74.     }else if (difficulty.trim().length()!=1){
  75.         msgsb.append("<BR>试题难易度长度不为1,请检查!"+"原试题为:<br>"+strHtml);
  76.         return -1;
  77.     }else{
  78.         String strAnswer="ABCDE";
  79.         if(strAnswer.indexOf(difficulty)<0){
  80.             msgsb.append("<BR>单选难易度的答案长度不为“ABCDE”中的字符,请检查!"+"原试题为:<br>"+strHtml);
  81.             return -1;
  82.         }
  83.     }
  84.     tipPos=strHtml.indexOf("#tip");
  85.     if(tipPos>0){
  86.         tip=strHtml.substring(tipPos+4);//取提示
  87.         strHtml=strHtml.substring(0,tipPos);
  88.         tip=Tools.toSql(tip);
  89.         strHtml=Tools.toSql(strHtml);
  90.     }
  91. answer=answer.toUpperCase();
  92.         int newID=-1;
  93.         if(!strHtml.equals("")){
  94. try{
  95.         answer.toUpperCase();
  96.         difficulty.toUpperCase();
  97. //String QuestClassType=QuestClassCode(con,subjectID, gradeID, knowPointID1,1,
  98.         //        questTypeID,   difficulty);
  99.         String QuestClassType="";
  100.     if(QuestClassType==null){
  101.     msgsb.append(" Error:试题编号生成错误n"+""+QuestClassType+strHtml);
  102.     }
  103. else
  104.     {
  105.             //source=1为自由测试
  106.     sql="insert into question(QuestID,Mark,Answer,TeacherID, `question`.`Source`,"+
  107.         "PubDate,QuestType,SubjectID,gradenote, "+
  108. "KnowPointID1,KnowPointID2,KnowPointID3,PreHard,Tip) "+ "values("+
  109.     "'"+QuestClassType+"',"+mark+",'"+answer+"',"+teaID+",1,NOW(),"+
  110. questTypeID+","+subjectID+","+"'"+gradeID+"'"+","+knowPointID1+","+knowPointID2+","+knowPointID3+
  111. ",'"+difficulty+"','"+tip+"'"+")";
  112. stmt=con.createStatement();
  113. stmt.executeUpdate(sql);
  114.     stmt.close();
  115.     stmt=con.createStatement();
  116.     ResultSet rs=stmt.executeQuery("select LAST_INSERT_ID() as newid from question");
  117.     if(rs.next()){
  118.     newID=rs.getInt("newID");
  119.                 rs.close();
  120.     stmt.close();
  121.     }
  122. }
  123. }//end try
  124. catch(SQLException se){
  125.     System.out.print(se);
  126.             System.out.print(sql);
  127.             msgsb.append("试题入库出错:SQL="+sql+"n"+se.toString());
  128. }catch(Exception e){
  129.             msgsb.append("试题格式可能出错 strHtml=:"+strHtml);
  130. }
  131. if(newID!=-1 && msgsb.toString().equals("OK")  ) {
  132. try{//修改题干
  133. temp=jinghua.TransformString.TransformSelction(strHtml,newID);
  134. temp=Tools.toSql(temp);
  135.             if(temp==null)
  136.                 throw new Exception("题目没有数据");
  137. stmt=con.createStatement();
  138. sql="update question set QuestText='"+temp+"' where ID="+newID;
  139. stmt.executeUpdate(sql);
  140.             stmt.close();
  141. }
  142. catch(SQLException se){
  143. try{
  144. con.rollback();
  145. }catch(Exception e){
  146. System.out.print(se);
  147. msgsb.append("试题入库出错:SQL="+sql+"n"+se.toString());
  148. }
  149.     }
  150.         catch(Exception se){
  151. try{
  152. con.rollback();
  153. }catch(Exception e){
  154.     System.out.print(se);
  155. msgsb.append("试题入库出错:"+se.toString());
  156. }
  157.         }
  158. }//end if
  159.     }else{
  160. try{
  161. con.rollback();
  162. }catch(Exception se)
  163. {
  164. msgsb.append("试题入库出错:"+se.toString());
  165. }
  166.         msgsb.append("提交试题出错:试题为空");
  167.     }
  168.     return newID;
  169. }//end submitOne
  170. //@@2. My pain _____apparent the moment I walked into the room. for the first man I met asked
  171. //sympathetically: "Are you feeling all right?"
  172. // A. must be  B. had  C. must have been  D. had to be
  173. //##A ##B  ##C  ##D
  174. //@A@5@E@ :@答案@分数@难度@
  175. %>
  176. <%
  177. try{
  178. //UN_debug
  179. msgsb.delete(0,msgsb.length() );
  180. msgsb.append("OK");
  181. try{
  182.     String TeacherID=request.getParameter("teacherID");
  183.     teaID=Integer.parseInt(TeacherID);
  184. }catch(Exception e){
  185.     Tools.showalert("您还没有登录,或连接超时,请重新登录!",out);
  186.     //response.sendRedirect("../login.jsp");
  187.     msgsb.append("teacherID Errorn");
  188.     Tools.showJS("window.open('../login.jsp','_self')",out);
  189.     //response.sendRedirect("../login.jsp");
  190. //    Tools.goback(out);
  191. }
  192. String testID=request.getParameter("testID");
  193. String mntestID=request.getParameter("mntestID");
  194. if(debug){
  195.     out.print("<br>testID="+testID+
  196. "<br>mntestID="+mntestID);
  197. }
  198. if(testID==null || mntestID==null)
  199. throw new Exception("调用参数不足!");
  200. Test t=new Test();
  201. t.id=Long.parseLong(testID);
  202. if (msgsb.toString().equals("OK"))
  203. {
  204. questTypeID=""+request.getParameter("QuestType");//试题类型ID
  205. subjectID=Integer.parseInt(request.getParameter("Subject"));//科目ID
  206. gradeID=""+request.getParameter("Grade");//年级ID
  207. difficulty=""+request.getParameter("Difficulty");//难度
  208. knowPointID1=""+request.getParameter("KnowPoint1");//知识点ID
  209. knowPointID2=""+request.getParameter("KnowPoint2");//知识点ID
  210. knowPointID3=""+request.getParameter("KnowPoint3");//知识点ID
  211. answer=request.getParameter("Answer");//答案
  212. answer=Tools.codestring(answer);
  213. answer=answer.toUpperCase();
  214. difficulty.toUpperCase();
  215. float mark=Float.parseFloat(request.getParameter("Mark"));//分数
  216. String strHtml=""+request.getParameter("Content");//获取提交的试题的HTML
  217. strHtml=Tools.codestring(strHtml);//转换字符集
  218. String group=request.getParameter("group");
  219. con=DBCon.getConnection();
  220. con.setAutoCommit(false);
  221. if(group.equals("single")){//提交单个试题
  222. if(debug) out.print("<br>submitone param: strHtml="+strHtml+
  223.      "<br>answer="+answer+"<br>mark="+mark+"<br>diff="+difficulty);
  224. ID=submitOne(strHtml,answer,mark,difficulty,con,false);
  225. }
  226. temp=msgsb.toString();
  227. if(debug){
  228.     out.print("<br><br><br>New Information:<br>");
  229.     out.print(temp);
  230. out.print("<br>New quest ID="+ID);
  231. }
  232. if(temp.equals("OK")){
  233. //提交成功
  234. sql="SELECT  `test`.`paper`,`test`.`testnum`,`test`.`mark` "+
  235. " FROM  `test` WHERE  (`test`.`id` = "+t.id+")";
  236.     stmt=con.createStatement();
  237.     rs=stmt.executeQuery(sql);
  238.     if(rs.next()){
  239.      t.paper=rs.getString("paper");
  240. t.mark=rs.getFloat("mark");
  241. t.testnum=rs.getInt("testnum");
  242.     }
  243.     rs.close();
  244.     stmt.close();
  245. if(debug) out.print("<br>test paper="+t.paper+
  246. "<br>testnum="+t.testnum+
  247. "<br>mark="+t.mark);
  248. t.testnum++;
  249. t.mark+=mark;
  250. //1574@A@10@A#
  251. t.paper=t.paper+String.valueOf(ID)+"@"+answer.toUpperCase()+
  252. "@"+mark+"@"+difficulty+"#";
  253. sql="update test set paper='"+t.paper+"',testnum="+t.testnum+",mark="+t.mark+
  254. " where id="+t.id;
  255. stmt=con.createStatement();
  256. stmt.executeUpdate(sql);
  257. stmt.close();
  258. if(debug)
  259. out.print("<br>update test sql="+sql);
  260. sql="update mn_test set mntotalmark=mntotalmark+"+mark+
  261. " where id="+mntestID;
  262. stmt=con.createStatement();
  263. stmt.executeUpdate(sql);
  264. stmt.close();
  265. con.commit();
  266. if(debug)
  267. out.print("<br>update mntest sql="+sql);
  268. if(group.equals("single") && !debug){//提交单个试题
  269. Tools.showalert(" 试题提交成功,新的试题ID为:"+ID,out);
  270.      Tools.goback(out);
  271. }
  272. }
  273. else{
  274. out.print("<br>"+msgsb.toString());
  275. %>
  276. <SCRIPT language="javascript">
  277. alert("试题未能提交成功,请检查提交信息或与系统管理员联系");
  278. //history.go(-1);
  279. </SCRIPT>
  280. <%
  281. }
  282. }
  283. }
  284. catch(Exception e){
  285.    out.print("<br>"+e.toString());
  286. }
  287. finally{
  288.     DBCon.dropConnection();
  289. }
  290. %>