diff options
author | ankitjavalkar | 2015-07-09 16:19:24 +0530 |
---|---|---|
committer | ankitjavalkar | 2015-09-07 17:58:59 +0530 |
commit | 81e119899ebc04b53ac9c15c66286c8dbc08726b (patch) | |
tree | 1eb648f180cfda74842b5a5addc378e9bf97d738 /testapp | |
parent | 31f5e743031d105b0406e9587dc33bb065cd6e4d (diff) | |
download | online_test-81e119899ebc04b53ac9c15c66286c8dbc08726b.tar.gz online_test-81e119899ebc04b53ac9c15c66286c8dbc08726b.tar.bz2 online_test-81e119899ebc04b53ac9c15c66286c8dbc08726b.zip |
Add Date-Time limits for exams
Diffstat (limited to 'testapp')
-rw-r--r-- | testapp/yaksh_app/forms.py | 13 | ||||
-rw-r--r-- | testapp/yaksh_app/models.py | 11 | ||||
-rw-r--r-- | testapp/yaksh_app/templates/yaksh_app/intro.html | 27 | ||||
-rw-r--r-- | testapp/yaksh_app/views.py | 198 |
4 files changed, 183 insertions, 66 deletions
diff --git a/testapp/yaksh_app/forms.py b/testapp/yaksh_app/forms.py index c0b5cdc..8a909f2 100644 --- a/testapp/yaksh_app/forms.py +++ b/testapp/yaksh_app/forms.py @@ -139,7 +139,10 @@ class QuizForm(forms.Form): self.fields['prerequisite'] = forms.CharField(required=False, widget=forms.Select(choices=quizzes)) - start_date = forms.DateField(initial=datetime.date.today) + start_date = forms.DateField(initial=datetime.date.today(), required=False) + start_time = forms.TimeField(initial=datetime.datetime.now().time(), required=False) + end_date = forms.DateField(initial=datetime.date(2199, 1, 1), required=False) + end_time = forms.TimeField(initial=datetime.time(0, 0, 0, 0), required=False) duration = forms.IntegerField(help_text='Will be taken in minutes') active = forms.BooleanField(required=False) description = forms.CharField(max_length=256, widget=forms.Textarea\ @@ -154,6 +157,9 @@ class QuizForm(forms.Form): def save(self): start_date = self.cleaned_data["start_date"] + start_time = self.cleaned_data["start_time"] + end_date = self.cleaned_data["end_date"] + end_time = self.cleaned_data["end_time"] duration = self.cleaned_data["duration"] active = self.cleaned_data['active'] description = self.cleaned_data["description"] @@ -163,7 +169,10 @@ class QuizForm(forms.Form): 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.start_date_time = datetime.datetime.combine(start_date, + start_time) + new_quiz.end_date_time = datetime.datetime.combine(end_date, + end_time) new_quiz.duration = duration new_quiz.active = active new_quiz.description = description diff --git a/testapp/yaksh_app/models.py b/testapp/yaksh_app/models.py index 9e37ef5..62ac9bc 100644 --- a/testapp/yaksh_app/models.py +++ b/testapp/yaksh_app/models.py @@ -160,7 +160,14 @@ class Quiz(models.Model): """ # The start date of the quiz. - start_date = models.DateField("Date of the quiz") + start_date_time = models.DateTimeField("Start Date and Time of the quiz", + default=datetime.datetime.now(), + null=True) + + # The end date and time of the quiz + end_date_time = models.DateTimeField("End Date and Time of the quiz", + default=datetime.datetime(2199, 1, 1, 0, 0, 0, 0), + null=True) # This is always in minutes. duration = models.IntegerField("Duration of quiz in minutes", default=20) @@ -192,7 +199,7 @@ class Quiz(models.Model): def __unicode__(self): desc = self.description or 'Quiz' - return '%s: on %s for %d minutes' % (desc, self.start_date, + return '%s: on %s for %d minutes' % (desc, self.start_date_time, self.duration) diff --git a/testapp/yaksh_app/templates/yaksh_app/intro.html b/testapp/yaksh_app/templates/yaksh_app/intro.html index 592937a..9c5c14d 100644 --- a/testapp/yaksh_app/templates/yaksh_app/intro.html +++ b/testapp/yaksh_app/templates/yaksh_app/intro.html @@ -5,8 +5,21 @@ {% block formtitle %}Important instructions & rules {% endblock %} {% block content %} - - + {% if enable_quiz_time or disable_quiz_time %} + {% if quiz_expired %} + <div class="alert alert-error"> + This Quiz has expired. You can no longer attempt this Quiz. + <br/> + </div> + {% else %} + <div class="alert"> + You can attempt this Quiz at any time between {{ enable_quiz_time }} and {{ disable_quiz_time }} + <br/> + You are not allowed to attempt the Quiz before or after this duration + <br/> + </div> + {% endif %} + {% endif %} <p> Welcome <strong>{{user.first_name.title}} {{user.last_name.title}}</strong>, to the programming quiz! </p> <p> This examination system has been developed with the intention of making you @@ -27,8 +40,14 @@ </ul> <p> We hope you enjoy taking this exam !!!</p> - <form action="{{URL_ROOT}}/exam/start/{{ attempt_num }}/{{ paper_id }}/" method="post" align="center"> + <form action="{{URL_ROOT}}/exam/quizzes/" method="post" align="center"> {% csrf_token %} - <center><button class="btn" type="submit" name="start">Start Exam!</button></center> + <center><button class="btn" name="home">Home</button></center> </form> + {% if not quiz_expired %} + <form action="{{URL_ROOT}}/exam/start/{{ attempt_num }}/{{ paper_id }}/" method="post" align="center"> + {% csrf_token %} + <center><button class="btn" type="submit" name="start">Start Exam!</button></center> + </form> + {% endif %} {% endblock content %} diff --git a/testapp/yaksh_app/views.py b/testapp/yaksh_app/views.py index f6666e6..e72919f 100644 --- a/testapp/yaksh_app/views.py +++ b/testapp/yaksh_app/views.py @@ -141,23 +141,20 @@ def quizlist_user(request): user = request.user avail_quizzes = list(QuestionPaper.objects.filter(quiz__active=True)) user_answerpapers = AnswerPaper.objects.filter(user=user) - quizzes_taken = user_answerpapers pre_requisites = [] - context = {} + enabled_quizzes = [] + disabled_quizzes = [] - if 'cannot_attempt' in request.GET: - context['cannot_attempt'] = True + cannot_attempt = True if 'cannot_attempt' in request.GET else False + quizzes_taken = None if user_answerpapers.count() == 0 else user_answerpapers - if user_answerpapers.count() == 0: - context['quizzes'] = avail_quizzes - context['user'] = user - context['quizzes_taken'] = None - return my_render_to_response("yaksh_app/quizzes_user.html", context) + context = {'cannot_attempt': cannot_attempt, + 'quizzes': avail_quizzes, + 'user': user, + 'quizzes_taken': quizzes_taken + } - context['quizzes'] = avail_quizzes - context['user'] = user - context['quizzes_taken'] = quizzes_taken - return my_render_to_response("yaksh_app/quizzes_user.html", context) + return my_render_to_response("exam/quizzes_user.html", context) def intro(request, questionpaper_id): @@ -167,6 +164,12 @@ 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 + quiz_enable_time = quest_paper.quiz.start_date_time + quiz_disable_time = quest_paper.quiz.end_date_time + + quiz_expired = False if quiz_enable_time <= datetime.datetime.now() \ + <= quiz_disable_time else True + if quest_paper.quiz.prerequisite: try: pre_quest = QuestionPaper.objects.get( @@ -185,27 +188,11 @@ def intro(request, questionpaper_id): attempted_papers = AnswerPaper.objects.filter(question_paper=quest_paper, user=user) already_attempted = attempted_papers.count() - if already_attempted == 0: - context = {'user': user, 'paper_id': questionpaper_id,\ - 'attempt_num': already_attempted + 1} - return my_render_to_response('yaksh_app/intro.html', context, - context_instance=ci) - - if already_attempted == attempt_number: - 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") + inprogress, previous_attempt, next_attempt = _check_previous_attempt(attempted_papers, + already_attempted, + attempt_number) - - if already_attempted < attempt_number or attempt_number < 0: - inprogress, previous_attempt = _check_previous_attempt(attempted_papers, - already_attempted) + if previous_attempt: if inprogress: return show_question(request, previous_attempt.current_question(), @@ -213,30 +200,51 @@ def intro(request, questionpaper_id): 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_num': already_attempted + 1} - return my_render_to_response('yaksh_app/intro.html', context, - context_instance=ci) + + if next_attempt: + if days_after_attempt >= time_lag: + context = {'user': user, + 'paper_id': questionpaper_id, + 'attempt_num': already_attempted + 1, + 'enable_quiz_time': quiz_enable_time, + 'disable_quiz_time': quiz_disable_time, + 'quiz_expired': quiz_expired + } + return my_render_to_response('exam/intro.html', context, + context_instance=ci) else: return my_redirect("/exam/quizzes/") + else: - 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 + context = {'user': user, + 'paper_id': questionpaper_id, + 'attempt_num': already_attempted + 1, + 'enable_quiz_time': quiz_enable_time, + 'disable_quiz_time': quiz_disable_time, + 'quiz_expired': quiz_expired + } + return my_render_to_response('exam/intro.html', context, + context_instance=ci) + + +def _check_previous_attempt(attempted_papers, already_attempted, attempt_number): + next_attempt = False if already_attempted == attempt_number else True + if already_attempted == 0: + return False, None, next_attempt else: - return False, previous_attempt + 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, next_attempt + else: + return False, previous_attempt, next_attempt + else: + return False, previous_attempt, next_attempt + def results_user(request): """Show list of Results of Quizzes that is taken by logged-in user.""" @@ -262,6 +270,9 @@ def edit_quiz(request): raise Http404('You are not allowed to view this page!') quiz_list = request.POST.getlist('quizzes') start_date = request.POST.getlist('start_date') + start_time = request.POST.getlist('start_time') + end_date = request.POST.getlist('end_date') + end_time = request.POST.getlist('end_time') duration = request.POST.getlist('duration') active = request.POST.getlist('active') description = request.POST.getlist('description') @@ -271,7 +282,10 @@ def edit_quiz(request): for j, quiz_id in enumerate(quiz_list): quiz = Quiz.objects.get(id=quiz_id) - quiz.start_date = start_date[j] + quiz.start_date_time = datetime.datetime.combine(start_date[j], + start_time[j]) + quiz.end_date_time = datetime.datetime.combine(end_date[j], + end_time[j]) quiz.duration = duration[j] quiz.active = active[j] quiz.description = description[j] @@ -471,7 +485,11 @@ def add_quiz(request, quiz_id=None): return my_redirect("/exam/manage/designquestionpaper") else: d = Quiz.objects.get(id=quiz_id) - d.start_date = form['start_date'].data + d.start_date_time = datetime.datetime.combine(form['start_date'].data, + form['start_time'].data) + d.end_date_time = datetime.datetime.combine(form['end_date'].data, + form['end_time'].data) + d.duration = form['duration'].data d.active = form['active'].data d.description = form['description'].data @@ -496,7 +514,10 @@ def add_quiz(request, quiz_id=None): else: d = Quiz.objects.get(id=quiz_id) form = QuizForm() - form.initial['start_date'] = d.start_date + form.initial['start_date'] = d.start_date_time.date() + form.initial['start_time'] = d.start_date_time.time() + form.initial['end_date'] = d.end_date_time.date() + form.initial['end_time'] = d.end_date_time.time() form.initial['duration'] = d.duration form.initial['description'] = d.description form.initial['active'] = d.active @@ -759,6 +780,65 @@ def user_login(request): 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 + # if questionpaper_id is None: + # return my_redirect('/exam/quizzes/') + # try: + # """Right now the app is designed so there is only one active quiz + # at a particular time.""" + # questionpaper = QuestionPaper.objects.get(id=questionpaper_id) + # except QuestionPaper.DoesNotExist: + # msg = 'Quiz not found, please contact your '\ + # 'instructor/administrator. Please login again thereafter.' + # return complete(request, msg, attempt_num, questionpaper_id) + + # # if questionpaper.quiz.start_date_time: + # # quiz_enable_time = questionpaper.quiz.start_date_time + # # else: + # # quiz_enable_time = datetime.datetime.now() + # # if questionpaper.quiz.end_date_time: + # # quiz_disable_time = questionpaper.quiz.end_date_time + # # else: + # # quiz_disable_time = datetime.datetime.now() + # # quiz_enable_time = questionpaper.quiz.start_date_time + # # quiz_disable_time = questionpaper.quiz.end_date_time + + # if not questionpaper.quiz.active: + # reason = 'The quiz has been deactivated!' + # return complete(request, reason, attempt_num, questionpaper_id) + + # if quiz_enable_time <= datetime.datetime.now() <= quiz_disable_time: + # try: + # old_paper = AnswerPaper.objects.get( + # question_paper=questionpaper, user=user, attempt_number=attempt_num) + # q = old_paper.current_question() + # return show_question(request, q, attempt_num, questionpaper_id) + # except AnswerPaper.DoesNotExist: + # ip = request.META['REMOTE_ADDR'] + # key = gen_key(10) + # try: + # profile = user.get_profile() + # except Profile.DoesNotExist: + # msg = 'You do not have a profile and cannot take the quiz!' + # raise Http404(msg) + + # new_paper = questionpaper.make_answerpaper(user, ip, attempt_num) + # # Make user directory. + # user_dir = get_user_dir(user) + # return start(request, attempt_num, questionpaper_id) + # else: + # ci = RequestContext(request) + # quiz_expired = datetime.datetime.now() >= quiz_disable_time + # context = {'user': user, 'paper_id': questionpaper_id,\ + # 'attempt_num': attempt_num, + # 'enable_quiz_time': quiz_enable_time, + # 'disable_quiz_time': quiz_disable_time, + # 'quiz_expired': quiz_expired} + # return my_render_to_response('exam/intro.html', context, + # context_instance=ci) + """Check the user cedentials and if any quiz is available, start the exam.""" user = request.user @@ -792,7 +872,6 @@ def start(request, attempt_num=None, questionpaper_id=None): user_dir = get_user_dir(user) return start(request, attempt_num, questionpaper_id) - def get_questions(paper): ''' Takes answerpaper as an argument. Returns the total questions as @@ -1230,7 +1309,10 @@ def show_all_quiz(request): for j in data: d = Quiz.objects.get(id=j) form = QuizForm() - form.initial['start_date'] = d.start_date + form.initial['start_date'] = d.start_date_time.date() + form.initial['start_time'] = d.start_date_time.time() + form.initial['end_date'] = d.end_date_time.date() + form.initial['end_time'] = d.end_date_time.time() form.initial['duration'] = d.duration form.initial['active'] = d.active form.initial['description'] = d.description |