summaryrefslogtreecommitdiff
path: root/yaksh/tasks.py
blob: 1c4658b0791843330cb690bbcd19543fc2ca136d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# Python Imports
from __future__ import absolute_import, unicode_literals
from textwrap import dedent

# Django and celery imports
from celery import shared_task
from django.urls import reverse
from django.shortcuts import get_object_or_404

# Local imports
from .models import Course, QuestionPaper, Quiz, AnswerPaper, CourseStatus
from notifications_plugin.models import NotificationMessage, Notification


@shared_task
def regrade_papers(data):
    question_id = data.get("question_id")
    questionpaper_id = data.get("questionpaper_id")
    answerpaper_id = data.get("answerpaper_id")
    course_id = data.get("course_id")
    user_id = data.get("user_id")
    quiz_id = data.get("quiz_id")
    quiz_name = data.get("quiz_name")
    course_name = data.get("course_name")

    url = reverse("yaksh:grade_user", args=[quiz_id, course_id])

    try:
        if answerpaper_id is not None and question_id is None:
            # Regrade specific user for all questions
            answerpaper = AnswerPaper.objects.get(id=answerpaper_id)
            url = reverse("yaksh:grade_user",
                          args=[quiz_id, answerpaper.user_id, course_id])
            for question in answerpaper.questions.all():
                answerpaper.regrade(question.id)
                course_status = CourseStatus.objects.filter(
                    user=answerpaper.user, course=answerpaper.course)
                if course_status.exists():
                    course_status.first().set_grade()

        elif answerpaper_id is not None and question_id is not None:
            # Regrade specific user for a specific question
            answerpaper = AnswerPaper.objects.get(pk=answerpaper_id)
            url = reverse("yaksh:grade_user",
                          args=[quiz_id, answerpaper.user_id, course_id])
            answerpaper.regrade(question_id)
            course_status = CourseStatus.objects.filter(
                user=answerpaper.user, course=answerpaper.course)
            if course_status.exists():
                course_status.first().set_grade()

        elif questionpaper_id is not None and question_id is not None:
            # Regrade all users for a specific question
            answerpapers = AnswerPaper.objects.filter(
                questions=question_id,
                question_paper_id=questionpaper_id, course_id=course_id)
            for answerpaper in answerpapers:
                answerpaper.regrade(question_id)
                course_status = CourseStatus.objects.filter(
                    user=answerpaper.user, course=answerpaper.course)
                if course_status.exists():
                    course_status.first().set_grade()

        message = dedent("""
            Quiz re-evaluation is complete.
            Click <a href="{0}">here</a> to view
            """.format(url)
            )
        notification_type = "success"
    except Exception as e:
        message = dedent("""
            Unable to regrade please try again.
            Click <a href="{0}">here</a> to view""".format(url)
            )
        notification_type = "warning"
    nm = NotificationMessage.objects.add_single_message(
        user_id, "{0} re-evaluation status".format(quiz_name),
        message, notification_type
    )
    notification = Notification.objects.add_single_notification(
        user_id, nm.id
    )