diff options
author | adityacp | 2018-04-04 15:35:22 +0530 |
---|---|---|
committer | adityacp | 2018-04-04 15:35:22 +0530 |
commit | 7c04964f7a4cd7d2621e6e4396ffb3bee76d5b0e (patch) | |
tree | d44c7d5ea929fae17b3fd1f6f62aaaeddad0964e | |
parent | 895102248fedae4dded163bfc8db0bb452f5bb6c (diff) | |
download | online_test-7c04964f7a4cd7d2621e6e4396ffb3bee76d5b0e.tar.gz online_test-7c04964f7a4cd7d2621e6e4396ffb3bee76d5b0e.tar.bz2 online_test-7c04964f7a4cd7d2621e6e4396ffb3bee76d5b0e.zip |
Change views.py, models.py, urls.py, custom_filters.py
- Add new view function to get per student course progress
- Change model methods to calculate completion percentage for module and course
- Add new url to fetch per student course progress
- Remove unnecessary template tag from custom_filter.py
-rw-r--r-- | yaksh/models.py | 24 | ||||
-rw-r--r-- | yaksh/templatetags/custom_filters.py | 6 | ||||
-rw-r--r-- | yaksh/urls.py | 2 | ||||
-rw-r--r-- | yaksh/views.py | 87 |
4 files changed, 88 insertions, 31 deletions
diff --git a/yaksh/models.py b/yaksh/models.py index d011bb0..4c9ab98 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -551,7 +551,7 @@ class LearningModule(models.Model): status_list = [unit.get_completion_status(user, course) for unit in units] count = status_list.count("completed") - percent = round((count / len(units)) * 100) + percent = round((count / units.count()) * 100) return percent def _create_module_copy(self, user, module_name): @@ -767,15 +767,14 @@ class Course(models.Model): next_index = 0 return modules.get(id=module_ids[next_index]) - def percent_completed(self, user): - modules = self.get_learning_modules() + def percent_completed(self, user, modules): if not modules: percent = 0.0 else: status_list = [module.get_module_complete_percent(self, user) for module in modules] count = sum(status_list) - percent = round((count / len(modules))) + percent = round((count / modules.count())) return percent def get_grade(self, user): @@ -786,6 +785,14 @@ class Course(models.Model): grade = "NA" return grade + def get_current_unit(self, user): + course_status = CourseStatus.objects.filter(course=self, user=user) + if course_status.exists(): + unit = course_status.first().current_unit + else: + unit = "NA" + return unit + def days_before_start(self): """ Get the days remaining for the start of the course """ if timezone.now() < self.start_enroll_time: @@ -794,6 +801,14 @@ class Course(models.Model): remaining_days = 0 return remaining_days + def get_completion_percent(self, user): + course_status = CourseStatus.objects.filter(course=self, user=user) + if course_status.exists(): + percentage = course_status.first().percent_completed + else: + percentage = 0 + return percentage + def __str__(self): return self.name @@ -808,6 +823,7 @@ class CourseStatus(models.Model): user = models.ForeignKey(User) grade = models.CharField(max_length=255, null=True, blank=True) percentage = models.FloatField(default=0.0) + percent_completed = models.IntegerField(default=0) def get_grade(self): return self.grade diff --git a/yaksh/templatetags/custom_filters.py b/yaksh/templatetags/custom_filters.py index 0c5eb5a..ee0d51a 100644 --- a/yaksh/templatetags/custom_filters.py +++ b/yaksh/templatetags/custom_filters.py @@ -66,14 +66,10 @@ def course_completion_percent(course, user): @register.simple_tag -def course_grade(course, user): - return course.get_grade(user) - - -@register.simple_tag def get_ordered_testcases(question, answerpaper): return question.get_ordered_test_cases(answerpaper) + @register.simple_tag def get_answer_for_arrange_options(ans, question): if type(ans) == bytes: diff --git a/yaksh/urls.py b/yaksh/urls.py index dd450ba..4d35e10 100644 --- a/yaksh/urls.py +++ b/yaksh/urls.py @@ -174,4 +174,6 @@ urlpatterns = [ views.course_status, name="course_status"), url(r'^manage/preview_questionpaper/(?P<questionpaper_id>\d+)/$', views.preview_questionpaper, name="preview_questionpaper"), + url(r'^manage/get_user_status/(?P<course_id>\d+)/(?P<student_id>\d+)/$', + views.get_user_data, name="get_user_data") ] diff --git a/yaksh/views.py b/yaksh/views.py index 1cb77fc..3a0b4e7 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -488,9 +488,6 @@ def start(request, questionpaper_id=None, attempt_num=None, course_id=None, 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) @@ -524,6 +521,9 @@ def start(request, questionpaper_id=None, attempt_num=None, course_id=None, return view_module(request, module_id=module_id, course_id=course_id, msg=msg) + # update course status with current unit + _update_unit_status(course_id, user, learning_unit) + # if any previous attempt last_attempt = AnswerPaper.objects.get_user_last_attempt( quest_paper, user, course_id) @@ -911,7 +911,7 @@ def quit(request, reason=None, attempt_num=None, questionpaper_id=None, @email_verified def complete(request, reason=None, attempt_num=None, questionpaper_id=None, course_id=None, module_id=None): - """Show a page to inform user that the quiz has been compeleted.""" + """Show a page to inform user that the quiz has been completed.""" user = request.user if questionpaper_id is None: message = reason or "An Unexpected Error occurred. Please contact your '\ @@ -2598,20 +2598,17 @@ def get_next_unit(request, course_id, module_id, current_unit_id=None, else: next_unit = learning_module.get_next_unit(current_learning_unit.id) - course_status = CourseStatus.objects.filter( + course_status, created = CourseStatus.objects.get_or_create( 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() # Add learning unit to completed units list if not first_unit: course_status.completed_units.add(current_learning_unit.id) + # Update course completion percentage + _update_course_percent(course, user) + # if last unit of current module go to next module is_last_unit = course.is_last_unit(learning_module, current_learning_unit.id) @@ -2738,9 +2735,11 @@ def course_modules(request, course_id, msg=None): msg = "{0} is either expired or not active".format(course.name) return quizlist_user(request, msg=msg) learning_modules = course.get_learning_modules() - context = {"course": course, "learning_modules": learning_modules, - "user": user, "msg": msg} + context = {"course": course, "user": user, "msg": msg} course_status = CourseStatus.objects.filter(course=course, user=user) + context['course_percentage'] = course.get_completion_percent(user) + context['modules'] = [(module, module.get_module_complete_percent(course, user)) + for module in learning_modules] if course_status.exists(): course_status = course_status.first() if not course_status.grade: @@ -2759,29 +2758,36 @@ def course_status(request, 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() + stud_details = [(student, course.get_grade(student), + course.get_completion_percent(student), + course.get_current_unit(student)) for student in students] context = { - 'course': course, 'students': students, - 'state': 'course_status', 'modules': course.get_learning_modules() + 'course': course, 'student_details': stud_details, + 'state': 'course_status' } 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( + course_status, created = CourseStatus.objects.get_or_create( 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() +def _update_course_percent(course, user): + course_status, created = CourseStatus.objects.get_or_create( + user=user, course=course, + ) + # Update course completion percent + modules = course.get_learning_modules() + course_status.percent_completed = course.percent_completed(user, modules) + course_status.save() + + @login_required @email_verified def preview_questionpaper(request, questionpaper_id): @@ -2799,3 +2805,40 @@ def preview_questionpaper(request, questionpaper_id): return my_render_to_response( 'yaksh/preview_questionpaper.html', context ) + + +@login_required +@email_verified +def get_user_data(request, course_id, student_id): + user = request.user + data = {} + response_kwargs = {} + response_kwargs['content_type'] = 'application/json' + course = Course.objects.get(id=course_id) + if not is_moderator(user): + data['msg'] = 'You are not a moderator' + data['status'] = False + elif not course.is_creator(user) and not course.is_teacher(user): + msg = 'You are neither course creator nor course teacher for {0}'.format( + course.name) + data['msg'] = msg + data['status'] = False + else: + student = User.objects.get(id=student_id) + data['status'] = True + modules = course.get_learning_modules() + module_percent = [(module, module.get_module_complete_percent(course, student)) + for module in modules] + data['modules'] = module_percent + _update_course_percent(course, student) + data['course_percentage'] = course.get_completion_percent(student) + data['student'] = student + template_path = os.path.join( + os.path.dirname(__file__), "templates", "yaksh", "user_status.html" + ) + with open(template_path) as f: + template_data = f.read() + template = Template(template_data) + context = Context(data) + data = template.render(context) + return HttpResponse(json.dumps({"user_data": data}), **response_kwargs) |