Bläddra i källkod

通用考试数据包导出

xiatian 1 år sedan
förälder
incheckning
f458d23ca1

+ 2 - 2
examcloud-core-questions-service/src/main/java/cn/com/qmth/examcloud/core/questions/service/impl/ExportPaperServiceImpl.java

@@ -564,7 +564,8 @@ public class ExportPaperServiceImpl implements ExportPaperService {
      * @param computerTestPaper
      * @param jsonDirectoryPath
      */
-    private void makeComputerTestPaperToJsonFile(String courseCode, ComputerTestPaper computerTestPaper,
+    @SuppressWarnings("deprecation")
+	private void makeComputerTestPaperToJsonFile(String courseCode, ComputerTestPaper computerTestPaper,
             String jsonDirectoryPath) {
         // 创建新的JSON文件
         File file = new File(jsonDirectoryPath + File.separator + courseCode + ".json");
@@ -589,7 +590,6 @@ public class ExportPaperServiceImpl implements ExportPaperService {
         }
     }
 
-    @SuppressWarnings("unused")
     @Override
     public void exportPaperFiles(List<String> paperIds, String exportContentList,
             HttpServletResponse response, PaperSeqMode seqMode, String examType, User user) throws Exception {

+ 230 - 8
examcloud-core-questions-service/src/main/java/cn/com/qmth/examcloud/core/questions/service/impl/ExportThemisPaperServiceImpl.java

@@ -92,7 +92,8 @@ public class ExportThemisPaperServiceImpl implements ExportThemisPaperService {
 							if (blocks != null && blocks.size() > 0) {
 								for (ThemisBlock block : blocks) {
 									if (block.getType().equals("audio")) {
-										String id = block.getValue().substring(0, block.getValue().lastIndexOf("."));
+										String val=block.getValue().toString();
+										String id = val.substring(0, val.lastIndexOf("."));
 										QuestionAudio questionAudio = Model.of(questionAudioRepo.findById(id));
 										String audioFileName = questionAudio.getId() + "."
 												+ questionAudio.getFileSuffixes();
@@ -120,7 +121,8 @@ public class ExportThemisPaperServiceImpl implements ExportThemisPaperService {
 										if (blocks != null && blocks.size() > 0) {
 											for (ThemisBlock block : blocks) {
 												if (block.getType().equals("audio")) {
-													String id = block.getValue().substring(0, block.getValue().lastIndexOf("."));
+													String val=block.getValue().toString();
+													String id = val.substring(0, val.lastIndexOf("."));
 													QuestionAudio questionAudio = Model
 															.of(questionAudioRepo.findById(id));
 													String audioFileName = questionAudio.getId() + "."
@@ -153,6 +155,7 @@ public class ExportThemisPaperServiceImpl implements ExportThemisPaperService {
 		makePaperToJsonFile(pa.getPaper(), jsonDirectoryPath);
 	}
 
+	@SuppressWarnings("deprecation")
 	private void makeAnswerToJsonFile(ThemisAnswer pa, String jsonDirectoryPath) {
 		// 创建新的JSON文件
 		File file = new File(jsonDirectoryPath + File.separator + "answer.json");
@@ -165,7 +168,7 @@ public class ExportThemisPaperServiceImpl implements ExportThemisPaperService {
 		FileOutputStream outputStream = null;
 		try {
 			outputStream = new FileOutputStream(file);
-			byte b[] = strJSON.getBytes();
+			byte b[] = strJSON.getBytes("UTF-8");
 			outputStream.write(b);
 			outputStream.flush();
 		} catch (FileNotFoundException e) {
@@ -177,6 +180,7 @@ public class ExportThemisPaperServiceImpl implements ExportThemisPaperService {
 		}
 	}
 
+	@SuppressWarnings("deprecation")
 	private void makePaperToJsonFile(ThemisPaper pa, String jsonDirectoryPath) {
 		// 创建新的JSON文件
 		File file = new File(jsonDirectoryPath + File.separator + "paper.json");
@@ -189,7 +193,7 @@ public class ExportThemisPaperServiceImpl implements ExportThemisPaperService {
 		FileOutputStream outputStream = null;
 		try {
 			outputStream = new FileOutputStream(file);
-			byte b[] = strJSON.getBytes();
+			byte b[] = strJSON.getBytes("UTF-8");
 			outputStream.write(b);
 			outputStream.flush();
 		} catch (FileNotFoundException e) {
@@ -227,7 +231,13 @@ public class ExportThemisPaperServiceImpl implements ExportThemisPaperService {
 				computerTestQuestion.setNumber(i + 1);
 				themisAnswerContent.setNumber(i + 1);
 				// 得到小题题干
-				computerTestQuestion.setBody(getBody(paperDetailUnit.getQuestion().getQuesBody(), computerTestPaper));
+				if(computerTestQuestion.getStructType()==4) {
+					computerTestQuestion.setBody(getFillBlankBody(paperDetailUnit.getQuestion().getQuesBody(), computerTestPaper));
+				}else if(computerTestQuestion.getStructType()==6){
+					computerTestQuestion.setBody(getNestedBody(paperDetailUnit.getQuestion().getQuesBody(), computerTestPaper));
+				}else {
+					computerTestQuestion.setBody(getBody(paperDetailUnit.getQuestion().getQuesBody(), computerTestPaper));
+				}
 				// 得到小题所有选项
 				computerTestQuestion.setOptions(getOption(paperDetailUnit.getQuestion(), computerTestPaper));
 				// 得到小题的答案
@@ -256,7 +266,14 @@ public class ExportThemisPaperServiceImpl implements ExportThemisPaperService {
 						// 设置套题中小题题号
 						subcomputerTestQuestion.setNumber(j + 1);
 						subthemisAnswerContent.setNumber(j + 1);
-						subcomputerTestQuestion.setBody(getBody(subQuestion.getQuesBody(), computerTestPaper));
+						
+						if(subcomputerTestQuestion.getStructType()==4) {
+							subcomputerTestQuestion.setBody(getFillBlankBody(subQuestion.getQuesBody(), computerTestPaper));
+						}else if(computerTestQuestion.getStructType()==6){
+							subcomputerTestQuestion.setBody(getNestedBody(subQuestion.getQuesBody(), computerTestPaper));
+						}else {
+							subcomputerTestQuestion.setBody(getBody(subQuestion.getQuesBody(), computerTestPaper));
+						}
 						subcomputerTestQuestion.setOptions(getOption(subQuestion, computerTestPaper));
 						if (subQuestion.getQuestionType() == QuesStructType.SINGLE_ANSWER_QUESTION
 								|| subQuestion.getQuestionType() == QuesStructType.MULTIPLE_ANSWER_QUESTION) {
@@ -293,7 +310,6 @@ public class ExportThemisPaperServiceImpl implements ExportThemisPaperService {
 		ret.setAnswer(themisAnswer);
 		return ret;
 	}
-
 	private ThemisSections getBody(String str, ThemisPaper computerTestPaper) {
 		ThemisSections body = new ThemisSections();
 		List<ThemisSection> sections = new ArrayList<>();
@@ -314,7 +330,27 @@ public class ExportThemisPaperServiceImpl implements ExportThemisPaperService {
 		body.setSections(sections);
 		return body;
 	}
-
+	private ThemisSections getFillBlankBody(String str, ThemisPaper computerTestPaper) {
+		ThemisSections body = new ThemisSections();
+		List<ThemisSection> sections = new ArrayList<>();
+		// 得到小题题干或者答案行数
+		if (StringUtils.isBlank(str)) {
+			return body;
+		}
+		String[] questionRowStrings = str.split("</p>");
+		for (int i = 0; i < questionRowStrings.length; i++) {
+			List<ThemisBlock> blocks = disposeFillBlankQuestionBody(questionRowStrings[i], computerTestPaper);
+			if (blocks != null && blocks.size() > 0) {
+				ThemisSection section = new ThemisSection();
+				// 将小题题干拆分为Block集合
+				section.setBlocks(blocks);
+				sections.add(section);
+			}
+		}
+		body.setSections(sections);
+		return body;
+	}
+	
 	private List<ThemisSubjectiveAnswer> getAnswer(String str, ThemisPaper computerTestPaper) {
 		List<ThemisSection> sections = new ArrayList<>();
 		// 得到小题题干或者答案行数
@@ -338,6 +374,192 @@ public class ExportThemisPaperServiceImpl implements ExportThemisPaperService {
 		an.setSections(sections);
 		return ans;
 	}
+	private String changeCloze(String text) {
+		StringBuffer buffer = new StringBuffer();
+        String regex = "##(\\d)##";
+
+        Pattern pattern = Pattern.compile(regex);
+        Matcher matcher = pattern.matcher(text);
+        // 使用find()方法查找匹配项
+        while (matcher.find()) {
+        	matcher.appendReplacement(buffer, "__"+matcher.group(1)+"__");
+        }
+        return buffer.toString();
+	}
+	private ThemisSections getNestedBody(String str, ThemisPaper computerTestPaper) {
+		ThemisSections body = new ThemisSections();
+		List<ThemisSection> sections = new ArrayList<>();
+		// 得到小题题干或者答案行数
+		if (StringUtils.isBlank(str)) {
+			return body;
+		}
+		String[] questionRowStrings = str.split("</p>");
+		for (int i = 0; i < questionRowStrings.length; i++) {
+			List<ThemisBlock> blocks = disposeNestedQuestionBody(questionRowStrings[i], computerTestPaper);
+			if (blocks != null && blocks.size() > 0) {
+				ThemisSection section = new ThemisSection();
+				// 将小题题干拆分为Block集合
+				section.setBlocks(blocks);
+				sections.add(section);
+			}
+		}
+		body.setSections(sections);
+		return body;
+	}
+	private List<ThemisBlock> disposeNestedQuestionBody(String questionRow, ThemisPaper computerTestPaper) {
+		List<ThemisBlock> blocks = new ArrayList<>();
+		// 去掉每行里面的<p>,<span>,</span>标签
+		questionRow = questionRow.replaceAll("<p>", "").replaceAll("</p>", "").replaceAll("<span>", "")
+				.replaceAll("</span>", "").replaceAll("</a>", "");
+		String[] questionRowStrings = questionRow.split("<|/>|>");
+		boolean hasAudio = false;
+		for (int i = 0; i < questionRowStrings.length; i++) {
+			ThemisBlock block = new ThemisBlock();
+			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.setType("audio");
+				String id=CommonUtils.getAttrValue(rowStr, "id");
+				QuestionAudio questionAudio = Model.of(questionAudioRepo.findById(id));
+				block.setValue(questionAudio.getId() + "."+ questionAudio.getFileSuffixes());
+				blocks.add(block);
+				hasAudio = true;
+			} 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 (StringUtils.isNotBlank(rowStr)) {
+					block.setValue(changeCloze(rowStr));
+					blocks.add(block);
+				}
+			}
+		}
+		if (hasAudio) {
+			computerTestPaper.setHasAudio(hasAudio);
+		}
+		return blocks;
+	}
+	
+	private List<ThemisBlock> disposeFillBlankQuestionBody(String questionRow, ThemisPaper computerTestPaper) {
+		List<ThemisBlock> blocks = new ArrayList<>();
+		// 去掉每行里面的<p>,<span>,</span>标签
+		questionRow = questionRow.replaceAll("<p>", "").replaceAll("</p>", "").replaceAll("<span>", "")
+				.replaceAll("</span>", "").replaceAll("</a>", "");
+		String[] questionRowStrings = questionRow.split("<|/>|>");
+		boolean hasAudio = false;
+		int blankIndex=0;
+		for (int i = 0; i < questionRowStrings.length; i++) {
+			ThemisBlock block = new ThemisBlock();
+			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.setType("audio");
+				String id=CommonUtils.getAttrValue(rowStr, "id");
+				QuestionAudio questionAudio = Model.of(questionAudioRepo.findById(id));
+				block.setValue(questionAudio.getId() + "."+ questionAudio.getFileSuffixes());
+				blocks.add(block);
+				hasAudio = true;
+			} else {
+				rowStr = rowStr.replace("&nbsp;", "");// 消除空格
+				rowStr = rowStr.replace("&quot;", "\"");// 将&quot;转换成\"
+				rowStr = rowStr.replace("&lt;", "<");// 将&lt;转换成<
+				rowStr = rowStr.replace("&gt;", ">");// 将&gt;转换成>
+				rowStr = rowStr.replace("&amp;", "&");// 将&amp;转换成&
+		        
+				String regex = "###";
+
+		        Pattern pattern = Pattern.compile(regex);
+		        Matcher matcher = pattern.matcher(rowStr);
+		        int indexStart=0;
+		        // 使用find()方法查找匹配项
+		        while (matcher.find()) {
+		        	blankIndex++;
+		            String content=rowStr.substring(indexStart, matcher.start());
+		            if(StringUtils.isNotEmpty(content)) {
+		            	ThemisBlock temblock = new ThemisBlock();
+			            temblock.setType("text");
+			            temblock.setValue(content);
+						blocks.add(temblock);
+		            }
+		            ThemisBlock blankblock = new ThemisBlock();
+		            blankblock.setType("cloze");
+		            blankblock.setValue(blankIndex);
+		            blocks.add(blankblock);
+		            indexStart=matcher.start()+regex.length();
+		        }
+		        if(indexStart<rowStr.length()) {
+		        	// 输出最后一个分隔符之后的字符串
+			        String content=rowStr.substring(indexStart, rowStr.length());
+		            if(StringUtils.isNotEmpty(content)) {
+		            	ThemisBlock temblock = new ThemisBlock();
+			            temblock.setType("text");
+			            temblock.setValue(content);
+						blocks.add(temblock);
+		            }
+		        }
+			}
+		}
+		if (hasAudio) {
+			computerTestPaper.setHasAudio(hasAudio);
+		}
+		return blocks;
+	}
+	
 
 	private List<ThemisBlock> disposeQuestionBodyOrOption(String questionRow, ThemisPaper computerTestPaper) {
 		List<ThemisBlock> blocks = new ArrayList<>();

+ 3 - 6
examcloud-core-questions-service/src/main/java/cn/com/qmth/examcloud/core/questions/service/themispaper/ThemisBlock.java

@@ -14,10 +14,7 @@ public class ThemisBlock {
      * video:视频
      */
     private String type;
-    /**
-     * 资源相对路径
-     */
-    private String value;
+    private Object value;
 
     private Map<String, Object> param;
 
@@ -29,11 +26,11 @@ public class ThemisBlock {
         this.type = type;
     }
 
-    public String getValue() {
+    public Object getValue() {
         return value;
     }
 
-    public void setValue(String value) {
+    public void setValue(Object value) {
         this.value = value;
     }