AddQuestion.vue 11.2 KB
Newer Older
耿迪迪's avatar
耿迪迪 committed
1 2 3 4 5 6 7
<template>
  <div ref="myBody" class="add-question flex">
    <div class="text flex">
      <div class="left">
        目前录入题目是第<span>{{ questionNextNum }}</span
        >道题
      </div>
8 9 10 11 12 13
      <div>
        <el-radio-group
          v-model="form.topicType"
          size="mini"
          @input="topicTypeChange"
        >
14 15 16 17 18 19 20 21 22
          <el-radio-button :disabled="checkLock" :label="1"
            >单选</el-radio-button
          >
          <el-radio-button :disabled="checkLock" :label="2"
            >多选</el-radio-button
          >
          <el-radio-button :disabled="checkLock" :label="3"
            >判断</el-radio-button
          >
23 24 25
        </el-radio-group>
      </div>

耿迪迪's avatar
耿迪迪 committed
26 27 28 29 30 31 32 33 34 35 36 37
      <div class="right">{{ courseName }}</div>
    </div>

    <el-form class="form flex" ref="form" :model="form" label-width="auto">
      <!-- <div class="top flex"> -->
      <div>
        <el-form-item
          label="题目内容"
          prop="topicTitle"
          :rules="{
            required: true,
            message: '必须输入题目内容',
38
            trigger: ['blur', 'change'],
耿迪迪's avatar
耿迪迪 committed
39 40 41 42 43 44 45 46
          }"
        >
          <el-input
            type="textarea"
            placeholder="多行输入"
            resize="none"
            rows="4"
            v-model="form.topicTitle"
47
            :disabled="checkLock"
耿迪迪's avatar
耿迪迪 committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61
          >
          </el-input>
        </el-form-item>
      </div>

      <div class="bottom">
        <!-- <el-form-item label="选项1" prop="title">
          <el-input v-model="form.title" placeholder="请输入"></el-input>
        </el-form-item> -->
        <el-form-item
          v-for="(question, index) in form.questions"
          :label="'选项' + (index + 1)"
          :key="question.key"
          :prop="'questions.' + index + '.value'"
62 63 64
          :rules="{
            required: true,
            message: '选项内容不能为空不能为空',
65
            trigger: ['blur', 'change'],
66
          }"
耿迪迪's avatar
耿迪迪 committed
67 68 69 70 71 72 73 74
        >
          <div class="add-select flex">
            <el-input
              type="textarea"
              placeholder="多行输入"
              style="flex: 1; margin-right: 10px"
              rows="2"
              v-model="question.value"
75
              :disabled="checkLock"
耿迪迪's avatar
耿迪迪 committed
76 77 78 79 80
            ></el-input>
            <div class="flex algin-items">
              <div
                @click="rightAnswerClick(index)"
                class="right"
81 82 83
                :class="{
                  active: answerNum.indexOf(index) >= 0,
                }"
耿迪迪's avatar
耿迪迪 committed
84 85 86 87 88 89
              >
                设为正确答案
              </div>
              <el-button
                size="mini"
                type="danger"
90
                v-if="index > 0 && form.topicType != 3"
耿迪迪's avatar
耿迪迪 committed
91
                @click.prevent="removeDomain(question)"
92
                :disabled="checkLock"
耿迪迪's avatar
耿迪迪 committed
93 94 95 96 97 98
                >删除</el-button
              >
            </div>
          </div>
        </el-form-item>

纪泽龙's avatar
纪泽龙 committed
99
        <!-- <el-form-item
耿迪迪's avatar
耿迪迪 committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
          class="noAttr"
          :label="`选项${form.questions.length + 1}`"
          prop=""
        >
          <div class="add-select flex">
            <el-input
              type="textarea"
              placeholder="多行输入"
              resize="none"
              rows="2"
              v-model="addValue"
              style="flex: 1; margin-right: 10px"
            >
            </el-input>
            <div class="flex algin-items">
              <div
                @click="rightAnswerClick(form.questions.length)"
                class="right"
                :class="{ active: answerNum === form.questions.length }"
              >
                设为正确答案
纪泽龙's avatar
纪泽龙 committed
121
              </div> -->
122
        <div style="padding-left: 30px" v-if="form.topicType != 3">
123 124
          <el-button
            size="mini"
125
            :disabled="checkLock"
126 127
            type="primary"
            @click.prevent="add(addValue)"
纪泽龙's avatar
纪泽龙 committed
128 129 130
            >新增选项</el-button
          >
        </div>
耿迪迪's avatar
耿迪迪 committed
131

纪泽龙's avatar
纪泽龙 committed
132
        <!-- </div>
耿迪迪's avatar
耿迪迪 committed
133
          </div>
纪泽龙's avatar
纪泽龙 committed
134
        </el-form-item> -->
耿迪迪's avatar
耿迪迪 committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
      </div>
    </el-form>
  </div>
</template>

<script>
import {
  addQuestion,
  checkQuestion,
  changeQuestion,
  getQuestion,
  getLessonById,
} from "@/api/educationPlanExam/lessonsProgram.js";

export default {
  name: "AnswerLesson",
  props: {
    // visible: {
    //   type: Boolean,
    //   default: false,
    // },
    courseId: {
      type: Number,
    },
    topicId: {
      type: Number,
    },
162 163 164 165
    // 如果是查看,就禁止修改
    checkLock: {
      type: Boolean,
    },
耿迪迪's avatar
耿迪迪 committed
166 167 168 169
  },
  components: {},
  data() {
    return {
170
      // topicType:1,
耿迪迪's avatar
耿迪迪 committed
171
      form: {
172
        topicType: 1,
耿迪迪's avatar
耿迪迪 committed
173
        topicTitle: "",
纪泽龙's avatar
纪泽龙 committed
174
        questions: [{ value: "" }, { value: "" }],
耿迪迪's avatar
耿迪迪 committed
175
      },
176
      answerNum: [],
耿迪迪's avatar
耿迪迪 committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190
      addValue: "",
      // 录入的是第几道题
      questionNextNum: 1,
      courseName: "",
    };
  },

  created() {
    // 如果存在就是修改
    if (this.topicId) {
      checkQuestion(this.topicId).then((res) => {
        console.log(res.data);
        const data = res.data;
        this.form = {
191
          topicType: data.topicType,
耿迪迪's avatar
耿迪迪 committed
192 193 194
          topicTitle: data.topicTitle,
          questions: JSON.parse(data.topicOption),
        };
195
        this.answerNum = JSON.parse(data.answer);
耿迪迪's avatar
耿迪迪 committed
196 197 198 199 200 201 202 203 204
      });
    }

    // 查询是第几道题
    this.getQuestion();
    // 获取课程标题
    this.getLessonById(this.courseId);
  },
  methods: {
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
    // 题目类型变化
    topicTypeChange(num) {
      if (num == 1) {
        this.answerNum = [];
      } else if (num == 2) {
        this.answerNum = [];
        // this.form.questions=[{ value: "" }, { value: "" }];
      } else {
        this.answerNum = [];
        const form = {
          topicType: 3,
          topicTitle: this.form.topicTitle,
          questions: [{ value: "对" }, { value: "错" }],
        };
        this.form = form;
      }
    },
耿迪迪's avatar
耿迪迪 committed
222 223
    getQuestion() {
      getQuestion({ courseId: this.courseId }).then((res) => {
224
        // 如果是修改 就是原来的值,如果不是,就是总数+1
纪泽龙's avatar
纪泽龙 committed
225
        console.log(res);
226 227 228
        if (this.topicId) {
          res.rows.forEach((item, index) => {
            if (item.topicId == this.topicId) {
纪泽龙's avatar
纪泽龙 committed
229
              this.questionNextNum = index + 1;
230 231 232 233 234
            }
          });
        } else {
          this.questionNextNum = res.total + 1;
        }
耿迪迪's avatar
耿迪迪 committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
      });
    },
    getLessonById(courseId) {
      getLessonById(courseId).then((res) => {
        console.log(res);
        this.courseName = res.data.courseName;
      });
    },
    addQuestion(data) {
      // 如果是修改,就用修改的方法,如果是新增,就用新增的方法
      if (this.topicId) {
        return changeQuestion({ topicId: this.topicId, ...data });
      } else {
        return addQuestion({ courseId: this.courseId, ...data });
      }
    },
    rightAnswerClick(index) {
252
      if (this.checkLock) return;
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
      if (this.form.topicType === 2) {
        const ind = this.answerNum.indexOf(index);
        if (ind < 0) {
          this.answerNum.push(index);
        } else {
          this.answerNum.splice(ind, 1);
        }
        this.answerNum = this.answerNum.sort((a, b) => {
          return a - b;
        });
        console.log(this.answerNum);
      } else {
        // 判断跟单选模式差不多
        this.answerNum = [index];
      }
耿迪迪's avatar
耿迪迪 committed
268 269 270
    },
    // 删除选项
    removeDomain(question) {
271 272
      if (this.checkLock) return;

耿迪迪's avatar
耿迪迪 committed
273
      const index = this.form.questions.indexOf(question);
274
      console.log(index);
耿迪迪's avatar
耿迪迪 committed
275
      // 如果是正确答案,就让正确答案清空
276 277 278 279 280 281 282 283 284 285 286 287 288
      const ind = this.answerNum.indexOf(index);
      if (ind >= 0) {
        this.answerNum.splice(ind, 1);
      }
      // 如果是最后一位呗删除,那不用管,如果不是最后一位置,这一位删除之后,则这一位后面的所有数字都要-1;
      if (index != this.form.questions.length - 1) {
        this.answerNum = this.answerNum.map((item, i) => {
          if (item >= index) {
            return item - 1;
          } else {
            return item;
          }
        });
耿迪迪's avatar
耿迪迪 committed
289 290 291 292
      }
      if (index >= 0) {
        this.form.questions.splice(index, 1);
      }
293 294
      console.log(this.answerNum);
      // console.log(this.form.questions)
耿迪迪's avatar
耿迪迪 committed
295 296 297 298 299 300 301
    },
    // 新增选项
    add(addValue) {
      this.form.questions.push({ value: addValue });
    },
    save(num = 2) {
      return new Promise((resove) => {
302
        if (this.answerNum.length <= 0) {
耿迪迪's avatar
耿迪迪 committed
303 304 305 306 307 308 309 310 311 312 313
          this.$message({
            message: "警告,请设置一个正确答案",
            type: "warning",
          });
          return resove(false);
        }
        this.$refs.form.validate((valid) => {
          if (valid) {
            const data = {};
            data.topicTitle = this.form.topicTitle;
            data.topicOption = JSON.stringify(this.form.questions);
314
            data.answer = JSON.stringify(this.answerNum);
315 316
            data.topicType = this.form.topicType;
            console.log(data);
耿迪迪's avatar
耿迪迪 committed
317 318 319 320
            this.addQuestion(data).then((res) => {
              if (res.code == 200) {
                // 把修改的这个归位,变成正常添加
                this.$emit("update:topicId", null);
321
                console.log("updateTopicId", this.topicId);
耿迪迪's avatar
耿迪迪 committed
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
                this.$message({
                  message: "添加题目成功",
                  type: "success",
                });
                this.$parent.$parent.componentsNumChange(num);
                this.$parent.$parent.$parent.getList();
                resove(true);
              }
            });
          }
        });
      });
    },

    saveAndNext() {
      this.save(3).then((res) => {
        if (res) {
          this.reset();
340 341
          // this.questionNextNum++;
          this.getQuestion();
耿迪迪's avatar
耿迪迪 committed
342 343 344 345
        }
      });
    },
    reset() {
346
      const topicType = this.form.topicType;
耿迪迪's avatar
耿迪迪 committed
347
      this.form = {
348
        topicType,
耿迪迪's avatar
耿迪迪 committed
349
        topicTitle: "",
350
        questions: [{ value: "" }, { value: "" }],
耿迪迪's avatar
耿迪迪 committed
351
      };
352
      this.answerNum = [];
耿迪迪's avatar
耿迪迪 committed
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
      this.addValue = "";
    },
  },
};
</script>
<style lang="scss" scoped>
.add-question {
  width: 100%;
  height: 550px;
  // overflow: hidden;
  flex-direction: column;
  padding-bottom: 7px;
  margin-bottom: 20px;
  border-bottom: 1px solid #bbbbbb;
  .form {
    flex: 1;
    flex-direction: column;
    height: 100%;
    .bottom {
      overflow-y: auto;
      height: 330px;
      box-sizing: border-box;

      .algin-items {
        align-items: center;
        width: 200px;
      }
      .right {
        display: inline-block;
        width: 133px;
        margin-right: 10px;
        line-height: initial;
        padding: 4px 0;
        border: 1px solid #bbbbbb;
        color: #101010;
        font-size: 12px;
        text-align: center;
        border-radius: 4px;
        box-sizing: border-box;
        cursor: pointer;
        &.active {
          background-color: #0bab0c;
          color: #ffffff;
        }
        &:hover {
          background-color: rgba(11, 171, 12, 0.5);
          color: #ffffff;
        }
      }
    }
  }
  .text {
    margin-top: 13px;
    margin-bottom: 34px;
    justify-content: space-between;
    height: 28px;
    .left {
      line-height: 28px;
      color: #101010;
      font-size: 14px;
    }
    .right {
      width: 411px;
      line-height: 28px;
      background: #1d84ff;
      padding-right: 5px;
      color: #fff;
      text-align: right;
    }
  }
}
</style>