Skip to content

Commit

Permalink
Show graphs for the lesson video statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
adityacp committed Dec 3, 2020
1 parent 3a442d0 commit e461b7c
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 21 deletions.
28 changes: 23 additions & 5 deletions stats/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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')

Expand All @@ -41,12 +59,12 @@ def get_log_counter(self):

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):
Expand All @@ -58,13 +76,13 @@ def set_current_time(self, ct):

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):
Expand All @@ -78,7 +96,7 @@ def set_watched(self):
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):
Expand Down
59 changes: 55 additions & 4 deletions stats/templates/view_lesson_tracking.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% extends "manage.html" %}
{% load static %}
{% block title %} Lesson Views {% endblock %}
{% block title %} Lesson Video Stats {% endblock %}
{% block script %}
<script type="text/javascript" src="{% static 'yaksh/js/jquery.tablesorter.min.js' %}">
</script>
Expand Down Expand Up @@ -33,7 +33,58 @@ <h3>Statistics for {% with trackings|first as entry %} {{entry.lesson}} {% endwi
<br><br>
{% include "yaksh/paginator.html" %}
<br>
<h4><strong>{{total}} student(s) viewed this lesson</strong></h4>
<div class="row">
<div class="col" id='barDiv1'></div>
<div class="col" id="barDiv2"></div>
<div class="col" id="barDiv3"></div>
</div>
<script type="text/javascript">
var config = {responsive: true}
var data = [
{
x: ["Completed", "Not Completed"],
y: ["{{completion.0}}", "{{completion.1}}"],
type: 'bar'
}
];
var layout = {
title: "Number of completions (Out of {{visits.2}})",
xaxis: {title: 'Completion status'},
yaxis: {title: 'Count'},
width: 400,
height: 400,
};
Plotly.newPlot('barDiv1', data, layout, config);
var data = [
{
x: ["Visited", "Not Visited"],
y: ["{{visits.0}}", "{{visits.1}}"],
type: 'bar'
}
];
var layout = {
title: "Number of visits (Out of {{visits.2}})",
xaxis: {title: 'Visit status'},
yaxis: {title: 'Count'},
width: 400,
height: 400,
};
Plotly.newPlot('barDiv2', data, layout, config);
var x_data = ["0-25", "25-50", "50-75", "75-100"], y_data = [];
{% for i, j in percentage_data.items %}
y_data.push("{{j}}")
{% endfor %}
var data = [{x: x_data, y: y_data, type: 'bar'}];
var layout = {
title: "Range wise completion (Out of {{total}})",
xaxis: {title: 'Percentage Range'},
yaxis: {title: 'Count'},
width: 400,
height: 400,
};
Plotly.newPlot('barDiv3', data, layout, config);
</script>
<br>
<table class="table table-responsive" id="stats-table">
<thead>
<tr>
Expand All @@ -57,13 +108,13 @@ <h4><strong>{{total}} student(s) viewed this lesson</strong></h4>
<td>{{track.creation_time}}</td>
<td>{{track.get_current_time}}</td>
<td>{{track.get_video_duration}}</td>
<td>{{track.get_percentage_complete}}</td>
<td>{{track.get_percentage_complete}} %</td>
<td>
{% with track.get_watched as watched %}
{% if watched %}
<span class="badge-pill badge-success">{{watched}}</span>
{% else %}
<span class="badge-pill badge-success">{{watched}}</span>
<span class="badge-pill badge-warning">{{watched}}</span>
{% endif %}
{% endwith %}
</td>
Expand Down
25 changes: 17 additions & 8 deletions stats/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
6 changes: 3 additions & 3 deletions yaksh/templates/yaksh/show_lesson_statistics.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% extends "manage.html" %}
{% load static %}
{% load custom_filters %}
{% block title %} Lesson Statistics {% endblock %}
{% block title %} Lesson Quiz Stats {% endblock %}
{% block pagetitle %} Statistics for {{lesson}} {% endblock %}
{% block script %}
<script type="text/javascript" src="{% static 'yaksh/js/jquery.tablesorter.min.js' %}">
Expand Down Expand Up @@ -135,8 +135,8 @@
<div class="progress" style="width: 30%">
{% if percent %}
<div class="progress-bar bg-success" role="progressbar" aria-valuenow="{{percent}}"
aria-valuemin="0" aria-valuemax="100" style="width:{{percent}}%">
<b style="color: white;">{{percent}}%</b>
aria-valuemin="0" aria-valuemax="100" style="width:{{percent|floatformat}}%">
<b style="color: white;">{{percent|floatformat}}%</b>
</div>
{% else %}
<b style="color: black;">0%</b>
Expand Down
2 changes: 1 addition & 1 deletion yaksh/templatetags/custom_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,5 +209,5 @@ def get_tc_percent(tc_id, data):
def get_lesson_views(course_id, lesson_id):
course = Course.objects.get(id=course_id)
return TrackLesson.objects.filter(
course_id=course_id, lesson_id=lesson_id
course_id=course_id, lesson_id=lesson_id, watched=True
).count(), course.students.count()

0 comments on commit e461b7c

Please sign in to comment.