diff options
author | Palaparthy Adityachandra | 2020-11-07 19:07:36 +0530 |
---|---|---|
committer | GitHub | 2020-11-07 19:07:36 +0530 |
commit | 39a13424ad5b5d59044bec27530bdad1ccf12c25 (patch) | |
tree | 886f3277e1f2399eafa8ff596c72c904aaae18f8 /stats | |
parent | 5d320e054cd125582c56a6c25a70ba57f1cccbce (diff) | |
parent | d09ff51b6c957137e705fee73f1808c6333eed7f (diff) | |
download | online_test-39a13424ad5b5d59044bec27530bdad1ccf12c25.tar.gz online_test-39a13424ad5b5d59044bec27530bdad1ccf12c25.tar.bz2 online_test-39a13424ad5b5d59044bec27530bdad1ccf12c25.zip |
Merge pull request #794 from adityacp/video_tracking
Basic tracking for video lessons
Diffstat (limited to 'stats')
-rw-r--r-- | stats/__init__.py | 0 | ||||
-rw-r--r-- | stats/admin.py | 14 | ||||
-rw-r--r-- | stats/apps.py | 5 | ||||
-rw-r--r-- | stats/models.py | 35 | ||||
-rw-r--r-- | stats/templates/view_lesson_tracking.html | 75 | ||||
-rw-r--r-- | stats/tests.py | 138 | ||||
-rw-r--r-- | stats/urls.py | 12 | ||||
-rw-r--r-- | stats/views.py | 57 |
8 files changed, 336 insertions, 0 deletions
diff --git a/stats/__init__.py b/stats/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/stats/__init__.py diff --git a/stats/admin.py b/stats/admin.py new file mode 100644 index 0000000..16ca528 --- /dev/null +++ b/stats/admin.py @@ -0,0 +1,14 @@ +# Django Imports +from django.contrib import admin + +# Local Imports +from stats.models import TrackLesson + + +class TrackLessonAdmin(admin.ModelAdmin): + search_fields = ['user__first_name', 'user__last_name', 'user__username', + 'course__name', 'lesson__name'] + readonly_fields = ["course", "user", "lesson"] + + +admin.site.register(TrackLesson, TrackLessonAdmin) 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..95def40 --- /dev/null +++ b/stats/models.py @@ -0,0 +1,35 @@ +# 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") + creation_time = models.DateTimeField(auto_now_add=True) + + class Meta: + unique_together = ('user', 'course', 'lesson') + + def get_last_access_time_and_vists(self): + lesson_logs = self.lessonlog_set + last_access_time = None + if lesson_logs.exists(): + last_access_time = lesson_logs.last().last_access_time + return last_access_time, lesson_logs.count() + + def __str__(self): + return (f"Track {self.lesson} in {self.course} " + f"for {self.user.get_full_name()}") + + +class LessonLog(models.Model): + track = models.ForeignKey(TrackLesson, on_delete=models.CASCADE) + last_access_time = models.DateTimeField(default=timezone.now) diff --git a/stats/templates/view_lesson_tracking.html b/stats/templates/view_lesson_tracking.html new file mode 100644 index 0000000..7962410 --- /dev/null +++ b/stats/templates/view_lesson_tracking.html @@ -0,0 +1,75 @@ +{% extends "manage.html" %} +{% load static %} +{% block title %} Lesson Views {% endblock %} +{% block script %} +<script type="text/javascript" src="{% static 'yaksh/js/jquery.tablesorter.min.js' %}"> +</script> +<script type="text/javascript"> + function get_time_in_seconds(time) { + var time = time.split(":"); + var hh = parseInt(time[0]); + var mm = parseInt(time[1]); + var ss = parseInt(time[2]); + return hh * 3600 + mm * 60 + ss; + } + + $(document).ready(function() { + $("#stats-table").tablesorter({}); + $('#stats-table tr').each(function() { + var td = $(this).find("td"); + var elapsed = td.eq(4).html(); + var duration = td.eq(5).html(); + if (elapsed != undefined || duration != undefined) { + percent = (get_time_in_seconds(elapsed) / get_time_in_seconds(duration)) * 100; + td.eq(6).html(Math.round(percent)); + } + }); + }); +</script> +{% endblock %} +{% block content %} +<div class="container"> + {% with objects.object_list as trackings %} + <center> + <h3>Statistics for {% with trackings|first as entry %} {{entry.lesson}} {% endwith %}</h3> + </center> + <a class="btn btn-primary" href="{% url 'yaksh:lesson_statistics' course_id lesson_id %}"> + <i class="fa fa-arrow-left"></i> Back + </a> + <br><br> + {% include "yaksh/paginator.html" %} + <br> + <h4><strong>{{total}} student(s) viewed this lesson</strong></h4> + <table class="table table-responsive" id="stats-table"> + <thead> + <tr> + <th>Sr No.</th> + <th>Student Name <i class="fa fa-sort"></i></th> + <th>Last access on <i class="fa fa-sort"></i></th> + <th>Started on <i class="fa fa-sort"></i></th> + <th>Current Duration <i class="fa fa-sort"></i></th> + <th>Video Duration <i class="fa fa-sort"></i></th> + <th>Percentage watched <i class="fa fa-sort"></i></th> + <th>Total visits <i class="fa fa-sort"></i></th> + </tr> + </thead> + {% for track in trackings %} + <tr> + <td>{{ forloop.counter0|add:objects.start_index }}</td> + <td>{{track.user.get_full_name}}</td> + {% with track.get_last_access_time_and_vists as time_and_visits %} + <td>{{time_and_visits.0}}</td> + <td>{{track.creation_time}}</td> + <td>{{track.current_time}}</td> + <td>{{track.video_duration}}</td> + <td></td> + <td>{{time_and_visits.1}}</td> + {% endwith %} + </tr> + {% endfor %} + </table> + {% endwith %} + <br> + {% include "yaksh/paginator.html" %} +</div> +{% endblock %}
\ No newline at end of file diff --git a/stats/tests.py b/stats/tests.py new file mode 100644 index 0000000..c256feb --- /dev/null +++ b/stats/tests.py @@ -0,0 +1,138 @@ +# Python Imports +import json + +# Django Imports +from django.test import TestCase, Client +from django.contrib.auth.models import User, Group +from django.urls import reverse + +# Local Imports +from stats.models import TrackLesson +from yaksh.models import Course, Lesson, LearningUnit, LearningModule + + +class TestTrackLesson(TestCase): + def setUp(self): + self.client = Client() + self.mod_group, created = Group.objects.get_or_create(name='moderator') + self.user_plaintext_pass = 'demo' + self.user = User.objects.create_user( + username='demo_user', + password=self.user_plaintext_pass, + first_name='first_name', + last_name='last_name', + email='demo@test.com', + ) + + # Create Student + self.student_plaintext_pass = 'demo_student' + self.student = User.objects.create_user( + username='demo_student', + password=self.student_plaintext_pass, + first_name='student_first_name', + last_name='student_last_name', + email='demo_student@test.com' + ) + + # Add to moderator group + self.mod_group.user_set.add(self.user) + + self.course = Course.objects.create( + name="Test_course", + enrollment="Open Enrollment", creator=self.user + ) + self.lesson = Lesson.objects.create( + name="Test_lesson", description="test description", + creator=self.user) + self.learning_unit = LearningUnit.objects.create( + order=0, type="lesson", lesson=self.lesson + ) + self.learning_module = LearningModule.objects.create( + order=0, name="Test_module", description="Demo module", + check_prerequisite=False, creator=self.user + ) + self.learning_module.learning_unit.add(self.learning_unit.id) + self.track = TrackLesson.objects.create( + user_id=self.student.id, course_id=self.course.id, + lesson_id=self.lesson.id + ) + + def tearDown(self): + self.client.logout() + self.mod_group.delete() + self.user.delete() + self.student.delete() + self.course.delete() + self.learning_unit.delete() + self.learning_module.delete() + + def test_add_video_track(self): + self.client.login( + username=self.student.username, + password=self.student_plaintext_pass + ) + + # Student not enrolled in the course fails to add the tracking + response = self.client.post( + reverse('stats:add_tracker', + kwargs={"tracker_id": self.track.id}), + data={'video_duration': '00:05:00'} + ) + self.assertEqual(response.status_code, 404) + + self.course.students.add(self.student.id) + # No current time given in the post data + response = self.client.post( + reverse('stats:add_tracker', + kwargs={"tracker_id": self.track.id}), + data={'video_duration': '00:05:00'} + ) + self.assertEqual(response.status_code, 200) + self.assertFalse(response.json().get('success')) + + # Valid post data + response = self.client.post( + reverse('stats:add_tracker', + kwargs={"tracker_id": self.track.id}), + data={'video_duration': '00:05:00', + 'current_video_time': '00:01:00'} + ) + + self.assertEqual(response.status_code, 200) + self.assertTrue(response.json().get('success')) + + def test_disallow_student_view_tracking(self): + self.client.login( + username=self.student.username, + password=self.student_plaintext_pass + ) + + # Fails to view the lesson data for student + response = self.client.get( + reverse('stats:view_lesson_watch_stats', + kwargs={"course_id": self.course.id, + "lesson_id": self.lesson.id}) + ) + self.assertEqual(response.status_code, 404) + + def test_allow_moderator_view_tracking(self): + self.client.login( + username=self.user.username, + password=self.user_plaintext_pass + ) + # Course creator can view the lesson data + response = self.client.get( + reverse('stats:view_lesson_watch_stats', + kwargs={"course_id": self.course.id, + "lesson_id": self.lesson.id}) + ) + response_data = response.context + self.assertEqual(response.status_code, 200) + 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)) + obtained_tracker = list(response_data.get( + 'objects').object_list.values_list("id", flat=True)) + self.assertEqual(obtained_tracker, expected_tracker) + 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/<int:tracker_id>', + views.add_tracker, name='add_tracker'), + path('view/watch/stats/<int:course_id>/<int:lesson_id>', + 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..f7c028f --- /dev/null +++ b/stats/views.py @@ -0,0 +1,57 @@ +# 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 +from django.http import Http404 + +# Local Imports +from stats.models import TrackLesson, LessonLog +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 + LessonLog.objects.create( + track_id=track.id, 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) |