LMS: Quiz Eval and Max Attempts Check
Co-authored-by: Chinmay Pai <chinmaydpai@gmail.com>
This commit is contained in:
parent
3574ac3f5e
commit
7409fe6438
@ -9,11 +9,22 @@ from frappe.model.document import Document
|
||||
class Quiz(Document):
|
||||
|
||||
|
||||
def validate_quiz_attempts(self, enrollment, quiz_name):
|
||||
print(enrollment, quiz_name)
|
||||
if self.max_attempts > 0:
|
||||
try:
|
||||
if len(frappe.get_all("Quiz Activity", {'enrollment': enrollment.name, 'quiz': quiz_name})) >= self.max_attempts:
|
||||
frappe.throw('Maximum attempts reached!')
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
def get_quiz(self):
|
||||
pass
|
||||
|
||||
|
||||
def evaluate(self, response_dict):
|
||||
def evaluate(self, response_dict, enrollment, quiz_name):
|
||||
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()}
|
||||
|
@ -86,7 +86,7 @@
|
||||
"bold": 0,
|
||||
"collapsible": 0,
|
||||
"columns": 0,
|
||||
"fieldname": "result",
|
||||
"fieldname": "quiz_result",
|
||||
"fieldtype": "Select",
|
||||
"hidden": 0,
|
||||
"ignore_user_permissions": 0,
|
||||
@ -123,8 +123,8 @@
|
||||
"issingle": 0,
|
||||
"istable": 1,
|
||||
"max_attachments": 0,
|
||||
"modified": "2018-10-22 03:38:29.007162",
|
||||
"modified_by": "Administrator",
|
||||
"modified": "2018-10-22 15:43:00.224162",
|
||||
"modified_by": "jess@education.com",
|
||||
"module": "Education",
|
||||
"name": "Quiz Result",
|
||||
"name_case": "",
|
||||
|
@ -6,5 +6,5 @@ from __future__ import unicode_literals
|
||||
import frappe
|
||||
from frappe.model.document import Document
|
||||
|
||||
class quiz_result(Document):
|
||||
class QuizResult(Document):
|
||||
pass
|
||||
|
@ -43,6 +43,7 @@ def get_overlap_for(doc, doctype, fieldname, value=None):
|
||||
|
||||
return existing[0] if existing else None
|
||||
|
||||
|
||||
def validate_duplicate_student(students):
|
||||
unique_students= []
|
||||
for stud in students:
|
||||
@ -64,20 +65,19 @@ def evaluate_quiz(quiz_response, **kwargs):
|
||||
quiz_response = json.loads(quiz_response)
|
||||
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)
|
||||
add_quiz_activity(course_name, quiz_name, score, answers, quiz_response)
|
||||
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
|
||||
|
||||
|
||||
def add_quiz_activity(course, quiz, score, answers, quiz_response):
|
||||
print(course, quiz, result, score)
|
||||
enrollment = get_course_enrollment(course, frappe.session.user)
|
||||
answer_list = list(answers.values())
|
||||
def add_quiz_activity(enrollment, quiz, score, answers, quiz_response):
|
||||
print(quiz, answers, score)
|
||||
if not enrollment:
|
||||
frappe.throw("The user is not enrolled for the course {course}".format(course=course))
|
||||
activity = frappe.get_doc({
|
||||
@ -85,13 +85,15 @@ def add_quiz_activity(course, quiz, score, answers, quiz_response):
|
||||
"enrollment": enrollment.name,
|
||||
"quiz": quiz,
|
||||
"score": score,
|
||||
"date": frappe.getdate()
|
||||
"date": frappe.utils.datetime.datetime.now()
|
||||
})
|
||||
for i in len(quiz_response):
|
||||
for question in quiz_response.keys():
|
||||
activity.append("result",
|
||||
{
|
||||
"selected_option": quiz_response[i],
|
||||
"result": answer_list[i]})
|
||||
"question": question,
|
||||
"selected_option": quiz_response[question],
|
||||
"quiz_result": 'Correct' if answers[question] else 'Wrong'
|
||||
})
|
||||
activity.save()
|
||||
frappe.db.commit()
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user