From fe5b3c41aa898fa7491a7ec9bce28c5e1c5b542d Mon Sep 17 00:00:00 2001 From: adityacp Date: Fri, 6 Nov 2020 18:21:48 +0530 Subject: Statistics app for video tracking --- stats/__init__.py | 0 stats/admin.py | 8 ++++ stats/apps.py | 5 +++ stats/models.py | 24 +++++++++++ stats/templates/view_lesson_tracking.html | 69 +++++++++++++++++++++++++++++++ stats/tests.py | 3 ++ stats/urls.py | 12 ++++++ stats/views.py | 54 ++++++++++++++++++++++++ 8 files changed, 175 insertions(+) create mode 100644 stats/__init__.py create mode 100644 stats/admin.py create mode 100644 stats/apps.py create mode 100644 stats/models.py create mode 100644 stats/templates/view_lesson_tracking.html create mode 100644 stats/tests.py create mode 100644 stats/urls.py create mode 100644 stats/views.py diff --git a/stats/__init__.py b/stats/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/stats/admin.py b/stats/admin.py new file mode 100644 index 0000000..b400d27 --- /dev/null +++ b/stats/admin.py @@ -0,0 +1,8 @@ +# Django Imports +from django.contrib import admin + +# Local Imports +from stats.models import TrackLesson + + +admin.site.register(TrackLesson) diff --git a/stats/apps.py b/stats/apps.py new file mode 100644 index 0000000..2d09b92 --- /dev/null +++ b/stats/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class StatsConfig(AppConfig): + name = 'stats' diff --git a/stats/models.py b/stats/models.py new file mode 100644 index 0000000..f2f1bce --- /dev/null +++ b/stats/models.py @@ -0,0 +1,24 @@ +# Django Imports +from django.db import models +from django.utils import timezone +from django.contrib.auth.models import User + +# Local Imports +from yaksh.models import Course, Lesson + + +class TrackLesson(models.Model): + user = models.ForeignKey(User, on_delete=models.CASCADE) + course = models.ForeignKey(Course, on_delete=models.CASCADE) + lesson = models.ForeignKey(Lesson, on_delete=models.CASCADE) + current_time = models.CharField(max_length=100, default="00:00:00") + video_duration = models.CharField(max_length=100, default="00:00:00") + last_access_time = models.DateTimeField(default=timezone.now) + creation_time = models.DateTimeField(auto_now_add=True) + + class Meta: + unique_together = ('user', 'course', 'lesson') + + def __str__(self): + return (f"Track {self.lesson} in {self.course} " + f"for {self.user.get_full_name()}") diff --git a/stats/templates/view_lesson_tracking.html b/stats/templates/view_lesson_tracking.html new file mode 100644 index 0000000..fd87d70 --- /dev/null +++ b/stats/templates/view_lesson_tracking.html @@ -0,0 +1,69 @@ +{% extends "manage.html" %} + +{% block title %} Lesson Views {% endblock %} +{% block script %} + +{% endblock %} +{% block content %} +
+ {% with objects.object_list as trackings %} +
+

Statistics for {% with trackings|first as entry %} {{entry.lesson}} {% endwith %}

+
+ +  Back + +

+ {% include "yaksh/paginator.html" %} +
+

{{total}} student(s) viewed this lesson

+ + + + + + + + + + + {% for track in trackings %} + + + + + + + + + + {% endfor %} +
Sr No.Student NameLast access onStarted onCurrent TimeVideo DurationPercentage watched
{{ forloop.counter0|add:objects.start_index }}{{track.user.get_full_name}}{{track.last_access_time}}{{track.creation_time}}{{track.current_time}}{{track.video_duration}} + +
+ {% endwith %} +
+ {% include "yaksh/paginator.html" %} +
+{% endblock %} \ No newline at end of file diff --git a/stats/tests.py b/stats/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/stats/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/stats/urls.py b/stats/urls.py new file mode 100644 index 0000000..f11148f --- /dev/null +++ b/stats/urls.py @@ -0,0 +1,12 @@ +from django.urls import path +from stats import views + + +app_name = "stats" + +urlpatterns = [ + path('submit/video/watch/', + views.add_tracker, name='add_tracker'), + path('view/watch/stats//', + views.view_lesson_watch_stats, name='view_lesson_watch_stats'), +] diff --git a/stats/views.py b/stats/views.py new file mode 100644 index 0000000..ddbc1b3 --- /dev/null +++ b/stats/views.py @@ -0,0 +1,54 @@ +# Django Imports +from django.shortcuts import render, get_object_or_404 +from django.http import JsonResponse +from django.utils import timezone +from django.contrib.auth.decorators import login_required +from django.core.paginator import Paginator + +# Local Imports +from stats.models import TrackLesson +from yaksh.models import Course +from yaksh.decorators import email_verified + + +@login_required +@email_verified +def add_tracker(request, tracker_id): + user = request.user + track = get_object_or_404( + TrackLesson.objects.select_related("course"), id=tracker_id + ) + if not track.course.is_student(user): + raise Http404("You are not enrolled in this course") + context = {} + video_duration = request.POST.get('video_duration') + current_time = request.POST.get('current_video_time') + if current_time: + track.current_time = current_time + track.video_duration = video_duration + track.last_access_time = timezone.now() + track.save() + success = True + else: + success = False + context = {"success": success} + return JsonResponse(context) + + +@login_required +@email_verified +def view_lesson_watch_stats(request, course_id, lesson_id): + user = request.user + 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') + trackings = TrackLesson.objects.get_queryset().filter( + course_id=course_id, lesson_id=lesson_id + ).order_by("id") + total = trackings.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} + return render(request, 'view_lesson_tracking.html', context) -- cgit