Skip to content

Commit

Permalink
modified fetch for answer_validation to also take care of mcqs
Browse files Browse the repository at this point in the history
  • Loading branch information
arielfayol37 committed Aug 19, 2023
1 parent ce02e23 commit 82e653f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 24 deletions.
Binary file modified db.sqlite3
Binary file not shown.
Binary file modified deimos/__pycache__/views.cpython-311.pyc
Binary file not shown.
14 changes: 13 additions & 1 deletion deimos/static/deimos/js/answer_input.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,19 @@ document.addEventListener('DOMContentLoaded', ()=> {
console.log(result.correct);
});
} else if( questionType.value ==='mcq'){
console.log('mcq answer')
fetch(`/${validateAnswerActionURL}`, {
method: 'POST',
headers: { 'X-CSRFToken': getCookie('csrftoken') },
body: JSON.stringify({
answer: getSelectedTrueAnswerIds(),
questionType: questionType.value
})
})
.then(response => response.json())
.then(result => {
// Print result
console.log(result.correct);
});
}

});
Expand Down
46 changes: 23 additions & 23 deletions deimos/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,29 +172,29 @@ def validate_answer(request, question_id, assignment_id=None, course_id=None):
question_student, created = QuestionStudent.objects.get_or_create(student=student, question=question)

if (question_student.get_num_attempts() < question.max_num_attempts and not question_student.success):

attempt = QuestionAttempt.objects.create(question_student=question_student)
attempt.content = submitted_answer
if question.answer_type == QuestionChoices.STRUCTURAL_EXPRESSION:
assert question.expression_answers.count() == 1
answer = question.expression_answers.first()
correct = compare_expressions(answer.content, submitted_answer)
elif question.answer_type == QuestionChoices.STRUCTURAL_FLOAT:
assert question.float_answers.count() == 1
answer = question.float_answers.first()
try:
correct = compare_floats(answer.content, float(submitted_answer))
except ValueError:
# TODO: Maybe return a value to the user side that will ask them to enter a float.
correct = False
if correct:
# Deduct points based on attempts, but ensure it doesn't go negative
attempt.num_points = max(0, question.num_points - (question.deduct_per_attempt * question_student.get_num_attempts()))
question_student.success = True
attempt.success = True

question_student.save() # Save the changes to the QuestionStudent instance
attempt.save()
if data["questionType"] == 'structural':
attempt = QuestionAttempt.objects.create(question_student=question_student)
attempt.content = submitted_answer
if question.answer_type == QuestionChoices.STRUCTURAL_EXPRESSION:
assert question.expression_answers.count() == 1
answer = question.expression_answers.first()
correct = compare_expressions(answer.content, submitted_answer)
elif question.answer_type == QuestionChoices.STRUCTURAL_FLOAT:
assert question.float_answers.count() == 1
answer = question.float_answers.first()
try:
correct = compare_floats(answer.content, float(submitted_answer))
except ValueError:
# TODO: Maybe return a value to the user side that will ask them to enter a float.
correct = False
if correct:
# Deduct points based on attempts, but ensure it doesn't go negative
attempt.num_points = max(0, question.num_points - (question.deduct_per_attempt * question_student.get_num_attempts()))
question_student.success = True
attempt.success = True
question_student.save() # Save the changes to the QuestionStudent instance
attempt.save()
# Return a JsonResponse
return JsonResponse({
'correct': correct
Expand Down

0 comments on commit 82e653f

Please sign in to comment.