|
@@ -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(" ", "");// 消除空格
|
|
|
+ rowStr = rowStr.replace(""", "\"");// 将"转换成\"
|
|
|
+ rowStr = rowStr.replace("<", "<");// 将<转换成<
|
|
|
+ rowStr = rowStr.replace(">", ">");// 将>转换成>
|
|
|
+ rowStr = rowStr.replace("&", "&");// 将&转换成&
|
|
|
+ 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 "";
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 清空临时目录中的文件
|
|
|
*
|