summaryrefslogtreecommitdiff
path: root/testapp/exam/views.py
diff options
context:
space:
mode:
Diffstat (limited to 'testapp/exam/views.py')
-rw-r--r--testapp/exam/views.py273
1 files changed, 130 insertions, 143 deletions
diff --git a/testapp/exam/views.py b/testapp/exam/views.py
index f4826a6..11aca06 100644
--- a/testapp/exam/views.py
+++ b/testapp/exam/views.py
@@ -165,7 +165,6 @@ def intro(request, questionpaper_id):
quest_paper = QuestionPaper.objects.get(id=questionpaper_id)
attempt_number = quest_paper.quiz.attempts_allowed
time_lag = quest_paper.quiz.time_between_attempts
-
if quest_paper.quiz.prerequisite:
try:
pre_quest = QuestionPaper.objects.get(
@@ -186,42 +185,35 @@ def intro(request, questionpaper_id):
already_attempted = attempted_papers.count()
if already_attempted == 0:
context = {'user': user, 'paper_id': questionpaper_id,\
- 'attempt_no': already_attempted + 1}
+ 'attempt_num': already_attempted + 1}
return my_render_to_response('exam/intro.html', context,
context_instance=ci)
+
if already_attempted == attempt_number:
- previous_attempt = attempted_papers[already_attempted-1]
- previous_attempt_day = previous_attempt.start_time
- today = datetime.datetime.today()
- if previous_attempt.status == 'inprogress':
- end_time = previous_attempt.end_time
- quiz_time = previous_attempt.question_paper.quiz.duration*60
- if quiz_time > (today-previous_attempt_day).seconds:
- return show_question(request,
- previous_attempt.current_question(),
- previous_attempt.attempt_number,
- previous_attempt.question_paper.id)
- else:
- return my_redirect("/exam/quizzes")
+ inprogress, previous_attempt = _check_previous_attempt(attempted_papers,
+ already_attempted)
+ if inprogress:
+ return show_question(request,
+ previous_attempt.current_question(),
+ previous_attempt.attempt_number,
+ previous_attempt.question_paper.id)
else:
- return my_redirect("/exam/quizzes/")
+ return my_redirect("/exam/quizzes")
+
if already_attempted < attempt_number or attempt_number < 0:
- previous_attempt = attempted_papers[already_attempted-1]
- previous_attempt_day = previous_attempt.start_time
- today = datetime.datetime.today()
- if previous_attempt.status == 'inprogress':
- end_time = previous_attempt.end_time
- quiz_time = previous_attempt.question_paper.quiz.duration*60
- if quiz_time > (today-previous_attempt_day).seconds:
- return show_question(request,
- previous_attempt.current_question(),
- previous_attempt.attempt_number,
- previous_attempt.question_paper.id)
- days_after_attempt = (today - previous_attempt_day).days
+ inprogress, previous_attempt = _check_previous_attempt(attempted_papers,
+ already_attempted)
+ if inprogress:
+ return show_question(request,
+ previous_attempt.current_question(),
+ previous_attempt.attempt_number,
+ previous_attempt.question_paper.id)
+ days_after_attempt = (datetime.datetime.today() - \
+ previous_attempt.start_time).days
if days_after_attempt >= time_lag:
context = {'user': user, 'paper_id': questionpaper_id,\
- 'attempt_no': already_attempted + 1}
+ 'attempt_num': already_attempted + 1}
return my_render_to_response('exam/intro.html', context,
context_instance=ci)
else:
@@ -230,6 +222,20 @@ def intro(request, questionpaper_id):
return my_redirect("/exam/quizzes/")
+def _check_previous_attempt(attempted_papers, already_attempted):
+ previous_attempt = attempted_papers[already_attempted-1]
+ previous_attempt_day = previous_attempt.start_time
+ today = datetime.datetime.today()
+ if previous_attempt.status == 'inprogress':
+ end_time = previous_attempt.end_time
+ quiz_time = previous_attempt.question_paper.quiz.duration*60
+ if quiz_time > (today-previous_attempt_day).seconds:
+ return True, previous_attempt
+ else:
+ return False, previous_attempt
+ else:
+ return False, previous_attempt
+
def results_user(request):
"""Show list of Results of Quizzes that is taken by logged-in user."""
user = request.user
@@ -644,8 +650,10 @@ rights/permissions and log in."""
users_per_paper = []
for paper in question_papers:
answer_papers = AnswerPaper.objects.filter(question_paper=paper)
- users_passed = AnswerPaper.objects.filter(question_paper=paper, passed=True).count()
- users_failed = AnswerPaper.objects.filter(question_paper=paper, passed=False).count()
+ users_passed = AnswerPaper.objects.filter(question_paper=paper,
+ passed=True).count()
+ users_failed = AnswerPaper.objects.filter(question_paper=paper,
+ passed=False).count()
temp = paper, answer_papers, users_passed, users_failed
users_per_paper.append(temp)
context = {'user': user, 'users_per_paper': users_per_paper}
@@ -682,7 +690,7 @@ def user_login(request):
context_instance=ci)
-def start(request, attempt_no=None, questionpaper_id=None):
+def start(request, attempt_num=None, questionpaper_id=None):
"""Check the user cedentials and if any quiz is available,
start the exam."""
user = request.user
@@ -695,13 +703,13 @@ def start(request, attempt_no=None, questionpaper_id=None):
except QuestionPaper.DoesNotExist:
msg = 'Quiz not found, please contact your '\
'instructor/administrator. Please login again thereafter.'
- return complete(request, msg, attempt_no, questionpaper_id)
+ return complete(request, msg, attempt_num, questionpaper_id)
try:
old_paper = AnswerPaper.objects.get(
- question_paper=questionpaper, user=user, attempt_number=attempt_no)
+ question_paper=questionpaper, user=user, attempt_number=attempt_num)
q = old_paper.current_question()
- return show_question(request, q, attempt_no, questionpaper_id)
+ return show_question(request, q, attempt_num, questionpaper_id)
except AnswerPaper.DoesNotExist:
ip = request.META['REMOTE_ADDR']
key = gen_key(10)
@@ -711,13 +719,43 @@ def start(request, attempt_no=None, questionpaper_id=None):
msg = 'You do not have a profile and cannot take the quiz!'
raise Http404(msg)
- new_paper = questionpaper.make_answerpaper(user, ip, attempt_no)
+ new_paper = questionpaper.make_answerpaper(user, ip, attempt_num)
# Make user directory.
user_dir = get_user_dir(user)
- return start(request, attempt_no, questionpaper_id)
+ return start(request, attempt_num, questionpaper_id)
+
+
+def get_questions(paper):
+ '''
+ Takes answerpaper as an argument. Returns the total questions as
+ ordered dictionary, the questions yet to attempt and the questions
+ attempted
+ '''
+ to_attempt = []
+ submitted = []
+ all_questions = []
+ questions = {}
+ if paper.questions:
+ to_attempt = (paper.questions).split('|')
+ if paper.questions_answered:
+ submitted = (paper.questions_answered).split('|')
+ if not to_attempt:
+ submitted.sort()
+ all_questions = submitted
+ if not submitted:
+ to_attempt.sort()
+ all_questions = to_attempt
+ if to_attempt and submitted:
+ q_append = to_attempt + submitted
+ q_append.sort()
+ all_questions = q_append
+ for num, value in enumerate(all_questions, 1):
+ questions[value] = num
+ questions = collections.OrderedDict(sorted(questions.items()))
+ return questions, to_attempt, submitted
-def question(request, q_id, attempt_no, questionpaper_id, success_msg=None):
+def question(request, q_id, attempt_num, questionpaper_id, success_msg=None):
"""Check the credentials of the user and start the exam."""
user = request.user
@@ -727,7 +765,7 @@ def question(request, q_id, attempt_no, questionpaper_id, success_msg=None):
try:
q_paper = QuestionPaper.objects.get(id=questionpaper_id)
paper = AnswerPaper.objects.get(
- user=request.user, attempt_number=attempt_no, question_paper=q_paper)
+ user=request.user, attempt_number=attempt_num, question_paper=q_paper)
except AnswerPaper.DoesNotExist:
return my_redirect('/exam/start/')
if not paper.question_paper.quiz.active:
@@ -740,35 +778,16 @@ def question(request, q_id, attempt_no, questionpaper_id, success_msg=None):
if time_left == 0:
return complete(request, reason='Your time is up!')
quiz_name = paper.question_paper.quiz.description
- to_attempt = []
- submitted = []
- if paper.questions:
- to_attempt = (paper.questions).split('|')
- if paper.questions_answered:
- submitted = (paper.questions_answered).split('|')
- all_questions = []
- if not to_attempt:
- submitted.sort()
- all_questions = submitted
- if not submitted:
- to_attempt.sort()
- all_questions = to_attempt
- if to_attempt and submitted:
- q_append = to_attempt + submitted
- q_append.sort()
- all_questions = q_append
- questions = {}
- for num, value in enumerate(all_questions, 1):
- questions[value] = num
- questions = collections.OrderedDict(sorted(questions.items()))
+ questions, to_attempt, submitted = get_questions(paper)
if success_msg is None:
- context = {'question': q, 'questions' : questions, 'paper': paper,
+ context = {'question': q, 'questions': questions, 'paper': paper,
'user': user, 'quiz_name': quiz_name, 'time_left': time_left,
- 'to_attempt' : to_attempt, 'submitted': submitted}
+ 'to_attempt': to_attempt, 'submitted': submitted}
else:
- context = {'question': q, 'questions' : questions, 'paper': paper,
+ context = {'question': q, 'questions': questions, 'paper': paper,
'user': user, 'quiz_name': quiz_name, 'time_left': time_left,
- 'success_msg': success_msg, 'to_attempt' : to_attempt, 'submitted' : submitted}
+ 'success_msg': success_msg, 'to_attempt': to_attempt,
+ 'submitted': submitted}
if q.type == 'code':
skipped_answer = paper.answers.filter(question=q, skipped=True)
if skipped_answer:
@@ -778,48 +797,53 @@ def question(request, q_id, attempt_no, questionpaper_id, success_msg=None):
context_instance=ci)
-def show_question(request, q_id, attempt_no, questionpaper_id, success_msg=None):
+def show_question(request, q_id, attempt_num, questionpaper_id, success_msg=None):
"""Show a question if possible."""
user = request.user
q_paper = QuestionPaper.objects.get(id=questionpaper_id)
- paper = AnswerPaper.objects.get(user=request.user, attempt_number=attempt_no,
+ paper = AnswerPaper.objects.get(user=request.user, attempt_number=attempt_num,
question_paper=q_paper)
if not user.is_authenticated() or paper.end_time < datetime.datetime.now():
return my_redirect('/exam/login/')
old_qid = request.POST.get('question_id')
if old_qid is not None:
-
quest = Question.objects.get(pk=old_qid)
user_code = request.POST.get('answer')
if quest.type == 'code':
- user_answer = user_code # not taking snippet here.
old_skipped = paper.answers.filter(question=quest, skipped=True)
- if old_skipped:
- skipped_answer = old_skipped[0]
- skipped_answer.answer=user_answer
- skipped_answer.save()
- else:
- skipped_answer = Answer(question=quest, answer=user_answer,
- correct=False, skipped=True)
- skipped_answer.save()
- paper.answers.add(skipped_answer)
+ _save_skipped_answer(old_skipped, user_code, paper, quest)
if len(q_id) == 0:
msg = 'Congratulations! You have successfully completed the quiz.'
- return complete(request, msg, attempt_no, questionpaper_id)
+ return complete(request, msg, attempt_num, questionpaper_id)
else:
- return question(request, q_id, attempt_no, questionpaper_id, success_msg)
+ return question(request, q_id, attempt_num, questionpaper_id, success_msg)
+
+def _save_skipped_answer(old_skipped, user_answer, paper, question):
+ """
+ Saves the answer on skip. Only the code questions are saved.
+ Snippet is not saved with the answer.
+ """
+ if old_skipped:
+ skipped_answer = old_skipped[0]
+ skipped_answer.answer=user_answer
+ skipped_answer.save()
+ else:
+ skipped_answer = Answer(question=question, answer=user_answer,
+ correct=False, skipped=True)
+ skipped_answer.save()
+ paper.answers.add(skipped_answer)
-def check(request, q_id, attempt_no=None, questionpaper_id=None):
+def check(request, q_id, attempt_num=None, questionpaper_id=None):
"""Checks the answers of the user for particular question"""
user = request.user
q_paper = QuestionPaper.objects.get(id=questionpaper_id)
- paper = AnswerPaper.objects.get(user=request.user, attempt_number=attempt_no,
+ paper = AnswerPaper.objects.get(user=request.user, attempt_number=attempt_num,
question_paper=q_paper)
if q_id in paper.questions_answered:
next_q = paper.skip()
- return show_question(request, next_q, attempt_no, questionpaper_id)
+ return show_question(request, next_q, attempt_num, questionpaper_id)
if not user.is_authenticated() or paper.end_time < datetime.datetime.now():
return my_redirect('/exam/login/')
@@ -831,30 +855,24 @@ def check(request, q_id, attempt_no=None, questionpaper_id=None):
success = True
if skip is not None:
if question.type == 'code':
- user_answer = user_code # not taking snippet here.
old_skipped = paper.answers.filter(question=question, skipped=True)
- if old_skipped:
- skipped_answer = old_skipped[0]
- skipped_answer.answer=user_answer
- skipped_answer.save()
- else:
- skipped_answer = Answer(question=question, answer=user_answer,
- correct=False, skipped=True)
- skipped_answer.save()
- paper.answers.add(skipped_answer)
+ _save_skipped_answer(old_skipped, user_code, paper, question)
next_q = paper.skip()
- return show_question(request, next_q, attempt_no, questionpaper_id)
+ return show_question(request, next_q, attempt_num, questionpaper_id)
# Add the answer submitted, regardless of it being correct or not.
if question.type == 'mcq':
user_answer = request.POST.get('answer')
elif question.type == 'mcc':
user_answer = request.POST.getlist('answer')
- elif question.type == 'basgn':
+ elif question.type == 'upload':
assign = AssignmentUpload()
assign.user = user.profile
assign.assignmentQuestion = question
- assign.assignmentFile = request.FILES['assignment']
+ # if time-up at upload question then the form is submitted without
+ # validation
+ if 'assignment' in request.FILES:
+ assign.assignmentFile = request.FILES['assignment']
assign.save()
user_answer = 'ASSIGNMENT UPLOADED'
else:
@@ -868,7 +886,7 @@ def check(request, q_id, attempt_no=None, questionpaper_id=None):
# If we were not skipped, we were asked to check. For any non-mcq
# questions, we obtain the results via XML-RPC with the code executed
# safely in a separate process (the code_server.py) running as nobody.
- if not question.type == 'basgn':
+ if not question.type == 'upload':
correct, success, err_msg = validate_answer(user, user_answer, question)
if correct:
new_answer.correct = correct
@@ -883,31 +901,14 @@ def check(request, q_id, attempt_no=None, questionpaper_id=None):
if not success: # Should only happen for non-mcq questions.
if time_left == 0:
reason = 'Your time is up!'
- return complete(request, reason, attempt_no, questionpaper_id)
+ return complete(request, reason, attempt_num, questionpaper_id)
if not paper.question_paper.quiz.active:
reason = 'The quiz has been deactivated!'
- return complete(request, reason, attempt_no, questionpaper_id)
- to_attempt = []
- submitted = []
- if paper.questions:
- to_attempt = (paper.questions).split('|')
- if paper.questions_answered:
- submitted = (paper.questions_answered).split('|')
- all_questions = []
- if not to_attempt:
- submitted.sort()
- all_questions = submitted
- if not submitted:
- to_attempt.sort()
- all_questions = to_attempt
- if to_attempt and submitted:
- q_append = to_attempt + submitted
- q_append.sort()
- all_questions = q_append
- questions = {}
- for num, value in enumerate(all_questions, 1):
- questions[value] = num
- questions = collections.OrderedDict(sorted(questions.items()))
+ return complete(request, reason, attempt_num, questionpaper_id)
+ if not paper.question_paper.quiz.active:
+ reason = 'The quiz has been deactivated!'
+ return complete(request, reason, attempt_num, questionpaper_id)
+ questions, to_attempt, submitted = get_questions(paper)
old_answer = paper.answers.filter(question=question, skipped=True)
if old_answer:
old_answer[0].answer = user_code
@@ -916,7 +917,8 @@ def check(request, q_id, attempt_no=None, questionpaper_id=None):
'error_message': err_msg,
'paper': paper, 'last_attempt': user_code,
'quiz_name': paper.question_paper.quiz.description,
- 'time_left': time_left, 'to_attempt' : to_attempt, 'submitted': submitted}
+ 'time_left': time_left, 'to_attempt': to_attempt,
+ 'submitted': submitted}
ci = RequestContext(request)
return my_render_to_response('exam/question.html', context,
@@ -924,10 +926,10 @@ def check(request, q_id, attempt_no=None, questionpaper_id=None):
else:
if time_left <= 0:
reason = 'Your time is up!'
- return complete(request, reason, attempt_no, questionpaper_id)
+ return complete(request, reason, attempt_num, questionpaper_id)
else:
next_q = paper.completed_question(question.id)
- return show_question(request, next_q, attempt_no,
+ return show_question(request, next_q, attempt_num,
questionpaper_id, success_msg)
@@ -963,15 +965,15 @@ def validate_answer(user, user_answer, question):
return correct, success, message
-def quit(request, attempt_no=None, questionpaper_id=None):
+def quit(request, attempt_num=None, questionpaper_id=None):
"""Show the quit page when the user logs out."""
context = {'id': questionpaper_id,
- 'attempt_no': attempt_no}
+ 'attempt_num': attempt_num}
return my_render_to_response('exam/quit.html', context,
context_instance=RequestContext(request))
-def complete(request, reason=None, attempt_no=None, questionpaper_id=None):
+def complete(request, reason=None, attempt_num=None, questionpaper_id=None):
"""Show a page to inform user that the quiz has been compeleted."""
user = request.user
@@ -983,7 +985,7 @@ def complete(request, reason=None, attempt_no=None, questionpaper_id=None):
else:
q_paper = QuestionPaper.objects.get(id=questionpaper_id)
paper = AnswerPaper.objects.get(user=user, question_paper=q_paper,
- attempt_number=attempt_no)
+ attempt_number=attempt_num)
paper.update_marks_obtained()
paper.update_percent()
paper.update_passed()
@@ -1297,7 +1299,8 @@ def design_questionpaper(request):
question_paper.fixed_questions.add(question_id)
if random_questions:
for random_question, num in zip(random_questions, random_number):
- question = Question.objects.get(id=random_question[0])
+ qid = random_question.split(',')[0]
+ question = Question.objects.get(id=int(qid))
marks = question.points
question_set = QuestionSet(marks=marks, num_questions=num)
question_set.save()
@@ -1312,19 +1315,3 @@ def design_questionpaper(request):
context = {'form': form}
return my_render_to_response('exam/design_questionpaper.html',
context, context_instance=ci)
-
-
-def submit_assignment(request, question_id=None):
- user = request.user
- skip = request.POST.get('skip', None)
- if request.method == "POST" and skip is not None:
- question = Question.objects.get(id=question_id)
- assignment = AssignmentUpload()
- assignment.user = user
- assignment.assignmentQuestion = question
- assignment.assignmentFile = request.FILES['assignment']
- assignment.save()
- #next question ke liye code idhar
- else:
- #code for skipping the question
- pass