feat: Quiz attempt limits is configurable
- Added a 'no limit' check to `check_quiz_completion` in utils.py
- Added disabled flag for questions in quiz component
- Refactored `get_quiz_without_answers` to send attempt limit data to client
diff --git a/erpnext/education/utils.py b/erpnext/education/utils.py
index a88f66b..641664c 100644
--- a/erpnext/education/utils.py
+++ b/erpnext/education/utils.py
@@ -135,7 +135,7 @@
def check_quiz_completion(quiz, enrollment_name):
attempts = frappe.get_all("Quiz Activity", filters={'enrollment': enrollment_name, 'quiz': quiz.name}, fields=["name", "activity_date", "score", "status"])
- status = bool(len(attempts) == quiz.max_attempts)
+ status = False if quiz.max_attempts == 0 else bool(len(attempts) == quiz.max_attempts)
score = None
result = None
if attempts:
diff --git a/erpnext/public/js/education/lms/components/Quiz.vue b/erpnext/public/js/education/lms/components/Quiz.vue
index c0d6537..0a6199a 100644
--- a/erpnext/public/js/education/lms/components/Quiz.vue
+++ b/erpnext/public/js/education/lms/components/Quiz.vue
@@ -10,21 +10,21 @@
<hr>
<div id="quiz" :name="content">
<div id="quiz-body">
- <component v-for="question in quizData" :key="question.name" v-bind:is="question.type" :question="question" @updateResponse="updateResponse"></component>
+ <component v-for="question in quizData" :key="question.name" v-bind:is="question.type" :question="question" @updateResponse="updateResponse" :isDisabled="isDisabled"></component>
</div>
<div class="mt-3">
<div>
- <div v-if="submitted" id="post-quiz-actions" class="row">
+ <div v-if="isDisabled || submitted" id="post-quiz-actions" class="row">
<div class="col-md-8 text-left">
- <h3>Your Score: <span id="result">{{ score }}</span></h3>
+ <span v-html="message"></span>
</div>
<div class="col-md-4 text-right">
- <slot></slot>
+ <slot></slot>
</div>
</div>
<div v-else id="quiz-actions" class="text-right">
- <button class='btn btn-outline-secondary' type="reset">Reset</button>
- <button class='btn btn-primary' @click="submitQuiz" type="button">Submit</button>
+ <button class='btn btn-outline-secondary' type="reset" :disabled="isDisabled">Reset</button>
+ <button class='btn btn-primary' @click="submitQuiz" type="button" :disabled="isDisabled">Submit</button>
</div>
</div>
</div>
@@ -50,12 +50,16 @@
quizData: '',
quizResponse: {},
score: '',
- submitted: false
+ submitted: false,
+ isDisabled: false,
+ quizStatus: {},
}
},
mounted() {
this.getQuizWithoutAnswers().then(data => {
- this.quizData = data
+ this.quizData = data.quizData
+ this.quizStatus = data.status
+ this.isDisabled = data.status.is_complete
});
},
components: {
@@ -67,6 +71,7 @@
return lms.call("get_quiz_without_answers",
{
quiz_name: this.content,
+ course_name: this.$route.params.course_name
}
)
},
@@ -81,8 +86,8 @@
course: this.$route.params.course_name
}
).then(data => {
- this.score = data,
- this.submitted = true,
+ this.score = data
+ this.submitted = true
this.quizResponse = null
});
}
@@ -96,6 +101,16 @@
return 'QuizSingleChoice'
}
},
+ message: function() {
+ if(this.submitted) {
+ return '<h3>Your Score: <span id="result">'+ this.score +'</span></h3>'
+ }
+ let message = '<h4>You have exhausted all attempts for this quiz.</h4>'
+ if(this.quizStatus.result == 'Pass') {
+ message = "<h4>You have successfully completed this quiz.</h4>Score: " + this.quizStatus.score
+ }
+ return message
+ }
},
};
</script>
diff --git a/erpnext/public/js/education/lms/components/Quiz/QuizMultipleChoice.vue b/erpnext/public/js/education/lms/components/Quiz/QuizMultipleChoice.vue
index 10c35f5..338b1ac 100644
--- a/erpnext/public/js/education/lms/components/Quiz/QuizMultipleChoice.vue
+++ b/erpnext/public/js/education/lms/components/Quiz/QuizMultipleChoice.vue
@@ -3,7 +3,7 @@
<h5>{{ question.question }}</h5>
<div class="options ml-2">
<div v-for="option in question.options" :key="option.name" class="form-check pb-1">
- <input v-model="checked" class="form-check-input" type="checkbox" :name="question.name" :id="option.name" :value="option.name" @change="emitResponse(question.name, option.name)">
+ <input v-model="checked" class="form-check-input" type="checkbox" :name="question.name" :id="option.name" :value="option.name" @change="emitResponse(question.name, option.name)" :disabled="isDisabled">
<label class="form-check-label" :for="option.name">
{{ option.option }}
</label>
@@ -14,7 +14,7 @@
<script>
export default {
- props: ['question'],
+ props: ['question', 'isDisabled'],
name: 'QuizSingleChoice',
data() {
return {
diff --git a/erpnext/public/js/education/lms/components/Quiz/QuizSingleChoice.vue b/erpnext/public/js/education/lms/components/Quiz/QuizSingleChoice.vue
index 560f3f0..235cbce 100644
--- a/erpnext/public/js/education/lms/components/Quiz/QuizSingleChoice.vue
+++ b/erpnext/public/js/education/lms/components/Quiz/QuizSingleChoice.vue
@@ -3,7 +3,7 @@
<h5>{{ question.question }}</h5>
<div class="options ml-2">
<div v-for="option in question.options" :key="option.name" class="form-check pb-1">
- <input class="form-check-input" type="radio" :name="question.name" :id="option.name" :value="option.name" @change="emitResponse(question.name, option.name)">
+ <input class="form-check-input" type="radio" :name="question.name" :id="option.name" :value="option.name" @change="emitResponse(question.name, option.name)" :disabled="isDisabled">
<label class="form-check-label" :for="option.name">
{{ option.option }}
</label>
@@ -14,7 +14,7 @@
<script>
export default {
- props: ['question'],
+ props: ['question', 'isDisabled'],
name: 'QuizSingleChoice',
methods: {
emitResponse(q, o) {
diff --git a/erpnext/www/lms.py b/erpnext/www/lms.py
index 6c3c4c2..9ccafa9 100644
--- a/erpnext/www/lms.py
+++ b/erpnext/www/lms.py
@@ -93,15 +93,20 @@
return None
@frappe.whitelist()
-def get_quiz_without_answers(quiz_name):
+def get_quiz_without_answers(quiz_name, course_name):
try:
- quiz = frappe.get_doc("Quiz", quiz_name).get_questions()
- quiz_output = [{'name':question.name, 'question':question.question, 'type': question.type, 'options':[{'name': option.name, 'option':option.option} for option in question.options]} for question in quiz]
- return quiz_output
+ quiz = frappe.get_doc("Quiz", quiz_name)
+ questions = quiz.get_questions()
except:
frappe.throw("Quiz {0} does not exist".format(quiz_name))
return None
+ enrollment = utils.get_course_enrollment(course_name).name
+ quiz_status = {}
+ quiz_status['is_complete'], quiz_status['score'], quiz_status['result'] = utils.check_quiz_completion(quiz, enrollment)
+ quiz_output = [{'name':question.name, 'question':question.question, 'type': question.type, 'options':[{'name': option.name, 'option':option.option} for option in question.options]} for question in questions]
+ return { 'quizData': quiz_output, 'status': quiz_status}
+
@frappe.whitelist()
def evaluate_quiz(course, quiz_response, quiz_name):
"""LMS Function: Evaluates a simple multiple choice quiz.