瀏覽代碼

给阅卷导出作答信息新加标答和题干字段

lideyin 5 年之前
父節點
當前提交
9b2d390375

+ 63 - 0
src/main/java/cn/com/qmth/examcloud/bridge/modules/cloudmarking/bean/Block.java

@@ -0,0 +1,63 @@
+package cn.com.qmth.examcloud.bridge.modules.cloudmarking.bean;
+
+import java.util.Map;
+
+/**
+ * @Description 从题库拷过来的代码,用于处理导出阅卷数据 ,见ExamRecordQuestionsCloudServiceProvider
+ * @Author lideyin
+ * @Date 2020/3/2 18:35
+ * @Version 1.0
+ */
+public class Block {
+    /**
+     * text:文字
+     * image:图片
+     * audio:音频
+     * video:视频
+     */
+    private String type;
+    /**
+     * 资源相对路径
+     */
+    private String value;
+    /**
+     * 播放次数
+     */
+    private Integer playTime;
+
+    private Map<String, Object> param;
+
+    public String getType() {
+        return type;
+    }
+
+    public void setType(String type) {
+        this.type = type;
+    }
+
+    public String getValue() {
+        return value;
+    }
+
+    public void setValue(String value) {
+        this.value = value;
+    }
+
+    public Map<String, Object> getParam() {
+        return param;
+    }
+
+    public void setParam(Map<String, Object> param) {
+        this.param = param;
+    }
+
+    public Integer getPlayTime() {
+        return playTime;
+    }
+
+    public void setPlayTime(Integer playTime) {
+        this.playTime = playTime;
+    }
+
+}
+

+ 25 - 0
src/main/java/cn/com/qmth/examcloud/bridge/modules/cloudmarking/bean/Section.java

@@ -0,0 +1,25 @@
+package cn.com.qmth.examcloud.bridge.modules.cloudmarking.bean;
+
+import java.util.List;
+
+/**
+ * @Description 从题库拷过来的代码,用于处理导出阅卷数据
+ * @Author lideyin
+ * @Date 2020/3/2 18:36
+ * @Version 1.0
+ */
+public class Section {
+
+    private List<Block> blocks;
+
+    public List<Block> getBlocks() {
+        return blocks;
+    }
+
+    public void setBlocks(List<Block> blocks) {
+        this.blocks = blocks;
+    }
+
+
+}
+

+ 25 - 0
src/main/java/cn/com/qmth/examcloud/bridge/modules/cloudmarking/bean/Sections.java

@@ -0,0 +1,25 @@
+package cn.com.qmth.examcloud.bridge.modules.cloudmarking.bean;
+
+import java.util.List;
+
+/**
+ * @Description 从题库拷过来的代码,用于处理导出阅卷数据
+ * @Author lideyin
+ * @Date 2020/3/2 18:35
+ * @Version 1.0
+ */
+public class Sections {
+
+    private List<Section> sections;
+
+    public List<Section> getSections() {
+        return sections;
+    }
+
+    public void setSections(List<Section> sections) {
+        this.sections = sections;
+    }
+
+
+}
+

+ 156 - 24
src/main/java/cn/com/qmth/examcloud/bridge/modules/cloudmarking/controller/CloudMarkingController.java

@@ -1,5 +1,8 @@
 package cn.com.qmth.examcloud.bridge.modules.cloudmarking.controller;
 
+import cn.com.qmth.examcloud.bridge.modules.cloudmarking.bean.Block;
+import cn.com.qmth.examcloud.bridge.modules.cloudmarking.bean.Section;
+import cn.com.qmth.examcloud.bridge.modules.cloudmarking.bean.Sections;
 import cn.com.qmth.examcloud.commons.exception.StatusException;
 import cn.com.qmth.examcloud.commons.util.IOUtil;
 import cn.com.qmth.examcloud.commons.util.JsonUtil;
@@ -25,10 +28,9 @@ import org.springframework.web.bind.annotation.RestController;
 import javax.annotation.Resource;
 import java.io.File;
 import java.nio.charset.Charset;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 import java.util.stream.Collectors;
 
 /**
@@ -108,7 +110,7 @@ public class CloudMarkingController {
             for (int i = 0; i < examStudentIdList.size(); i++) {
                 Long examStudentId = examStudentIdList.get(i);
 
-                List<Map<String, Object>> answerMapList = new ArrayList<>();
+                List<Map<String, Object>> resultMapList = new ArrayList<>();
 
                 //当前学生的作答记录
                 List<SubjectiveAnswerBean> curStuAnswerList = subjectiveAnswerList.
@@ -117,32 +119,25 @@ public class CloudMarkingController {
                         collect(Collectors.toList());
 
                 for (SubjectiveAnswerBean sab : curStuAnswerList) {
-                    Map<String, Object> answerMap = Maps.newHashMap();
-                    answerMap.put("mainNumber", sab.getMainNumber());
-                    answerMap.put("subNumber", sab.getOrder());
+                    Map<String, Object> resultMap = Maps.newHashMap();
+                    //构建基本信息
+                    resultMap.put("mainNumber", sab.getMainNumber());//大题号
+                    resultMap.put("subNumber", sab.getOrder());//小题号
 
-                    Map<String, Object> bodyMap = Maps.newHashMap();
+                    //构建题干
+                    resultMap.put("body",getBodyOrAnswer(sab.getBody()));
 
-                    List<Map<String, Object>> sectionMapList = new ArrayList<>();
-                    Map<String, Object> sectionMap = Maps.newHashMap();
-                    List<Map<String, Object>> blockMapList = new ArrayList<>();
+                    //构建标准答案
+                    resultMap.put("answer",getBodyOrAnswer(sab.getAnswer()));
 
-                    Map<String, Object> blockMap = Maps.newHashMap();
-                    blockMap.put("type", sab.getRealAnswerType());
-                    blockMap.put("value", sab.getStudentAnswer());
-                    blockMapList.add(blockMap);
+                    //构建学生作答
+                    resultMap.put("studentAnswer", buildStudentAnswerMap(sab));
 
-                    sectionMap.put("blocks", blockMapList);
-                    sectionMapList.add(sectionMap);
-
-                    bodyMap.put("sections", sectionMapList);
-                    answerMap.put("body", bodyMap);
-
-                    answerMapList.add(answerMap);
+                    resultMapList.add(resultMap);
                 }
 
                 //将考生结果保存至临时文件
-                String json = JsonUtil.toJson(answerMapList);
+                String json = JsonUtil.toJson(resultMapList);
                 String filePath = tempAnswerDir + "/" + examId + "-" + courseCode + "-" + examStudentId + ".json";
                 IOUtil.toFile(json.getBytes(Charset.forName("UTF-8")), filePath);
             }
@@ -168,6 +163,143 @@ public class CloudMarkingController {
         return responseEntity;
     }
 
+    private Map<String, Object> buildStudentAnswerMap(SubjectiveAnswerBean sab) {
+        //构建学生作答
+        Map<String, Object> studentAnswerMap = Maps.newHashMap();
+
+        List<Map<String, Object>> sectionMapList = new ArrayList<>();
+        Map<String, Object> sectionMap = Maps.newHashMap();
+        List<Map<String, Object>> blockMapList = new ArrayList<>();
+
+        Map<String, Object> blockMap = Maps.newHashMap();
+        blockMap.put("type", sab.getRealAnswerType());
+        blockMap.put("value", sab.getStudentAnswer());
+        blockMapList.add(blockMap);
+
+        sectionMap.put("blocks", blockMapList);
+        sectionMapList.add(sectionMap);
+
+        studentAnswerMap.put("sections", sectionMapList);
+        return studentAnswerMap;
+    }
+
+    /**
+     * 获取格式化后后的题干或标准答案
+     * @param str
+     * @return
+     */
+    private Sections getBodyOrAnswer(String str) {
+        Sections body = new Sections();
+        List<Section> sections = new ArrayList<>();
+        // 得到小题题干或者答案行数
+        if (org.apache.commons.lang3.StringUtils.isBlank(str)) {
+            return body;
+        }
+        String[] questionRowStrings = str.split("</p>");
+        for (int i = 0; i < questionRowStrings.length; i++) {
+            List<Block> blocks = disposeQuestionBodyOrOption(questionRowStrings[i]);
+            if (blocks != null && blocks.size() > 0) {
+                Section section = new Section();
+                // 将小题题干拆分为Block集合
+                section.setBlocks(blocks);
+                sections.add(section);
+            }
+        }
+        body.setSections(sections);
+        return body;
+    }
+
+    private List<Block> disposeQuestionBodyOrOption(String questionRow) {
+        List<Block> blocks = new ArrayList<>();
+        // 去掉每行里面的<p>,<span>,</span>标签
+        questionRow = questionRow.replaceAll("<p>", "").replaceAll("</p>", "").replaceAll("<span>", "")
+                .replaceAll("</span>", "").replaceAll("</a>", "");
+        String[] questionRowStrings = questionRow.split("<|/>|>");
+        for (int i = 0; i < questionRowStrings.length; i++) {
+            Block block = new Block();
+            String rowStr = questionRowStrings[i];
+            // 判断是否有图片
+            if (rowStr.startsWith("img")) {
+                rowStr = "<" + rowStr + ">";
+                Map<String, Object> param = new HashMap<>();
+                // 需要继续做截取,取到Parma
+                block.setType("image");
+                // 获取图片的路径
+                List<String> strSrcList = getImg(rowStr, "src");
+                if (strSrcList.size() > 0) {
+                    String strSrc = strSrcList.get(0).replaceAll("src=\"", "").replaceAll("\"", "");
+                    block.setValue(strSrc);
+                }
+                // 获取图片的高度
+                List<String> strHeightList = getImg(rowStr, "height");
+                if (strHeightList.size() > 0) {
+                    String strHeight = strHeightList.get(0).replaceAll("height=\"", "").replaceAll("\"", "");
+                    param.put("height", strHeight);
+                }
+                // 获取图片的宽度
+                List<String> strWidthList = getImg(rowStr, "width");
+                if (strHeightList.size() > 0) {
+                    String strWidth = strWidthList.get(0).replaceAll("width=\"", "").replaceAll("\"", "");
+                    param.put("width", strWidth);
+                }
+                block.setParam(param);
+                blocks.add(block);
+            } else if (rowStr.startsWith("a") && rowStr.contains("id") && rowStr.contains("name")) { // 处理音频
+                rowStr = "<" + rowStr + ">";
+                block.setPlayTime(1);
+                block.setType("audio");
+                block.setValue(this.getAttrValue(rowStr, "id"));
+                blocks.add(block);
+            } else {
+                block.setType("text");
+                rowStr = rowStr.replace("&nbsp;", "");// 消除空格
+                rowStr = rowStr.replace("&quot;", "\"");// 将&quot;转换成\"
+                rowStr = rowStr.replace("&lt;", "<");// 将&lt;转换成<
+                rowStr = rowStr.replace("&gt;", ">");// 将&gt;转换成>
+                rowStr = rowStr.replace("&amp;", "&");// 将&amp;转换成&
+                if (org.apache.commons.lang3.StringUtils.isNotBlank(rowStr)) {
+                    block.setValue(rowStr);
+                    blocks.add(block);
+                }
+            }
+        }
+
+        return blocks;
+    }
+
+    /**
+     * 获取图片里面的路径,长度,宽度
+     */
+    private List<String> getImg(String s, String str) {
+        String regex;
+        List<String> list = new ArrayList<>();
+        regex = str + "=\"(.*?)\"";
+        Pattern pa = Pattern.compile(regex, Pattern.DOTALL);
+        Matcher ma = pa.matcher(s);
+        while (ma.find()) {
+            list.add(ma.group());
+        }
+        return list;
+    }
+
+    private String getAttrValue(String questionStr, String attrName) {
+        Pattern aPattern = Pattern.compile("a.*");
+        Matcher aMatcher = aPattern.matcher(questionStr);
+
+        if (aMatcher.find()) {
+            String idRegex = attrName + "=\".*?\"";
+            Pattern idPattern = Pattern.compile(idRegex);
+            Matcher idMatcher = idPattern.matcher(aMatcher.group());
+            if (idMatcher.find()) {
+                return idMatcher.group()
+                        .replaceAll(attrName + "=\"", "")
+                        .replaceAll("\"", "");
+            }
+        }
+
+        return "";
+    }
+
     /**
      * 清空临时目录中的文件
      *