EditSelectQuestion.vue 21 KB

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