questionDetail.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import Config from "../../model/config";
  2. import {formatTime, getDataSet, getEventParam, toast} from "../../utils/utils";
  3. import Api from "../../model/api";
  4. import {questionTypes, questionTypeTexts} from "../../model/enum";
  5. Page({
  6. data: {
  7. exams: [],
  8. answers: {},
  9. answerSize: 0,
  10. qType: questionTypeTexts,
  11. questionTypesEnum: questionTypes,
  12. id: '',
  13. teamId: '',
  14. courseId: '',
  15. eduStuId: '',
  16. scheduleId: '',
  17. },
  18. async onLoad(options) {
  19. let id = options.id;
  20. let teamId = options.teamId;
  21. let courseId = options.courseId;
  22. let eduStuId = options.eduStuId;
  23. let scheduleId = options.scheduleId;
  24. let res = await Api.getQuestion(id, true);
  25. let testQuestionsList = res.data.questionnaireList;
  26. let exams = [
  27. ...(testQuestionsList.singleChoice || []),
  28. ...(testQuestionsList.multipleChoice || []),
  29. ...(testQuestionsList.qaq || []),
  30. ];
  31. exams.forEach(item => {
  32. if (item && item.testAnswerList) {
  33. item.testAnswerList.forEach((test, index) => {
  34. test.opt = Config.OPT_TIP[index];
  35. })
  36. }
  37. })
  38. this.setData({
  39. exams,
  40. id, teamId, courseId, eduStuId, scheduleId
  41. })
  42. },
  43. slectAnswer(e) {
  44. let question = getDataSet(e, "q");
  45. let opt = getDataSet(e, "opt");
  46. let id = opt.id;
  47. let qId = question.id;
  48. let obj = this.data.answers[qId] || {};
  49. if (question.questionType == questionTypes.SINGLE) {
  50. Object.keys(obj).forEach(key => {
  51. if (key != id) {
  52. obj[key] = false
  53. }
  54. })
  55. }
  56. obj[id] = !obj[id];
  57. this.saveAnswer(qId, obj);
  58. },
  59. saveAnswer(qId, obj) {
  60. let newArr = {
  61. ...this.data.answers,
  62. [qId]: obj
  63. };
  64. let answerSize = 0;
  65. this.data.exams.forEach(preQ => {
  66. let answer = newArr[preQ.id];
  67. let answer2 = newArr[preQ.id + "*"];
  68. if (this.checkAnswers(preQ, answer, answer2)) {
  69. answerSize++;
  70. }
  71. })
  72. this.setData({
  73. answers: {
  74. ...this.data.answers,
  75. [qId]: obj
  76. },
  77. answerSize
  78. })
  79. },
  80. onChange(e) {
  81. let qId = getDataSet(e, "id");
  82. let detail = getEventParam(e);
  83. this.saveAnswer(qId, detail);
  84. },
  85. checkAnswers(preQ, answer, answer2) {
  86. let flag = false;
  87. if (answer && (preQ.questionType == questionTypes.SINGLE || preQ.questionType == questionTypes.MULTIPLE)) {
  88. flag = Object.keys(answer).some(key => {
  89. return answer[key];
  90. })
  91. } else {
  92. if (answer) {
  93. flag = !!answer.trim();
  94. } else {
  95. flag = answer2 ? !!answer2.trim() : false;
  96. }
  97. }
  98. return flag;
  99. },
  100. geneContent() {
  101. let arr = [];
  102. this.data.exams.forEach(question => {
  103. if (question.questionType == questionTypes.SINGLE || question.questionType == questionTypes.MULTIPLE) {
  104. let answer = this.data.answers[question.id];
  105. let opts = [];
  106. if (answer) {
  107. opts = Object.keys(answer).filter(a => {
  108. return answer[a];
  109. })
  110. }
  111. let otherAnswer = {};
  112. if (this.data.answers[question.id + "*"]) {
  113. otherAnswer.otherAnswer = this.data.answers[question.id + "*"]
  114. }
  115. arr.push({
  116. id: question.id,
  117. answer: opts.join(","),
  118. ...otherAnswer
  119. })
  120. } else {
  121. arr.push({
  122. id: question.id,
  123. answer: this.data.answers[question.id]
  124. })
  125. }
  126. });
  127. return JSON.stringify(arr);
  128. },
  129. async toPut(e) {
  130. let flag = true;
  131. this.data.exams.forEach(preQ => {
  132. let answer = this.data.answers[preQ.id];
  133. let answer2 = this.data.answers[preQ.id + "*"];
  134. if (!this.checkAnswers(preQ, answer, answer2)) {
  135. flag = false;
  136. }
  137. })
  138. if (!flag) {
  139. toast("存在未答的题目")
  140. return;
  141. }
  142. let content = this.geneContent();
  143. await Api.putQuestion({
  144. teamId: this.data.teamId,
  145. courseId: this.data.courseId,
  146. studentId: this.data.eduStuId,
  147. scheduleId: this.data.scheduleId,
  148. questionnaireId: this.data.id,
  149. answerBegin: formatTime(),
  150. answers: content,
  151. }, true);
  152. const eventChannel = this.getOpenerEventChannel()
  153. eventChannel.emit('answer');
  154. wx.navigateBack();
  155. },
  156. });