EditSelectQuestion.vue 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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() {
  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(
  342. QUESTION_API +
  343. "/courseProperty/enable?courseCode=" +
  344. encodeURIComponent(this.courseNo)
  345. )
  346. .then((response) => {
  347. this.coursePropertyList = response.data;
  348. });
  349. }
  350. if (isEmptyStr(this.quesModel.answerType)) {
  351. this.quesModel.answerType = "DIVERSIFIED_TEXT";
  352. }
  353. },
  354. mounted() {
  355. setTimeout(() => {
  356. if (this.quesModel.id) {
  357. this.$store.commit("UPDATE_CURRENT_PATHS", ["试题管理", "试题修改"]);
  358. } else {
  359. this.$store.commit("UPDATE_CURRENT_PATHS", ["试题管理", "试题新增"]);
  360. }
  361. }, 200);
  362. },
  363. methods: {
  364. findQuestionById(questionId) {
  365. this.$http
  366. .get(QUESTION_API + "/question/" + questionId)
  367. .then((response) => {
  368. this.quesModel = response.data;
  369. if (this.quesModel.quesOptions) {
  370. for (var i = 0; i < this.quesModel.quesOptions.length; i++) {
  371. var option = this.quesModel.quesOptions[i];
  372. if (
  373. this.quesModel.questionType == "SINGLE_ANSWER_QUESTION" &&
  374. option.isCorrect == 1
  375. ) {
  376. this.singleRightAnswer = String.fromCharCode(65 + i);
  377. }
  378. if (
  379. this.quesModel.questionType == "MULTIPLE_ANSWER_QUESTION" &&
  380. option.isCorrect == 1
  381. ) {
  382. this.multipleRightAnswer.push(String.fromCharCode(65 + i));
  383. }
  384. }
  385. }
  386. if (isEmptyStr(this.quesModel.answerType)) {
  387. this.quesModel.answerType = "DIVERSIFIED_TEXT";
  388. }
  389. this.initCourseProperty();
  390. });
  391. },
  392. submitForm(formName) {
  393. this.$refs[formName].validate((valid) => {
  394. if (valid) {
  395. this.setRightAnswer();
  396. if (!this.hasAnswer()) {
  397. this.$notify({
  398. message: "请选择答案",
  399. type: "error",
  400. });
  401. return false;
  402. }
  403. if (this.questionId) {
  404. this.editQuestion();
  405. } else {
  406. this.saveNewQuestion();
  407. }
  408. } else {
  409. return false;
  410. }
  411. });
  412. },
  413. //修改试题
  414. editQuestion() {
  415. if (this.quesModel.quesOptions.length == 0) {
  416. this.$confirm("无选项将删除该试题, 是否继续?", "提示", {
  417. confirmButtonText: "确定",
  418. cancelButtonText: "取消",
  419. type: "warning",
  420. })
  421. .then(() => {
  422. this.fullscreenLoading = true;
  423. this.$http
  424. .delete(
  425. QUESTION_API + "/paper/deleteQuestion/" + this.quesModel.id
  426. )
  427. .then((response) => {
  428. if (response.data.length > 0) {
  429. this.deleteInfo =
  430. "该试题被试卷:" +
  431. response.data.join(" , ") +
  432. "使用,不能删除";
  433. this.deleteDialogVisible = true;
  434. } else {
  435. this.$notify({
  436. message: "删除成功",
  437. type: "success",
  438. });
  439. this.$router.push({ path: "/questions/question_list/0" });
  440. }
  441. });
  442. })
  443. .catch(() => {});
  444. } else {
  445. this.fullscreenLoading = true;
  446. this.$http.put(QUESTION_API + "/question", this.quesModel).then(() => {
  447. this.$notify({
  448. message: "保存成功",
  449. type: "success",
  450. });
  451. this.fullscreenLoading = false;
  452. });
  453. }
  454. },
  455. //新增试题
  456. saveNewQuestion() {
  457. if (!this.quesModel.difficultyDegree) {
  458. this.$notify({
  459. message: "请选择试题难度",
  460. type: "error",
  461. });
  462. return false;
  463. }
  464. this.fullscreenLoading = true;
  465. this.$http
  466. .post(
  467. QUESTION_API +
  468. "/paper/addQuestion/" +
  469. this.paperId +
  470. "/" +
  471. this.paperDetailId,
  472. this.quesModel
  473. )
  474. .then(() => {
  475. this.fullscreenLoading = false;
  476. this.$notify({
  477. type: "success",
  478. message: "保存成功",
  479. });
  480. this.$router.push({ path: "/questions/question_list/0" });
  481. })
  482. .catch(() => {
  483. this.fullscreenLoading = false;
  484. });
  485. },
  486. //新增选项
  487. addQuesOption() {
  488. this.quesModel.quesOptions.push({
  489. number: "",
  490. optionBody: "",
  491. isCorrect: "",
  492. });
  493. for (var i = 0; i < this.quesModel.quesOptions.length; i++) {
  494. this.quesModel.quesOptions[i]["number"] = i + 1;
  495. }
  496. },
  497. //删除选项
  498. removeQuesOption(option) {
  499. this.singleRightAnswer = "";
  500. this.multipleRightAnswer = [];
  501. let index = this.quesModel.quesOptions.indexOf(option);
  502. if (index !== -1) {
  503. this.quesModel.quesOptions.splice(index, 1);
  504. }
  505. if (this.quesModel.quesOptions.length > 0) {
  506. for (var i = 0; i < this.quesModel.quesOptions.length; i++) {
  507. var quesOption = this.quesModel.quesOptions[i];
  508. quesOption["number"] = i + 1;
  509. if (quesOption.isCorrect == 1) {
  510. var answerOrderNum = String.fromCharCode(65 + i);
  511. if (this.quesModel.questionType == "SINGLE_ANSWER_QUESTION") {
  512. this.singleRightAnswer = answerOrderNum;
  513. }
  514. if (this.quesModel.questionType == "MULTIPLE_ANSWER_QUESTION") {
  515. this.multipleRightAnswer.push(answerOrderNum);
  516. }
  517. }
  518. }
  519. }
  520. },
  521. //在正确的option上设置isCorrect=1
  522. setRightAnswer() {
  523. if (this.quesModel.quesOptions.length == 0) {
  524. return false;
  525. }
  526. for (var i = 0; i < this.quesModel.quesOptions.length; i++) {
  527. var option = this.quesModel.quesOptions[i];
  528. var answerOrderNum = String.fromCharCode(65 + i);
  529. if (this.quesModel.questionType == "SINGLE_ANSWER_QUESTION") {
  530. option["isCorrect"] =
  531. answerOrderNum == this.singleRightAnswer ? 1 : 0;
  532. }
  533. if (this.quesModel.questionType == "MULTIPLE_ANSWER_QUESTION") {
  534. option["isCorrect"] =
  535. this.multipleRightAnswer.indexOf(answerOrderNum) > -1 ? 1 : 0;
  536. }
  537. }
  538. },
  539. hasAnswer() {
  540. if (this.quesModel.questionType == "SINGLE_ANSWER_QUESTION") {
  541. if (!this.singleRightAnswer || this.singleRightAnswer == "") {
  542. return false;
  543. } else {
  544. return true;
  545. }
  546. }
  547. if (this.quesModel.questionType == "MULTIPLE_ANSWER_QUESTION") {
  548. if (!this.multipleRightAnswer || this.multipleRightAnswer == "") {
  549. return false;
  550. } else {
  551. return true;
  552. }
  553. }
  554. },
  555. backToQuesList() {
  556. this.$router.push({
  557. path: "/questions/question_list/1",
  558. });
  559. },
  560. //查询所有课程属性名
  561. initCourseProperty() {
  562. var code = this.quesModel.course.code;
  563. this.$http
  564. .get(
  565. QUESTION_API +
  566. "/courseProperty/enable?courseCode=" +
  567. encodeURIComponent(code)
  568. )
  569. .then((response) => {
  570. this.coursePropertyList = response.data;
  571. });
  572. },
  573. //查询一级属性
  574. searchFirst() {
  575. this.firstPropertyId = "";
  576. this.secondPropertyId = "";
  577. this.secondPropertyList = [];
  578. for (let courseProperty of this.coursePropertyList) {
  579. if (courseProperty.name == this.coursePropertyName) {
  580. this.$http
  581. .get(QUESTION_API + "/property/first/" + courseProperty.id)
  582. .then((response) => {
  583. this.firstPropertyList = response.data;
  584. });
  585. }
  586. }
  587. },
  588. //查询二级属性
  589. searchSecond() {
  590. this.secondPropertyId = "";
  591. if (this.firstPropertyId) {
  592. this.$http
  593. .get(QUESTION_API + "/property/second/" + this.firstPropertyId)
  594. .then((response) => {
  595. this.secondPropertyList = response.data;
  596. });
  597. }
  598. },
  599. //新增属性
  600. insertProperty() {
  601. if (!this.checkInsertPro()) {
  602. return false;
  603. }
  604. var quesProperty = {
  605. id: "",
  606. coursePropertyName: "",
  607. firstProperty: {},
  608. secondProperty: {},
  609. };
  610. if (
  611. this.quesModel.quesProperties === undefined ||
  612. this.quesModel.quesProperties === null ||
  613. this.quesModel.quesProperties.length == 0
  614. ) {
  615. this.quesModel.quesProperties = [];
  616. }
  617. quesProperty.id =
  618. this.coursePropertyName +
  619. "-" +
  620. this.firstPropertyId +
  621. "-" +
  622. this.secondPropertyId;
  623. for (let quesPro of this.quesModel.quesProperties) {
  624. if (quesPro.id == quesProperty.id) {
  625. this.$notify({
  626. message: "该属性已存在,请重新选择",
  627. type: "error",
  628. });
  629. return false;
  630. }
  631. }
  632. quesProperty.coursePropertyName = this.coursePropertyName;
  633. //取到一级属性对象
  634. for (let property of this.firstPropertyList) {
  635. if (property.id == this.firstPropertyId) {
  636. quesProperty.firstProperty = property;
  637. }
  638. }
  639. //判断是否有二级属性
  640. if (
  641. this.secondPropertyList != undefined &&
  642. this.secondPropertyList.length > 0
  643. ) {
  644. if (!this.secondPropertyId) {
  645. this.$notify({
  646. message: "请选择二级属性",
  647. type: "error",
  648. });
  649. return false;
  650. }
  651. }
  652. //取到二级属性对象
  653. for (let property of this.secondPropertyList) {
  654. if (property.id == this.secondPropertyId) {
  655. quesProperty.secondProperty = property;
  656. }
  657. }
  658. this.quesModel.quesProperties.push(quesProperty);
  659. this.quesModel = Object.assign({}, this.quesModel);
  660. //清空下拉框
  661. this.coursePropertyName = "";
  662. this.firstPropertyId = "";
  663. this.secondPropertyId = "";
  664. this.firstPropertyList = [];
  665. this.secondPropertyList = [];
  666. },
  667. //删除属性
  668. handleClose(tag) {
  669. this.quesModel.quesProperties.splice(
  670. this.quesModel.quesProperties.indexOf(tag),
  671. 1
  672. );
  673. this.quesModel = Object.assign({}, this.quesModel);
  674. },
  675. //新增属性验证
  676. checkInsertPro() {
  677. if (!this.coursePropertyName) {
  678. this.$notify({
  679. message: "请选择属性",
  680. type: "error",
  681. });
  682. return false;
  683. }
  684. if (!this.firstPropertyId) {
  685. this.$notify({
  686. message: "请选择一级属性",
  687. type: "error",
  688. });
  689. return false;
  690. }
  691. return true;
  692. },
  693. },
  694. };
  695. </script>