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.py182
1 files changed, 84 insertions, 98 deletions
diff --git a/testapp/exam/views.py b/testapp/exam/views.py
index 7103006..4165b7f 100644
--- a/testapp/exam/views.py
+++ b/testapp/exam/views.py
@@ -186,7 +186,7 @@ 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 or attempt_number < 0:
@@ -195,7 +195,7 @@ def intro(request, questionpaper_id):
days_after_attempt = (today - previous_attempt_day).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:
@@ -618,8 +618,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}
@@ -656,7 +658,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
@@ -669,13 +671,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)
@@ -685,13 +687,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 question(request, q_id, attempt_no, questionpaper_id, success_msg=None):
+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_num, questionpaper_id, success_msg=None):
"""Check the credentials of the user and start the exam."""
user = request.user
@@ -701,7 +733,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:
@@ -714,35 +746,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:
@@ -752,11 +765,11 @@ 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/')
@@ -766,14 +779,15 @@ def show_question(request, q_id, attempt_no, questionpaper_id, success_msg=None)
user_code = request.POST.get('answer')
if quest.type == 'code':
old_skipped = paper.answers.filter(question=quest, skipped=True)
- _save_skipped_answer(old_skipped, user_code, paper)
+ _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):
+
+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.
@@ -783,17 +797,17 @@ def _save_skipped_answer(old_skipped, user_answer, paper):
skipped_answer.answer=user_answer
skipped_answer.save()
else:
- skipped_answer = Answer(question=quest, answer=user_answer,
+ 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 not user.is_authenticated() or paper.end_time < datetime.datetime.now():
return my_redirect('/exam/login/')
@@ -806,20 +820,23 @@ def check(request, q_id, attempt_no=None, questionpaper_id=None):
if skip is not None:
if question.type == 'code':
old_skipped = paper.answers.filter(question=question, skipped=True)
- _save_skipped_answer(old_skipped, user_code, paper)
+ _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:
@@ -833,7 +850,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
@@ -848,31 +865,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_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)
+ 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
@@ -881,7 +881,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,
@@ -889,10 +890,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)
@@ -928,15 +929,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
@@ -948,7 +949,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()
@@ -1262,7 +1263,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()
@@ -1277,19 +1279,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