Browse Source

feat: 做题时间记录

ChenYangfan 6 years ago
parent
commit
20ca99eb64
1 changed files with 34 additions and 3 deletions
  1. 34 3
      src/views/quiz/StudentDoQuiz.vue

+ 34 - 3
src/views/quiz/StudentDoQuiz.vue

@@ -11,7 +11,8 @@
         <div>最近提交:{{!isEmpty(answersInfo.submitAt) ? momentFromNow(answersInfo.submitAt) : '无提交历史'}}</div>
       </div>
       <template v-for="(question, index) in questions">
-        <do-quiz-question-card @choice="handleChoice" :key="question.id" :question-prop="question" :index="index"></do-quiz-question-card>
+        <do-quiz-question-card @choice="handleChoice" :key="question.id" :question-prop="question"
+                               :index="index"></do-quiz-question-card>
       </template>
       <div class="mt-24">
         <c-button class="mb-16" theme="green" icon="check">提交答案</c-button>
@@ -51,6 +52,7 @@ export default {
       lastDate: new Date().valueOf(),
       choiceDate: 0,
       questionPointer: -1,
+      records: {},
 
       QuestionKind
     }
@@ -96,6 +98,7 @@ export default {
     },
     handleFormSubmit () {
       const { quizId } = this.$route.params
+      this.pushRecord({ questionId: this.questionPointer, startAt: this.lastDate, endAt: this.choiceDate })
       const answers = {}
       let uncompleted = false
       this.questions.map(question => {
@@ -108,14 +111,42 @@ export default {
       })
       if (uncompleted) return
 
-      submitQuizStudentAnswer({ quizId, answers }).then(res => {
+      submitQuizStudentAnswer({ quizId, answers, records: this.records }).then(res => {
         this.hasSubmit = true
         this.$setSuccessMessage('提交成功')
         this.$router.go(-1)
       }).catch(err => { this.$setErrorMessage(err.response.data.msg) })
     },
+    // 做题时间判断逻辑
     handleChoice ({ questionId }) {
-      console.log(questionId)
+      if (this.questionPointer === -1) {
+        // 初始化条件
+        this.questionPointer = questionId
+        this.choiceDate = new Date().valueOf()
+        return
+      }
+
+      if (this.questionPointer === questionId) {
+        this.choiceDate = new Date().valueOf()
+        return
+      }
+
+      if (this.questionPointer !== questionId) {
+        this.pushRecord({ questionId: this.questionPointer, startAt: this.lastDate, endAt: this.choiceDate })
+        // 切换题目
+        this.questionPointer = questionId
+        this.lastDate = this.choiceDate
+        this.choiceDate = new Date().valueOf()
+      }
+    },
+    // 将记录推到record中,当record中已经存在这个id,则将时间差累加到结束时间上
+    pushRecord ({ questionId, startAt, endAt }) {
+      const record = this.records[questionId]
+      if (isEmpty(record)) {
+        this.records[questionId] = { startAt: new Date(startAt), endAt: new Date(endAt) }
+      } else {
+        record.endAt = record.endAt + endAt - startAt
+      }
     },
     momentFromNow,
     isEmpty