quiz/utils.py
author nishanth
Wed, 21 Apr 2010 21:53:33 +0530
changeset 39 0fa055b8ea98
parent 12 81cd0140a0f2
child 40 89d29b1c42b5
permissions -rw-r--r--
implemented correct_quiz function and added it at the time of closing quiz and modified question bank
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
     1
import string
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
     2
import random
39
0fa055b8ea98 implemented correct_quiz function and added it at the time of closing quiz and modified question bank
nishanth
parents: 12
diff changeset
     3
import re
12
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
     4
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
     5
def gen_key(no_of_chars):
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
     6
    allowed_chars = string.digits+string.uppercase
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
     7
    return ''.join([random.choice(allowed_chars) for i in range(no_of_chars)])
39
0fa055b8ea98 implemented correct_quiz function and added it at the time of closing quiz and modified question bank
nishanth
parents: 12
diff changeset
     8
0fa055b8ea98 implemented correct_quiz function and added it at the time of closing quiz and modified question bank
nishanth
parents: 12
diff changeset
     9
def correct_quiz(quiz):
0fa055b8ea98 implemented correct_quiz function and added it at the time of closing quiz and modified question bank
nishanth
parents: 12
diff changeset
    10
    """ read each submitted answer and update the is_correct variable.
0fa055b8ea98 implemented correct_quiz function and added it at the time of closing quiz and modified question bank
nishanth
parents: 12
diff changeset
    11
    """
0fa055b8ea98 implemented correct_quiz function and added it at the time of closing quiz and modified question bank
nishanth
parents: 12
diff changeset
    12
0fa055b8ea98 implemented correct_quiz function and added it at the time of closing quiz and modified question bank
nishanth
parents: 12
diff changeset
    13
    for answer in quiz.que_answered.all():
0fa055b8ea98 implemented correct_quiz function and added it at the time of closing quiz and modified question bank
nishanth
parents: 12
diff changeset
    14
0fa055b8ea98 implemented correct_quiz function and added it at the time of closing quiz and modified question bank
nishanth
parents: 12
diff changeset
    15
        expected_ans = answer.question.expected_ans
0fa055b8ea98 implemented correct_quiz function and added it at the time of closing quiz and modified question bank
nishanth
parents: 12
diff changeset
    16
0fa055b8ea98 implemented correct_quiz function and added it at the time of closing quiz and modified question bank
nishanth
parents: 12
diff changeset
    17
        ans_regex_patterns = [ r"\s*".join(ans_pattern.split()) for ans_pattern in expected_ans.split("\n") ]
0fa055b8ea98 implemented correct_quiz function and added it at the time of closing quiz and modified question bank
nishanth
parents: 12
diff changeset
    18
        for pattern in ans_regex_patterns:
0fa055b8ea98 implemented correct_quiz function and added it at the time of closing quiz and modified question bank
nishanth
parents: 12
diff changeset
    19
            if re.findall(pattern, answer.submitted_ans):
0fa055b8ea98 implemented correct_quiz function and added it at the time of closing quiz and modified question bank
nishanth
parents: 12
diff changeset
    20
                answer.is_correct = True
0fa055b8ea98 implemented correct_quiz function and added it at the time of closing quiz and modified question bank
nishanth
parents: 12
diff changeset
    21
                answer.save()
0fa055b8ea98 implemented correct_quiz function and added it at the time of closing quiz and modified question bank
nishanth
parents: 12
diff changeset
    22
                break
0fa055b8ea98 implemented correct_quiz function and added it at the time of closing quiz and modified question bank
nishanth
parents: 12
diff changeset
    23
0fa055b8ea98 implemented correct_quiz function and added it at the time of closing quiz and modified question bank
nishanth
parents: 12
diff changeset
    24
"""
0fa055b8ea98 implemented correct_quiz function and added it at the time of closing quiz and modified question bank
nishanth
parents: 12
diff changeset
    25
we send quiz objects filtered by event and quiz num one by and update answer objects in all corresponding answers
0fa055b8ea98 implemented correct_quiz function and added it at the time of closing quiz and modified question bank
nishanth
parents: 12
diff changeset
    26
later while retreving, we filter on user also ( for displaying in the table)
0fa055b8ea98 implemented correct_quiz function and added it at the time of closing quiz and modified question bank
nishanth
parents: 12
diff changeset
    27
the table is by default in sorted fashion and also has the links.
0fa055b8ea98 implemented correct_quiz function and added it at the time of closing quiz and modified question bank
nishanth
parents: 12
diff changeset
    28
seperate table for each quiz.
0fa055b8ea98 implemented correct_quiz function and added it at the time of closing quiz and modified question bank
nishanth
parents: 12
diff changeset
    29
"""
0fa055b8ea98 implemented correct_quiz function and added it at the time of closing quiz and modified question bank
nishanth
parents: 12
diff changeset
    30