Selaa lähdekoodia

getQuestionContentForClient

deason 3 vuotta sitten
vanhempi
commit
04fb3cbc62

+ 10 - 1
examcloud-core-oe-student-api-provider/src/main/java/cn/com/qmth/examcloud/core/oe/student/api/controller/client/ExamProcessController.java

@@ -1,12 +1,16 @@
 package cn.com.qmth.examcloud.core.oe.student.api.controller.client;
 
+import cn.com.qmth.examcloud.api.commons.security.bean.User;
+import cn.com.qmth.examcloud.core.oe.student.base.utils.Check;
 import cn.com.qmth.examcloud.core.oe.student.bean.*;
+import cn.com.qmth.examcloud.core.oe.student.service.ExamRecordQuestionsService;
 import cn.com.qmth.examcloud.support.examing.ExamQuestion;
 import cn.com.qmth.examcloud.support.examing.ExamRecordPaperStruct;
 import cn.com.qmth.examcloud.web.support.ControllerSupport;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import io.swagger.annotations.ApiParam;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
 import javax.servlet.http.HttpServletRequest;
@@ -17,6 +21,9 @@ import java.util.List;
 @RequestMapping("${app.api.oe.student}/client/exam/process")
 public class ExamProcessController extends ControllerSupport {
 
+    @Autowired
+    private ExamRecordQuestionsService examRecordQuestionsService;
+
     @ApiOperation(value = "开始考试")
     @PostMapping("/startExam")
     public StartExamInfo startExam(@RequestParam Long examStudentId) {
@@ -68,7 +75,9 @@ public class ExamProcessController extends ControllerSupport {
     @ApiOperation(value = "考试过程中-获取试题内容")
     @PostMapping("/getQuestionContent")
     public String getQuestionContent(@RequestParam String questionId) {
-        return null;
+        User user = getAccessUser();
+        Check.isBlank(questionId, "questionId不能为空");
+        return examRecordQuestionsService.getQuestionContentForClient(user.getUserId(), questionId);
     }
 
     @ApiOperation(value = "考试过程中-考生试题作答")

+ 2 - 1
examcloud-core-oe-student-service/src/main/java/cn/com/qmth/examcloud/core/oe/student/service/ExamRecordQuestionsService.java

@@ -16,7 +16,6 @@ public interface ExamRecordQuestionsService {
 
     /**
      * 保存
-     *
      */
     void saveExamQuestion(Long examRecordDataId, Integer order, ExamQuestion questions);
 
@@ -41,6 +40,8 @@ public interface ExamRecordQuestionsService {
 
     String getQuestionContent(Long studentId, String questionId);
 
+    String getQuestionContentForClient(Long studentId, String questionId);
+
     void submitQuestionAnswer(Long studentId, List<ExamStudentQuestionInfo> examQuestionInfos, String referer, String agent);
 
     GetExamRecordQuestionsResp getExamRecordQuestions(GetExamRecordQuestionsReq req);

+ 49 - 3
examcloud-core-oe-student-service/src/main/java/cn/com/qmth/examcloud/core/oe/student/service/impl/ExamRecordQuestionsServiceImpl.java

@@ -16,11 +16,13 @@ import cn.com.qmth.examcloud.question.commons.core.paper.DefaultPaper;
 import cn.com.qmth.examcloud.question.commons.core.paper.DefaultQuestionGroup;
 import cn.com.qmth.examcloud.question.commons.core.paper.DefaultQuestionStructureWrapper;
 import cn.com.qmth.examcloud.question.commons.core.paper.DefaultQuestionUnitWrapper;
+import cn.com.qmth.examcloud.question.commons.core.question.DefaultQuestion;
 import cn.com.qmth.examcloud.question.commons.core.question.DefaultQuestionStructure;
 import cn.com.qmth.examcloud.question.commons.core.question.DefaultQuestionUnit;
 import cn.com.qmth.examcloud.support.cache.CacheHelper;
 import cn.com.qmth.examcloud.support.cache.bean.QuestionCacheBean;
 import cn.com.qmth.examcloud.support.examing.*;
+import cn.com.qmth.examcloud.support.handler.QuestionBodyHandler;
 import cn.com.qmth.examcloud.support.redis.RedisKeyHelper;
 import cn.com.qmth.examcloud.web.helpers.GlobalHelper;
 import cn.com.qmth.examcloud.web.redis.RedisClient;
@@ -29,6 +31,7 @@ import com.google.common.cache.CacheBuilder;
 import org.apache.commons.lang.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
@@ -162,9 +165,51 @@ public class ExamRecordQuestionsServiceImpl implements ExamRecordQuestionsServic
         } else {// 如果本地缓存不存在,则从redis中获取, 并在本地缓存存在存储一份
             QuestionCacheBean getQuestionResp = CacheHelper.getQuestion(examSessionInfo.getExamId(),
                     examSessionInfo.getCourseCode(), examSessionInfo.getPaperType(), questionId);
-            DefaultQuestionStructure questionStructure = getQuestionResp.getDefaultQuestion().getMasterVersion();
+
+            DefaultQuestion defaultQuestion = getQuestionResp.getDefaultQuestion();
+            DefaultQuestionStructure questionStructure = defaultQuestion.getMasterVersion();
+            List<DefaultQuestionUnit> questionUnits = questionStructure.getQuestionUnitList();
+
+            // 在线考试,清除答案
+            if (ExamType.ONLINE.name().equals(examSessionInfo.getExamType())
+                    || ExamType.ONLINE_HOMEWORK.name().equals(examSessionInfo.getExamType())) {
+                for (DefaultQuestionUnit questionUnit : questionUnits) {
+                    questionUnit.setRightAnswer(null);
+                }
+            }
+            String resultJson = JsonUtil.toJson(questionStructure);
+            questionContentCache.put(cacheKey, resultJson);
+            return resultJson;
+        }
+    }
+
+    @Override
+    public String getQuestionContentForClient(Long studentId, String questionId) {
+        ExamingSession examSessionInfo = examingSessionService.getExamingSession(studentId);
+        if (examSessionInfo == null
+                || examSessionInfo.getExamingStatus().equals(ExamingStatus.INFORMAL)) {
+            throw new StatusException("2001", "考试已结束,不允许获取试题内容");
+        }
+
+        // 本地缓存key规则:examId_courseCode_paperType_questionId
+        String cacheKey = examSessionInfo.getExamId() + "_" + examSessionInfo.getCourseCode() + "_"
+                + examSessionInfo.getPaperType() + "_" + questionId;
+        String contentJson = questionContentCache.getIfPresent(cacheKey);
+
+        // 如果本地缓存中存在,则从本地缓存中获取
+        if (StringUtils.isNotEmpty(contentJson)) {
+            return contentJson;
+        } else {// 如果本地缓存不存在,则从redis中获取, 并在本地缓存存在存储一份
+            QuestionCacheBean getQuestionResp = CacheHelper.getQuestion(examSessionInfo.getExamId(),
+                    examSessionInfo.getCourseCode(), examSessionInfo.getPaperType(), questionId);
+
+            DefaultQuestion defaultQuestion = getQuestionResp.getDefaultQuestion();
+            DefaultQuestionStructure questionStructure = defaultQuestion.getMasterVersion();
             List<DefaultQuestionUnit> questionUnits = questionStructure.getQuestionUnitList();
 
+            // 将题干、选项等 HTML结构转换为“富文本”JSON结构
+            QuestionBodyHandler.convertRichText(defaultQuestion);
+
             // 在线考试,清除答案
             if (ExamType.ONLINE.name().equals(examSessionInfo.getExamType())
                     || ExamType.ONLINE_HOMEWORK.name().equals(examSessionInfo.getExamType())) {
@@ -187,8 +232,8 @@ public class ExamRecordQuestionsServiceImpl implements ExamRecordQuestionsServic
         }
         long examRecordDataId = examSessionInfo.getExamRecordDataId();
 
-        if(StringUtils.isEmpty(referer) || StringUtils.isEmpty(agent) || !agent.contains("electron-exam-shell")){
-            redisClient.set("OE_SESSION_WARN_"+examSessionInfo.getExamRecordDataId(), true,12 * 60 * 60);
+        if (StringUtils.isEmpty(referer) || StringUtils.isEmpty(agent) || !agent.contains("electron-exam-shell")) {
+            redisClient.set("OE_SESSION_WARN_" + examSessionInfo.getExamRecordDataId(), true, 12 * 60 * 60);
         }
 
         for (ExamStudentQuestionInfo examQuestionInfo : examQuestionInfos) {
@@ -304,4 +349,5 @@ public class ExamRecordQuestionsServiceImpl implements ExamRecordQuestionsServic
         eq.setAudioPlayTimes(temp.getAudioPlayTimes());
         eq.setAnswerType(temp.getAnswerType());
     }
+
 }