diff options
Diffstat (limited to 'yaksh/views.py')
-rw-r--r-- | yaksh/views.py | 108 |
1 files changed, 85 insertions, 23 deletions
diff --git a/yaksh/views.py b/yaksh/views.py index 49249ca..2bc5dfe 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -320,10 +320,9 @@ def add_quiz(request, quiz_id=None, course_id=None): return my_redirect("/exam/manage/courses/") else: - quiz = Quiz.objects.get(id=quiz_id) if quiz_id else None form = QuizForm(instance=quiz) - context["quiz_id"] = quiz_id context["course_id"] = course_id + context["quiz"] = quiz context["form"] = form return my_render_to_response( 'yaksh/add_quiz.html', context, context_instance=ci @@ -346,7 +345,7 @@ def add_exercise(request, quiz_id=None, course_id=None): if course_id: course = get_object_or_404(Course, pk=course_id) if not course.is_creator(user) and not course.is_teacher(user): - raise Http404('This quiz does not belong to you') + raise Http404('This Course does not belong to you') context = {} if request.method == "POST": @@ -370,9 +369,8 @@ def add_exercise(request, quiz_id=None, course_id=None): return my_redirect("/exam/manage/courses/") else: - quiz = Quiz.objects.get(id=quiz_id) if quiz_id else None form = ExerciseForm(instance=quiz) - context["quiz_id"] = quiz_id + context["exercise"] = quiz context["course_id"] = course_id context["form"] = form return my_render_to_response( @@ -477,6 +475,16 @@ def start(request, questionpaper_id=None, attempt_num=None, course_id=None, learning_module = course.learning_module.get(id=module_id) learning_unit = learning_module.learning_unit.get(quiz=quest_paper.quiz.id) + # unit module prerequiste check + if learning_module.has_prerequisite(): + if not learning_module.is_prerequisite_passed(user, course): + msg = "You have not completed the module previous to {0}".format( + learning_module.name) + return course_modules(request, course_id, msg) + + # update course status with current unit + _update_unit_status(course_id, user, learning_unit) + # is user enrolled in the course if not course.is_enrolled(user): msg = 'You are not enrolled in {0} course'.format(course.name) @@ -931,14 +939,14 @@ def add_course(request, course_id=None): raise Http404("You are not allowed to view this course") else: course = None - if not is_moderator(user): raise Http404('You are not allowed to view this page') if request.method == 'POST': form = CourseForm(request.POST, instance=course) if form.is_valid(): new_course = form.save(commit=False) - new_course.creator = user + if course_id is None: + new_course.creator = user new_course.save() return my_redirect('/exam/manage/courses') else: @@ -1157,7 +1165,7 @@ def show_statistics(request, questionpaper_id, attempt_number=None, course_id): return my_redirect('/exam/manage/') question_stats = AnswerPaper.objects.get_question_statistics( - questionpaper_id, attempt_number + questionpaper_id, attempt_number, course_id ) context = {'question_stats': question_stats, 'quiz': quiz, 'questionpaper_id': questionpaper_id, @@ -1291,14 +1299,21 @@ def _remove_already_present(questionpaper_id, questions): @login_required @email_verified -def design_questionpaper(request, quiz_id, questionpaper_id=None): +def design_questionpaper(request, quiz_id, questionpaper_id=None, + course_id=None): user = request.user if not is_moderator(user): raise Http404('You are not allowed to view this page!') - quiz = Quiz.objects.get(id=quiz_id) - if not quiz.creator == user: - raise Http404('This course does not belong to you') + if quiz_id: + quiz = get_object_or_404(Quiz, pk=quiz_id) + if quiz.creator != user and not course_id: + raise Http404('This quiz does not belong to you') + if course_id: + course = get_object_or_404(Course, pk=course_id) + if not course.is_creator(user) and not course.is_teacher(user): + raise Http404('This Course does not belong to you') + filter_form = QuestionFilterForm(user=user) questions = None marks = None @@ -1306,7 +1321,8 @@ def design_questionpaper(request, quiz_id, questionpaper_id=None): if questionpaper_id is None: question_paper = QuestionPaper.objects.get_or_create(quiz_id=quiz_id)[0] else: - question_paper = get_object_or_404(QuestionPaper, id=questionpaper_id) + question_paper = get_object_or_404(QuestionPaper, id=questionpaper_id, + quiz_id=quiz_id) qpaper_form = QuestionPaperForm(instance=question_paper) if request.method == 'POST': @@ -1523,9 +1539,9 @@ def download_quiz_csv(request, course_id, quiz_id): questions = question_paper.get_question_bank() answerpapers = AnswerPaper.objects.filter(question_paper=question_paper, - attempt_number=attempt_number) + attempt_number=attempt_number, course_id=course_id) if not answerpapers: - return monitor(request, quiz_id) + return monitor(request, quiz_id, course_id) response = HttpResponse(content_type='text/csv') response['Content-Disposition'] = 'attachment; filename="{0}-{1}-attempt{2}.csv"'.format( course.name.replace('.', ''), quiz.description.replace('.', ''), @@ -2332,6 +2348,15 @@ def show_lesson(request, lesson_id, module_id, course_id): learn_module = course.learning_module.get(id=module_id) learn_unit = learn_module.learning_unit.get(lesson_id=lesson_id) learning_units = learn_module.get_learning_units() + if learn_module.has_prerequisite(): + if not learn_module.is_prerequisite_passed(user, course): + msg = "You have not completed the module previous to {0}".format( + learn_module.name) + return view_module(request, module_id, course_id, msg) + + # update course status with current unit + _update_unit_status(course_id, user, learn_unit) + all_modules = course.get_learning_modules() if learn_unit.has_prerequisite(): if not learn_unit.is_prerequisite_passed(user, learn_module, course): @@ -2508,7 +2533,7 @@ def preview_html_text(request): @login_required @email_verified -def get_next_unit(request, course_id, module_id, current_unit_id, +def get_next_unit(request, course_id, module_id, current_unit_id=None, first_unit=None): user = request.user course = Course.objects.prefetch_related("learning_module").get( @@ -2517,8 +2542,14 @@ def get_next_unit(request, course_id, module_id, current_unit_id, raise Http404('You are not enrolled for this course!') learning_module = course.learning_module.prefetch_related( "learning_unit").get(id=module_id) - current_learning_unit = learning_module.learning_unit.get( - id=current_unit_id) + + if current_unit_id: + current_learning_unit = learning_module.learning_unit.get( + id=current_unit_id) + else: + next_module = course.next_module(learning_module.id) + return my_redirect("/exam/quizzes/view_module/{0}/{1}".format( + next_module.id, course_id)) if first_unit: next_unit = current_learning_unit @@ -2547,9 +2578,6 @@ def get_next_unit(request, course_id, module_id, current_unit_id, return my_redirect("/exam/quizzes/view_module/{0}/{1}/".format( next_module.id, course.id)) - # make next available unit as current unit - course_status.current_unit = next_unit - course_status.save() if next_unit.type == "quiz": return my_redirect("/exam/start/{0}/{1}/{2}".format( next_unit.quiz.questionpaper_set.get().id, module_id, course_id)) @@ -2635,13 +2663,14 @@ def view_module(request, module_id, course_id, msg=None): all_modules = course.get_learning_modules() if learning_module.has_prerequisite(): if not learning_module.is_prerequisite_passed(user, course): - msg = "You have not completed the previous learning module" + msg = "You have not completed the module previous to {0}".format( + learning_module.name) return course_modules(request, course_id, msg) learning_units = learning_module.get_learning_units() context['learning_units'] = learning_units context['learning_module'] = learning_module - context['first_unit'] = learning_units[0] + context['first_unit'] = learning_units.first() context['all_modules'] = all_modules context['user'] = user context['course'] = course @@ -2666,3 +2695,36 @@ def course_modules(request, course_id, msg=None): context = {"course": course, "learning_modules": learning_modules, "user": user, "msg": msg} return my_render_to_response('yaksh/course_modules.html', context) + + +@login_required +@email_verified +def course_status(request, course_id): + user = request.user + if not is_moderator(user): + raise Http404('You are not allowed to view this page!') + course = get_object_or_404(Course, pk=course_id) + if not course.is_creator(user) and not course.is_teacher(user): + raise Http404('This course does not belong to you') + students = course.get_only_students() + context = { + 'course': course, 'students': students, + 'state': 'course_status', 'modules': course.get_learning_modules() + } + return my_render_to_response('yaksh/course_detail.html', context) + + +def _update_unit_status(course_id, user, unit): + """ Update course status with current unit """ + course_status = CourseStatus.objects.filter( + user=user, course_id=course_id, + ) + if not course_status.exists(): + course_status = CourseStatus.objects.create( + user=user, course_id=course_id + ) + else: + course_status = course_status.first() + # make next available unit as current unit + course_status.current_unit = unit + course_status.save() |