useTaskWatch.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { watch } from "vue";
  2. import { useMarkStore } from "@/store";
  3. import useQuestion from "./useQuestion";
  4. export default function useTaskWatch() {
  5. const markStore = useMarkStore();
  6. const { isArbitrated } = useQuestion();
  7. watch(
  8. () => markStore.currentTask,
  9. () => {
  10. // 初始化 task.markResult ,始终保证 task 下有 markResult
  11. // 1. 评卷时,如果没有 markResult ,则初始化一个 markResult 给它
  12. // 1. 回评时,先清空它的 markResult ,然后初始化一个 markResult 给它
  13. if (!markStore.currentTask) return;
  14. // 主观题检查时,记录修改的试题信息
  15. const currentTaskModifyQuestion = {};
  16. markStore.currentTask.questionList.forEach((q) => {
  17. const qno = `${q.mainNumber}_${q.subNumber}`;
  18. currentTaskModifyQuestion[qno] = false;
  19. });
  20. markStore.currentTaskModifyQuestion = currentTaskModifyQuestion;
  21. const task = markStore.currentTask;
  22. if (task.previous && task.markResult) {
  23. task.markResult = undefined;
  24. }
  25. if (task.markResult) return;
  26. // 管理后台可能不设置 questionList, 而且它不用 markResult
  27. if (!task.questionList) {
  28. task.questionList = [];
  29. // return;
  30. }
  31. // 初始化 __index
  32. task.questionList.forEach((q, i) => {
  33. q.__index = i;
  34. // 回评的时候,如果是自己标记问题卷的题目,则转成非自己标记问题卷的题目
  35. // 这样一来,就不能编辑问题卷,但是依旧可以编辑新设置未提交的问题卷
  36. if (markStore.historyOpen && q.problem && q.selfMark) {
  37. q.selfMark = false;
  38. }
  39. });
  40. task.__markStartTime = Date.now();
  41. const statusValue = markStore.setting.statusValue;
  42. const { examId, studentId, paperNumber } = task;
  43. const selfQuestions = task.questionList.filter(
  44. (q) => q.selfMark && !isArbitrated(q)
  45. );
  46. task.markResult = {
  47. examId,
  48. paperNumber,
  49. studentId,
  50. spent: 0,
  51. statusValue,
  52. questionList: task.questionList,
  53. markedQuestionId: "",
  54. markerTrackList: selfQuestions
  55. .map((q) => {
  56. const trackList =
  57. q.headerTrackList && q.headerTrackList.length
  58. ? q.headerTrackList
  59. : q.markerTrackList;
  60. return trackList || [];
  61. })
  62. .flat(),
  63. markerTagList: selfQuestions
  64. .map((q) => {
  65. const tagList =
  66. q.headerTagList && q.headerTagList.length
  67. ? q.headerTagList
  68. : q.markerTagList;
  69. return tagList || [];
  70. })
  71. .flat(),
  72. scoreList: task.questionList.map((q) =>
  73. q.selfMark && !isArbitrated(q) ? q.markerScore : null
  74. ),
  75. markerScore: 0, // 后期通过 scoreList 自动更新
  76. };
  77. task.markResult.markerTrackList.forEach((t) => {
  78. if (t.unanswered) {
  79. t.score = -0;
  80. }
  81. });
  82. }
  83. );
  84. // 唯一根据 scoreList 自动更新 markerScore
  85. watch(
  86. () => markStore.currentTask?.markResult.scoreList,
  87. () => {
  88. if (!markStore.currentTask) return;
  89. const scoreList = markStore.currentTask.markResult.scoreList.filter(
  90. (v) => v !== null
  91. );
  92. const result =
  93. scoreList.length === 0
  94. ? null
  95. : scoreList.reduce((acc, v) => (acc += Math.round(v * 1000)), 0) /
  96. 1000;
  97. markStore.currentTask.markResult.markerScore = result;
  98. },
  99. { deep: true }
  100. );
  101. }