EditOtherQuestion.vue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. <!-- 编辑填空,问答,判断题 -->
  2. <template>
  3. <section v-loading="fullscreenLoading" class="content edit-question">
  4. <div class="part-box">
  5. <div class="part-box-header">
  6. <h1 class="part-box-title">
  7. {{ quesModel.id ? "试题修改" : "试题新增" }}
  8. </h1>
  9. <div>
  10. <el-button
  11. type="primary"
  12. icon="icon icon-save-white"
  13. :disabled="saveDisabled"
  14. @click="saveQues('quesModel')"
  15. >保存</el-button
  16. >
  17. <el-button
  18. type="danger"
  19. plain
  20. icon="icon icon-back"
  21. @click="backToQuesList()"
  22. >返回
  23. </el-button>
  24. </div>
  25. </div>
  26. <el-form
  27. ref="quesModel"
  28. class="padding-tb-20 form-tight"
  29. :model="quesModel"
  30. :rules="rules"
  31. label-position="right"
  32. label-width="100px"
  33. >
  34. <el-form-item label="题型">
  35. <el-select
  36. v-model="quesModel.questionType"
  37. :disabled="true"
  38. placeholder="请输入题型"
  39. >
  40. <el-option
  41. v-for="item in questionTypes"
  42. :key="item.value"
  43. :label="item.label"
  44. :value="item.value"
  45. >
  46. </el-option>
  47. </el-select>
  48. </el-form-item>
  49. <!-- created by weiwenhai -->
  50. <el-form-item label="难度">
  51. <el-select
  52. v-model="quesModel.difficultyDegree"
  53. placeholder="请输入难度"
  54. >
  55. <el-option
  56. v-for="item in difficultyDegreeList"
  57. :key="item.value"
  58. :label="item.label"
  59. :value="item.value"
  60. >
  61. </el-option>
  62. </el-select>
  63. </el-form-item>
  64. <el-form-item label="公开度">
  65. <el-select v-model="quesModel.publicity" placeholder="请输入公开度">
  66. <el-option
  67. v-for="item in publicityList"
  68. :key="item.value"
  69. :label="item.label"
  70. :value="item.value"
  71. >
  72. </el-option>
  73. </el-select>
  74. </el-form-item>
  75. <el-form-item
  76. v-if="quesModel.questionType == 'TEXT_ANSWER_QUESTION'"
  77. label="作答类型"
  78. >
  79. <el-select v-model="quesModel.answerType">
  80. <el-option
  81. v-for="item in answerTypes"
  82. :key="item.value"
  83. :label="item.label"
  84. :value="item.value"
  85. >
  86. </el-option>
  87. </el-select>
  88. </el-form-item>
  89. <el-form-item label="属性列表">
  90. <el-tooltip
  91. v-for="(content, index) in quesModel.quesProperties"
  92. :key="index"
  93. placement="top"
  94. >
  95. <div slot="content">
  96. <span v-if="content.firstProperty != null"
  97. >一级属性:{{ content.firstProperty.name }}({{
  98. content.firstProperty.code
  99. }})</span
  100. ><br />
  101. <span v-if="content.secondProperty != null"
  102. >二级属性:{{ content.secondProperty.name }}({{
  103. content.secondProperty.code
  104. }})</span
  105. >
  106. </div>
  107. <el-tag
  108. :key="content.id"
  109. style="margin-right: 5px"
  110. closable
  111. effect="dark"
  112. type="primary"
  113. @close="handleClose(content)"
  114. >
  115. {{ content.coursePropertyName || content.courseProperty.name }}
  116. </el-tag>
  117. </el-tooltip>
  118. </el-form-item>
  119. <el-row :gutter="10">
  120. <el-col :xs="6" :sm="6" :md="6" :lg="6">
  121. <el-form-item label="属性名">
  122. <el-select
  123. v-model="coursePropertyName"
  124. placeholder="属性名"
  125. class="property_with"
  126. @change="searchFirst"
  127. >
  128. <el-option
  129. v-for="item in coursePropertyList"
  130. :key="item.name"
  131. :label="item.name"
  132. :value="item.name"
  133. >
  134. </el-option>
  135. </el-select>
  136. </el-form-item>
  137. </el-col>
  138. <el-col :xs="6" :sm="6" :md="6" :lg="6">
  139. <el-form-item label="一级">
  140. <el-select
  141. v-model="firstPropertyId"
  142. placeholder="一级"
  143. class="property_with"
  144. @change="searchSecond"
  145. >
  146. <el-option
  147. v-for="item in firstPropertyList"
  148. :key="item.id"
  149. :label="item.name"
  150. :value="item.id"
  151. >
  152. </el-option>
  153. </el-select>
  154. </el-form-item>
  155. </el-col>
  156. <el-col :xs="6" :sm="6" :md="6" :lg="6">
  157. <el-form-item label="二级">
  158. <el-select
  159. v-model="secondPropertyId"
  160. placeholder="二级"
  161. class="property_with"
  162. >
  163. <el-option
  164. v-for="item in secondPropertyList"
  165. :key="item.id"
  166. :label="item.name"
  167. :value="item.id"
  168. >
  169. </el-option>
  170. </el-select>
  171. </el-form-item>
  172. </el-col>
  173. <el-col :xs="3" :sm="3" :md="3" :lg="3">
  174. <el-form-item label-width="0px">
  175. <el-button
  176. type="primary"
  177. icon="icon icon-plus-white"
  178. @click="insertProperty"
  179. >新增属性</el-button
  180. >
  181. </el-form-item>
  182. </el-col>
  183. </el-row>
  184. </el-form>
  185. </div>
  186. <div class="part-box">
  187. <el-form class="form-tight" label-width="100px">
  188. <!-- end -->
  189. <el-form-item label="题干" prop="quesBody">
  190. <v-editor
  191. v-model="quesModel.quesBody"
  192. :enable-answer-point="enableAnswerPoint"
  193. @change="quesBodyChange"
  194. ></v-editor>
  195. </el-form-item>
  196. <el-form-item
  197. v-for="(option, index) in quesModel.quesOptions"
  198. :key="option.number"
  199. >
  200. <div class="question-edit-option">
  201. <div class="option-check">
  202. {{ index | optionOrderWordFilter }}
  203. </div>
  204. <div class="option-body">
  205. <v-editor v-model="option.quesBody"></v-editor>
  206. </div>
  207. </div>
  208. </el-form-item>
  209. <!-- <div class="line-seperator"></div> -->
  210. <!-- 非套题 -->
  211. <!-- <el-form-item
  212. v-if="
  213. quesModel.questionType !== 'NESTED_ANSWER_QUESTION' &&
  214. quesModel.questionType !== 'BOOL_ANSWER_QUESTION'
  215. "
  216. label="答案"
  217. prop="quesAnswer"
  218. >
  219. <v-editor v-model="quesModel.quesAnswer"></v-editor>
  220. </el-form-item> -->
  221. <el-form-item
  222. v-if="quesModel.questionType === 'FILL_BLANK_QUESTION'"
  223. label="答案"
  224. prop="quesAnswer"
  225. >
  226. <div v-if="quesAnswer" class="option-list">
  227. <div
  228. v-for="(opt, index) of quesAnswer"
  229. :key="index"
  230. class="option-item"
  231. >
  232. <div class="option-item-info">
  233. <span>({{ index + 1 }})</span>
  234. </div>
  235. <v-editor
  236. :value="opt"
  237. class="option-item-body"
  238. @change="(val) => updateAnswerPoint(index, val)"
  239. />
  240. </div>
  241. </div>
  242. </el-form-item>
  243. <!-- 问答题 -->
  244. <el-form-item
  245. v-if="quesModel.questionType === 'TEXT_ANSWER_QUESTION'"
  246. label="答案"
  247. prop="quesAnswer"
  248. >
  249. <v-editor v-model="quesAnswer" @change="textAnswerChange"></v-editor>
  250. </el-form-item>
  251. <!-- 判断题 -->
  252. <el-form-item
  253. v-if="quesModel.questionType == 'BOOL_ANSWER_QUESTION'"
  254. label="答案"
  255. prop="quesAnswer"
  256. >
  257. <el-select
  258. v-model="quesAnswer"
  259. placeholder="请选择"
  260. @change="boolAnswerChange"
  261. >
  262. <el-option
  263. v-for="op in options"
  264. :key="op.value"
  265. :label="op.label"
  266. :value="op.value"
  267. >
  268. </el-option>
  269. </el-select>
  270. </el-form-item>
  271. </el-form>
  272. </div>
  273. </section>
  274. </template>
  275. <script>
  276. import { QUESTION_API } from "@/constants/constants";
  277. import { isEmptyStr, QUESTION_TYPES } from "../constants/constants";
  278. export default {
  279. name: "EditOtherApp",
  280. data() {
  281. return {
  282. fullscreenLoading: false,
  283. questionTypes: QUESTION_TYPES,
  284. courseName: "",
  285. courseNo: "",
  286. paperDetailId: "",
  287. paperId: "",
  288. quesModel: {
  289. courseName: "",
  290. courseNo: "",
  291. quesAnswer: "",
  292. quesBody: "",
  293. questionType: "",
  294. difficultyDegree: 0.5,
  295. publicity: true,
  296. answerType: "",
  297. quesProperties: [],
  298. score: 0,
  299. },
  300. quesAnswer: null,
  301. prevAnswerPointCount: 0,
  302. options: [
  303. {
  304. value: 0,
  305. label: "错误",
  306. },
  307. {
  308. value: 1,
  309. label: "正确",
  310. },
  311. ],
  312. difficultyDegreeList: [
  313. { label: 0.1, value: 0.1 },
  314. { label: 0.2, value: 0.2 },
  315. { label: 0.3, value: 0.3 },
  316. { label: 0.4, value: 0.4 },
  317. { label: 0.5, value: 0.5 },
  318. { label: 0.6, value: 0.6 },
  319. { label: 0.7, value: 0.7 },
  320. { label: 0.8, value: 0.8 },
  321. { label: 0.9, value: 0.9 },
  322. { label: 1.0, value: 1.0 },
  323. ],
  324. publicityList: [
  325. { label: "公开", value: true },
  326. { label: "非公开", value: false },
  327. ],
  328. answerTypes: [
  329. { label: "文本", value: "DIVERSIFIED_TEXT" },
  330. { label: "音频", value: "SINGLE_AUDIO" },
  331. ],
  332. coursePropertyList: [],
  333. coursePropertyName: "", //课程属性名
  334. firstPropertyList: [], //一级属性集合
  335. firstPropertyId: "", //一级属性id
  336. secondPropertyList: [], //二级属性集合
  337. secondPropertyId: "", //二级属性id
  338. //验证
  339. rules: {
  340. quesAnswer: [
  341. { required: true, message: "请输入答案", trigger: "change" },
  342. ],
  343. },
  344. };
  345. },
  346. computed: {
  347. saveDisabled() {
  348. if (!this.questionId && this.quesModel.quesOptions.length == 0) {
  349. return true;
  350. }
  351. return false;
  352. },
  353. enableAnswerPoint() {
  354. return this.quesModel.questionType === "FILL_BLANK_QUESTION";
  355. },
  356. },
  357. created() {
  358. this.paperId = this.$route.params.paperId;
  359. this.paperDetailId = this.$route.params.paperDetailId;
  360. this.courseName = this.$route.params.courseName;
  361. this.courseNo = this.$route.params.courseNo;
  362. let questionType = this.$route.params.questionType;
  363. if (questionType) {
  364. this.quesModel.questionType = questionType;
  365. }
  366. this.questionId = this.$route.params.id;
  367. if (this.questionId) {
  368. this.quesModel["id"] = this.questionId;
  369. this.getQues(this.questionId);
  370. }
  371. if (this.courseNo) {
  372. this.$http
  373. .get(
  374. QUESTION_API +
  375. "/courseProperty/enable?courseCode=" +
  376. encodeURIComponent(this.courseNo)
  377. )
  378. .then((response) => {
  379. this.coursePropertyList = response.data;
  380. });
  381. }
  382. if (isEmptyStr(this.quesModel.answerType)) {
  383. this.quesModel.answerType = "DIVERSIFIED_TEXT";
  384. }
  385. },
  386. mounted() {
  387. setTimeout(() => {
  388. if (this.quesModel.id) {
  389. this.$store.commit("UPDATE_CURRENT_PATHS", ["试题管理", "试题修改"]);
  390. } else {
  391. this.$store.commit("UPDATE_CURRENT_PATHS", ["试题管理", "试题新增"]);
  392. }
  393. }, 200);
  394. },
  395. methods: {
  396. boolAnswerChange(val) {
  397. this.quesModel.quesAnswer = val ? "true" : "false";
  398. },
  399. textAnswerChange(val) {
  400. this.quesModel.quesAnswer = JSON.stringify([val]);
  401. },
  402. resetNumberAndSaveAnswerPoints(answer) {
  403. this.quesAnswer = answer.map((element, index) => {
  404. element.index = index + 1;
  405. return element;
  406. });
  407. this.quesModel.quesAnswer = JSON.stringify(this.quesAnswer);
  408. },
  409. updateAnswerPoint(index, value) {
  410. // console.log(index, this.question.answer[index]);
  411. this.quesAnswer[index] = { ...value };
  412. this.resetNumberAndSaveAnswerPoints(this.quesAnswer);
  413. },
  414. getAnswerPointCount(bodyJson) {
  415. let count = 0;
  416. bodyJson.sections.forEach((section) => {
  417. section.blocks.forEach((block) => {
  418. if (block.type === "cloze") count++;
  419. });
  420. });
  421. return count;
  422. },
  423. quesBodyChange(quesBodyJson) {
  424. this.quesModel.quesBody = quesBodyJson;
  425. let curPonitCount = this.getAnswerPointCount(quesBodyJson);
  426. if (curPonitCount === this.prevAnswerPointCount) return;
  427. this.prevAnswerPointCount = curPonitCount;
  428. let newAnswer = [];
  429. for (let i = 0; i < curPonitCount; i++) {
  430. newAnswer.push(this.quesAnswer[i] || { sections: [] });
  431. }
  432. this.resetNumberAndSaveAnswerPoints(newAnswer);
  433. },
  434. saveQues(formName) {
  435. if (!this.quesModel.difficultyDegree) {
  436. this.$notify({
  437. message: "请选择试题难度",
  438. type: "error",
  439. });
  440. return;
  441. }
  442. if (this.quesModel.questionType === "FILL_BLANK_QUESTION") {
  443. const pointCount = this.getAnswerPointCount(this.quesModel.quesBody);
  444. if (!pointCount) {
  445. this.$notify({
  446. message: "请插入答题点",
  447. type: "error",
  448. });
  449. return;
  450. }
  451. }
  452. this.$refs[formName].validate((valid) => {
  453. if (valid) {
  454. if (this.questionId) {
  455. this.fullscreenLoading = true;
  456. //修改逻辑
  457. this.$http
  458. .put(QUESTION_API + "/question", this.quesModel)
  459. .then(() => {
  460. this.$notify({
  461. message: "保存成功",
  462. type: "success",
  463. });
  464. this.fullscreenLoading = false;
  465. });
  466. } else {
  467. //新增逻辑
  468. this.quesModel.courseNo = this.courseNo;
  469. this.quesModel.courseName = this.courseName;
  470. this.fullscreenLoading = true;
  471. this.$http
  472. .post(
  473. QUESTION_API +
  474. "/paper/addQuestion/" +
  475. this.paperId +
  476. "/" +
  477. this.paperDetailId,
  478. this.quesModel
  479. )
  480. .then(() => {
  481. this.fullscreenLoading = false;
  482. this.$notify({
  483. type: "success",
  484. message: `保存成功`,
  485. });
  486. this.$router.push({ path: "/questions/question_list/0" });
  487. })
  488. .catch(() => {
  489. this.fullscreenLoading = false;
  490. });
  491. }
  492. } else {
  493. return false;
  494. }
  495. });
  496. },
  497. getQues(id) {
  498. this.$http.get(QUESTION_API + "/question/" + id).then((response) => {
  499. this.quesModel = response.data;
  500. if (isEmptyStr(this.quesModel.answerType)) {
  501. this.quesModel.answerType = "DIVERSIFIED_TEXT";
  502. }
  503. if (this.quesModel.questionType === "BOOL_ANSWER_QUESTION") {
  504. this.quesAnswer = this.quesModel.quesAnswer === "true" ? 1 : 0;
  505. } else if (this.quesModel.questionType === "TEXT_ANSWER_QUESTION") {
  506. const answer = JSON.parse(this.quesModel.quesAnswer);
  507. this.quesAnswer = answer[0];
  508. } else if (this.quesModel.questionType === "FILL_BLANK_QUESTION") {
  509. const answer = JSON.parse(this.quesModel.quesAnswer);
  510. this.quesAnswer = answer;
  511. this.prevAnswerPointCount = this.getAnswerPointCount(
  512. this.quesModel.quesBody
  513. );
  514. } else {
  515. this.quesAnswer = null;
  516. }
  517. this.initCourseProperty();
  518. });
  519. },
  520. backToQuesList() {
  521. this.$router.push({
  522. path: "/questions/question_list/1",
  523. });
  524. },
  525. //查询所有课程属性名
  526. initCourseProperty() {
  527. var code = this.quesModel.course.code;
  528. if (!code) return;
  529. this.$http
  530. .get(QUESTION_API + "/courseProperty/code/" + code)
  531. .then((response) => {
  532. this.coursePropertyList = response.data;
  533. });
  534. },
  535. //查询一级属性
  536. searchFirst() {
  537. this.firstPropertyId = "";
  538. this.secondPropertyId = "";
  539. this.secondPropertyList = [];
  540. for (let courseProperty of this.coursePropertyList) {
  541. if (courseProperty.name == this.coursePropertyName) {
  542. this.$http
  543. .get(QUESTION_API + "/property/first/" + courseProperty.id)
  544. .then((response) => {
  545. this.firstPropertyList = response.data;
  546. });
  547. }
  548. }
  549. },
  550. //查询二级属性
  551. searchSecond() {
  552. this.secondPropertyId = "";
  553. this.$http
  554. .get(QUESTION_API + "/property/second/" + this.firstPropertyId)
  555. .then((response) => {
  556. this.secondPropertyList = response.data;
  557. });
  558. },
  559. //新增属性
  560. insertProperty() {
  561. if (!this.checkInsertPro()) {
  562. return false;
  563. }
  564. var quesProperty = {
  565. id: "",
  566. coursePropertyName: "",
  567. firstProperty: {},
  568. secondProperty: {},
  569. };
  570. if (
  571. this.quesModel.quesProperties === undefined ||
  572. this.quesModel.quesProperties === null ||
  573. this.quesModel.quesProperties.length == 0
  574. ) {
  575. this.quesModel.quesProperties = [];
  576. }
  577. quesProperty.id =
  578. this.coursePropertyName +
  579. "-" +
  580. this.firstPropertyId +
  581. "-" +
  582. this.secondPropertyId;
  583. for (let quesPro of this.quesModel.quesProperties) {
  584. if (quesPro.id == quesProperty.id) {
  585. this.$notify({
  586. message: "该属性已存在,请重新选择",
  587. type: "error",
  588. });
  589. return false;
  590. }
  591. }
  592. quesProperty.coursePropertyName = this.coursePropertyName;
  593. //取到一级属性对象
  594. for (let property of this.firstPropertyList) {
  595. if (property.id == this.firstPropertyId) {
  596. quesProperty.firstProperty = property;
  597. }
  598. }
  599. //判断是否有二级属性
  600. if (
  601. this.secondPropertyList != undefined &&
  602. this.secondPropertyList.length > 0
  603. ) {
  604. if (!this.secondPropertyId) {
  605. this.$notify({
  606. message: "请选择二级属性",
  607. type: "error",
  608. });
  609. return false;
  610. }
  611. }
  612. //取到二级属性对象
  613. for (let property of this.secondPropertyList) {
  614. if (property.id == this.secondPropertyId) {
  615. quesProperty.secondProperty = property;
  616. }
  617. }
  618. this.quesModel.quesProperties.push(quesProperty);
  619. this.quesModel = Object.assign({}, this.quesModel);
  620. //清空下拉框
  621. this.coursePropertyName = "";
  622. this.firstPropertyId = "";
  623. this.secondPropertyId = "";
  624. this.firstPropertyList = [];
  625. this.secondPropertyList = [];
  626. },
  627. //删除属性
  628. handleClose(tag) {
  629. this.quesModel.quesProperties.splice(
  630. this.quesModel.quesProperties.indexOf(tag),
  631. 1
  632. );
  633. this.quesModel = Object.assign({}, this.quesModel);
  634. },
  635. //新增属性验证
  636. checkInsertPro() {
  637. if (!this.coursePropertyName) {
  638. this.$notify({
  639. message: "请选择属性",
  640. type: "error",
  641. });
  642. return false;
  643. }
  644. if (!this.firstPropertyId) {
  645. this.$notify({
  646. message: "请选择一级属性",
  647. type: "error",
  648. });
  649. return false;
  650. }
  651. return true;
  652. },
  653. },
  654. };
  655. </script>