fix: quiz shuffle issue

This commit is contained in:
Jannat Patel
2024-07-30 16:58:09 +05:30
parent 794c0e760b
commit 7d15527831
2 changed files with 27 additions and 13 deletions

View File

@@ -3,9 +3,7 @@
<div class="bg-blue-100 py-2 px-2 mb-4 rounded-md text-sm text-blue-800"> <div class="bg-blue-100 py-2 px-2 mb-4 rounded-md text-sm text-blue-800">
<div class="leading-relaxed"> <div class="leading-relaxed">
{{ {{
__('This quiz consists of {0} questions.').format( __('This quiz consists of {0} questions.').format(questions.length)
quiz.data.questions.length
)
}} }}
</div> </div>
<div v-if="quiz.data.passing_percentage" class="leading-relaxed"> <div v-if="quiz.data.passing_percentage" class="leading-relaxed">
@@ -59,7 +57,7 @@
</div> </div>
</div> </div>
<div v-else-if="!quizSubmission.data"> <div v-else-if="!quizSubmission.data">
<div v-for="(question, qtidx) in quiz.data.questions"> <div v-for="(question, qtidx) in questions">
<div <div
v-if="qtidx == activeQuestion - 1 && questionDetails.data" v-if="qtidx == activeQuestion - 1 && questionDetails.data"
class="border rounded-md p-5" class="border rounded-md p-5"
@@ -166,7 +164,7 @@
{{ {{
__('Question {0} of {1}').format( __('Question {0} of {1}').format(
activeQuestion, activeQuestion,
quiz.data.questions.length questions.length
) )
}} }}
</div> </div>
@@ -179,7 +177,7 @@
</span> </span>
</Button> </Button>
<Button <Button
v-else-if="activeQuestion != quiz.data.questions.length" v-else-if="activeQuestion != questions.length"
@click="nextQuetion()" @click="nextQuetion()"
> >
<span> <span>
@@ -250,6 +248,7 @@ const activeQuestion = ref(0)
const currentQuestion = ref('') const currentQuestion = ref('')
const selectedOptions = reactive([0, 0, 0, 0]) const selectedOptions = reactive([0, 0, 0, 0])
const showAnswers = reactive([]) const showAnswers = reactive([])
let questions = reactive([])
const possibleAnswer = ref(null) const possibleAnswer = ref(null)
const props = defineProps({ const props = defineProps({
@@ -270,15 +269,28 @@ const quiz = createResource({
cache: ['quiz', props.quizName], cache: ['quiz', props.quizName],
auto: true, auto: true,
onSuccess(data) { onSuccess(data) {
if (data.shuffle_questions) { shuffleQuiz()
data.questions = data.questions.sort(() => Math.random() - 0.5)
}
if (data.limit_questions_to) {
data.questions = data.questions.slice(0, data.limit_questions_to)
}
}, },
}) })
const shuffleQuiz = () => {
let data = quiz.data
if (data.shuffle_questions) {
questions = shuffleArray(data.questions)
}
if (data.limit_questions_to) {
questions = questions.slice(0, data.limit_questions_to)
}
}
const shuffleArray = (array) => {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1))
;[array[i], array[j]] = [array[j], array[i]]
}
return array
}
const attempts = createResource({ const attempts = createResource({
url: 'frappe.client.get_list', url: 'frappe.client.get_list',
makeParams(values) { makeParams(values) {
@@ -473,6 +485,7 @@ const resetQuiz = () => {
selectedOptions.splice(0, selectedOptions.length, ...[0, 0, 0, 0]) selectedOptions.splice(0, selectedOptions.length, ...[0, 0, 0, 0])
showAnswers.length = 0 showAnswers.length = 0
quizSubmission.reset() quizSubmission.reset()
shuffleQuiz()
} }
const getSubmissionColumns = () => { const getSubmissionColumns = () => {

View File

@@ -56,6 +56,7 @@ class LMSCertificateRequest(Document):
"evaluator": self.evaluator, "evaluator": self.evaluator,
"date": self.date, "date": self.date,
"start_time": self.start_time, "start_time": self.start_time,
"member": ["!=", self.member],
}, },
): ):
frappe.throw(_("The slot is already booked by another participant.")) frappe.throw(_("The slot is already booked by another participant."))
@@ -72,7 +73,7 @@ class LMSCertificateRequest(Document):
) )
for req in existing_requests: for req in existing_requests:
if req.name != self.name and ( if (
req.date == getdate(self.date) req.date == getdate(self.date)
or getdate() < getdate(req.date) or getdate() < getdate(req.date)
or ( or (