# All files / src/questions TextQuestion.vue

0% Statements 0/17
0% Branches 0/8
0% Functions 0/2
0% Lines 0/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147

     <template>
      <transition name="question">
        <div v-if="active" :class="['card', 'question', addClass]">
          <div class="card-header alert-light border-bottom border-light text-dark">
            <slot></slot>
          </div>
          <div class="card-body">
            <box v-if="showHint" type="tip">
              <slot name="hint"></slot>
            </box>

            <textarea
              v-if="hasInput"
              v-model="textareaText"
              class="form-control"
              :disabled="answered.value"
            ></textarea>

            <div v-if="state !== 0">
              <hr />
              <div class="mb-2">
                <slot name="answer"></slot>
              </div>
              <strong>Keywords:&nbsp;</strong>
              <span
                v-for="keyword in keywords.split(',')"
                :key="keyword"
                class="badge badge-pill badge-light font-weight-normal"
              >
                {{ keyword }}
              </span>
            </div>
          </div>

          <div class="card-footer alert-light border-top border-light text-dark">
            <i v-if="state === 1" class="fa fa-times text-danger border-danger result-icon"></i>
            <i v-else-if="state === 2" class="fa fa-check text-success border-success result-icon"></i>

            <div class="float-right">
              <transition name="q-btn">
                <button
                  v-if="$slots.hint && !showHint && state === 0"
                  type="button"
                  class="btn btn-success q-btn"
                  @click="showHint = true"
                >
                  Hint
                </button>
              </transition>
              <transition name="q-btn" mode="out-in">
                <button
                  v-if="state === 0"
                  key="check"
                  type="button"
                  class="btn btn-primary q-btn"
                  @click="checkAnswer"
                >
                  Check
                </button>
                <button
                  v-else-if="questions"
                  key="active"
                  type="button"
                  class="btn btn-primary q-btn"
                  @click="gotoNextQuestion"
                >
                  Next
                </button>
              </transition>
            </div>
          </div>
        </div>
      </transition>
    </template>

    <script>
    import {
      STATE_WRONG,
      STATE_CORRECT,
    } from './QuestionConstants';
    import QuizQuestionMixin from './QuizQuestionMixin';
    import QuestionMixin from './QuestionMixin';

    export default {
      name: 'McqQuestion',
      mixins: [QuizQuestionMixin, QuestionMixin],
      props: {
        // Todo deprecate this
        hasInput: {
          type: Boolean,
          default: false,
        },
        keywords: {
          type: String,
          default: '',
        },
        // The proportion of keywords needed to get the answer correct. Ranges from 0.0 to 1.0.
        threshold: {
          type: Number,
          default: 0.5,
        },
      },
      data() {
        return {
          textareaText: '',
        };
      },
      methods: {
        checkAnswer() {
          // Todo deprecate this guard clause
          const isCheckableTextQuestion = this.hasInput && this.keywords.length;
          if (!isCheckableTextQuestion) {
            this.state = STATE_CORRECT;
            this.answered.value = true;
            return;
          }

          const lowerCasedText = this.textareaText.toLowerCase();
          const keywords = this.keywords.split(',');

          let numMatching = 0;
          for (let i = 0; i < keywords.length; i += 1) {
            if (lowerCasedText.includes(keywords[i].toLowerCase())) {
              numMatching += 1;
            }
          }

          if (numMatching / keywords.length >= this.threshold) {
            this.state = STATE_CORRECT;
          } else {
            this.state = STATE_WRONG;
          }

          this.answered.value = true;
        },
      },
    };
    </script>

    <style scoped>
    .form-control {
        height: auto;
        min-height: 75px;
        margin-bottom: 10px;
    }
    </style>