diff options
Diffstat (limited to 'yaksh')
-rw-r--r-- | yaksh/admin.py | 5 | ||||
-rw-r--r-- | yaksh/models.py | 68 | ||||
-rw-r--r-- | yaksh/static/yaksh/js/show_toc.js | 2 | ||||
-rw-r--r-- | yaksh/views.py | 51 |
4 files changed, 119 insertions, 7 deletions
diff --git a/yaksh/admin.py b/yaksh/admin.py index d377158..6fb05d1 100644 --- a/yaksh/admin.py +++ b/yaksh/admin.py @@ -1,7 +1,8 @@ from yaksh.models import Question, Quiz, QuestionPaper, Profile from yaksh.models import (TestCase, StandardTestCase, StdIOBasedTestCase, Course, AnswerPaper, CourseStatus, LearningModule, - Lesson, Post, Comment, Topic, TableOfContents + Lesson, Post, Comment, Topic, TableOfContents, + VideoQuizAnswer, Answer ) from django.contrib import admin @@ -61,3 +62,5 @@ admin.site.register(Lesson, LessonAdmin) admin.site.register(LearningModule, LearningModuleAdmin) admin.site.register(Topic) admin.site.register(TableOfContents) +admin.site.register(VideoQuizAnswer) +admin.site.register(Answer) diff --git a/yaksh/models.py b/yaksh/models.py index 851e5c6..570c4c6 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -2784,8 +2784,74 @@ class VideoQuizAnswer(models.Model): student = models.ForeignKey(User, on_delete=models.CASCADE) answer = models.ForeignKey(Answer, on_delete=models.CASCADE) + def check_answer(self, user_answer): + result = {'success': False, 'error': ['Incorrect answer'], + 'weight': 0.0} + question = self.toc.content_object + if question.type == 'mcq': + expected_answer = question.get_test_case(correct=True).id + if user_answer.strip() == str(expected_answer).strip(): + result['success'] = True + result['error'] = ['Correct answer'] + + elif question.type == 'mcc': + expected_answers = [] + for opt in question.get_test_cases(correct=True): + expected_answers.append(str(opt.id)) + if set(user_answer) == set(expected_answers): + result['success'] = True + result['error'] = ['Correct answer'] + + elif question.type == 'integer': + expected_answers = [] + for tc in question.get_test_cases(): + expected_answers.append(int(tc.correct)) + if int(user_answer) in expected_answers: + result['success'] = True + result['error'] = ['Correct answer'] + + elif question.type == 'string': + tc_status = [] + for tc in question.get_test_cases(): + if tc.string_check == "lower": + if tc.correct.lower().splitlines()\ + == user_answer.lower().splitlines(): + tc_status.append(True) + else: + if tc.correct.splitlines()\ + == user_answer.splitlines(): + tc_status.append(True) + if any(tc_status): + result['success'] = True + result['error'] = ['Correct answer'] + + elif question.type == 'float': + user_answer = float(user_answer) + tc_status = [] + user_answer = float(user_answer) + for tc in question.get_test_cases(): + if abs(tc.correct - user_answer) <= tc.error_margin: + tc_status.append(True) + if any(tc_status): + result['success'] = True + result['error'] = ['Correct answer'] + + elif question.type == 'arrange': + testcase_ids = sorted( + [tc.id for tc in question.get_test_cases()] + ) + if user_answer == testcase_ids: + result['success'] = True + result['error'] = ['Correct answer'] + self.answer.error = result + ans_status = result.get("success") + self.answer.correct = ans_status + if ans_status: + self.answer.marks = self.answer.question.points + self.answer.save() + def __str__(self): - return f"Lesson answer of {self.toc} by {self.user.get_full_name()}" + return f"Lesson answer of {self.toc} by {self.student.get_full_name()}" class MicroManager(models.Model): diff --git a/yaksh/static/yaksh/js/show_toc.js b/yaksh/static/yaksh/js/show_toc.js index 121d9e3..f42346b 100644 --- a/yaksh/static/yaksh/js/show_toc.js +++ b/yaksh/static/yaksh/js/show_toc.js @@ -92,8 +92,6 @@ function ajax_call(url, method, data, csrf) { unlock_screen(); if (msg.data) { show_question(msg.data); - } else { - $("#dialog").dialog("close"); } if(msg.message) alert(msg.message); }, diff --git a/yaksh/views.py b/yaksh/views.py index 6f8aa2f..ff22a26 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -38,7 +38,7 @@ from yaksh.models import ( StdIOBasedTestCase, StringTestCase, TestCase, User, get_model_class, FIXTURES_DIR_PATH, MOD_GROUP_NAME, Lesson, LessonFile, LearningUnit, LearningModule, CourseStatus, question_types, Post, Comment, - Topic, TableOfContents, MicroManager + Topic, TableOfContents, VideoQuizAnswer, MicroManager ) from yaksh.forms import ( UserRegisterForm, UserLoginForm, QuizForm, QuestionForm, @@ -3832,6 +3832,51 @@ def submit_marker_quiz(request, course_id, toc_id): if not course.is_student(user): raise Http404("You are not allowed to view this page") toc = get_object_or_404(TableOfContents, pk=toc_id) - print(request.POST) - context = {"success": True} + current_question = toc.content_object + if current_question.type == 'mcq': + user_answer = request.POST.get('answer') + elif current_question.type == 'integer': + try: + user_answer = int(request.POST.get('answer')) + except ValueError: + msg = "Please enter an Integer Value" + elif current_question.type == 'float': + try: + user_answer = float(request.POST.get('answer')) + except ValueError: + msg = "Please enter a Float Value" + elif current_question.type == 'string': + user_answer = str(request.POST.get('answer')) + elif current_question.type == 'mcc': + user_answer = request.POST.getlist('answer') + elif current_question.type == 'arrange': + user_answer_ids = request.POST.get('answer').split(',') + user_answer = [int(ids) for ids in user_answer_ids] + + def is_valid_answer(user_answer): + success = True + if current_question.type == "mcc" and not user_answer: + success = False + elif not str(user_answer): + success = False + return success + + if is_valid_answer(user_answer): + if not VideoQuizAnswer.objects.filter( + toc_id=toc_id, student_id=user.id).exists(): + answer = Answer.objects.create( + question_id=current_question.id, answer=user_answer, + correct=False, error=json.dumps([]) + ) + lesson_ans = VideoQuizAnswer.objects.create( + toc_id=toc_id, student=user, answer=answer + ) + if toc.content == 2: + lesson_ans.check_answer(user_answer) + msg = "Answer saved successfully" + else: + msg = "You have already submitted the answer" + else: + msg = "Please submit a valid answer" + context = {"success": True, "message": msg} return JsonResponse(context) |