BankedClozeQuestion.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <template>
  2. <div class="banked-cloze-question nested-question">
  3. <el-form
  4. ref="modalFormComp"
  5. :model="modalForm"
  6. :rules="rules"
  7. label-width="100px"
  8. >
  9. <el-form-item prop="quesBody" label="大题题干">
  10. <v-editor
  11. v-model="modalForm.quesBody"
  12. :enable-answer-point="IS_BANKED_CLOZE"
  13. @answer-point-changed="answerPointsChange"
  14. @change="quesBodyChange"
  15. ></v-editor>
  16. </el-form-item>
  17. <el-form-item
  18. v-for="(option, oindex) in modalForm.quesOptions"
  19. :key="option.nmuber"
  20. :prop="`quesOptions.${oindex}.optionBody`"
  21. :rules="optionRule"
  22. >
  23. <div class="question-edit-option">
  24. <div class="option-check">
  25. {{ (option.number - 1) | optionOrderWordFilter }}
  26. </div>
  27. <div class="option-body">
  28. <v-editor
  29. v-model="option.optionBody"
  30. @change="() => optionBodyChange(oindex)"
  31. ></v-editor>
  32. </div>
  33. <div class="option-delete">
  34. <el-button
  35. size="mini"
  36. circle
  37. type="primary"
  38. icon="el-icon-plus"
  39. title="新增"
  40. @click.prevent="addQuesOption(oindex)"
  41. ></el-button>
  42. <el-button
  43. size="mini"
  44. circle
  45. type="danger"
  46. icon="el-icon-delete"
  47. title="删除"
  48. :disabled="deleleOptionDisabled"
  49. @click.prevent="removeQuesOption(oindex)"
  50. ></el-button>
  51. </div>
  52. </div>
  53. </el-form-item>
  54. <el-form-item label="答题模式">
  55. <el-select v-model="modalForm.quesParam.matchingMode">
  56. <el-option
  57. v-for="item in matchingModes"
  58. :key="item.value"
  59. :label="item.label"
  60. :value="item.value"
  61. >
  62. </el-option>
  63. </el-select>
  64. </el-form-item>
  65. </el-form>
  66. <el-collapse v-model="activeNames">
  67. <el-collapse-item
  68. v-for="(subq, index) in modalForm.subQuestions"
  69. :key="subq.questionKey"
  70. :name="subq.questionKey"
  71. >
  72. <div slot="title" class="subq-header">
  73. <h3>第{{ index + 1 }}小题</h3>
  74. <div>
  75. <el-button
  76. v-if="!IS_BANKED_CLOZE"
  77. class="btn-danger"
  78. size="mini"
  79. type="text"
  80. icon="el-icon-delete"
  81. title="删除"
  82. @click.stop="removeSubQuestion(index)"
  83. ></el-button>
  84. </div>
  85. </div>
  86. <match-question
  87. ref="MatchQuestion"
  88. :question="subq"
  89. :parent-question="modalForm"
  90. ></match-question>
  91. </el-collapse-item>
  92. </el-collapse>
  93. <el-button
  94. v-if="!IS_BANKED_CLOZE"
  95. type="primary"
  96. size="small"
  97. icon="el-icon-circle-plus-outline"
  98. @click="addSubQuestion()"
  99. >新增小题</el-button
  100. >
  101. </div>
  102. </template>
  103. <script>
  104. import { isAnEmptyRichText } from "@/utils/utils";
  105. import { randomCode } from "@/plugins/utils";
  106. import {
  107. getInitQuestionModel,
  108. getMatchQuestionModel,
  109. } from "../model/questionModel";
  110. import MatchQuestion from "./MatchQuestion.vue";
  111. export default {
  112. name: "BankedClozeQuestion",
  113. components: { MatchQuestion },
  114. props: {
  115. question: {
  116. type: Object,
  117. default() {
  118. return {};
  119. },
  120. },
  121. },
  122. data() {
  123. return {
  124. modalForm: {},
  125. activeNames: [],
  126. rules: {
  127. quesBody: [
  128. {
  129. validator: (rule, value, callback) => {
  130. if (!this.IS_BANKED_CLOZE) return callback();
  131. if (!value || isAnEmptyRichText(value)) {
  132. return callback(new Error(`请输入题干`));
  133. }
  134. callback();
  135. },
  136. trigger: "change",
  137. },
  138. ],
  139. },
  140. optionRule: {
  141. validator: (rule, value, callback) => {
  142. if (!value || isAnEmptyRichText(value)) {
  143. return callback(new Error(`请输入选项内容`));
  144. }
  145. callback();
  146. },
  147. trigger: "change",
  148. },
  149. matchingModes: [
  150. { label: "单用", value: 1 },
  151. { label: "复用", value: 2 },
  152. ],
  153. };
  154. },
  155. computed: {
  156. IS_BANKED_CLOZE() {
  157. return this.modalForm.questionType === "BANKED_CLOZE";
  158. },
  159. deleleOptionDisabled() {
  160. return this.modalForm.quesOptions.length === 1;
  161. },
  162. },
  163. created() {
  164. this.initData();
  165. },
  166. methods: {
  167. initData() {
  168. this.modalForm = this.$objAssign(
  169. getInitQuestionModel("PARAGRAPH_MATCHING"),
  170. this.question
  171. );
  172. },
  173. optionBodyChange(oindex) {
  174. this.$refs.modalFormComp.validateField(
  175. `quesOptions.${oindex}.optionBody`,
  176. () => {}
  177. );
  178. },
  179. addQuesOption(index) {
  180. if (this.modalForm.quesOptions.length >= 20) {
  181. this.$message.error("选项最多20个");
  182. return;
  183. }
  184. this.modalForm.quesOptions.splice(index + 1, 0, {
  185. number: 0,
  186. body: { sections: [] },
  187. });
  188. this.resetNumberAndSaveOptions();
  189. },
  190. removeQuesOption(index) {
  191. if (this.deleleOptionDisabled) return;
  192. this.modalForm.quesOptions.splice(index, 1);
  193. this.resetNumberAndSaveOptions();
  194. this.$nextTick(() => {
  195. for (let i = 0; i < this.$refs.MatchQuestion.length; i++) {
  196. this.$refs.MatchQuestion[i].optionChange();
  197. }
  198. });
  199. },
  200. resetNumberAndSaveOptions() {
  201. this.modalForm.quesOptions.forEach((option, index) => {
  202. option.number = index + 1;
  203. });
  204. },
  205. addSubQuestion(index = null) {
  206. let newQuestion = getMatchQuestionModel();
  207. newQuestion.questionKey = randomCode();
  208. newQuestion.courseId = this.question.courseId;
  209. const nindex =
  210. index === null ? this.modalForm.subQuestions.length : index + 1;
  211. this.modalForm.subQuestions.splice(nindex, 0, newQuestion);
  212. if (!this.activeNames.includes(newQuestion.questionKey))
  213. this.activeNames.push(newQuestion.questionKey);
  214. },
  215. async removeSubQuestion(index) {
  216. if (this.IS_BANKED_CLOZE) return;
  217. const res = await this.$confirm("确定要删除该小题吗?", "提示", {
  218. type: "warning",
  219. }).catch(() => {});
  220. if (res !== "confirm") return;
  221. const delQuestionKey = this.modalForm.subQuestions[index].questionKey;
  222. this.activeNames = this.activeNames.filter((k) => k !== delQuestionKey);
  223. this.modalForm.subQuestions.splice(index, 1);
  224. },
  225. answerPointsChange(answerPointsChanged) {
  226. let subQuestions = [];
  227. answerPointsChanged.forEach((item) => {
  228. const order = Number(item);
  229. if (order === 0) {
  230. let newQuestion = getMatchQuestionModel();
  231. newQuestion.questionKey = randomCode();
  232. newQuestion.courseId = this.question.courseId;
  233. subQuestions.push(newQuestion);
  234. } else {
  235. subQuestions.push(this.modalForm.subQuestions[order - 1]);
  236. }
  237. });
  238. this.modalForm.subQuestions = subQuestions;
  239. this.activeNames = subQuestions.map((item) => item.questionKey);
  240. },
  241. quesBodyChange() {
  242. this.$refs.modalFormComp.validateField(`quesBody`, () => {});
  243. },
  244. async validate() {
  245. const valid = await this.$refs.modalFormComp.validate().catch(() => {});
  246. if (!valid) return;
  247. let errorQuestionIndexs = [];
  248. for (let i = 0; i < this.$refs.MatchQuestion.length; i++) {
  249. const compInst = this.$refs.MatchQuestion[i];
  250. const res = await compInst.validate().catch(() => {});
  251. if (!res) {
  252. errorQuestionIndexs.push(i + 1);
  253. }
  254. }
  255. if (errorQuestionIndexs.length) {
  256. this.$message.error(
  257. `请完成第${errorQuestionIndexs.join()}小题的编辑!`
  258. );
  259. return Promise.reject();
  260. }
  261. return true;
  262. },
  263. getData() {
  264. this.modalForm.subQuestions = this.$refs.MatchQuestion.map((compInst) =>
  265. compInst.getData()
  266. );
  267. return this.modalForm;
  268. },
  269. },
  270. };
  271. </script>