From e44bd4577bf839148c0824a9d3cc84a789f25b95 Mon Sep 17 00:00:00 2001 From: prathamesh Date: Mon, 11 Aug 2014 13:00:33 +0530 Subject: redirect to dashboard after exam. After exam is over or time-out the user is redirected to the result page. The user is not logged out. Current time is checked with the quiz end time, so that the user cannot reattempt the quiz. --- testapp/exam/views.py | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) (limited to 'testapp/exam') diff --git a/testapp/exam/views.py b/testapp/exam/views.py index 7c9af6c..aeb93ca 100644 --- a/testapp/exam/views.py +++ b/testapp/exam/views.py @@ -22,6 +22,7 @@ from exam.forms import UserRegisterForm, UserLoginForm, QuizForm,\ from exam.xmlrpc_clients import code_server from settings import URL_ROOT + # The directory where user data can be saved. OUTPUT_DIR = abspath(join(dirname(__file__), 'output')) @@ -142,7 +143,6 @@ def quizlist_user(request): pre_requisites = [] context = {} - if 'cannot_attempt' in request.GET: context['cannot_attempt'] = True @@ -155,7 +155,7 @@ def quizlist_user(request): for answer_paper in user_answerpapers: for quiz in avail_quizzes: if answer_paper.question_paper.id == quiz.id and \ - answer_paper.end_time != answer_paper.start_time: + answer_paper.end_time != answer_paper.start_time: avail_quizzes.remove(quiz) quizzes_taken.append(answer_paper) @@ -181,11 +181,11 @@ def intro(request, questionpaper_id): return my_render_to_response('exam/intro.html', context, context_instance=ci) else: - context = {'user': user, 'cannot_attempt':True} + context = {'user': user, 'cannot_attempt': True} return my_redirect("/exam/quizzes/?cannot_attempt=True") except: - context = {'user': user, 'cannot_attempt':True} + context = {'user': user, 'cannot_attempt': True} return my_redirect("/exam/quizzes/?cannot_attempt=True") context = {'user': user, 'paper_id': questionpaper_id} ci = RequestContext(request) @@ -389,7 +389,7 @@ def add_quiz(request, quiz_id=None): form.initial['active'] = d.active form.initial['pass_criteria'] = d.pass_criteria form.initial['language'] = d.language - form.initial['prerequisite'] = d.prerequisite_id + form.initial['prerequisite'] = d.prerequisite_id return my_render_to_response('exam/add_quiz.html', {'form': form}, context_instance=ci) @@ -607,10 +607,11 @@ rights/permissions and log in.""" 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} + context = {'user': user, 'users_per_paper': users_per_paper} return my_render_to_response('manage.html', context) return my_redirect('/exam/login/') + def user_login(request): """Take the credentials of the user and log the user in.""" @@ -691,6 +692,9 @@ def question(request, q_id, questionpaper_id, success_msg=None): if not paper.question_paper.quiz.active: reason = 'The quiz has been deactivated!' return complete(request, reason, questionpaper_id) + elif paper.end_time < datetime.datetime.now(): + reason = 'You have already attempted the quiz' + return complete(request, reason, questionpaper_id) time_left = paper.time_left() if time_left == 0: return complete(request, reason='Your time is up!') @@ -722,11 +726,11 @@ def check(request, q_id, questionpaper_id=None): """Checks the answers of the user for particular question""" user = request.user - if not user.is_authenticated(): - return my_redirect('/exam/login/') - question = get_object_or_404(Question, pk=q_id) q_paper = QuestionPaper.objects.get(id=questionpaper_id) paper = AnswerPaper.objects.get(user=request.user, question_paper=q_paper) + if not user.is_authenticated() or paper.end_time < datetime.datetime.now(): + return my_redirect('/exam/login/') + question = get_object_or_404(Question, pk=q_id) snippet_code = request.POST.get('snippet') skip = request.POST.get('skip', None) success_msg = False @@ -816,7 +820,7 @@ def validate_answer(user, user_answer, question): success, message = code_server.run_code(user_answer, question.test, user_dir, question.language) if success: - correct = True + correct = True return correct, success, message @@ -842,6 +846,7 @@ def complete(request, reason=None, questionpaper_id=None): paper.update_marks_obtained() paper.update_percent() paper.update_passed() + paper.end_time = datetime.datetime.now() paper.save() obt_marks = paper.marks_obtained tot_marks = paper.question_paper.total_marks @@ -850,12 +855,10 @@ def complete(request, reason=None, questionpaper_id=None): you answered all the questions correctly.\ You have been logged out successfully,\ Thank You !"} - logout(request) return my_render_to_response('exam/complete.html', context) else: message = reason or "You are successfully logged out" context = {'message': message} - logout(request) return my_render_to_response('exam/complete.html', context) no = False message = reason or 'The quiz has been completed. Thank you.' @@ -866,7 +869,7 @@ def complete(request, reason=None, questionpaper_id=None): if not no: # Logout the user and quit with the message given. answer_paper = AnswerPaper.objects.get(id=answerpaper_id) - answer_paper.endtime = datetime.datetime.now() + answer_paper.end_time = datetime.datetime.now() answer_paper.save() return my_redirect('/exam/quizzes/') else: @@ -1118,8 +1121,8 @@ def ajax_questionpaper(request, query): question_list = fixed_question_list + random_question_list questions = list(Question.objects.filter(type=question_type, points=marks_selected)) - questions = [ question for question in questions \ - if not str(question.id) in question_list ] + questions = [question for question in questions \ + if not str(question.id) in question_list] return my_render_to_response('exam/ajax_questions.html', {'questions': questions}) @@ -1147,7 +1150,7 @@ def design_questionpaper(request): question_paper.save() if fixed_questions: fixed_questions_ids = ",".join(fixed_questions) - fixed_questions_ids_list = fixed_questions_ids.split(',') + fixed_questions_ids_list = fixed_questions_ids.split(',') for question_id in fixed_questions_ids_list: question_paper.fixed_questions.add(question_id) if random_questions: -- cgit From 962cee1705509bb595cda51d104ef1ef7cf0b4fd Mon Sep 17 00:00:00 2001 From: prathamesh Date: Mon, 11 Aug 2014 14:44:48 +0530 Subject: fixed minor issues in css. --- testapp/exam/static/exam/css/question_quiz.css | 12 +++++++++--- testapp/exam/static/exam/js/add_quiz.js | 4 ++++ 2 files changed, 13 insertions(+), 3 deletions(-) (limited to 'testapp/exam') diff --git a/testapp/exam/static/exam/css/question_quiz.css b/testapp/exam/static/exam/css/question_quiz.css index b702bd4..ee249d4 100644 --- a/testapp/exam/static/exam/css/question_quiz.css +++ b/testapp/exam/static/exam/css/question_quiz.css @@ -5,14 +5,20 @@ table th, table td .mini-text { - height : 9px; - width : 30px; + height : 25px; + width : 70px; } .select-type { - width : 80px; + width : 225px; } .tag-text { + height : 30px; width : 290px; } +.date-text +{ + height : 30px; + width : 100px; +} diff --git a/testapp/exam/static/exam/js/add_quiz.js b/testapp/exam/static/exam/js/add_quiz.js index d5688a8..184881c 100644 --- a/testapp/exam/static/exam/js/add_quiz.js +++ b/testapp/exam/static/exam/js/add_quiz.js @@ -1,5 +1,9 @@ function test() { + + document.getElementById('id_duration').setAttribute('class','mini-text'); + document.getElementById('id_pass_criteria').setAttribute('class','mini-text'); + document.getElementById('id_start_date').setAttribute('class','date-text'); if (document.getElementById("id_description").value != "") { document.getElementById("submit").innerHTML = "Save"; -- cgit From 0808861705c08aede6c0dc9e07ac891911a7bbfb Mon Sep 17 00:00:00 2001 From: prathamesh Date: Tue, 23 Sep 2014 14:44:47 +0530 Subject: Models and views modified. Added number of attempts option for quiz. Also days lag between two attempts. added test status and attempt number for the test. Removed profile foreign key from answer paper models since user foreign key is present in the answer paper model. Urls slightly modified to include attemt number for a given quiz. --- testapp/exam/forms.py | 14 ++++ testapp/exam/models.py | 33 +++++++-- testapp/exam/templates/exam/intro.html | 2 +- testapp/exam/templates/exam/question.html | 9 +-- testapp/exam/templates/exam/quit.html | 4 +- testapp/exam/urls.py | 8 +-- testapp/exam/views.py | 107 ++++++++++++++++++------------ 7 files changed, 113 insertions(+), 64 deletions(-) (limited to 'testapp/exam') diff --git a/testapp/exam/forms.py b/testapp/exam/forms.py index 9bfedbe..9d68ce2 100644 --- a/testapp/exam/forms.py +++ b/testapp/exam/forms.py @@ -32,6 +32,12 @@ question_types = ( UNAME_CHARS = letters + "._" + digits PWD_CHARS = letters + punctuation + digits +attempts = [(i, i) for i in range(1, 6)] +attempts.append((-1, 'Infinite')) + +days_between_attempts = ((j, j) for j in range(401)) + + class UserRegisterForm(forms.Form): """A Class to create new form for User's Registration. It has the various fields and functions required to register @@ -143,6 +149,10 @@ class QuizForm(forms.Form): pass_criteria = forms.FloatField(initial=40, help_text='Will be taken as percentage') language = forms.CharField(widget=forms.Select(choices=languages)) + attempts_allowed = forms.IntegerField(widget=forms.Select(choices=attempts)) + time_between_attempts = forms.IntegerField\ + (widget=forms.Select(choices=days_between_attempts), + help_text='Will be in days') def save(self): start_date = self.cleaned_data["start_date"] @@ -152,6 +162,8 @@ class QuizForm(forms.Form): pass_criteria = self.cleaned_data["pass_criteria"] language = self.cleaned_data["language"] prerequisite = self.cleaned_data["prerequisite"] + attempts_allowed = self.cleaned_data["attempts_allowed"] + time_between_attempts = self.cleaned_data["time_between_attempts"] new_quiz = Quiz() new_quiz.start_date = start_date new_quiz.duration = duration @@ -160,6 +172,8 @@ class QuizForm(forms.Form): new_quiz.pass_criteria = pass_criteria new_quiz.language = language new_quiz.prerequisite_id = prerequisite + new_quiz.attempts_allowed = attempts_allowed + new_quiz.time_between_attempts = time_between_attempts new_quiz.save() diff --git a/testapp/exam/models.py b/testapp/exam/models.py index 196ee73..259f875 100644 --- a/testapp/exam/models.py +++ b/testapp/exam/models.py @@ -31,7 +31,15 @@ question_types = ( ("mcc", "Multiple Correct Choices"), ("code", "Code"), ) +attempts = [(i, i) for i in range(1, 6)] +attempts.append((-1, 'Infinite')) +days_between_attempts = ((j, j) for j in range(401)) + +test_status = ( + ('inprogress', 'Inprogress'), + ('completed', 'Completed'), + ) ############################################################################### class Question(models.Model): @@ -125,6 +133,12 @@ class Quiz(models.Model): # Programming language for a quiz language = models.CharField(max_length=20, choices=languages) + # Number of attempts for the quiz + attempts_allowed = models.IntegerField(default=1, choices=attempts) + + time_between_attempts = models.IntegerField("Number of Days",\ + choices=days_between_attempts) + class Meta: verbose_name_plural = "Quizzes" @@ -171,9 +185,9 @@ class QuestionPaper(models.Model): questions += question_set.get_random_questions() return questions - def make_answerpaper(self, user, ip): + def make_answerpaper(self, user, ip, attempt_no): """Creates an answer paper for the user to attempt the quiz""" - ans_paper = AnswerPaper(user=user, profile=user.profile, user_ip=ip) + ans_paper = AnswerPaper(user=user, user_ip=ip, attempt_number=attempt_no) ans_paper.start_time = datetime.datetime.now() ans_paper.end_time = ans_paper.start_time \ + datetime.timedelta(minutes=self.quiz.duration) @@ -214,10 +228,6 @@ class AnswerPaper(models.Model): # The user taking this question paper. user = models.ForeignKey(User) - # The user's profile, we store a reference to make it easier to access the - # data. - profile = models.ForeignKey(Profile) - # All questions that remain to be attempted for a particular Student # (a list of ids separated by '|') questions = models.CharField(max_length=128) @@ -225,6 +235,9 @@ class AnswerPaper(models.Model): # The Quiz to which this question paper is attached to. question_paper = models.ForeignKey(QuestionPaper) + # The attempt number for the question paper. + attempt_number = models.IntegerField() + # The time when this paper was started by the user. start_time = models.DateTimeField() @@ -252,6 +265,10 @@ class AnswerPaper(models.Model): # Result of the quiz, True if student passes the exam. passed = models.NullBooleanField() + # Status of the quiz attempt + status = models.CharField(max_length=20, choices=test_status,\ + default='inprogress') + def current_question(self): """Returns the current active question to display.""" qs = self.questions.split('|') @@ -343,6 +360,10 @@ class AnswerPaper(models.Model): else: self.passed = False + def update_status(self): + """ Sets status to completed """ + self.status = 'completed' + def get_question_answers(self): """ Return a dictionary with keys as questions and a list of the diff --git a/testapp/exam/templates/exam/intro.html b/testapp/exam/templates/exam/intro.html index ec1888a..58068ed 100644 --- a/testapp/exam/templates/exam/intro.html +++ b/testapp/exam/templates/exam/intro.html @@ -27,7 +27,7 @@
We hope you enjoy taking this exam !!!
- diff --git a/testapp/exam/templates/exam/question.html b/testapp/exam/templates/exam/question.html index a3e8629..855a29e 100644 --- a/testapp/exam/templates/exam/question.html +++ b/testapp/exam/templates/exam/question.html @@ -74,7 +74,7 @@ function setSnippetHeight() - {% endblock content %} diff --git a/testapp/exam/urls.py b/testapp/exam/urls.py index 37a031d..d553941 100644 --- a/testapp/exam/urls.py +++ b/testapp/exam/urls.py @@ -6,15 +6,15 @@ urlpatterns = patterns('exam.views', url(r'^quizzes/$','quizlist_user'), url(r'^results/$','results_user'), url(r'^start/$', 'start'), - url(r'^start/(?P