diff options
author | Prabhu Ramachandran | 2016-07-05 10:00:35 -0400 |
---|---|---|
committer | GitHub | 2016-07-05 10:00:35 -0400 |
commit | 15152d232ab81a1ec6af24b10424d85f1c01d170 (patch) | |
tree | 283190f3ab51896e75d538296036272e856a1f7d | |
parent | c8dfe62663d715dcf4b571c1f738428165734d2a (diff) | |
parent | 9c61c84194e6b52913dfc284765a7b7bd77201ad (diff) | |
download | online_test-15152d232ab81a1ec6af24b10424d85f1c01d170.tar.gz online_test-15152d232ab81a1ec6af24b10424d85f1c01d170.tar.bz2 online_test-15152d232ab81a1ec6af24b10424d85f1c01d170.zip |
Merge pull request #101 from maheshgudi/test_modes
Test modes
-rw-r--r-- | yaksh/forms.py | 3 | ||||
-rw-r--r-- | yaksh/models.py | 105 | ||||
-rw-r--r-- | yaksh/static/yaksh/js/add_quiz.js | 19 | ||||
-rw-r--r-- | yaksh/templates/manage.html | 15 | ||||
-rw-r--r-- | yaksh/templates/yaksh/add_quiz.html | 28 | ||||
-rw-r--r-- | yaksh/templates/yaksh/intro.html | 5 | ||||
-rw-r--r-- | yaksh/templates/yaksh/showquestions.html | 3 | ||||
-rw-r--r-- | yaksh/tests.py | 93 | ||||
-rw-r--r-- | yaksh/urls.py | 3 | ||||
-rw-r--r-- | yaksh/views.py | 128 |
10 files changed, 361 insertions, 41 deletions
diff --git a/yaksh/forms.py b/yaksh/forms.py index 26e0967..8b864d0 100644 --- a/yaksh/forms.py +++ b/yaksh/forms.py @@ -163,7 +163,7 @@ class QuizForm(forms.ModelForm): class Meta: model = Quiz - fields = '__all__' + exclude = ["is_trial"] class QuestionForm(forms.ModelForm): @@ -205,7 +205,6 @@ class CourseForm(forms.ModelForm): model = Course fields = ['name', 'active', 'enrollment'] - class ProfileForm(forms.ModelForm): """ profile form for students and moderators """ diff --git a/yaksh/models.py b/yaksh/models.py index 87f7c13..2c1554a 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -4,9 +4,10 @@ from random import sample, shuffle from itertools import islice, cycle from collections import Counter from django.db import models +from django.db.models import Q from django.contrib.auth.models import User from django.forms.models import model_to_dict -from django.contrib.contenttypes.models import ContentType +from django.contrib.contenttypes.models import ContentType from taggit.managers import TaggableManager from django.utils import timezone import pytz @@ -59,6 +60,17 @@ def get_model_class(model): ############################################################################### +class CourseManager(models.Manager): + + def create_trial_course(self, user): + """Creates a trial course for testing questions""" + trial_course = self.create(name="trial_course", enrollment="open", + creator=user, is_trial=True) + trial_course.enroll(False, user) + return trial_course + + +############################################################################### class Course(models.Model): """ Course for students""" name = models.CharField(max_length=128) @@ -70,7 +82,8 @@ class Course(models.Model): rejected = models.ManyToManyField(User, related_name='rejected') created_on = models.DateTimeField(auto_now_add=True) teachers = models.ManyToManyField(User, related_name='teachers') - + is_trial = models.BooleanField(default=False) + objects = CourseManager() def request(self, *users): self.requests.add(*users) @@ -111,7 +124,7 @@ class Course(models.Model): return True if self.enrollment == enrollment_methods[1][0] else False def get_quizzes(self): - return self.quiz_set.all() + return self.quiz_set.filter(is_trial=False) def activate(self): self.active = True @@ -280,10 +293,47 @@ class Answer(models.Model): def __unicode__(self): return self.answer + ############################################################################### class QuizManager(models.Manager): def get_active_quizzes(self): - return self.filter(active=True) + return self.filter(active=True, is_trial=False) + + def create_trial_quiz(self, trial_course, user): + """Creates a trial quiz for testing questions""" + trial_quiz = self.create(course=trial_course, + duration=1000, + description="trial_questions", + is_trial=True, + time_between_attempts=0 + ) + return trial_quiz + + def create_trial_from_quiz(self, original_quiz_id, user, godmode): + """Creates a trial quiz from existing quiz""" + trial_quiz_name = "Trial_orig_id_{0}_{1}".format(original_quiz_id, + "godmode" if godmode else "usermode" + ) + + if self.filter(description=trial_quiz_name).exists(): + trial_quiz = self.get(description=trial_quiz_name) + + else: + trial_quiz = self.get(id=original_quiz_id) + trial_quiz.course.enroll(False, user) + trial_quiz.pk = None + trial_quiz.description = trial_quiz_name + trial_quiz.is_trial = True + trial_quiz.time_between_attempts = 0 + trial_quiz.prerequisite = None + if godmode: + trial_quiz.duration = 1000 + trial_quiz.active = True + trial_quiz.start_date_time = timezone.now() + trial_quiz.end_date_time = datetime(2199, 1, 1, 0, 0, 0, 0) + trial_quiz.save() + return trial_quiz + ############################################################################### class Quiz(models.Model): """A quiz that students will participate in. One can think of this @@ -331,6 +381,8 @@ class Quiz(models.Model): time_between_attempts = models.IntegerField("Number of Days",\ choices=days_between_attempts) + is_trial = models.BooleanField(default=False) + objects = QuizManager() class Meta: @@ -342,7 +394,7 @@ class Quiz(models.Model): def has_prerequisite(self): return True if self.prerequisite else False - + def __unicode__(self): desc = self.description or 'Quiz' return '%s: on %s for %d minutes' % (desc, self.start_date_time, @@ -350,6 +402,47 @@ class Quiz(models.Model): ############################################################################### +class QuestionPaperManager(models.Manager): + + def _create_trial_from_questionpaper(self, original_quiz_id): + """Creates a copy of the original questionpaper""" + trial_questionpaper = self.get(quiz_id=original_quiz_id) + trial_questions = {"fixed_questions": trial_questionpaper + .fixed_questions.all(), + "random_questions": trial_questionpaper + .random_questions.all() + } + trial_questionpaper.pk = None + trial_questionpaper.save() + return trial_questionpaper, trial_questions + + def create_trial_paper_to_test_questions(self, trial_quiz, + questions_list): + """Creates a trial question paper to test selected questions""" + if questions_list is not None: + trial_questionpaper = self.create(quiz=trial_quiz, + total_marks=10, + ) + trial_questionpaper.fixed_questions.add(*questions_list) + return trial_questionpaper + + def create_trial_paper_to_test_quiz(self, trial_quiz, original_quiz_id): + """Creates a trial question paper to test quiz.""" + if self.filter(quiz=trial_quiz).exists(): + trial_questionpaper = self.get(quiz=trial_quiz) + else: + trial_questionpaper, trial_questions = self._create_trial_from_questionpaper\ + (original_quiz_id) + trial_questionpaper.quiz = trial_quiz + trial_questionpaper.fixed_questions\ + .add(*trial_questions["fixed_questions"]) + trial_questionpaper.random_questions\ + .add(*trial_questions["random_questions"]) + trial_questionpaper.save() + return trial_questionpaper + + +############################################################################### class QuestionPaper(models.Model): """Question paper stores the detail of the questions.""" @@ -368,6 +461,8 @@ class QuestionPaper(models.Model): # Total marks for the question paper. total_marks = models.FloatField() + objects = QuestionPaperManager() + def update_total_marks(self): """ Updates the total marks for the Question Paper""" marks = 0.0 diff --git a/yaksh/static/yaksh/js/add_quiz.js b/yaksh/static/yaksh/js/add_quiz.js index 184881c..33358a7 100644 --- a/yaksh/static/yaksh/js/add_quiz.js +++ b/yaksh/static/yaksh/js/add_quiz.js @@ -9,3 +9,22 @@ function test() document.getElementById("submit").innerHTML = "Save"; } } + +String.prototype.beginsWith = function (string) { + return(this.indexOf(string) === 0); +}; + +function usermode(location) +{ + var select = document.getElementById("id_prerequisite"); + var select_text = select.options[select.selectedIndex].text; + if (select_text.beginsWith("----")){ + window.alert("No prerequisite for this course.\n \ + You can attempt the quiz."); + window.location.replace(location); + } else { + window.alert(select_text + " is a prerequisite for this course.\n \ + You are still allowed to attempt this quiz.") + window.location.replace(location); + } + }
\ No newline at end of file diff --git a/yaksh/templates/manage.html b/yaksh/templates/manage.html index 4b9e2ba..334f6a2 100644 --- a/yaksh/templates/manage.html +++ b/yaksh/templates/manage.html @@ -77,6 +77,21 @@ <h5>Click on the button given below to add a new course.</h5> <button class="btn" type="button" onClick='location.replace("{{URL_ROOT}}/exam/manage/add_course");'>Add New Course</button> </center> + {% if trial_quiz %} + <h5/> You have trial papers. + <table class="bordered-table zebra-striped"> + <form action="" method="post"> + {% csrf_token %} + {% for quiz in trial_quiz %} + <tr> + <td> <input type = "checkbox" name="delete_quiz" value = {{quiz.id}}></input></td> + <td> <a href="{{URL_ROOT}}/exam/manage/gradeuser/{{quiz.id}}">{{quiz.description}}</td> + </tr> + {% endfor %} + </table> + <center><button class="btn" type="submit">Delete Selected</button></center> + </form> + {% endif %} {% endblock %} </div> </div> diff --git a/yaksh/templates/yaksh/add_quiz.html b/yaksh/templates/yaksh/add_quiz.html index c47d1f7..5a0bee4 100644 --- a/yaksh/templates/yaksh/add_quiz.html +++ b/yaksh/templates/yaksh/add_quiz.html @@ -11,6 +11,7 @@ {% endblock %} {% block onload %} onload="javascript:test();" {% endblock %} {% block manage %} + <form name=frm id=frm action="" method="post" > {% csrf_token %} <center> @@ -18,8 +19,33 @@ {{ form.as_table }} </table> </center> - + <center><button class="btn" type="submit" id="submit" name="questionpaper">Design Question Paper</button> + <button class="btn" type="button" name="button" onClick='location.replace("{{URL_ROOT}}/exam/manage/courses/");'>Cancel</button> </center> + </form> +{% if quiz_id %} + + <h5>You can check the quiz by attempting it in the following modes:</h5> + <center> + <button class="btn" type="button" name="button" onClick='usermode("{{URL_ROOT}}/exam/manage/usermode/{{quiz_id}}");'>User Mode</button> + + <button class="btn" type="button" name="button" onClick='location.replace("{{URL_ROOT}}/exam/manage/godmode/{{quiz_id}}");'>God Mode</button> + <a href="" onclick="$('#help').show(); return false;">Help </a> + </center> + <br/> + <div style="display: none;" id="help"> + <ol> + <li><b>User Mode:</b> Attempt quiz the way normal users will attempt i.e. - + <ul> + <li><i>Quiz will have the same duration as that of the original quiz.</li> + <li>Quiz won't start if the course is inactive or the quiz time has expired.</li> + <li>You will be notified about quiz prerequisites.(You can still attempt the quiz though)</i></li> + </ul> + </p> + <li> <b>God Mode:</b> Attempt quiz without any time or eligibilty constraints.</p> + <a href="" onclick="$('#help').hide(); return false"> Close </a> + </div> + {% endif %} {% endblock %} diff --git a/yaksh/templates/yaksh/intro.html b/yaksh/templates/yaksh/intro.html index f05e9a1..b4bc6d7 100644 --- a/yaksh/templates/yaksh/intro.html +++ b/yaksh/templates/yaksh/intro.html @@ -39,8 +39,11 @@ </li> </ul> <p> We hope you enjoy taking this exam !!!</p> - + {% if user == "moderator" %} + <form action="{{URL_ROOT}}/exam/manage/" method="post" align="center"> + {%else%} <form action="{{URL_ROOT}}/exam/quizzes/" method="post" align="center"> + {% endif %} {% csrf_token %} <center><button class="btn" name="home">Home</button></center> </form> diff --git a/yaksh/templates/yaksh/showquestions.html b/yaksh/templates/yaksh/showquestions.html index ea42797..2f4d218 100644 --- a/yaksh/templates/yaksh/showquestions.html +++ b/yaksh/templates/yaksh/showquestions.html @@ -57,7 +57,8 @@ <button class="btn" type="button" onclick='location.replace("{{URL_ROOT}}/exam/manage/addquestion/");'>Add Question</button> <button class="btn" type="submit" onClick="return confirm_delete(frm);" name='delete' value='delete'>Delete Selected</button> {% if questions %} -<button class="btn" type="submit" name='download' value='download'>Download Selected</button> +<button class="btn" type="submit" name='download' value='download'>Download Selected</button> +<button class="btn" type="submit" name="test" value="test">Test Selected</button> {% endif %} </form> {% endblock %} diff --git a/yaksh/tests.py b/yaksh/tests.py index e71a78f..441f0f6 100644 --- a/yaksh/tests.py +++ b/yaksh/tests.py @@ -8,6 +8,7 @@ from django.utils import timezone import pytz from django.contrib.auth.models import Group + def setUpModule(): # create user profile user = User.objects.create_user(username='demo_user', @@ -156,11 +157,15 @@ class QuestionTestCases(unittest.TestCase): self.assertTrue(question_data.active) self.assertEqual(question_data.snippet, 'def fact()') + ############################################################################### class QuizTestCases(unittest.TestCase): def setUp(self): + self.creator = User.objects.get(pk=1) + self.teacher = User.objects.get(pk=2) self.quiz1 = Quiz.objects.get(pk=1) self.quiz2 = Quiz.objects.get(pk=2) + self.trial_course = Course.objects.create_trial_course(self.creator) def test_quiz(self): """ Test Quiz""" @@ -188,6 +193,50 @@ class QuizTestCases(unittest.TestCase): for quiz in quizzes: self.assertTrue(quiz.active) + def test_create_trial_quiz(self): + """Test to check if trial quiz is created""" + trial_quiz = Quiz.objects.create_trial_quiz(self.trial_course, + self.creator + ) + self.assertEqual(trial_quiz.course, self.trial_course) + self.assertEqual(trial_quiz.duration, 1000) + self.assertEqual(trial_quiz.description, "trial_questions") + self.assertTrue(trial_quiz.is_trial) + self.assertEqual(trial_quiz.time_between_attempts, 0) + + def test_create_trial_from_quiz_godmode(self): + """Test to check if a copy of original quiz is created in godmode""" + trial_quiz = Quiz.objects.create_trial_from_quiz(self.quiz1.id, + self.creator, + True + ) + self.assertEqual(trial_quiz.description, "Trial_orig_id_1_godmode") + self.assertTrue(trial_quiz.is_trial) + self.assertEqual(trial_quiz.duration, 1000) + self.assertTrue(trial_quiz.active) + self.assertEqual(trial_quiz.end_date_time, + datetime(2199, 1, 1, 0, 0, 0, 0) + ) + self.assertEqual(trial_quiz.time_between_attempts, 0) + + def test_create_trial_from_quiz_usermode(self): + """Test to check if a copy of original quiz is created in usermode""" + trial_quiz = Quiz.objects.create_trial_from_quiz(self.quiz2.id, + self.creator, + False + ) + self.assertEqual(trial_quiz.description, "Trial_orig_id_2_usermode") + self.assertTrue(trial_quiz.is_trial) + self.assertEqual(trial_quiz.duration, self.quiz2.duration) + self.assertEqual(trial_quiz.active, self.quiz2.active) + self.assertEqual(trial_quiz.start_date_time, + self.quiz2.start_date_time + ) + self.assertEqual(trial_quiz.end_date_time, + self.quiz2.end_date_time + ) + self.assertEqual(trial_quiz.time_between_attempts, 0) + ############################################################################### class QuestionPaperTestCases(unittest.TestCase): @@ -245,6 +294,11 @@ class QuestionPaperTestCases(unittest.TestCase): user=self.user ) + # For Trial case + self.questions_list = [self.questions[3].id, self.questions[5].id] + trial_course = Course.objects.create_trial_course(self.user) + trial_quiz = Quiz.objects.create_trial_quiz(trial_course, self.user) + def test_questionpaper(self): """ Test question paper""" self.assertEqual(self.question_paper.quiz.description, 'demo quiz') @@ -297,6 +351,30 @@ class QuestionPaperTestCases(unittest.TestCase): # test can_attempt_now(self): self.assertFalse(self.question_paper.can_attempt_now(self.user)) + def test_create_trial_paper_to_test_quiz(self): + trial_paper = QuestionPaper.objects.create_trial_paper_to_test_quiz\ + (trial_quiz, + self.question_paper.id + ) + self.assertEqual(trial_paper.quiz, trial_quiz) + self.assertEqual(trial_paper.fixed_questions.all(), + self.question_paper.fixed_questions.all() + ) + self.assertEqual(trial_paper.random_questions.all(), + self.question_paper.random_questions.all() + ) + + def test_create_trial_paper_to_test_questions(self): + trial_paper = QuestionPaper.objects.\ + create_trial_paper_to_test_questions( + trial_quiz, self.questions_list + ) + self.assertEqual(trial_paper.quiz, trial_quiz) + self.assertEqual(self.questions_list, + self.question_paper.fixed_questions + .values_list("id", flat=True) + ) + ############################################################################### class AnswerPaperTestCases(unittest.TestCase): @@ -433,6 +511,7 @@ class CourseTestCases(unittest.TestCase): self.quiz1 = Quiz.objects.get(pk=1) self.quiz2 = Quiz.objects.get(pk=2) + def test_is_creator(self): """ Test is_creator method of Course""" self.assertTrue(self.course.is_creator(self.creator)) @@ -505,6 +584,18 @@ class CourseTestCases(unittest.TestCase): result = self.course.is_teacher(self.student2) self.assertTrue(result) + def test_create_trial_course(self): + """Test to check if trial course is created""" + # Test for manager method create_trial_course + trial_course = Course.objects.create_trial_course(self.creator) + self.assertEqual(trial_course.name, "trial_course") + self.assertEqual(trial_course.enrollment, "open") + self.assertTrue(trial_course.active) + self.assertEqual(trial_course.students.get(user=self.creator.id), + self.creator + ) + self.assertTrue(trial_course.is_trial) + ############################################################################### class TestCaseTestCases(unittest.TestCase): @@ -567,4 +658,4 @@ class TestCaseTestCases(unittest.TestCase): result = self.question1.consolidate_answer_data( user_answer="demo_answer" ) - self.assertEqual(result, self.answer_data_json)
\ No newline at end of file + self.assertEqual(result, self.answer_data_json) diff --git a/yaksh/urls.py b/yaksh/urls.py index feac8c1..d7b56e4 100644 --- a/yaksh/urls.py +++ b/yaksh/urls.py @@ -90,7 +90,8 @@ urlpatterns += [ url(r'^manage/allotted_course/$', views.allotted_courses), url(r'^manage/remove_teachers/(?P<course_id>\d+)/$', views.remove_teachers), url(r'^manage/download_questions/$', views.show_all_questions), - url(r'^manage/upload_questions/$', views.show_all_questions) + url(r'^manage/upload_questions/$', views.show_all_questions), + url(r'^manage/(?P<mode>[\w\-]+)/(?P<quiz_id>\d+)/$', views.test_quiz) ] diff --git a/yaksh/views.py b/yaksh/views.py index db2f9bf..02b2487 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -125,14 +125,13 @@ def quizlist_user(request): user = request.user avail_quizzes = Quiz.objects.get_active_quizzes() user_answerpapers = AnswerPaper.objects.filter(user=user) - courses = Course.objects.filter(active=True) + courses = Course.objects.filter(active=True, is_trial=False) context = { 'quizzes': avail_quizzes, 'user': user, 'courses': courses, 'quizzes_taken': user_answerpapers, } - return my_render_to_response("yaksh/quizzes_user.html", context) @@ -227,7 +226,7 @@ def add_quiz(request, quiz_id=None): ci = RequestContext(request) if not is_moderator(user): raise Http404('You are not allowed to view this page!') - + context = {} if request.method == "POST": if quiz_id is None: form = QuizForm(request.POST, user=user) @@ -239,18 +238,22 @@ def add_quiz(request, quiz_id=None): form = QuizForm(request.POST, user=user, instance=quiz) if form.is_valid(): form.save() + context["quiz_id"] = quiz_id return my_redirect("/exam/manage/") - return my_render_to_response('yaksh/add_quiz.html', - {'form': form}, - context_instance=ci) + + context["form"] = form + return my_render_to_response('yaksh/add_quiz.html', context, + context_instance=ci) else: if quiz_id is None: form = QuizForm(user=user) else: quiz = Quiz.objects.get(id=quiz_id) form = QuizForm(user=user, instance=quiz) + context["quiz_id"] = quiz_id + context["form"] = form return my_render_to_response('yaksh/add_quiz.html', - {'form': form}, + context, context_instance=ci) @@ -262,7 +265,7 @@ def show_all_questionpapers(request, questionpaper_id=None): raise Http404('You are not allowed to view this page!') if questionpaper_id is None: - qu_papers = QuestionPaper.objects.all() + qu_papers = QuestionPaper.objects.filter(is_trial=False) context = {'papers': qu_papers} return my_render_to_response('yaksh/showquestionpapers.html', context, context_instance=ci) @@ -282,8 +285,20 @@ def prof_manage(request): """Take credentials of the user with professor/moderator rights/permissions and log in.""" user = request.user + ci = RequestContext(request) if user.is_authenticated() and is_moderator(user): - question_papers = QuestionPaper.objects.filter(quiz__course__creator=user) + question_papers = QuestionPaper.objects.filter(quiz__course__creator=user, + quiz__is_trial=False + ) + trial_quiz = Quiz.objects.filter(course__creator=user, is_trial=True) + if request.method == "POST": + delete_quiz = request.POST.getlist('delete_quiz') + for quiz_id in delete_quiz: + quiz = Quiz.objects.get(id=quiz_id) + if quiz.course.is_trial == True: + quiz.course.delete() + else: + quiz.delete() users_per_paper = [] for paper in question_papers: answer_papers = AnswerPaper.objects.filter(question_paper=paper) @@ -293,8 +308,10 @@ rights/permissions and log in.""" 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} - return my_render_to_response('manage.html', context) + context = {'user': user, 'users_per_paper': users_per_paper, + 'trial_quiz': trial_quiz + } + return my_render_to_response('manage.html', context, context_instance=ci) return my_redirect('/exam/login/') @@ -341,11 +358,17 @@ def start(request, questionpaper_id=None, attempt_num=None): msg = 'Quiz not found, please contact your '\ 'instructor/administrator. Please login again thereafter.' return complete(request, msg, attempt_num, questionpaper_id=None) - if not quest_paper.quiz.course.is_enrolled(user) : + if not quest_paper.quiz.course.is_enrolled(user): raise Http404('You are not allowed to view this page!') # prerequisite check and passing criteria - if quest_paper.quiz.has_prerequisite() and not quest_paper.is_prerequisite_passed(user): - return quizlist_user(request) + if quest_paper.quiz.is_expired(): + if is_moderator(user): + return redirect("/exam/manage") + return redirect("/exam/quizzes") + if quest_paper.quiz.has_prerequisite() and not quest_paper.is_prerequisite_passed(user): + if is_moderator(user): + return redirect("/exam/manage") + return redirect("/exam/quizzes") # if any previous attempt last_attempt = AnswerPaper.objects.get_user_last_attempt( questionpaper=quest_paper, user=user) @@ -353,11 +376,15 @@ def start(request, questionpaper_id=None, attempt_num=None): return show_question(request, last_attempt.current_question(), last_attempt) # allowed to start if not quest_paper.can_attempt_now(user): - return quizlist_user(request) + if is_moderator(user): + return redirect("/exam/manage") + return redirect("/exam/quizzes") if attempt_num is None: attempt_number = 1 if not last_attempt else last_attempt.attempt_number +1 context = {'user': user, 'questionpaper': quest_paper, 'attempt_num': attempt_number} + if is_moderator(user): + context["user"] = "moderator" return my_render_to_response('yaksh/intro.html', context, context_instance=ci) else: @@ -604,7 +631,7 @@ def courses(request): ci = RequestContext(request) if not is_moderator(user): raise Http404('You are not allowed to view this page') - courses = Course.objects.filter(creator=user) + courses = Course.objects.filter(creator=user, is_trial=False) return my_render_to_response('yaksh/courses.html', {'courses': courses}, context_instance=ci) @@ -725,8 +752,10 @@ def monitor(request, questionpaper_id=None): raise Http404('You are not allowed to view this page!') if questionpaper_id is None: - q_paper = QuestionPaper.objects.filter(Q(quiz__course__creator=user)| - Q(quiz__course__teachers=user)).distinct() + q_paper = QuestionPaper.objects.filter(Q(quiz__course__creator=user) | + Q(quiz__course__teachers=user), + quiz__course__is_trial=False + ).distinct() context = {'papers': [], 'quiz': None, 'quizzes': q_paper} @@ -734,9 +763,10 @@ def monitor(request, questionpaper_id=None): context_instance=ci) # quiz_id is not None. try: - q_paper = QuestionPaper.objects.filter(Q(quiz__course__creator=user)| - Q(quiz__course__teachers=user), - id=questionpaper_id).distinct() + q_paper = QuestionPaper.objects.filter(Q(quiz__course__creator=user) | + Q(quiz__course__teachers=user), + quiz__course__is_trial=False, + id=questionpaper_id).distinct() except QuestionPaper.DoesNotExist: papers = [] q_paper = None @@ -798,7 +828,7 @@ def ajax_questions_filter(request): @login_required def show_all_questions(request): - """Show a list of all the questions currently in the databse.""" + """Show a list of all the questions currently in the database.""" user = request.user ci = RequestContext(request) @@ -834,8 +864,17 @@ def show_all_questions(request): "{0}_questions.json"'.format(user) return response else: - msg = "Please select atleast one question" - context['msg'] = msg + context['msg'] = "Please select atleast one question to download" + + if request.POST.get('test') == 'test': + question_ids = request.POST.getlist("question") + if question_ids: + trial_paper = test_mode(user, False, question_ids, None) + trial_paper.update_total_marks() + trial_paper.save() + return my_redirect("/exam/start/1/{0}".format(trial_paper.id)) + else: + context["msg"] = "Please select atleast one question to test" questions = Question.objects.filter(user_id=user.id) form = QuestionFilterForm(user=user) @@ -918,14 +957,15 @@ def grade_user(request, quiz_id=None, user_id=None, attempt_number=None): ci = RequestContext(request) if not current_user.is_authenticated() or not is_moderator(current_user): raise Http404('You are not allowed to view this page!') - course_details = Course.objects.filter(Q(creator=current_user)| - Q(teachers=current_user)).distinct() + course_details = Course.objects.filter(Q(creator=current_user) | + Q(teachers=current_user), + is_trial=False).distinct() context = {"course_details": course_details} if quiz_id is not None: questionpaper_id = QuestionPaper.objects.filter(quiz_id=quiz_id)\ .values("id") - user_details = AnswerPaper.objects.get_users_for_questionpaper\ - (questionpaper_id) + user_details = AnswerPaper.objects\ + .get_users_for_questionpaper(questionpaper_id) context = {"users": user_details, "quiz_id": quiz_id} if user_id is not None: @@ -1167,7 +1207,7 @@ def allotted_courses(request): @login_required def remove_teachers(request, course_id): """ remove user from a course """ - + user = request.user if not is_moderator(user): raise Http404('You are not allowed to view this page!') @@ -1178,3 +1218,33 @@ def remove_teachers(request, course_id): teachers = User.objects.filter(id__in=teacher_ids) course.remove_teachers(*teachers) return my_redirect('/exam/manage/courses') + + +def test_mode(user, godmode=False, questions_list=None, quiz_id=None): + """creates a trial question paper for the moderators""" + + if questions_list is not None: + trial_course = Course.objects.create_trial_course(user) + trial_quiz = Quiz.objects.create_trial_quiz(trial_course, user) + trial_questionpaper = QuestionPaper.objects\ + .create_trial_paper_to_test_questions\ + (trial_quiz, questions_list) + else: + trial_quiz = Quiz.objects.create_trial_from_quiz(quiz_id, user, godmode) + trial_questionpaper = QuestionPaper.objects\ + .create_trial_paper_to_test_quiz\ + (trial_quiz, quiz_id) + return trial_questionpaper + + +@login_required +def test_quiz(request, mode, quiz_id): + """creates a trial quiz for the moderators""" + godmode = True if mode == "godmode" else False + current_user = request.user + quiz = Quiz.objects.get(id=quiz_id) + if (quiz.is_expired() or not quiz.active) and not godmode: + return my_redirect('/exam/manage') + + trial_questionpaper = test_mode(current_user, godmode, None, quiz_id) + return my_redirect("/exam/start/{0}".format(trial_questionpaper.id)) |