diff options
author | prathamesh | 2015-03-31 16:14:19 +0530 |
---|---|---|
committer | prathamesh | 2015-03-31 16:14:19 +0530 |
commit | b0551e3cb8dc59fb83ea622375c9e53003fcb489 (patch) | |
tree | d252ce943ae2286013edb78a75dff9d7356bc98e | |
parent | 671df0da02345710ebd4b18d45f2a573de350cc3 (diff) | |
download | online_test-b0551e3cb8dc59fb83ea622375c9e53003fcb489.tar.gz online_test-b0551e3cb8dc59fb83ea622375c9e53003fcb489.tar.bz2 online_test-b0551e3cb8dc59fb83ea622375c9e53003fcb489.zip |
Function for creating an ordered dictionary of questions
-rw-r--r-- | testapp/exam/views.py | 72 |
1 files changed, 30 insertions, 42 deletions
diff --git a/testapp/exam/views.py b/testapp/exam/views.py index 8d7dd18..2c97334 100644 --- a/testapp/exam/views.py +++ b/testapp/exam/views.py @@ -690,6 +690,34 @@ def start(request, attempt_no=None, questionpaper_id=None): user_dir = get_user_dir(user) return start(request, attempt_no, 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): """Check the credentials of the user and start the exam.""" @@ -714,27 +742,7 @@ 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, 'user': user, 'quiz_name': quiz_name, 'time_left': time_left, @@ -818,27 +826,7 @@ def check(request, q_id, attempt_no=None, questionpaper_id=None): 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())) + questions, to_attempt, submitted = get_questions(paper) context = {'question': question, 'questions': questions, 'error_message': err_msg, 'paper': paper, 'last_attempt': user_code, |