summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--yaksh/models.py64
-rw-r--r--yaksh/urls.py4
-rw-r--r--yaksh/views.py40
3 files changed, 89 insertions, 19 deletions
diff --git a/yaksh/models.py b/yaksh/models.py
index 1e45851..d5b2923 100644
--- a/yaksh/models.py
+++ b/yaksh/models.py
@@ -161,6 +161,23 @@ class Lesson(models.Model):
def get_files(self):
return LessonFile.objects.filter(lesson=self)
+ def _create_lesson_copy(self, user):
+ lesson_files = self.get_files()
+ new_lesson = self
+ new_lesson.id = None
+ new_lesson.name = "Copy of {0}".format(self.name)
+ new_lesson.creator = user
+ new_lesson.save()
+ for _file in lesson_files:
+ file_name = os.path.basename(_file.file.name)
+ if os.path.exists(_file.file.path):
+ lesson_file = open(_file.file.path, "rb")
+ django_file = File(lesson_file)
+ lesson_file_obj = LessonFile()
+ lesson_file_obj.lesson = new_lesson
+ lesson_file_obj.file.save(file_name, django_file, save=True)
+ return new_lesson
+
#############################################################################
class LessonFile(models.Model):
@@ -352,6 +369,17 @@ class Quiz(models.Model):
course=course, passed=False
).values_list("user", flat=True).distinct().count()
+ def _create_quiz_copy(self, user):
+ question_papers = self.questionpaper_set.all()
+ new_quiz = self
+ new_quiz.id = None
+ new_quiz.description = "Copy of {0}".format(self.description)
+ new_quiz.creator = user
+ new_quiz.save()
+ for qp in question_papers:
+ qp._create_duplicate_questionpaper(new_quiz)
+ return new_quiz
+
def __str__(self):
desc = self.description or 'Quiz'
return '%s: on %s for %d minutes' % (desc, self.start_date_time,
@@ -402,6 +430,17 @@ class LearningUnit(models.Model):
success = False
return success
+ def _create_unit_copy(self, user):
+ if self.type == "quiz":
+ new_quiz = self.quiz._create_quiz_copy(user)
+ new_unit = LearningUnit.objects.create(
+ order=self.order, type="quiz", quiz=new_quiz)
+ else:
+ new_lesson = self.lesson._create_lesson_copy(user)
+ new_unit = LearningUnit.objects.create(
+ order=self.order, type="lesson", lesson=new_lesson)
+ return new_unit
+
###############################################################################
class LearningModule(models.Model):
@@ -496,6 +535,18 @@ class LearningModule(models.Model):
percent = round((count / len(units)) * 100)
return percent
+ def _create_module_copy(self, user, module_name):
+ learning_units = self.learning_unit.order_by("order")
+ new_module = self
+ new_module.id = None
+ new_module.name = module_name
+ new_module.creator = user
+ new_module.save()
+ for unit in learning_units:
+ new_unit = unit._create_unit_copy(user)
+ new_module.learning_unit.add(new_unit)
+ return new_module
+
def __str__(self):
return self.name
@@ -556,6 +607,15 @@ class Course(models.Model):
return new_course
+ def create_shallow_copy(self, user):
+ learning_modules = self.learning_module.order_by("order")
+ copy_course_name = "Copy Of {0}".format(self.name)
+ new_course = self._create_duplicate_instance(user, copy_course_name)
+ for module in learning_modules:
+ copy_module_name = "Copy of {0}".format(module.name)
+ new_module = module._create_module_copy(user, copy_module_name)
+ new_course.learning_module.add(new_module)
+
def request(self, *users):
self.requests.add(*users)
@@ -1129,8 +1189,8 @@ class QuestionPaper(models.Model):
return questions
def _create_duplicate_questionpaper(self, quiz):
- new_questionpaper = QuestionPaper.objects.create(quiz=quiz,
- shuffle_questions=self.shuffle_questions,
+ new_questionpaper = QuestionPaper.objects.create(
+ quiz=quiz, shuffle_questions=self.shuffle_questions,
total_marks=self.total_marks,
fixed_question_order=self.fixed_question_order
)
diff --git a/yaksh/urls.py b/yaksh/urls.py
index 08c2091..b1e6249 100644
--- a/yaksh/urls.py
+++ b/yaksh/urls.py
@@ -86,8 +86,8 @@ urlpatterns = [
views.show_statistics, name="show_statistics"),
url(r'^manage/download_quiz_csv/(?P<course_id>\d+)/(?P<quiz_id>\d+)/$',
views.download_quiz_csv, name="download_quiz_csv"),
- url(r'^manage/duplicate_course/(?P<course_id>\d+)/$', views.duplicate_course,
- name='duplicate_course'),
+ url(r'^manage/duplicate_course/(?P<copy_type>copy|clone)/(?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 2bc5dfe..1055bda 100644
--- a/yaksh/views.py
+++ b/yaksh/views.py
@@ -390,8 +390,8 @@ def prof_manage(request, msg=None):
return my_redirect('/exam/login')
if not is_moderator(user):
return my_redirect('/exam/')
- courses = Course.objects.filter(creator=user, is_trial=False)
-
+ courses = Course.objects.filter(Q(creator=user) | Q(teachers=user),
+ is_trial=False)
trial_paper = AnswerPaper.objects.filter(
user=user, question_paper__quiz__is_trial=True,
course__is_trial=True
@@ -399,17 +399,18 @@ def prof_manage(request, msg=None):
if request.method == "POST":
delete_paper = request.POST.getlist('delete_paper')
for answerpaper_id in delete_paper:
- answerpaper = AnswerPaper.objects.get(id=answerpaper_id)
- qpaper = answerpaper.question_paper
- answerpaper.course.remove_trial_modules()
- answerpaper.course.delete()
- if qpaper.quiz.is_trial:
- qpaper.quiz.delete()
- else:
- if qpaper.answerpaper_set.count() == 1:
+ answerpaper = AnswerPaper.objects.filter(id=answerpaper_id)
+ if answerpaper.exists():
+ qpaper = answerpaper.first().question_paper
+ answerpaper.first().course.remove_trial_modules()
+ answerpaper.first().course.delete()
+ if qpaper.quiz.is_trial:
qpaper.quiz.delete()
else:
- answerpaper.delete()
+ if qpaper.answerpaper_set.count() == 1:
+ qpaper.quiz.delete()
+ else:
+ answerpaper.delete()
context = {'user': user, 'courses': courses,
'trial_paper': trial_paper, 'msg': msg
}
@@ -2240,17 +2241,26 @@ def download_sample_csv(request):
@login_required
@email_verified
-def duplicate_course(request, course_id):
+def duplicate_course(request, copy_type, course_id):
user = request.user
course = Course.objects.get(id=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)
+ if copy_type == "copy":
+ # Link all the modules from current course to copied course
+ course.create_duplicate_course(user)
+ else:
+ # Create new entries of modules, lessons/quizzes
+ # from current course to copied course
+ course.create_shallow_copy(user)
else:
- msg = 'You do not have permissions to clone this course, please contact your '\
- 'instructor/administrator.'
+ msg = dedent(
+ '''\
+ You do not have permissions to clone {0} course, please contact
+ your instructor/administrator.'''.format(course.name)
+ )
return complete(request, msg, attempt_num=None, questionpaper_id=None)
return my_redirect('/exam/manage/courses/')