Added methods to saving Quiz attempts
diff --git a/erpnext/education/doctype/quiz/quiz.py b/erpnext/education/doctype/quiz/quiz.py
index a1cc36d..232ee0e 100644
--- a/erpnext/education/doctype/quiz/quiz.py
+++ b/erpnext/education/doctype/quiz/quiz.py
@@ -27,8 +27,19 @@
# self.validate_quiz_attempts(enrollment, quiz_name)
self.get_questions()
answers = {q.name:q.get_answer() for q in self.get_questions()}
- correct_answers = {question: (answers[question] == response_dict[question]) for question in response_dict.keys()}
- return correct_answers, (sum(correct_answers.values()) * 100 ) / len(answers)
+ correct_answers = {}
+ for key in answers:
+ try:
+ result = (response_dict[key] == answers[key])
+ except:
+ result = False
+ correct_answers[key] = result
+ score = (sum(correct_answers.values()) * 100 ) / len(answers)
+ if score >= self.passing_score:
+ status = "Pass"
+ else:
+ status = "Fail"
+ return correct_answers, score, status
def get_questions(self):
diff --git a/erpnext/public/js/education/web-academy/components/ContentQuiz.vue b/erpnext/public/js/education/web-academy/components/ContentQuiz.vue
index e2ea31e..7044d76 100644
--- a/erpnext/public/js/education/web-academy/components/ContentQuiz.vue
+++ b/erpnext/public/js/education/web-academy/components/ContentQuiz.vue
@@ -72,6 +72,7 @@
frappe.call({
method: "erpnext.www.academy.evaluate_quiz",
args: {
+ enrollment: this.$root.$data.enrolledCourses[this.$route.params.course],
quiz_response: this.quizResponse,
quiz_name: this.content
}
diff --git a/erpnext/www/academy.py b/erpnext/www/academy.py
index 339830d..69faeba 100644
--- a/erpnext/www/academy.py
+++ b/erpnext/www/academy.py
@@ -81,7 +81,7 @@
return None
@frappe.whitelist()
-def evaluate_quiz(quiz_response, quiz_name):
+def evaluate_quiz(enrollment, quiz_response, quiz_name):
"""LMS Function: Evaluates a simple multiple choice quiz.
@@ -89,20 +89,27 @@
"""
import json
quiz_response = json.loads(quiz_response)
+ print(quiz_response)
quiz = frappe.get_doc("Quiz", quiz_name)
- answers, score = quiz.evaluate(quiz_response, quiz_name)
+ answers, score, status = quiz.evaluate(quiz_response, quiz_name)
+ print("-----------------")
+ print(answers)
+
+ result = {k: ('Correct' if v else 'Wrong') for k,v in answers.items()}
+ result_data = []
+ for key in answers:
+ item = {}
+ item['question'] = key
+ item['quiz_result'] = result[key]
+ try:
+ item['selected_option'] = frappe.get_value('Options', quiz_response[key], 'option')
+ except:
+ item['selected_option'] = "Unattempted"
+ result_data.append(item)
+ # result_data = [{'question': key, 'selected_option': frappe.get_value('Options', quiz_response[key], 'option'), 'quiz_result': result[key]} for key in answers]
+
+ add_quiz_activity(enrollment, quiz_name, result_data, score, status)
return(score)
- # quiz_name = kwargs.get('quiz')
- # course_name = kwargs.get('course')
- # enrollment = get_course_enrollment(course_name, frappe.session.user)
- # try:
- # quiz = frappe.get_doc("Quiz", quiz_name)
- # answers, score = quiz.evaluate(quiz_response, enrollment, quiz_name)
- # add_quiz_activity(enrollment, quiz_name, score, answers, quiz_response)
- # return score
- # except frappe.DoesNotExistError:
- # frappe.throw("Quiz {0} does not exist".format(quiz_name))
- # return None
@frappe.whitelist()
def get_completed_courses(email=frappe.session.user):
@@ -189,11 +196,17 @@
def check_activity_exists(enrollment, content_type, content):
activity = frappe.get_all("Course Activity", filters={'enrollment': enrollment, 'content_type': content_type, 'content': content})
- if activity:
- return True
- else:
- return False
+ return bool(activity)
-@frappe.whitelist()
-def add_quiz_activity(enrollment, quiz, score, answers):
- pass
\ No newline at end of file
+def add_quiz_activity(enrollment, quiz_name, result_data, score, status):
+ quiz_activity = frappe.get_doc({
+ "doctype": "Quiz Activity",
+ "enrollment": enrollment,
+ "quiz": quiz_name,
+ "result": result_data,
+ "score": score,
+ "status": status
+ })
+ quiz_activity.save()
+ print(quiz_activity)
+ frappe.db.commit()
\ No newline at end of file