From e461b7c71d3d33f7e1cde4415ae35733078f4070 Mon Sep 17 00:00:00 2001
From: adityacp
Date: Thu, 3 Dec 2020 14:32:15 +0530
Subject: Show graphs for the lesson video statistics
---
stats/models.py | 28 ++++++++++++---
stats/templates/view_lesson_tracking.html | 59 ++++++++++++++++++++++++++++---
stats/views.py | 25 ++++++++-----
3 files changed, 95 insertions(+), 17 deletions(-)
(limited to 'stats')
diff --git a/stats/models.py b/stats/models.py
index 56c7f0d..60bc7bd 100644
--- a/stats/models.py
+++ b/stats/models.py
@@ -24,6 +24,22 @@ def time_to_seconds(time):
seconds=time.second).total_seconds()
+class TrackLessonManager(models.Manager):
+ def get_percentage_data(self, tracked_lessons):
+ percentage_data = {"1": 0, "2": 0, "3": 0, "4": 0}
+ for tracked in tracked_lessons:
+ percent = tracked.get_percentage_complete()
+ if percent < 25:
+ percentage_data["1"] = percentage_data["1"] + 1
+ elif percent >= 25 and percent < 50:
+ percentage_data["2"] = percentage_data["2"] + 1
+ elif percent >= 50 and percent < 75:
+ percentage_data["3"] = percentage_data["3"] + 1
+ elif percent >= 75:
+ percentage_data["4"] = percentage_data["4"] + 1
+ return percentage_data
+
+
class TrackLesson(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
course = models.ForeignKey(Course, on_delete=models.CASCADE)
@@ -33,6 +49,8 @@ class TrackLesson(models.Model):
creation_time = models.DateTimeField(auto_now_add=True)
watched = models.BooleanField(default=False)
+ objects = TrackLessonManager()
+
class Meta:
unique_together = ('user', 'course', 'lesson')
@@ -41,12 +59,12 @@ class TrackLesson(models.Model):
def get_current_time(self):
if self.current_time == '00:00:00':
- return 'just started'
+ return '00:00:00'
return self.current_time
def get_video_duration(self):
if self.video_duration == '00:00:00':
- return 'will be available after 25% completion'
+ return '00:00:00'
return self.video_duration
def set_current_time(self, ct):
@@ -58,13 +76,13 @@ class TrackLesson(models.Model):
def get_percentage_complete(self):
if self.current_time == '00:00:00' and self.video_duration == '00:00:00':
- return 'less than 25%'
+ return 0
duration = str_to_time(self.video_duration)
watch_time = str_to_time(self.current_time)
duration_seconds = time_to_seconds(duration)
watched_seconds = time_to_seconds(watch_time)
percentage = round((watched_seconds / duration_seconds) * 100)
- return 'approx {0} %'.format(percentage)
+ return percentage
def get_last_access_time(self):
@@ -78,7 +96,7 @@ class TrackLesson(models.Model):
if self.current_time != '00:00:00' and self.video_duration != '00:00:00':
duration = str_to_time(self.video_duration)
watch_time = (str_to_datetime(self.current_time) + timezone.timedelta(
- seconds=10)).time()
+ seconds=120)).time()
self.watched = watch_time >= duration
def get_watched(self):
diff --git a/stats/templates/view_lesson_tracking.html b/stats/templates/view_lesson_tracking.html
index d8d35c2..a9d3025 100644
--- a/stats/templates/view_lesson_tracking.html
+++ b/stats/templates/view_lesson_tracking.html
@@ -1,6 +1,6 @@
{% extends "manage.html" %}
{% load static %}
-{% block title %} Lesson Views {% endblock %}
+{% block title %} Lesson Video Stats {% endblock %}
{% block script %}
@@ -33,7 +33,58 @@
{% include "yaksh/paginator.html" %}
-
{{track.creation_time}} | {{track.get_current_time}} | {{track.get_video_duration}} | -{{track.get_percentage_complete}} | +{{track.get_percentage_complete}} % | {% with track.get_watched as watched %} {% if watched %} {{watched}} {% else %} - {{watched}} + {{watched}} {% endif %} {% endwith %} | diff --git a/stats/views.py b/stats/views.py index 53b7cf7..a5cdeb7 100644 --- a/stats/views.py +++ b/stats/views.py @@ -27,14 +27,14 @@ def add_tracker(request, tracker_id): if current_time: track.set_current_time(current_time) track.video_duration = video_duration - LessonLog.objects.create( - track_id=track.id, current_time=current_time, - last_access_time=timezone.now() - ) track.save() if not track.watched: track.set_watched() track.save() + LessonLog.objects.create( + track_id=track.id, current_time=current_time, + last_access_time=timezone.now() + ) success = True else: success = False @@ -46,16 +46,25 @@ def add_tracker(request, tracker_id): @email_verified def view_lesson_watch_stats(request, course_id, lesson_id): user = request.user - course = get_object_or_404(Course, pk=course_id) + course = get_object_or_404( + Course.objects.prefetch_related("students"), id=course_id + ) if not course.is_creator(user) and not course.is_teacher(user): raise Http404('This course does not belong to you') trackings = TrackLesson.objects.get_queryset().filter( course_id=course_id, lesson_id=lesson_id ).order_by("id") - total = trackings.count() + percentage_data = TrackLesson.objects.get_percentage_data(trackings) + visited = trackings.count() + completed = trackings.filter(watched=True).count() + students_total = course.students.count() paginator = Paginator(trackings, 30) page = request.GET.get('page') trackings = paginator.get_page(page) - context = {'objects': trackings, 'total': total, 'course_id': course_id, - 'lesson_id': lesson_id} + context = { + 'objects': trackings, 'total': visited, 'course_id': course_id, + 'lesson_id': lesson_id, "percentage_data": percentage_data, + 'completion': [completed, students_total-completed, students_total], + 'visits': [visited, students_total-visited, students_total] + } return render(request, 'view_lesson_tracking.html', context) -- cgit From abee2fe4085f42ca3b92192d59150a780a765c26 Mon Sep 17 00:00:00 2001 From: adityacp Date: Fri, 4 Dec 2020 11:10:12 +0530 Subject: Fix tests --- stats/test_models.py | 8 ++++---- stats/tests.py | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) (limited to 'stats') diff --git a/stats/test_models.py b/stats/test_models.py index 5506a64..7f84330 100644 --- a/stats/test_models.py +++ b/stats/test_models.py @@ -70,7 +70,7 @@ class TrackLessonTestCase(TestCase): def test_get_current_time(self): # Given tracker = self.tracker - expected_time = 'just started' + expected_time = '00:00:00' # When current_time = tracker.get_current_time() @@ -81,7 +81,7 @@ class TrackLessonTestCase(TestCase): def test_get_video_duration(self): # Given tracker = self.tracker - expected_duration = 'will be available after 25% completion' + expected_duration = '00:00:00' # When duration = tracker.get_video_duration() @@ -117,7 +117,7 @@ class TrackLessonTestCase(TestCase): def test_get_percentage_complete(self): # Given tracker = self.tracker - expected_percentage = 'less than 25%' + expected_percentage = 0 # When percentage = tracker.get_percentage_complete() @@ -126,7 +126,7 @@ class TrackLessonTestCase(TestCase): self.assertEqual(percentage, expected_percentage) # Given - expected_percentage = 'approx 75 %' + expected_percentage = 75 # When tracker.set_current_time('00:03:00') diff --git a/stats/tests.py b/stats/tests.py index c256feb..540ff4d 100644 --- a/stats/tests.py +++ b/stats/tests.py @@ -131,8 +131,8 @@ class TestTrackLesson(TestCase): self.assertEqual(response_data.get('total'), 1) expected_tracker = list(TrackLesson.objects.filter( user_id=self.student.id, course_id=self.course.id, - lesson_id=self.lesson.id).values_list("id", flat=True)) + lesson_id=self.lesson.id)) obtained_tracker = list(response_data.get( - 'objects').object_list.values_list("id", flat=True)) + 'objects').object_list) self.assertEqual(obtained_tracker, expected_tracker) -- cgit