EditSelectQuestion.vue 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  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. <v-editor v-model="quesModel.quesBody"></v-editor>
  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. <v-editor v-model="option.optionBody"></v-editor>
  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. courseId: "",
  265. difficultyDegree: 0.5,
  266. publicity: true,
  267. answerType: "",
  268. quesProperties: [],
  269. score: 1,
  270. },
  271. difficultyDegreeList: [
  272. { label: 0.1, value: 0.1 },
  273. { label: 0.2, value: 0.2 },
  274. { label: 0.3, value: 0.3 },
  275. { label: 0.4, value: 0.4 },
  276. { label: 0.5, value: 0.5 },
  277. { label: 0.6, value: 0.6 },
  278. { label: 0.7, value: 0.7 },
  279. { label: 0.8, value: 0.8 },
  280. { label: 0.9, value: 0.9 },
  281. { label: 1.0, value: 1.0 },
  282. ],
  283. publicityList: [
  284. { label: "公开", value: true },
  285. { label: "非公开", value: false },
  286. ],
  287. answerTypes: [
  288. { label: "文本", value: "DIVERSIFIED_TEXT" },
  289. { label: "音频", value: "SINGLE_AUDIO" },
  290. ],
  291. coursePropertyList: [],
  292. coursePropertyName: "", //课程属性名
  293. firstPropertyList: [], //一级属性集合
  294. firstPropertyId: "", //一级属性id
  295. secondPropertyList: [], //二级属性集合
  296. secondPropertyId: "", //二级属性id
  297. //验证
  298. rules: {
  299. // quesBody: [
  300. // { required: true, message: '请输入题干', trigger: 'blur'}
  301. // ]
  302. },
  303. singleRightAnswer: "", //接收单选答案
  304. multipleRightAnswer: [], //接收多选答案
  305. };
  306. },
  307. computed: {
  308. answer() {
  309. if (this.quesModel.questionType == "SINGLE_ANSWER_QUESTION") {
  310. return this.singleRightAnswer;
  311. } else if (this.quesModel.questionType == "MULTIPLE_ANSWER_QUESTION") {
  312. var obj = this.multipleRightAnswer;
  313. return obj.sort().toString();
  314. }
  315. return this.quesModel.quesAnswer;
  316. },
  317. saveDisabled() {
  318. if (!this.questionId && this.quesModel.quesOptions.length == 0) {
  319. return true;
  320. }
  321. return false;
  322. },
  323. },
  324. created() {
  325. let questionId = this.$route.params.id;
  326. if (questionId) {
  327. this.questionId = questionId;
  328. this.quesModel["id"] = questionId;
  329. this.findQuestionById(questionId);
  330. }
  331. this.paperId = this.$route.params.paperId;
  332. this.paperDetailId = this.$route.params.paperDetailId;
  333. this.courseId = this.$route.params.courseId;
  334. let questionType = this.$route.params.questionType;
  335. if (questionType) {
  336. this.quesModel.questionType = questionType;
  337. }
  338. if (this.courseId) {
  339. this.$http
  340. .get(QUESTION_API + "/courseProperty/enable?courseId=" + this.courseId)
  341. .then((response) => {
  342. this.coursePropertyList = response.data;
  343. });
  344. }
  345. if (isEmptyStr(this.quesModel.answerType)) {
  346. this.quesModel.answerType = "DIVERSIFIED_TEXT";
  347. }
  348. },
  349. mounted() {
  350. setTimeout(() => {
  351. if (this.quesModel.id) {
  352. this.$store.commit("UPDATE_CURRENT_PATHS", ["试题管理", "试题修改"]);
  353. } else {
  354. this.$store.commit("UPDATE_CURRENT_PATHS", ["试题管理", "试题新增"]);
  355. }
  356. }, 200);
  357. },
  358. methods: {
  359. findQuestionById(questionId) {
  360. this.$http
  361. .get(QUESTION_API + "/question/" + questionId)
  362. .then((response) => {
  363. this.quesModel = response.data;
  364. if (this.quesModel.quesOptions) {
  365. for (var i = 0; i < this.quesModel.quesOptions.length; i++) {
  366. var option = this.quesModel.quesOptions[i];
  367. if (
  368. this.quesModel.questionType == "SINGLE_ANSWER_QUESTION" &&
  369. option.isCorrect == 1
  370. ) {
  371. this.singleRightAnswer = String.fromCharCode(65 + i);
  372. }
  373. if (
  374. this.quesModel.questionType == "MULTIPLE_ANSWER_QUESTION" &&
  375. option.isCorrect == 1
  376. ) {
  377. this.multipleRightAnswer.push(String.fromCharCode(65 + i));
  378. }
  379. }
  380. }
  381. if (isEmptyStr(this.quesModel.answerType)) {
  382. this.quesModel.answerType = "DIVERSIFIED_TEXT";
  383. }
  384. this.initCourseProperty();
  385. });
  386. },
  387. submitForm(formName) {
  388. this.$refs[formName].validate((valid) => {
  389. if (valid) {
  390. this.setRightAnswer();
  391. if (!this.hasAnswer()) {
  392. this.$notify({
  393. message: "请选择答案",
  394. type: "error",
  395. });
  396. return false;
  397. }
  398. if (this.questionId) {
  399. this.editQuestion();
  400. } else {
  401. this.saveNewQuestion();
  402. }
  403. } else {
  404. return false;
  405. }
  406. });
  407. },
  408. //修改试题
  409. editQuestion() {
  410. if (this.quesModel.quesOptions.length == 0) {
  411. this.$confirm("无选项将删除该试题, 是否继续?", "提示", {
  412. confirmButtonText: "确定",
  413. cancelButtonText: "取消",
  414. type: "warning",
  415. })
  416. .then(() => {
  417. this.fullscreenLoading = true;
  418. this.$http
  419. .delete(
  420. QUESTION_API + "/paper/deleteQuestion/" + this.quesModel.id
  421. )
  422. .then((response) => {
  423. if (response.data.length > 0) {
  424. this.deleteInfo =
  425. "该试题被试卷:" +
  426. response.data.join(" , ") +
  427. "使用,不能删除";
  428. this.deleteDialogVisible = true;
  429. } else {
  430. this.$notify({
  431. message: "删除成功",
  432. type: "success",
  433. });
  434. this.$router.push({ path: "/questions/question_list/0" });
  435. }
  436. });
  437. })
  438. .catch(() => {});
  439. } else {
  440. this.fullscreenLoading = true;
  441. this.$http.put(QUESTION_API + "/question", this.quesModel).then(() => {
  442. this.$notify({
  443. message: "保存成功",
  444. type: "success",
  445. });
  446. this.fullscreenLoading = false;
  447. });
  448. }
  449. },
  450. //新增试题
  451. saveNewQuestion() {
  452. if (!this.quesModel.difficultyDegree) {
  453. this.$notify({
  454. message: "请选择试题难度",
  455. type: "error",
  456. });
  457. return false;
  458. }
  459. this.fullscreenLoading = true;
  460. this.$http
  461. .post(
  462. QUESTION_API +
  463. "/paper/addQuestion/" +
  464. this.paperId +
  465. "/" +
  466. this.paperDetailId,
  467. this.quesModel
  468. )
  469. .then(() => {
  470. this.fullscreenLoading = false;
  471. this.$notify({
  472. type: "success",
  473. message: "保存成功",
  474. });
  475. this.$router.push({ path: "/questions/question_list/0" });
  476. })
  477. .catch(() => {
  478. this.fullscreenLoading = false;
  479. });
  480. },
  481. //新增选项
  482. addQuesOption() {
  483. this.quesModel.quesOptions.push({
  484. number: "",
  485. optionBody: "",
  486. isCorrect: "",
  487. });
  488. for (var i = 0; i < this.quesModel.quesOptions.length; i++) {
  489. this.quesModel.quesOptions[i]["number"] = i + 1;
  490. }
  491. },
  492. //删除选项
  493. removeQuesOption(option) {
  494. this.singleRightAnswer = "";
  495. this.multipleRightAnswer = [];
  496. let index = this.quesModel.quesOptions.indexOf(option);
  497. if (index !== -1) {
  498. this.quesModel.quesOptions.splice(index, 1);
  499. }
  500. if (this.quesModel.quesOptions.length > 0) {
  501. for (var i = 0; i < this.quesModel.quesOptions.length; i++) {
  502. var quesOption = this.quesModel.quesOptions[i];
  503. quesOption["number"] = i + 1;
  504. if (quesOption.isCorrect == 1) {
  505. var answerOrderNum = String.fromCharCode(65 + i);
  506. if (this.quesModel.questionType == "SINGLE_ANSWER_QUESTION") {
  507. this.singleRightAnswer = answerOrderNum;
  508. }
  509. if (this.quesModel.questionType == "MULTIPLE_ANSWER_QUESTION") {
  510. this.multipleRightAnswer.push(answerOrderNum);
  511. }
  512. }
  513. }
  514. }
  515. },
  516. //在正确的option上设置isCorrect=1
  517. setRightAnswer() {
  518. if (this.quesModel.quesOptions.length == 0) {
  519. return false;
  520. }
  521. let quesAnswer = [];
  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. if (option["isCorrect"]) quesAnswer.push(i + 1);
  534. }
  535. this.quesModel.quesAnswer = JSON.stringify(quesAnswer);
  536. },
  537. hasAnswer() {
  538. if (this.quesModel.questionType == "SINGLE_ANSWER_QUESTION") {
  539. if (!this.singleRightAnswer || this.singleRightAnswer == "") {
  540. return false;
  541. } else {
  542. return true;
  543. }
  544. }
  545. if (this.quesModel.questionType == "MULTIPLE_ANSWER_QUESTION") {
  546. if (!this.multipleRightAnswer || this.multipleRightAnswer == "") {
  547. return false;
  548. } else {
  549. return true;
  550. }
  551. }
  552. },
  553. backToQuesList() {
  554. this.$router.push({
  555. path: "/questions/question_list/1",
  556. });
  557. },
  558. //查询所有课程属性名
  559. initCourseProperty() {
  560. var courseId = this.quesModel.courseId;
  561. this.$http
  562. .get(QUESTION_API + "/courseProperty/enable?courseId=" + courseId)
  563. .then((response) => {
  564. this.coursePropertyList = response.data;
  565. });
  566. },
  567. //查询一级属性
  568. searchFirst() {
  569. this.firstPropertyId = "";
  570. this.secondPropertyId = "";
  571. this.secondPropertyList = [];
  572. for (let courseProperty of this.coursePropertyList) {
  573. if (courseProperty.name == this.coursePropertyName) {
  574. this.$http
  575. .get(QUESTION_API + "/property/first/" + courseProperty.id)
  576. .then((response) => {
  577. this.firstPropertyList = response.data;
  578. });
  579. }
  580. }
  581. },
  582. //查询二级属性
  583. searchSecond() {
  584. this.secondPropertyId = "";
  585. if (this.firstPropertyId) {
  586. this.$http
  587. .get(QUESTION_API + "/property/second/" + this.firstPropertyId)
  588. .then((response) => {
  589. this.secondPropertyList = response.data;
  590. });
  591. }
  592. },
  593. //新增属性
  594. insertProperty() {
  595. if (!this.checkInsertPro()) {
  596. return false;
  597. }
  598. var quesProperty = {
  599. id: "",
  600. coursePropertyName: "",
  601. firstProperty: {},
  602. secondProperty: {},
  603. };
  604. if (
  605. this.quesModel.quesProperties === undefined ||
  606. this.quesModel.quesProperties === null ||
  607. this.quesModel.quesProperties.length == 0
  608. ) {
  609. this.quesModel.quesProperties = [];
  610. }
  611. quesProperty.id =
  612. this.coursePropertyName +
  613. "-" +
  614. this.firstPropertyId +
  615. "-" +
  616. this.secondPropertyId;
  617. for (let quesPro of this.quesModel.quesProperties) {
  618. if (quesPro.id == quesProperty.id) {
  619. this.$notify({
  620. message: "该属性已存在,请重新选择",
  621. type: "error",
  622. });
  623. return false;
  624. }
  625. }
  626. quesProperty.coursePropertyName = this.coursePropertyName;
  627. //取到一级属性对象
  628. for (let property of this.firstPropertyList) {
  629. if (property.id == this.firstPropertyId) {
  630. quesProperty.firstProperty = property;
  631. }
  632. }
  633. //判断是否有二级属性
  634. if (
  635. this.secondPropertyList != undefined &&
  636. this.secondPropertyList.length > 0
  637. ) {
  638. if (!this.secondPropertyId) {
  639. this.$notify({
  640. message: "请选择二级属性",
  641. type: "error",
  642. });
  643. return false;
  644. }
  645. }
  646. //取到二级属性对象
  647. for (let property of this.secondPropertyList) {
  648. if (property.id == this.secondPropertyId) {
  649. quesProperty.secondProperty = property;
  650. }
  651. }
  652. this.quesModel.quesProperties.push(quesProperty);
  653. this.quesModel = Object.assign({}, this.quesModel);
  654. //清空下拉框
  655. this.coursePropertyName = "";
  656. this.firstPropertyId = "";
  657. this.secondPropertyId = "";
  658. this.firstPropertyList = [];
  659. this.secondPropertyList = [];
  660. },
  661. //删除属性
  662. handleClose(tag) {
  663. this.quesModel.quesProperties.splice(
  664. this.quesModel.quesProperties.indexOf(tag),
  665. 1
  666. );
  667. this.quesModel = Object.assign({}, this.quesModel);
  668. },
  669. //新增属性验证
  670. checkInsertPro() {
  671. if (!this.coursePropertyName) {
  672. this.$notify({
  673. message: "请选择属性",
  674. type: "error",
  675. });
  676. return false;
  677. }
  678. if (!this.firstPropertyId) {
  679. this.$notify({
  680. message: "请选择一级属性",
  681. type: "error",
  682. });
  683. return false;
  684. }
  685. return true;
  686. },
  687. },
  688. };
  689. </script>