EditSelectQuestion.vue 20 KB

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