diff options
author | ankitjavalkar | 2017-04-12 19:03:43 +0530 |
---|---|---|
committer | ankitjavalkar | 2017-04-28 17:16:45 +0530 |
commit | fd4198167dd4caea0ed9a56f02e82f424495f7b9 (patch) | |
tree | 6daf3cab464603a19382a6c091cc7dc6e2ea113d | |
parent | b285378b2eca29e7125bf9474cd57c973a202f37 (diff) | |
download | online_test-fd4198167dd4caea0ed9a56f02e82f424495f7b9.tar.gz online_test-fd4198167dd4caea0ed9a56f02e82f424495f7b9.tar.bz2 online_test-fd4198167dd4caea0ed9a56f02e82f424495f7b9.zip |
Add feature to allow cloning a course
-rw-r--r-- | yaksh/models.py | 68 | ||||
-rw-r--r-- | yaksh/templates/yaksh/course_detail.html | 4 | ||||
-rw-r--r-- | yaksh/urls.py | 2 | ||||
-rw-r--r-- | yaksh/views.py | 12 |
4 files changed, 86 insertions, 0 deletions
diff --git a/yaksh/models.py b/yaksh/models.py index f6867f0..e856f9b 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -151,6 +151,41 @@ class Course(models.Model): objects = CourseManager() + def create_duplicate_course(self, user): ##@@ + quizzes = self.quiz_set.all() + prerequisite_map = [] + duplicate_quiz_map = {} + new_course = Course.objects.create(creator=user, + name="Copy Of {0}".format(self.name), + enrollment=self.enrollment, + active=self.active, + is_trial=self.is_trial, + instructions=self.instructions, + start_enroll_time=self.start_enroll_time, + end_enroll_time=self.end_enroll_time + ) + + new_course.teachers.add(*self.teachers.all()) + + for q in quizzes: + new_quiz = q._create_duplicate_quiz(new_course) + if q.id not in duplicate_quiz_map.keys(): + duplicate_quiz_map[q.id] = new_quiz.id + + for orig_qid, dupl_qid in duplicate_quiz_map.items(): + original_quiz = Quiz.objects.get(id=orig_qid) + if original_quiz.prerequisite: + duplicate_quiz = Quiz.objects.get(id=dupl_qid) + prereq_id = original_quiz.prerequisite.id + duplicate_prereq_id = duplicate_quiz_map.get(prereq_id) + if duplicate_prereq_id: + duplicate_prerequisite = Quiz.objects.get( + id=duplicate_prereq_id + ) + duplicate_quiz.prerequite.add(duplicate_prerequisite) + + return new_course + def request(self, *users): self.requests.add(*users) @@ -656,6 +691,28 @@ class Quiz(models.Model): def has_prerequisite(self): return True if self.prerequisite else False + def _create_duplicate_quiz(self, course): + questionpaper = self.questionpaper_set.all() + new_quiz = Quiz.objects.create(course=course, + start_date_time=self.start_date_time, + end_date_time=self.end_date_time, + duration=self.duration, + active=self.active, + description="Copy Of {0}".format(self.description), + pass_criteria=self.pass_criteria, + language=self.language, + attempts_allowed=self.attempts_allowed, + time_between_attempts=self.time_between_attempts, + is_trial=self.is_trial, + instructions=self.instructions, + allow_skip=self.allow_skip + ) + + for qp in questionpaper: + qp._create_duplicate_questionpaper(new_quiz) + + return new_quiz + def create_demo_quiz(self, course): demo_quiz = Quiz.objects.create( start_date_time=timezone.now(), @@ -741,6 +798,17 @@ class QuestionPaper(models.Model): objects = QuestionPaperManager() + def _create_duplicate_questionpaper(self, quiz): + new_questionpaper = QuestionPaper.objects.create(quiz=quiz, + shuffle_questions=self.shuffle_questions, + total_marks=self.total_marks, + fixed_question_order=self.fixed_question_order + ) + new_questionpaper.fixed_questions.add(*self.fixed_questions.all()) + new_questionpaper.random_questions.add(*self.random_questions.all()) + + return new_questionpaper + def update_total_marks(self): """ Updates the total marks for the Question Paper""" marks = 0.0 diff --git a/yaksh/templates/yaksh/course_detail.html b/yaksh/templates/yaksh/course_detail.html index 81569fa..cd4144f 100644 --- a/yaksh/templates/yaksh/course_detail.html +++ b/yaksh/templates/yaksh/course_detail.html @@ -20,6 +20,10 @@ <a href="{{URL_ROOT}}/exam/manage/toggle_status/{{ course.id }}/"> {% if course.active %}Deactivate Course {% else %} Activate Course {% endif %}</a> </li> + <li> + <a href="{{URL_ROOT}}/exam/manage/duplicate_course/{{ course.id }}/"> + Clone Course</a> + </li> </ul> </div> </div> diff --git a/yaksh/urls.py b/yaksh/urls.py index 20ce918..b02b797 100644 --- a/yaksh/urls.py +++ b/yaksh/urls.py @@ -59,6 +59,8 @@ urlpatterns = [ views.show_statistics), url(r'^manage/monitor/download_csv/(?P<questionpaper_id>\d+)/$', views.download_csv), + url(r'^manage/duplicate_course/(?P<course_id>\d+)/$', views.duplicate_course, + name='duplicate_course'), url(r'manage/courses/$', views.courses, name='courses'), url(r'manage/add_course/$', views.add_course, name='add_course'), url(r'manage/edit_course/(?P<course_id>\d+)$', views.add_course, name='edit_course'), diff --git a/yaksh/views.py b/yaksh/views.py index 35df9f9..bad91f8 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -1597,3 +1597,15 @@ def download_assignment_file(request, quiz_id, question_id=None, user_id=None): ) response.write(zipfile_name.read()) return response + +@login_required +def duplicate_course(request, course_id): + user = request.user + course = get_object_or_404(Course, pk=course_id) + if not is_moderator(user): + raise Http404('You are not allowed to view this page!') + + if course.is_teacher(user) or course.is_creator(user): + course.create_duplicate_course(user) + + return my_redirect('/exam/manage/courses/') |